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

Function DecodeBase58

src/commons/base58.cpp:23–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 2

BOOST_AUTO_TEST_CASEFunction · 0.85
DecodeBase58CheckFunction · 0.85

Calls 4

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

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68