Bitcoin Core  25.99.0
P2P Digital Currency
strencodings.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 <span.h>
7 #include <util/strencodings.h>
8 
9 #include <array>
10 #include <cassert>
11 #include <cstring>
12 #include <limits>
13 #include <optional>
14 #include <ostream>
15 #include <string>
16 #include <vector>
17 
18 static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
19 
20 static const std::string SAFE_CHARS[] =
21 {
22  CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
23  CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
24  CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
25  CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
26 };
27 
28 std::string SanitizeString(std::string_view str, int rule)
29 {
30  std::string result;
31  for (char c : str) {
32  if (SAFE_CHARS[rule].find(c) != std::string::npos) {
33  result.push_back(c);
34  }
35  }
36  return result;
37 }
38 
39 const signed char p_util_hexdigit[256] =
40 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
41  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
42  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
43  0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
44  -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
45  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
46  -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
47  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
48  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
49  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
50  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
51  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
52  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
53  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
54  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
55  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
56 
57 signed char HexDigit(char c)
58 {
59  return p_util_hexdigit[(unsigned char)c];
60 }
61 
62 bool IsHex(std::string_view str)
63 {
64  for (char c : str) {
65  if (HexDigit(c) < 0) return false;
66  }
67  return (str.size() > 0) && (str.size()%2 == 0);
68 }
69 
70 bool IsHexNumber(std::string_view str)
71 {
72  if (str.substr(0, 2) == "0x") str.remove_prefix(2);
73  for (char c : str) {
74  if (HexDigit(c) < 0) return false;
75  }
76  // Return false for empty string or "0x".
77  return str.size() > 0;
78 }
79 
80 template <typename Byte>
81 std::optional<std::vector<Byte>> TryParseHex(std::string_view str)
82 {
83  std::vector<Byte> vch;
84  auto it = str.begin();
85  while (it != str.end()) {
86  if (IsSpace(*it)) {
87  ++it;
88  continue;
89  }
90  auto c1 = HexDigit(*(it++));
91  if (it == str.end()) return std::nullopt;
92  auto c2 = HexDigit(*(it++));
93  if (c1 < 0 || c2 < 0) return std::nullopt;
94  vch.push_back(Byte(c1 << 4) | Byte(c2));
95  }
96  return vch;
97 }
98 template std::optional<std::vector<std::byte>> TryParseHex(std::string_view);
99 template std::optional<std::vector<uint8_t>> TryParseHex(std::string_view);
100 
101 bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
102 {
103  bool valid = false;
104  size_t colon = in.find_last_of(':');
105  // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
106  bool fHaveColon = colon != in.npos;
107  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
108  bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(':', colon - 1) != in.npos)};
109  if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
110  uint16_t n;
111  if (ParseUInt16(in.substr(colon + 1), &n)) {
112  in = in.substr(0, colon);
113  portOut = n;
114  valid = (portOut != 0);
115  }
116  } else {
117  valid = true;
118  }
119  if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') {
120  hostOut = in.substr(1, in.size() - 2);
121  } else {
122  hostOut = in;
123  }
124 
125  return valid;
126 }
127 
129 {
130  static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
131 
132  std::string str;
133  str.reserve(((input.size() + 2) / 3) * 4);
134  ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
135  while (str.size() % 4) str += '=';
136  return str;
137 }
138 
139 std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str)
140 {
141  static const int8_t decode64_table[256]{
142  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
143  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
144  -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
145  -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
146  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
147  29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
148  49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
149  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
151  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
152  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
153  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
154  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
155  };
156 
157  if (str.size() % 4 != 0) return {};
158  /* One or two = characters at the end are permitted. */
159  if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
160  if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
161 
162  std::vector<unsigned char> ret;
163  ret.reserve((str.size() * 3) / 4);
164  bool valid = ConvertBits<6, 8, false>(
165  [&](unsigned char c) { ret.push_back(c); },
166  str.begin(), str.end(),
167  [](char c) { return decode64_table[uint8_t(c)]; }
168  );
169  if (!valid) return {};
170 
171  return ret;
172 }
173 
174 std::string EncodeBase32(Span<const unsigned char> input, bool pad)
175 {
176  static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
177 
178  std::string str;
179  str.reserve(((input.size() + 4) / 5) * 8);
180  ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end());
181  if (pad) {
182  while (str.size() % 8) {
183  str += '=';
184  }
185  }
186  return str;
187 }
188 
189 std::string EncodeBase32(std::string_view str, bool pad)
190 {
191  return EncodeBase32(MakeUCharSpan(str), pad);
192 }
193 
194 std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str)
195 {
196  static const int8_t decode32_table[256]{
197  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
198  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
199  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
200  -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
201  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
202  3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
203  23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
204  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
205  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
206  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
207  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
208  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
209  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
210  };
211 
212  if (str.size() % 8 != 0) return {};
213  /* 1, 3, 4, or 6 padding '=' suffix characters are permitted. */
214  if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
215  if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2);
216  if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1);
217  if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2);
218 
219  std::vector<unsigned char> ret;
220  ret.reserve((str.size() * 5) / 8);
221  bool valid = ConvertBits<5, 8, false>(
222  [&](unsigned char c) { ret.push_back(c); },
223  str.begin(), str.end(),
224  [](char c) { return decode32_table[uint8_t(c)]; }
225  );
226 
227  if (!valid) return {};
228 
229  return ret;
230 }
231 
232 namespace {
233 template <typename T>
234 bool ParseIntegral(std::string_view str, T* out)
235 {
236  static_assert(std::is_integral<T>::value);
237  // Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
238  // handling leading +/- for backwards compatibility.
239  if (str.length() >= 2 && str[0] == '+' && str[1] == '-') {
240  return false;
241  }
242  const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] == '+') ? str.substr(1) : str);
243  if (!opt_int) {
244  return false;
245  }
246  if (out != nullptr) {
247  *out = *opt_int;
248  }
249  return true;
250 }
251 }; // namespace
252 
253 bool ParseInt32(std::string_view str, int32_t* out)
254 {
255  return ParseIntegral<int32_t>(str, out);
256 }
257 
258 bool ParseInt64(std::string_view str, int64_t* out)
259 {
260  return ParseIntegral<int64_t>(str, out);
261 }
262 
263 bool ParseUInt8(std::string_view str, uint8_t* out)
264 {
265  return ParseIntegral<uint8_t>(str, out);
266 }
267 
268 bool ParseUInt16(std::string_view str, uint16_t* out)
269 {
270  return ParseIntegral<uint16_t>(str, out);
271 }
272 
273 bool ParseUInt32(std::string_view str, uint32_t* out)
274 {
275  return ParseIntegral<uint32_t>(str, out);
276 }
277 
278 bool ParseUInt64(std::string_view str, uint64_t* out)
279 {
280  return ParseIntegral<uint64_t>(str, out);
281 }
282 
283 std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
284 {
285  assert(width >= indent);
286  std::stringstream out;
287  size_t ptr = 0;
288  size_t indented = 0;
289  while (ptr < in.size())
290  {
291  size_t lineend = in.find_first_of('\n', ptr);
292  if (lineend == std::string::npos) {
293  lineend = in.size();
294  }
295  const size_t linelen = lineend - ptr;
296  const size_t rem_width = width - indented;
297  if (linelen <= rem_width) {
298  out << in.substr(ptr, linelen + 1);
299  ptr = lineend + 1;
300  indented = 0;
301  } else {
302  size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
303  if (finalspace == std::string::npos || finalspace < ptr) {
304  // No place to break; just include the entire word and move on
305  finalspace = in.find_first_of("\n ", ptr);
306  if (finalspace == std::string::npos) {
307  // End of the string, just add it and break
308  out << in.substr(ptr);
309  break;
310  }
311  }
312  out << in.substr(ptr, finalspace - ptr) << "\n";
313  if (in[finalspace] == '\n') {
314  indented = 0;
315  } else if (indent) {
316  out << std::string(indent, ' ');
317  indented = indent;
318  }
319  ptr = finalspace + 1;
320  }
321  }
322  return out.str();
323 }
324 
333 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
334 
336 static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
337 {
338  if(ch == '0')
339  ++mantissa_tzeros;
340  else {
341  for (int i=0; i<=mantissa_tzeros; ++i) {
342  if (mantissa > (UPPER_BOUND / 10LL))
343  return false; /* overflow */
344  mantissa *= 10;
345  }
346  mantissa += ch - '0';
347  mantissa_tzeros = 0;
348  }
349  return true;
350 }
351 
352 bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
353 {
354  int64_t mantissa = 0;
355  int64_t exponent = 0;
356  int mantissa_tzeros = 0;
357  bool mantissa_sign = false;
358  bool exponent_sign = false;
359  int ptr = 0;
360  int end = val.size();
361  int point_ofs = 0;
362 
363  if (ptr < end && val[ptr] == '-') {
364  mantissa_sign = true;
365  ++ptr;
366  }
367  if (ptr < end)
368  {
369  if (val[ptr] == '0') {
370  /* pass single 0 */
371  ++ptr;
372  } else if (val[ptr] >= '1' && val[ptr] <= '9') {
373  while (ptr < end && IsDigit(val[ptr])) {
374  if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
375  return false; /* overflow */
376  ++ptr;
377  }
378  } else return false; /* missing expected digit */
379  } else return false; /* empty string or loose '-' */
380  if (ptr < end && val[ptr] == '.')
381  {
382  ++ptr;
383  if (ptr < end && IsDigit(val[ptr]))
384  {
385  while (ptr < end && IsDigit(val[ptr])) {
386  if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
387  return false; /* overflow */
388  ++ptr;
389  ++point_ofs;
390  }
391  } else return false; /* missing expected digit */
392  }
393  if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
394  {
395  ++ptr;
396  if (ptr < end && val[ptr] == '+')
397  ++ptr;
398  else if (ptr < end && val[ptr] == '-') {
399  exponent_sign = true;
400  ++ptr;
401  }
402  if (ptr < end && IsDigit(val[ptr])) {
403  while (ptr < end && IsDigit(val[ptr])) {
404  if (exponent > (UPPER_BOUND / 10LL))
405  return false; /* overflow */
406  exponent = exponent * 10 + val[ptr] - '0';
407  ++ptr;
408  }
409  } else return false; /* missing expected digit */
410  }
411  if (ptr != end)
412  return false; /* trailing garbage */
413 
414  /* finalize exponent */
415  if (exponent_sign)
416  exponent = -exponent;
417  exponent = exponent - point_ofs + mantissa_tzeros;
418 
419  /* finalize mantissa */
420  if (mantissa_sign)
421  mantissa = -mantissa;
422 
423  /* convert to one 64-bit fixed-point value */
424  exponent += decimals;
425  if (exponent < 0)
426  return false; /* cannot represent values smaller than 10^-decimals */
427  if (exponent >= 18)
428  return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
429 
430  for (int i=0; i < exponent; ++i) {
431  if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
432  return false; /* overflow */
433  mantissa *= 10;
434  }
435  if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
436  return false; /* overflow */
437 
438  if (amount_out)
439  *amount_out = mantissa;
440 
441  return true;
442 }
443 
444 std::string ToLower(std::string_view str)
445 {
446  std::string r;
447  for (auto ch : str) r += ToLower(ch);
448  return r;
449 }
450 
451 std::string ToUpper(std::string_view str)
452 {
453  std::string r;
454  for (auto ch : str) r += ToUpper(ch);
455  return r;
456 }
457 
458 std::string Capitalize(std::string str)
459 {
460  if (str.empty()) return str;
461  str[0] = ToUpper(str.front());
462  return str;
463 }
464 
465 namespace {
466 
467 using ByteAsHex = std::array<char, 2>;
468 
469 constexpr std::array<ByteAsHex, 256> CreateByteToHexMap()
470 {
471  constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
472 
473  std::array<ByteAsHex, 256> byte_to_hex{};
474  for (size_t i = 0; i < byte_to_hex.size(); ++i) {
475  byte_to_hex[i][0] = hexmap[i >> 4];
476  byte_to_hex[i][1] = hexmap[i & 15];
477  }
478  return byte_to_hex;
479 }
480 
481 } // namespace
482 
483 std::string HexStr(const Span<const uint8_t> s)
484 {
485  std::string rv(s.size() * 2, '\0');
486  static constexpr auto byte_to_hex = CreateByteToHexMap();
487  static_assert(sizeof(byte_to_hex) == 512);
488 
489  char* it = rv.data();
490  for (uint8_t v : s) {
491  std::memcpy(it, byte_to_hex[v].data(), 2);
492  it += 2;
493  }
494 
495  assert(it == rv.data() + rv.size());
496  return rv;
497 }
498 
499 std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
500 {
501  if (str.empty()) {
502  return std::nullopt;
503  }
504  auto multiplier = default_multiplier;
505  char unit = str.back();
506  switch (unit) {
507  case 'k':
508  multiplier = ByteUnit::k;
509  break;
510  case 'K':
511  multiplier = ByteUnit::K;
512  break;
513  case 'm':
514  multiplier = ByteUnit::m;
515  break;
516  case 'M':
517  multiplier = ByteUnit::M;
518  break;
519  case 'g':
520  multiplier = ByteUnit::g;
521  break;
522  case 'G':
523  multiplier = ByteUnit::G;
524  break;
525  case 't':
526  multiplier = ByteUnit::t;
527  break;
528  case 'T':
529  multiplier = ByteUnit::T;
530  break;
531  default:
532  unit = 0;
533  break;
534  }
535 
536  uint64_t unit_amount = static_cast<uint64_t>(multiplier);
537  auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
538  if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) { // check overflow
539  return std::nullopt;
540  }
541  return *parsed_num * unit_amount;
542 }
int ret
constexpr std::size_t size() const noexcept
Definition: span.h:186
constexpr C * end() const noexcept
Definition: span.h:175
constexpr C * begin() const noexcept
Definition: span.h:174
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:281
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:152
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
Definition: strencodings.h:40
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:168
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
static const std::string SAFE_CHARS[]
bool IsHexNumber(std::string_view str)
Return true if the string is a hex number, optionally prefixed with "0x".
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-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.
bool ParseUInt16(std::string_view str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
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].
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
const signed char p_util_hexdigit[256]
std::string EncodeBase64(Span< const unsigned char > input)
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
bool ParseUInt8(std::string_view str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
bool ParseUInt64(std::string_view str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
signed char HexDigit(char c)
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
bool IsHex(std::string_view str)
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
static const int64_t UPPER_BOUND
Upper bound for mantissa.
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 ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
static const std::string CHARS_ALPHA_NUM
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
assert(!tx.IsCoinBase())