| 70 | } |
| 71 | |
| 72 | std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) |
| 73 | { |
| 74 | // Skip & count leading zeroes. |
| 75 | size_t zeroes = 0; |
| 76 | size_t length = 0; |
| 77 | while (pbegin != pend && *pbegin == 0) { |
| 78 | pbegin++; |
| 79 | zeroes++; |
| 80 | } |
| 81 | // Allocate enough space in big-endian base58 representation. |
| 82 | size_t size = static_cast<size_t>((pend - pbegin) * 138 / 100 + 1); // log(256) / log(58), rounded up. |
| 83 | std::vector<unsigned char> b58(size); |
| 84 | // Process the bytes. |
| 85 | while (pbegin != pend) { |
| 86 | int carry = *pbegin; |
| 87 | size_t i = 0; |
| 88 | // Apply "b58 = b58 * 256 + ch". |
| 89 | for (auto it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); ++it, ++i) { |
| 90 | carry += 256 * (*it); |
| 91 | *it = carry % 58; |
| 92 | carry /= 58; |
| 93 | } |
| 94 | |
| 95 | assert(carry == 0); |
| 96 | length = i; |
| 97 | pbegin++; |
| 98 | } |
| 99 | // Skip leading zeroes in base58 result. |
| 100 | auto it = b58.begin(); |
| 101 | std::advance(it, static_cast<int64_t>(size - length)); |
| 102 | while (it != b58.end() && *it == 0) |
| 103 | ++it; |
| 104 | // Translate the result into a string. |
| 105 | std::string str; |
| 106 | str.reserve(zeroes + static_cast<size_t>(std::distance(it, b58.end()))); |
| 107 | str.assign(zeroes, '1'); |
| 108 | while (it != b58.end()) |
| 109 | str += pszBase58[*(it++)]; |
| 110 | return str; |
| 111 | } |
| 112 | |
| 113 | std::string EncodeBase58(const std::vector<unsigned char>& vch) |
| 114 | { |
no test coverage detected