1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include <util/strencodings.h>
7 #include <util/string.h>
8
9 #include <tinyformat.h>
10
11 #include <algorithm>
12 #include <cstdlib>
13 #include <cstring>
14 #include <errno.h>
15 #include <limits>
16
17 static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
18
19 static const std::string SAFE_CHARS[] =
20 {
21 CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
22 CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
23 CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
24 CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
25 };
26
SanitizeString(const std::string & str,int rule)27 std::string SanitizeString(const std::string& str, int rule)
28 {
29 std::string strResult;
30 for (std::string::size_type i = 0; i < str.size(); i++)
31 {
32 if (SAFE_CHARS[rule].find(str[i]) != std::string::npos)
33 strResult.push_back(str[i]);
34 }
35 return strResult;
36 }
37
38 const signed char p_util_hexdigit[256] =
39 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
40 -1,-1,-1,-1,-1,-1,-1,-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 0,1,2,3,4,5,6,7,8,9,-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,0xa,0xb,0xc,0xd,0xe,0xf,-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,
53 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
54 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
55
HexDigit(char c)56 signed char HexDigit(char c)
57 {
58 return p_util_hexdigit[(unsigned char)c];
59 }
60
IsHex(const std::string & str)61 bool IsHex(const std::string& str)
62 {
63 for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
64 {
65 if (HexDigit(*it) < 0)
66 return false;
67 }
68 return (str.size() > 0) && (str.size()%2 == 0);
69 }
70
IsHexNumber(const std::string & str)71 bool IsHexNumber(const std::string& str)
72 {
73 size_t starting_location = 0;
74 if (str.size() > 2 && *str.begin() == '0' && *(str.begin()+1) == 'x') {
75 starting_location = 2;
76 }
77 for (const char c : str.substr(starting_location)) {
78 if (HexDigit(c) < 0) return false;
79 }
80 // Return false for empty string or "0x".
81 return (str.size() > starting_location);
82 }
83
ParseHex(const char * psz)84 std::vector<unsigned char> ParseHex(const char* psz)
85 {
86 // convert hex dump to vector
87 std::vector<unsigned char> vch;
88 while (true)
89 {
90 while (IsSpace(*psz))
91 psz++;
92 signed char c = HexDigit(*psz++);
93 if (c == (signed char)-1)
94 break;
95 unsigned char n = (c << 4);
96 c = HexDigit(*psz++);
97 if (c == (signed char)-1)
98 break;
99 n |= c;
100 vch.push_back(n);
101 }
102 return vch;
103 }
104
ParseHex(const std::string & str)105 std::vector<unsigned char> ParseHex(const std::string& str)
106 {
107 return ParseHex(str.c_str());
108 }
109
SplitHostPort(std::string in,int & portOut,std::string & hostOut)110 void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
111 size_t colon = in.find_last_of(':');
112 // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
113 bool fHaveColon = colon != in.npos;
114 bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
115 bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
116 if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
117 int32_t n;
118 if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
119 in = in.substr(0, colon);
120 portOut = n;
121 }
122 }
123 if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
124 hostOut = in.substr(1, in.size()-2);
125 else
126 hostOut = in;
127 }
128
EncodeBase64(Span<const unsigned char> input)129 std::string EncodeBase64(Span<const unsigned char> input)
130 {
131 static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
132
133 std::string str;
134 str.reserve(((input.size() + 2) / 3) * 4);
135 ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
136 while (str.size() % 4) str += '=';
137 return str;
138 }
139
EncodeBase64(const std::string & str)140 std::string EncodeBase64(const std::string& str)
141 {
142 return EncodeBase64(MakeUCharSpan(str));
143 }
144
DecodeBase64(const char * p,bool * pf_invalid)145 std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
146 {
147 static const int decode64_table[256] =
148 {
149 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
152 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
153 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
154 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
155 49, 50, 51, -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, -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
162 };
163
164 const char* e = p;
165 std::vector<uint8_t> val;
166 val.reserve(strlen(p));
167 while (*p != 0) {
168 int x = decode64_table[(unsigned char)*p];
169 if (x == -1) break;
170 val.push_back(x);
171 ++p;
172 }
173
174 std::vector<unsigned char> ret;
175 ret.reserve((val.size() * 3) / 4);
176 bool valid = ConvertBits<6, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
177
178 const char* q = p;
179 while (valid && *p != 0) {
180 if (*p != '=') {
181 valid = false;
182 break;
183 }
184 ++p;
185 }
186 valid = valid && (p - e) % 4 == 0 && p - q < 4;
187 if (pf_invalid) *pf_invalid = !valid;
188
189 return ret;
190 }
191
DecodeBase64(const std::string & str,bool * pf_invalid)192 std::string DecodeBase64(const std::string& str, bool* pf_invalid)
193 {
194 if (!ValidAsCString(str)) {
195 if (pf_invalid) {
196 *pf_invalid = true;
197 }
198 return {};
199 }
200 std::vector<unsigned char> vchRet = DecodeBase64(str.c_str(), pf_invalid);
201 return std::string((const char*)vchRet.data(), vchRet.size());
202 }
203
EncodeBase32(Span<const unsigned char> input,bool pad)204 std::string EncodeBase32(Span<const unsigned char> input, bool pad)
205 {
206 static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
207
208 std::string str;
209 str.reserve(((input.size() + 4) / 5) * 8);
210 ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end());
211 if (pad) {
212 while (str.size() % 8) {
213 str += '=';
214 }
215 }
216 return str;
217 }
218
EncodeBase32(const std::string & str,bool pad)219 std::string EncodeBase32(const std::string& str, bool pad)
220 {
221 return EncodeBase32(MakeUCharSpan(str), pad);
222 }
223
DecodeBase32(const char * p,bool * pf_invalid)224 std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
225 {
226 static const int decode32_table[256] =
227 {
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, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
231 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
232 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
233 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
234 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
235 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
236 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
237 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
238 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
239 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
240 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
241 };
242
243 const char* e = p;
244 std::vector<uint8_t> val;
245 val.reserve(strlen(p));
246 while (*p != 0) {
247 int x = decode32_table[(unsigned char)*p];
248 if (x == -1) break;
249 val.push_back(x);
250 ++p;
251 }
252
253 std::vector<unsigned char> ret;
254 ret.reserve((val.size() * 5) / 8);
255 bool valid = ConvertBits<5, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
256
257 const char* q = p;
258 while (valid && *p != 0) {
259 if (*p != '=') {
260 valid = false;
261 break;
262 }
263 ++p;
264 }
265 valid = valid && (p - e) % 8 == 0 && p - q < 8;
266 if (pf_invalid) *pf_invalid = !valid;
267
268 return ret;
269 }
270
DecodeBase32(const std::string & str,bool * pf_invalid)271 std::string DecodeBase32(const std::string& str, bool* pf_invalid)
272 {
273 if (!ValidAsCString(str)) {
274 if (pf_invalid) {
275 *pf_invalid = true;
276 }
277 return {};
278 }
279 std::vector<unsigned char> vchRet = DecodeBase32(str.c_str(), pf_invalid);
280 return std::string((const char*)vchRet.data(), vchRet.size());
281 }
282
ParsePrechecks(const std::string & str)283 NODISCARD static bool ParsePrechecks(const std::string& str)
284 {
285 if (str.empty()) // No empty string allowed
286 return false;
287 if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size()-1]))) // No padding allowed
288 return false;
289 if (!ValidAsCString(str)) // No embedded NUL characters allowed
290 return false;
291 return true;
292 }
293
ParseInt32(const std::string & str,int32_t * out)294 bool ParseInt32(const std::string& str, int32_t *out)
295 {
296 if (!ParsePrechecks(str))
297 return false;
298 char *endp = nullptr;
299 errno = 0; // strtol will not set errno if valid
300 long int n = strtol(str.c_str(), &endp, 10);
301 if(out) *out = (int32_t)n;
302 // Note that strtol returns a *long int*, so even if strtol doesn't report an over/underflow
303 // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
304 // platforms the size of these types may be different.
305 return endp && *endp == 0 && !errno &&
306 n >= std::numeric_limits<int32_t>::min() &&
307 n <= std::numeric_limits<int32_t>::max();
308 }
309
ParseInt64(const std::string & str,int64_t * out)310 bool ParseInt64(const std::string& str, int64_t *out)
311 {
312 if (!ParsePrechecks(str))
313 return false;
314 char *endp = nullptr;
315 errno = 0; // strtoll will not set errno if valid
316 long long int n = strtoll(str.c_str(), &endp, 10);
317 if(out) *out = (int64_t)n;
318 // Note that strtoll returns a *long long int*, so even if strtol doesn't report an over/underflow
319 // we still have to check that the returned value is within the range of an *int64_t*.
320 return endp && *endp == 0 && !errno &&
321 n >= std::numeric_limits<int64_t>::min() &&
322 n <= std::numeric_limits<int64_t>::max();
323 }
324
ParseUInt8(const std::string & str,uint8_t * out)325 bool ParseUInt8(const std::string& str, uint8_t *out)
326 {
327 uint32_t u32;
328 if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
329 return false;
330 }
331 if (out != nullptr) {
332 *out = static_cast<uint8_t>(u32);
333 }
334 return true;
335 }
336
ParseUInt32(const std::string & str,uint32_t * out)337 bool ParseUInt32(const std::string& str, uint32_t *out)
338 {
339 if (!ParsePrechecks(str))
340 return false;
341 if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoul accepts these by default if they fit in the range
342 return false;
343 char *endp = nullptr;
344 errno = 0; // strtoul will not set errno if valid
345 unsigned long int n = strtoul(str.c_str(), &endp, 10);
346 if(out) *out = (uint32_t)n;
347 // Note that strtoul returns a *unsigned long int*, so even if it doesn't report an over/underflow
348 // we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit
349 // platforms the size of these types may be different.
350 return endp && *endp == 0 && !errno &&
351 n <= std::numeric_limits<uint32_t>::max();
352 }
353
ParseUInt64(const std::string & str,uint64_t * out)354 bool ParseUInt64(const std::string& str, uint64_t *out)
355 {
356 if (!ParsePrechecks(str))
357 return false;
358 if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoull accepts these by default if they fit in the range
359 return false;
360 char *endp = nullptr;
361 errno = 0; // strtoull will not set errno if valid
362 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
363 if(out) *out = (uint64_t)n;
364 // Note that strtoull returns a *unsigned long long int*, so even if it doesn't report an over/underflow
365 // we still have to check that the returned value is within the range of an *uint64_t*.
366 return endp && *endp == 0 && !errno &&
367 n <= std::numeric_limits<uint64_t>::max();
368 }
369
370
ParseDouble(const std::string & str,double * out)371 bool ParseDouble(const std::string& str, double *out)
372 {
373 if (!ParsePrechecks(str))
374 return false;
375 if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
376 return false;
377 std::istringstream text(str);
378 text.imbue(std::locale::classic());
379 double result;
380 text >> result;
381 if(out) *out = result;
382 return text.eof() && !text.fail();
383 }
384
FormatParagraph(const std::string & in,size_t width,size_t indent)385 std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
386 {
387 std::stringstream out;
388 size_t ptr = 0;
389 size_t indented = 0;
390 while (ptr < in.size())
391 {
392 size_t lineend = in.find_first_of('\n', ptr);
393 if (lineend == std::string::npos) {
394 lineend = in.size();
395 }
396 const size_t linelen = lineend - ptr;
397 const size_t rem_width = width - indented;
398 if (linelen <= rem_width) {
399 out << in.substr(ptr, linelen + 1);
400 ptr = lineend + 1;
401 indented = 0;
402 } else {
403 size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
404 if (finalspace == std::string::npos || finalspace < ptr) {
405 // No place to break; just include the entire word and move on
406 finalspace = in.find_first_of("\n ", ptr);
407 if (finalspace == std::string::npos) {
408 // End of the string, just add it and break
409 out << in.substr(ptr);
410 break;
411 }
412 }
413 out << in.substr(ptr, finalspace - ptr) << "\n";
414 if (in[finalspace] == '\n') {
415 indented = 0;
416 } else if (indent) {
417 out << std::string(indent, ' ');
418 indented = indent;
419 }
420 ptr = finalspace + 1;
421 }
422 }
423 return out.str();
424 }
425
atoi64(const std::string & str)426 int64_t atoi64(const std::string& str)
427 {
428 #ifdef _MSC_VER
429 return _atoi64(str.c_str());
430 #else
431 return strtoll(str.c_str(), nullptr, 10);
432 #endif
433 }
434
atoi(const std::string & str)435 int atoi(const std::string& str)
436 {
437 return atoi(str.c_str());
438 }
439
440 /** Upper bound for mantissa.
441 * 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer.
442 * Larger integers cannot consist of arbitrary combinations of 0-9:
443 *
444 * 999999999999999999 1^18-1
445 * 9223372036854775807 (1<<63)-1 (max int64_t)
446 * 9999999999999999999 1^19-1 (would overflow)
447 */
448 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
449
450 /** Helper function for ParseFixedPoint */
ProcessMantissaDigit(char ch,int64_t & mantissa,int & mantissa_tzeros)451 static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
452 {
453 if(ch == '0')
454 ++mantissa_tzeros;
455 else {
456 for (int i=0; i<=mantissa_tzeros; ++i) {
457 if (mantissa > (UPPER_BOUND / 10LL))
458 return false; /* overflow */
459 mantissa *= 10;
460 }
461 mantissa += ch - '0';
462 mantissa_tzeros = 0;
463 }
464 return true;
465 }
466
ParseFixedPoint(const std::string & val,int decimals,int64_t * amount_out)467 bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
468 {
469 int64_t mantissa = 0;
470 int64_t exponent = 0;
471 int mantissa_tzeros = 0;
472 bool mantissa_sign = false;
473 bool exponent_sign = false;
474 int ptr = 0;
475 int end = val.size();
476 int point_ofs = 0;
477
478 if (ptr < end && val[ptr] == '-') {
479 mantissa_sign = true;
480 ++ptr;
481 }
482 if (ptr < end)
483 {
484 if (val[ptr] == '0') {
485 /* pass single 0 */
486 ++ptr;
487 } else if (val[ptr] >= '1' && val[ptr] <= '9') {
488 while (ptr < end && IsDigit(val[ptr])) {
489 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
490 return false; /* overflow */
491 ++ptr;
492 }
493 } else return false; /* missing expected digit */
494 } else return false; /* empty string or loose '-' */
495 if (ptr < end && val[ptr] == '.')
496 {
497 ++ptr;
498 if (ptr < end && IsDigit(val[ptr]))
499 {
500 while (ptr < end && IsDigit(val[ptr])) {
501 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
502 return false; /* overflow */
503 ++ptr;
504 ++point_ofs;
505 }
506 } else return false; /* missing expected digit */
507 }
508 if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
509 {
510 ++ptr;
511 if (ptr < end && val[ptr] == '+')
512 ++ptr;
513 else if (ptr < end && val[ptr] == '-') {
514 exponent_sign = true;
515 ++ptr;
516 }
517 if (ptr < end && IsDigit(val[ptr])) {
518 while (ptr < end && IsDigit(val[ptr])) {
519 if (exponent > (UPPER_BOUND / 10LL))
520 return false; /* overflow */
521 exponent = exponent * 10 + val[ptr] - '0';
522 ++ptr;
523 }
524 } else return false; /* missing expected digit */
525 }
526 if (ptr != end)
527 return false; /* trailing garbage */
528
529 /* finalize exponent */
530 if (exponent_sign)
531 exponent = -exponent;
532 exponent = exponent - point_ofs + mantissa_tzeros;
533
534 /* finalize mantissa */
535 if (mantissa_sign)
536 mantissa = -mantissa;
537
538 /* convert to one 64-bit fixed-point value */
539 exponent += decimals;
540 if (exponent < 0)
541 return false; /* cannot represent values smaller than 10^-decimals */
542 if (exponent >= 18)
543 return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
544
545 for (int i=0; i < exponent; ++i) {
546 if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
547 return false; /* overflow */
548 mantissa *= 10;
549 }
550 if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
551 return false; /* overflow */
552
553 if (amount_out)
554 *amount_out = mantissa;
555
556 return true;
557 }
558
ToLower(const std::string & str)559 std::string ToLower(const std::string& str)
560 {
561 std::string r;
562 for (auto ch : str) r += ToLower((unsigned char)ch);
563 return r;
564 }
565
ToUpper(const std::string & str)566 std::string ToUpper(const std::string& str)
567 {
568 std::string r;
569 for (auto ch : str) r += ToUpper((unsigned char)ch);
570 return r;
571 }
572
Capitalize(std::string str)573 std::string Capitalize(std::string str)
574 {
575 if (str.empty()) return str;
576 str[0] = ToUpper(str.front());
577 return str;
578 }
579
HexStr(const Span<const uint8_t> s)580 std::string HexStr(const Span<const uint8_t> s)
581 {
582 std::string rv;
583 static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
584 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
585 rv.reserve(s.size() * 2);
586 for (uint8_t v: s) {
587 rv.push_back(hexmap[v >> 4]);
588 rv.push_back(hexmap[v & 15]);
589 }
590 return rv;
591 }
592