Android-cuttlefish cvd tool
packet_runtime.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 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/*
18* TODO: b/416777029 - Stop using this copy of the file
19*/
20
21#pragma once
22
23#include <cassert>
24#include <cstdint>
25#include <memory>
26#include <utility>
27#include <vector>
28
29namespace pdl::packet {
30
34class slice {
35 public:
36 slice() = default;
37 slice(slice const&) = default;
38 slice(std::shared_ptr<const std::vector<uint8_t>> packet)
39 : packet_(std::move(packet)), offset_(0), size_(packet_->size()) {}
40
41 slice(std::shared_ptr<const std::vector<uint8_t>> packet, size_t offset,
42 size_t size)
43 : packet_(std::move(packet)), offset_(offset), size_(size) {}
44
48 slice subrange(size_t offset, size_t size) const {
49 assert((offset + size) <= size_);
50 return slice(packet_, offset_ + offset, size);
51 }
52
57 template <typename T, size_t N = sizeof(T)>
58 T read_le() {
59 static_assert(N <= sizeof(T));
60 assert(N <= size_);
61 T value = 0;
62 for (size_t n = 0; n < N; n++) {
63 value |= (T)at(n) << (8 * n);
64 }
65 skip(N);
66 return value;
67 }
68
73 template <typename T, size_t N = sizeof(T)>
74 T read_be() {
75 static_assert(N <= sizeof(T));
76 assert(N <= size_);
77 T value = 0;
78 for (size_t n = 0; n < N; n++) {
79 value = (value << 8) | (T)at(n);
80 }
81 skip(N);
82 return value;
83 }
84
87 uint8_t at(size_t offset) const {
88 assert(offset <= size_);
89 return packet_->at(offset_ + offset);
90 }
91
94 void skip(size_t size) {
95 assert(size <= size_);
96 offset_ += size;
97 size_ -= size;
98 }
99
101 void clear() { size_ = 0; }
102
104 size_t size() const { return size_; }
105
107 std::vector<uint8_t> bytes() const {
108 return std::vector<uint8_t>(packet_->cbegin() + offset_,
109 packet_->cbegin() + offset_ + size_);
110 }
111
112 bool operator==(slice const& other) const {
113 return size_ == other.size_ &&
114 std::equal(packet_->begin() + offset_,
115 packet_->begin() + offset_ + size_,
116 other.packet_->begin());
117 }
118
119 private:
120 std::shared_ptr<const std::vector<uint8_t>> packet_;
121 size_t offset_{0};
122 size_t size_{0};
123};
124
126class Builder {
127 public:
128 virtual ~Builder() = default;
129
132 virtual void Serialize(std::vector<uint8_t>&) const {}
133
136 virtual size_t GetSize() const { return 0; }
137
139 template <typename T, size_t N = sizeof(T)>
140 static void write_le(std::vector<uint8_t>& output, T value) {
141 static_assert(N <= sizeof(T));
142 for (size_t n = 0; n < N; n++) {
143 output.push_back(value >> (8 * n));
144 }
145 }
146
148 template <typename T, size_t N = sizeof(T)>
149 static void write_be(std::vector<uint8_t>& output, T value) {
150 static_assert(N <= sizeof(T));
151 for (size_t n = 0; n < N; n++) {
152 output.push_back(value >> (8 * (N - 1 - n)));
153 }
154 }
155
157 virtual std::vector<uint8_t> SerializeToBytes() const {
158 std::vector<uint8_t> output;
159 Serialize(output);
160 return output;
161 }
162};
163
164} // namespace pdl::packet
Interface class for generated packet builders.
Definition: packet_runtime.h:126
static void write_be(std::vector< uint8_t > &output, T value)
Write a scalar value encoded in big-endian.
Definition: packet_runtime.h:149
virtual ~Builder()=default
static void write_le(std::vector< uint8_t > &output, T value)
Write a scalar value encoded in little-endian.
Definition: packet_runtime.h:140
virtual void Serialize(std::vector< uint8_t > &) const
Definition: packet_runtime.h:132
virtual std::vector< uint8_t > SerializeToBytes() const
Helper method to serialize the packet to a byte vector.
Definition: packet_runtime.h:157
virtual size_t GetSize() const
Definition: packet_runtime.h:136
Definition: packet_runtime.h:34
std::shared_ptr< const std::vector< uint8_t > > packet_
Definition: packet_runtime.h:120
std::vector< uint8_t > bytes() const
Return the contents of the slice as a byte vector.
Definition: packet_runtime.h:107
T read_le()
Definition: packet_runtime.h:58
slice subrange(size_t offset, size_t size) const
Definition: packet_runtime.h:48
slice(slice const &)=default
uint8_t at(size_t offset) const
Definition: packet_runtime.h:87
bool operator==(slice const &other) const
Definition: packet_runtime.h:112
slice(std::shared_ptr< const std::vector< uint8_t > > packet, size_t offset, size_t size)
Definition: packet_runtime.h:41
size_t size_
Definition: packet_runtime.h:122
slice(std::shared_ptr< const std::vector< uint8_t > > packet)
Definition: packet_runtime.h:38
void skip(size_t size)
Definition: packet_runtime.h:94
size_t offset_
Definition: packet_runtime.h:121
T read_be()
Definition: packet_runtime.h:74
size_t size() const
Return the size of the slice in bytes.
Definition: packet_runtime.h:104
void clear()
Empty the slice.
Definition: packet_runtime.h:101
Definition: packet_runtime.h:29
Definition: logging.h:464