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_ + ")";
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   s << "PacketView<!kLittleEndian> " << GetGetterFunctionName() << "BigEndian() const {";
75   s << "ASSERT(was_validated_);";
76   s << "size_t end_index = size();";
77   s << "auto to_bound = begin();";
78   GenBounds(s, start_offset, end_offset, GetSize());
79   s << "return GetBigEndianSubview(field_begin, field_end);";
80   s << "}\n";
81 }
82 
GetBuilderParameterType() const83 std::string PayloadField::GetBuilderParameterType() const {
84   return "std::unique_ptr<BasePacketBuilder>";
85 }
86 
BuilderParameterMustBeMoved() const87 bool PayloadField::BuilderParameterMustBeMoved() const {
88   return true;
89 }
90 
GenBuilderParameterFromView(std::ostream & s) const91 void PayloadField::GenBuilderParameterFromView(std::ostream& s) const {
92   s << "std::make_unique<RawBuilder>(std::vector<uint8_t>(view.GetPayload().begin(), view.GetPayload().end()))";
93 }
94 
HasParameterValidator() const95 bool PayloadField::HasParameterValidator() const {
96   return false;
97 }
98 
GenParameterValidator(std::ostream &) const99 void PayloadField::GenParameterValidator(std::ostream&) const {
100   // There is no validation needed for a payload
101 }
102 
GenInserter(std::ostream &) const103 void PayloadField::GenInserter(std::ostream&) const {
104   ERROR() << __func__ << " Should never be called.";
105 }
106 
GenValidator(std::ostream &) const107 void PayloadField::GenValidator(std::ostream&) const {
108   // Do nothing
109 }
110 
GenStringRepresentation(std::ostream & s,std::string) const111 void PayloadField::GenStringRepresentation(std::ostream& s, std::string) const {
112   // TODO: we should parse the child packets
113   s << "\"PAYLOAD[]\"";
114 }
115 
GetRustDataType() const116 std::string PayloadField::GetRustDataType() const {
117   return "Vec::<u8>";
118 }
119 
GenRustGetter(std::ostream & s,Size start_offset,Size) const120 void PayloadField::GenRustGetter(std::ostream& s, Size start_offset, Size) const {
121   s << "let " << GetName() << ": " << GetRustDataType() << " = ";
122   if (size_field_ == nullptr) {
123     s << "bytes[" << start_offset.bytes() << "..].into();";
124   } else {
125     s << "bytes[" << start_offset.bytes() << "..(";
126     s << start_offset.bytes() << " + " << size_field_->GetName() << " as usize)].into();";
127   }
128 }
129 
GenRustWriter(std::ostream &,Size,Size) const130 void PayloadField::GenRustWriter(std::ostream&, Size, Size) const {}
131