Bitcoin ABC  0.26.3
P2P Digital Currency
strencodings.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 <util/strencodings.h>
7 #include <util/string.h>
8 
9 #include <tinyformat.h>
10 
11 #include <algorithm>
12 #include <cerrno>
13 #include <cstdlib>
14 #include <cstring>
15 #include <limits>
16 
17 static const std::string CHARS_ALPHA_NUM =
18  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
19 
20 static const std::string SAFE_CHARS[] = {
21  // SAFE_CHARS_DEFAULT
22  CHARS_ALPHA_NUM + " .,;-_/:?@()",
23  // SAFE_CHARS_UA_COMMENT
24  CHARS_ALPHA_NUM + " .,;-_?@",
25  // SAFE_CHARS_FILENAME
26  CHARS_ALPHA_NUM + ".-_",
27  // SAFE_CHARS_URI
28  CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%",
29 };
30 
31 std::string SanitizeString(const std::string &str, int rule) {
32  std::string strResult;
33  for (std::string::size_type i = 0; i < str.size(); i++) {
34  if (SAFE_CHARS[rule].find(str[i]) != std::string::npos) {
35  strResult.push_back(str[i]);
36  }
37  }
38  return strResult;
39 }
40 
41 const signed char p_util_hexdigit[256] = {
42  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -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, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -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  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58 };
59 
60 signed char HexDigit(char c) {
61  return p_util_hexdigit[(uint8_t)c];
62 }
63 
64 bool IsHex(const std::string &str) {
65  for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) {
66  if (HexDigit(*it) < 0) {
67  return false;
68  }
69  }
70  return (str.size() > 0) && (str.size() % 2 == 0);
71 }
72 
73 bool IsHexNumber(const std::string &str) {
74  size_t starting_location = 0;
75  if (str.size() > 2 && *str.begin() == '0' && *(str.begin() + 1) == 'x') {
76  starting_location = 2;
77  }
78  for (auto c : str.substr(starting_location)) {
79  if (HexDigit(c) < 0) {
80  return false;
81  }
82  }
83  // Return false for empty string or "0x".
84  return (str.size() > starting_location);
85 }
86 
87 std::vector<uint8_t> ParseHex(const char *psz) {
88  // convert hex dump to vector
89  std::vector<uint8_t> vch;
90  while (true) {
91  while (IsSpace(*psz)) {
92  psz++;
93  }
94  signed char c = HexDigit(*psz++);
95  if (c == (signed char)-1) {
96  break;
97  }
98  auto n{uint8_t(c << 4)};
99  c = HexDigit(*psz++);
100  if (c == (signed char)-1) {
101  break;
102  }
103  n |= c;
104  vch.push_back(n);
105  }
106  return vch;
107 }
108 
109 std::vector<uint8_t> ParseHex(const std::string &str) {
110  return ParseHex(str.c_str());
111 }
112 
113 void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut) {
114  size_t colon = in.find_last_of(':');
115  // if a : is found, and it either follows a [...], or no other : is in the
116  // string, treat it as port separator
117  bool fHaveColon = colon != in.npos;
118  // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is
119  // safe
120  bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']');
121  bool fMultiColon =
122  fHaveColon && (in.find_last_of(':', colon - 1) != in.npos);
123  if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
124  uint16_t n;
125  if (ParseUInt16(in.substr(colon + 1), &n)) {
126  in = in.substr(0, colon);
127  portOut = n;
128  }
129  }
130  if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') {
131  hostOut = in.substr(1, in.size() - 2);
132  } else {
133  hostOut = in;
134  }
135 }
136 
137 std::string EncodeBase64(Span<const uint8_t> input) {
138  static const char *pbase64 =
139  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
140 
141  std::string str;
142  str.reserve(((input.size() + 2) / 3) * 4);
143  ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(),
144  input.end());
145  while (str.size() % 4) {
146  str += '=';
147  }
148  return str;
149 }
150 
151 std::string EncodeBase64(const std::string &str) {
152  return EncodeBase64(MakeUCharSpan(str));
153 }
154 
155 std::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid) {
156  static const int8_t decode64_table[256] = {
157  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
158  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
159  -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
160  58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
161  7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
162  25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
163  37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
164  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
165  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
166  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
167  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
168  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
169  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
171  -1, -1, -1, -1};
172 
173  const char *e = p;
174  std::vector<uint8_t> val;
175  val.reserve(strlen(p));
176  while (*p != 0) {
177  int x = decode64_table[(uint8_t)*p];
178  if (x == -1) {
179  break;
180  }
181  val.push_back(uint8_t(x));
182  ++p;
183  }
184 
185  std::vector<uint8_t> ret;
186  ret.reserve((val.size() * 3) / 4);
187  bool valid = ConvertBits<6, 8, false>([&](uint8_t c) { ret.push_back(c); },
188  val.begin(), val.end());
189 
190  const char *q = p;
191  while (valid && *p != 0) {
192  if (*p != '=') {
193  valid = false;
194  break;
195  }
196  ++p;
197  }
198  valid = valid && (p - e) % 4 == 0 && p - q < 4;
199  if (pf_invalid) {
200  *pf_invalid = !valid;
201  }
202 
203  return ret;
204 }
205 
206 std::string DecodeBase64(const std::string &str, bool *pf_invalid) {
207  if (!ValidAsCString(str)) {
208  if (pf_invalid) {
209  *pf_invalid = true;
210  }
211  return {};
212  }
213  std::vector<uint8_t> vchRet = DecodeBase64(str.c_str(), pf_invalid);
214  return std::string((const char *)vchRet.data(), vchRet.size());
215 }
216 
217 std::string EncodeBase32(Span<const uint8_t> input, bool pad) {
218  static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
219 
220  std::string str;
221  str.reserve(((input.size() + 4) / 5) * 8);
222  ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(),
223  input.end());
224  if (pad) {
225  while (str.size() % 8) {
226  str += '=';
227  }
228  }
229  return str;
230 }
231 
232 std::string EncodeBase32(const std::string &str, bool pad) {
233  return EncodeBase32(MakeUCharSpan(str), pad);
234 }
235 
236 std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid) {
237  static const int8_t decode32_table[256] = {
238  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
239  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
240  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
241  30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
242  7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
243  25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
244  11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1,
245  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
246  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
247  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
248  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
249  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
250  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
251  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
252  -1, -1, -1, -1};
253 
254  const char *e = p;
255  std::vector<uint8_t> val;
256  val.reserve(strlen(p));
257  while (*p != 0) {
258  int x = decode32_table[(uint8_t)*p];
259  if (x == -1) {
260  break;
261  }
262  val.push_back(uint8_t(x));
263  ++p;
264  }
265 
266  std::vector<uint8_t> ret;
267  ret.reserve((val.size() * 5) / 8);
268  bool valid = ConvertBits<5, 8, false>([&](uint8_t c) { ret.push_back(c); },
269  val.begin(), val.end());
270 
271  const char *q = p;
272  while (valid && *p != 0) {
273  if (*p != '=') {
274  valid = false;
275  break;
276  }
277  ++p;
278  }
279  valid = valid && (p - e) % 8 == 0 && p - q < 8;
280  if (pf_invalid) {
281  *pf_invalid = !valid;
282  }
283 
284  return ret;
285 }
286 
287 std::string DecodeBase32(const std::string &str, bool *pf_invalid) {
288  if (!ValidAsCString(str)) {
289  if (pf_invalid) {
290  *pf_invalid = true;
291  }
292  return {};
293  }
294  std::vector<uint8_t> vchRet = DecodeBase32(str.c_str(), pf_invalid);
295  return std::string((const char *)vchRet.data(), vchRet.size());
296 }
297 
298 [[nodiscard]] static bool ParsePrechecks(const std::string &str) {
299  // No empty string allowed
300  if (str.empty()) {
301  return false;
302  }
303  // No padding allowed
304  if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size() - 1]))) {
305  return false;
306  }
307  // No embedded NUL characters allowed
308  if (!ValidAsCString(str)) {
309  return false;
310  }
311  return true;
312 }
313 
314 bool ParseInt32(const std::string &str, int32_t *out) {
315  if (!ParsePrechecks(str)) {
316  return false;
317  }
318  char *endp = nullptr;
319  // strtol will not set errno if valid
320  errno = 0;
321  long int n = strtol(str.c_str(), &endp, 10);
322  if (out) {
323  *out = (int32_t)n;
324  }
325  // Note that strtol returns a *long int*, so even if strtol doesn't report
326  // an over/underflow we still have to check that the returned value is
327  // within the range of an *int32_t*. On 64-bit platforms the size of these
328  // types may be different.
329  return endp && *endp == 0 && !errno &&
330  n >= std::numeric_limits<int32_t>::min() &&
331  n <= std::numeric_limits<int32_t>::max();
332 }
333 
334 bool ParseInt64(const std::string &str, int64_t *out) {
335  if (!ParsePrechecks(str)) {
336  return false;
337  }
338  char *endp = nullptr;
339  // strtoll will not set errno if valid
340  errno = 0;
341  long long int n = strtoll(str.c_str(), &endp, 10);
342  if (out) {
343  *out = (int64_t)n;
344  }
345  // Note that strtoll returns a *long long int*, so even if strtol doesn't
346  // report an over/underflow we still have to check that the returned value
347  // is within the range of an *int64_t*.
348  return endp && *endp == 0 && !errno &&
349  n >= std::numeric_limits<int64_t>::min() &&
350  n <= std::numeric_limits<int64_t>::max();
351 }
352 
353 bool ParseUInt8(const std::string &str, uint8_t *out) {
354  uint32_t u32;
355  if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
356  return false;
357  }
358  if (out != nullptr) {
359  *out = static_cast<uint8_t>(u32);
360  }
361  return true;
362 }
363 
364 bool ParseUInt16(const std::string &str, uint16_t *out) {
365  uint32_t u32;
366  if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) {
367  return false;
368  }
369  if (out != nullptr) {
370  *out = static_cast<uint16_t>(u32);
371  }
372  return true;
373 }
374 
375 bool ParseUInt32(const std::string &str, uint32_t *out) {
376  if (!ParsePrechecks(str)) {
377  return false;
378  }
379  // Reject negative values, unfortunately strtoul accepts these by default if
380  // they fit in the range
381  if (str.size() >= 1 && str[0] == '-') {
382  return false;
383  }
384  char *endp = nullptr;
385  // strtoul will not set errno if valid
386  errno = 0;
387  unsigned long int n = strtoul(str.c_str(), &endp, 10);
388  if (out) {
389  *out = (uint32_t)n;
390  }
391  // Note that strtoul returns a *unsigned long int*, so even if it doesn't
392  // report an over/underflow we still have to check that the returned value
393  // is within the range of an *uint32_t*. On 64-bit platforms the size of
394  // these types may be different.
395  return endp && *endp == 0 && !errno &&
396  n <= std::numeric_limits<uint32_t>::max();
397 }
398 
399 bool ParseUInt64(const std::string &str, uint64_t *out) {
400  if (!ParsePrechecks(str)) {
401  return false;
402  }
403  // Reject negative values, unfortunately strtoull accepts these by default
404  // if they fit in the range
405  if (str.size() >= 1 && str[0] == '-') {
406  return false;
407  }
408  char *endp = nullptr;
409  // strtoull will not set errno if valid
410  errno = 0;
411  unsigned long long int n = strtoull(str.c_str(), &endp, 10);
412  if (out) {
413  *out = (uint64_t)n;
414  }
415  // Note that strtoull returns a *unsigned long long int*, so even if it
416  // doesn't report an over/underflow we still have to check that the returned
417  // value is within the range of an *uint64_t*.
418  return endp && *endp == 0 && !errno &&
419  n <= std::numeric_limits<uint64_t>::max();
420 }
421 
422 bool ParseDouble(const std::string &str, double *out) {
423  if (!ParsePrechecks(str)) {
424  return false;
425  }
426  // No hexadecimal floats allowed
427  if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') {
428  return false;
429  }
430  std::istringstream text(str);
431  text.imbue(std::locale::classic());
432  double result;
433  text >> result;
434  if (out) {
435  *out = result;
436  }
437  return text.eof() && !text.fail();
438 }
439 
440 std::string FormatParagraph(const std::string &in, size_t width,
441  size_t indent) {
442  std::stringstream out;
443  size_t ptr = 0;
444  size_t indented = 0;
445  while (ptr < in.size()) {
446  size_t lineend = in.find_first_of('\n', ptr);
447  if (lineend == std::string::npos) {
448  lineend = in.size();
449  }
450  const size_t linelen = lineend - ptr;
451  const size_t rem_width = width - indented;
452  if (linelen <= rem_width) {
453  out << in.substr(ptr, linelen + 1);
454  ptr = lineend + 1;
455  indented = 0;
456  } else {
457  size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
458  if (finalspace == std::string::npos || finalspace < ptr) {
459  // No place to break; just include the entire word and move on
460  finalspace = in.find_first_of("\n ", ptr);
461  if (finalspace == std::string::npos) {
462  // End of the string, just add it and break
463  out << in.substr(ptr);
464  break;
465  }
466  }
467  out << in.substr(ptr, finalspace - ptr) << "\n";
468  if (in[finalspace] == '\n') {
469  indented = 0;
470  } else if (indent) {
471  out << std::string(indent, ' ');
472  indented = indent;
473  }
474  ptr = finalspace + 1;
475  }
476  }
477  return out.str();
478 }
479 
480 int64_t atoi64(const std::string &str) {
481 #ifdef _MSC_VER
482  return _atoi64(str.c_str());
483 #else
484  return strtoll(str.c_str(), nullptr, 10);
485 #endif
486 }
487 
488 int atoi(const std::string &str) {
489  return atoi(str.c_str());
490 }
491 
501 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
502 
504 static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa,
505  int &mantissa_tzeros) {
506  if (ch == '0') {
507  ++mantissa_tzeros;
508  } else {
509  for (int i = 0; i <= mantissa_tzeros; ++i) {
510  // overflow
511  if (mantissa > (UPPER_BOUND / 10LL)) {
512  return false;
513  }
514  mantissa *= 10;
515  }
516  mantissa += ch - '0';
517  mantissa_tzeros = 0;
518  }
519  return true;
520 }
521 
522 bool ParseFixedPoint(const std::string &val, int decimals,
523  int64_t *amount_out) {
524  int64_t mantissa = 0;
525  int64_t exponent = 0;
526  int mantissa_tzeros = 0;
527  bool mantissa_sign = false;
528  bool exponent_sign = false;
529  int ptr = 0;
530  int end = val.size();
531  int point_ofs = 0;
532 
533  if (ptr < end && val[ptr] == '-') {
534  mantissa_sign = true;
535  ++ptr;
536  }
537  if (ptr < end) {
538  if (val[ptr] == '0') {
539  // pass single 0
540  ++ptr;
541  } else if (val[ptr] >= '1' && val[ptr] <= '9') {
542  while (ptr < end && IsDigit(val[ptr])) {
543  if (!ProcessMantissaDigit(val[ptr], mantissa,
544  mantissa_tzeros)) {
545  // overflow
546  return false;
547  }
548  ++ptr;
549  }
550  } else {
551  // missing expected digit
552  return false;
553  }
554  } else {
555  // empty string or loose '-'
556  return false;
557  }
558  if (ptr < end && val[ptr] == '.') {
559  ++ptr;
560  if (ptr < end && IsDigit(val[ptr])) {
561  while (ptr < end && IsDigit(val[ptr])) {
562  if (!ProcessMantissaDigit(val[ptr], mantissa,
563  mantissa_tzeros)) {
564  // overflow
565  return false;
566  }
567  ++ptr;
568  ++point_ofs;
569  }
570  } else {
571  // missing expected digit
572  return false;
573  }
574  }
575  if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E')) {
576  ++ptr;
577  if (ptr < end && val[ptr] == '+') {
578  ++ptr;
579  } else if (ptr < end && val[ptr] == '-') {
580  exponent_sign = true;
581  ++ptr;
582  }
583  if (ptr < end && IsDigit(val[ptr])) {
584  while (ptr < end && IsDigit(val[ptr])) {
585  if (exponent > (UPPER_BOUND / 10LL)) {
586  // overflow
587  return false;
588  }
589  exponent = exponent * 10 + val[ptr] - '0';
590  ++ptr;
591  }
592  } else {
593  // missing expected digit
594  return false;
595  }
596  }
597  if (ptr != end) {
598  // trailing garbage
599  return false;
600  }
601  // finalize exponent
602  if (exponent_sign) {
603  exponent = -exponent;
604  }
605  exponent = exponent - point_ofs + mantissa_tzeros;
606 
607  // finalize mantissa
608  if (mantissa_sign) {
609  mantissa = -mantissa;
610  }
611 
612  // convert to one 64-bit fixed-point value
613  exponent += decimals;
614  if (exponent < 0) {
615  // cannot represent values smaller than 10^-decimals
616  return false;
617  }
618  if (exponent >= 18) {
619  // cannot represent values larger than or equal to 10^(18-decimals)
620  return false;
621  }
622 
623  for (int i = 0; i < exponent; ++i) {
624  if (mantissa > (UPPER_BOUND / 10LL) ||
625  mantissa < -(UPPER_BOUND / 10LL)) {
626  // overflow
627  return false;
628  }
629  mantissa *= 10;
630  }
631  if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND) {
632  // overflow
633  return false;
634  }
635 
636  if (amount_out) {
637  *amount_out = mantissa;
638  }
639 
640  return true;
641 }
642 
643 std::string ToLower(const std::string &str) {
644  std::string r;
645  for (auto ch : str) {
646  r += ToLower(ch);
647  }
648  return r;
649 }
650 
651 std::string ToUpper(const std::string &str) {
652  std::string r;
653  for (auto ch : str) {
654  r += ToUpper(ch);
655  }
656  return r;
657 }
658 
659 std::string Capitalize(std::string str) {
660  if (str.empty()) {
661  return str;
662  }
663  str[0] = ToUpper(str.front());
664  return str;
665 }
666 
667 std::string HexStr(const Span<const uint8_t> s) {
668  std::string rv(s.size() * 2, '\0');
669  static constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
670  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
671  auto it = rv.begin();
672  for (uint8_t v : s) {
673  *it++ = hexmap[v >> 4];
674  *it++ = hexmap[v & 15];
675  }
676  assert(it == rv.end());
677  return rv;
678 }
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
constexpr std::size_t size() const noexcept
Definition: span.h:209
constexpr C * end() const noexcept
Definition: span.h:200
constexpr C * begin() const noexcept
Definition: span.h:199
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
Definition: span.h:307
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
static const std::string SAFE_CHARS[]
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string EncodeBase64(Span< const uint8_t > input)
std::string FormatParagraph(const std::string &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 bool ParsePrechecks(const std::string &str)
bool ParseUInt16(const std::string &str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
const signed char p_util_hexdigit[256]
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::string EncodeBase32(Span< const uint8_t > input, bool pad)
Base32 encode.
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool IsHex(const std::string &str)
Returns true if each character in str is a hex character, and has an even number of hex digits.
std::vector< uint8_t > DecodeBase64(const char *p, bool *pf_invalid)
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
signed char HexDigit(char c)
int atoi(const std::string &str)
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
static const int64_t UPPER_BOUND
Upper bound for mantissa.
int64_t atoi64(const std::string &str)
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
static const std::string CHARS_ALPHA_NUM
std::vector< uint8_t > ParseHex(const char *psz)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:83
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:99
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:80
assert(!tx.IsCoinBase())