21 static const std::string CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
23 static const std::string SAFE_CHARS[] =
25 CHARS_ALPHA_NUM +
" .,;-_/:?@()",
26 CHARS_ALPHA_NUM +
" .,;-_?@",
27 CHARS_ALPHA_NUM +
".-_",
32 std::string strResult;
33 for (std::string::size_type i = 0; i < str.size(); i++)
35 if (SAFE_CHARS[rule].find(str[i]) != std::string::npos)
36 strResult.push_back(str[i]);
47 bool validateURL(
const std::string& strURL, std::string& strErr,
unsigned int maxSize)
50 if (strURL.size() > maxSize) {
51 strErr =
strprintf(
"Invalid URL: %d exceeds limit of %d characters.", strURL.size(), maxSize);
56 std::regex url_regex(R
"(^(https?)://[^\s/$.?#][^\s]*[^\s/.]\.[^\s/.][^\s]*[^\s.]$)");
57 if (!std::regex_match(strURL, url_regex)) {
58 strErr =
"Invalid URL";
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
71 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
80 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
81 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
82 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90 bool IsHex(
const std::string& str)
92 for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) {
96 return (str.size() > 0) && (str.size() % 2 == 0);
99 std::vector<unsigned char>
ParseHex(
const char* psz)
102 std::vector<unsigned char> vch;
104 while (isspace(*psz))
107 if (c == (
signed char)-1)
109 unsigned char n = (c << 4);
111 if (c == (
signed char)-1)
119 std::vector<unsigned char>
ParseHex(
const std::string& str)
126 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
129 str.reserve(((input.
size() + 2) / 3) * 4);
130 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(), input.
end());
131 while (str.size() % 4) str +=
'=';
140 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid)
142 static const int decode64_table[256] =
144 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
145 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
146 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
147 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
148 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
149 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
150 49, 50, 51, -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
153 -1, -1, -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, -1, -1,
155 -1, -1, -1, -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
160 std::vector<uint8_t> val;
161 val.reserve(strlen(p));
163 int x = decode64_table[(
unsigned char)*p];
169 std::vector<unsigned char> ret;
170 ret.reserve((val.size() * 3) / 4);
171 bool valid = ConvertBits<6, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
174 while (valid && *p != 0) {
181 valid = valid && (p - e) % 4 == 0 && p - q < 4;
182 if (pfInvalid) *pfInvalid = !valid;
189 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str());
190 return std::string((
const char*)vchRet.data(), vchRet.size());
195 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
198 str.reserve(((input.
size() + 4) / 5) * 8);
199 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
201 while (str.size() % 8) {
214 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid)
216 static const int decode32_table[256] =
218 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
219 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
220 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
221 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
222 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
223 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
224 23, 24, 25, -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
227 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
228 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
229 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
230 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
234 std::vector<uint8_t> val;
235 val.reserve(strlen(p));
237 int x = decode32_table[(
unsigned char)*p];
243 std::vector<unsigned char> ret;
244 ret.reserve((val.size() * 5) / 8);
245 bool valid = ConvertBits<5, 8, false>([&](
unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
248 while (valid && *p != 0) {
255 valid = valid && (p - e) % 8 == 0 && p - q < 8;
256 if (pfInvalid) *pfInvalid = !valid;
263 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str());
264 return std::string((
const char*)vchRet.data(), vchRet.size());
267 static bool ParsePrechecks(
const std::string& str)
271 if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1])))
280 if (!ParsePrechecks(str))
282 char *endp =
nullptr;
284 long int n = strtol(str.c_str(), &endp, 10);
285 if(out) *out = (int32_t)n;
289 return endp && *endp == 0 && !errno &&
290 n >= std::numeric_limits<int32_t>::min() &&
291 n <= std::numeric_limits<int32_t>::max();
296 if (!ParsePrechecks(str))
298 char *endp =
nullptr;
300 long long int n = strtoll(str.c_str(), &endp, 10);
301 if(out) *out = (int64_t)n;
304 return endp && *endp == 0 && !errno &&
305 n >= std::numeric_limits<int64_t>::min() &&
306 n <= std::numeric_limits<int64_t>::max();
315 if (out !=
nullptr) {
316 *out =
static_cast<uint8_t
>(
u32);
323 if (!ParsePrechecks(str))
325 if (str.size() >= 1 && str[0] ==
'-')
327 char *endp =
nullptr;
329 unsigned long int n = strtoul(str.c_str(), &endp, 10);
330 if(out) *out = (uint32_t)n;
334 return endp && *endp == 0 && !errno &&
335 n <= std::numeric_limits<uint32_t>::max();
340 if (!ParsePrechecks(str))
342 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
344 std::istringstream text(str);
345 text.imbue(std::locale::classic());
348 if(out) *out = result;
349 return text.eof() && !text.fail();
354 std::stringstream out;
357 while (ptr < in.size()) {
359 ptr = in.find_first_not_of(
' ', ptr);
360 if (ptr == std::string::npos)
363 size_t endword = in.find_first_of(
' ', ptr);
364 if (endword == std::string::npos)
368 if ((col + endword - ptr) > width) {
370 for (
size_t i = 0; i < indent; ++i)
377 out << in.substr(ptr, endword - ptr);
378 col += endword - ptr + 1;
399 return strtoll(psz,
nullptr, 10);
406 return _atoi64(str.c_str());
408 return strtoll(str.c_str(),
nullptr, 10);
412 int atoi(
const std::string& str)
414 return atoi(str.c_str());
426 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
429 static inline bool ProcessMantissaDigit(
char ch, int64_t &mantissa,
int &mantissa_tzeros)
434 for (
int i=0; i<=mantissa_tzeros; ++i) {
435 if (mantissa > (UPPER_BOUND / 10LL))
439 mantissa += ch -
'0';
447 int64_t mantissa = 0;
448 int64_t exponent = 0;
449 int mantissa_tzeros = 0;
450 bool mantissa_sign =
false;
451 bool exponent_sign =
false;
453 int end = val.size();
456 if (ptr < end && val[ptr] ==
'-') {
457 mantissa_sign =
true;
462 if (val[ptr] ==
'0') {
465 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
466 while (ptr < end &&
IsDigit(val[ptr])) {
467 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
473 if (ptr < end && val[ptr] ==
'.')
476 if (ptr < end &&
IsDigit(val[ptr]))
478 while (ptr < end &&
IsDigit(val[ptr])) {
479 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
486 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
489 if (ptr < end && val[ptr] ==
'+')
491 else if (ptr < end && val[ptr] ==
'-') {
492 exponent_sign =
true;
495 if (ptr < end &&
IsDigit(val[ptr])) {
496 while (ptr < end &&
IsDigit(val[ptr])) {
497 if (exponent > (UPPER_BOUND / 10LL))
499 exponent = exponent * 10 + val[ptr] -
'0';
509 exponent = -exponent;
510 exponent = exponent - point_ofs + mantissa_tzeros;
514 mantissa = -mantissa;
517 exponent += decimals;
523 for (
int i=0; i < exponent; ++i) {
524 if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
528 if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
532 *amount_out = mantissa;
539 std::transform(str.begin(), str.end(), str.begin(), [](
unsigned char c){return ToLower(c);});
545 for (
auto ch : str) r +=
ToLower((
unsigned char)ch);
552 for (
auto ch : str) r +=
ToUpper((
unsigned char)ch);
558 if (str.empty())
return str;
566 static constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
567 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
568 rv.reserve(s.
size() * 2);
570 rv.push_back(hexmap[v >> 4]);
571 rv.push_back(hexmap[v & 15]);
579 const unsigned int strLen = str.length();
581 for (
unsigned i = 0; i < strLen; i++) {
582 c = (
unsigned char) str[i];
583 if (0x00 <= c && c <= 0x7f) n=0;
584 else if ((c & 0xE0) == 0xC0) n=1;
585 else if ( c == 0xED && i < (strLen - 1) && ((
unsigned char)str[i+1] & 0xA0) == 0xA0)
587 else if ((c & 0xF0) == 0xE0) n=2;
588 else if ((c & 0xF8) == 0xF0) n=3;
590 for (
int j=0; j < n && i < strLen; j++) {
591 if ((++i == strLen) || (( (
unsigned char)str[i] & 0xC0) != 0x80))
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
constexpr C * end() const noexcept
constexpr C * begin() const noexcept
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
void Downcase(std::string &str)
Converts the given string to its lowercase equivalent.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
int64_t atoi64(const char *psz)
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.
const signed char p_util_hexdigit[256]
std::string EncodeBase64(Span< const unsigned char > input)
std::string itostr(int n)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::vector< unsigned char > ParseHex(const char *psz)
bool validateURL(const std::string &strURL)
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 > DecodeBase64(const char *p, bool *pfInvalid)
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
bool IsValidUTF8(const std::string &str)
Checks for valid 4-byte UTF-8 encoding in a string.
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)
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
signed char HexDigit(char c)
int atoi(const std::string &str)
std::string i64tostr(int64_t n)
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid)
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.
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.