forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryCoding.h
More file actions
178 lines (154 loc) · 6.19 KB
/
Copy pathBinaryCoding.h
File metadata and controls
178 lines (154 loc) · 6.19 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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#pragma once
#include <cstddef>
#include <cstdint>
#include <vector>
namespace TW {
/// Encodes a 16-bit little-endian value into the provided buffer.
inline void encode16LE(uint16_t val, std::vector<uint8_t>& data) {
data.push_back(static_cast<uint8_t>(val));
data.push_back(static_cast<uint8_t>(val >> 8));
}
/// Decodes a 16-bit little-endian value from the provided buffer.
inline uint16_t decode16LE(const uint8_t* _Nonnull src) {
return static_cast<uint16_t>((src[0]) | ((uint16_t)(src[1]) << 8));
}
/// Encodes a 32-bit little-endian value into the provided buffer.
inline void encode32LE(uint32_t val, std::vector<uint8_t>& data) {
data.push_back(static_cast<uint8_t>(val));
data.push_back(static_cast<uint8_t>((val >> 8)));
data.push_back(static_cast<uint8_t>((val >> 16)));
data.push_back(static_cast<uint8_t>((val >> 24)));
}
/// Decodes a 32-bit little-endian value from the provided buffer.
inline uint32_t decode32LE(const uint8_t* _Nonnull src) {
// clang-format off
return static_cast<uint32_t>(src[0])
| (static_cast<uint32_t>(src[1]) << 8)
| (static_cast<uint32_t>(src[2]) << 16)
| (static_cast<uint32_t>(src[3]) << 24);
// clang-format on
}
/// Encodes a 64-bit little-endian value into the provided buffer.
inline void encode64LE(uint64_t val, std::vector<uint8_t>& data) {
data.push_back(static_cast<uint8_t>(val));
data.push_back(static_cast<uint8_t>((val >> 8)));
data.push_back(static_cast<uint8_t>((val >> 16)));
data.push_back(static_cast<uint8_t>((val >> 24)));
data.push_back(static_cast<uint8_t>((val >> 32)));
data.push_back(static_cast<uint8_t>((val >> 40)));
data.push_back(static_cast<uint8_t>((val >> 48)));
data.push_back(static_cast<uint8_t>((val >> 56)));
}
/// Decodes a 64-bit little-endian value from the provided buffer.
inline uint64_t decode64LE(const uint8_t* _Nonnull src) {
// clang-format off
return static_cast<uint64_t>(src[0])
| (static_cast<uint64_t>(src[1]) << 8)
| (static_cast<uint64_t>(src[2]) << 16)
| (static_cast<uint64_t>(src[3]) << 24)
| (static_cast<uint64_t>(src[4]) << 32)
| (static_cast<uint64_t>(src[5]) << 40)
| (static_cast<uint64_t>(src[6]) << 48)
| (static_cast<uint64_t>(src[7]) << 56);
// clang-format on
}
/// Returns the number of bytes it would take to serialize the provided value
/// as a variable-length integer (varint).
inline std::size_t varIntSize(std::size_t value) {
// The value is small enough to be represented by itself.
if (value < 0xfd) {
return 1;
}
// Discriminant 1 byte plus 2 bytes for the uint16.
if (value <= UINT16_MAX) {
return 3;
}
// Discriminant 1 byte plus 4 bytes for the uint32.
if (value <= UINT32_MAX) {
return 5;
}
// Discriminant 1 byte plus 8 bytes for the uint64.
return 9;
}
/// Encodes a value as a variable-length integer.
///
/// A variable-length integer (varint) is an encoding for integers up to a max
/// value of 2^64-1 that uses a variable number of bytes depending on the value
/// being encoded. It produces fewer bytes for smaller numbers as opposed to a
/// fixed-size encoding.
///
/// @returns the number of bytes written.
inline std::size_t encodeVarInt(std::size_t size, std::vector<uint8_t>& data) {
if (size < 0xfd) {
data.push_back(static_cast<uint8_t>(size));
return 1;
}
if (size <= UINT16_MAX) {
data.push_back(0xfd);
encode16LE((uint16_t)size, data);
return 3;
}
if (size <= UINT32_MAX) {
data.push_back(0xfe);
encode32LE((uint32_t)size, data);
return 5;
}
data.push_back(0xff);
encode64LE((uint64_t)size, data);
return 9;
}
/// Encodes a 16-bit big-endian value into the provided buffer.
inline void encode16BE(uint16_t val, std::vector<uint8_t>& data) {
data.push_back(static_cast<uint8_t>(val >> 8));
data.push_back(static_cast<uint8_t>(val));
}
/// Decodes a 16-bit big-endian value from the provided buffer.
inline uint16_t decode16BE(const uint8_t* _Nonnull src) {
return static_cast<uint16_t>((src[1]) | ((uint16_t)(src[0]) << 8));
}
/// Encodes a 32-bit big-endian value into the provided buffer.
inline void encode32BE(uint32_t val, std::vector<uint8_t>& data) {
data.push_back(static_cast<uint8_t>((val >> 24)));
data.push_back(static_cast<uint8_t>((val >> 16)));
data.push_back(static_cast<uint8_t>((val >> 8)));
data.push_back(static_cast<uint8_t>(val));
}
/// Decodes a 32-bit big-endian value from the provided buffer.
inline uint32_t decode32BE(const uint8_t* _Nonnull src) {
// clang-format off
return static_cast<uint32_t>(src[3])
| (static_cast<uint32_t>(src[2]) << 8)
| (static_cast<uint32_t>(src[1]) << 16)
| (static_cast<uint32_t>(src[0]) << 24);
// clang-format on
}
/// Encodes a 64-bit big-endian value into the provided buffer.
inline void encode64BE(uint64_t val, std::vector<uint8_t>& data) {
data.push_back(static_cast<uint8_t>((val >> 56)));
data.push_back(static_cast<uint8_t>((val >> 48)));
data.push_back(static_cast<uint8_t>((val >> 40)));
data.push_back(static_cast<uint8_t>((val >> 32)));
data.push_back(static_cast<uint8_t>((val >> 24)));
data.push_back(static_cast<uint8_t>((val >> 16)));
data.push_back(static_cast<uint8_t>((val >> 8)));
data.push_back(static_cast<uint8_t>(val));
}
/// Decodes a 64-bit big-endian value from the provided buffer.
inline uint64_t decode64BE(const uint8_t* _Nonnull src) {
// clang-format off
return static_cast<uint64_t>(src[7])
| (static_cast<uint64_t>(src[6]) << 8)
| (static_cast<uint64_t>(src[5]) << 16)
| (static_cast<uint64_t>(src[4]) << 24)
| (static_cast<uint64_t>(src[3]) << 32)
| (static_cast<uint64_t>(src[2]) << 40)
| (static_cast<uint64_t>(src[1]) << 48)
| (static_cast<uint64_t>(src[0]) << 56);
// clang-format on
}
} // namespace TW