Bitcoin ABC  0.29.2
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::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid) {
152  static const int8_t decode64_table[256] = {
153  -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, -1, -1,
155  -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
156  58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
157  7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
158  25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
159  37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
160  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
161  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
162  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
163  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -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};
168 
169  const char *e = p;
170  std::vector<uint8_t> val;
171  val.reserve(strlen(p));
172  while (*p != 0) {
173  int x = decode64_table[(uint8_t)*p];
174  if (x == -1) {
175  break;
176  }
177  val.push_back(uint8_t(x));
178  ++p;
179  }
180 
181  std::vector<uint8_t> ret;
182  ret.reserve((val.size() * 3) / 4);
183  bool valid = ConvertBits<6, 8, false>([&](uint8_t c) { ret.push_back(c); },
184  val.begin(), val.end());
185 
186  const char *q = p;
187  while (valid && *p != 0) {
188  if (*p != '=') {
189  valid = false;
190  break;
191  }
192  ++p;
193  }
194  valid = valid && (p - e) % 4 == 0 && p - q < 4;
195  if (pf_invalid) {
196  *pf_invalid = !valid;
197  }
198 
199  return ret;
200 }
201 
202 std::string DecodeBase64(const std::string &str, bool *pf_invalid) {
203  if (!ValidAsCString(str)) {
204  if (pf_invalid) {
205  *pf_invalid = true;
206  }
207  return {};
208  }
209  std::vector<uint8_t> vchRet = DecodeBase64(str.c_str(), pf_invalid);
210  return std::string((const char *)vchRet.data(), vchRet.size());
211 }
212 
213 std::string EncodeBase32(Span<const uint8_t> input, bool pad) {
214  static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
215 
216  std::string str;
217  str.reserve(((input.size() + 4) / 5) * 8);
218  ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(),
219  input.end());
220  if (pad) {
221  while (str.size() % 8) {
222  str += '=';
223  }
224  }
225  return str;
226 }
227 
228 std::string EncodeBase32(const std::string &str, bool pad) {
229  return EncodeBase32(MakeUCharSpan(str), pad);
230 }
231 
232 std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid) {
233  static const int8_t decode32_table[256] = {
234  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
235  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
236  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
237  30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
238  7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
239  25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
240  11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1,
241  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
242  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
243  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
244  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -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};
249 
250  const char *e = p;
251  std::vector<uint8_t> val;
252  val.reserve(strlen(p));
253  while (*p != 0) {
254  int x = decode32_table[(uint8_t)*p];
255  if (x == -1) {
256  break;
257  }
258  val.push_back(uint8_t(x));
259  ++p;
260  }
261 
262  std::vector<uint8_t> ret;
263  ret.reserve((val.size() * 5) / 8);
264  bool valid = ConvertBits<5, 8, false>([&](uint8_t c) { ret.push_back(c); },
265  val.begin(), val.end());
266 
267  const char *q = p;
268  while (valid && *p != 0) {
269  if (*p != '=') {
270  valid = false;
271  break;
272  }
273  ++p;
274  }
275  valid = valid && (p - e) % 8 == 0 && p - q < 8;
276  if (pf_invalid) {
277  *pf_invalid = !valid;
278  }
279 
280  return ret;
281 }
282 
283 std::string DecodeBase32(const std::string &str, bool *pf_invalid) {
284  if (!ValidAsCString(str)) {
285  if (pf_invalid) {
286  *pf_invalid = true;
287  }
288  return {};
289  }
290  std::vector<uint8_t> vchRet = DecodeBase32(str.c_str(), pf_invalid);
291  return std::string((const char *)vchRet.data(), vchRet.size());
292 }
293 
294 [[nodiscard]] static bool ParsePrechecks(const std::string &str) {
295  // No empty string allowed
296  if (str.empty()) {
297  return false;
298  }
299  // No padding allowed
300  if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size() - 1]))) {
301  return false;
302  }
303  // No embedded NUL characters allowed
304  if (!ValidAsCString(str)) {
305  return false;
306  }
307  return true;
308 }
309 
310 bool ParseInt32(const std::string &str, int32_t *out) {
311  if (!ParsePrechecks(str)) {
312  return false;
313  }
314  char *endp = nullptr;
315  // strtol will not set errno if valid
316  errno = 0;
317  long int n = strtol(str.c_str(), &endp, 10);
318  if (out) {
319  *out = (int32_t)n;
320  }
321  // Note that strtol returns a *long int*, so even if strtol doesn't report
322  // an over/underflow we still have to check that the returned value is
323  // within the range of an *int32_t*. On 64-bit platforms the size of these
324  // types may be different.
325  return endp && *endp == 0 && !errno &&
326  n >= std::numeric_limits<int32_t>::min() &&
327  n <= std::numeric_limits<int32_t>::max();
328 }
329 
330 bool ParseInt64(const std::string &str, int64_t *out) {
331  if (!ParsePrechecks(str)) {
332  return false;
333  }
334  char *endp = nullptr;
335  // strtoll will not set errno if valid
336  errno = 0;
337  long long int n = strtoll(str.c_str(), &endp, 10);
338  if (out) {
339  *out = (int64_t)n;
340  }
341  // Note that strtoll returns a *long long int*, so even if strtol doesn't
342  // report an over/underflow we still have to check that the returned value
343  // is within the range of an *int64_t*.
344  return endp && *endp == 0 && !errno &&
345  n >= std::numeric_limits<int64_t>::min() &&
346  n <= std::numeric_limits<int64_t>::max();
347 }
348 
349 bool ParseUInt8(const std::string &str, uint8_t *out) {
350  uint32_t u32;
351  if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
352  return false;
353  }
354  if (out != nullptr) {
355  *out = static_cast<uint8_t>(u32);
356  }
357  return true;
358 }
359 
360 bool ParseUInt16(const std::string &str, uint16_t *out) {
361  uint32_t u32;
362  if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) {
363  return false;
364  }
365  if (out != nullptr) {
366  *out = static_cast<uint16_t>(u32);
367  }
368  return true;
369 }
370 
371 bool ParseUInt32(const std::string &str, uint32_t *out) {
372  if (!ParsePrechecks(str)) {
373  return false;
374  }
375  // Reject negative values, unfortunately strtoul accepts these by default if
376  // they fit in the range
377  if (str.size() >= 1 && str[0] == '-') {
378  return false;
379  }
380  char *endp = nullptr;
381  // strtoul will not set errno if valid
382  errno = 0;
383  unsigned long int n = strtoul(str.c_str(), &endp, 10);
384  if (out) {
385  *out = (uint32_t)n;
386  }
387  // Note that strtoul returns a *unsigned long int*, so even if it doesn't
388  // report an over/underflow we still have to check that the returned value
389  // is within the range of an *uint32_t*. On 64-bit platforms the size of
390  // these types may be different.
391  return endp && *endp == 0 && !errno &&
392  n <= std::numeric_limits<uint32_t>::max();
393 }
394 
395 bool ParseUInt64(const std::string &str, uint64_t *out) {
396  if (!ParsePrechecks(str)) {
397  return false;
398  }
399  // Reject negative values, unfortunately strtoull accepts these by default
400  // if they fit in the range
401  if (str.size() >= 1 && str[0] == '-') {
402  return false;
403  }
404  char *endp = nullptr;
405  // strtoull will not set errno if valid
406  errno = 0;
407  unsigned long long int n = strtoull(str.c_str(), &endp, 10);
408  if (out) {
409  *out = (uint64_t)n;
410  }
411  // Note that strtoull returns a *unsigned long long int*, so even if it
412  // doesn't report an over/underflow we still have to check that the returned
413  // value is within the range of an *uint64_t*.
414  return endp && *endp == 0 && !errno &&
415  n <= std::numeric_limits<uint64_t>::max();
416 }
417 
418 bool ParseDouble(const std::string &str, double *out) {
419  if (!ParsePrechecks(str)) {
420  return false;
421  }
422  // No hexadecimal floats allowed
423  if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') {
424  return false;
425  }
426  std::istringstream text(str);
427  text.imbue(std::locale::classic());
428  double result;
429  text >> result;
430  if (out) {
431  *out = result;
432  }
433  return text.eof() && !text.fail();
434 }
435 
436 std::string FormatParagraph(const std::string &in, size_t width,
437  size_t indent) {
438  std::stringstream out;
439  size_t ptr = 0;
440  size_t indented = 0;
441  while (ptr < in.size()) {
442  size_t lineend = in.find_first_of('\n', ptr);
443  if (lineend == std::string::npos) {
444  lineend = in.size();
445  }
446  const size_t linelen = lineend - ptr;
447  const size_t rem_width = width - indented;
448  if (linelen <= rem_width) {
449  out << in.substr(ptr, linelen + 1);
450  ptr = lineend + 1;
451  indented = 0;
452  } else {
453  size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
454  if (finalspace == std::string::npos || finalspace < ptr) {
455  // No place to break; just include the entire word and move on
456  finalspace = in.find_first_of("\n ", ptr);
457  if (finalspace == std::string::npos) {
458  // End of the string, just add it and break
459  out << in.substr(ptr);
460  break;
461  }
462  }
463  out << in.substr(ptr, finalspace - ptr) << "\n";
464  if (in[finalspace] == '\n') {
465  indented = 0;
466  } else if (indent) {
467  out << std::string(indent, ' ');
468  indented = indent;
469  }
470  ptr = finalspace + 1;
471  }
472  }
473  return out.str();
474 }
475 
476 int64_t atoi64(const std::string &str) {
477 #ifdef _MSC_VER
478  return _atoi64(str.c_str());
479 #else
480  return strtoll(str.c_str(), nullptr, 10);
481 #endif
482 }
483 
484 int atoi(const std::string &str) {
485  return atoi(str.c_str());
486 }
487 
497 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
498 
500 static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa,
501  int &mantissa_tzeros) {
502  if (ch == '0') {
503  ++mantissa_tzeros;
504  } else {
505  for (int i = 0; i <= mantissa_tzeros; ++i) {
506  // overflow
507  if (mantissa > (UPPER_BOUND / 10LL)) {
508  return false;
509  }
510  mantissa *= 10;
511  }
512  mantissa += ch - '0';
513  mantissa_tzeros = 0;
514  }
515  return true;
516 }
517 
518 bool ParseFixedPoint(const std::string &val, int decimals,
519  int64_t *amount_out) {
520  int64_t mantissa = 0;
521  int64_t exponent = 0;
522  int mantissa_tzeros = 0;
523  bool mantissa_sign = false;
524  bool exponent_sign = false;
525  int ptr = 0;
526  int end = val.size();
527  int point_ofs = 0;
528 
529  if (ptr < end && val[ptr] == '-') {
530  mantissa_sign = true;
531  ++ptr;
532  }
533  if (ptr < end) {
534  if (val[ptr] == '0') {
535  // pass single 0
536  ++ptr;
537  } else if (val[ptr] >= '1' && val[ptr] <= '9') {
538  while (ptr < end && IsDigit(val[ptr])) {
539  if (!ProcessMantissaDigit(val[ptr], mantissa,
540  mantissa_tzeros)) {
541  // overflow
542  return false;
543  }
544  ++ptr;
545  }
546  } else {
547  // missing expected digit
548  return false;
549  }
550  } else {
551  // empty string or loose '-'
552  return false;
553  }
554  if (ptr < end && val[ptr] == '.') {
555  ++ptr;
556  if (ptr < end && IsDigit(val[ptr])) {
557  while (ptr < end && IsDigit(val[ptr])) {
558  if (!ProcessMantissaDigit(val[ptr], mantissa,
559  mantissa_tzeros)) {
560  // overflow
561  return false;
562  }
563  ++ptr;
564  ++point_ofs;
565  }
566  } else {
567  // missing expected digit
568  return false;
569  }
570  }
571  if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E')) {
572  ++ptr;
573  if (ptr < end && val[ptr] == '+') {
574  ++ptr;
575  } else if (ptr < end && val[ptr] == '-') {
576  exponent_sign = true;
577  ++ptr;
578  }
579  if (ptr < end && IsDigit(val[ptr])) {
580  while (ptr < end && IsDigit(val[ptr])) {
581  if (exponent > (UPPER_BOUND / 10LL)) {
582  // overflow
583  return false;
584  }
585  exponent = exponent * 10 + val[ptr] - '0';
586  ++ptr;
587  }
588  } else {
589  // missing expected digit
590  return false;
591  }
592  }
593  if (ptr != end) {
594  // trailing garbage
595  return false;
596  }
597  // finalize exponent
598  if (exponent_sign) {
599  exponent = -exponent;
600  }
601  exponent = exponent - point_ofs + mantissa_tzeros;
602 
603  // finalize mantissa
604  if (mantissa_sign) {
605  mantissa = -mantissa;
606  }
607 
608  // convert to one 64-bit fixed-point value
609  exponent += decimals;
610  if (exponent < 0) {
611  // cannot represent values smaller than 10^-decimals
612  return false;
613  }
614  if (exponent >= 18) {
615  // cannot represent values larger than or equal to 10^(18-decimals)
616  return false;
617  }
618 
619  for (int i = 0; i < exponent; ++i) {
620  if (mantissa > (UPPER_BOUND / 10LL) ||
621  mantissa < -(UPPER_BOUND / 10LL)) {
622  // overflow
623  return false;
624  }
625  mantissa *= 10;
626  }
627  if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND) {
628  // overflow
629  return false;
630  }
631 
632  if (amount_out) {
633  *amount_out = mantissa;
634  }
635 
636  return true;
637 }
638 
639 std::string ToLower(const std::string &str) {
640  std::string r;
641  for (auto ch : str) {
642  r += ToLower(ch);
643  }
644  return r;
645 }
646 
647 std::string ToUpper(const std::string &str) {
648  std::string r;
649  for (auto ch : str) {
650  r += ToUpper(ch);
651  }
652  return r;
653 }
654 
655 std::string Capitalize(std::string str) {
656  if (str.empty()) {
657  return str;
658  }
659  str[0] = ToUpper(str.front());
660  return str;
661 }
662 
663 std::string HexStr(const Span<const uint8_t> s) {
664  std::string rv(s.size() * 2, '\0');
665  static constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
666  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
667  auto it = rv.begin();
668  for (uint8_t v : s) {
669  *it++ = hexmap[v >> 4];
670  *it++ = hexmap[v & 15];
671  }
672  assert(it == rv.end());
673  return rv;
674 }
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:337
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:88
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:104
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())