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