ClickHouse/src/Common/Base58.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
3.5 KiB
C++
Raw Normal View History

#include <Common/Base58.h>
2022-09-11 01:53:04 +00:00
namespace DB
{
2022-09-11 04:31:15 +00:00
size_t encodeBase58(const UInt8 * src, size_t src_length, UInt8 * dst)
2022-09-11 01:53:04 +00:00
{
const char * base58_encoding_alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
size_t processed = 0;
2022-09-11 06:09:14 +00:00
size_t idx = 0;
2022-09-11 01:53:04 +00:00
size_t zeros = 0;
2022-09-11 06:09:14 +00:00
while (processed < src_length && *src == 0)
2022-09-11 01:53:04 +00:00
{
++processed;
++zeros;
*dst = '1';
++dst;
2022-09-11 04:31:15 +00:00
++src;
2022-09-11 01:53:04 +00:00
}
2022-09-11 06:09:14 +00:00
while (processed < src_length)
2022-09-11 01:53:04 +00:00
{
2022-09-11 06:09:14 +00:00
UInt32 carry = *src;
2022-09-11 01:53:04 +00:00
for (size_t j = 0; j < idx; ++j)
{
2022-09-11 06:09:14 +00:00
carry += static_cast<UInt32>(dst[j]) << 8;
2022-09-11 01:53:04 +00:00
dst[j] = static_cast<UInt8>(carry % 58);
carry /= 58;
}
while (carry > 0)
{
dst[idx] = static_cast<UInt8>(carry % 58);
++idx;
carry /= 58;
}
2022-09-11 06:09:14 +00:00
2022-09-11 01:53:04 +00:00
++src;
++processed;
}
size_t c_idx = idx >> 1;
for (size_t i = 0; i < c_idx; ++i)
{
2024-05-09 00:07:04 +00:00
char s = base58_encoding_alphabet[dst[i]];
dst[i] = base58_encoding_alphabet[dst[idx - (i + 1)]];
2022-09-11 01:53:04 +00:00
dst[idx - (i + 1)] = s;
}
if ((idx & 1))
{
2024-05-09 00:07:04 +00:00
dst[c_idx] = base58_encoding_alphabet[dst[c_idx]];
2022-09-11 01:53:04 +00:00
}
2022-09-11 06:09:14 +00:00
return zeros + idx;
2022-09-11 01:53:04 +00:00
}
2022-09-11 06:09:14 +00:00
std::optional<size_t> decodeBase58(const UInt8 * src, size_t src_length, UInt8 * dst)
2022-09-11 01:53:04 +00:00
{
2022-09-11 06:09:14 +00:00
static const Int8 map_digits[256] =
2022-09-11 01:53:04 +00:00
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1,
-1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1,
-1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46,
2022-09-11 06:09:14 +00:00
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2022-09-11 01:53:04 +00:00
};
size_t processed = 0;
2022-09-11 06:09:14 +00:00
size_t idx = 0;
2022-09-11 01:53:04 +00:00
size_t zeros = 0;
2022-09-11 06:09:14 +00:00
while (processed < src_length && *src == '1')
2022-09-11 01:53:04 +00:00
{
++processed;
++zeros;
*dst = '\0';
++dst;
++src;
}
2022-09-11 06:09:14 +00:00
while (processed < src_length)
2022-09-11 01:53:04 +00:00
{
2022-09-11 18:43:36 +00:00
Int8 digit = map_digits[*src];
UInt32 carry = digit == -1 ? 0xFFFFFFFFU : static_cast<UInt32>(digit);
2022-09-11 01:53:04 +00:00
if (carry == static_cast<UInt32>(-1))
{
2022-09-11 06:09:14 +00:00
return {};
2022-09-11 01:53:04 +00:00
}
for (size_t j = 0; j < idx; ++j)
{
2024-05-10 01:31:40 +00:00
carry += dst[j] * 58;
2022-09-11 01:53:04 +00:00
dst[j] = static_cast<UInt8>(carry & 0xFF);
carry >>= 8;
}
while (carry > 0)
{
dst[idx] = static_cast<UInt8>(carry & 0xFF);
++idx;
carry >>= 8;
}
++src;
++processed;
}
size_t c_idx = idx >> 1;
for (size_t i = 0; i < c_idx; ++i)
{
2022-09-11 06:09:14 +00:00
UInt8 s = dst[i];
2022-09-11 01:53:04 +00:00
dst[i] = dst[idx - (i + 1)];
dst[idx - (i + 1)] = s;
}
2022-09-11 06:09:14 +00:00
return zeros + idx;
2022-09-11 01:53:04 +00:00
}
}