Bitcoin Core  22.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-2021 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 <attributes.h>
13 #include <span.h>
14 #include <util/string.h>
15 
16 #include <charconv>
17 #include <cstdint>
18 #include <iterator>
19 #include <limits>
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
26 {
31 };
32 
38 enum class ByteUnit : uint64_t {
39  NOOP = 1ULL,
40  k = 1000ULL,
41  K = 1024ULL,
42  m = 1'000'000ULL,
43  M = 1ULL << 20,
44  g = 1'000'000'000ULL,
45  G = 1ULL << 30,
46  t = 1'000'000'000'000ULL,
47  T = 1ULL << 40,
48 };
49 
57 std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
58 std::vector<unsigned char> ParseHex(const char* psz);
59 std::vector<unsigned char> ParseHex(const std::string& str);
60 signed char HexDigit(char c);
61 /* Returns true if each character in str is a hex character, and has an even
62  * number of hex digits.*/
63 bool IsHex(const std::string& str);
67 bool IsHexNumber(const std::string& str);
68 std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
69 std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
70 std::string EncodeBase64(Span<const unsigned char> input);
71 inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
72 inline std::string EncodeBase64(const std::string& str) { return EncodeBase64(MakeUCharSpan(str)); }
73 std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
74 std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
75 
81 std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
82 
88 std::string EncodeBase32(const std::string& str, bool pad = true);
89 
90 void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut);
91 
92 // LocaleIndependentAtoi is provided for backwards compatibility reasons.
93 //
94 // New code should use ToIntegral or the ParseInt* functions
95 // which provide parse error feedback.
96 //
97 // The goal of LocaleIndependentAtoi is to replicate the defined behaviour of
98 // std::atoi as it behaves under the "C" locale, and remove some undefined
99 // behavior. If the parsed value is bigger than the integer type's maximum
100 // value, or smaller than the integer type's minimum value, std::atoi has
101 // undefined behavior, while this function returns the maximum or minimum
102 // values, respectively.
103 template <typename T>
104 T LocaleIndependentAtoi(const std::string& str)
105 {
106  static_assert(std::is_integral<T>::value);
107  T result;
108  // Emulate atoi(...) handling of white space and leading +/-.
109  std::string s = TrimString(str);
110  if (!s.empty() && s[0] == '+') {
111  if (s.length() >= 2 && s[1] == '-') {
112  return 0;
113  }
114  s = s.substr(1);
115  }
116  auto [_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
117  if (error_condition == std::errc::result_out_of_range) {
118  if (s.length() >= 1 && s[0] == '-') {
119  // Saturate underflow, per strtoll's behavior.
120  return std::numeric_limits<T>::min();
121  } else {
122  // Saturate overflow, per strtoll's behavior.
123  return std::numeric_limits<T>::max();
124  }
125  } else if (error_condition != std::errc{}) {
126  return 0;
127  }
128  return result;
129 }
130 
136 constexpr bool IsDigit(char c)
137 {
138  return c >= '0' && c <= '9';
139 }
140 
152 constexpr inline bool IsSpace(char c) noexcept {
153  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
154 }
155 
164 template <typename T>
165 std::optional<T> ToIntegral(const std::string& str)
166 {
167  static_assert(std::is_integral<T>::value);
168  T result;
169  const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
170  if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
171  return std::nullopt;
172  }
173  return result;
174 }
175 
181 [[nodiscard]] bool ParseInt32(const std::string& str, int32_t *out);
182 
188 [[nodiscard]] bool ParseInt64(const std::string& str, int64_t *out);
189 
195 [[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out);
196 
202 [[nodiscard]] bool ParseUInt16(const std::string& str, uint16_t* out);
203 
209 [[nodiscard]] bool ParseUInt32(const std::string& str, uint32_t *out);
210 
216 [[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out);
217 
221 std::string HexStr(const Span<const uint8_t> s);
222 inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
223 inline std::string HexStr(const Span<const std::byte> s) { return HexStr(MakeUCharSpan(s)); }
224 
229 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
230 
236 template <typename T>
237 bool TimingResistantEqual(const T& a, const T& b)
238 {
239  if (b.size() == 0) return a.size() == 0;
240  size_t accumulator = a.size() ^ b.size();
241  for (size_t i = 0; i < a.size(); i++)
242  accumulator |= size_t(a[i] ^ b[i%b.size()]);
243  return accumulator == 0;
244 }
245 
251 [[nodiscard]] bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
252 
254 template<int frombits, int tobits, bool pad, typename O, typename I>
255 bool ConvertBits(const O& outfn, I it, I end) {
256  size_t acc = 0;
257  size_t bits = 0;
258  constexpr size_t maxv = (1 << tobits) - 1;
259  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
260  while (it != end) {
261  acc = ((acc << frombits) | *it) & max_acc;
262  bits += frombits;
263  while (bits >= tobits) {
264  bits -= tobits;
265  outfn((acc >> bits) & maxv);
266  }
267  ++it;
268  }
269  if (pad) {
270  if (bits) outfn((acc << (tobits - bits)) & maxv);
271  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
272  return false;
273  }
274  return true;
275 }
276 
287 constexpr char ToLower(char c)
288 {
289  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
290 }
291 
301 std::string ToLower(const std::string& str);
302 
313 constexpr char ToUpper(char c)
314 {
315  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
316 }
317 
327 std::string ToUpper(const std::string& str);
328 
338 std::string Capitalize(std::string str);
339 
351 std::optional<uint64_t> ParseByteUnits(const std::string& str, ByteUnit default_multiplier);
352 
353 #endif // BITCOIN_UTIL_STRENCODINGS_H
ParseByteUnits
std::optional< uint64_t > ParseByteUnits(const std::string &str, ByteUnit default_multiplier)
Parse a string with suffix unit [k|K|m|M|g|G|t|T].
Definition: strencodings.cpp:525
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
MakeUCharSpan
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:285
ConvertBits
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
Definition: strencodings.h:255
ByteUnit::M
@ M
Capitalize
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
Definition: strencodings.cpp:504
string.h
ByteUnit
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
Definition: strencodings.h:38
SAFE_CHARS_UA_COMMENT
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
Definition: strencodings.h:28
ParseFixedPoint
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
Definition: strencodings.cpp:398
TrimString
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:18
ParseUInt16
bool ParseUInt16(const std::string &str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
Definition: strencodings.cpp:314
ToLower
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:287
IsSpace
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:152
SAFE_CHARS_FILENAME
@ SAFE_CHARS_FILENAME
Chars allowed in filenames.
Definition: strencodings.h:29
SAFE_CHARS_DEFAULT
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
Definition: strencodings.h:27
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
FormatParagraph
std::string FormatParagraph(const std::string &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.
Definition: strencodings.cpp:329
ToIntegral
std::optional< T > ToIntegral(const std::string &str)
Convert string to integral type T.
Definition: strencodings.h:165
ParseUInt8
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
Definition: strencodings.cpp:309
ParseUInt64
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:324
span.h
ToUpper
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
Definition: strencodings.h:313
ParseUInt32
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:319
SAFE_CHARS_URI
@ SAFE_CHARS_URI
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:30
TimingResistantEqual
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:237
attributes.h
IsDigit
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:136
ParseInt64
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:304
ByteUnit::K
@ K
ParseInt32
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:299
ByteUnit::T
@ T
ByteUnit::NOOP
@ NOOP
LocaleIndependentAtoi
T LocaleIndependentAtoi(const std::string &str)
Definition: strencodings.h:104
ByteUnit::g
@ g
ByteUnit::m
@ m
ByteUnit::k
@ k
SafeChars
SafeChars
Utilities for converting data from/to strings.
Definition: strencodings.h:25
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:511