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 : 333475 : [[nodiscard]] static bool DecodeBase58Raw(std::string_view str, std::vector<unsigned char>& vch, int max_ret_len)
43 : : {
44 : 333475 : const char* psz = str.data();
45 : 333475 : const char* const end = str.data() + str.size();
46 : : // Skip leading spaces.
47 [ + + + + ]: 356370 : while (psz != end && IsSpace(*psz))
48 : 22895 : psz++;
49 : : // Skip and count leading '1's.
50 : : int zeroes = 0;
51 : 351200 : int length = 0;
52 [ + + + + ]: 351200 : while (psz != end && *psz == '1') {
53 : 17866 : zeroes++;
54 [ + + ]: 17866 : if (zeroes > max_ret_len) return false;
55 : 17725 : psz++;
56 : : }
57 : : // Allocate enough space in big-endian base256 representation.
58 : 666668 : const size_t size{static_cast<size_t>(std::min<uint64_t>(
59 : 333334 : uint64_t(end - psz) * 733 / 1000 + 1, // log(58) / log(256), rounded up.
60 [ + + + + ]: 666647 : uint64_t(std::max(max_ret_len - zeroes, 0)) + 1))};
61 : 333334 : 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 [ + + + + ]: 27527921 : while (psz != end && !IsSpace(*psz)) {
65 : : // Decode base58 character
66 : 27250097 : int carry{mapBase58[uint8_t(*psz)]};
67 [ + + ]: 27250097 : if (carry == -1) // Invalid b58 character
68 : : return false;
69 : 27249049 : int i = 0;
70 [ + + - + ]: 1003118793 : for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) {
71 : 975869744 : carry += 58 * (*it);
72 : 975869744 : *it = carry % 256;
73 : 975869744 : carry /= 256;
74 : : }
75 [ - + ]: 27249049 : assert(carry == 0);
76 : 27249049 : length = i;
77 [ + + ]: 27249049 : if (length + zeroes > max_ret_len) return false;
78 : 27194587 : psz++;
79 : : }
80 : : // Skip trailing spaces.
81 [ + + + + ]: 435917 : while (psz != end && IsSpace(*psz))
82 : 158093 : psz++;
83 [ + + ]: 277824 : if (psz != end)
84 : : return false;
85 : : // Skip leading zeroes in b256.
86 [ + - ]: 277592 : std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
87 : : // Copy result into output vector.
88 [ + - ]: 277592 : vch.reserve(zeroes + (b256.end() - it));
89 [ + - ]: 277592 : vch.assign(zeroes, 0x00);
90 [ + + ]: 18283629 : while (it != b256.end())
91 [ + - ]: 18006037 : vch.push_back(*(it++));
92 : : return true;
93 : 333334 : }
94 : :
95 : 4612921 : std::string EncodeBase58(std::span<const unsigned char> input)
96 : : {
97 : : // Skip & count leading zeroes.
98 : 4612921 : int zeroes = 0;
99 : 4612921 : int length = 0;
100 [ + + + + ]: 4734878 : while (input.size() > 0 && input[0] == 0) {
101 : 121957 : input = input.subspan(1);
102 : 121957 : zeroes++;
103 : : }
104 : : // Allocate enough space in big-endian base58 representation.
105 : 4612921 : int size = input.size() * 138 / 100 + 1; // log(256) / log(58), rounded up.
106 : 4612921 : std::vector<unsigned char> b58(size);
107 : : // Process the bytes.
108 [ + + ]: 356326202 : while (input.size() > 0) {
109 : 351713281 : int carry = input[0];
110 : 351713281 : int i = 0;
111 : : // Apply "b58 = b58 * 256 + ch".
112 [ + + - + ]: 19607215294 : for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
113 : 19255502013 : carry += 256 * (*it);
114 : 19255502013 : *it = carry % 58;
115 : 19255502013 : carry /= 58;
116 : : }
117 : :
118 [ - + ]: 351713281 : assert(carry == 0);
119 : 351713281 : length = i;
120 : 351713281 : input = input.subspan(1);
121 : : }
122 : : // Skip leading zeroes in base58 result.
123 : 4612921 : std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
124 [ + + + - ]: 4612921 : while (it != b58.end() && *it == 0)
125 : 0 : it++;
126 : : // Translate the result into a string.
127 [ + - ]: 4612921 : std::string str;
128 [ + - ]: 4612921 : str.reserve(zeroes + (b58.end() - it));
129 [ + - ]: 4612921 : str.assign(zeroes, '1');
130 : 481142681 : while (it != b58.end())
131 [ + - + + ]: 481142681 : str += pszBase58[*(it++)];
132 : 4612921 : return str;
133 : 4612921 : }
134 : :
135 : 7226 : bool DecodeBase58(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret_len)
136 : : {
137 [ + + ]: 14452 : if (ContainsNUL(str)) {
138 : : return false;
139 : : }
140 : 6601 : return DecodeBase58Raw(str, vchRet, max_ret_len);
141 : : }
142 : :
143 : 4582790 : std::string EncodeBase58Check(std::span<const unsigned char> input)
144 : : {
145 : : // add 4-byte hash check to the end
146 : 4582790 : std::vector<unsigned char> vch(input.begin(), input.end());
147 [ + - ]: 4582790 : uint256 hash = Hash(vch);
148 [ + - ]: 4582790 : vch.insert(vch.end(), hash.data(), hash.data() + 4);
149 [ - + + - ]: 4582790 : return EncodeBase58(vch);
150 : 4582790 : }
151 : :
152 : 326874 : [[nodiscard]] static bool DecodeBase58CheckRaw(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret_len)
153 : : {
154 [ + + ]: 326874 : if (!DecodeBase58Raw(str, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) ||
155 [ + + ]: 271614 : (vchRet.size() < 4)) {
156 [ + + ]: 61088 : vchRet.clear();
157 : 61088 : return false;
158 : : }
159 : : // re-calculate the checksum, ensure it matches the included 4-byte checksum
160 : 265786 : uint256 hash = Hash(std::span{vchRet}.first(vchRet.size() - 4));
161 [ - + + + ]: 265786 : if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
162 [ + - ]: 1013 : vchRet.clear();
163 : 1013 : return false;
164 : : }
165 : 264773 : vchRet.resize(vchRet.size() - 4);
166 : 264773 : return true;
167 : : }
168 : :
169 : 327510 : bool DecodeBase58Check(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret)
170 : : {
171 [ + + ]: 655020 : if (ContainsNUL(str)) {
172 : : return false;
173 : : }
174 : 326874 : return DecodeBase58CheckRaw(str, vchRet, max_ret);
175 : : }
|