| 23 | static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
| 24 | |
| 25 | bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch) |
| 26 | { |
| 27 | // Skip leading spaces. |
| 28 | while (*psz && isspace(*psz)) |
| 29 | psz++; |
| 30 | // Skip and count leading '1's. |
| 31 | int zeroes = 0; |
| 32 | while (*psz == '1') { |
| 33 | zeroes++; |
| 34 | psz++; |
| 35 | } |
| 36 | // Allocate enough space in big-endian base256 representation. |
| 37 | std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up. |
| 38 | // Process the characters. |
| 39 | while (*psz && !isspace(*psz)) { |
| 40 | // Decode base58 character |
| 41 | const char* ch = strchr(pszBase58, *psz); |
| 42 | if (ch == NULL) |
| 43 | return false; |
| 44 | // Apply "b256 = b256 * 58 + ch". |
| 45 | int carry = ch - pszBase58; |
| 46 | for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) { |
| 47 | carry += 58 * (*it); |
| 48 | *it = carry % 256; |
| 49 | carry /= 256; |
| 50 | } |
| 51 | assert(carry == 0); |
| 52 | psz++; |
| 53 | } |
| 54 | // Skip trailing spaces. |
| 55 | while (isspace(*psz)) |
| 56 | psz++; |
| 57 | if (*psz != 0) |
| 58 | return false; |
| 59 | // Skip leading zeroes in b256. |
| 60 | std::vector<unsigned char>::iterator it = b256.begin(); |
| 61 | while (it != b256.end() && *it == 0) |
| 62 | it++; |
| 63 | // Copy result into output vector. |
| 64 | vch.reserve(zeroes + (b256.end() - it)); |
| 65 | vch.assign(zeroes, 0x00); |
| 66 | while (it != b256.end()) |
| 67 | vch.push_back(*(it++)); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | std::string DecodeBase58(const char* psz) |
| 72 | { |