MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / DecodeBase58

Function DecodeBase58

src/base58.cpp:40–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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))
44 psz++;
45 // Skip and count leading '1's.
46 int zeroes = 0;
47 int length = 0;
48 while (*psz == '1') {
49 zeroes++;
50 if (zeroes > max_ret_len) return false;
51 psz++;
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)) {
59 // Decode base58 character
60 int carry = mapBase58[(uint8_t)*psz];
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;
68 }
69 assert(carry == 0);
70 length = i;
71 if (length + zeroes > max_ret_len) return false;
72 psz++;
73 }
74 // Skip trailing spaces.
75 while (IsSpace(*psz))
76 psz++;
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);
84 while (it != b256.end())
85 vch.push_back(*(it++));
86 return true;
87}
88
89std::string EncodeBase58(std::span<const unsigned char> input)
90{

Callers 5

DecodeDestinationFunction · 0.85
DecodeBase58CheckFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
FUZZ_TARGETFunction · 0.85
Base58DecodeFunction · 0.85

Calls 10

IsSpaceFunction · 0.85
sizeFunction · 0.85
ContainsNoNULFunction · 0.85
rbeginMethod · 0.80
rendMethod · 0.80
beginMethod · 0.45
reserveMethod · 0.45
endMethod · 0.45
assignMethod · 0.45
push_backMethod · 0.45

Tested by 2

BOOST_AUTO_TEST_CASEFunction · 0.68
FUZZ_TARGETFunction · 0.68