aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/Base58.cpp
blob: 32289c23e91cb627d02dc0e07d82eb656191622a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <Common/Base58.h>


namespace DB
{

size_t encodeBase58(const UInt8 * src, size_t src_length, UInt8 * dst)
{
    const char * base58_encoding_alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

    size_t processed = 0;
    size_t idx = 0;
    size_t zeros = 0;

    while (processed < src_length && *src == 0)
    {
        ++processed;
        ++zeros;
        *dst = '1';
        ++dst;
        ++src;
    }

    while (processed < src_length)
    {
        UInt32 carry = *src;

        for (size_t j = 0; j < idx; ++j)
        {
            carry += static_cast<UInt32>(dst[j]) << 8;
            dst[j] = static_cast<UInt8>(carry % 58);
            carry /= 58;
        }

        while (carry > 0)
        {
            dst[idx] = static_cast<UInt8>(carry % 58);
            ++idx;
            carry /= 58;
        }

        ++src;
        ++processed;
    }

    size_t c_idx = idx >> 1;
    for (size_t i = 0; i < c_idx; ++i)
    {
        char s = base58_encoding_alphabet[static_cast<UInt8>(dst[i])];
        dst[i] = base58_encoding_alphabet[static_cast<UInt8>(dst[idx - (i + 1)])];
        dst[idx - (i + 1)] = s;
    }

    if ((idx & 1))
    {
        dst[c_idx] = base58_encoding_alphabet[static_cast<UInt8>(dst[c_idx])];
    }

    return zeros + idx;
}


std::optional<size_t> decodeBase58(const UInt8 * src, size_t src_length, UInt8 * dst)
{
    static const Int8 map_digits[256] =
    {
        -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,
        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,
    };

    size_t processed = 0;
    size_t idx = 0;
    size_t zeros = 0;

    while (processed < src_length && *src == '1')
    {
        ++processed;
        ++zeros;
        *dst = '\0';
        ++dst;
        ++src;
    }

    while (processed < src_length)
    {
        Int8 digit = map_digits[*src];
        UInt32 carry = digit == -1 ? 0xFFFFFFFFU : static_cast<UInt32>(digit);
        if (carry == static_cast<UInt32>(-1))
        {
            return {};
        }
        for (size_t j = 0; j < idx; ++j)
        {
            carry += static_cast<UInt8>(dst[j]) * 58;
            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)
    {
        UInt8 s = dst[i];
        dst[i] = dst[idx - (i + 1)];
        dst[idx - (i + 1)] = s;
    }

    return zeros + idx;
}

}