| 19 | static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
| 20 | |
| 21 | bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch) |
| 22 | { |
| 23 | // Skip leading spaces. |
| 24 | while (*psz && isspace(*psz)) |
| 25 | psz++; |
| 26 | // Skip and count leading '1's. |
| 27 | size_t zeroes = 0; |
| 28 | size_t length = 0; |
| 29 | while (*psz == '1') { |
| 30 | zeroes++; |
| 31 | psz++; |
| 32 | } |
| 33 | // Allocate enough space in big-endian base256 representation. |
| 34 | size_t size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up. |
| 35 | std::vector<unsigned char> b256(size); |
| 36 | // Process the characters. |
| 37 | while (*psz && !isspace(*psz)) { |
| 38 | // Decode base58 character |
| 39 | const char* ch = strchr(pszBase58, *psz); |
| 40 | if (ch == nullptr) |
| 41 | return false; |
| 42 | // Apply "b256 = b256 * 58 + ch". |
| 43 | auto carry = ch - pszBase58; |
| 44 | size_t i = 0; |
| 45 | for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) { |
| 46 | carry += 58 * (*it); |
| 47 | *it = static_cast<unsigned char>(carry % 256); |
| 48 | carry /= 256; |
| 49 | } |
| 50 | assert(carry == 0); |
| 51 | length = i; |
| 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 | auto it = b256.begin(); |
| 61 | std::advance(it, static_cast<int64_t>(size - length)); |
| 62 | while (it != b256.end() && *it == 0) |
| 63 | ++it; |
| 64 | // Copy result into output vector. |
| 65 | vch.reserve(zeroes + static_cast<size_t>(std::distance(it, b256.end()))); |
| 66 | vch.assign(zeroes, 0x00); |
| 67 | while (it != b256.end()) |
| 68 | vch.push_back(*(it++)); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) |
| 73 | { |
no test coverage detected