1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "fields/custom_field_fixed_size.h"
18
19 #include "util.h"
20
21 const std::string CustomFieldFixedSize::kFieldType = "CustomField";
22
CustomFieldFixedSize(std::string name,std::string type_name,int size,ParseLocation loc)23 CustomFieldFixedSize::CustomFieldFixedSize(std::string name, std::string type_name, int size, ParseLocation loc)
24 : ScalarField(name, size, loc), type_name_(type_name) {}
25
GetFieldType() const26 const std::string& CustomFieldFixedSize::GetFieldType() const {
27 return CustomFieldFixedSize::kFieldType;
28 }
29
GetDataType() const30 std::string CustomFieldFixedSize::GetDataType() const {
31 return type_name_;
32 }
33
GetRustDataType() const34 std::string CustomFieldFixedSize::GetRustDataType() const {
35 return type_name_;
36 }
37
GenBounds(std::ostream & s,Size start_offset,Size end_offset,Size size) const38 int CustomFieldFixedSize::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const {
39 if (!start_offset.empty()) {
40 // Default to start if available.
41 s << "auto " << GetName() << "_it = to_bound + (" << start_offset << ") / 8;";
42 } else if (!end_offset.empty()) {
43 Size byte_offset = size + end_offset;
44 s << "auto " << GetName() << "_it = to_bound (+ to_bound.NumBytesRemaining() - (" << byte_offset << ") / 8);";
45 } else {
46 ERROR(this) << "Ambiguous offset for field.";
47 }
48 return 0; // num_leading_bits
49 }
50
GenExtractor(std::ostream & s,int,bool) const51 void CustomFieldFixedSize::GenExtractor(std::ostream& s, int, bool) const {
52 s << "*" << GetName() << "_ptr = " << GetName() << "_it.extract<" << GetDataType() << ">();";
53 }
54
HasParameterValidator() const55 bool CustomFieldFixedSize::HasParameterValidator() const {
56 return false;
57 }
58
GenParameterValidator(std::ostream &) const59 void CustomFieldFixedSize::GenParameterValidator(std::ostream&) const {
60 // Do nothing.
61 }
62
GenInserter(std::ostream & s) const63 void CustomFieldFixedSize::GenInserter(std::ostream& s) const {
64 s << "insert(" << GetName() << "_, i);";
65 }
66
GenValidator(std::ostream &) const67 void CustomFieldFixedSize::GenValidator(std::ostream&) const {
68 // Do nothing.
69 }
70
GenStringRepresentation(std::ostream & s,std::string accessor) const71 void CustomFieldFixedSize::GenStringRepresentation(std::ostream& s, std::string accessor) const {
72 // We assume that custom fields will have a ToString() method
73 s << accessor << ".ToString()";
74 }
75
GetRustParseDataType() const76 std::string CustomFieldFixedSize::GetRustParseDataType() const {
77 return "[u8; " + std::to_string(GetSize().bytes()) + "]";
78 }
79
GenRustGetter(std::ostream & s,Size start_offset,Size end_offset) const80 void CustomFieldFixedSize::GenRustGetter(std::ostream& s, Size start_offset, Size end_offset) const {
81 Size size = GetSize();
82 int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());
83 if (num_leading_bits != 0) {
84 ERROR(this) << "must be byte aligned";
85 }
86 if (size.bits() % 8 != 0) {
87 ERROR(this) << "size must be in full bytes";
88 }
89
90 s << "let " << GetName() << " = bytes[" << start_offset.bytes() << "..";
91 s << start_offset.bytes() + size.bytes() << "].try_into().unwrap();";
92 }
93
GenRustWriter(std::ostream & s,Size start_offset,Size end_offset) const94 void CustomFieldFixedSize::GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const {
95 Size size = GetSize();
96 int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());
97 if (num_leading_bits != 0) {
98 ERROR(this) << "must be byte aligned";
99 }
100 if (size.bits() % 8 != 0) {
101 ERROR(this) << "size must be in full bytes";
102 }
103
104 s << "let " << GetName() << ": " << GetRustParseDataType() << " = self." << GetName() << ".into();";
105 s << "buffer[" << start_offset.bytes() << ".." << start_offset.bytes() + GetSize().bytes() << "].copy_from_slice(&"
106 << GetName() << ");";
107 }
108