-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
85 lines (70 loc) · 2.16 KB
/
Utils.cpp
File metadata and controls
85 lines (70 loc) · 2.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
#include "Utils.hpp"
void Utils::appendBytes(Bytes &orig, const Bytes &toAppend) {
Bytes tmp;
// @fix does this reliably check if orig and toAppend refernce same vector?
if(&orig == &toAppend) {
tmp = Bytes(toAppend.begin(), toAppend.end());
} else {
tmp = toAppend;
}
orig.insert(orig.end(), tmp.begin(), tmp.end());
}
void Utils::appendBytesArr(Bytes &orig, const uint8_t *toAppend, const size_t size) {
orig.insert(orig.end(), toAppend, toAppend + size);
}
Bytes Utils::hexStrToBytes(const char *str) {
Bytes result;
size_t length = std::strlen(str);
assert(length % 2 == 0);
for (size_t i = 0; i < length; i += 2) {
unsigned int temp;
std::sscanf(&str[i], "%02x", &temp);
result.push_back(static_cast<std::uint8_t>(temp));
}
return result;
}
Bytes Utils::Uint256ToBytes(const Uint256 &value) {
Bytes bytes;
size_t size = Uint256::NUM_WORDS * 4;
uint8_t serial[size];
value.getBigEndianBytes(serial); // fill serial with bytes from Uint256 val
bytes.insert(bytes.begin(), serial, serial + size);
return bytes;
}
Bytes Utils::hashToBytes(const Sha256Hash &hash) {
return Bytes(hash.value, hash.value + Sha256Hash::HASH_LEN);
}
string Utils::bytesToStr(const Bytes &orig) {
string str = "";
for(auto &b : orig) {
str += hexify<uint8_t>(b);
}
return str;
}
string Utils::toStr(const Sha256Hash& hash) {
return bytesToStr(hashToBytes(hash));
}
string Utils::toStr(const Uint256& value) {
return bytesToStr(Uint256ToBytes(value));
}
/*
string toStr(const Uint256& value);
string toStr(const CurvePoint& curvePoint);
*/
// @param length: number of bytes to display at either end
string Utils::abridgeBytes(const Bytes &bytes, size_t length) {
assert(bytes.size() >= length*2);
Bytes::const_iterator front = bytes.begin();
Bytes::const_iterator back = bytes.end();
--back;
Bytes l, r;
// @fix use insert operation O(1)
//l.insert(l.begin(), bytes.begin(), bytes.begin() + length);
//r.insert(l.begin(), back - length, back);
//r.insert(l.begin(), bytes.end() - length, bytes.end());
for(int i=0; i<length; i++, ++front, --back) {
l.push_back(*front);
r.insert(r.begin(), *back);
}
return "0x" + bytesToStr(l) + "..." + bytesToStr(r);
}