-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawDataField.cpp
More file actions
502 lines (432 loc) · 16 KB
/
RawDataField.cpp
File metadata and controls
502 lines (432 loc) · 16 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include <cmath>
#include <cstring>
#include <new>
#include <stdexcept>
#include "RawDataField.hpp"
#include "DataField.hpp"
#include "misc.hpp"
//==============================================================================
RawDataField::RawDataField(unsigned long length,
misc::DataUnits length_units,
IndexingMode bit_indexing_mode) :
RawDataField(0, 0, length, length_units, true, bit_indexing_mode, false)
{
}
//==============================================================================
RawDataField::RawDataField(std::uint8_t* buffer,
unsigned long length,
misc::DataUnits length_units,
bool memory_internal,
IndexingMode bit_indexing_mode) :
RawDataField(buffer,
0,
length,
length_units,
memory_internal,
bit_indexing_mode,
false)
{
// Delegate RawDataField constructor has already set up our "raw_data"
// pointer. At this point we can just start using it.
if (memory_internal)
{
DataField::readRaw(buffer);
}
}
//==============================================================================
RawDataField::RawDataField(const std::uint8_t* buffer,
unsigned long length,
misc::DataUnits length_units,
bool memory_internal,
IndexingMode bit_indexing_mode) :
RawDataField(0,
buffer,
length,
length_units,
memory_internal,
bit_indexing_mode,
true)
{
// Delegate RawDataField constructor has already set up our "raw_data"
// pointer. At this point we can just start using it.
if (memory_internal)
{
DataField::readRaw(buffer);
}
}
//==============================================================================
// cppcheck-suppress uninitMemberVar
RawDataField::RawDataField(const RawDataField& raw_data_field) :
RawDataField(0,
0,
raw_data_field.getLengthBits(),
misc::BITS,
true,
raw_data_field.getBitIndexingMode(),
false)
{
// Delegate RawDataField constructor has already set up our "raw_data"
// pointer. At this point we can just start using it.
raw_data_field.DataField::writeRaw(raw_data);
}
//==============================================================================
RawDataField::RawDataField(std::uint8_t* buffer,
const std::uint8_t* buffer_const,
unsigned long length,
misc::DataUnits length_units,
bool memory_internal,
IndexingMode bit_indexing_mode,
bool const_mode) :
DataField(),
raw_data(buffer),
raw_data_const(buffer_const),
memory_internal(memory_internal),
bit_indexing_mode(bit_indexing_mode),
const_mode(const_mode)
{
if (length == 0)
{
throw std::invalid_argument("Length must be at least 1");
}
// Set length_bits appropriately
if (length_units == misc::BYTES)
{
length_bits = length * BITS_PER_BYTE;
}
else if (length_units == misc::BITS)
{
length_bits = length;
}
else
{
std::runtime_error("Unsupported units type specified");
}
if (memory_internal)
{
// We're managing memory internally so we need some memory. Memory
// amount calculation is copied from the getLengthBytes definition in
// DataField. We don't use that directly here since getLengthBytes will
// call getLengthBits on this class, and this class isn't fully
// instantiated yet.
raw_data = new std::uint8_t[static_cast<unsigned int>(
std::ceil(static_cast<double>(length_bits) /
static_cast<double>(BITS_PER_BYTE)))];
}
}
//==============================================================================
RawDataField::~RawDataField()
{
if (memory_internal)
{
delete[] raw_data;
}
}
//==============================================================================
unsigned long RawDataField::readRaw(std::uint8_t* buffer,
misc::ByteOrder source_byte_order)
{
// Prevent us from reading into const memory
constModeExceptionCheck();
// No byteswapping regardless of "source_byte_order" setting
memcpy(raw_data, buffer, getLengthBytes());
return length_bits;
}
//==============================================================================
unsigned long RawDataField::readRaw(const std::uint8_t* buffer,
misc::ByteOrder source_byte_order)
{
// Prevent us from reading into const memory
constModeExceptionCheck();
// No byteswapping regardless of "source_byte_order" setting
memcpy(raw_data, buffer, getLengthBytes());
return length_bits;
}
//==============================================================================
unsigned long RawDataField::writeRaw(
std::uint8_t* buffer,
misc::ByteOrder destination_byte_order) const
{
// No byteswapping regardless of "destination_byte_order" setting
if (const_mode)
{
memcpy(buffer, raw_data_const, getLengthBytes());
}
else
{
memcpy(buffer, raw_data, getLengthBytes());
}
return length_bits;
}
//==============================================================================
std::uint8_t RawDataField::getByte(unsigned int index) const
{
throwIfIndexOutOfRange(index, getLengthBytes());
if (const_mode)
{
return raw_data_const[index];
}
return raw_data[index];
}
//==============================================================================
void RawDataField::setByte(unsigned int index, std::uint8_t value)
{
// Prevent us from reading into const memory
constModeExceptionCheck();
throwIfIndexOutOfRange(index, getLengthBytes());
raw_data[index] = value;
}
//==============================================================================
bool RawDataField::getBit(unsigned long index) const
{
throwIfIndexOutOfRange(index, length_bits);
// This returns the index of the byte we want and the index of the bit
// within that byte
std::ldiv_t div_result = std::ldiv(static_cast<long>(index), BITS_PER_BYTE);
std::uint8_t target_byte = 0;
// This is the byte containing the bit we want
if (const_mode)
{
target_byte = raw_data_const[div_result.quot];
}
else
{
target_byte = raw_data[div_result.quot];
}
// We still need to find the right bit, div_result.rem has the index. Shift
// the bit we want down to the least significant bit and then mask out the
// other bits
if (bit_indexing_mode == LS_LEAST)
{
target_byte >>= div_result.rem;
}
else
{
target_byte >>= BITS_PER_BYTE - div_result.rem - 1;
}
return (target_byte & 0x1) == 1;
}
//==============================================================================
void RawDataField::setBit(unsigned long index, bool value)
{
// Prevent us from modifying const memory
constModeExceptionCheck();
throwIfIndexOutOfRange(index, length_bits);
std::uint8_t mask = 1;
std::uint8_t target_byte = 0;
if (value)
{
target_byte = 1;
}
// This returns the index of the byte we want and the index of the bit
// within that byte
std::ldiv_t div_result = std::ldiv(static_cast<long>(index), BITS_PER_BYTE);
// Shift the bit we set above to the correct position depending on bit
// indexing setting
unsigned int shift_amount = static_cast<unsigned int>(div_result.rem);
if (bit_indexing_mode == MS_LEAST)
{
shift_amount =
static_cast<unsigned int>(BITS_PER_BYTE - div_result.rem - 1);
}
target_byte <<= shift_amount;
// Shift the mask into the right place as well
mask <<= shift_amount;
// We have the byte and mask shifted properly, now we just have to write the
// byte into the proper place in raw_bit_field without disturbing the other
// bits
unsigned int byte_index = static_cast<unsigned int>(div_result.quot);
// Mask the bit setting in
raw_data[byte_index] &= ~mask;
raw_data[byte_index] |= target_byte;
}
//==============================================================================
template <class T>
void RawDataField::getBitsAsNumericType(T& type_var,
unsigned int start_bit,
unsigned int count) const
{
// Handle bad input
if (start_bit >= length_bits || start_bit + count > length_bits)
{
throw std::out_of_range("Out-of-range bit(s) specified");
}
else if (count > sizeof(T) * BITS_PER_BYTE)
{
throw std::out_of_range("Not enough bits in the destination type");
}
// We could not do this and leave irrelevant bits untouched, but that
// wouldn't match the usage convention of pretty much every other getter
// class member function ever
type_var = 0;
// Use the given variable as if it were just raw data, and set bits inside
// it
RawDataField working_rdf(reinterpret_cast<std::uint8_t*>(&type_var),
sizeof(T),
misc::BYTES,
false,
bit_indexing_mode);
// Copy all the bits; an alternative implementation would be to memcpy the
// relevant data over, shift down and then mask out the irrelevant bits, but
// this uses the getBit() and setBit() functions which account for bit
// indexing mode us.
for (unsigned int i = 0; i < count; ++i)
{
working_rdf.setBit(i, getBit(start_bit + i));
}
}
// Use this macro to instantiate getBitsAsNumericType() for all the intrinsic
// numeric types. "char" is arguably not a numeric type but let's include it
// since it's sometimes useful to use it as if it were a numeric type
#define INSTANTIATE_GETBITSASNUMERICTYPE(Type) \
template void \
RawDataField::getBitsAsNumericType(Type&, unsigned int, unsigned int) const;
INSTANTIATE_GETBITSASNUMERICTYPE(char);
INSTANTIATE_GETBITSASNUMERICTYPE(double);
INSTANTIATE_GETBITSASNUMERICTYPE(float);
INSTANTIATE_GETBITSASNUMERICTYPE(int);
INSTANTIATE_GETBITSASNUMERICTYPE(long);
INSTANTIATE_GETBITSASNUMERICTYPE(long double);
INSTANTIATE_GETBITSASNUMERICTYPE(long long);
INSTANTIATE_GETBITSASNUMERICTYPE(short);
INSTANTIATE_GETBITSASNUMERICTYPE(unsigned char);
INSTANTIATE_GETBITSASNUMERICTYPE(unsigned int);
INSTANTIATE_GETBITSASNUMERICTYPE(unsigned long);
INSTANTIATE_GETBITSASNUMERICTYPE(unsigned long long);
INSTANTIATE_GETBITSASNUMERICTYPE(unsigned short);
//==============================================================================
template <class T>
void RawDataField::setBitsAsNumericType(T type_var,
unsigned int start_bit,
unsigned int count)
{
// Prevent us from modifying const memory
constModeExceptionCheck();
// Handle bad input
if (start_bit >= length_bits || start_bit + count > length_bits)
{
throw std::out_of_range("Out-of-range bit(s) specified");
}
else if (count > sizeof(T) * BITS_PER_BYTE)
{
throw std::out_of_range("Not enough bits in the source type");
}
// Use the given variable as if it were raw data, and set bits inside it
RawDataField working_rdf(reinterpret_cast<std::uint8_t*>(&type_var),
sizeof(T),
misc::BYTES,
false,
bit_indexing_mode);
// Copy all the bits; an alternative implementation would be to memcpy the
// relevant data over, shift down and then mask out the irrelevant bits
for (unsigned int i = 0; i < count; ++i)
{
setBit(start_bit + i, working_rdf.getBit(i));
}
}
// Use this macro to instantiate setBitsAsNumericType() for all the intrinsic
// numeric types. "char" is arguably not a numeric type but let's include it
// since it's sometimes useful to use it as if it were a numeric type
#define INSTANTIATE_SETBITSASNUMERICTYPE(Type) \
template void \
RawDataField::setBitsAsNumericType(Type, unsigned int, unsigned int);
INSTANTIATE_SETBITSASNUMERICTYPE(char);
INSTANTIATE_SETBITSASNUMERICTYPE(double);
INSTANTIATE_SETBITSASNUMERICTYPE(float);
INSTANTIATE_SETBITSASNUMERICTYPE(int);
INSTANTIATE_SETBITSASNUMERICTYPE(long);
INSTANTIATE_SETBITSASNUMERICTYPE(long double);
INSTANTIATE_SETBITSASNUMERICTYPE(long long);
INSTANTIATE_SETBITSASNUMERICTYPE(short);
INSTANTIATE_SETBITSASNUMERICTYPE(unsigned char);
INSTANTIATE_SETBITSASNUMERICTYPE(unsigned int);
INSTANTIATE_SETBITSASNUMERICTYPE(unsigned long);
INSTANTIATE_SETBITSASNUMERICTYPE(unsigned long long);
INSTANTIATE_SETBITSASNUMERICTYPE(unsigned short);
//==============================================================================
void RawDataField::shiftUp(unsigned int shift_bits)
{
// Prevent us from modifying into const memory
constModeExceptionCheck();
if (shift_bits >= length_bits)
{
throw std::runtime_error(
"Requested shift amount must be less than the width of the field");
}
// A shift of 0 bits is a no-op
if (shift_bits == 0)
{
return;
}
for (unsigned int i = length_bits - 1; i != shift_bits - 1; --i)
{
setBit(i, getBit(i - shift_bits));
}
// Shift in zeros
for (unsigned int i = shift_bits - 1; i != 0; --i)
{
setBit(i, false);
}
// Bit 0 will always be unset. Ideally this would be done in the loop above
// but because we're looping down we have to stop that loop one short
setBit(0, false);
}
//==============================================================================
void RawDataField::shiftDown(unsigned int shift_bits)
{
// Prevent us from modifying into const memory
constModeExceptionCheck();
if (shift_bits >= length_bits)
{
throw std::runtime_error(
"Requested shift amount must be less than the width of the field");
}
// A shift of 0 bits is a no-op
if (shift_bits == 0)
{
return;
}
// Copy over the shifted bits
for (unsigned int i = 0; i < length_bits - shift_bits; i++)
{
setBit(i, getBit(i + shift_bits));
}
// Shift in zeros
for (unsigned int i = length_bits - shift_bits; i < length_bits; i++)
{
setBit(i, false);
}
}
//==============================================================================
RawDataField& RawDataField::operator=(const RawDataField& raw_data_field)
{
// Prevent us from modifying const memory
constModeExceptionCheck();
if (this != &raw_data_field)
{
raw_data_field.DataField::writeRaw(raw_data);
}
return *this;
}
//==============================================================================
bool operator==(const RawDataField& lhs, const RawDataField& rhs)
{
if (lhs.getLengthBits() != rhs.getLengthBits())
{
return false;
}
// We know both raw data fields have equal length at this point
unsigned int length_bits = lhs.getLengthBits();
for (unsigned int i = 0; i < length_bits; i++)
{
if (lhs.getBit(i) != rhs.getBit(i))
{
return false;
}
}
return true;
}
//==============================================================================
bool operator!=(const RawDataField& lhs, const RawDataField& rhs)
{
return !(lhs == rhs);
}