Branch data Line data Source code
1 : : // Copyright (c) 2014-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <base58.h>
6 : :
7 : : #include <hash.h>
8 : : #include <uint256.h>
9 : : #include <util/strencodings.h>
10 : : #include <util/string.h>
11 : :
12 : : #include <algorithm>
13 : : #include <cassert>
14 : : #include <cstdint>
15 : : #include <cstring>
16 : :
17 : : #include <limits>
18 : :
19 : : using util::ContainsNUL;
20 : :
21 : : /** All alphanumeric characters except for "0", "I", "O", and "l" */
22 : : static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
23 : : static const int8_t mapBase58[256] = {
24 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
25 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
26 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
27 : : -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
28 : : -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
29 : : 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1,
30 : : -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46,
31 : : 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
32 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
33 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
34 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
35 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
36 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
37 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
38 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
39 : : -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
40 : : };
41 : :
42 : 2295 : [[nodiscard]] static bool DecodeBase58Raw(std::string_view str, std::vector<unsigned char>& vch, int max_ret_len)
43 : : {
44 : 2295 : const char* psz = str.data();
45 : 2295 : const char* const end = str.data() + str.size();
46 : : // Skip leading spaces.
47 [ + + + + ]: 2309 : while (psz != end && IsSpace(*psz))
48 : 14 : psz++;
49 : : // Skip and count leading '1's.
50 : : int zeroes = 0;
51 : 2571 : int length = 0;
52 [ + + + + ]: 2571 : while (psz != end && *psz == '1') {
53 : 276 : zeroes++;
54 [ + - ]: 276 : if (zeroes > max_ret_len) return false;
55 : 276 : psz++;
56 : : }
57 : : // Allocate enough space in big-endian base256 representation.
58 : 4590 : const size_t size{static_cast<size_t>(std::min<uint64_t>(
59 : 2295 : uint64_t(end - psz) * 733 / 1000 + 1, // log(58) / log(256), rounded up.
60 [ + - + + ]: 4590 : uint64_t(std::max(max_ret_len - zeroes, 0)) + 1))};
61 : 2295 : std::vector<unsigned char> b256(size);
62 : : // Process the characters.
63 : : static_assert(std::size(mapBase58) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
64 [ + + + + ]: 156650 : while (psz != end && !IsSpace(*psz)) {
65 : : // Decode base58 character
66 : 155072 : int carry{mapBase58[uint8_t(*psz)]};
67 [ + + ]: 155072 : if (carry == -1) // Invalid b58 character
68 : : return false;
69 : 154663 : int i = 0;
70 [ + + - + ]: 5504841 : for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
71 : 5350178 : carry += 58 * (*it);
72 : 5350178 : *it = carry % 256;
73 : 5350178 : carry /= 256;
74 : : }
75 [ - + ]: 154663 : assert(carry == 0);
76 : 154663 : length = i;
77 [ + + ]: 154663 : if (length + zeroes > max_ret_len) return false;
78 : 154355 : psz++;
79 : : }
80 : : // Skip trailing spaces.
81 [ + + + + ]: 1592 : while (psz != end && IsSpace(*psz))
82 : 14 : psz++;
83 [ + + ]: 1578 : if (psz != end)
84 : : return false;
85 : : // Skip leading zeroes in b256.
86 [ + - ]: 1577 : std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
87 : : // Copy result into output vector.
88 [ + - ]: 1577 : vch.reserve(zeroes + (b256.end() - it));
89 [ + - ]: 1577 : vch.assign(zeroes, 0x00);
90 [ + + ]: 101097 : while (it != b256.end())
91 [ + - ]: 99520 : vch.push_back(*(it++));
92 : : return true;
93 : 2295 : }
94 : :
95 : 16667 : std::string EncodeBase58(std::span<const unsigned char> input)
96 : : {
97 : : // Skip & count leading zeroes.
98 : 16667 : int zeroes = 0;
99 : 16667 : int length = 0;
100 [ + + + + ]: 16998 : while (input.size() > 0 && input[0] == 0) {
101 : 331 : input = input.subspan(1);
102 : 331 : zeroes++;
103 : : }
104 : : // Allocate enough space in big-endian base58 representation.
105 : 16667 : int size = input.size() * 138 / 100 + 1; // log(256) / log(58), rounded up.
106 : 16667 : std::vector<unsigned char> b58(size);
107 : : // Process the bytes.
108 [ + + ]: 1363232 : while (input.size() > 0) {
109 : 1346565 : int carry = input[0];
110 : 1346565 : int i = 0;
111 : : // Apply "b58 = b58 * 256 + ch".
112 [ + + - + ]: 76599048 : for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
113 : 75252483 : carry += 256 * (*it);
114 : 75252483 : *it = carry % 58;
115 : 75252483 : carry /= 58;
116 : : }
117 : :
118 [ - + ]: 1346565 : assert(carry == 0);
119 : 1346565 : length = i;
120 : 1346565 : input = input.subspan(1);
121 : : }
122 : : // Skip leading zeroes in base58 result.
123 : 16667 : std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
124 [ + + + - ]: 16667 : while (it != b58.end() && *it == 0)
125 : 0 : it++;
126 : : // Translate the result into a string.
127 [ + - ]: 16667 : std::string str;
128 [ + - ]: 16667 : str.reserve(zeroes + (b58.end() - it));
129 [ + - ]: 16667 : str.assign(zeroes, '1');
130 : 1839679 : while (it != b58.end())
131 [ + - + + ]: 1839679 : str += pszBase58[*(it++)];
132 : 16667 : return str;
133 : 16667 : }
134 : :
135 : 279 : bool DecodeBase58(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret_len)
136 : : {
137 [ + + ]: 558 : if (ContainsNUL(str)) {
138 : : return false;
139 : : }
140 : 276 : return DecodeBase58Raw(str, vchRet, max_ret_len);
141 : : }
142 : :
143 : 16646 : std::string EncodeBase58Check(std::span<const unsigned char> input)
144 : : {
145 : : // add 4-byte hash check to the end
146 : 16646 : std::vector<unsigned char> vch(input.begin(), input.end());
147 [ + - ]: 16646 : uint256 hash = Hash(vch);
148 [ + - ]: 16646 : vch.insert(vch.end(), hash.data(), hash.data() + 4);
149 [ - + + - ]: 16646 : return EncodeBase58(vch);
150 : 16646 : }
151 : :
152 : 2019 : [[nodiscard]] static bool DecodeBase58CheckRaw(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret_len)
153 : : {
154 [ + + ]: 2019 : if (!DecodeBase58Raw(str, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) ||
155 [ + + ]: 1415 : (vchRet.size() < 4)) {
156 [ + + ]: 621 : vchRet.clear();
157 : 621 : return false;
158 : : }
159 : : // re-calculate the checksum, ensure it matches the included 4-byte checksum
160 : 1398 : uint256 hash = Hash(std::span{vchRet}.first(vchRet.size() - 4));
161 [ - + + + ]: 1398 : if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
162 [ + - ]: 69 : vchRet.clear();
163 : 69 : return false;
164 : : }
165 : 1329 : vchRet.resize(vchRet.size() - 4);
166 : 1329 : return true;
167 : : }
168 : :
169 : 2020 : bool DecodeBase58Check(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret)
170 : : {
171 [ + + ]: 4040 : if (ContainsNUL(str)) {
172 : : return false;
173 : : }
174 : 2019 : return DecodeBase58CheckRaw(str, vchRet, max_ret);
175 : : }
|