MCPcopy Create free account
hub / github.com/ElementsProject/elements / EncodeBase58

Function EncodeBase58

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

Source from the content-addressed store, hash-verified

85}
86
87std::string EncodeBase58(Span<const unsigned char> input)
88{
89 // Skip & count leading zeroes.
90 int zeroes = 0;
91 int length = 0;
92 while (input.size() > 0 && input[0] == 0) {
93 input = input.subspan(1);
94 zeroes++;
95 }
96 // Allocate enough space in big-endian base58 representation.
97 int size = input.size() * 138 / 100 + 1; // log(256) / log(58), rounded up.
98 std::vector<unsigned char> b58(size);
99 // Process the bytes.
100 while (input.size() > 0) {
101 int carry = input[0];
102 int i = 0;
103 // Apply "b58 = b58 * 256 + ch".
104 for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
105 carry += 256 * (*it);
106 *it = carry % 58;
107 carry /= 58;
108 }
109
110 assert(carry == 0);
111 length = i;
112 input = input.subspan(1);
113 }
114 // Skip leading zeroes in base58 result.
115 std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
116 while (it != b58.end() && *it == 0)
117 it++;
118 // Translate the result into a string.
119 std::string str;
120 str.reserve(zeroes + (b58.end() - it));
121 str.assign(zeroes, '1');
122 while (it != b58.end())
123 str += pszBase58[*(it++)];
124 return str;
125}
126
127bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len)
128{

Callers 6

EncodeBase58CheckFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85
FUZZ_TARGET_INITFunction · 0.85
ConsumeScalarRPCArgumentFunction · 0.85
DummyAddressFunction · 0.85
Base58EncodeFunction · 0.85

Calls 7

rbeginMethod · 0.80
rendMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
reserveMethod · 0.45
assignMethod · 0.45

Tested by 3

BOOST_AUTO_TEST_CASEFunction · 0.68
FUZZ_TARGET_INITFunction · 0.68
ConsumeScalarRPCArgumentFunction · 0.68