1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
6 #include "utilstrencodings.h"
8 #include "tinyformat.h"
17 string
SanitizeString(const string
& str
)
20 * safeChars chosen to allow simple messages/URLs/email addresses, but avoid anything
21 * even possibly remotely dangerous like & or >
23 static string
safeChars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 .,;_/:?@()");
25 for (std::string::size_type i
= 0; i
< str
.size(); i
++)
27 if (safeChars
.find(str
[i
]) != std::string::npos
)
28 strResult
.push_back(str
[i
]);
33 const signed char p_util_hexdigit
[256] =
34 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
35 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
36 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
37 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
38 -1,0xa,0xb,0xc,0xd,0xe,0xf,-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 -1,0xa,0xb,0xc,0xd,0xe,0xf,-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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
43 -1,-1,-1,-1,-1,-1,-1,-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, };
51 signed char HexDigit(char c
)
53 return p_util_hexdigit
[(unsigned char)c
];
56 bool IsHex(const string
& str
)
58 for(std::string::const_iterator
it(str
.begin()); it
!= str
.end(); ++it
)
60 if (HexDigit(*it
) < 0)
63 return (str
.size() > 0) && (str
.size()%2 == 0);
66 vector
<unsigned char> ParseHex(const char* psz
)
68 // convert hex dump to vector
69 vector
<unsigned char> vch
;
74 signed char c
= HexDigit(*psz
++);
75 if (c
== (signed char)-1)
77 unsigned char n
= (c
<< 4);
79 if (c
== (signed char)-1)
87 vector
<unsigned char> ParseHex(const string
& str
)
89 return ParseHex(str
.c_str());
92 string
EncodeBase64(const unsigned char* pch
, size_t len
)
94 static const char *pbase64
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
97 strRet
.reserve((len
+2)/3*4);
100 const unsigned char *pchEnd
= pch
+len
;
107 case 0: // we have no bits
108 strRet
+= pbase64
[enc
>> 2];
109 left
= (enc
& 3) << 4;
113 case 1: // we have two bits
114 strRet
+= pbase64
[left
| (enc
>> 4)];
115 left
= (enc
& 15) << 2;
119 case 2: // we have four bits
120 strRet
+= pbase64
[left
| (enc
>> 6)];
121 strRet
+= pbase64
[enc
& 63];
129 strRet
+= pbase64
[left
];
138 string
EncodeBase64(const string
& str
)
140 return EncodeBase64((const unsigned char*)str
.c_str(), str
.size());
143 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
165 vector
<unsigned char> vchRet
;
166 vchRet
.reserve(strlen(p
)*3/4);
173 int dec
= decode64_table
[(unsigned char)*p
];
174 if (dec
== -1) break;
178 case 0: // we have no bits and get 6
183 case 1: // we have 6 bits and keep 4
184 vchRet
.push_back((left
<<2) | (dec
>>4));
189 case 2: // we have 4 bits and get 6, we keep 2
190 vchRet
.push_back((left
<<4) | (dec
>>2));
195 case 3: // we have 2 bits and get 6
196 vchRet
.push_back((left
<<6) | dec
);
205 case 0: // 4n base64 characters processed: ok
208 case 1: // 4n+1 base64 character processed: impossible
212 case 2: // 4n+2 base64 characters processed: require '=='
213 if (left
|| p
[0] != '=' || p
[1] != '=' || decode64_table
[(unsigned char)p
[2]] != -1)
217 case 3: // 4n+3 base64 characters processed: require '='
218 if (left
|| p
[0] != '=' || decode64_table
[(unsigned char)p
[1]] != -1)
226 string
DecodeBase64(const string
& str
)
228 vector
<unsigned char> vchRet
= DecodeBase64(str
.c_str());
229 return (vchRet
.size() == 0) ? string() : string((const char*)&vchRet
[0], vchRet
.size());
232 string
EncodeBase32(const unsigned char* pch
, size_t len
)
234 static const char *pbase32
= "abcdefghijklmnopqrstuvwxyz234567";
237 strRet
.reserve((len
+4)/5*8);
240 const unsigned char *pchEnd
= pch
+len
;
247 case 0: // we have no bits
248 strRet
+= pbase32
[enc
>> 3];
249 left
= (enc
& 7) << 2;
253 case 1: // we have three bits
254 strRet
+= pbase32
[left
| (enc
>> 6)];
255 strRet
+= pbase32
[(enc
>> 1) & 31];
256 left
= (enc
& 1) << 4;
260 case 2: // we have one bit
261 strRet
+= pbase32
[left
| (enc
>> 4)];
262 left
= (enc
& 15) << 1;
266 case 3: // we have four bits
267 strRet
+= pbase32
[left
| (enc
>> 7)];
268 strRet
+= pbase32
[(enc
>> 2) & 31];
269 left
= (enc
& 3) << 3;
273 case 4: // we have two bits
274 strRet
+= pbase32
[left
| (enc
>> 5)];
275 strRet
+= pbase32
[enc
& 31];
280 static const int nPadding
[5] = {0, 6, 4, 3, 1};
283 strRet
+= pbase32
[left
];
284 for (int n
=0; n
<nPadding
[mode
]; n
++)
291 string
EncodeBase32(const string
& str
)
293 return EncodeBase32((const unsigned char*)str
.c_str(), str
.size());
296 vector
<unsigned char> DecodeBase32(const char* p
, bool* pfInvalid
)
298 static const int decode32_table
[256] =
300 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
301 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
302 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
303 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
304 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
305 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
306 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
307 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
308 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
309 -1, -1, -1, -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
318 vector
<unsigned char> vchRet
;
319 vchRet
.reserve((strlen(p
))*5/8);
326 int dec
= decode32_table
[(unsigned char)*p
];
327 if (dec
== -1) break;
331 case 0: // we have no bits and get 5
336 case 1: // we have 5 bits and keep 2
337 vchRet
.push_back((left
<<3) | (dec
>>2));
342 case 2: // we have 2 bits and keep 7
343 left
= left
<< 5 | dec
;
347 case 3: // we have 7 bits and keep 4
348 vchRet
.push_back((left
<<1) | (dec
>>4));
353 case 4: // we have 4 bits, and keep 1
354 vchRet
.push_back((left
<<4) | (dec
>>1));
359 case 5: // we have 1 bit, and keep 6
360 left
= left
<< 5 | dec
;
364 case 6: // we have 6 bits, and keep 3
365 vchRet
.push_back((left
<<2) | (dec
>>3));
370 case 7: // we have 3 bits, and keep 0
371 vchRet
.push_back((left
<<5) | dec
);
380 case 0: // 8n base32 characters processed: ok
383 case 1: // 8n+1 base32 characters processed: impossible
389 case 2: // 8n+2 base32 characters processed: require '======'
390 if (left
|| p
[0] != '=' || p
[1] != '=' || p
[2] != '=' || p
[3] != '=' || p
[4] != '=' || p
[5] != '=' || decode32_table
[(unsigned char)p
[6]] != -1)
394 case 4: // 8n+4 base32 characters processed: require '===='
395 if (left
|| p
[0] != '=' || p
[1] != '=' || p
[2] != '=' || p
[3] != '=' || decode32_table
[(unsigned char)p
[4]] != -1)
399 case 5: // 8n+5 base32 characters processed: require '==='
400 if (left
|| p
[0] != '=' || p
[1] != '=' || p
[2] != '=' || decode32_table
[(unsigned char)p
[3]] != -1)
404 case 7: // 8n+7 base32 characters processed: require '='
405 if (left
|| p
[0] != '=' || decode32_table
[(unsigned char)p
[1]] != -1)
413 string
DecodeBase32(const string
& str
)
415 vector
<unsigned char> vchRet
= DecodeBase32(str
.c_str());
416 return (vchRet
.size() == 0) ? string() : string((const char*)&vchRet
[0], vchRet
.size());
419 static bool ParsePrechecks(const std::string
& str
)
421 if (str
.empty()) // No empty string allowed
423 if (str
.size() >= 1 && (isspace(str
[0]) || isspace(str
[str
.size()-1]))) // No padding allowed
425 if (str
.size() != strlen(str
.c_str())) // No embedded NUL characters allowed
430 bool ParseInt32(const std::string
& str
, int32_t *out
)
432 if (!ParsePrechecks(str
))
435 errno
= 0; // strtol will not set errno if valid
436 long int n
= strtol(str
.c_str(), &endp
, 10);
437 if(out
) *out
= (int32_t)n
;
438 // Note that strtol returns a *long int*, so even if strtol doesn't report a over/underflow
439 // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
440 // platforms the size of these types may be different.
441 return endp
&& *endp
== 0 && !errno
&&
442 n
>= std::numeric_limits
<int32_t>::min() &&
443 n
<= std::numeric_limits
<int32_t>::max();
446 bool ParseInt64(const std::string
& str
, int64_t *out
)
448 if (!ParsePrechecks(str
))
451 errno
= 0; // strtoll will not set errno if valid
452 long long int n
= strtoll(str
.c_str(), &endp
, 10);
453 if(out
) *out
= (int64_t)n
;
454 // Note that strtoll returns a *long long int*, so even if strtol doesn't report a over/underflow
455 // we still have to check that the returned value is within the range of an *int64_t*.
456 return endp
&& *endp
== 0 && !errno
&&
457 n
>= std::numeric_limits
<int64_t>::min() &&
458 n
<= std::numeric_limits
<int64_t>::max();
461 bool ParseDouble(const std::string
& str
, double *out
)
463 if (!ParsePrechecks(str
))
465 if (str
.size() >= 2 && str
[0] == '0' && str
[1] == 'x') // No hexadecimal floats allowed
467 std::istringstream
text(str
);
468 text
.imbue(std::locale::classic());
471 if(out
) *out
= result
;
472 return text
.eof() && !text
.fail();
475 std::string
FormatParagraph(const std::string
& in
, size_t width
, size_t indent
)
477 std::stringstream out
;
480 while(ptr
< in
.size())
482 // Find beginning of next word
483 ptr
= in
.find_first_not_of(' ', ptr
);
484 if (ptr
== std::string::npos
)
486 // Find end of next word
487 size_t endword
= in
.find_first_of(' ', ptr
);
488 if (endword
== std::string::npos
)
490 // Add newline and indentation if this wraps over the allowed width
493 if ((col
+ endword
- ptr
) > width
)
496 for(size_t i
=0; i
<indent
; ++i
)
503 out
<< in
.substr(ptr
, endword
- ptr
);
504 col
+= endword
- ptr
+ 1;
510 std::string
i64tostr(int64_t n
)
512 return strprintf("%d", n
);
515 std::string
itostr(int n
)
517 return strprintf("%d", n
);
520 int64_t atoi64(const char* psz
)
525 return strtoll(psz
, NULL
, 10);
529 int64_t atoi64(const std::string
& str
)
532 return _atoi64(str
.c_str());
534 return strtoll(str
.c_str(), NULL
, 10);
538 int atoi(const std::string
& str
)
540 return atoi(str
.c_str());
543 /** Upper bound for mantissa.
544 * 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer.
545 * Larger integers cannot consist of arbitrary combinations of 0-9:
547 * 999999999999999999 1^18-1
548 * 9223372036854775807 (1<<63)-1 (max int64_t)
549 * 9999999999999999999 1^19-1 (would overflow)
551 static const int64_t UPPER_BOUND
= 1000000000000000000LL - 1LL;
553 /** Helper function for ParseFixedPoint */
554 static inline bool ProcessMantissaDigit(char ch
, int64_t &mantissa
, int &mantissa_tzeros
)
559 for (int i
=0; i
<=mantissa_tzeros
; ++i
) {
560 if (mantissa
> (UPPER_BOUND
/ 10LL))
561 return false; /* overflow */
564 mantissa
+= ch
- '0';
570 bool ParseFixedPoint(const std::string
&val
, int decimals
, int64_t *amount_out
)
572 int64_t mantissa
= 0;
573 int64_t exponent
= 0;
574 int mantissa_tzeros
= 0;
575 bool mantissa_sign
= false;
576 bool exponent_sign
= false;
578 int end
= val
.size();
581 if (ptr
< end
&& val
[ptr
] == '-') {
582 mantissa_sign
= true;
587 if (val
[ptr
] == '0') {
590 } else if (val
[ptr
] >= '1' && val
[ptr
] <= '9') {
591 while (ptr
< end
&& val
[ptr
] >= '0' && val
[ptr
] <= '9') {
592 if (!ProcessMantissaDigit(val
[ptr
], mantissa
, mantissa_tzeros
))
593 return false; /* overflow */
596 } else return false; /* missing expected digit */
597 } else return false; /* empty string or loose '-' */
598 if (ptr
< end
&& val
[ptr
] == '.')
601 if (ptr
< end
&& val
[ptr
] >= '0' && val
[ptr
] <= '9')
603 while (ptr
< end
&& val
[ptr
] >= '0' && val
[ptr
] <= '9') {
604 if (!ProcessMantissaDigit(val
[ptr
], mantissa
, mantissa_tzeros
))
605 return false; /* overflow */
609 } else return false; /* missing expected digit */
611 if (ptr
< end
&& (val
[ptr
] == 'e' || val
[ptr
] == 'E'))
614 if (ptr
< end
&& val
[ptr
] == '+')
616 else if (ptr
< end
&& val
[ptr
] == '-') {
617 exponent_sign
= true;
620 if (ptr
< end
&& val
[ptr
] >= '0' && val
[ptr
] <= '9') {
621 while (ptr
< end
&& val
[ptr
] >= '0' && val
[ptr
] <= '9') {
622 if (exponent
> (UPPER_BOUND
/ 10LL))
623 return false; /* overflow */
624 exponent
= exponent
* 10 + val
[ptr
] - '0';
627 } else return false; /* missing expected digit */
630 return false; /* trailing garbage */
632 /* finalize exponent */
634 exponent
= -exponent
;
635 exponent
= exponent
- point_ofs
+ mantissa_tzeros
;
637 /* finalize mantissa */
639 mantissa
= -mantissa
;
641 /* convert to one 64-bit fixed-point value */
642 exponent
+= decimals
;
644 return false; /* cannot represent values smaller than 10^-decimals */
646 return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
648 for (int i
=0; i
< exponent
; ++i
) {
649 if (mantissa
> (UPPER_BOUND
/ 10LL) || mantissa
< -(UPPER_BOUND
/ 10LL))
650 return false; /* overflow */
653 if (mantissa
> UPPER_BOUND
|| mantissa
< -UPPER_BOUND
)
654 return false; /* overflow */
657 *amount_out
= mantissa
;