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 (
const char 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+/";
132 str.reserve(((len + 2) / 3) * 4);
133 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, pch, pch + len);
134 while (str.size() % 4) str +=
'=';
140 return EncodeBase64((
const unsigned char*)str.c_str(), str.size());
143 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid)
145 static const int decode64_table[256] =
147 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
148 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
149 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
150 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
151 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
152 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
153 49, 50, 51, -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, -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
163 std::vector<uint8_t> val;
164 val.reserve(strlen(p));
166 int x = decode64_table[(
unsigned char)*p];
172 std::vector<unsigned char>
ret;
173 ret.reserve((val.size() * 3) / 4);
174 bool valid = ConvertBits<6, 8, false>([&](
unsigned char c) {
ret.
push_back(c); }, val.begin(), val.end());
177 while (valid && *p != 0) {
184 valid = valid && (p - e) % 4 == 0 && p - q < 4;
185 if (pfInvalid) *pfInvalid = !valid;
192 std::vector<unsigned char> vchRet =
DecodeBase64(str.c_str());
193 return std::string((
const char*)vchRet.data(), vchRet.size());
198 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
201 str.reserve(((len + 4) / 5) * 8);
202 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, pch, pch + len);
203 while (str.size() % 8) str +=
'=';
209 return EncodeBase32((
const unsigned char*)str.c_str(), str.size());
212 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid)
214 static const int decode32_table[256] =
216 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
217 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
218 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
219 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
220 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
221 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
222 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
223 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, -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
232 std::vector<uint8_t> val;
233 val.reserve(strlen(p));
235 int x = decode32_table[(
unsigned char)*p];
241 std::vector<unsigned char>
ret;
242 ret.reserve((val.size() * 5) / 8);
243 bool valid = ConvertBits<5, 8, false>([&](
unsigned char c) {
ret.
push_back(c); }, val.begin(), val.end());
246 while (valid && *p != 0) {
253 valid = valid && (p - e) % 8 == 0 && p - q < 8;
254 if (pfInvalid) *pfInvalid = !valid;
261 std::vector<unsigned char> vchRet =
DecodeBase32(str.c_str());
262 return std::string((
const char*)vchRet.data(), vchRet.size());
265 static bool ParsePrechecks(
const std::string& str)
269 if (str.size() >= 1 && (
IsSpace(str[0]) ||
IsSpace(str[str.size()-1])))
271 if (str.size() != strlen(str.c_str()))
278 if (!ParsePrechecks(str))
280 char *endp =
nullptr;
282 long int n = strtol(str.c_str(), &endp, 10);
283 if(out) *out = (int32_t)n;
287 return endp && *endp == 0 && !errno &&
288 n >= std::numeric_limits<int32_t>::min() &&
289 n <= std::numeric_limits<int32_t>::max();
294 if (!ParsePrechecks(str))
296 char *endp =
nullptr;
298 long long int n = strtoll(str.c_str(), &endp, 10);
299 if(out) *out = (int64_t)n;
302 return endp && *endp == 0 && !errno &&
303 n >= std::numeric_limits<int64_t>::min() &&
304 n <= std::numeric_limits<int64_t>::max();
309 if (!ParsePrechecks(str))
311 if (str.size() >= 1 && str[0] ==
'-')
313 char *endp =
nullptr;
315 unsigned long int n = strtoul(str.c_str(), &endp, 10);
316 if(out) *out = (uint32_t)n;
320 return endp && *endp == 0 && !errno &&
321 n <= std::numeric_limits<uint32_t>::max();
326 if (!ParsePrechecks(str))
328 if (str.size() >= 1 && str[0] ==
'-')
330 char *endp =
nullptr;
332 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
333 if(out) *out = (uint64_t)n;
336 return endp && *endp == 0 && !errno &&
337 n <= std::numeric_limits<uint64_t>::max();
343 if (!ParsePrechecks(str))
345 if (str.size() >= 2 && str[0] ==
'0' && str[1] ==
'x')
347 std::istringstream text(str);
348 text.imbue(std::locale::classic());
351 if(out) *out = result;
352 return text.eof() && !text.fail();
357 std::stringstream out;
360 while (ptr < in.size())
362 size_t lineend = in.find_first_of(
'\n', ptr);
363 if (lineend == std::string::npos) {
366 const size_t linelen = lineend - ptr;
367 const size_t rem_width = width - indented;
368 if (linelen <= rem_width) {
369 out << in.substr(ptr, linelen + 1);
373 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
374 if (finalspace == std::string::npos || finalspace < ptr) {
376 finalspace = in.find_first_of(
"\n ", ptr);
377 if (finalspace == std::string::npos) {
379 out << in.substr(ptr);
383 out << in.substr(ptr, finalspace - ptr) <<
"\n";
384 if (in[finalspace] ==
'\n') {
387 out << std::string(indent,
' ');
390 ptr = finalspace + 1;
411 return strtoll(psz,
nullptr, 10);
418 return _atoi64(str.c_str());
420 return strtoll(str.c_str(),
nullptr, 10);
424 int atoi(
const std::string& str)
426 return atoi(str.c_str());
437 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
440 static inline bool ProcessMantissaDigit(
char ch, int64_t &mantissa,
int &mantissa_tzeros)
445 for (
int i=0; i<=mantissa_tzeros; ++i) {
446 if (mantissa > (UPPER_BOUND / 10LL))
450 mantissa += ch -
'0';
458 int64_t mantissa = 0;
459 int64_t exponent = 0;
460 int mantissa_tzeros = 0;
461 bool mantissa_sign =
false;
462 bool exponent_sign =
false;
464 int end = val.size();
467 if (ptr <
end && val[ptr] ==
'-') {
468 mantissa_sign =
true;
473 if (val[ptr] ==
'0') {
476 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
478 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
484 if (ptr <
end && val[ptr] ==
'.')
490 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
497 if (ptr <
end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
500 if (ptr <
end && val[ptr] ==
'+')
502 else if (ptr <
end && val[ptr] ==
'-') {
503 exponent_sign =
true;
508 if (exponent > (UPPER_BOUND / 10LL))
510 exponent = exponent * 10 + val[ptr] -
'0';
520 exponent = -exponent;
521 exponent = exponent - point_ofs + mantissa_tzeros;
525 mantissa = -mantissa;
528 exponent += decimals;
534 for (
int i=0; i < exponent; ++i) {
535 if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
539 if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
543 *amount_out = mantissa;
548 bool ParseHDKeypath(
const std::string& keypath_str, std::vector<uint32_t>& keypath)
550 std::stringstream ss(keypath_str);
553 while (std::getline(ss, item,
'/')) {
554 if (item.compare(
"m") == 0) {
563 size_t pos = item.find(
"'");
564 if (pos != std::string::npos) {
566 if (pos != item.size() - 1) {
570 item = item.substr(0, item.size() - 1);
574 if (item.find_first_not_of(
"0123456789" ) != std::string::npos) {
583 keypath.push_back(path);
591 std::transform(str.begin(), str.end(), str.begin(), [](
unsigned char c){
return ToLower(c);});
596 if (str.empty())
return str;
constexpr unsigned char ToLower(unsigned char c)
Converts the given character to its lowercase equivalent.
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)
void Downcase(std::string &str)
Converts the given string to its lowercase equivalent.
UniValue ret(UniValue::VARR)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
constexpr unsigned char ToUpper(unsigned char c)
Converts the given character to its uppercase equivalent.
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool push_back(const UniValue &val)
const signed char p_util_hexdigit[256]
bool ParseHDKeypath(const std::string &keypath_str, std::vector< uint32_t > &keypath)
Parse an HD keypaths like "m/7/0'/2000".
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.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
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)