Repository navigation
Expand file tree
/
Copy pathByteBuffer.h
More file actions
545 lines (483 loc) · 13.2 KB
/
Copy pathByteBuffer.h
File metadata and controls
545 lines (483 loc) · 13.2 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#ifndef __BYTEBUFFER_H__
#define __BYTEBUFFER_H__
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#include <stdexcept>
// Default size of the buffer
constexpr uint32_t DEFAULT_BUFFER_SIZE = 2048;
// Mark value indicating no mark is set
constexpr uint32_t MARK_UNSET = UINT32_MAX;
// Byte order enumeration for cross-platform data serialization
enum class ByteOrder {
ORDER_BIG_ENDIAN, // Most significant byte first (network byte order)
ORDER_LITTLE_ENDIAN // Least significant byte first
};
class ByteBuffer
{
public:
// Disable copy constructor and assignment operator to prevent double-free issues caused by shallow copy
ByteBuffer(const ByteBuffer&) = delete;
ByteBuffer& operator=(const ByteBuffer&) = delete;
// Allow move semantics
ByteBuffer(ByteBuffer&& other) noexcept
: mark_(other.mark_),
limit_(other.limit_),
position_(other.position_),
capacity_(other.capacity_),
name_(std::move(other.name_)),
buffer_(std::move(other.buffer_))
{
other.mark_ = MARK_UNSET;
other.limit_ = 0;
other.position_ = 0;
other.capacity_ = 0;
}
ByteBuffer& operator=(ByteBuffer&& other) noexcept
{
if (this != &other)
{
mark_ = other.mark_;
limit_ = other.limit_;
position_ = other.position_;
capacity_ = other.capacity_;
name_ = std::move(other.name_);
buffer_ = std::move(other.buffer_);
other.mark_ = MARK_UNSET;
other.limit_ = 0;
other.position_ = 0;
other.capacity_ = 0;
}
return *this;
}
ByteBuffer(uint32_t capacity = DEFAULT_BUFFER_SIZE, const char* name = "")
: mark_(MARK_UNSET),
limit_(capacity),
position_(0),
capacity_(capacity),
name_(name),
buffer_(capacity, 0)
{
}
ByteBuffer(uint8_t* arr, uint32_t length, const char* name = "")
: mark_(MARK_UNSET),
limit_(length),
position_(0),
capacity_(length),
name_(name),
buffer_(length, 0)
{
putBytes(arr, capacity_);
clear();
}
~ByteBuffer() = default;
// Write Methods
ByteBuffer& put(ByteBuffer* bb)
{
for (uint32_t i = 0; i < bb->limit(); i++)
append<uint8_t>(bb->get(i));
return *this;
}
ByteBuffer& put(uint8_t value)
{
append<uint8_t>(value);
return *this;
}
ByteBuffer& put(uint8_t value, uint32_t index)
{
insert<uint8_t>(value, index);
return *this;
}
ByteBuffer& putBytes(const uint8_t* buf, uint32_t len)
{
for (uint32_t i = 0; i < len; i++)
append<uint8_t>(buf[i]);
return *this;
}
ByteBuffer& putBytes(const uint8_t* buf, uint32_t len, uint32_t index)
{
position_ = index;
for (uint32_t i = 0; i < len; i++)
append<uint8_t>(buf[i]);
return *this;
}
ByteBuffer& putChar(char value)
{
append<uint8_t>(value);
return *this;
}
ByteBuffer& putChar(char value, uint32_t index)
{
insert<uint8_t>(value, index);
return *this;
}
ByteBuffer& putShort(uint16_t value)
{
append<uint16_t>(value);
return *this;
}
ByteBuffer& putShort(uint16_t value, uint32_t index)
{
insert<uint16_t>(value, index);
return *this;
}
ByteBuffer& putInt(uint32_t value)
{
append<uint32_t>(value);
return *this;
}
ByteBuffer& putInt(uint32_t value, uint32_t index)
{
insert<uint32_t>(value, index);
return *this;
}
ByteBuffer& putLong(uint64_t value)
{
append<uint64_t>(value);
return *this;
}
ByteBuffer& putLong(uint64_t value, uint32_t index)
{
insert<uint64_t>(value, index);
return *this;
}
ByteBuffer& putFloat(float value)
{
append<float>(value);
return *this;
}
ByteBuffer& putFloat(float value, uint32_t index)
{
insert<float>(value, index);
return *this;
}
ByteBuffer& putDouble(double value)
{
append<double>(value);
return *this;
}
ByteBuffer& putDouble(double value, uint32_t index)
{
insert<double>(value, index);
return *this;
}
// Read Methods
uint8_t get()
{
return read<uint8_t>();
}
uint8_t get(uint32_t index) const
{
return read<uint8_t>(index);
}
void getBytes(uint8_t* buf, uint32_t len)
{
if (buffer_.empty() || position_ + len > limit_)
return;
std::copy(buffer_.begin() + position_, buffer_.begin() + position_ + len, buf);
position_ += len;
}
void getBytes(uint32_t index, uint8_t* buf, uint32_t len) const
{
if (buffer_.empty() || index + len > limit_)
return;
std::copy(buffer_.begin() + index, buffer_.begin() + index + len, buf);
}
char getChar()
{
return read<uint8_t>();
}
char getChar(uint32_t index) const
{
return read<uint8_t>(index);
}
uint16_t getShort()
{
return read<uint16_t>();
}
uint16_t getShort(uint32_t index) const
{
return read<uint16_t>(index);
}
uint32_t getInt()
{
return read<uint32_t>();
}
uint32_t getInt(uint32_t index) const
{
return read<uint32_t>(index);
}
uint64_t getLong()
{
return read<uint64_t>();
}
uint64_t getLong(uint32_t index) const
{
return read<uint64_t>(index);
}
float getFloat()
{
return read<float>();
}
float getFloat(uint32_t index) const
{
return read<float>(index);
}
double getDouble()
{
return read<double>();
}
double getDouble(uint32_t index) const
{
return read<double>(index);
}
bool equals(const ByteBuffer* other) const
{
uint32_t len = limit();
if (len != other->limit())
return false;
for (uint32_t i = 0; i < len; i++)
{
if (get(i) != other->get(i))
return false;
}
return true;
}
ByteBuffer* duplicate()
{
ByteBuffer* newBuffer = new ByteBuffer(capacity_);
// copy data
newBuffer->put(this);
newBuffer->limit(limit_);
newBuffer->position(position_);
return newBuffer;
}
ByteBuffer& clear()
{
position_ = 0;
mark_ = MARK_UNSET;
limit_ = capacity_;
return *this;
}
ByteBuffer& flip()
{
limit_ = position_;
position_ = 0;
mark_ = MARK_UNSET;
return *this;
}
ByteBuffer& mark()
{
mark_ = position_;
return *this;
}
ByteBuffer& discardMark()
{
mark_ = MARK_UNSET;
return *this;
}
ByteBuffer& reset()
{
if (mark_ != MARK_UNSET)
position_ = mark_;
return *this;
}
ByteBuffer& rewind()
{
mark_ = MARK_UNSET;
position_ = 0;
return *this;
}
ByteBuffer& compact()
{
if (position_ >= limit_)
{
position_ = 0;
}
else
{
uint32_t remaining = limit_ - position_;
std::copy(buffer_.begin() + position_, buffer_.begin() + limit_, buffer_.begin());
position_ = remaining;
}
limit_ = capacity_;
return *this;
}
bool hasRemaining() const
{
return limit_ > position_;
}
uint32_t remaining() const
{
return position_ < limit_ ? limit_ - position_ : 0;
}
uint32_t capacity() const
{
return capacity_;
}
uint32_t position() const
{
return position_;
}
uint32_t limit() const
{
return limit_;
}
ByteBuffer& limit(uint32_t newLimit)
{
if (position_ > newLimit)
position_ = newLimit;
if (mark_ > newLimit)
mark_ = MARK_UNSET;
return *this;
}
ByteBuffer& position(uint32_t newPosition)
{
position_ = newPosition;
return *this;
}
void printInfo() const
{
std::cout << "ByteBuffer " << name_ << ":\n"
<< "\tmark(" << mark_ << "), "
<< "position(" << position_ << "), "
<< "limit(" << limit_ << "), "
<< "capacity(" << capacity_ << ")." << std::endl;
}
// Array access methods (Java compatibility)
bool hasArray() const { return true; }
uint8_t* array() { return buffer_.data(); }
const uint8_t* array() const { return buffer_.data(); }
uint32_t arrayOffset() const { return 0; }
// Direct buffer check (Java compatibility)
bool isDirect() const { return false; }
// Byte order methods (Java compatibility)
ByteOrder order() const { return byteOrder_; }
ByteBuffer& order(ByteOrder order) { byteOrder_ = order; return *this; }
// Compare buffers (Java compareTo compatibility)
int compareTo(const ByteBuffer& other) const
{
uint32_t len = std::min(limit_, other.limit_);
for (uint32_t i = 0; i < len; i++)
{
uint8_t a = buffer_[i];
uint8_t b = other.buffer_[i];
if (a != b)
return static_cast<int>(a) - static_cast<int>(b);
}
return static_cast<int>(limit_) - static_cast<int>(other.limit_);
}
// Hash function for use in unordered containers
size_t hash() const
{
size_t h = 0;
for (uint32_t i = 0; i < limit_; i++)
{
h = h * 31 + buffer_[i];
}
return h;
}
// String representation (hex dump)
std::string toString() const
{
char buf[64];
std::snprintf(buf, sizeof(buf), "ByteBuffer[pos=%u lim=%u cap=%u]",
position_, limit_, capacity_);
return std::string(buf);
}
// Static method to get native byte order
static ByteOrder nativeOrder()
{
uint16_t test = 0x0001;
return (*reinterpret_cast<uint8_t*>(&test) == 0x01)
? ByteOrder::ORDER_LITTLE_ENDIAN
: ByteOrder::ORDER_BIG_ENDIAN;
}
private:
// Byte swap helper for endianness conversion
template<typename T>
static T byteswap(T value)
{
T result = 0;
uint8_t* src = reinterpret_cast<uint8_t*>(&value);
uint8_t* dst = reinterpret_cast<uint8_t*>(&result);
for (uint32_t i = 0; i < sizeof(T); i++)
{
dst[i] = src[sizeof(T) - 1 - i];
}
return result;
}
template <typename T>
T read(uint32_t index) const
{
if (index + sizeof(T) > limit_)
return 0;
T data;
memcpy(&data, &buffer_[index], sizeof(T));
// Apply byte order conversion if needed
if (byteOrder_ != nativeOrder())
{
data = byteswap(data);
}
return data;
}
template <typename T>
T read()
{
T data = read<T>(position_);
position_ += sizeof(T);
return data;
}
template<typename T>
void append(T data)
{
uint32_t s = sizeof(data);
checkSize(s);
// Apply byte order conversion if needed
if (byteOrder_ != nativeOrder())
{
T swapped = byteswap(data);
memcpy(&buffer_[position_], reinterpret_cast<uint8_t*>(&swapped), s);
}
else
{
memcpy(&buffer_[position_], reinterpret_cast<uint8_t*>(&data), s);
}
position_ += s;
}
template<typename T>
void insert(T data, uint32_t index)
{
uint32_t s = sizeof(data);
checkSize(index, s);
position_ = index;
append<T>(data);
}
void checkSize(uint32_t increase)
{
checkSize(position_, increase);
}
void checkSize(uint32_t index, uint32_t increase)
{
if (index + increase <= capacity_)
return;
uint32_t newSize = capacity_ + (increase + BUFFER_SIZE_INCREASE - 1) /
BUFFER_SIZE_INCREASE * BUFFER_SIZE_INCREASE;
buffer_.resize(newSize, 0);
capacity_ = newSize;
}
private:
const uint32_t BUFFER_SIZE_INCREASE = 2048;
std::string name_;
std::vector<uint8_t> buffer_;
uint32_t mark_;
uint32_t limit_;
uint32_t position_;
uint32_t capacity_;
ByteOrder byteOrder_ = nativeOrder(); // Default to native byte order
};
#endif