16 static const std::string CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
18 static const std::string SAFE_CHARS[] =
20 CHARS_ALPHA_NUM +
" .,;-_/:?@()",
21 CHARS_ALPHA_NUM +
" .,;-_?@",
22 CHARS_ALPHA_NUM +
".-_",
27 std::string strResult;
28 for (std::string::size_type i = 0; i < str.size(); i++)
30 if (SAFE_CHARS[rule].find(str[i]) != std::string::npos)
31 strResult.push_back(str[i]);
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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
40 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
41 -1,0xa,0xb,0xc,0xd,0xe,0xf,-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 -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,-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,
52 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
59 bool IsHex(
const std::string& str)
61 for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
66 return (str.size() > 0) && (str.size()%2 == 0);
71 size_t starting_location = 0;
72 if (str.size() > 2 && *str.begin() ==
'0' && *(str.begin()+1) ==
'x') {
73 starting_location = 2;
75 for (
auto c : str.substr(starting_location)) {
79 return (str.size() > starting_location);
82 std::vector<unsigned char>
ParseHex(
const char* psz)
85 std::vector<unsigned char> vch;
91 if (c == (
signed char)-1)
93 unsigned char n = (c << 4);
95 if (c == (
signed char)-1)
103 std::vector<unsigned char>
ParseHex(
const std::string& str)
109 size_t colon = in.find_last_of(
':');
111 bool fHaveColon = colon != in.npos;
112 bool fBracketed = fHaveColon && (in[0]==
'[' && in[colon-1]==
']');
113 bool fMultiColon = fHaveColon && (in.find_last_of(
':',colon-1) != in.npos);
114 if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
116 if (
ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
117 in = in.substr(0, colon);
121 if (in.size()>0 && in[0] ==
'[' && in[in.size()-1] ==
']')
122 hostOut = in.substr(1, in.size()-2);
129 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
131 std::string strRet =
"";
132 strRet.reserve((len+2)/3*4);
135 const unsigned char *pchEnd = pch+len;
143 strRet += pbase64[enc >> 2];
144 left = (enc & 3) << 4;
149 strRet += pbase64[left | (enc >> 4)];
150 left = (enc & 15) << 2;
155 strRet += pbase64[left | (enc >> 6)];
156 strRet += pbase64[enc & 63];
164 strRet += pbase64[left];
175 return EncodeBase64((
const unsigned char*)str.c_str(), str.size());
178 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid)
180 static const int decode64_table[256] =
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
184 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
185 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
186 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
187 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
188 49, 50, 51, -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, -1, -1, -1, -1,
194 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
200 std::vector<unsigned char> vchRet;
201 vchRet.reserve(strlen(p)*3/4);
208 int dec = decode64_table[(
unsigned char)*p];
209 if (dec == -1)
break;
219 vchRet.push_back((left<<2) | (dec>>4));
225 vchRet.push_back((left<<4) | (dec>>2));
231 vchRet.push_back((left<<6) | dec);
248 if (left || p[0] !=
'=' || p[1] !=
'=' || decode64_table[(
unsigned char)p[2]] != -1)
253 if (left || p[0] !=
'=' || decode64_table[(
unsigned char)p[1]] != -1)
263 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str());
264 return std::string((
const char*)vchRet.data(), vchRet.size());
269 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
271 std::string strRet=
"";
272 strRet.reserve((len+4)/5*8);
275 const unsigned char *pchEnd = pch+len;
283 strRet += pbase32[enc >> 3];
284 left = (enc & 7) << 2;
289 strRet += pbase32[left | (enc >> 6)];
290 strRet += pbase32[(enc >> 1) & 31];
291 left = (enc & 1) << 4;
296 strRet += pbase32[left | (enc >> 4)];
297 left = (enc & 15) << 1;
302 strRet += pbase32[left | (enc >> 7)];
303 strRet += pbase32[(enc >> 2) & 31];
304 left = (enc & 3) << 3;
309 strRet += pbase32[left | (enc >> 5)];
310 strRet += pbase32[enc & 31];
315 static const int nPadding[5] = {0, 6, 4, 3, 1};
318 strRet += pbase32[left];
319 for (
int n=0; n<nPadding[mode]; n++)
328 return EncodeBase32((
const unsigned char*)str.c_str(), str.size());
331 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid)
333 static const int decode32_table[256] =
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
337 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
338 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
339 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
340 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
341 23, 24, 25, -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, -1, -1, -1, -1,
347 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
353 std::vector<unsigned char> vchRet;
354 vchRet.reserve((strlen(p))*5/8);
361 int dec = decode32_table[(
unsigned char)*p];
362 if (dec == -1)
break;
372 vchRet.push_back((left<<3) | (dec>>2));
378 left = left << 5 | dec;
383 vchRet.push_back((left<<1) | (dec>>4));
389 vchRet.push_back((left<<4) | (dec>>1));
395 left = left << 5 | dec;
400 vchRet.push_back((left<<2) | (dec>>3));
406 vchRet.push_back((left<<5) | dec);
425 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || p[4] !=
'=' || p[5] !=
'=' || decode32_table[(
unsigned char)p[6]] != -1)
430 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || decode32_table[(
unsigned char)p[4]] != -1)
435 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || decode32_table[(
unsigned char)p[3]] != -1)
440 if (left || p[0] !=
'=' || decode32_table[(
unsigned char)p[1]] != -1)
450 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str());
451 return std::string((
const char*)vchRet.data(), vchRet.size());
454 static bool ParsePrechecks(
const std::string& str)
458 if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1])))
460 if (str.size() != strlen(str.c_str()))
467 if (!ParsePrechecks(str))
469 char *endp =
nullptr;
471 long int n = strtol(str.c_str(), &endp, 10);
472 if(out) *out = (int32_t)n;
476 return endp && *endp == 0 && !errno &&
477 n >= std::numeric_limits<int32_t>::min() &&
478 n <= std::numeric_limits<int32_t>::max();
483 if (!ParsePrechecks(str))
485 char *endp =
nullptr;
487 long long int n = strtoll(str.c_str(), &endp, 10);
488 if(out) *out = (int64_t)n;
491 return endp && *endp == 0 && !errno &&
492 n >= std::numeric_limits<int64_t>::min() &&
493 n <= std::numeric_limits<int64_t>::max();
498 if (!ParsePrechecks(str))
500 if (str.size() >= 1 && str[0] ==
'-')
502 char *endp =
nullptr;
504 unsigned long int n = strtoul(str.c_str(), &endp, 10);
505 if(out) *out = (uint32_t)n;
509 return endp && *endp == 0 && !errno &&
510 n <= std::numeric_limits<uint32_t>::max();
515 if (!ParsePrechecks(str))
517 if (str.size() >= 1 && str[0] ==
'-')
519 char *endp =
nullptr;
521 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
522 if(out) *out = (uint64_t)n;
525 return endp && *endp == 0 && !errno &&
526 n <= std::numeric_limits<uint64_t>::max();
532 if (!ParsePrechecks(str))
534 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
536 std::istringstream text(str);
537 text.imbue(std::locale::classic());
540 if(out) *out = result;
541 return text.eof() && !text.fail();
546 std::stringstream out;
549 while (ptr < in.size())
551 size_t lineend = in.find_first_of(
'\n', ptr);
552 if (lineend == std::string::npos) {
555 const size_t linelen = lineend - ptr;
556 const size_t rem_width = width - indented;
557 if (linelen <= rem_width) {
558 out << in.substr(ptr, linelen + 1);
562 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
563 if (finalspace == std::string::npos || finalspace < ptr) {
565 finalspace = in.find_first_of(
"\n ", ptr);
566 if (finalspace == std::string::npos) {
568 out << in.substr(ptr);
572 out << in.substr(ptr, finalspace - ptr) <<
"\n";
573 if (in[finalspace] ==
'\n') {
576 out << std::string(indent,
' ');
579 ptr = finalspace + 1;
600 return strtoll(psz,
nullptr, 10);
607 return _atoi64(str.c_str());
609 return strtoll(str.c_str(),
nullptr, 10);
613 int atoi(
const std::string& str)
615 return atoi(str.c_str());
626 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
629 static inline bool ProcessMantissaDigit(
char ch, int64_t &mantissa,
int &mantissa_tzeros)
634 for (
int i=0; i<=mantissa_tzeros; ++i) {
635 if (mantissa > (UPPER_BOUND / 10LL))
639 mantissa += ch -
'0';
647 int64_t mantissa = 0;
648 int64_t exponent = 0;
649 int mantissa_tzeros = 0;
650 bool mantissa_sign =
false;
651 bool exponent_sign =
false;
653 int end = val.size();
656 if (ptr < end && val[ptr] ==
'-') {
657 mantissa_sign =
true;
662 if (val[ptr] ==
'0') {
665 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
666 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
667 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
673 if (ptr < end && val[ptr] ==
'.')
676 if (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9')
678 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
679 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
686 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
689 if (ptr < end && val[ptr] ==
'+')
691 else if (ptr < end && val[ptr] ==
'-') {
692 exponent_sign =
true;
695 if (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
696 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
697 if (exponent > (UPPER_BOUND / 10LL))
699 exponent = exponent * 10 + val[ptr] -
'0';
709 exponent = -exponent;
710 exponent = exponent - point_ofs + mantissa_tzeros;
714 mantissa = -mantissa;
717 exponent += decimals;
723 for (
int i=0; i < exponent; ++i) {
724 if (mantissa > (420000000000000000LL) || mantissa < -(UPPER_BOUND / 10LL))
728 if (mantissa > 4200000000000000000LL || mantissa < -UPPER_BOUND)
732 *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)
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...
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.
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.
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)