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-1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
30
+ -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
31
+ -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
32
+ -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
33
+ -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
34
+ };
17
35
18
36
bool DecodeBase58 (const char * psz, std::vector<unsigned char >& vch)
19
37
{
@@ -31,13 +49,12 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
31
49
int size = strlen (psz) * 733 /1000 + 1 ; // log(58) / log(256), rounded up.
32
50
std::vector<unsigned char > b256 (size);
33
51
// Process the characters.
52
+ static_assert (sizeof (mapBase58)/sizeof (mapBase58[0 ]) == 256 , " mapBase58.size() should be 256" ); // guarantee not out of range
34
53
while (*psz && !isspace (*psz)) {
35
54
// Decode base58 character
36
- const char * ch = strchr (pszBase58, *psz) ;
37
- if (ch == nullptr )
55
+ int carry = mapBase58[( uint8_t ) *psz] ;
56
+ if (carry == - 1 ) // Invalid b58 character
38
57
return false ;
39
- // Apply "b256 = b256 * 58 + ch".
40
- int carry = ch - pszBase58;
41
58
int i = 0 ;
42
59
for (std::vector<unsigned char >::reverse_iterator it = b256.rbegin (); (carry != 0 || i < length) && (it != b256.rend ()); ++it, ++i) {
43
60
carry += 58 * (*it);
0 commit comments