v29.4-roots.3 · Commit 28: [PATCH 28/30] fix(wallet): secure asynchronous sweep cancellation
src/base58.cpp
Before: src/base58.cpp · After: src/base58.cpp
File metadata
index cab99f0cb5df1b0d30a76989a2933532203d67d8..2cc1dab9ccd03473955a5216471228f71c5210fd 100644
Download original patch · Release notes
Diff rows 1–53 of 53. Line numbers refer to the original before/after files.
+ Added− RemovedUnmarked lines are context.
Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.
src/base58.cpp
CHANGED
|
@@ -37,7 +37,8 @@ static const int8_t mapBase58[256] = {
|
|
|
37
37
|
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
-
[[nodiscard]] static bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_len)
|
|
40
|
+
template <typename Allocator>
|
|
41
|
+
[[nodiscard]] static bool DecodeBase58(const char* psz, std::vector<unsigned char, Allocator>& vch, int max_ret_len)
|
|
41
42
|
{
|
|
42
43
|
// Skip leading spaces.
|
|
43
44
|
while (*psz && IsSpace(*psz))
|
|
@@ -52,7 +53,7 @@ static const int8_t mapBase58[256] = {
|
|
|
52
53
|
}
|
|
53
54
|
// Allocate enough space in big-endian base256 representation.
|
|
54
55
|
int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up.
|
|
55
|
-
std::vector<unsigned char> b256(size);
|
|
56
|
+
std::vector<unsigned char, Allocator> b256(size);
|
|
56
57
|
// Process the characters.
|
|
57
58
|
static_assert(std::size(mapBase58) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
|
|
58
59
|
while (*psz && !IsSpace(*psz)) {
|
|
@@ -61,7 +62,7 @@ static const int8_t mapBase58[256] = {
|
|
|
61
62
|
if (carry == -1) // Invalid b58 character
|
|
62
63
|
return false;
|
|
63
64
|
int i = 0;
|
|
64
|
-
for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
|
|
65
|
+
for (auto it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
|
|
65
66
|
carry += 58 * (*it);
|
|
66
67
|
*it = carry % 256;
|
|
67
68
|
carry /= 256;
|
|
@@ -77,7 +78,7 @@ static const int8_t mapBase58[256] = {
|
|
|
77
78
|
if (*psz != 0)
|
|
78
79
|
return false;
|
|
79
80
|
// Skip leading zeroes in b256.
|
|
80
|
-
std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
|
|
81
|
+
auto it = b256.begin() + (size - length);
|
|
81
82
|
// Copy result into output vector.
|
|
82
83
|
vch.reserve(zeroes + (b256.end() - it));
|
|
83
84
|
vch.assign(zeroes, 0x00);
|
|
@@ -143,7 +144,8 @@ std::string EncodeBase58Check(Span<const unsigned char> input)
|
|
|
143
144
|
return EncodeBase58(vch);
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
[[nodiscard]] static bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len)
|
|
147
|
+
template <typename Allocator>
|
|
148
|
+
[[nodiscard]] static bool DecodeBase58Check(const char* psz, std::vector<unsigned char, Allocator>& vchRet, int max_ret_len)
|
|
147
149
|
{
|
|
148
150
|
if (!DecodeBase58(psz, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) ||
|
|
149
151
|
(vchRet.size() < 4)) {
|
|
@@ -167,3 +169,11 @@ bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRe
|
|
|
167
169
|
}
|
|
168
170
|
return DecodeBase58Check(str.c_str(), vchRet, max_ret);
|
|
169
171
|
}
|
|
172
|
+
|
|
173
|
+
bool DecodeBase58Check(const SecureString& str, std::vector<unsigned char, secure_allocator<unsigned char>>& vchRet, int max_ret)
|
|
174
|
+
{
|
|
175
|
+
if (std::find(str.begin(), str.end(), '\0') != str.end()) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return DecodeBase58Check(str.c_str(), vchRet, max_ret);
|
|
179
|
+
}
|