Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/util/strencodings.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <util/strencodings.h>
7
8
#include <crypto/hex_base.h>
9
#include <span.h>
10
#include <util/check.h>
11
#include <util/overflow.h>
12
13
#include <compare>
14
#include <limits>
15
#include <optional>
16
#include <sstream>
17
#include <string>
18
#include <vector>
19
20
static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
21
22
static const std::string SAFE_CHARS[] =
23
{
24
    CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
25
    CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
26
    CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
27
    CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
28
};
29
30
std::string SanitizeString(std::string_view str, int rule)
31
702k
{
32
702k
    std::string result;
33
5.05M
    for (char c : str) {
34
5.05M
        if (SAFE_CHARS[rule].find(c) != std::string::npos) {
35
5.05M
            result.push_back(c);
36
5.05M
        }
37
5.05M
    }
38
702k
    return result;
39
702k
}
40
41
bool IsHex(std::string_view str)
42
101k
{
43
561M
    for (char c : str) {
44
561M
        if (HexDigit(c) < 0) return false;
45
561M
    }
46
100k
    return (str.size() > 0) && (str.size()%2 == 0);
47
101k
}
48
49
template <typename Byte>
50
std::optional<std::vector<Byte>> TryParseHex(std::string_view str)
51
220k
{
52
220k
    std::vector<Byte> vch;
53
220k
    vch.reserve(str.size() / 2); // two hex characters form a single byte
54
55
220k
    auto it = str.begin();
56
291M
    while (it != str.end()) {
57
291M
        if (IsSpace(*it)) {
58
116
            ++it;
59
116
            continue;
60
116
        }
61
291M
        auto c1 = HexDigit(*(it++));
62
291M
        if (it == str.end()) return std::nullopt;
63
291M
        auto c2 = HexDigit(*(it++));
64
291M
        if (c1 < 0 || c2 < 0) return std::nullopt;
65
291M
        vch.push_back(Byte(c1 << 4) | Byte(c2));
66
291M
    }
67
220k
    return vch;
68
220k
}
std::optional<std::vector<std::byte, std::allocator<std::byte>>> TryParseHex<std::byte>(std::basic_string_view<char, std::char_traits<char>>)
Line
Count
Source
51
3.98k
{
52
3.98k
    std::vector<Byte> vch;
53
3.98k
    vch.reserve(str.size() / 2); // two hex characters form a single byte
54
55
3.98k
    auto it = str.begin();
56
73.7k
    while (it != str.end()) {
57
69.7k
        if (IsSpace(*it)) {
58
10
            ++it;
59
10
            continue;
60
10
        }
61
69.7k
        auto c1 = HexDigit(*(it++));
62
69.7k
        if (it == str.end()) return std::nullopt;
63
69.7k
        auto c2 = HexDigit(*(it++));
64
69.7k
        if (c1 < 0 || c2 < 0) return std::nullopt;
65
69.7k
        vch.push_back(Byte(c1 << 4) | Byte(c2));
66
69.7k
    }
67
3.97k
    return vch;
68
3.98k
}
std::optional<std::vector<unsigned char, std::allocator<unsigned char>>> TryParseHex<unsigned char>(std::basic_string_view<char, std::char_traits<char>>)
Line
Count
Source
51
216k
{
52
216k
    std::vector<Byte> vch;
53
216k
    vch.reserve(str.size() / 2); // two hex characters form a single byte
54
55
216k
    auto it = str.begin();
56
291M
    while (it != str.end()) {
57
291M
        if (IsSpace(*it)) {
58
106
            ++it;
59
106
            continue;
60
106
        }
61
291M
        auto c1 = HexDigit(*(it++));
62
291M
        if (it == str.end()) return std::nullopt;
63
291M
        auto c2 = HexDigit(*(it++));
64
291M
        if (c1 < 0 || c2 < 0) return std::nullopt;
65
291M
        vch.push_back(Byte(c1 << 4) | Byte(c2));
66
291M
    }
67
216k
    return vch;
68
216k
}
69
template std::optional<std::vector<std::byte>> TryParseHex(std::string_view);
70
template std::optional<std::vector<uint8_t>> TryParseHex(std::string_view);
71
72
bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
73
526k
{
74
526k
    bool valid = false;
75
526k
    size_t colon = in.find_last_of(':');
76
    // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
77
526k
    bool fHaveColon = colon != in.npos;
78
526k
    bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
79
526k
    bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(':', colon - 1) != in.npos)};
80
526k
    if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
81
1.04k
        if (const auto n{ToIntegral<uint16_t>(in.substr(colon + 1))}) {
82
941
            in = in.substr(0, colon);
83
941
            portOut = *n;
84
941
            valid = (portOut != 0);
85
941
        }
86
525k
    } else {
87
525k
        valid = true;
88
525k
    }
89
526k
    if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') {
90
35
        hostOut = in.substr(1, in.size() - 2);
91
526k
    } else {
92
526k
        hostOut = in;
93
526k
    }
94
95
526k
    return valid;
96
526k
}
97
98
std::string EncodeBase64(std::span<const unsigned char> input)
99
2.00k
{
100
2.00k
    static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
101
102
2.00k
    std::string str;
103
2.00k
    str.reserve(CeilDiv(input.size(), 3u) * 4);
104
3.87M
    ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
105
2.88k
    while (str.size() % 4) str += '=';
106
2.00k
    return str;
107
2.00k
}
108
109
std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str)
110
171k
{
111
171k
    static const int8_t decode64_table[256]{
112
171k
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
113
171k
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
114
171k
        -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
115
171k
        -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
116
171k
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
117
171k
        29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
118
171k
        49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
119
171k
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
120
171k
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
121
171k
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
122
171k
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
123
171k
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
124
171k
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
125
171k
    };
126
127
171k
    if (str.size() % 4 != 0) return {};
128
    /* One or two = characters at the end are permitted. */
129
171k
    if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
130
171k
    if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
131
132
171k
    std::vector<unsigned char> ret;
133
171k
    ret.reserve((str.size() * 3) / 4);
134
171k
    bool valid = ConvertBits<6, 8, false>(
135
17.2M
        [&](unsigned char c) { ret.push_back(c); },
136
171k
        str.begin(), str.end(),
137
23.0M
        [](char c) { return decode64_table[uint8_t(c)]; }
138
171k
    );
139
171k
    if (!valid) return {};
140
141
171k
    return ret;
142
171k
}
143
144
std::string EncodeBase32(std::span<const unsigned char> input, bool pad)
145
296
{
146
296
    static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
147
148
296
    std::string str;
149
296
    str.reserve(CeilDiv(input.size(), 5u) * 8);
150
15.4k
    ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end());
151
296
    if (pad) {
152
227
        while (str.size() % 8) {
153
20
            str += '=';
154
20
        }
155
207
    }
156
296
    return str;
157
296
}
158
159
std::string EncodeBase32(std::string_view str, bool pad)
160
15
{
161
15
    return EncodeBase32(MakeUCharSpan(str), pad);
162
15
}
163
164
std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str)
165
131
{
166
131
    static const int8_t decode32_table[256]{
167
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
168
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
169
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
170
131
        -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
171
131
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1,  0,  1,  2,
172
131
         3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
173
131
        23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
175
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
176
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
177
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
178
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
179
131
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
180
131
    };
181
182
131
    if (str.size() % 8 != 0) return {};
183
    /* 1, 3, 4, or 6 padding '=' suffix characters are permitted. */
184
128
    if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
185
128
    if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2);
186
128
    if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
187
128
    if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2);
188
189
128
    std::vector<unsigned char> ret;
190
128
    ret.reserve((str.size() * 5) / 8);
191
128
    bool valid = ConvertBits<5, 8, false>(
192
3.63k
        [&](unsigned char c) { ret.push_back(c); },
193
128
        str.begin(), str.end(),
194
5.86k
        [](char c) { return decode32_table[uint8_t(c)]; }
195
128
    );
196
197
128
    if (!valid) return {};
198
199
120
    return ret;
200
128
}
201
202
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
203
169
{
204
169
    assert(width >= indent);
205
169
    std::stringstream out;
206
169
    size_t ptr = 0;
207
169
    size_t indented = 0;
208
668
    while (ptr < in.size())
209
501
    {
210
501
        size_t lineend = in.find_first_of('\n', ptr);
211
501
        if (lineend == std::string::npos) {
212
413
            lineend = in.size();
213
413
        }
214
501
        const size_t linelen = lineend - ptr;
215
501
        const size_t rem_width = width - indented;
216
501
        if (linelen <= rem_width) {
217
225
            out << in.substr(ptr, linelen + 1);
218
225
            ptr = lineend + 1;
219
225
            indented = 0;
220
276
        } else {
221
276
            size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
222
276
            if (finalspace == std::string::npos || finalspace < ptr) {
223
                // No place to break; just include the entire word and move on
224
8
                finalspace = in.find_first_of("\n ", ptr);
225
8
                if (finalspace == std::string::npos) {
226
                    // End of the string, just add it and break
227
2
                    out << in.substr(ptr);
228
2
                    break;
229
2
                }
230
8
            }
231
274
            out << in.substr(ptr, finalspace - ptr) << "\n";
232
274
            if (in[finalspace] == '\n') {
233
2
                indented = 0;
234
272
            } else if (indent) {
235
240
                out << std::string(indent, ' ');
236
240
                indented = indent;
237
240
            }
238
274
            ptr = finalspace + 1;
239
274
        }
240
501
    }
241
169
    return out.str();
242
169
}
243
244
/** Upper bound for mantissa.
245
 * 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer.
246
 * Larger integers cannot consist of arbitrary combinations of 0-9:
247
 *
248
 *   999999999999999999  1^18-1
249
 *  9223372036854775807  (1<<63)-1  (max int64_t)
250
 *  9999999999999999999  1^19-1     (would overflow)
251
 */
252
static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
253
254
/** Helper function for ParseFixedPoint */
255
static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
256
40.9k
{
257
40.9k
    if(ch == '0')
258
11.5k
        ++mantissa_tzeros;
259
29.4k
    else {
260
62.2k
        for (int i=0; i<=mantissa_tzeros; ++i) {
261
32.8k
            if (mantissa > (UPPER_BOUND / 10LL))
262
31
                return false; /* overflow */
263
32.7k
            mantissa *= 10;
264
32.7k
        }
265
29.4k
        mantissa += ch - '0';
266
29.4k
        mantissa_tzeros = 0;
267
29.4k
    }
268
40.9k
    return true;
269
40.9k
}
270
271
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
272
41.1k
{
273
41.1k
    int64_t mantissa = 0;
274
41.1k
    int64_t exponent = 0;
275
41.1k
    int mantissa_tzeros = 0;
276
41.1k
    bool mantissa_sign = false;
277
41.1k
    bool exponent_sign = false;
278
41.1k
    int ptr = 0;
279
41.1k
    int end = val.size();
280
41.1k
    int point_ofs = 0;
281
282
41.1k
    if (ptr < end && val[ptr] == '-') {
283
44
        mantissa_sign = true;
284
44
        ++ptr;
285
44
    }
286
41.1k
    if (ptr < end)
287
41.0k
    {
288
41.0k
        if (val[ptr] == '0') {
289
            /* pass single 0 */
290
34.6k
            ++ptr;
291
34.6k
        } else if (val[ptr] >= '1' && val[ptr] <= '9') {
292
15.6k
            while (ptr < end && IsDigit(val[ptr])) {
293
9.22k
                if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
294
0
                    return false; /* overflow */
295
9.22k
                ++ptr;
296
9.22k
            }
297
6.39k
        } else return false; /* missing expected digit */
298
41.0k
    } else return false; /* empty string or loose '-' */
299
41.0k
    if (ptr < end && val[ptr] == '.')
300
17.9k
    {
301
17.9k
        ++ptr;
302
17.9k
        if (ptr < end && IsDigit(val[ptr]))
303
17.9k
        {
304
49.6k
            while (ptr < end && IsDigit(val[ptr])) {
305
31.7k
                if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
306
31
                    return false; /* overflow */
307
31.7k
                ++ptr;
308
31.7k
                ++point_ofs;
309
31.7k
            }
310
17.9k
        } else return false; /* missing expected digit */
311
17.9k
    }
312
41.0k
    if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
313
3.25k
    {
314
3.25k
        ++ptr;
315
3.25k
        if (ptr < end && val[ptr] == '+')
316
4
            ++ptr;
317
3.25k
        else if (ptr < end && val[ptr] == '-') {
318
3.24k
            exponent_sign = true;
319
3.24k
            ++ptr;
320
3.24k
        }
321
3.25k
        if (ptr < end && IsDigit(val[ptr])) {
322
9.75k
            while (ptr < end && IsDigit(val[ptr])) {
323
6.49k
                if (exponent > (UPPER_BOUND / 10LL))
324
0
                    return false; /* overflow */
325
6.49k
                exponent = exponent * 10 + val[ptr] - '0';
326
6.49k
                ++ptr;
327
6.49k
            }
328
3.25k
        } else return false; /* missing expected digit */
329
3.25k
    }
330
41.0k
    if (ptr != end)
331
8
        return false; /* trailing garbage */
332
333
    /* finalize exponent */
334
41.0k
    if (exponent_sign)
335
3.24k
        exponent = -exponent;
336
41.0k
    exponent = exponent - point_ofs + mantissa_tzeros;
337
338
    /* finalize mantissa */
339
41.0k
    if (mantissa_sign)
340
24
        mantissa = -mantissa;
341
342
    /* convert to one 64-bit fixed-point value */
343
41.0k
    exponent += decimals;
344
41.0k
    if (exponent < 0)
345
65
        return false; /* cannot represent values smaller than 10^-decimals */
346
40.9k
    if (exponent >= 18)
347
6
        return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
348
349
327k
    for (int i=0; i < exponent; ++i) {
350
286k
        if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
351
9
            return false; /* overflow */
352
286k
        mantissa *= 10;
353
286k
    }
354
40.9k
    if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
355
0
        return false; /* overflow */
356
357
40.9k
    if (amount_out)
358
40.9k
        *amount_out = mantissa;
359
360
40.9k
    return true;
361
40.9k
}
362
363
std::string ToLower(std::string_view str)
364
23.2k
{
365
23.2k
    std::string r;
366
23.2k
    r.reserve(str.size());
367
92.8k
    for (auto ch : str) r += ToLower(ch);
368
23.2k
    return r;
369
23.2k
}
370
371
std::string ToUpper(std::string_view str)
372
739
{
373
739
    std::string r;
374
739
    r.reserve(str.size());
375
6.08k
    for (auto ch : str) r += ToUpper(ch);
376
739
    return r;
377
739
}
378
379
std::string Capitalize(std::string str)
380
66
{
381
66
    if (str.empty()) return str;
382
64
    str[0] = ToUpper(str.front());
383
64
    return str;
384
66
}
385
386
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
387
1.13k
{
388
1.13k
    if (str.empty()) {
389
1
        return std::nullopt;
390
1
    }
391
1.13k
    auto multiplier = default_multiplier;
392
1.13k
    char unit = str.back();
393
1.13k
    switch (unit) {
394
1
    case 'k':
395
1
        multiplier = ByteUnit::k;
396
1
        break;
397
1
    case 'K':
398
1
        multiplier = ByteUnit::K;
399
1
        break;
400
4
    case 'm':
401
4
        multiplier = ByteUnit::m;
402
4
        break;
403
1.11k
    case 'M':
404
1.11k
        multiplier = ByteUnit::M;
405
1.11k
        break;
406
2
    case 'g':
407
2
        multiplier = ByteUnit::g;
408
2
        break;
409
1
    case 'G':
410
1
        multiplier = ByteUnit::G;
411
1
        break;
412
1
    case 't':
413
1
        multiplier = ByteUnit::t;
414
1
        break;
415
2
    case 'T':
416
2
        multiplier = ByteUnit::T;
417
2
        break;
418
8
    default:
419
8
        unit = 0;
420
8
        break;
421
1.13k
    }
422
423
1.13k
    uint64_t unit_amount = static_cast<uint64_t>(multiplier);
424
1.13k
    auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
425
1.13k
    if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) { // check overflow
426
9
        return std::nullopt;
427
9
    }
428
1.12k
    return *parsed_num * unit_amount;
429
1.13k
}
430
431
bool CaseInsensitiveEqual(std::string_view s1, std::string_view s2)
432
30
{
433
30
    if (s1.size() != s2.size()) return false;
434
660
    for (size_t i = 0; i < s1.size(); ++i) {
435
638
        char c1 = s1[i];
436
638
        if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a');
437
638
        char c2 = s2[i];
438
638
        if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a');
439
638
        if (c1 != c2) return false;
440
638
    }
441
22
    return true;
442
29
}