forked from charlieh0tel/google-libusb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgbuffer.cc
320 lines (278 loc) · 9.41 KB
/
gbuffer.cc
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2012 Google Inc. All Rights Reserved.
//
#include "gbuffer.h"
#include <stddef.h>
#include <stdint.h>
#include <cstring>
#include <glog/logging.h>
#include "ghexdump.h"
namespace glibusb {
Buffer::Buffer() {}
Buffer::~Buffer() {}
Buffer::Buffer(const void *src, Buffer::size_type length) {
buffer_.resize(length);
if (length > 0) {
uint8_t *dst = &(buffer_[0]);
memcpy(dst, src, length);
}
}
bool Buffer::operator==(const Buffer &other) const {
return buffer_ == other.buffer_;
}
bool Buffer::operator!=(const Buffer &other) const {
return !(*this == other);
}
Buffer *Buffer::MakeSlice(Buffer::size_type offset,
Buffer::size_type length) const {
CHECK_LE(offset + length, buffer_.size());
if (length == 0) {
return new Buffer();
} else {
const uint8_t *p = &(buffer_[offset]);
return new Buffer(p, length);
}
}
void Buffer::Clear() {
buffer_.clear();
}
void Buffer::Resize(Buffer::size_type length) {
buffer_.resize(length);
}
void *Buffer::GetBufferPointer(Buffer::size_type length) {
return GetBufferPointer(0, length);
}
const void *Buffer::GetBufferPointer(Buffer::size_type length) const {
return GetBufferPointer(0, length);
}
void *Buffer::GetBufferPointer(Buffer::size_type offset,
Buffer::size_type length) {
if (length == 0) {
return NULL;
} else {
CHECK_LE(offset + length, buffer_.size());
uint8_t *p = &(buffer_[offset]);
return static_cast<void *>(p);
}
}
const void *Buffer::GetBufferPointer(Buffer::size_type offset,
Buffer::size_type length) const {
if (length == 0) {
return NULL;
} else {
CHECK_LE(offset + length, buffer_.size());
const uint8_t *p = &(buffer_[offset]);
return static_cast<const void *>(p);
}
}
// Specialized template for Get
template <>
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
uint8_t *value_out) const {
CHECK_LT(byte_offset, buffer_.size());
*CHECK_NOTNULL(value_out) = buffer_[byte_offset];
return sizeof(uint8_t);
}
// Specialized template for Get
template <>
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
int8_t *value_out) const {
uint8_t value;
Get(byte_offset, &value);
*CHECK_NOTNULL(value_out) = static_cast<int8_t>(value);
return sizeof(int8_t);
}
// Specialized template for Get
template <>
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
uint16_t *value_out) const {
CHECK_LT(byte_offset + 1, buffer_.size());
uint16_t byte0 = static_cast<uint16_t>(buffer_[byte_offset]);
uint16_t byte1 = static_cast<uint16_t>(buffer_[byte_offset + 1]);
uint16_t value = byte0 | (byte1 << 8);
*CHECK_NOTNULL(value_out) = value;
return sizeof(uint16_t);
}
// Specialized template for Get
template <>
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
int16_t *value_out) const {
uint16_t value;
Get(byte_offset, &value);
*CHECK_NOTNULL(value_out) = static_cast<int16_t>(value);
return sizeof(int16_t);
}
// Specialized template for Get
template <>
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
uint32_t *value_out) const {
CHECK_LT(byte_offset + 3, buffer_.size());
uint32_t byte0 = static_cast<uint32_t>(buffer_[byte_offset]);
uint32_t byte1 = static_cast<uint32_t>(buffer_[byte_offset + 1]);
uint32_t byte2 = static_cast<uint32_t>(buffer_[byte_offset + 2]);
uint32_t byte3 = static_cast<uint32_t>(buffer_[byte_offset + 3]);
uint32_t value = byte0 | (byte1 << 8) | (byte2 << 16) | (byte3 << 24);
*CHECK_NOTNULL(value_out) = value;
return sizeof(uint32_t);
}
// Specialized template for Get
template <>
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
int32_t *value_out) const {
uint32_t value;
Get(byte_offset, &value);
*CHECK_NOTNULL(value_out) = static_cast<int32_t>(value);
return sizeof(int32_t);
}
// Specialized template for Get
template <>
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
uint64_t *value_out) const {
CHECK_LT(byte_offset + 7, buffer_.size());
uint64_t byte0 = static_cast<uint64_t>(buffer_[byte_offset]);
uint64_t byte1 = static_cast<uint64_t>(buffer_[byte_offset + 1]);
uint64_t byte2 = static_cast<uint64_t>(buffer_[byte_offset + 2]);
uint64_t byte3 = static_cast<uint64_t>(buffer_[byte_offset + 3]);
uint64_t byte4 = static_cast<uint64_t>(buffer_[byte_offset + 4]);
uint64_t byte5 = static_cast<uint64_t>(buffer_[byte_offset + 5]);
uint64_t byte6 = static_cast<uint64_t>(buffer_[byte_offset + 6]);
uint64_t byte7 = static_cast<uint64_t>(buffer_[byte_offset + 7]);
uint64_t value =
byte0 | (byte1 << 8) | (byte2 << 16) | (byte3 << 24) |
(byte4 << 32) | (byte5 << 40) | (byte6 << 48) | (byte7 << 56);
*CHECK_NOTNULL(value_out) = value;
return sizeof(uint64_t);
}
// Specialized template for Get
template <>
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
int64_t *value_out) const {
uint64_t value;
Get(byte_offset, &value);
*CHECK_NOTNULL(value_out) = static_cast<int64_t>(value);
return sizeof(int64_t);
}
Buffer::size_type Buffer::Get(Buffer::size_type byte_offset,
std::string *out) const {
CHECK_NOTNULL(out);
out->clear();
size_type n = 0;
for (size_t i = byte_offset; /**/; ++i, ++n) {
uint8_t p;
Get(i, &p);
if (!p) {
break;
}
out->push_back(static_cast<char>(p));
}
// strings are always padded out to 4 bytes
n = (n + 3) & ~3;
return n;
}
// Specialized template for Put
template <>
Buffer::size_type Buffer::Put(Buffer::size_type byte_offset, uint8_t value) {
CHECK_LT(byte_offset, buffer_.size());
buffer_[byte_offset] = value;
return sizeof(uint8_t);
}
// Specialized template for Put
template <>
Buffer::size_type Buffer::Put(Buffer::size_type byte_offset, int8_t value) {
return Put(byte_offset, static_cast<uint8_t>(value));
}
// Specialized template for Put
template <>
Buffer::size_type Buffer::Put(Buffer::size_type byte_offset, uint16_t value) {
CHECK_LT(byte_offset + 1, buffer_.size());
uint8_t byte_0 = static_cast<uint8_t>(value & 0xff);
uint8_t byte_1 = static_cast<uint8_t>((value >> 8) & 0xff);
buffer_[byte_offset] = byte_0;
buffer_[byte_offset + 1] = byte_1;
return sizeof(uint16_t);
}
// Specialized template for Put
template <>
Buffer::size_type Buffer::Put(Buffer::size_type byte_offset, int16_t value) {
return Put(byte_offset, static_cast<uint16_t>(value));
}
// Specialized template for Put
template <>
Buffer::size_type Buffer::Put(Buffer::size_type byte_offset, uint32_t value) {
CHECK_LT(byte_offset + 3, buffer_.size());
uint8_t byte_0 = static_cast<uint8_t>(value & 0xff);
uint8_t byte_1 = static_cast<uint8_t>((value >> 8) & 0xff);
uint8_t byte_2 = static_cast<uint8_t>((value >> 16) & 0xff);
uint8_t byte_3 = static_cast<uint8_t>((value >> 24) & 0xff);
buffer_[byte_offset] = byte_0;
buffer_[byte_offset + 1] = byte_1;
buffer_[byte_offset + 2] = byte_2;
buffer_[byte_offset + 3] = byte_3;
return sizeof(uint32_t);
}
// Specialized template for Put
template <>
Buffer::size_type Buffer::Put(Buffer::size_type byte_offset, int32_t value) {
return Put(byte_offset, static_cast<uint32_t>(value));
}
// Specialized template for Put
template <>
Buffer::size_type Buffer::Put(Buffer::size_type byte_offset, uint64_t value) {
CHECK_LT(byte_offset + 7, buffer_.size());
uint8_t byte_0 = static_cast<uint8_t>(value & 0xff);
uint8_t byte_1 = static_cast<uint8_t>((value >> 8) & 0xff);
uint8_t byte_2 = static_cast<uint8_t>((value >> 16) & 0xff);
uint8_t byte_3 = static_cast<uint8_t>((value >> 24) & 0xff);
uint8_t byte_4 = static_cast<uint8_t>((value >> 32) & 0xff);
uint8_t byte_5 = static_cast<uint8_t>((value >> 40) & 0xff);
uint8_t byte_6 = static_cast<uint8_t>((value >> 48) & 0xff);
uint8_t byte_7 = static_cast<uint8_t>((value >> 56) & 0xff);
buffer_[byte_offset] = byte_0;
buffer_[byte_offset + 1] = byte_1;
buffer_[byte_offset + 2] = byte_2;
buffer_[byte_offset + 3] = byte_3;
buffer_[byte_offset + 4] = byte_4;
buffer_[byte_offset + 5] = byte_5;
buffer_[byte_offset + 6] = byte_6;
buffer_[byte_offset + 7] = byte_7;
return sizeof(uint64_t);
}
// Specialized template for Put
template <>
Buffer::size_type Buffer::Put(Buffer::size_type byte_offset, int64_t value) {
return Put(byte_offset, static_cast<uint64_t>(value));
}
void Buffer::Append(const Buffer &source) {
buffer_.insert(buffer_.end(), source.buffer_.begin(), source.buffer_.end());
}
void Buffer::AddHeader(Buffer::size_type length) {
buffer_.insert(buffer_.begin(), length, 0);
}
void Buffer::RemoveHeader(Buffer::size_type length) {
if (length > 0) {
CHECK_LE(length, buffer_.size());
buffer_.erase(buffer_.begin(), buffer_.begin() + length);
}
}
void Buffer::Copy(const Buffer &source) {
buffer_.assign(source.buffer_.begin(), source.buffer_.end());
}
#if 0
void Buffer::WriteOrDie(File *fp) const {
size_type n = Length();
const void *p = GetBufferPointer(n);
fp->WriteOrDie(p, n);
}
void Buffer::WriteToPathOrDie(const char *name) const {
FileCloser file(File::OpenOrDie(name, "w"));
WriteOrDie(file.get());
}
#endif
std::string Buffer::Dump() const {
size_type n = Length();
if (n == 0) {
return "";
} else {
return glibusb::Dump(&(buffer_[0]), n);
}
}
} // namespace glibusb