17 static const string CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
19 static const string SAFE_CHARS[] =
21 CHARS_ALPHA_NUM +
" .,;-_/:?@()",
22 CHARS_ALPHA_NUM +
" .,;-_?@"
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, };
61 for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
66 return (str.size() > 0) && (str.size()%2 == 0);
69 vector<unsigned char>
ParseHex(
const char* psz)
72 vector<unsigned char> vch;
78 if (c == (
signed char)-1)
80 unsigned char n = (c << 4);
82 if (c == (
signed char)-1)
90 vector<unsigned char>
ParseHex(
const string& str)
97 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
100 strRet.reserve((len+2)/3*4);
103 const unsigned char *pchEnd = pch+len;
111 strRet += pbase64[enc >> 2];
112 left = (enc & 3) << 4;
117 strRet += pbase64[left | (enc >> 4)];
118 left = (enc & 15) << 2;
123 strRet += pbase64[left | (enc >> 6)];
124 strRet += pbase64[enc & 63];
132 strRet += pbase64[left];
143 return EncodeBase64((
const unsigned char*)str.c_str(), str.size());
148 static const int decode64_table[256] =
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
152 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
153 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
154 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
155 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
156 49, 50, 51, -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, -1, -1, -1, -1,
162 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
168 vector<unsigned char> vchRet;
169 vchRet.reserve(strlen(p)*3/4);
176 int dec = decode64_table[(
unsigned char)*p];
177 if (dec == -1)
break;
187 vchRet.push_back((left<<2) | (dec>>4));
193 vchRet.push_back((left<<4) | (dec>>2));
199 vchRet.push_back((left<<6) | dec);
216 if (left || p[0] !=
'=' || p[1] !=
'=' || decode64_table[(
unsigned char)p[2]] != -1)
221 if (left || p[0] !=
'=' || decode64_table[(
unsigned char)p[1]] != -1)
231 vector<unsigned char> vchRet =
DecodeBase64(str.c_str());
232 return (vchRet.size() == 0) ? string() : string((
const char*)&vchRet[0], vchRet.size());
237 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
240 strRet.reserve((len+4)/5*8);
243 const unsigned char *pchEnd = pch+len;
251 strRet += pbase32[enc >> 3];
252 left = (enc & 7) << 2;
257 strRet += pbase32[left | (enc >> 6)];
258 strRet += pbase32[(enc >> 1) & 31];
259 left = (enc & 1) << 4;
264 strRet += pbase32[left | (enc >> 4)];
265 left = (enc & 15) << 1;
270 strRet += pbase32[left | (enc >> 7)];
271 strRet += pbase32[(enc >> 2) & 31];
272 left = (enc & 3) << 3;
277 strRet += pbase32[left | (enc >> 5)];
278 strRet += pbase32[enc & 31];
283 static const int nPadding[5] = {0, 6, 4, 3, 1};
286 strRet += pbase32[left];
287 for (
int n=0; n<nPadding[mode]; n++)
296 return EncodeBase32((
const unsigned char*)str.c_str(), str.size());
301 static const int decode32_table[256] =
303 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
304 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
305 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
306 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
307 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
308 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
309 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
310 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
311 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
312 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
313 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
314 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
315 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
321 vector<unsigned char> vchRet;
322 vchRet.reserve((strlen(p))*5/8);
329 int dec = decode32_table[(
unsigned char)*p];
330 if (dec == -1)
break;
340 vchRet.push_back((left<<3) | (dec>>2));
346 left = left << 5 | dec;
351 vchRet.push_back((left<<1) | (dec>>4));
357 vchRet.push_back((left<<4) | (dec>>1));
363 left = left << 5 | dec;
368 vchRet.push_back((left<<2) | (dec>>3));
374 vchRet.push_back((left<<5) | dec);
393 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || p[4] !=
'=' || p[5] !=
'=' || decode32_table[(
unsigned char)p[6]] != -1)
398 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || p[3] !=
'=' || decode32_table[(
unsigned char)p[4]] != -1)
403 if (left || p[0] !=
'=' || p[1] !=
'=' || p[2] !=
'=' || decode32_table[(
unsigned char)p[3]] != -1)
408 if (left || p[0] !=
'=' || decode32_table[(
unsigned char)p[1]] != -1)
418 vector<unsigned char> vchRet =
DecodeBase32(str.c_str());
419 return (vchRet.size() == 0) ? string() : string((
const char*)&vchRet[0], vchRet.size());
422 static bool ParsePrechecks(
const std::string& str)
426 if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1])))
428 if (str.size() != strlen(str.c_str()))
435 if (!ParsePrechecks(str))
439 long int n = strtol(str.c_str(), &endp, 10);
440 if(out) *out = (int32_t)n;
444 return endp && *endp == 0 && !errno &&
445 n >= std::numeric_limits<int32_t>::min() &&
446 n <= std::numeric_limits<int32_t>::max();
451 if (!ParsePrechecks(str))
455 long long int n = strtoll(str.c_str(), &endp, 10);
456 if(out) *out = (int64_t)n;
459 return endp && *endp == 0 && !errno &&
460 n >= std::numeric_limits<int64_t>::min() &&
461 n <= std::numeric_limits<int64_t>::max();
466 if (!ParsePrechecks(str))
468 if (str.size() >= 1 && str[0] ==
'-')
472 unsigned long int n = strtoul(str.c_str(), &endp, 10);
473 if(out) *out = (uint32_t)n;
477 return endp && *endp == 0 && !errno &&
478 n <= std::numeric_limits<uint32_t>::max();
483 if (!ParsePrechecks(str))
485 if (str.size() >= 1 && str[0] ==
'-')
489 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
490 if(out) *out = (uint64_t)n;
493 return endp && *endp == 0 && !errno &&
494 n <= std::numeric_limits<uint64_t>::max();
500 if (!ParsePrechecks(str))
502 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
504 std::istringstream text(str);
505 text.imbue(std::locale::classic());
508 if(out) *out = result;
509 return text.eof() && !text.fail();
514 std::stringstream out;
517 while (ptr < in.size())
519 size_t lineend = in.find_first_of(
'\n', ptr);
520 if (lineend == std::string::npos) {
523 const size_t linelen = lineend - ptr;
524 const size_t rem_width = width - indented;
525 if (linelen <= rem_width) {
526 out << in.substr(ptr, linelen + 1);
530 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
531 if (finalspace == std::string::npos || finalspace < ptr) {
533 finalspace = in.find_first_of(
"\n ", ptr);
534 if (finalspace == std::string::npos) {
536 out << in.substr(ptr);
540 out << in.substr(ptr, finalspace - ptr) <<
"\n";
541 if (in[finalspace] ==
'\n') {
544 out << std::string(indent,
' ');
547 ptr = finalspace + 1;
568 return strtoll(psz, NULL, 10);
575 return _atoi64(str.c_str());
577 return strtoll(str.c_str(), NULL, 10);
581 int atoi(
const std::string& str)
583 return atoi(str.c_str());
594 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
597 static inline bool ProcessMantissaDigit(
char ch, int64_t &mantissa,
int &mantissa_tzeros)
602 for (
int i=0; i<=mantissa_tzeros; ++i) {
603 if (mantissa > (UPPER_BOUND / 10LL))
607 mantissa += ch -
'0';
615 int64_t mantissa = 0;
616 int64_t exponent = 0;
617 int mantissa_tzeros = 0;
618 bool mantissa_sign =
false;
619 bool exponent_sign =
false;
621 int end = val.size();
624 if (ptr < end && val[ptr] ==
'-') {
625 mantissa_sign =
true;
630 if (val[ptr] ==
'0') {
633 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
634 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
635 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
641 if (ptr < end && val[ptr] ==
'.')
644 if (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9')
646 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
647 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
654 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
657 if (ptr < end && val[ptr] ==
'+')
659 else if (ptr < end && val[ptr] ==
'-') {
660 exponent_sign =
true;
663 if (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
664 while (ptr < end && val[ptr] >=
'0' && val[ptr] <=
'9') {
665 if (exponent > (UPPER_BOUND / 10LL))
667 exponent = exponent * 10 + val[ptr] -
'0';
677 exponent = -exponent;
678 exponent = exponent - point_ofs + mantissa_tzeros;
682 mantissa = -mantissa;
685 exponent += decimals;
691 for (
int i=0; i < exponent; ++i) {
692 if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
696 if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
700 *amount_out = mantissa;
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.
vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid)
int64_t atoi64(const char *psz)
const signed char p_util_hexdigit[256]
string SanitizeString(const string &str, int rule)
std::string itostr(int n)
string EncodeBase64(const unsigned char *pch, size_t len)
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
string EncodeBase32(const unsigned char *pch, size_t len)
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid)
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 string &str)
signed char HexDigit(char c)
int atoi(const std::string &str)
std::string i64tostr(int64_t n)
vector< unsigned char > ParseHex(const char *psz)
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.