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