-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcfdcore_bytedata.cpp
667 lines (569 loc) · 18.5 KB
/
cfdcore_bytedata.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
// Copyright 2019 CryptoGarage
/**
* @file cfdcore_bytedata.cpp
*
* @brief implimentation of ByteData class
*/
#include "cfdcore/cfdcore_bytedata.h"
#include <limits>
#include <string>
#include <vector>
#include "cfdcore/cfdcore_exception.h"
#include "cfdcore/cfdcore_logger.h"
#include "cfdcore/cfdcore_util.h"
namespace cfd {
namespace core {
using logger::warn;
//////////////////////////////////
/// Internal function
//////////////////////////////////
/**
* @brief check big endian.
* @retval true big endian.
* @retval false little endian.
*/
static bool IsBigEndian() {
static const uint32_t k32bitValue = 0x04030201;
static bool* is_big_endian = nullptr;
static bool kTrue = true;
static bool kFalse = false;
if (is_big_endian == nullptr) {
uint8_t buf[4];
memcpy(buf, &k32bitValue, sizeof(buf));
if (buf[0] == 1) {
// little
is_big_endian = &kFalse;
} else {
is_big_endian = &kTrue;
}
}
return *is_big_endian;
}
//////////////////////////////////
/// ByteData
//////////////////////////////////
ByteData::ByteData() : data_(0) {
// do nothing
}
ByteData::ByteData(const std::vector<uint8_t>& vector) : data_(vector) {
if (data_.size() > std::numeric_limits<uint32_t>::max()) {
warn(CFD_LOG_SOURCE, "It exceeds the handling size.");
throw CfdException(kCfdIllegalStateError, "It exceeds the handling size.");
}
}
ByteData::ByteData(const std::string& hex)
: data_(StringUtil::StringToByte(hex)) {}
ByteData::ByteData(const uint8_t* buffer, uint32_t size) : data_(size) {
if (buffer == nullptr) {
if (size == 0) {
// create empty buffer
} else {
warn(CFD_LOG_SOURCE, "buffer is null.");
throw CfdException(kCfdIllegalArgumentError, "buffer is null.");
}
} else if (size != 0) {
data_.resize(size);
memcpy(data_.data(), buffer, size);
}
}
ByteData::ByteData(const uint8_t single_byte) : data_(1) {
data_[0] = single_byte;
}
std::string ByteData::GetHex() const {
return StringUtil::ByteToString(data_);
}
std::vector<uint8_t> ByteData::GetBytes() const { return data_; }
size_t ByteData::GetDataSize() const { return data_.size(); }
bool ByteData::Empty() const { return IsEmpty(); }
bool ByteData::IsEmpty() const { return data_.size() == 0; }
bool ByteData::Equals(const ByteData& bytedata) const {
if (data_ == bytedata.data_) {
return true;
}
return false;
}
uint8_t ByteData::GetHeadData() const {
return (data_.empty()) ? 0 : data_[0];
}
ByteData ByteData::Serialize() const {
Serializer obj(static_cast<uint32_t>(data_.size()));
obj.AddVariableBuffer(data_.data(), static_cast<uint32_t>(data_.size()));
return obj.Output();
}
size_t ByteData::GetSerializeSize() const {
return Serializer::GetVariableIntSize(data_.size()) + data_.size();
}
ByteData ByteData::GetVariableInt(uint64_t v) {
Serializer obj(sizeof(v) + 1);
obj.AddVariableInt(v);
return obj.Output();
}
bool ByteData::IsLarge(const ByteData& source, const ByteData& destination) {
return source.data_ < destination.data_;
}
std::vector<ByteData> ByteData::SplitData(
const std::vector<uint32_t>& split_size_list) const {
std::vector<ByteData> result;
uint32_t offset = 0;
uint32_t max = static_cast<uint32_t>(data_.size());
for (uint32_t size : split_size_list) {
if (size == 0) {
result.emplace_back(ByteData());
} else if ((offset + size) > max) {
throw CfdException(
kCfdIllegalArgumentError, "total size is maximum over.");
} else {
result.emplace_back(&data_[offset], size);
offset += size;
}
}
return result;
}
ByteData ByteData::SplitData(uint32_t size_from_top) const {
auto ret = SplitData(std::vector<uint32_t>{size_from_top});
return ret[0];
}
void ByteData::Push(const ByteData& back_insert_data) {
if (back_insert_data.IsEmpty()) return;
const std::vector<uint8_t>& insert_bytes = back_insert_data.data_;
data_.reserve(data_.size() + insert_bytes.size() + 8);
std::copy(
insert_bytes.begin(), insert_bytes.end(), std::back_inserter(data_));
}
void ByteData::Push(const ByteData160& back_insert_data) {
std::vector<uint8_t> insert_bytes = back_insert_data.GetBytes();
data_.reserve(data_.size() + insert_bytes.size() + 8);
std::copy(
insert_bytes.begin(), insert_bytes.end(), std::back_inserter(data_));
}
void ByteData::Push(const ByteData256& back_insert_data) {
std::vector<uint8_t> insert_bytes = back_insert_data.GetBytes();
data_.reserve(data_.size() + insert_bytes.size() + 8);
std::copy(
insert_bytes.begin(), insert_bytes.end(), std::back_inserter(data_));
}
bool ByteData::operator==(const ByteData& object) const {
return (data_ == object.data_);
}
//////////////////////////////////
/// ByteData160
//////////////////////////////////
ByteData160::ByteData160() : data_(std::vector<uint8_t>(kByteData160Length)) {
memset(data_.data(), 0, data_.size());
}
ByteData160::ByteData160(const std::vector<uint8_t>& vector)
: data_(std::vector<uint8_t>(kByteData160Length)) {
if (vector.size() != kByteData160Length) {
warn(CFD_LOG_SOURCE, "ByteData160 size unmatch. size={}.", vector.size());
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ByteData160 size unmatch.");
}
data_ = vector;
}
ByteData160::ByteData160(const std::string& hex)
: data_(std::vector<uint8_t>(kByteData160Length)) {
std::vector<uint8_t> vector = StringUtil::StringToByte(hex);
if (vector.size() != kByteData160Length) {
warn(CFD_LOG_SOURCE, "ByteData160 size unmatch. size={}.", vector.size());
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ByteData160 size unmatch.");
}
data_ = vector;
}
ByteData160::ByteData160(const ByteData& byte_data)
: ByteData160(byte_data.GetBytes()) {}
std::string ByteData160::GetHex() const {
return StringUtil::ByteToString(data_);
}
std::vector<uint8_t> ByteData160::GetBytes() const { return data_; }
bool ByteData160::Empty() const { return IsEmpty(); }
bool ByteData160::IsEmpty() const {
std::vector<uint8_t> data(kByteData160Length);
memset(data.data(), 0, data.size());
return data_ == data;
}
bool ByteData160::Equals(const ByteData160& bytedata) const {
if (data_ == bytedata.data_) {
return true;
}
return false;
}
ByteData ByteData160::GetData() const { return ByteData(data_); }
uint8_t ByteData160::GetHeadData() const { return data_[0]; }
ByteData ByteData160::Serialize() const {
Serializer obj(static_cast<uint32_t>(data_.size()));
obj.AddVariableBuffer(data_.data(), static_cast<uint32_t>(data_.size()));
return obj.Output();
}
bool ByteData160::operator==(const ByteData160& object) const {
return (data_ == object.data_);
}
//////////////////////////////////
/// ByteData256
//////////////////////////////////
ByteData256::ByteData256() : data_(std::vector<uint8_t>(kByteData256Length)) {
memset(data_.data(), 0, data_.size());
}
ByteData256::ByteData256(const std::vector<uint8_t>& vector)
: data_(std::vector<uint8_t>(kByteData256Length)) {
if (vector.size() != kByteData256Length) {
warn(CFD_LOG_SOURCE, "ByteData256 size unmatch. size={}.", vector.size());
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ByteData256 size unmatch.");
}
data_ = vector;
}
ByteData256::ByteData256(const std::string& hex)
: data_(std::vector<uint8_t>(kByteData256Length)) {
std::vector<uint8_t> vector = StringUtil::StringToByte(hex);
if (vector.size() != kByteData256Length) {
warn(CFD_LOG_SOURCE, "ByteData256 size unmatch. size={}.", vector.size());
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ByteData256 size unmatch.");
}
data_ = vector;
}
ByteData256::ByteData256(const ByteData& byte_data)
: ByteData256(byte_data.GetBytes()) {}
std::string ByteData256::GetHex() const {
return StringUtil::ByteToString(data_);
}
std::vector<uint8_t> ByteData256::GetBytes() const { return data_; }
bool ByteData256::Empty() const { return IsEmpty(); }
bool ByteData256::IsEmpty() const {
std::vector<uint8_t> data(kByteData256Length);
memset(data.data(), 0, data.size());
return data_ == data;
}
bool ByteData256::Equals(const ByteData256& bytedata) const {
if (data_ == bytedata.data_) {
return true;
}
return false;
}
ByteData ByteData256::GetData() const { return ByteData(data_); }
uint8_t ByteData256::GetHeadData() const { return data_[0]; }
ByteData ByteData256::Serialize() const {
Serializer obj(static_cast<uint32_t>(data_.size()));
obj.AddVariableBuffer(data_.data(), static_cast<uint32_t>(data_.size()));
return obj.Output();
}
bool ByteData256::operator==(const ByteData256& object) const {
return (data_ == object.data_);
}
//////////////////////////////////
/// Serializer
//////////////////////////////////
Serializer::Serializer() : buffer_(8), offset_(0) {
// do nothing
}
Serializer::Serializer(uint32_t initial_size)
: buffer_(initial_size + 9), offset_(0) {
// do nothing
}
Serializer::Serializer(const Serializer& object)
: buffer_(object.buffer_), offset_(object.offset_) {
// do nothing
}
Serializer& Serializer::operator=(const Serializer& object) {
if (this != &object) {
buffer_ = object.buffer_;
offset_ = object.offset_;
}
return *this;
}
bool Serializer::IsBigEndian() { return cfd::core::IsBigEndian(); }
void Serializer::CheckNeedSize(uint32_t need_size) {
size_t size = buffer_.size() - static_cast<size_t>(offset_);
if (size < need_size) {
size_t cap = buffer_.capacity() - static_cast<size_t>(offset_);
if (cap < (need_size * 2)) {
buffer_.reserve(buffer_.capacity() + (need_size * 10));
}
buffer_.resize(buffer_.size() + (need_size * 2));
}
}
uint32_t Serializer::GetVariableIntSize(uint64_t value) {
if (value <= kViMax8)
return 1;
else if (value <= std::numeric_limits<uint16_t>::max())
return 3;
else if (value <= std::numeric_limits<uint32_t>::max())
return 5;
else
return 9;
}
void Serializer::AddVariableInt(uint64_t value) {
// TODO(k-matsuzawa) need endian support.
CheckNeedSize(9);
uint8_t* buf = &buffer_.data()[offset_];
if (value <= kViMax8) {
*buf = static_cast<uint8_t>(value);
++offset_;
} else if (value <= std::numeric_limits<uint16_t>::max()) {
*buf = kViTag16;
++buf;
uint16_t v16 = static_cast<uint16_t>(value);
memcpy(buf, &v16, sizeof(v16));
offset_ += sizeof(v16) + 1;
} else if (value <= std::numeric_limits<uint32_t>::max()) {
*buf = kViTag32;
++buf;
uint32_t v32 = static_cast<uint32_t>(value);
memcpy(buf, &v32, sizeof(v32));
offset_ += sizeof(v32) + 1;
} else {
*buf = kViTag64;
++buf;
uint64_t v64 = value;
memcpy(buf, &v64, sizeof(v64));
offset_ += sizeof(v64) + 1;
}
}
void Serializer::AddVariableBuffer(const ByteData& buffer) {
auto buf = buffer.GetBytes();
if (buf.size() > std::numeric_limits<uint32_t>::max()) {
warn(CFD_LOG_SOURCE, "It exceeds the handling size.");
throw CfdException(kCfdIllegalStateError, "It exceeds the handling size.");
}
AddVariableBuffer(buf.data(), static_cast<uint32_t>(buf.size()));
}
void Serializer::AddPrefixBuffer(uint64_t prefix, const ByteData& buffer) {
auto buf = buffer.GetBytes();
if (buf.size() > std::numeric_limits<uint32_t>::max()) {
warn(CFD_LOG_SOURCE, "It exceeds the handling size.");
throw CfdException(kCfdIllegalStateError, "It exceeds the handling size.");
}
AddPrefixBuffer(prefix, buf.data(), static_cast<uint32_t>(buf.size()));
}
void Serializer::AddDirectBytes(const ByteData& buffer) {
auto buf = buffer.GetBytes();
if (buf.size() > std::numeric_limits<uint32_t>::max()) {
warn(CFD_LOG_SOURCE, "It exceeds the handling size.");
throw CfdException(kCfdIllegalStateError, "It exceeds the handling size.");
}
AddDirectBytes(buf.data(), static_cast<uint32_t>(buf.size()));
}
void Serializer::AddDirectBytes(const ByteData256& buffer) {
auto buf = buffer.GetBytes();
AddDirectBytes(buf.data(), static_cast<uint32_t>(buf.size()));
}
void Serializer::AddVariableBuffer(
const uint8_t* buffer, uint32_t buffer_size) {
AddVariableInt(buffer_size);
AddDirectBytes(buffer, buffer_size);
}
void Serializer::AddPrefixBuffer(
uint64_t prefix, const uint8_t* buffer, uint32_t buffer_size) {
uint32_t size = GetVariableIntSize(prefix) + buffer_size;
AddVariableInt(size);
AddVariableInt(prefix);
AddDirectBytes(buffer, buffer_size);
}
void Serializer::AddDirectBytes(const uint8_t* buffer, uint32_t buffer_size) {
if ((buffer != nullptr) && (buffer_size != 0)) {
CheckNeedSize(buffer_size);
uint8_t* buf = &buffer_.data()[offset_];
memcpy(buf, buffer, buffer_size);
offset_ += buffer_size;
}
}
void Serializer::AddDirectByte(uint8_t byte_data) {
CheckNeedSize(4);
uint8_t* buf = &buffer_.data()[offset_];
*buf = byte_data;
++offset_;
}
void Serializer::AddDirectNumber(uint32_t number) {
CheckNeedSize(sizeof(number));
uint8_t* buf = &buffer_.data()[offset_];
// TODO(k-matsuzawa) need endian support.
memcpy(buf, &number, sizeof(number));
offset_ += sizeof(number);
}
void Serializer::AddDirectNumber(uint64_t number) {
CheckNeedSize(sizeof(number));
uint8_t* buf = &buffer_.data()[offset_];
// TODO(k-matsuzawa) need endian support.
memcpy(buf, &number, sizeof(number));
offset_ += sizeof(number);
}
void Serializer::AddDirectNumber(int64_t number) {
CheckNeedSize(sizeof(number));
uint8_t* buf = &buffer_.data()[offset_];
// TODO(k-matsuzawa) need endian support.
memcpy(buf, &number, sizeof(number));
offset_ += sizeof(number);
}
void Serializer::AddDirectBigEndianNumber(uint32_t number) {
CheckNeedSize(sizeof(number));
uint8_t* buf = &buffer_.data()[offset_];
if (IsBigEndian()) {
memcpy(buf, &number, sizeof(number));
} else {
uint8_t tmp_buf[4] = {
static_cast<uint8_t>((number & 0xff000000) >> 24),
static_cast<uint8_t>((number & 0x00ff0000) >> 16),
static_cast<uint8_t>((number & 0x0000ff00) >> 8),
static_cast<uint8_t>(number & 0x000000ff),
};
memcpy(buf, tmp_buf, sizeof(tmp_buf));
}
offset_ += sizeof(number);
}
Serializer& Serializer::operator<<(const ByteData& buffer) {
AddDirectBytes(buffer);
return *this;
}
Serializer& Serializer::operator<<(const ByteData256& buffer) {
AddDirectBytes(buffer);
return *this;
}
Serializer& Serializer::operator<<(uint8_t byte_data) {
AddDirectByte(byte_data);
return *this;
}
Serializer& Serializer::operator<<(uint32_t number) {
AddDirectNumber(number);
return *this;
}
Serializer& Serializer::operator<<(uint64_t number) {
AddDirectNumber(number);
return *this;
}
Serializer& Serializer::operator<<(int64_t number) {
AddDirectNumber(number);
return *this;
}
ByteData Serializer::Output() { return ByteData(buffer_.data(), offset_); }
Deserializer::Deserializer(const std::vector<uint8_t>& buffer)
: buffer_(buffer), offset_(0) {
// do nothing
}
Deserializer::Deserializer(const ByteData& buffer)
: Deserializer(buffer.GetBytes()) {
// do nothing
}
Deserializer::Deserializer(const Deserializer& object)
: buffer_(object.buffer_), offset_(object.offset_) {
// do nothing
}
Deserializer& Deserializer::operator=(const Deserializer& object) {
if (this != &object) {
buffer_ = object.buffer_;
offset_ = object.offset_;
}
return *this;
}
uint64_t Deserializer::ReadUint64() {
uint64_t result = 0;
CheckReadSize(sizeof(result));
memcpy(&result, &buffer_.data()[offset_], sizeof(result));
offset_ += sizeof(result);
return result;
}
uint32_t Deserializer::ReadUint32() {
uint32_t result = 0;
CheckReadSize(sizeof(result));
memcpy(&result, &buffer_.data()[offset_], sizeof(result));
offset_ += sizeof(result);
return result;
}
uint8_t Deserializer::ReadUint8() {
uint8_t result = 0;
CheckReadSize(sizeof(result));
memcpy(&result, &buffer_.data()[offset_], sizeof(result));
offset_ += sizeof(result);
return result;
}
uint32_t Deserializer::ReadUint32FromBigEndian() {
uint32_t result = 0;
CheckReadSize(sizeof(result));
if (IsBigEndian()) {
memcpy(&result, &buffer_.data()[offset_], sizeof(result));
} else {
uint8_t tmp_buf[4];
memcpy(tmp_buf, &buffer_.data()[offset_], sizeof(tmp_buf));
result = static_cast<uint32_t>(tmp_buf[3] & 0x000000ff) +
static_cast<uint32_t>((tmp_buf[2] << 8) & 0x0000ff00) +
static_cast<uint32_t>((tmp_buf[1] << 16) & 0x00ff0000) +
static_cast<uint32_t>((tmp_buf[0] << 24) & 0xff000000);
}
offset_ += sizeof(result);
return result;
}
uint64_t Deserializer::ReadVariableInt() {
CheckReadSize(1);
const uint8_t* buf = buffer_.data() + offset_;
uint64_t value = 0;
if (*buf <= Serializer::kViMax8) {
value = *buf;
offset_ += 1;
} else if (*buf == Serializer::kViTag16) {
CheckReadSize(3);
++buf;
uint16_t num;
memcpy(&num, buf, sizeof(num));
value = num;
offset_ += 1 + sizeof(num);
} else if (*buf == Serializer::kViTag32) {
CheckReadSize(5);
++buf;
uint32_t num;
memcpy(&num, buf, sizeof(num));
value = num;
offset_ += 1 + sizeof(num);
} else {
CheckReadSize(9);
++buf;
uint64_t num;
memcpy(&num, buf, sizeof(num));
value = num;
offset_ += 1 + sizeof(num);
}
return value;
}
std::vector<uint8_t> Deserializer::ReadBuffer(uint32_t size) {
CheckReadSize(size);
std::vector<uint8_t> result(size);
memcpy(result.data(), &buffer_.data()[offset_], size);
offset_ += size;
return result;
}
void Deserializer::ReadArray(uint8_t* output, size_t size) {
if (output != nullptr) {
CheckReadSize(size);
memcpy(output, &buffer_.data()[offset_], size);
offset_ += static_cast<uint32_t>(size);
}
}
std::vector<uint8_t> Deserializer::ReadVariableBuffer() {
// TODO(k-matsuzawa) need endian support.
uint64_t data_size = ReadVariableInt();
if (data_size == 0) {
return std::vector<uint8_t>();
}
CheckReadSize(data_size);
uint8_t* buf = buffer_.data() + offset_;
std::vector<uint8_t> result(data_size);
memcpy(result.data(), buf, data_size);
offset_ += static_cast<uint32_t>(data_size);
return result;
}
ByteData Deserializer::ReadVariableData() {
return ByteData(ReadVariableBuffer());
}
uint32_t Deserializer::GetReadSize() { return offset_; }
bool Deserializer::HasEof() { return (buffer_.size() <= offset_); }
void Deserializer::CheckReadSize(uint64_t size) {
if (size > std::numeric_limits<uint32_t>::max()) {
warn(CFD_LOG_SOURCE, "It exceeds the handling size.");
throw CfdException(kCfdIllegalStateError, "It exceeds the handling size.");
}
if (buffer_.size() < (offset_ + size)) {
warn(CFD_LOG_SOURCE, "deserialize buffer EOF.");
throw CfdException(kCfdIllegalStateError, "deserialize buffer EOF.");
}
}
} // namespace core
} // namespace cfd