| 36 | }; |
| 37 | |
| 38 | bool DecodeBase58(const char* psz, std::vector<uint8_t> &vch) { |
| 39 | |
| 40 | uint8_t digits[256]; |
| 41 | |
| 42 | // Skip and count leading '1' |
| 43 | int zeroes = 0; |
| 44 | while (*psz == '1') { |
| 45 | zeroes++; |
| 46 | psz++; |
| 47 | } |
| 48 | |
| 49 | int length = (int)strlen(psz); |
| 50 | |
| 51 | // Process the characters |
| 52 | int digitslen = 1; |
| 53 | digits[0] = 0; |
| 54 | for (int i = 0; i < length; i++) { |
| 55 | |
| 56 | // Decode base58 character |
| 57 | if (psz[i] & 0x80) |
| 58 | return false; |
| 59 | |
| 60 | int8_t c = b58digits_map[psz[i]]; |
| 61 | if (c < 0) |
| 62 | return false; |
| 63 | |
| 64 | uint32_t carry = (uint32_t)c; |
| 65 | for (int j = 0; j < digitslen; j++) { |
| 66 | carry += (uint32_t)(digits[j]) * 58; |
| 67 | digits[j] = (uint8_t)(carry % 256); |
| 68 | carry /= 256; |
| 69 | } |
| 70 | while (carry > 0) { |
| 71 | digits[digitslen++] = (uint8_t)(carry % 256); |
| 72 | carry /= 256; |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | |
| 77 | vch.clear(); |
| 78 | vch.reserve(zeroes + digitslen); |
| 79 | // zeros |
| 80 | for (int i = 0; i < zeroes; i++) |
| 81 | vch.push_back(0); |
| 82 | |
| 83 | // reverse |
| 84 | for (int i = 0; i < digitslen; i++) |
| 85 | vch.push_back(digits[digitslen - 1 - i]); |
| 86 | |
| 87 | return true; |
| 88 | |
| 89 | } |
| 90 | |
| 91 | std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) { |
| 92 |
no outgoing calls
no test coverage detected