v30.3-roots.1 · Commit 28: [PATCH 28/68] fix(wallet): secure asynchronous sweep cancellation
src/base58.cpp
Before: src/base58.cpp · After: src/base58.cpp
File metadata
index 19a5bd3e550fbe6dfa2546ab3d8f354bcb468179..dcc9c408ae135cc8e6c9adda15d691abf1f357ab 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. Before is on the left; after is on the right. Unified view fits narrow screens more easily.
src/base58.cpp
CHANGED
|
@@ -37,7 +37,8 @@ static const int8_t mapBase58[256] = {
|
|
| 37 |
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
|
| 38 |
};
|
| 39 |
|
| 40 |
-
[[nodiscard]] static bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_len)
|
|
|
|
| 41 |
{
|
| 42 |
// Skip leading spaces.
|
| 43 |
while (*psz && IsSpace(*psz))
|
|
@@ -52,7 +53,7 @@ static const int8_t mapBase58[256] = {
|
|
| 52 |
}
|
| 53 |
// Allocate enough space in big-endian base256 representation.
|
| 54 |
int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up.
|
| 55 |
-
std::vector<unsigned char> b256(size);
|
| 56 |
// Process the characters.
|
| 57 |
static_assert(std::size(mapBase58) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
|
| 58 |
while (*psz && !IsSpace(*psz)) {
|
|
@@ -61,7 +62,7 @@ static const int8_t mapBase58[256] = {
|
|
| 61 |
if (carry == -1) // Invalid b58 character
|
| 62 |
return false;
|
| 63 |
int i = 0;
|
| 64 |
-
for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
|
| 65 |
carry += 58 * (*it);
|
| 66 |
*it = carry % 256;
|
| 67 |
carry /= 256;
|
|
@@ -77,7 +78,7 @@ static const int8_t mapBase58[256] = {
|
|
| 77 |
if (*psz != 0)
|
| 78 |
return false;
|
| 79 |
// Skip leading zeroes in b256.
|
| 80 |
-
std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
|
| 81 |
// Copy result into output vector.
|
| 82 |
vch.reserve(zeroes + (b256.end() - it));
|
| 83 |
vch.assign(zeroes, 0x00);
|
|
@@ -143,7 +144,8 @@ std::string EncodeBase58Check(std::span<const unsigned char> input)
|
|
| 143 |
return EncodeBase58(vch);
|
| 144 |
}
|
| 145 |
|
| 146 |
-
[[nodiscard]] static bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len)
|
|
|
|
| 147 |
{
|
| 148 |
if (!DecodeBase58(psz, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) ||
|
| 149 |
(vchRet.size() < 4)) {
|
|
@@ -167,3 +169,11 @@ bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRe
|
|
| 167 |
}
|
| 168 |
return DecodeBase58Check(str.c_str(), vchRet, max_ret);
|
| 169 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
|
| 38 |
};
|
| 39 |
|
| 40 |
+
template <typename Allocator>
|
| 41 |
+
[[nodiscard]] static bool DecodeBase58(const char* psz, std::vector<unsigned char, Allocator>& vch, int max_ret_len)
|
| 42 |
{
|
| 43 |
// Skip leading spaces.
|
| 44 |
while (*psz && IsSpace(*psz))
|
|
|
|
| 53 |
}
|
| 54 |
// Allocate enough space in big-endian base256 representation.
|
| 55 |
int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up.
|
| 56 |
+
std::vector<unsigned char, Allocator> b256(size);
|
| 57 |
// Process the characters.
|
| 58 |
static_assert(std::size(mapBase58) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
|
| 59 |
while (*psz && !IsSpace(*psz)) {
|
|
|
|
| 62 |
if (carry == -1) // Invalid b58 character
|
| 63 |
return false;
|
| 64 |
int i = 0;
|
| 65 |
+
for (auto it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
|
| 66 |
carry += 58 * (*it);
|
| 67 |
*it = carry % 256;
|
| 68 |
carry /= 256;
|
|
|
|
| 78 |
if (*psz != 0)
|
| 79 |
return false;
|
| 80 |
// Skip leading zeroes in b256.
|
| 81 |
+
auto it = b256.begin() + (size - length);
|
| 82 |
// Copy result into output vector.
|
| 83 |
vch.reserve(zeroes + (b256.end() - it));
|
| 84 |
vch.assign(zeroes, 0x00);
|
|
|
|
| 144 |
return EncodeBase58(vch);
|
| 145 |
}
|
| 146 |
|
| 147 |
+
template <typename Allocator>
|
| 148 |
+
[[nodiscard]] static bool DecodeBase58Check(const char* psz, std::vector<unsigned char, Allocator>& vchRet, int max_ret_len)
|
| 149 |
{
|
| 150 |
if (!DecodeBase58(psz, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) ||
|
| 151 |
(vchRet.size() < 4)) {
|
|
|
|
| 169 |
}
|
| 170 |
return DecodeBase58Check(str.c_str(), vchRet, max_ret);
|
| 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 |
+
}
|