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

Function DecodeBase58

src/base58.cpp:21–65  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
20
21bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
22{
23 // Skip leading spaces.
24 while (*psz && isspace(*psz))
25 psz++;
26 // Skip and count leading '1's.
27 int zeroes = 0;
28 while (*psz == '1') {
29 zeroes++;
30 psz++;
31 }
32 // Allocate enough space in big-endian base256 representation.
33 std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up.
34 // Process the characters.
35 while (*psz && !isspace(*psz)) {
36 // Decode base58 character
37 const char* ch = strchr(pszBase58, *psz);
38 if (ch == NULL)
39 return false;
40 // Apply "b256 = b256 * 58 + ch".
41 int carry = ch - pszBase58;
42 for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) {
43 carry += 58 * (*it);
44 *it = carry % 256;
45 carry /= 256;
46 }
47 assert(carry == 0);
48 psz++;
49 }
50 // Skip trailing spaces.
51 while (isspace(*psz))
52 psz++;
53 if (*psz != 0)
54 return false;
55 // Skip leading zeroes in b256.
56 std::vector<unsigned char>::iterator it = b256.begin();
57 while (it != b256.end() && *it == 0)
58 it++;
59 // Copy result into output vector.
60 vch.reserve(zeroes + (b256.end() - it));
61 vch.assign(zeroes, 0x00);
62 while (it != b256.end())
63 vch.push_back(*(it++));
64 return true;
65}
66
67std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
68{

Callers 2

DecodeBase58CheckFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 4

reserveMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
push_backMethod · 0.45

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68