summaryrefslogtreecommitdiff
path: root/system/gd/packet/parser/fields/variable_length_struct_field.cc
blob: 6506d8f298ca7703300a2759813f56ddfa62e9b3 (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
/*
 * 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/variable_length_struct_field.h"
#include "util.h"

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

VariableLengthStructField::VariableLengthStructField(std::string name, std::string type_name, ParseLocation loc)
    : PacketField(name, loc), type_name_(type_name) {}

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

Size VariableLengthStructField::GetSize() const {
  return Size();
}

Size VariableLengthStructField::GetBuilderSize() const {
  std::string ret = "(" + GetName() + "_->size() * 8) ";
  return ret;
}

std::string VariableLengthStructField::GetDataType() const {
  std::string ret = "std::unique_ptr<" + type_name_ + ">";
  return ret;
}

void VariableLengthStructField::GenExtractor(std::ostream& s, int, bool) const {
  s << GetName() << "_ptr = Parse" << type_name_ << "(" << GetName() << "_it);";
  s << "if (" << GetName() << "_ptr != nullptr) {";
  s << GetName() << "_it = " << GetName() << "_it + " << GetName() << "_ptr->size();";
  s << "} else {";
  s << GetName() << "_it = " << GetName() << "_it + " << GetName() << "_it.NumBytesRemaining();";
  s << "}";
}

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

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

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

bool VariableLengthStructField::BuilderParameterMustBeMoved() const {
  return true;
}

bool VariableLengthStructField::HasParameterValidator() const {
  return false;
}

void VariableLengthStructField::GenParameterValidator(std::ostream&) const {
  // Validated at compile time.
}

void VariableLengthStructField::GenInserter(std::ostream& s) const {
  s << GetName() << "_->Serialize(i);";
}

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

std::string VariableLengthStructField::GetRustDataType() const {
  std::string ret = "std::boxed::Box<" + type_name_ + ">";
  return ret;
}

void VariableLengthStructField::GenRustGetter(std::ostream&, Size, Size, std::string) const {}

void VariableLengthStructField::GenRustWriter(std::ostream&, Size, Size) const {}