Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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>
13#include <span.h>
14#include <util/string.h>
15
16#include <algorithm>
17#include <array>
18#include <bit>
19#include <charconv>
20#include <cstddef>
21#include <cstdint>
22#include <limits>
23#include <optional>
24#include <string>
25#include <string_view>
26#include <system_error>
27#include <type_traits>
28#include <vector>
29
38
44enum class ByteUnit : uint64_t {
45 NOOP = 1ULL,
46 k = 1000ULL,
47 K = 1024ULL,
48 m = 1'000'000ULL,
49 M = 1ULL << 20,
50 g = 1'000'000'000ULL,
51 G = 1ULL << 30,
52 t = 1'000'000'000'000ULL,
53 T = 1ULL << 40,
54};
55
63std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
65template <typename Byte = std::byte>
66std::optional<std::vector<Byte>> TryParseHex(std::string_view str);
68template <typename Byte = uint8_t>
69std::vector<Byte> ParseHex(std::string_view hex_str)
70{
71 return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{});
72}
73/* Returns true if each character in str is a hex character, and has an even
74 * number of hex digits.*/
75bool IsHex(std::string_view str);
76std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str);
77std::string EncodeBase64(std::span<const unsigned char> input);
78inline std::string EncodeBase64(std::span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
79inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); }
80std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str);
81
87std::string EncodeBase32(std::span<const unsigned char> input, bool pad = true);
88
94std::string EncodeBase32(std::string_view str, bool pad = true);
95
105bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
106
107// LocaleIndependentAtoi is provided for backwards compatibility reasons.
108//
109// New code should use ToIntegral.
110//
111// The goal of LocaleIndependentAtoi is to replicate the defined behaviour of
112// std::atoi as it behaves under the "C" locale, and remove some undefined
113// behavior. If the parsed value is bigger than the integer type's maximum
114// value, or smaller than the integer type's minimum value, std::atoi has
115// undefined behavior, while this function returns the maximum or minimum
116// values, respectively.
117template <typename T>
118T LocaleIndependentAtoi(std::string_view str)
119{
120 static_assert(std::is_integral_v<T>);
121 T result;
122 // Emulate atoi(...) handling of white space and leading +/-.
123 std::string_view s = util::TrimStringView(str);
124 if (!s.empty() && s[0] == '+') {
125 if (s.length() >= 2 && s[1] == '-') {
126 return 0;
127 }
128 s = s.substr(1);
129 }
130 auto [_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
131 if (error_condition == std::errc::result_out_of_range) {
132 if (s.length() >= 1 && s[0] == '-') {
133 // Saturate underflow, per strtoll's behavior.
134 return std::numeric_limits<T>::min();
135 } else {
136 // Saturate overflow, per strtoll's behavior.
137 return std::numeric_limits<T>::max();
138 }
139 } else if (error_condition != std::errc{}) {
140 return 0;
141 }
142 return result;
143}
144
150constexpr bool IsDigit(char c)
151{
152 return c >= '0' && c <= '9';
153}
154
166constexpr inline bool IsSpace(char c) noexcept {
167 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
168}
169
179template <typename T>
180std::optional<T> ToIntegral(std::string_view str, size_t base = 10)
181{
182 static_assert(std::is_integral_v<T>);
183 T result;
184 const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result, base);
185 if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
186 return std::nullopt;
187 }
188 return result;
189}
190
195std::string FormatParagraph(std::string_view in, size_t width = 79, size_t indent = 0);
196
202template <typename T>
203bool TimingResistantEqual(const T& a, const T& b)
204{
205 if (b.size() == 0) return a.size() == 0;
206 size_t accumulator = a.size() ^ b.size();
207 for (size_t i = 0; i < a.size(); i++)
208 accumulator |= size_t(a[i] ^ b[i%b.size()]);
209 return accumulator == 0;
210}
211
216[[nodiscard]] bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out);
217
218namespace {
220struct IntIdentity
221{
222 [[maybe_unused]] int operator()(int x) const { return x; }
223};
224
225} // namespace
226
228template<int frombits, int tobits, bool pad, typename O, typename It, typename I = IntIdentity>
229bool ConvertBits(O outfn, It it, It end, I infn = {}) {
230 size_t acc = 0;
231 size_t bits = 0;
232 constexpr size_t maxv = (1 << tobits) - 1;
233 constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
234 while (it != end) {
235 int v = infn(*it);
236 if (v < 0) return false;
237 acc = ((acc << frombits) | v) & max_acc;
238 bits += frombits;
239 while (bits >= tobits) {
240 bits -= tobits;
241 outfn((acc >> bits) & maxv);
242 }
243 ++it;
244 }
245 if (pad) {
246 if (bits) outfn((acc << (tobits - bits)) & maxv);
247 } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
248 return false;
249 }
250 return true;
251}
252
263constexpr char ToLower(char c)
264{
265 return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
266}
267
277std::string ToLower(std::string_view str);
278
289constexpr char ToUpper(char c)
290{
291 return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
292}
293
303std::string ToUpper(std::string_view str);
304
314std::string Capitalize(std::string str);
315
327std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier);
328
329namespace util {
331consteval uint8_t ConstevalHexDigit(const char c)
332{
333 if (c >= '0' && c <= '9') return c - '0';
334 if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
335
336 throw "Only lowercase hex digits are allowed, for consistency";
337}
338
339namespace detail {
340template <size_t N>
341struct Hex {
342 std::array<std::byte, N / 2> bytes{};
343 consteval Hex(const char (&hex_str)[N])
344 // 2 hex digits required per byte + implicit null terminator
345 requires(N % 2 == 1)
346 {
347 if (hex_str[N - 1]) throw "null terminator required";
348 for (std::size_t i = 0; i < bytes.size(); ++i) {
349 bytes[i] = static_cast<std::byte>(
350 (ConstevalHexDigit(hex_str[2 * i]) << 4) |
351 ConstevalHexDigit(hex_str[2 * i + 1]));
352 }
353 }
354};
355} // namespace detail
356
358 bool operator()(std::string_view s1, std::string_view s2) const
359 {
360 return ToLower(s1) == ToLower(s2);
361 }
362};
363
365 size_t operator()(std::string_view s) const
366 {
367 return std::hash<std::string>{}(ToLower(s));
368 }
369};
370
400inline namespace hex_literals {
401
402template <util::detail::Hex str>
403constexpr auto operator""_hex() { return str.bytes; }
404
405template <util::detail::Hex str>
406constexpr auto operator""_hex_u8() { return std::bit_cast<std::array<uint8_t, str.bytes.size()>>(str.bytes); }
407
408template <util::detail::Hex str>
409constexpr auto operator""_hex_v() { return std::vector<std::byte>{str.bytes.begin(), str.bytes.end()}; }
410
411template <util::detail::Hex str>
412inline auto operator""_hex_v_u8() { return std::vector<uint8_t>{UCharCast(str.bytes.data()), UCharCast(str.bytes.data() + str.bytes.size())}; }
413
414} // inline namespace hex_literals
415} // namespace util
416
417#endif // BITCOIN_UTIL_STRENCODINGS_H
#define T(expected, seed, data)
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition string.h:159
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.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
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)
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
std::optional< T > ToIntegral(std::string_view str, size_t base=10)
Convert string to integral type T.
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.
bool IsHex(std::string_view str)
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
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.
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.
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
@ SAFE_CHARS_URI
Chars allowed in URIs (RFC 3986).
@ SAFE_CHARS_FILENAME
Chars allowed in filenames.
size_t operator()(std::string_view s) const
bool operator()(std::string_view s1, std::string_view s2) const
consteval Hex(const char(&hex_str)[N])
std::array< std::byte, N/2 > bytes
consteval auto _(util::TranslatedLiteral str)
Definition translation.h:79
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.