|
| 1 | +// Copyright (c) 2012-2013 The Cryptonote developers |
| 2 | +// Distributed under the MIT/X11 software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | + |
| 6 | +#include "base58.h" |
| 7 | + |
| 8 | +#include <assert.h> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#include "crypto/hash.h" |
| 13 | +#include "int-util.h" |
| 14 | +#include "util.h" |
| 15 | +#include "varint.h" |
| 16 | + |
| 17 | +namespace tools |
| 18 | +{ |
| 19 | + namespace base58 |
| 20 | + { |
| 21 | + namespace |
| 22 | + { |
| 23 | + const char alphabet[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
| 24 | + const size_t alphabet_size = sizeof(alphabet) - 1; |
| 25 | + const size_t encoded_block_sizes[] = {0, 2, 3, 5, 6, 7, 9, 10, 11}; |
| 26 | + const size_t full_block_size = sizeof(encoded_block_sizes) / sizeof(encoded_block_sizes[0]) - 1; |
| 27 | + const size_t full_encoded_block_size = encoded_block_sizes[full_block_size]; |
| 28 | + const size_t addr_checksum_size = 4; |
| 29 | + |
| 30 | + struct reverse_alphabet |
| 31 | + { |
| 32 | + reverse_alphabet() |
| 33 | + { |
| 34 | + m_data.resize(alphabet[alphabet_size - 1] - alphabet[0] + 1, -1); |
| 35 | + |
| 36 | + for (size_t i = 0; i < alphabet_size; ++i) |
| 37 | + { |
| 38 | + size_t idx = static_cast<size_t>(alphabet[i] - alphabet[0]); |
| 39 | + m_data[idx] = static_cast<int8_t>(i); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + int operator()(char letter) const |
| 44 | + { |
| 45 | + size_t idx = static_cast<size_t>(letter - alphabet[0]); |
| 46 | + return idx < m_data.size() ? m_data[idx] : -1; |
| 47 | + } |
| 48 | + |
| 49 | + static reverse_alphabet instance; |
| 50 | + |
| 51 | + private: |
| 52 | + std::vector<int8_t> m_data; |
| 53 | + }; |
| 54 | + |
| 55 | + reverse_alphabet reverse_alphabet::instance; |
| 56 | + |
| 57 | + struct decoded_block_sizes |
| 58 | + { |
| 59 | + decoded_block_sizes() |
| 60 | + { |
| 61 | + m_data.resize(encoded_block_sizes[full_block_size] + 1, -1); |
| 62 | + for (size_t i = 0; i <= full_block_size; ++i) |
| 63 | + { |
| 64 | + m_data[encoded_block_sizes[i]] = static_cast<int>(i); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + int operator()(size_t encoded_block_size) const |
| 69 | + { |
| 70 | + assert(encoded_block_size <= full_encoded_block_size); |
| 71 | + return m_data[encoded_block_size]; |
| 72 | + } |
| 73 | + |
| 74 | + static decoded_block_sizes instance; |
| 75 | + |
| 76 | + private: |
| 77 | + std::vector<int> m_data; |
| 78 | + }; |
| 79 | + |
| 80 | + decoded_block_sizes decoded_block_sizes::instance; |
| 81 | + |
| 82 | + uint64_t uint_8be_to_64(const uint8_t* data, size_t size) |
| 83 | + { |
| 84 | + assert(1 <= size && size <= sizeof(uint64_t)); |
| 85 | + |
| 86 | + uint64_t res = 0; |
| 87 | + switch (9 - size) |
| 88 | + { |
| 89 | + case 1: res |= *data++; |
| 90 | + case 2: res <<= 8; res |= *data++; |
| 91 | + case 3: res <<= 8; res |= *data++; |
| 92 | + case 4: res <<= 8; res |= *data++; |
| 93 | + case 5: res <<= 8; res |= *data++; |
| 94 | + case 6: res <<= 8; res |= *data++; |
| 95 | + case 7: res <<= 8; res |= *data++; |
| 96 | + case 8: res <<= 8; res |= *data; break; |
| 97 | + default: assert(false); |
| 98 | + } |
| 99 | + |
| 100 | + return res; |
| 101 | + } |
| 102 | + |
| 103 | + void uint_64_to_8be(uint64_t num, size_t size, uint8_t* data) |
| 104 | + { |
| 105 | + assert(1 <= size && size <= sizeof(uint64_t)); |
| 106 | + |
| 107 | + uint64_t num_be = SWAP64BE(num); |
| 108 | + memcpy(data, reinterpret_cast<uint8_t*>(&num_be) + sizeof(uint64_t) - size, size); |
| 109 | + } |
| 110 | + |
| 111 | + void encode_block(const char* block, size_t size, char* res) |
| 112 | + { |
| 113 | + assert(1 <= size && size <= sizeof(full_block_size)); |
| 114 | + |
| 115 | + uint64_t num = uint_8be_to_64(reinterpret_cast<const uint8_t*>(block), size); |
| 116 | + int i = static_cast<int>(encoded_block_sizes[size]) - 1; |
| 117 | + while (0 < num) |
| 118 | + { |
| 119 | + uint64_t remainder = num % alphabet_size; |
| 120 | + num /= alphabet_size; |
| 121 | + res[i] = alphabet[remainder]; |
| 122 | + --i; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + bool decode_block(const char* block, size_t size, char* res) |
| 127 | + { |
| 128 | + assert(1 <= size && size <= full_encoded_block_size); |
| 129 | + |
| 130 | + int res_size = decoded_block_sizes::instance(size); |
| 131 | + if (res_size <= 0) |
| 132 | + return false; // Invalid block size |
| 133 | + |
| 134 | + uint64_t res_num = 0; |
| 135 | + uint64_t order = 1; |
| 136 | + for (size_t i = size - 1; i < size; --i) |
| 137 | + { |
| 138 | + int digit = reverse_alphabet::instance(block[i]); |
| 139 | + if (digit < 0) |
| 140 | + return false; // Invalid symbol |
| 141 | + |
| 142 | + uint64_t product_hi; |
| 143 | + uint64_t tmp = res_num + mul128(order, digit, &product_hi); |
| 144 | + if (tmp < res_num || 0 != product_hi) |
| 145 | + return false; // Overflow |
| 146 | + |
| 147 | + res_num = tmp; |
| 148 | + order *= alphabet_size; // Never overflows, 58^10 < 2^64 |
| 149 | + } |
| 150 | + |
| 151 | + if (static_cast<size_t>(res_size) < full_block_size && (UINT64_C(1) << (8 * res_size)) <= res_num) |
| 152 | + return false; // Overflow |
| 153 | + |
| 154 | + uint_64_to_8be(res_num, res_size, reinterpret_cast<uint8_t*>(res)); |
| 155 | + |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + std::string encode(const std::string& data) |
| 161 | + { |
| 162 | + if (data.empty()) |
| 163 | + return std::string(); |
| 164 | + |
| 165 | + size_t full_block_count = data.size() / full_block_size; |
| 166 | + size_t last_block_size = data.size() % full_block_size; |
| 167 | + size_t res_size = full_block_count * full_encoded_block_size + encoded_block_sizes[last_block_size]; |
| 168 | + |
| 169 | + std::string res(res_size, alphabet[0]); |
| 170 | + for (size_t i = 0; i < full_block_count; ++i) |
| 171 | + { |
| 172 | + encode_block(data.data() + i * full_block_size, full_block_size, &res[i * full_encoded_block_size]); |
| 173 | + } |
| 174 | + |
| 175 | + if (0 < last_block_size) |
| 176 | + { |
| 177 | + encode_block(data.data() + full_block_count * full_block_size, last_block_size, &res[full_block_count * full_encoded_block_size]); |
| 178 | + } |
| 179 | + |
| 180 | + return res; |
| 181 | + } |
| 182 | + |
| 183 | + bool decode(const std::string& enc, std::string& data) |
| 184 | + { |
| 185 | + if (enc.empty()) |
| 186 | + { |
| 187 | + data.clear(); |
| 188 | + return true; |
| 189 | + } |
| 190 | + |
| 191 | + size_t full_block_count = enc.size() / full_encoded_block_size; |
| 192 | + size_t last_block_size = enc.size() % full_encoded_block_size; |
| 193 | + int last_block_decoded_size = decoded_block_sizes::instance(last_block_size); |
| 194 | + if (last_block_decoded_size < 0) |
| 195 | + return false; // Invalid enc length |
| 196 | + size_t data_size = full_block_count * full_block_size + last_block_decoded_size; |
| 197 | + |
| 198 | + data.resize(data_size, 0); |
| 199 | + for (size_t i = 0; i < full_block_count; ++i) |
| 200 | + { |
| 201 | + if (!decode_block(enc.data() + i * full_encoded_block_size, full_encoded_block_size, &data[i * full_block_size])) |
| 202 | + return false; |
| 203 | + } |
| 204 | + |
| 205 | + if (0 < last_block_size) |
| 206 | + { |
| 207 | + if (!decode_block(enc.data() + full_block_count * full_encoded_block_size, last_block_size, |
| 208 | + &data[full_block_count * full_block_size])) |
| 209 | + return false; |
| 210 | + } |
| 211 | + |
| 212 | + return true; |
| 213 | + } |
| 214 | + |
| 215 | + std::string encode_addr(uint64_t tag, const std::string& data) |
| 216 | + { |
| 217 | + std::string buf = get_varint_data(tag); |
| 218 | + buf += data; |
| 219 | + crypto::hash hash = crypto::cn_fast_hash(buf.data(), buf.size()); |
| 220 | + const char* hash_data = reinterpret_cast<const char*>(&hash); |
| 221 | + buf.append(hash_data, addr_checksum_size); |
| 222 | + return encode(buf); |
| 223 | + } |
| 224 | + |
| 225 | + bool decode_addr(std::string addr, uint64_t& tag, std::string& data) |
| 226 | + { |
| 227 | + std::string addr_data; |
| 228 | + bool r = decode(addr, addr_data); |
| 229 | + if (!r) return false; |
| 230 | + if (addr_data.size() <= addr_checksum_size) return false; |
| 231 | + |
| 232 | + std::string checksum(addr_checksum_size, '\0'); |
| 233 | + checksum = addr_data.substr(addr_data.size() - addr_checksum_size); |
| 234 | + |
| 235 | + addr_data.resize(addr_data.size() - addr_checksum_size); |
| 236 | + crypto::hash hash = crypto::cn_fast_hash(addr_data.data(), addr_data.size()); |
| 237 | + std::string expected_checksum(reinterpret_cast<const char*>(&hash), addr_checksum_size); |
| 238 | + if (expected_checksum != checksum) return false; |
| 239 | + |
| 240 | + int read = tools::read_varint(addr_data.begin(), addr_data.end(), tag); |
| 241 | + if (read <= 0) return false; |
| 242 | + |
| 243 | + data = addr_data.substr(read); |
| 244 | + return true; |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments