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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
/*
* 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 "parent_def.h"
#include "fields/all_fields.h"
#include "util.h"
ParentDef::ParentDef(std::string name, FieldList fields) : ParentDef(name, fields, nullptr) {}
ParentDef::ParentDef(std::string name, FieldList fields, ParentDef* parent)
: TypeDef(name), fields_(fields), parent_(parent) {}
void ParentDef::AddParentConstraint(std::string field_name, std::variant<int64_t, std::string> value) {
// NOTE: This could end up being very slow if there are a lot of constraints.
const auto& parent_params = parent_->GetParamList();
const auto& constrained_field = parent_params.GetField(field_name);
if (constrained_field == nullptr) {
ERROR() << "Attempting to constrain field " << field_name << " in parent " << parent_->name_
<< ", but no such field exists.";
}
if (constrained_field->GetFieldType() == ScalarField::kFieldType) {
if (!std::holds_alternative<int64_t>(value)) {
ERROR(constrained_field) << "Attempting to constrain a scalar field to an enum value in " << parent_->name_;
}
} else if (constrained_field->GetFieldType() == EnumField::kFieldType) {
if (!std::holds_alternative<std::string>(value)) {
ERROR(constrained_field) << "Attempting to constrain an enum field to a scalar value in " << parent_->name_;
}
const auto& enum_def = static_cast<EnumField*>(constrained_field)->GetEnumDef();
if (!enum_def.HasEntry(std::get<std::string>(value))) {
ERROR(constrained_field) << "No matching enumeration \"" << std::get<std::string>(value)
<< "\" for constraint on enum in parent " << parent_->name_ << ".";
}
// For enums, we have to qualify the value using the enum type name.
value = enum_def.GetTypeName() + "::" + std::get<std::string>(value);
} else {
ERROR(constrained_field) << "Field in parent " << parent_->name_ << " is not viable for constraining.";
}
parent_constraints_.insert(std::pair(field_name, value));
}
void ParentDef::AddTestCase(std::string packet_bytes) {
test_cases_.insert(std::move(packet_bytes));
}
// Assign all size fields to their corresponding variable length fields.
// Will crash if
// - there aren't any fields that don't match up to a field.
// - the size field points to a fixed size field.
// - if the size field comes after the variable length field.
void ParentDef::AssignSizeFields() {
for (const auto& field : fields_) {
DEBUG() << "field name: " << field->GetName();
if (field->GetFieldType() != SizeField::kFieldType && field->GetFieldType() != CountField::kFieldType) {
continue;
}
const SizeField* size_field = static_cast<SizeField*>(field);
// Check to see if a corresponding field can be found.
const auto& var_len_field = fields_.GetField(size_field->GetSizedFieldName());
if (var_len_field == nullptr) {
ERROR(field) << "Could not find corresponding field for size/count field.";
}
// Do the ordering check to ensure the size field comes before the
// variable length field.
for (auto it = fields_.begin(); *it != size_field; it++) {
DEBUG() << "field name: " << (*it)->GetName();
if (*it == var_len_field) {
ERROR(var_len_field, size_field) << "Size/count field must come before the variable length field it describes.";
}
}
if (var_len_field->GetFieldType() == PayloadField::kFieldType) {
const auto& payload_field = static_cast<PayloadField*>(var_len_field);
payload_field->SetSizeField(size_field);
continue;
}
if (var_len_field->GetFieldType() == BodyField::kFieldType) {
const auto& body_field = static_cast<BodyField*>(var_len_field);
body_field->SetSizeField(size_field);
continue;
}
if (var_len_field->GetFieldType() == VectorField::kFieldType) {
const auto& vector_field = static_cast<VectorField*>(var_len_field);
vector_field->SetSizeField(size_field);
continue;
}
// If we've reached this point then the field wasn't a variable length field.
// Check to see if the field is a variable length field
ERROR(field, size_field) << "Can not use size/count in reference to a fixed size field.\n";
}
}
void ParentDef::SetEndianness(bool is_little_endian) {
is_little_endian_ = is_little_endian;
}
// Get the size. You scan specify without_payload in order to exclude payload fields as children will be overriding it.
Size ParentDef::GetSize(bool without_payload) const {
auto size = Size(0);
for (const auto& field : fields_) {
if (without_payload &&
(field->GetFieldType() == PayloadField::kFieldType || field->GetFieldType() == BodyField::kFieldType)) {
continue;
}
// The offset to the field must be passed in as an argument for dynamically sized custom fields.
if (field->GetFieldType() == CustomField::kFieldType && field->GetSize().has_dynamic()) {
std::stringstream custom_field_size;
// Custom fields are special as their size field takes an argument.
custom_field_size << field->GetSize().dynamic_string() << "(begin()";
// Check if we can determine offset from begin(), otherwise error because by this point,
// the size of the custom field is unknown and can't be subtracted from end() to get the
// offset.
auto offset = GetOffsetForField(field->GetName(), false);
if (offset.empty()) {
ERROR(field) << "Custom Field offset can not be determined from begin().";
}
if (offset.bits() % 8 != 0) {
ERROR(field) << "Custom fields must be byte aligned.";
}
if (offset.has_bits()) custom_field_size << " + " << offset.bits() / 8;
if (offset.has_dynamic()) custom_field_size << " + " << offset.dynamic_string();
custom_field_size << ")";
size += custom_field_size.str();
continue;
}
size += field->GetSize();
}
if (parent_ != nullptr) {
size += parent_->GetSize(true);
}
return size;
}
// Get the offset until the field is reached, if there is no field
// returns an empty Size. from_end requests the offset to the field
// starting from the end() iterator. If there is a field with an unknown
// size along the traversal, then an empty size is returned.
Size ParentDef::GetOffsetForField(std::string field_name, bool from_end) const {
// Check first if the field exists.
if (fields_.GetField(field_name) == nullptr) {
ERROR() << "Can't find a field offset for nonexistent field named: " << field_name << " in " << name_;
}
PacketField* padded_field = nullptr;
{
PacketField* last_field = nullptr;
for (const auto field : fields_) {
if (field->GetFieldType() == PaddingField::kFieldType) {
padded_field = last_field;
}
last_field = field;
}
}
// We have to use a generic lambda to conditionally change iteration direction
// due to iterator and reverse_iterator being different types.
auto size_lambda = [&](auto from, auto to) -> Size {
auto size = Size(0);
for (auto it = from; it != to; it++) {
// We've reached the field, end the loop.
if ((*it)->GetName() == field_name) break;
const auto& field = *it;
// If there is a field with an unknown size before the field, return an empty Size.
if (field->GetSize().empty() && padded_field != field) {
return Size();
}
if (field != padded_field) {
if (!from_end || field->GetFieldType() != PaddingField::kFieldType) {
size += field->GetSize();
}
}
}
return size;
};
// Change iteration direction based on from_end.
auto size = Size();
if (from_end)
size = size_lambda(fields_.rbegin(), fields_.rend());
else
size = size_lambda(fields_.begin(), fields_.end());
if (size.empty()) return size;
// We need the offset until a payload or body field.
if (parent_ != nullptr) {
if (parent_->fields_.HasPayload()) {
auto parent_payload_offset = parent_->GetOffsetForField("payload", from_end);
if (parent_payload_offset.empty()) {
ERROR() << "Empty offset for payload in " << parent_->name_ << " finding the offset for field: " << field_name;
}
size += parent_payload_offset;
} else {
auto parent_body_offset = parent_->GetOffsetForField("body", from_end);
if (parent_body_offset.empty()) {
ERROR() << "Empty offset for body in " << parent_->name_ << " finding the offset for field: " << field_name;
}
size += parent_body_offset;
}
}
return size;
}
FieldList ParentDef::GetParamList() const {
FieldList params;
std::set<std::string> param_types = {
ScalarField::kFieldType,
EnumField::kFieldType,
ArrayField::kFieldType,
VectorField::kFieldType,
CustomField::kFieldType,
StructField::kFieldType,
VariableLengthStructField::kFieldType,
PayloadField::kFieldType,
};
if (parent_ != nullptr) {
auto parent_params = parent_->GetParamList().GetFieldsWithTypes(param_types);
// Do not include constrained fields in the params
for (const auto& field : parent_params) {
if (parent_constraints_.find(field->GetName()) == parent_constraints_.end()) {
params.AppendField(field);
}
}
}
// Add our parameters.
return params.Merge(fields_.GetFieldsWithTypes(param_types));
}
void ParentDef::GenMembers(std::ostream& s) const {
// Add the parameter list.
for (const auto& field : fields_) {
if (field->GenBuilderMember(s)) {
s << "_{};";
}
}
}
void ParentDef::GenSize(std::ostream& s) const {
auto header_fields = fields_.GetFieldsBeforePayloadOrBody();
auto footer_fields = fields_.GetFieldsAfterPayloadOrBody();
Size padded_size;
const PacketField* padded_field = nullptr;
const PacketField* last_field = nullptr;
for (const auto& field : fields_) {
if (field->GetFieldType() == PaddingField::kFieldType) {
if (!padded_size.empty()) {
ERROR() << "Only one padding field is allowed. Second field: " << field->GetName();
}
padded_field = last_field;
padded_size = field->GetSize();
}
last_field = field;
}
s << "protected:";
s << "size_t BitsOfHeader() const {";
s << "return 0";
if (parent_ != nullptr) {
if (parent_->GetDefinitionType() == Type::PACKET) {
s << " + " << parent_->name_ << "Builder::BitsOfHeader() ";
} else {
s << " + " << parent_->name_ << "::BitsOfHeader() ";
}
}
for (const auto& field : header_fields) {
if (field == padded_field) {
s << " + " << padded_size;
} else {
s << " + " << field->GetBuilderSize();
}
}
s << ";";
s << "}\n\n";
s << "size_t BitsOfFooter() const {";
s << "return 0";
for (const auto& field : footer_fields) {
if (field == padded_field) {
s << " + " << padded_size;
} else {
s << " + " << field->GetBuilderSize();
}
}
if (parent_ != nullptr) {
if (parent_->GetDefinitionType() == Type::PACKET) {
s << " + " << parent_->name_ << "Builder::BitsOfFooter() ";
} else {
s << " + " << parent_->name_ << "::BitsOfFooter() ";
}
}
s << ";";
s << "}\n\n";
if (fields_.HasPayload()) {
s << "size_t GetPayloadSize() const {";
s << "if (payload_ != nullptr) {return payload_->size();}";
s << "else { return size() - (BitsOfHeader() + BitsOfFooter()) / 8;}";
s << ";}\n\n";
}
s << "public:";
s << "virtual size_t size() const override {";
s << "return (BitsOfHeader() / 8)";
if (fields_.HasPayload()) {
s << "+ payload_->size()";
}
if (fields_.HasBody()) {
for (const auto& field : header_fields) {
if (field->GetFieldType() == SizeField::kFieldType) {
const auto& field_name = ((SizeField*)field)->GetSizedFieldName();
if (field_name == "body") {
s << "+ body_size_extracted_";
}
}
}
}
s << " + (BitsOfFooter() / 8);";
s << "}\n";
}
void ParentDef::GenSerialize(std::ostream& s) const {
auto header_fields = fields_.GetFieldsBeforePayloadOrBody();
auto footer_fields = fields_.GetFieldsAfterPayloadOrBody();
s << "protected:";
s << "void SerializeHeader(BitInserter&";
if (parent_ != nullptr || header_fields.size() != 0) {
s << " i ";
}
s << ") const {";
if (parent_ != nullptr) {
if (parent_->GetDefinitionType() == Type::PACKET) {
s << parent_->name_ << "Builder::SerializeHeader(i);";
} else {
s << parent_->name_ << "::SerializeHeader(i);";
}
}
const PacketField* padded_field = nullptr;
{
PacketField* last_field = nullptr;
for (const auto field : header_fields) {
if (field->GetFieldType() == PaddingField::kFieldType) {
padded_field = last_field;
}
last_field = field;
}
}
for (const auto& field : header_fields) {
if (field->GetFieldType() == SizeField::kFieldType) {
const auto& field_name = ((SizeField*)field)->GetSizedFieldName();
const auto& sized_field = fields_.GetField(field_name);
if (sized_field == nullptr) {
ERROR(field) << __func__ << ": Can't find sized field named " << field_name;
}
if (sized_field->GetFieldType() == PayloadField::kFieldType) {
s << "size_t payload_bytes = GetPayloadSize();";
std::string modifier = ((PayloadField*)sized_field)->size_modifier_;
if (modifier != "") {
s << "static_assert((" << modifier << ")%8 == 0, \"Modifiers must be byte-aligned\");";
s << "payload_bytes = payload_bytes + (" << modifier << ") / 8;";
}
s << "ASSERT(payload_bytes < (static_cast<size_t>(1) << " << field->GetSize().bits() << "));";
s << "insert(static_cast<" << field->GetDataType() << ">(payload_bytes), i," << field->GetSize().bits() << ");";
} else if (sized_field->GetFieldType() == BodyField::kFieldType) {
s << field->GetName() << "_extracted_ = 0;";
s << "size_t local_size = " << name_ << "::size();";
s << "ASSERT((size() - local_size) < (static_cast<size_t>(1) << " << field->GetSize().bits() << "));";
s << "insert(static_cast<" << field->GetDataType() << ">(size() - local_size), i," << field->GetSize().bits()
<< ");";
} else {
if (sized_field->GetFieldType() != VectorField::kFieldType) {
ERROR(field) << __func__ << ": Unhandled sized field type for " << field_name;
}
const auto& vector_name = field_name + "_";
const VectorField* vector = (VectorField*)sized_field;
s << "size_t " << vector_name + "bytes = 0;";
if (vector->element_size_.empty() || vector->element_size_.has_dynamic()) {
s << "for (auto elem : " << vector_name << ") {";
s << vector_name + "bytes += elem.size(); }";
} else {
s << vector_name + "bytes = ";
s << vector_name << ".size() * ((" << vector->element_size_ << ") / 8);";
}
std::string modifier = vector->GetSizeModifier();
if (modifier != "") {
s << "static_assert((" << modifier << ")%8 == 0, \"Modifiers must be byte-aligned\");";
s << vector_name << "bytes = ";
s << vector_name << "bytes + (" << modifier << ") / 8;";
}
s << "ASSERT(" << vector_name + "bytes < (1 << " << field->GetSize().bits() << "));";
s << "insert(" << vector_name << "bytes, i, ";
s << field->GetSize().bits() << ");";
}
} else if (field->GetFieldType() == ChecksumStartField::kFieldType) {
const auto& field_name = ((ChecksumStartField*)field)->GetStartedFieldName();
const auto& started_field = fields_.GetField(field_name);
if (started_field == nullptr) {
ERROR(field) << __func__ << ": Can't find checksum field named " << field_name << "(" << field->GetName()
<< ")";
}
s << "auto shared_checksum_ptr = std::make_shared<" << started_field->GetDataType() << ">();";
s << "shared_checksum_ptr->Initialize();";
s << "i.RegisterObserver(packet::ByteObserver(";
s << "[shared_checksum_ptr](uint8_t byte){ shared_checksum_ptr->AddByte(byte);},";
s << "[shared_checksum_ptr](){ return static_cast<uint64_t>(shared_checksum_ptr->GetChecksum());}));";
} else if (field->GetFieldType() == PaddingField::kFieldType) {
s << "ASSERT(unpadded_size <= " << field->GetSize().bytes() << ");";
s << "size_t padding_bytes = ";
s << field->GetSize().bytes() << " - unpadded_size;";
s << "for (size_t padding = 0; padding < padding_bytes; padding++) {i.insert_byte(0);}";
} else if (field->GetFieldType() == CountField::kFieldType) {
const auto& vector_name = ((SizeField*)field)->GetSizedFieldName() + "_";
s << "insert(" << vector_name << ".size(), i, " << field->GetSize().bits() << ");";
} else {
if (field == padded_field) {
s << "size_t unpadded_size = (" << field->GetBuilderSize() << ") / 8;";
}
field->GenInserter(s);
}
}
s << "}\n\n";
s << "void SerializeFooter(BitInserter&";
if (parent_ != nullptr || footer_fields.size() != 0) {
s << " i ";
}
s << ") const {";
for (const auto& field : footer_fields) {
field->GenInserter(s);
}
if (parent_ != nullptr) {
if (parent_->GetDefinitionType() == Type::PACKET) {
s << parent_->name_ << "Builder::SerializeFooter(i);";
} else {
s << parent_->name_ << "::SerializeFooter(i);";
}
}
s << "}\n\n";
s << "public:";
s << "virtual void Serialize(BitInserter& i) const override {";
s << "SerializeHeader(i);";
if (fields_.HasPayload()) {
s << "payload_->Serialize(i);";
}
s << "SerializeFooter(i);";
s << "}\n";
}
void ParentDef::GenInstanceOf(std::ostream& s) const {
if (parent_ != nullptr && parent_constraints_.size() > 0) {
s << "static bool IsInstance(const " << parent_->name_ << "& parent) {";
// Get the list of parent params.
FieldList parent_params = parent_->GetParamList().GetFieldsWithoutTypes({
PayloadField::kFieldType,
BodyField::kFieldType,
});
// Check if constrained parent fields are set to their correct values.
for (const auto& field : parent_params) {
const auto& constraint = parent_constraints_.find(field->GetName());
if (constraint != parent_constraints_.end()) {
s << "if (parent." << field->GetName() << "_ != ";
if (field->GetFieldType() == ScalarField::kFieldType) {
s << std::get<int64_t>(constraint->second) << ")";
s << "{ return false;}";
} else if (field->GetFieldType() == EnumField::kFieldType) {
s << std::get<std::string>(constraint->second) << ")";
s << "{ return false;}";
} else {
ERROR(field) << "Constraints on non enum/scalar fields should be impossible.";
}
}
}
s << "return true;}";
}
}
const ParentDef* ParentDef::GetRootDef() const {
if (parent_ == nullptr) {
return this;
}
return parent_->GetRootDef();
}
std::vector<const ParentDef*> ParentDef::GetAncestors() const {
std::vector<const ParentDef*> res;
auto parent = parent_;
while (parent != nullptr) {
res.push_back(parent);
parent = parent->parent_;
}
std::reverse(res.begin(), res.end());
return res;
}
std::map<std::string, std::variant<int64_t, std::string>> ParentDef::GetAllConstraints() const {
std::map<std::string, std::variant<int64_t, std::string>> res;
res.insert(parent_constraints_.begin(), parent_constraints_.end());
for (auto parent : GetAncestors()) {
res.insert(parent->parent_constraints_.begin(), parent->parent_constraints_.end());
}
return res;
}
bool ParentDef::HasAncestorNamed(std::string name) const {
auto parent = parent_;
while (parent != nullptr) {
if (parent->name_ == name) {
return true;
}
parent = parent->parent_;
}
return false;
}
std::string ParentDef::FindConstraintField() const {
std::string res;
for (const auto& child : children_) {
if (!child->parent_constraints_.empty()) {
return child->parent_constraints_.begin()->first;
}
res = child->FindConstraintField();
}
return res;
}
std::map<const ParentDef*, const std::variant<int64_t, std::string>>
ParentDef::FindDescendantsWithConstraint(
std::string constraint_name) const {
std::map<const ParentDef*, const std::variant<int64_t, std::string>> res;
for (auto const& child : children_) {
auto constraint = child->parent_constraints_.find(constraint_name);
if (constraint != child->parent_constraints_.end()) {
res.insert(std::pair(child, constraint->second));
}
auto m = child->FindDescendantsWithConstraint(constraint_name);
res.insert(m.begin(), m.end());
}
return res;
}
std::vector<const ParentDef*> ParentDef::FindPathToDescendant(std::string descendant) const {
std::vector<const ParentDef*> res;
for (auto const& child : children_) {
auto v = child->FindPathToDescendant(descendant);
if (v.size() > 0) {
res.insert(res.begin(), v.begin(), v.end());
res.push_back(child);
}
if (child->name_ == descendant) {
res.push_back(child);
return res;
}
}
return res;
}
bool ParentDef::HasChildEnums() const {
return !children_.empty() || fields_.HasPayload();
}
void ParentDef::GenRustConformanceCheck(std::ostream& s) const {
auto fields = fields_.GetFieldsWithTypes({
FixedScalarField::kFieldType,
});
for (auto const& field : fields) {
auto start_offset = GetOffsetForField(field->GetName(), false);
auto end_offset = GetOffsetForField(field->GetName(), true);
auto f = (FixedScalarField*)field;
f->GenRustGetter(s, start_offset, end_offset, name_);
s << "if " << f->GetName() << " != ";
f->GenValue(s);
s << " { return false; } ";
}
}
void ParentDef::GenRustWriteToFields(std::ostream& s) const {
auto fields = fields_.GetFieldsWithoutTypes({
BodyField::kFieldType,
PaddingField::kFieldType,
ReservedField::kFieldType,
});
for (auto const& field : fields) {
auto start_field_offset = GetOffsetForField(field->GetName(), false);
auto end_field_offset = GetOffsetForField(field->GetName(), true);
if (start_field_offset.empty() && end_field_offset.empty()) {
ERROR(field) << "Field location for " << field->GetName() << " is ambiguous, "
<< "no method exists to determine field location from begin() or end().\n";
}
if (field->GetFieldType() == SizeField::kFieldType) {
const auto& field_name = ((SizeField*)field)->GetSizedFieldName();
const auto& sized_field = fields_.GetField(field_name);
if (sized_field == nullptr) {
ERROR(field) << __func__ << ": Can't find sized field named " << field_name;
}
if (sized_field->GetFieldType() == PayloadField::kFieldType) {
std::string modifier = ((PayloadField*)sized_field)->size_modifier_;
if (modifier != "") {
ERROR(field) << __func__ << ": size modifiers not implemented yet for " << field_name;
}
s << "let " << field->GetName() << " = " << field->GetRustDataType()
<< "::try_from(self.child.get_total_size()).expect(\"payload size did not fit\");";
} else if (sized_field->GetFieldType() == BodyField::kFieldType) {
s << "let " << field->GetName() << " = " << field->GetRustDataType()
<< "::try_from(self.get_total_size() - self.get_size()).expect(\"payload size did not fit\");";
} else if (sized_field->GetFieldType() == VectorField::kFieldType) {
const auto& vector_name = field_name + "_bytes";
const VectorField* vector = (VectorField*)sized_field;
if (vector->element_size_.empty() || vector->element_size_.has_dynamic()) {
s << "let " << vector_name + " = self." << field_name
<< ".iter().fold(0, |acc, x| acc + x.get_total_size());";
} else {
s << "let " << vector_name + " = self." << field_name << ".len() * ((" << vector->element_size_ << ") / 8);";
}
std::string modifier = vector->GetSizeModifier();
if (modifier != "") {
s << "let " << vector_name << " = " << vector_name << " + (" << modifier.substr(1) << ") / 8;";
}
s << "let " << field->GetName() << " = " << field->GetRustDataType() << "::try_from(" << vector_name
<< ").expect(\"payload size did not fit\");";
} else {
ERROR(field) << __func__ << ": Unhandled sized field type for " << field_name;
}
}
field->GenRustWriter(s, start_field_offset, end_field_offset);
}
}
void ParentDef::GenSizeRetVal(std::ostream& s) const {
int size = 0;
auto fields = fields_.GetFieldsWithoutTypes({
BodyField::kFieldType,
});
const PacketField* padded_field = nullptr;
auto padding_fields = fields_.GetFieldsWithTypes({
PaddingField::kFieldType,
});
if (padding_fields.size()) {
PacketField* last_field = nullptr;
for (const auto field : fields) {
if (field->GetFieldType() == PaddingField::kFieldType) {
padded_field = last_field;
}
last_field = field;
}
}
s << "let ret = 0;";
for (const auto field : fields) {
bool is_vector = field->GetFieldType() == VectorField::kFieldType;
if (field != padded_field) { // Skip the size of padded fields
if (is_vector) {
if (size > 0) {
if (size % 8 != 0) {
ERROR() << "size is not a multiple of 8!\n";
}
s << "let ret = ret + " << size / 8 << ";";
size = 0;
}
const VectorField* vector = (VectorField*)field;
if (vector->element_size_.empty() || vector->element_size_.has_dynamic()) {
s << "let ret = ret + self." << vector->GetName() << ".iter().fold(0, |acc, x| acc + x.get_total_size());";
} else {
s << "let ret = ret + (self." << vector->GetName() << ".len() * ((" << vector->element_size_ << ") / 8));";
}
} else {
size += field->GetSize().bits();
}
} else {
s << "/* Skipping " << field->GetName() << " since it is padded */";
}
}
if (size > 0) {
if (size % 8 != 0) {
ERROR() << "size is not a multiple of 8!\n";
}
s << "let ret = ret + " << size / 8 << ";";
}
s << "ret";
}
|