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, -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
28+ -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,
29+ -1 ,-1 ,-1 ,-1 ,-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+ };
1533
1634bool DecodeBase58 (const char * psz, std::vector<unsigned char >& vch)
1735{
@@ -29,13 +47,12 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
2947 int size = strlen (psz) * 733 /1000 + 1 ; // log(58) / log(256), rounded up.
3048 std::vector<unsigned char > b256 (size);
3149 // Process the characters.
50+ static_assert (sizeof (mapBase58)/sizeof (mapBase58[0 ]) == 256 , " mapBase58.size() should be 256" ); // guarantee not out of range
3251 while (*psz && !isspace (*psz)) {
3352 // Decode base58 character
34- const char * ch = strchr (pszBase58, *psz) ;
35- if (ch == nullptr )
53+ int carry = mapBase58[( uint8_t ) *psz] ;
54+ if (carry == - 1 ) // Invalid b58 character
3655 return false ;
37- // Apply "b256 = b256 * 58 + ch".
38- int carry = ch - pszBase58;
3956 int i = 0 ;
4057 for (std::vector<unsigned char >::reverse_iterator it = b256.rbegin (); (carry != 0 || i < length) && (it != b256.rend ()); ++it, ++i) {
4158 carry += 58 * (*it);
0 commit comments