Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-03-24 13:57

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