• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/payload_field.h"
18 #include "util.h"
19 
20 const std::string PayloadField::kFieldType = "PayloadField";
21 
PayloadField(std::string modifier,ParseLocation loc)22 PayloadField::PayloadField(std::string modifier, ParseLocation loc)
23     : PacketField("payload", loc), size_field_(nullptr), size_modifier_(modifier) {}
24 
SetSizeField(const SizeField * size_field)25 void PayloadField::SetSizeField(const SizeField* size_field) {
26   if (size_field_ != nullptr) {
27     ERROR(this, size_field_, size_field) << "The size field for the payload has already been assigned.";
28   }
29 
30   size_field_ = size_field;
31 }
32 
GetFieldType() const33 const std::string& PayloadField::GetFieldType() const {
34   return PayloadField::kFieldType;
35 }
36 
GetSize() const37 Size PayloadField::GetSize() const {
38   if (size_field_ == nullptr) {
39     if (!size_modifier_.empty()) {
40       ERROR(this) << "Missing size field for payload with size modifier.";
41     }
42     return Size();
43   }
44 
45   std::string dynamic_size = "(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "() * 8)";
46   if (!size_modifier_.empty()) {
47     dynamic_size += "- (" + size_modifier_.substr(1) + " * 8)";
48   }
49 
50   return dynamic_size;
51 }
52 
GetDataType() const53 std::string PayloadField::GetDataType() const {
54   return "PacketView";
55 }
56 
GenExtractor(std::ostream &,int,bool) const57 void PayloadField::GenExtractor(std::ostream&, int, bool) const {
58   ERROR(this) << __func__ << " should never be called. ";
59 }
60 
GetGetterFunctionName() const61 std::string PayloadField::GetGetterFunctionName() const {
62   return "GetPayload";
63 }
64 
GenGetter(std::ostream & s,Size start_offset,Size end_offset) const65 void PayloadField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
66   s << "PacketView<kLittleEndian> " << GetGetterFunctionName() << "() const {";
67   s << "ASSERT(was_validated_);";
68   s << "size_t end_index = size();";
69   s << "auto to_bound = begin();";
70   GenBounds(s, start_offset, end_offset, GetSize());
71   s << "return GetLittleEndianSubview(field_begin, field_end);";
72   s << "}\n\n";
73 }
74 
GetBuilderParameterType() const75 std::string PayloadField::GetBuilderParameterType() const {
76   return "std::unique_ptr<BasePacketBuilder>";
77 }
78 
BuilderParameterMustBeMoved() const79 bool PayloadField::BuilderParameterMustBeMoved() const {
80   return true;
81 }
82 
GenBuilderParameterFromView(std::ostream & s) const83 void PayloadField::GenBuilderParameterFromView(std::ostream& s) const {
84   s << "std::make_unique<RawBuilder>(std::vector<uint8_t>(view.GetPayload().begin(), view.GetPayload().end()))";
85 }
86 
HasParameterValidator() const87 bool PayloadField::HasParameterValidator() const {
88   return false;
89 }
90 
GenParameterValidator(std::ostream &) const91 void PayloadField::GenParameterValidator(std::ostream&) const {
92   // There is no validation needed for a payload
93 }
94 
GenInserter(std::ostream &) const95 void PayloadField::GenInserter(std::ostream&) const {
96   ERROR() << __func__ << " Should never be called.";
97 }
98 
GenValidator(std::ostream &) const99 void PayloadField::GenValidator(std::ostream&) const {
100   // Do nothing
101 }
102 
GenStringRepresentation(std::ostream & s,std::string) const103 void PayloadField::GenStringRepresentation(std::ostream& s, std::string) const {
104   // TODO: we should parse the child packets
105   s << "\"PAYLOAD[]\"";
106 }
107 
GetRustDataType() const108 std::string PayloadField::GetRustDataType() const {
109   return "Vec::<u8>";
110 }
111 
GenBoundsCheck(std::ostream & s,Size start_offset,Size,std::string parent_name) const112 void PayloadField::GenBoundsCheck(std::ostream& s, Size start_offset, Size, std::string parent_name) const {
113   if (size_field_ != nullptr) {
114     s << "let want_ = " << start_offset.bytes() << " + (" << size_field_->GetName() << " as usize)";
115     if (!size_modifier_.empty()) {
116       s << " - " << size_modifier_.substr(1);
117     }
118     s << ";";
119     s << "if bytes.len() < want_ {";
120     s << " return Err(Error::InvalidLengthError{";
121     s << "    obj: \"" << parent_name << "\".to_string(),";
122     s << "    field: \"" << GetName() << "\".to_string(),";
123     s << "    wanted: want_,";
124     s << "    got: bytes.len()});";
125     s << "}";
126     if (!size_modifier_.empty()) {
127       s << "if (" << size_field_->GetName() << " as usize) < " << size_modifier_.substr(1) << " {";
128       s << " return Err(Error::ImpossibleStructError);";
129       s << "}";
130     }
131   }
132 }
133 
GenRustGetter(std::ostream & s,Size start_offset,Size,std::string) const134 void PayloadField::GenRustGetter(std::ostream& s, Size start_offset, Size, std::string) const {
135   s << "let " << GetName() << ": " << GetRustDataType() << " = ";
136   if (size_field_ == nullptr) {
137     s << "bytes[" << start_offset.bytes() << "..].into();";
138   } else {
139     s << "bytes[" << start_offset.bytes() << "..(";
140     s << start_offset.bytes() << " + " << size_field_->GetName() << " as usize)].into();";
141   }
142 }
143 
GenRustWriter(std::ostream &,Size,Size) const144 void PayloadField::GenRustWriter(std::ostream&, Size, Size) const {}
145