11
11
#include < assert.h>
12
12
#include < string.h>
13
13
14
+ #include < limits>
15
+
14
16
/* * All alphanumeric characters except for "0", "I", "O", and "l" */
15
17
static const char * pszBase58 = " 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" ;
16
18
static const int8_t mapBase58[256 ] = {
@@ -32,7 +34,7 @@ static const int8_t mapBase58[256] = {
32
34
-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
33
35
};
34
36
35
- bool DecodeBase58 (const char * psz, std::vector<unsigned char >& vch)
37
+ bool DecodeBase58 (const char * psz, std::vector<unsigned char >& vch, int max_ret_len )
36
38
{
37
39
// Skip leading spaces.
38
40
while (*psz && IsSpace (*psz))
@@ -42,6 +44,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
42
44
int length = 0 ;
43
45
while (*psz == ' 1' ) {
44
46
zeroes++;
47
+ if (zeroes > max_ret_len) return false ;
45
48
psz++;
46
49
}
47
50
// Allocate enough space in big-endian base256 representation.
@@ -62,6 +65,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
62
65
}
63
66
assert (carry == 0 );
64
67
length = i;
68
+ if (length + zeroes > max_ret_len) return false ;
65
69
psz++;
66
70
}
67
71
// Skip trailing spaces.
@@ -71,8 +75,6 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
71
75
return false ;
72
76
// Skip leading zeroes in b256.
73
77
std::vector<unsigned char >::iterator it = b256.begin () + (size - length);
74
- while (it != b256.end () && *it == 0 )
75
- it++;
76
78
// Copy result into output vector.
77
79
vch.reserve (zeroes + (b256.end () - it));
78
80
vch.assign (zeroes, 0x00 );
@@ -126,9 +128,9 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch)
126
128
return EncodeBase58 (vch.data (), vch.data () + vch.size ());
127
129
}
128
130
129
- bool DecodeBase58 (const std::string& str, std::vector<unsigned char >& vchRet)
131
+ bool DecodeBase58 (const std::string& str, std::vector<unsigned char >& vchRet, int max_ret_len )
130
132
{
131
- return DecodeBase58 (str.c_str (), vchRet);
133
+ return DecodeBase58 (str.c_str (), vchRet, max_ret_len );
132
134
}
133
135
134
136
std::string EncodeBase58Check (const std::vector<unsigned char >& vchIn)
@@ -140,9 +142,9 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
140
142
return EncodeBase58 (vch);
141
143
}
142
144
143
- bool DecodeBase58Check (const char * psz, std::vector<unsigned char >& vchRet)
145
+ bool DecodeBase58Check (const char * psz, std::vector<unsigned char >& vchRet, int max_ret_len )
144
146
{
145
- if (!DecodeBase58 (psz, vchRet) ||
147
+ if (!DecodeBase58 (psz, vchRet, max_ret_len > std::numeric_limits< int >:: max () - 4 ? std::numeric_limits< int >:: max () : max_ret_len + 4 ) ||
146
148
(vchRet.size () < 4 )) {
147
149
vchRet.clear ();
148
150
return false ;
@@ -157,7 +159,7 @@ bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
157
159
return true ;
158
160
}
159
161
160
- bool DecodeBase58Check (const std::string& str, std::vector<unsigned char >& vchRet)
162
+ bool DecodeBase58Check (const std::string& str, std::vector<unsigned char >& vchRet, int max_ret )
161
163
{
162
- return DecodeBase58Check (str.c_str (), vchRet);
164
+ return DecodeBase58Check (str.c_str (), vchRet, max_ret );
163
165
}
0 commit comments