forked from huaerxiela/douyin-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ByteBuf.cpp
878 lines (853 loc) · 20.8 KB
/
ByteBuf.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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
#include "ByteBuf.h"
#include "ByteBuf.h"
namespace
{
#define INT8_BIT sizeof(int8_t)
#define INT16_BIT sizeof(int16_t)
#define INT32_BIT sizeof(int32_t)
#define INT64_BIT sizeof(int64_t)
#define FLOAT_BIT sizeof(float)
#define DOUBLE_BIT sizeof(double)
}
class ByteBufData
{
public:
ByteBufData()
: readerIndex(0), writerIndex(0), markedReaderIndex(0), markedWriterIndex(0)
{}
private:
friend class sh::ByteBuf;
std::string originData;
int32_t readerIndex;
int32_t writerIndex;
int32_t markedReaderIndex;
int32_t markedWriterIndex;
private:
template<class T>
T getT(int32_t index, uint8_t max_bit) const
{
if (originData.length() - index < max_bit)
{
return 0;
}
T value = 0;
for (uint8_t delta = 0; delta < max_bit; delta++)
{
value += (originData.at(delta + index) & 0xff) << (8 * (max_bit - delta - 1));
}
return value;
}
template<class T>
T getTLE(int32_t index, uint8_t max_bit) const
{
if (originData.length() - index < max_bit)
{
return 0;
}
T value = 0;
for (uint8_t delta = 0; delta < max_bit; delta++)
{
value += (originData.at(delta + index) & 0xff) << (8 * delta);
}
return value;
}
template<class T>
T readT(uint8_t max_bit)
{
if (originData.length() - readerIndex < max_bit)
{
return 0;
}
T value = 0;
for (uint8_t delta = 0; delta < max_bit; delta++)
{
value += (originData.at(delta + readerIndex) & 0xff) << (8 * (max_bit - delta - 1));
}
readerIndex += max_bit;
return value;
}
template<class T>
T readTLE(uint8_t max_bit)
{
if (originData.length() - readerIndex < max_bit)
{
return 0;
}
T value = 0;
for (uint8_t delta = 0; delta < max_bit; delta++)
{
value += (originData.at(delta + readerIndex) & 0xff) << (8 * delta);
}
readerIndex += max_bit;
return value;
}
template<class T>
void setT(int32_t index, T value)
{
setData(index, toBinary(value));
}
template<class T>
void setTLE(int32_t index, T value)
{
setData(index, toBinaryLE(value));
}
void setData(int32_t index, const std::string &data)
{
if (index > originData.length())
{
originData.append(data);
}
else
{
originData.insert(index, data);
}
}
template<class T>
void writeT(T value)
{
writeData(toBinary(value));
}
template<class T>
void writeTLE(T value)
{
writeData(toBinaryLE(value));
}
void writeData(const std::string &data)
{
if (writerIndex > originData.length())
{
originData.append(data);
writerIndex = (int32_t) originData.length();
}
else
{
originData.insert(writerIndex, data);
writerIndex += (int32_t) data.length();
}
}
template<class T>
std::string toBinary(T value)
{
std::string result;
int8_t max_bit = sizeof(T);
for (uint8_t index = 0; index < max_bit; index++)
{
result.push_back(value >> (8 * (max_bit - index - 1)) & 0xff);
}
return result;
}
template<class T>
std::string toBinaryLE(T value)
{
std::string result;
int8_t max_bit = sizeof(T);
for (uint8_t index = 0; index < max_bit; index++)
{
result.push_back(value >> (8 * index) & 0xff);
}
return result;
}
};
sh::ByteBuf::ByteBuf()
{
d = std::unique_ptr<::ByteBufData>(new ByteBufData());
}
sh::ByteBuf::ByteBuf(const char *data, int32_t size)
{
d = std::unique_ptr<::ByteBufData>(new ByteBufData());
d->originData.append(data, size);
d->writerIndex += size;
}
sh::ByteBuf::ByteBuf(const std::string &data)
{
d = std::unique_ptr<::ByteBufData>(new ByteBufData());
d->originData.append(data);
d->writerIndex += data.length();
}
sh::ByteBuf::ByteBuf(int32_t size, char ch)
{
d = std::unique_ptr<::ByteBufData>(new ByteBufData());
d->originData.append(size, ch);
d->writerIndex += size;
}
sh::ByteBuf::ByteBuf(const sh::ByteBuf &other)
{
d = std::unique_ptr<::ByteBufData>(new ByteBufData());
d->originData.append(other.d->originData);
d->readerIndex = other.d->readerIndex;
d->writerIndex = other.d->writerIndex;
d->markedReaderIndex = other.d->markedReaderIndex;
d->markedWriterIndex = other.d->markedWriterIndex;
}
sh::ByteBuf::~ByteBuf() = default;
sh::ByteBuf &sh::ByteBuf::operator=(const sh::ByteBuf &other)
{
if (&other != this)
{
d->originData.append(other.d->originData);
d->readerIndex = other.d->readerIndex;
d->writerIndex = other.d->writerIndex;
d->markedReaderIndex = other.d->markedReaderIndex;
d->markedWriterIndex = other.d->markedWriterIndex;
}
return *this;
}
std::string sh::ByteBuf::toHexString(Case type, const std::string &fill_str) const
{
std::string result;
for (char ch: d->originData)
{
if (Case::Lower == type)
{
result.push_back(toHexLower(ch >> 4));
result.push_back(toHexLower(ch & 0xF));
}
else
{
result.push_back(toHexUpper(ch >> 4));
result.push_back(toHexUpper(ch & 0xF));
}
result.append(fill_str);
}
return result;
}
const char *sh::ByteBuf::data() const noexcept
{
return d->originData.data();
}
int32_t sh::ByteBuf::bytesBefore(char value)
{
for (int32_t index = d->readerIndex; index < d->writerIndex; index++)
{
if (d->originData.at(index) == value)
{
return index - d->readerIndex;
}
}
return -1;
}
int32_t sh::ByteBuf::bytesBefore(int32_t length, char value)
{
return bytesBefore(d->readerIndex, length, value);
}
int32_t sh::ByteBuf::bytesBefore(int32_t index, int32_t length, char value)
{
bool last_ok;
int32_t second_index;
for (; index < d->writerIndex - length; index++)
{
//找到第一个符合 && 剩余的长度足够
if (d->originData.at(index) == value && (index + length) < d->writerIndex)
{
last_ok = true;
//从后一个开始检查length-1个字符
for (second_index = index + 1; second_index < (index + length); second_index++)
{
if (d->originData.at(second_index) != value)
{
last_ok = false;
break;
}
}
if (last_ok)
{
return index - d->readerIndex;
}
}
}
return -1;
}
int32_t sh::ByteBuf::capacity() const
{
return (int32_t) d->originData.capacity();
}
sh::ByteBuf &sh::ByteBuf::capacity(int32_t new_capacity)
{
d->originData.reserve(new_capacity);
return *this;
}
sh::ByteBuf &sh::ByteBuf::clear()
{
d->readerIndex = 0;
d->writerIndex = 0;
return *this;
}
int32_t sh::ByteBuf::compareTo(const sh::ByteBuf &buffer) const
{
return d->originData.compare(buffer.d->originData);
}
sh::ByteBuf sh::ByteBuf::copy() const
{
return sh::ByteBuf(d->originData);
}
sh::ByteBuf sh::ByteBuf::copy(int32_t index, int32_t length) const
{
return sh::ByteBuf(d->originData.substr(index, length));
}
sh::ByteBuf &sh::ByteBuf::discardReadBytes()
{
d->originData = d->originData.substr(d->readerIndex + 1);
d->readerIndex = 0;
d->writerIndex = 0;
return *this;
}
int32_t sh::ByteBuf::indexOf(int32_t fromIndex, int32_t toIndex, char value) const
{
for (int32_t index = fromIndex; index < d->originData.length() && index < toIndex; index++)
{
if (d->originData.at(index) == value)
{
return index;
}
}
return -1;
}
sh::ByteBuf sh::ByteBuf::setIndex(int32_t readerIndex, int32_t writerIndex)
{
if (d->originData.empty())
{
return *this;
}
d->readerIndex = readerIndex;
d->writerIndex = writerIndex;
if (d->readerIndex >= d->originData.length())
{
d->readerIndex = (int32_t) d->originData.length() - 1;
}
if (d->writerIndex > d->originData.length())
{
d->writerIndex = (int32_t) d->originData.length();
}
return *this;
}
bool sh::ByteBuf::isReadable() const
{
return (d->writerIndex - d->readerIndex) > 0;
}
bool sh::ByteBuf::isReadable(int32_t size)
{
return (d->writerIndex - d->readerIndex) >= size;
}
sh::ByteBuf &sh::ByteBuf::markReaderIndex()
{
d->markedReaderIndex = d->readerIndex;
return *this;
}
sh::ByteBuf &sh::ByteBuf::markWriterIndex()
{
d->markedWriterIndex = d->writerIndex;
return *this;
}
int32_t sh::ByteBuf::readerIndex() const
{
return d->readerIndex;
}
sh::ByteBuf &sh::ByteBuf::readerIndex(int32_t reader_index)
{
if (reader_index <= d->writerIndex)
{
d->readerIndex = reader_index;
}
return *this;
}
sh::ByteBuf &sh::ByteBuf::resetReaderIndex()
{
d->readerIndex = d->markedReaderIndex;
return *this;
}
int32_t sh::ByteBuf::writerIndex() const
{
return d->writerIndex;
}
sh::ByteBuf &sh::ByteBuf::writerIndex(int32_t writer_index)
{
if (writer_index < d->originData.length())
{
d->writerIndex = writer_index;
}
return *this;
}
sh::ByteBuf &sh::ByteBuf::resetWriterIndex()
{
d->writerIndex = d->markedWriterIndex;
return *this;
}
int32_t sh::ByteBuf::readableBytes() const
{
return d->writerIndex - d->readerIndex;
}
char sh::ByteBuf::getByte(int32_t index) const
{
if (index >= d->originData.length())
{
return 0;
}
return d->originData.at(index);
}
int32_t sh::ByteBuf::getBytes(int32_t index, char *dst) const
{
if (index >= d->originData.length())
{
return 0;
}
std::string res = d->originData.substr(index);
*dst = *res.data();
return (int32_t) res.length();
}
int32_t sh::ByteBuf::getBytes(int32_t index, char *dst, int32_t length) const
{
if (index >= d->originData.length())
{
return 0;
}
std::string res = d->originData.substr(index, length);
*dst = *res.data();
return (int32_t) res.length();
}
sh::ByteBuf sh::ByteBuf::getBytes(int32_t index) const
{
if (index >= d->originData.length())
{
return sh::ByteBuf();
}
std::string res = d->originData.substr(index);
return sh::ByteBuf(res);
}
sh::ByteBuf sh::ByteBuf::getBytes(int32_t index, int32_t length) const
{
if (index >= d->originData.length())
{
return sh::ByteBuf();
}
std::string res = d->originData.substr(index, length);
return sh::ByteBuf(res);
}
sh::ByteBuf sh::ByteBuf::getBytes(int32_t index, int32_t dstIndex, int32_t length) const
{
if (index >= d->originData.length())
{
return sh::ByteBuf();
}
sh::ByteBuf data(dstIndex, 0);
std::string res = d->originData.substr(index, length);
data.writeBytes(res.data(), (int32_t) res.length());
return data;
}
int8_t sh::ByteBuf::getChar(int32_t index) const
{
if (index >= d->originData.length())
{
return 0;
}
return d->originData.at(index);
}
uint8_t sh::ByteBuf::getUnsignedChar(int32_t index) const
{
return getChar(index);
}
int16_t sh::ByteBuf::getShort(int32_t index) const
{
return d->getT<int16_t>(index, INT16_BIT);
}
int16_t sh::ByteBuf::getShortLE(int32_t index) const
{
return d->getTLE<int16_t>(index, INT16_BIT);
}
uint16_t sh::ByteBuf::getUnsignedShort(int32_t index) const
{
return d->getT<int16_t>(index, INT16_BIT);
}
uint16_t sh::ByteBuf::getUnsignedShortLE(int32_t index) const
{
return d->getTLE<int16_t>(index, INT16_BIT);
}
int32_t sh::ByteBuf::getInt(int32_t index) const
{
return d->getT<int32_t>(index, INT32_BIT);
}
int32_t sh::ByteBuf::getIntLE(int32_t index) const
{
return d->getTLE<int32_t>(index, INT32_BIT);
}
uint32_t sh::ByteBuf::getUnsignedInt(int32_t index) const
{
return d->getT<int32_t>(index, INT32_BIT);
}
uint32_t sh::ByteBuf::getUnsignedIntLE(int32_t index) const
{
return d->getTLE<int32_t>(index, INT32_BIT);
}
int64_t sh::ByteBuf::getLong(int32_t index) const
{
return d->getT<int64_t>(index, INT64_BIT);
}
int64_t sh::ByteBuf::getLongLE(int32_t index) const
{
return d->getTLE<int64_t>(index, INT64_BIT);
}
uint64_t sh::ByteBuf::getUnsignedLong(int32_t index) const
{
return d->getT<int64_t>(index, INT64_BIT);
}
uint64_t sh::ByteBuf::getUnsignedLongLE(int32_t index) const
{
return d->getTLE<int64_t>(index, INT64_BIT);
}
float sh::ByteBuf::getFloat(int32_t index) const
{
float result = 0.00;
if (d->originData.length() - index < FLOAT_BIT)
{
return 0.00;
}
char *p = (char *) &result;
for (; index < index + FLOAT_BIT; index++)
{
*(p + index) = d->originData.at(index);
}
return result;
}
float sh::ByteBuf::getFloatLE(int32_t index) const
{
float result = 0.00;
if (d->originData.length() - index < FLOAT_BIT)
{
return 0.00;
}
char *p = (char *) &result;
for (; index < index + FLOAT_BIT; index++)
{
*(p + index) = d->originData.at(FLOAT_BIT - index - 1);
}
return result;
}
double sh::ByteBuf::getDouble(int32_t index) const
{
double result = 0.00;
if (d->originData.length() - index < DOUBLE_BIT)
{
return 0.00;
}
char *p = (char *) &result;
for (; index < index + DOUBLE_BIT; index++)
{
*(p + index) = d->originData.at(index);
}
return result;
}
double sh::ByteBuf::getDoubleLE(int32_t index) const
{
double result = 0.00;
if (d->originData.length() - index < DOUBLE_BIT)
{
return 0.00;
}
char *p = (char *) &result;
for (; index < index + DOUBLE_BIT; index++)
{
*(p + index) = d->originData.at(DOUBLE_BIT - index - 1);
}
return result;
}
int8_t sh::ByteBuf::readChar()
{
if (d->originData.length() - d->readerIndex < INT8_BIT)
{
return 0;
}
return d->originData.at(d->readerIndex++);
}
uint8_t sh::ByteBuf::readUnsignedChar()
{
return readChar();
}
int16_t sh::ByteBuf::readShort()
{
return d->readT<int16_t>(INT16_BIT);
}
int16_t sh::ByteBuf::readShortLE()
{
return d->readTLE<int16_t>(INT16_BIT);
}
uint16_t sh::ByteBuf::readUnsignedShort()
{
return d->readT<int16_t>(INT16_BIT);
}
uint16_t sh::ByteBuf::readUnsignedShortLE()
{
return d->readTLE<int16_t>(INT16_BIT);
}
int32_t sh::ByteBuf::readInt()
{
return d->readT<int32_t>(INT32_BIT);
}
int32_t sh::ByteBuf::readIntLE()
{
return d->readTLE<int32_t>(INT32_BIT);
}
uint32_t sh::ByteBuf::readUnsignedInt()
{
return d->readT<int32_t>(INT32_BIT);
}
uint32_t sh::ByteBuf::readUnsignedIntLE()
{
return d->readTLE<int32_t>(INT32_BIT);
}
int64_t sh::ByteBuf::readLong()
{
return d->readT<int64_t>(INT64_BIT);
}
int64_t sh::ByteBuf::readLongLE()
{
return d->readTLE<int64_t>(INT64_BIT);
}
uint64_t sh::ByteBuf::readUnsignedLong()
{
return d->readT<int64_t>(INT64_BIT);
}
uint64_t sh::ByteBuf::readUnsignedLongLE()
{
return d->readTLE<int64_t>(INT64_BIT);
}
sh::ByteBuf sh::ByteBuf::readBytes(int32_t length)
{
std::string data = d->originData.substr(d->readerIndex, length);
d->readerIndex += (int32_t) data.length();
return sh::ByteBuf(data);
}
double sh::ByteBuf::readDouble()
{
double result = 0.00;
if (d->originData.length() - d->readerIndex < DOUBLE_BIT)
{
return 0.00;
}
char *p = (char *) &result;
for (; d->readerIndex < d->readerIndex + DOUBLE_BIT; d->readerIndex++)
{
*(p + d->readerIndex) = d->originData.at(d->readerIndex);
}
return result;
}
double sh::ByteBuf::readDoubleLE()
{
double result = 0.00;
if (d->originData.length() - d->readerIndex < DOUBLE_BIT)
{
return 0.00;
}
char *p = (char *) &result;
for (; d->readerIndex < d->readerIndex + DOUBLE_BIT; d->readerIndex++)
{
*(p + d->readerIndex) = d->originData.at(DOUBLE_BIT - d->readerIndex - 1);
}
return result;
}
sh::ByteBuf &sh::ByteBuf::skipBytes(int32_t length)
{
d->readerIndex += length;
if (d->readerIndex > d->originData.length())
{
d->readerIndex = (int32_t) d->originData.length();
}
return *this;
}
sh::ByteBuf &sh::ByteBuf::setChar(int32_t index, int8_t value)
{
d->setT(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setUnsignedChar(int32_t index, uint8_t value)
{
d->setT(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setShort(int32_t index, int16_t value)
{
d->setT(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setShortLE(int32_t index, int16_t value)
{
d->setTLE(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setUnsignedShort(int32_t index, uint16_t value)
{
d->setT(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setUnsignedShortLE(int32_t index, uint16_t value)
{
d->setTLE(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setInt(int32_t index, int32_t value)
{
d->setT(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setIntLE(int32_t index, int32_t value)
{
d->setTLE(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setUnsignedInt(int32_t index, uint32_t value)
{
d->setT(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setUnsignedIntLE(int32_t index, uint32_t value)
{
d->setTLE(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setLong(int32_t index, int64_t value)
{
d->setT(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setLongLE(int32_t index, int64_t value)
{
d->setTLE(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setUnsignedLong(int32_t index, uint64_t value)
{
d->setT(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setUnsignedLongLE(int32_t index, uint64_t value)
{
d->setTLE(index, value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::setBytes(int32_t index, const ByteBuf &buf)
{
d->setData(index, buf.data());
return *this;
}
sh::ByteBuf &sh::ByteBuf::setBytes(int32_t index, const char *data, int32_t size)
{
d->setData(index, std::string(data, size));
return *this;
}
sh::ByteBuf &sh::ByteBuf::setZero(int32_t index, int32_t length)
{
d->setData(index, std::string(length, 0));
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeChar(int8_t value)
{
d->writeT(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeUnsignedChar(uint8_t value)
{
d->writeT(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeShort(int16_t value)
{
d->writeT(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeShortLE(int16_t value)
{
d->writeTLE(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeUnsignedShort(uint16_t value)
{
d->writeT(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeUnsignedShortLE(uint16_t value)
{
d->writeTLE(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeInt(int32_t value)
{
d->writeT(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeIntLE(int32_t value)
{
d->writeTLE(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeUnsignedInt(uint32_t value)
{
d->writeT(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeUnsignedIntLE(uint32_t value)
{
d->writeTLE(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeLong(int64_t value)
{
d->writeT(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeLongLE(int64_t value)
{
d->writeTLE(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeUnsignedLong(uint64_t value)
{
d->writeT(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeUnsignedLongLE(uint64_t value)
{
d->writeTLE(value);
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeBytes(const ByteBuf &buf)
{
d->writeData(buf.data());
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeBytes(const char *data, int32_t size)
{
d->writeData(std::string(data, size));
return *this;
}
sh::ByteBuf &sh::ByteBuf::writeZero(int32_t length)
{
d->writeData(std::string(length, 0));
return *this;
}
char sh::ByteBuf::toHexLower(uint32_t value) noexcept
{
return "0123456789abcdef"[value & 0xF];
}
char sh::ByteBuf::toHexUpper(uint32_t value) noexcept
{
return "0123456789ABCDEF"[value & 0xF];
}
int32_t sh::ByteBuf::fromHex(uint32_t c) noexcept
{
return ((c >= '0') && (c <= '9')) ? int32_t(c - '0') :
((c >= 'A') && (c <= 'F')) ? int32_t(c - 'A' + 10) :
((c >= 'a') && (c <= 'f')) ? int32_t(c - 'a' + 10) :
/* otherwise */ -1;
}
sh::ByteBuf sh::ByteBuf::fromHex(const std::string &hexEncoded) noexcept
{
sh::ByteBuf data;
char ch = 0x00;
for (int32_t index = 0; index < hexEncoded.length() - 1; index++)
{
ch |= (char) fromHex(hexEncoded.at(index++)) << 4;
ch |= (char) fromHex(hexEncoded.at(index));
data.writeChar(ch);
ch = 0x00;
}
return data;
}