summaryrefslogtreecommitdiff
path: root/system/gd/packet/parser/fields/scalar_field.cc
blob: 10c85284c7a39f03abf4035a5b462303f18f303b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "fields/scalar_field.h"

#include "fields/fixed_scalar_field.h"
#include "fields/size_field.h"
#include "util.h"

const std::string ScalarField::kFieldType = "ScalarField";

ScalarField::ScalarField(std::string name, int size, ParseLocation loc) : PacketField(name, loc), size_(size) {
  if (size_ > 64 || size_ < 0) {
    ERROR(this) << "Not implemented for size_ = " << size_;
  }
}

const std::string& ScalarField::GetFieldType() const {
  return ScalarField::kFieldType;
}

Size ScalarField::GetSize() const {
  return size_;
}

std::string ScalarField::GetDataType() const {
  return util::GetTypeForSize(size_);
}

int GetShiftBits(int i) {
  int bits_past_byte_boundary = i % 8;
  if (bits_past_byte_boundary == 0) {
    return 0;
  } else {
    return 8 - bits_past_byte_boundary;
  }
}

int ScalarField::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const {
  int num_leading_bits = 0;

  if (!start_offset.empty()) {
    // Default to start if available.
    num_leading_bits = start_offset.bits() % 8;
    s << "auto " << GetName() << "_it = to_bound + (" << start_offset << ") / 8;";
  } else if (!end_offset.empty()) {
    num_leading_bits = GetShiftBits(end_offset.bits() + size.bits());
    Size byte_offset = Size(num_leading_bits + size.bits()) + end_offset;
    s << "auto " << GetName() << "_it = to_bound + (to_bound.NumBytesRemaining() - (" << byte_offset << ") / 8);";
  } else {
    ERROR(this) << "Ambiguous offset for field.";
  }
  return num_leading_bits;
}

void ScalarField::GenExtractor(std::ostream& s, int num_leading_bits, bool) const {
  Size size = GetSize();
  // Extract the correct number of bytes. The return type could be different
  // from the extract type if an earlier field causes the beginning of the
  // current field to start in the middle of a byte.
  std::string extract_type = util::GetTypeForSize(size.bits() + num_leading_bits);
  s << "auto extracted_value = " << GetName() << "_it.extract<" << extract_type << ">();";

  // Right shift the result to remove leading bits.
  if (num_leading_bits != 0) {
    s << "extracted_value >>= " << num_leading_bits << ";";
  }
  // Mask the result if necessary.
  if (util::RoundSizeUp(size.bits()) != size.bits()) {
    uint64_t mask = 0;
    for (int i = 0; i < size.bits(); i++) {
      mask <<= 1;
      mask |= 1;
    }
    s << "extracted_value &= 0x" << std::hex << mask << std::dec << ";";
  }
  s << "*" << GetName() << "_ptr = static_cast<" << GetDataType() << ">(extracted_value);";
}

std::string ScalarField::GetGetterFunctionName() const {
  std::stringstream ss;
  ss << "Get" << util::UnderscoreToCamelCase(GetName());
  return ss.str();
}

void ScalarField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  s << GetDataType() << " " << GetGetterFunctionName() << "() const {";
  s << "ASSERT(was_validated_);";
  s << "auto to_bound = begin();";
  int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
  s << GetDataType() << " " << GetName() << "_value{};";
  s << GetDataType() << "* " << GetName() << "_ptr = &" << GetName() << "_value;";
  GenExtractor(s, num_leading_bits, false);
  s << "return " << GetName() << "_value;";
  s << "}";
}

std::string ScalarField::GetBuilderParameterType() const {
  return GetDataType();
}

bool ScalarField::HasParameterValidator() const {
  return util::RoundSizeUp(GetSize().bits()) != GetSize().bits();
}

void ScalarField::GenParameterValidator(std::ostream& s) const {
  s << "ASSERT(" << GetName() << " < (static_cast<uint64_t>(1) << " << GetSize().bits() << "));";
}

void ScalarField::GenInserter(std::ostream& s) const {
  if (GetSize().bits() == 8) {
    s << "i.insert_byte(" << GetName() << "_);";
  } else {
    s << "insert(" << GetName() << "_, i," << GetSize().bits() << ");";
  }
}

void ScalarField::GenValidator(std::ostream&) const {
  // Do nothing
}

void ScalarField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
  s << "+" << accessor;
}

std::string ScalarField::GetRustDataType() const {
  return util::GetRustTypeForSize(size_);
}

std::string ScalarField::GetRustParseDataType() const {
  return util::GetRustTypeForSize(size_);
}

int ScalarField::GetRustBitOffset(
    std::ostream&, Size start_offset, Size end_offset, Size size) const {
  int num_leading_bits = 0;

  if (!start_offset.empty()) {
    // Default to start if available.
    num_leading_bits = start_offset.bits() % 8;
  } else if (!end_offset.empty()) {
    num_leading_bits = GetShiftBits(end_offset.bits() + size.bits());
    Size byte_offset = Size(num_leading_bits + size.bits()) + end_offset;
  } else {
    ERROR(this) << "Ambiguous offset for field.";
  }
  return num_leading_bits;
}

void ScalarField::GenRustGetter(std::ostream& s, Size start_offset, Size end_offset, std::string parent_name) const {
  Size size = GetSize();

  int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());

  s << "let " << GetName() << " = ";
  auto offset = num_leading_bits == 0 ? 0 : -1;
  s << GetRustParseDataType() << "::from_le_bytes([";
  int total_bytes;
  if (size_ <= 8) {
    total_bytes = 1;
  } else if (size_ <= 16) {
    total_bytes = 2;
  } else if (size_ <= 32) {
    total_bytes = 4;
  } else {
    total_bytes = 8;
  }
  for (int i = 0; i < total_bytes; i++) {
    if (i > 0) {
      s << ",";
    }
    if (i < size.bytes()) {
      s << "bytes[" << start_offset.bytes() + i + offset << "]";
    } else {
      s << 0;
    }
  }
  s << "]);";

  if (num_leading_bits != 0) {
    s << "let " << GetName() << " = " << GetName() << " >> " << num_leading_bits << ";";
  }

  if (util::RoundSizeUp(size.bits()) != size.bits()) {
    uint64_t mask = 0;
    for (int i = 0; i < size.bits(); i++) {
      mask <<= 1;
      mask |= 1;
    }
    s << "let " << GetName() << " = ";
    s << GetName() << " & 0x" << std::hex << mask << std::dec << ";";
  }

  // needs casting from primitive
  if (GetRustParseDataType() != GetRustDataType()) {
    s << "let " << GetName() << " = ";
    s << GetRustDataType() << "::from_" << GetRustParseDataType() << "(" << GetName() << ").ok_or_else(||";
    s << "Error::InvalidEnumValueError {";
    s << "    obj: \"" << parent_name << "\".to_string(),";
    s << "    field: \"" << GetName() << "\".to_string(),";
    s << "    value: " << GetName() << " as u64,";
    s << "    type_: \"" << GetRustDataType() << "\".to_string(),";
    s << "})?;";
  }
}

void ScalarField::GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const {
  Size size = GetSize();
  int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());

  if (GetFieldType() == SizeField::kFieldType || GetFieldType() == FixedScalarField::kFieldType) {
    // Do nothing, the field access has already happened
  } else if (GetRustParseDataType() != GetRustDataType()) {
    // needs casting to primitive
    s << "let " << GetName() << " = self." << GetName() << ".to_" << GetRustParseDataType() << "().unwrap();";
  } else {
    s << "let " << GetName() << " = self." << GetName() << ";";
  }
  if (util::RoundSizeUp(size.bits()) != size.bits()) {
    uint64_t mask = 0;
    for (int i = 0; i < size.bits(); i++) {
      mask <<= 1;
      mask |= 1;
    }
    s << "let " << GetName() << " = ";
    s << GetName() << " & 0x" << std::hex << mask << std::dec << ";";
  }

  int access_offset = 0;
  if (num_leading_bits != 0) {
    access_offset = -1;
    uint64_t mask = 0;
    for (int i = 0; i < num_leading_bits; i++) {
      mask <<= 1;
      mask |= 1;
    }
    s << "let " << GetName() << " = (" << GetName() << " << " << num_leading_bits << ") | ("
      << "(buffer[" << start_offset.bytes() + access_offset << "] as " << GetRustParseDataType() << ") & 0x" << std::hex
      << mask << std::dec << ");";
  }

  s << "buffer[" << start_offset.bytes() + access_offset << ".."
    << start_offset.bytes() + GetSize().bytes() + access_offset << "].copy_from_slice(&" << GetName()
    << ".to_le_bytes()[0.." << size.bytes() << "]);";
}