15 static const std::string
CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
26 std::string strResult;
27 for (std::string::size_type i = 0; i < str.size(); i++)
29 if (
SAFE_CHARS[rule].find(str[i]) != std::string::npos)
30 strResult.push_back(str[i]);
36 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
37 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
38 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
39 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
40 -1,0xa,0xb,0xc,0xd,0xe,0xf,-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,0xa,0xb,0xc,0xd,0xe,0xf,-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 -1,-1,-1,-1,-1,-1,-1,-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, };
58 bool IsHex(
const std::string& str)
60 for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
65 return (str.size() > 0) && (str.size()%2 == 0);
70 size_t starting_location = 0;
71 if (str.size() > 2 && *str.begin() ==
'0' && *(str.begin()+1) ==
'x') {
72 starting_location = 2;
74 for (
auto c : str.substr(starting_location)) {
78 return (str.size() > starting_location);
81 std::vector<unsigned char>
ParseHex(
const char* psz)
84 std::vector<unsigned char> vch;
90 if (c == (
signed char)-1)
92 unsigned char n = (c << 4);
94 if (c == (
signed char)-1)
102 std::vector<unsigned char>
ParseHex(
const std::string& str)
108 size_t colon = in.find_last_of(
':');
110 bool fHaveColon = colon != in.npos;
111 bool fBracketed = fHaveColon && (in[0]==
'[' && in[colon-1]==
']');
112 bool fMultiColon = fHaveColon && (in.find_last_of(
':',colon-1) != in.npos);
113 if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
115 if (
ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
116 in = in.substr(0, colon);
120 if (in.size()>0 && in[0] ==
'[' && in[in.size()-1] ==
']')
121 hostOut = in.substr(1, in.size()-2);
128 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
130 std::string strRet =
"";
131 strRet.reserve((len+2)/3*4);
134 const unsigned char *pchEnd = pch+len;
142 strRet += pbase64[enc >> 2];
143 left = (enc & 3) << 4;
148 strRet += pbase64[left | (enc >> 4)];
149 left = (enc & 15) << 2;
154 strRet += pbase64[left | (enc >> 6)];
155 strRet += pbase64[enc & 63];
163 strRet += pbase64[left];
174 return EncodeBase64((
const unsigned char*)str.c_str(), str.size());
177 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid)
179 static const int decode64_table[256] =
181 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
182 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
183 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
184 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
185 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
186 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
187 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
188 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
189 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
190 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
191 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
192 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
193 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
199 std::vector<unsigned char> vchRet;
200 vchRet.reserve(strlen(p)*3/4);
207 int dec = decode64_table[(
unsigned char)*p];
208 if (dec == -1)
break;
218 vchRet.push_back((left<<2) | (dec>>4));
224 vchRet.push_back((left<<4) | (dec>>2));
230 vchRet.push_back((left<<6) | dec);
247 if (left || p[0] !=
'=' || p[1] !=
'=' || decode64_table[(
unsigned char)p[2]] != -1)
252 if (left || p[0] !=
'=' || decode64_table[(
unsigned char)p[1]] != -1)
262 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str());
263 return std::string((
const char*)vchRet.data(), vchRet.size());
268 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
270 std::string strRet=
"";
271 strRet.reserve((len+4)/5*8);
274 const unsigned char *pchEnd = pch+len;
282 strRet += pbase32[enc >> 3];
283 left = (enc & 7) << 2;
288 strRet += pbase32[left | (enc >> 6)];
289 strRet += pbase32[(enc >> 1) & 31];
290 left = (enc & 1) << 4;
295 strRet += pbase32[left | (enc >> 4)];
296 left = (enc & 15) << 1;
301 strRet += pbase32[left | (enc >> 7)];
302 strRet += pbase32[(enc >> 2) & 31];
303 left = (enc & 3) << 3;
308 strRet += pbase32[left | (enc >> 5)];
309 strRet += pbase32[enc & 31];
314 static const int nPadding[5] = {0, 6, 4, 3, 1};
317 strRet += pbase32[left];
318 for (
int n=0; n<nPadding[mode]; n++)
327 return EncodeBase32((
const unsigned char*)str.c_str(), str.size());
330 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid)
332 static const int decode32_table[256] =
334 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
335 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
336 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
337 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
338 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
339 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
340 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
341 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
342 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
343 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
344 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
345 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
346 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
352 std::vector<unsigned char> vchRet;
353 vchRet.reserve((strlen(p))*5/8);
360 int dec = decode32_table[(
unsigned char)*p];
361 if (dec == -1)
break;
371 vchRet.push_back((left<<3) | (dec>>2));
377 left = left << 5 | dec;
382 vchRet.push_back((left<<1) | (dec>>4));
388 vchRet.push_back((left<<4) | (dec>>1));
394 left = left << 5 | dec;
399 vchRet.push_back((left<<2) | (dec>>3));
405 vchRet.push_back((left<<5) | dec);
424 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || p[4] !=
'=' || p[5] !=
'=' || decode32_table[(
unsigned char)p[6]] != -1)
429 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || decode32_table[(
unsigned char)p[4]] != -1)
434 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || decode32_table[(
unsigned char)p[3]] != -1)
439 if (left || p[0] !=
'=' || decode32_table[(
unsigned char)p[1]] != -1)
449 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str());
450 return std::string((
const char*)vchRet.data(), vchRet.size());
457 if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1])))
459 if (str.size() != strlen(str.c_str()))
468 char *endp =
nullptr;
470 long int n = strtol(str.c_str(), &endp, 10);
471 if(out) *out = (int32_t)n;
475 return endp && *endp == 0 && !errno &&
476 n >= std::numeric_limits<int32_t>::min() &&
477 n <= std::numeric_limits<int32_t>::max();
484 char *endp =
nullptr;
486 long long int n = strtoll(str.c_str(), &endp, 10);
487 if(out) *out = (int64_t)n;
490 return endp && *endp == 0 && !errno &&
491 n >= std::numeric_limits<int64_t>::min() &&
492 n <= std::numeric_limits<int64_t>::max();
499 if (str.size() >= 1 && str[0] ==
'-')
501 char *endp =
nullptr;
503 unsigned long int n = strtoul(str.c_str(), &endp, 10);
504 if(out) *out = (uint32_t)n;
508 return endp && *endp == 0 && !errno &&
509 n <= std::numeric_limits<uint32_t>::max();
516 if (str.size() >= 1 && str[0] ==
'-')
518 char *endp =
nullptr;
520 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
521 if(out) *out = (uint64_t)n;
524 return endp && *endp == 0 && !errno &&
525 n <= std::numeric_limits<uint64_t>::max();
533 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
535 std::istringstream text(str);
536 text.imbue(std::locale::classic());
539 if(out) *out = result;
540 return text.eof() && !text.fail();
545 std::stringstream out;
548 while (ptr < in.size())
550 size_t lineend = in.find_first_of(
'\n', ptr);
551 if (lineend == std::string::npos) {
554 const size_t linelen = lineend - ptr;
555 const size_t rem_width = width - indented;
556 if (linelen <= rem_width) {
557 out << in.substr(ptr, linelen + 1);
561 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
562 if (finalspace == std::string::npos || finalspace < ptr) {
564 finalspace = in.find_first_of(
"\n ", ptr);
565 if (finalspace == std::string::npos) {
567 out << in.substr(ptr);
571 out << in.substr(ptr, finalspace - ptr) <<
"\n";
572 if (in[finalspace] ==
'\n') {
575 out << std::string(indent,
' ');
578 ptr = finalspace + 1;
599 return strtoll(psz,
nullptr, 10);
606 return _atoi64(str.c_str());
608 return strtoll(str.c_str(),
nullptr, 10);
612 int atoi(
const std::string& str)
614 return atoi(str.c_str());
633 for (
int i=0; i<=mantissa_tzeros; ++i) {
638 mantissa += ch -
'0';
646 int64_t mantissa = 0;
647 int64_t exponent = 0;
648 int mantissa_tzeros = 0;
649 bool mantissa_sign =
false;
650 bool exponent_sign =
false;
652 int end = val.size();
655 if (ptr < end && val[ptr] ==
'-') {
656 mantissa_sign =
true;
661 if (val[ptr] ==
'0') {
664 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
665 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
672 if (ptr < end && val[ptr] ==
'.')
675 if (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9')
677 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
685 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
688 if (ptr < end && val[ptr] ==
'+')
690 else if (ptr < end && val[ptr] ==
'-') {
691 exponent_sign =
true;
694 if (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
695 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
698 exponent = exponent * 10 + val[ptr] -
'0';
708 exponent = -exponent;
709 exponent = exponent - point_ofs + mantissa_tzeros;
713 mantissa = -mantissa;
716 exponent += decimals;
722 for (
int i=0; i < exponent; ++i) {
731 *amount_out = mantissa;
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
const signed char p_util_hexdigit[256]
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid)
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)
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)
static const std::string SAFE_CHARS[]
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
static const std::string CHARS_ALPHA_NUM
int64_t atoi64(const char *psz)
std::string i64tostr(int64_t n)
std::string EncodeBase32(const unsigned char *pch, size_t len)
signed char HexDigit(char c)
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
static const int64_t UPPER_BOUND
Upper bound for mantissa.
std::string itostr(int n)
int atoi(const std::string &str)
std::string EncodeBase64(const unsigned char *pch, size_t len)
std::vector< unsigned char > ParseHex(const char *psz)