/
data_buffer.cpp
420 lines (358 loc) · 9.86 KB
/
data_buffer.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <algorithm>
#include <cstring>
#include "data_buffer.h"
#include "utils.h"
DataValue::DataValue() : _value()
{
}
template <typename T> DataValue::DataValue(T value)
{
_value.resize(sizeof(T));
memcpy(_value.data(), &value, sizeof(T));
}
template DataValue::DataValue<std::int8_t>(std::int8_t value);
template DataValue::DataValue<std::int16_t>(std::int16_t value);
template DataValue::DataValue<std::int32_t>(std::int32_t value);
template DataValue::DataValue<std::int64_t>(std::int64_t value);
template DataValue::DataValue<std::uint8_t>(std::uint8_t value);
template DataValue::DataValue<std::uint16_t>(std::uint16_t value);
template DataValue::DataValue<std::uint32_t>(std::uint32_t value);
template DataValue::DataValue<std::uint64_t>(std::uint64_t value);
DataValue::DataValue(const std::string &value) : _value()
{
std::copy(value.begin(), value.end(), std::back_inserter(_value));
}
DataValue::DataValue(const std::vector<std::uint8_t> &value) : _value(value)
{
}
DataValue::DataValue(const DataValue &dataValue) : _value(dataValue._value)
{
}
DataValue::DataValue(DataValue &&dataValue) : _value(std::move(dataValue._value))
{
}
DataValue::~DataValue()
{
}
DataValue& DataValue::operator =(DataValue &&dataValue)
{
_value = std::move(dataValue._value);
return *this;
}
/**
* Returns the amount of bytes the value is made of.
*
* @return Size in bytes.
*/
std::size_t DataValue::getSize() const
{
return _value.size();
}
/**
* Returns the boolean value of the value. The method takes sizeof(uint64_t)
* bytes and checks whether there is at least one bit set to 1.
*
* @return Boolean value.
*/
bool DataValue::getBool() const
{
return getInt<uint64_t>();
}
/**
* Returns the value as integer type.
*
* @return Integer value.
*/
template <typename T> T DataValue::getInt() const
{
T value;
std::size_t bytesToCopy = std::min(getSize(), sizeof(T));
memcpy(&value, _value.data(), bytesToCopy);
return value;
}
template std::int8_t DataValue::getInt<std::int8_t>() const;
template std::int16_t DataValue::getInt<std::int16_t>() const;
template std::int32_t DataValue::getInt<std::int32_t>() const;
template std::int64_t DataValue::getInt<std::int64_t>() const;
template std::uint8_t DataValue::getInt<std::uint8_t>() const;
template std::uint16_t DataValue::getInt<std::uint16_t>() const;
template std::uint32_t DataValue::getInt<std::uint32_t>() const;
template std::uint64_t DataValue::getInt<std::uint64_t>() const;
/**
* Returns the value as string. The method reads bytes until null-terminator
* is hit or the end of the buffer.
*
* @return String value.
*/
std::string DataValue::getString() const
{
std::string str = "";
std::size_t pos = 0;
// We read until we hit null terminator or we hit the end of the buffer
while ((_value[pos] != '\0') && (pos < getSize()))
{
str += _value[pos];
pos++;
}
return str;
}
/**
* Returns the value as raw bytes.
*
* @return Raw bytes.
*/
const std::vector<std::uint8_t>& DataValue::getBytes() const
{
return _value;
}
DataBuffer::DataBuffer() : _data()
{
}
DataBuffer::DataBuffer(std::size_t size) : _data(size, 0)
{
}
DataBuffer::DataBuffer(const std::vector<std::uint8_t> &data) : _data(data)
{
}
DataBuffer::DataBuffer(const DataBuffer &dataBuffer) : _data(dataBuffer._data)
{
}
DataBuffer::DataBuffer(const DataBuffer &dataBuffer, std::size_t offset, std::size_t count)
{
count = offset + count >= dataBuffer.getSize() ? dataBuffer.getSize() - offset : count;
_data.reserve(count);
std::copy(dataBuffer._data.begin() + offset, dataBuffer._data.begin() + offset + count, std::back_inserter(_data));
}
DataBuffer::DataBuffer(DataBuffer &&dataBuffer) : _data(std::move(dataBuffer._data))
{
}
DataBuffer::~DataBuffer()
{
}
DataBuffer& DataBuffer::operator =(DataBuffer &&dataBuffer)
{
_data = std::move(dataBuffer._data);
return *this;
}
/**
* Creates new DataBuffer with the contents of the whole file.
*
* @param file The file to read from.
*
* @return DataBuffer with contents of the file.
*/
std::unique_ptr<DataBuffer> DataBuffer::createFromFile(FILE *file)
{
if (file == nullptr)
return nullptr;
std::size_t size;
if (!fileSize(file, size))
return nullptr;
std::vector<std::uint8_t> contents;
if (!readFile(file, 0, size, contents))
return nullptr;
return std::make_unique<DataBuffer>(contents);
}
bool DataBuffer::writeToFile(FILE* file)
{
if (file == nullptr)
return false;
if (!writeFile(file, 0, _data))
return false;
return true;
}
/**
* Returns the size of the buffer.
*
* @return The size of the buffer in bytes.
*/
std::size_t DataBuffer::getSize() const
{
return _data.size();
}
const std::vector<std::uint8_t>& DataBuffer::getBuffer() const
{
return _data;
}
/**
* Creates the copy of the sub-buffer from the given offset up to given number of bytes.
*
* @param offset The offset to copy from.
* @param amount The number of bytes to copy.
*
* @return Sub-buffer.
*/
DataBuffer DataBuffer::getSubBuffer(std::size_t offset, std::size_t amount) const
{
return DataBuffer(*this, offset, amount);
}
/**
* Reads the value in the buffer from the specified offset and specified size.
*
* @param offset The offset where to read from.
* @param amount The number of bytes to read.
*
* @return DataValue representing the read value.
*/
DataValue DataBuffer::read(std::size_t offset, std::size_t amount) const
{
// Check of boundaries
if (offset >= getSize())
return DataValue();
// Calculate amount of bytes to copy in case we can run out of buffer boundaries
std::size_t bytesToCopy = offset + amount >= getSize() ? getSize() - offset : amount;
std::vector<std::uint8_t> bytes;
bytes.reserve(bytesToCopy);
// Copy using back inserter
std::copy(_data.begin() + offset, _data.begin() + offset + bytesToCopy, std::back_inserter(bytes));
return DataValue(bytes);
}
/**
* Reads the specific bits from the byte at the specified offset. Method also
* access neighbour elements if bitCount overlaps current byte. No more than
* 64 bits are read.
*
* @param byteOffset The offset of the byte.
* @param bitOffset The bit from which to start reading. 0 is LSB.
* @param bitCount The number of bits to read.
*
* @return DataValue representing the read value.
*/
DataValue DataBuffer::readBits(std::size_t byteOffset, std::uint8_t bitOffset, std::size_t bitCount) const
{
if (byteOffset >= getSize())
return DataValue();
if (bitOffset >= 8)
return DataValue();
// We don't read more than 64 bits
if (bitCount >= (sizeof(std::uint64_t) * 8))
bitCount = 64;
std::uint8_t currentByte = _data[byteOffset] >> bitOffset;
std::size_t bitsWritten = 0;
std::uint64_t val = 0;
while (bitCount > 0)
{
// We take the current byte and mask it out so only required number of bits is taken into account
// We then shift it right so it fits right position based on the amount of bits already read
val |= static_cast<std::uint64_t>((currentByte & _getBitMask(bitCount))) << bitsWritten;
// We now need to calculate how many bits we have written
// If there is less than 8 bits left, use that count as number of bits we have written
// Otherwise, calculate it with formula 8 - bitOffset
if (bitOffset + bitCount < 8)
{
bitsWritten += bitCount;
bitCount -= bitCount;
}
else
{
bitsWritten += 8 - bitOffset;
bitCount -= 8 - bitOffset;
}
byteOffset++;
if (byteOffset >= getSize())
break;
currentByte = _data[byteOffset];
bitOffset = 0; // We must set bitOffset to 0 because we will be reading from this offset in all consecutive iterations
}
return DataValue(val);
}
/**
* Reads the specific bits from the bit at the specified offset. Method also
* access neighbour elements if bitCount overlaps current byte. No more than
* 64 bits are read.
*
* @param bitOffset The bit from which to start reading. 0 is LSB of whole buffer.
* @param bitCount The number of bits to read.
*
* @return DataValue representing the read value.
*/
DataValue DataBuffer::readBits(std::size_t bitOffset, std::size_t bitCount) const
{
// Dividing by 8 to get index of the byte
std::size_t byteOffset = bitOffset >> 3;
// Modulo 8 to get bit in byte offset
std::uint8_t bitInByteOffset = bitOffset & 7;
return readBits(byteOffset, bitInByteOffset, bitCount);
}
std::uint8_t DataBuffer::_getBitMask(std::size_t bitCount)
{
// We don't use breaks in switch intentionally
// We first set mask to highest possible (0xFF) and then abuse fall through switch
// and set bits 0.
// The order of cases is very important!!! Do not change!!!
std::uint8_t mask = 0xFF;
switch (bitCount)
{
case 0:
mask &= ~0x01;
case 1:
mask &= ~0x02;
case 2:
mask &= ~0x04;
case 3:
mask &= ~0x08;
case 4:
mask &= ~0x10;
case 5:
mask &= ~0x20;
case 6:
mask &= ~0x40;
case 7:
mask &= ~0x80;
default:
break;
}
return mask;
}
void DataBuffer::write(std::size_t offset, const std::vector<std::uint8_t>& data)
{
if (offset + data.size() - 1 >= getSize())
_data.resize(offset + data.size());
std::copy(data.begin(), data.end(), _data.begin() + offset);
}
void DataBuffer::write(std::size_t offset, std::uint8_t byte)
{
if (offset >= getSize())
_data.resize(offset + 1);
_data[offset] = byte;
}
void DataBuffer::write(std::size_t offset, const DataValue& value)
{
write(offset, value.getBytes());
}
/**
* Appends another DataBuffer to this DataBuffer.
*
* @param data DataBuffer to append.
*/
void DataBuffer::append(const DataBuffer& data)
{
append(data._data);
}
/**
* Appends vector of bytes to this DataBuffer.
*
* @param data Data to append.
*/
void DataBuffer::append(const std::vector<std::uint8_t> &data)
{
std::copy(data.begin(), data.end(), std::back_inserter(_data));
}
/**
* Appends single byte to this DataBuffer.
*
* @param byte Byte to append.
*/
void DataBuffer::append(std::uint8_t byte)
{
_data.push_back(byte);
}
/**
* Appends a value to this DataBuffer.
*
* @param value Value to append.
*/
void DataBuffer::append(const DataValue& value)
{
append(value.getBytes());
}