Bitcoin Core 29.99.0
P2P Digital Currency
strencodings.h
Go to the documentation of this file.
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
9#ifndef BITCOIN_UTIL_STRENCODINGS_H
10#define BITCOIN_UTIL_STRENCODINGS_H
11
12#include <crypto/hex_base.h> // IWYU pragma: export
13#include <span.h>
14#include <util/string.h>
15
16#include <array>
17#include <bit>
18#include <charconv>
19#include <cstddef>
20#include <cstdint>
21#include <limits>
22#include <optional>
23#include <string> // IWYU pragma: export
24#include <string_view> // IWYU pragma: export
25#include <system_error>
26#include <type_traits>
27#include <vector>
28
31{
36};
37
43enum class ByteUnit : uint64_t {
44 NOOP = 1ULL,
45 k = 1000ULL,
46 K = 1024ULL,
47 m = 1'000'000ULL,
48 M = 1ULL << 20,
49 g = 1'000'000'000ULL,
50 G = 1ULL << 30,
51 t = 1'000'000'000'000ULL,
52 T = 1ULL << 40,
53};
54
62std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
64template <typename Byte = std::byte>
65std::optional<std::vector<Byte>> TryParseHex(std::string_view str);
67template <typename Byte = uint8_t>
68std::vector<Byte> ParseHex(std::string_view hex_str)
69{
70 return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{});
71}
72/* Returns true if each character in str is a hex character, and has an even
73 * number of hex digits.*/
74bool IsHex(std::string_view str);
75std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str);
76std::string EncodeBase64(std::span<const unsigned char> input);
77inline std::string EncodeBase64(std::span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
78inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); }
79std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str);
80
86std::string EncodeBase32(std::span<const unsigned char> input, bool pad = true);
87
93std::string EncodeBase32(std::string_view str, bool pad = true);
94
104bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
105
106// LocaleIndependentAtoi is provided for backwards compatibility reasons.
107//
108// New code should use ToIntegral.
109//
110// The goal of LocaleIndependentAtoi is to replicate the defined behaviour of
111// std::atoi as it behaves under the "C" locale, and remove some undefined
112// behavior. If the parsed value is bigger than the integer type's maximum
113// value, or smaller than the integer type's minimum value, std::atoi has
114// undefined behavior, while this function returns the maximum or minimum
115// values, respectively.
116template <typename T>
117T LocaleIndependentAtoi(std::string_view str)
118{
119 static_assert(std::is_integral_v<T>);
120 T result;
121 // Emulate atoi(...) handling of white space and leading +/-.
122 std::string_view s = util::TrimStringView(str);
123 if (!s.empty() && s[0] == '+') {
124 if (s.length() >= 2 && s[1] == '-') {
125 return 0;
126 }
127 s = s.substr(1);
128 }
129 auto [_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
130 if (error_condition == std::errc::result_out_of_range) {
131 if (s.length() >= 1 && s[0] == '-') {
132 // Saturate underflow, per strtoll's behavior.
133 return std::numeric_limits<T>::min();
134 } else {
135 // Saturate overflow, per strtoll's behavior.
136 return std::numeric_limits<T>::max();
137 }
138 } else if (error_condition != std::errc{}) {
139 return 0;
140 }
141 return result;
142}
143
149constexpr bool IsDigit(char c)
150{
151 return c >= '0' && c <= '9';
152}
153
165constexpr inline bool IsSpace(char c) noexcept {
166 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
167}
168
177template <typename T>
178std::optional<T> ToIntegral(std::string_view str)
179{
180 static_assert(std::is_integral_v<T>);
181 T result;
182 const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
183 if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
184 return std::nullopt;
185 }
186 return result;
187}
188
193std::string FormatParagraph(std::string_view in, size_t width = 79, size_t indent = 0);
194
200template <typename T>
201bool TimingResistantEqual(const T& a, const T& b)
202{
203 if (b.size() == 0) return a.size() == 0;
204 size_t accumulator = a.size() ^ b.size();
205 for (size_t i = 0; i < a.size(); i++)
206 accumulator |= size_t(a[i] ^ b[i%b.size()]);
207 return accumulator == 0;
208}
209
214[[nodiscard]] bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out);
215
216namespace {
218struct IntIdentity
219{
220 [[maybe_unused]] int operator()(int x) const { return x; }
221};
222
223} // namespace
224
226template<int frombits, int tobits, bool pad, typename O, typename It, typename I = IntIdentity>
227bool ConvertBits(O outfn, It it, It end, I infn = {}) {
228 size_t acc = 0;
229 size_t bits = 0;
230 constexpr size_t maxv = (1 << tobits) - 1;
231 constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
232 while (it != end) {
233 int v = infn(*it);
234 if (v < 0) return false;
235 acc = ((acc << frombits) | v) & max_acc;
236 bits += frombits;
237 while (bits >= tobits) {
238 bits -= tobits;
239 outfn((acc >> bits) & maxv);
240 }
241 ++it;
242 }
243 if (pad) {
244 if (bits) outfn((acc << (tobits - bits)) & maxv);
245 } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
246 return false;
247 }
248 return true;
249}
250
261constexpr char ToLower(char c)
262{
263 return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
264}
265
275std::string ToLower(std::string_view str);
276
287constexpr char ToUpper(char c)
288{
289 return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
290}
291
301std::string ToUpper(std::string_view str);
302
312std::string Capitalize(std::string str);
313
325std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier);
326
327namespace util {
329consteval uint8_t ConstevalHexDigit(const char c)
330{
331 if (c >= '0' && c <= '9') return c - '0';
332 if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
333
334 throw "Only lowercase hex digits are allowed, for consistency";
335}
336
337namespace detail {
338template <size_t N>
339struct Hex {
340 std::array<std::byte, N / 2> bytes{};
341 consteval Hex(const char (&hex_str)[N])
342 // 2 hex digits required per byte + implicit null terminator
343 requires(N % 2 == 1)
344 {
345 if (hex_str[N - 1]) throw "null terminator required";
346 for (std::size_t i = 0; i < bytes.size(); ++i) {
347 bytes[i] = static_cast<std::byte>(
348 (ConstevalHexDigit(hex_str[2 * i]) << 4) |
349 ConstevalHexDigit(hex_str[2 * i + 1]));
350 }
351 }
352};
353} // namespace detail
354
384inline namespace hex_literals {
385
386template <util::detail::Hex str>
387constexpr auto operator""_hex() { return str.bytes; }
388
389template <util::detail::Hex str>
390constexpr auto operator""_hex_u8() { return std::bit_cast<std::array<uint8_t, str.bytes.size()>>(str.bytes); }
391
392template <util::detail::Hex str>
393constexpr auto operator""_hex_v() { return std::vector<std::byte>{str.bytes.begin(), str.bytes.end()}; }
394
395template <util::detail::Hex str>
396inline auto operator""_hex_v_u8() { return std::vector<uint8_t>{UCharCast(str.bytes.data()), UCharCast(str.bytes.data() + str.bytes.size())}; }
397
398} // inline namespace hex_literals
399} // namespace util
400
401#endif // BITCOIN_UTIL_STRENCODINGS_H
#define T(expected, seed, data)
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
Definition: strencodings.h:329
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:146
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
Definition: span.h:111
unsigned char * UCharCast(char *c)
Definition: span.h:95
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:261
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:149
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
Definition: strencodings.h:287
std::string EncodeBase32(std::span< const unsigned char > input, bool pad=true)
Base32 encode.
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
T LocaleIndependentAtoi(std::string_view str)
Definition: strencodings.h:117
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
Definition: strencodings.h:43
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:201
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:68
bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:165
bool IsHex(std::string_view str)
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
std::optional< T > ToIntegral(std::string_view str)
Convert string to integral type T.
Definition: strencodings.h:178
bool SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
Splits socket address string into host string and port value.
bool ConvertBits(O outfn, It it, It end, I infn={})
Convert from one power-of-2 number base to another.
Definition: strencodings.h:227
std::string EncodeBase64(std::span< const unsigned char > input)
std::string FormatParagraph(std::string_view in, size_t width=79, size_t indent=0)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
std::string SanitizeString(std::string_view str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::optional< uint64_t > ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
Parse a string with suffix unit [k|K|m|M|g|G|t|T].
SafeChars
Utilities for converting data from/to strings.
Definition: strencodings.h:31
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
Definition: strencodings.h:32
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
Definition: strencodings.h:33
@ SAFE_CHARS_URI
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:35
@ SAFE_CHARS_FILENAME
Chars allowed in filenames.
Definition: strencodings.h:34
consteval Hex(const char(&hex_str)[N])
Definition: strencodings.h:341
std::array< std::byte, N/2 > bytes
Definition: strencodings.h:340
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79