-
Notifications
You must be signed in to change notification settings - Fork 15
/
bytes.cpp
140 lines (112 loc) · 3.46 KB
/
bytes.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
#include <faabric/util/bytes.h>
#include <faabric/util/logging.h>
#include <faabric/util/timing.h>
#include <sstream>
#include <stack>
#include <vector>
namespace faabric::util {
std::vector<uint8_t> stringToBytes(const std::string& str)
{
if (str.empty()) {
std::vector<uint8_t> empty;
return empty;
}
// Get raw data as byte pointer
const char* cstr = str.c_str();
auto* rawBytes = reinterpret_cast<const uint8_t*>(cstr);
// Wrap in bytes vector
std::vector<uint8_t> actual(rawBytes, rawBytes + str.length());
return actual;
}
void trimTrailingZeros(std::vector<uint8_t>& vectorIn)
{
long i = vectorIn.size() - 1;
while (i >= 0 && vectorIn.at((unsigned long)i) == 0) {
i--;
}
if (i < 0) {
vectorIn.clear();
} else {
vectorIn.resize((unsigned long)i + 1);
}
}
int safeCopyToBuffer(const std::vector<uint8_t>& dataIn,
uint8_t* buffer,
int bufferLen)
{
int dataSize = (int)dataIn.size();
if (bufferLen <= 0) {
return dataSize;
}
return safeCopyToBuffer(dataIn.data(), dataIn.size(), buffer, bufferLen);
}
int safeCopyToBuffer(const uint8_t* dataIn,
int dataLen,
uint8_t* buffer,
int bufferLen)
{
if (dataLen == 0) {
return 0;
}
// Truncate date being copied into a short buffer
int copyLen = std::min(dataLen, bufferLen);
std::copy(dataIn, dataIn + copyLen, buffer);
return copyLen;
}
// This function assumes that the byte array has been generated from an int
// of the same size, with the same endianess (effectively, using something like
// BYTES(int*)). See the test in util/test_bytes.cpp for an example
int bytesToInt(const std::vector<uint8_t>& bytes)
{
int returnValue;
assert(bytes.size() == sizeof(int));
std::memcpy(&returnValue, bytes.data(), bytes.size());
return returnValue;
}
std::string bytesToString(const std::vector<uint8_t>& bytes)
{
unsigned long byteLen = bytes.size();
const char* charPtr = reinterpret_cast<const char*>(bytes.data());
const std::string result = std::string(charPtr, charPtr + byteLen);
return result;
}
std::string formatByteArrayToIntString(const std::vector<uint8_t>& bytes)
{
std::stringstream ss;
ss << "[";
for (int i = 0; i < bytes.size(); i++) {
ss << (int)bytes.at(i);
if (i < bytes.size() - 1) {
ss << ", ";
}
}
ss << "]";
return ss.str();
}
std::string byteArrayToHexString(const uint8_t* data, int dataSize)
{
std::stringstream ss;
ss << std::hex;
for (int i = 0; i < dataSize; ++i) {
ss << std::setw(2) << std::setfill('0') << static_cast<int>(data[i]);
}
return ss.str();
}
std::vector<uint8_t> hexStringToByteArray(const std::string& hexString)
{
if (hexString.length() % 2 != 0) {
SPDLOG_ERROR("The provided hex string has not an even number of"
"characters. Can't convert {} to a byte array",
hexString);
throw std::runtime_error("Provided hex string has not even length");
}
std::vector<uint8_t> byteArray;
byteArray.reserve(hexString.length() / 2);
for (int i = 0; i < hexString.length(); i += 2) {
std::string byteString = hexString.substr(i, 2);
uint8_t byteChar = (uint8_t)strtol(byteString.c_str(), nullptr, 16);
byteArray.push_back(byteChar);
}
return byteArray;
}
}