@@ -25,12 +25,14 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
25
25
psz++;
26
26
// Skip and count leading '1's.
27
27
int zeroes = 0 ;
28
+ int length = 0 ;
28
29
while (*psz == ' 1' ) {
29
30
zeroes++;
30
31
psz++;
31
32
}
32
33
// 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
+ int size = strlen (psz) * 733 /1000 + 1 ; // log(58) / log(256), rounded up.
35
+ std::vector<unsigned char > b256 (size);
34
36
// Process the characters.
35
37
while (*psz && !isspace (*psz)) {
36
38
// Decode base58 character
@@ -39,12 +41,14 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
39
41
return false ;
40
42
// Apply "b256 = b256 * 58 + ch".
41
43
int carry = ch - pszBase58;
42
- for (std::vector<unsigned char >::reverse_iterator it = b256.rbegin (); it != b256.rend (); it++) {
44
+ int i = 0 ;
45
+ for (std::vector<unsigned char >::reverse_iterator it = b256.rbegin (); (carry != 0 || i < length) && (it != b256.rend ()); ++it, ++i) {
43
46
carry += 58 * (*it);
44
47
*it = carry % 256 ;
45
48
carry /= 256 ;
46
49
}
47
50
assert (carry == 0 );
51
+ length = i;
48
52
psz++;
49
53
}
50
54
// Skip trailing spaces.
@@ -53,7 +57,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
53
57
if (*psz != 0 )
54
58
return false ;
55
59
// Skip leading zeroes in b256.
56
- std::vector<unsigned char >::iterator it = b256.begin ();
60
+ std::vector<unsigned char >::iterator it = b256.begin () + (size - length) ;
57
61
while (it != b256.end () && *it == 0 )
58
62
it++;
59
63
// Copy result into output vector.
0 commit comments