Debian Codegraph @

mlspp/lib/tls_syntax/src/tls_syntax.cpp

raw symbols SCIP
1 #include <tls/tls_syntax.h>
2
3 // NOLINTNEXTLINE(llvmlibc-implementation-in-namespace)
4 namespace mlspp::tls {
5
6 void
7 ostream::write_raw(const std::vector<uint8_t>& bytes)
8 {
9 // Not sure what the default argument is here
10 _buffer.insert(_buffer.end(), bytes.begin(), bytes.end());
11 }
12
13 // Primitive type writers
14 ostream&
15 ostream::write_uint(uint64_t value, int length)
16 {
17 for (int i = length - 1; i >= 0; --i) {
18 _buffer.push_back(static_cast<uint8_t>(value >> unsigned(8 * i)));
19 }
20 return *this;
21 }
22
23 ostream&
24 operator<<(ostream& out, bool data)
25 {
26 if (data) {
27 return out << uint8_t(1);
28 }
29
30 return out << uint8_t(0);
31 }
32
33 ostream&
34 operator<<(ostream& out, uint8_t data) // NOLINT(llvmlibc-callee-namespace)
35 {
36 return out.write_uint(data, 1);
37 }
38
39 ostream&
40 operator<<(ostream& out, uint16_t data)
41 {
42 return out.write_uint(data, 2);
43 }
44
45 ostream&
46 operator<<(ostream& out, uint32_t data)
47 {
48 return out.write_uint(data, 4);
49 }
50
51 ostream&
52 operator<<(ostream& out, uint64_t data)
53 {
54 return out.write_uint(data, 8);
55 }
56
57 // Because pop_back() on an empty vector is undefined
58 uint8_t
59 istream::next()
60 {
61 if (_buffer.empty()) {
62 throw ReadError("Attempt to read from empty buffer");
63 }
64
65 const uint8_t value = _buffer.back();
66 _buffer.pop_back();
67 return value;
68 }
69
70 // Primitive type readers
71
72 istream&
73 operator>>(istream& in, bool& data)
74 {
75 uint8_t val = 0;
76 in >> val;
77
78 // Linter thinks uint8_t is signed (?)
79 // NOLINTNEXTLINE(hicpp-signed-bitwise)
80 if ((val & 0xFE) != 0) {
81 throw ReadError("Malformed boolean");
82 }
83
84 data = (val == 1);
85 return in;
86 }
87
88 istream&
89 operator>>(istream& in, uint8_t& data) // NOLINT(llvmlibc-callee-namespace)
90 {
91 return in.read_uint(data, 1);
92 }
93
94 istream&
95 operator>>(istream& in, uint16_t& data)
96 {
97 return in.read_uint(data, 2);
98 }
99
100 istream&
101 operator>>(istream& in, uint32_t& data)
102 {
103 return in.read_uint(data, 4);
104 }
105
106 istream&
107 operator>>(istream& in, uint64_t& data)
108 {
109 return in.read_uint(data, 8);
110 }
111
112 // Varint encoding
113 static constexpr size_t VARINT_HEADER_OFFSET = 6;
114 static constexpr uint64_t VARINT_1_HEADER = 0x00; // 0 << V1_OFFSET
115 static constexpr uint64_t VARINT_2_HEADER = 0x4000; // 1 << V2_OFFSET
116 static constexpr uint64_t VARINT_4_HEADER = 0x80000000; // 2 << V4_OFFSET
117 static constexpr uint64_t VARINT_1_MAX = 0x3f;
118 static constexpr uint64_t VARINT_2_MAX = 0x3fff;
119 static constexpr uint64_t VARINT_4_MAX = 0x3fffffff;
120
121 ostream&
122 varint::encode(ostream& str, const uint64_t& val)
123 {
124 if (val <= VARINT_1_MAX) {
125 return str.write_uint(VARINT_1_HEADER | val, 1);
126 }
127
128 if (val <= VARINT_2_MAX) {
129 return str.write_uint(VARINT_2_HEADER | val, 2);
130 }
131
132 if (val <= VARINT_4_MAX) {
133 return str.write_uint(VARINT_4_HEADER | val, 4);
134 }
135
136 throw WriteError("Varint value exceeds maximum size");
137 }
138
139 istream&
140 varint::decode(istream& str, uint64_t& val)
141 {
142 auto log_size = size_t(str._buffer.back() >> VARINT_HEADER_OFFSET);
143 if (log_size > 2) {
144 throw ReadError("Malformed varint header");
145 }
146
147 auto read = uint64_t(0);
148 auto read_bytes = size_t(size_t(1) << log_size);
149 str.read_uint(read, read_bytes);
150
151 switch (log_size) {
152 case 0:
153 read ^= VARINT_1_HEADER;
154 break;
155
156 case 1:
157 read ^= VARINT_2_HEADER;
158 if (read <= VARINT_1_MAX) {
159 throw ReadError("Non-minimal varint");
160 }
161 break;
162
163 case 2:
164 read ^= VARINT_4_HEADER;
165 if (read <= VARINT_2_MAX) {
166 throw ReadError("Non-minimal varint");
167 }
168 break;
169
170 default:
171 throw ReadError("Malformed varint header");
172 }
173
174 val = read;
175 return str;
176 }
177
178 } // namespace mlspp::tls