| 81 | } |
| 82 | |
| 83 | std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) |
| 84 | { |
| 85 | // Skip & count leading zeroes. |
| 86 | int zeroes = 0; |
| 87 | int length = 0; |
| 88 | while (pbegin != pend && *pbegin == 0) { |
| 89 | pbegin++; |
| 90 | zeroes++; |
| 91 | } |
| 92 | // Allocate enough space in big-endian base58 representation. |
| 93 | int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up. |
| 94 | std::vector<unsigned char> b58(size); |
| 95 | // Process the bytes. |
| 96 | while (pbegin != pend) { |
| 97 | int carry = *pbegin; |
| 98 | int i = 0; |
| 99 | // Apply "b58 = b58 * 256 + ch". |
| 100 | for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) { |
| 101 | carry += 256 * (*it); |
| 102 | *it = carry % 58; |
| 103 | carry /= 58; |
| 104 | } |
| 105 | |
| 106 | assert(carry == 0); |
| 107 | length = i; |
| 108 | pbegin++; |
| 109 | } |
| 110 | // Skip leading zeroes in base58 result. |
| 111 | std::vector<unsigned char>::iterator it = b58.begin() + (size - length); |
| 112 | while (it != b58.end() && *it == 0) |
| 113 | it++; |
| 114 | // Translate the result into a string. |
| 115 | std::string str; |
| 116 | str.reserve(zeroes + (b58.end() - it)); |
| 117 | str.assign(zeroes, '1'); |
| 118 | while (it != b58.end()) |
| 119 | str += pszBase58[*(it++)]; |
| 120 | return str; |
| 121 | } |
| 122 | |
| 123 | std::string EncodeBase58(const std::vector<unsigned char>& vch) |
| 124 | { |