minor cleanup: remove unnecessary variable
[bitcoinplatinum.git] / src / utilstrencodings.cpp
blob74bf66fbf609ced32a920c12c3f1671c4342bb99
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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"
10 #include <cstdlib>
11 #include <cstring>
12 #include <errno.h>
13 #include <limits>
15 static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
17 static const std::string SAFE_CHARS[] =
19 CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
20 CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
21 CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
24 std::string SanitizeString(const std::string& str, int rule)
26 std::string strResult;
27 for (std::string::size_type i = 0; i < str.size(); i++)
29 if (SAFE_CHARS[rule].find(str[i]) != std::string::npos)
30 strResult.push_back(str[i]);
32 return strResult;
35 const signed char p_util_hexdigit[256] =
36 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
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 0,1,2,3,4,5,6,7,8,9,-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,0xa,0xb,0xc,0xd,0xe,0xf,-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,
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, };
53 signed char HexDigit(char c)
55 return p_util_hexdigit[(unsigned char)c];
58 bool IsHex(const std::string& str)
60 for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
62 if (HexDigit(*it) < 0)
63 return false;
65 return (str.size() > 0) && (str.size()%2 == 0);
68 std::vector<unsigned char> ParseHex(const char* psz)
70 // convert hex dump to vector
71 std::vector<unsigned char> vch;
72 while (true)
74 while (isspace(*psz))
75 psz++;
76 signed char c = HexDigit(*psz++);
77 if (c == (signed char)-1)
78 break;
79 unsigned char n = (c << 4);
80 c = HexDigit(*psz++);
81 if (c == (signed char)-1)
82 break;
83 n |= c;
84 vch.push_back(n);
86 return vch;
89 std::vector<unsigned char> ParseHex(const std::string& str)
91 return ParseHex(str.c_str());
94 std::string EncodeBase64(const unsigned char* pch, size_t len)
96 static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
98 std::string strRet = "";
99 strRet.reserve((len+2)/3*4);
101 int mode=0, left=0;
102 const unsigned char *pchEnd = pch+len;
104 while (pch<pchEnd)
106 int enc = *(pch++);
107 switch (mode)
109 case 0: // we have no bits
110 strRet += pbase64[enc >> 2];
111 left = (enc & 3) << 4;
112 mode = 1;
113 break;
115 case 1: // we have two bits
116 strRet += pbase64[left | (enc >> 4)];
117 left = (enc & 15) << 2;
118 mode = 2;
119 break;
121 case 2: // we have four bits
122 strRet += pbase64[left | (enc >> 6)];
123 strRet += pbase64[enc & 63];
124 mode = 0;
125 break;
129 if (mode)
131 strRet += pbase64[left];
132 strRet += '=';
133 if (mode == 1)
134 strRet += '=';
137 return strRet;
140 std::string EncodeBase64(const std::string& str)
142 return EncodeBase64((const unsigned char*)str.c_str(), str.size());
145 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
147 static const int decode64_table[256] =
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
164 if (pfInvalid)
165 *pfInvalid = false;
167 std::vector<unsigned char> vchRet;
168 vchRet.reserve(strlen(p)*3/4);
170 int mode = 0;
171 int left = 0;
173 while (1)
175 int dec = decode64_table[(unsigned char)*p];
176 if (dec == -1) break;
177 p++;
178 switch (mode)
180 case 0: // we have no bits and get 6
181 left = dec;
182 mode = 1;
183 break;
185 case 1: // we have 6 bits and keep 4
186 vchRet.push_back((left<<2) | (dec>>4));
187 left = dec & 15;
188 mode = 2;
189 break;
191 case 2: // we have 4 bits and get 6, we keep 2
192 vchRet.push_back((left<<4) | (dec>>2));
193 left = dec & 3;
194 mode = 3;
195 break;
197 case 3: // we have 2 bits and get 6
198 vchRet.push_back((left<<6) | dec);
199 mode = 0;
200 break;
204 if (pfInvalid)
205 switch (mode)
207 case 0: // 4n base64 characters processed: ok
208 break;
210 case 1: // 4n+1 base64 character processed: impossible
211 *pfInvalid = true;
212 break;
214 case 2: // 4n+2 base64 characters processed: require '=='
215 if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
216 *pfInvalid = true;
217 break;
219 case 3: // 4n+3 base64 characters processed: require '='
220 if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
221 *pfInvalid = true;
222 break;
225 return vchRet;
228 std::string DecodeBase64(const std::string& str)
230 std::vector<unsigned char> vchRet = DecodeBase64(str.c_str());
231 return (vchRet.size() == 0) ? std::string() : std::string((const char*)&vchRet[0], vchRet.size());
234 std::string EncodeBase32(const unsigned char* pch, size_t len)
236 static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
238 std::string strRet="";
239 strRet.reserve((len+4)/5*8);
241 int mode=0, left=0;
242 const unsigned char *pchEnd = pch+len;
244 while (pch<pchEnd)
246 int enc = *(pch++);
247 switch (mode)
249 case 0: // we have no bits
250 strRet += pbase32[enc >> 3];
251 left = (enc & 7) << 2;
252 mode = 1;
253 break;
255 case 1: // we have three bits
256 strRet += pbase32[left | (enc >> 6)];
257 strRet += pbase32[(enc >> 1) & 31];
258 left = (enc & 1) << 4;
259 mode = 2;
260 break;
262 case 2: // we have one bit
263 strRet += pbase32[left | (enc >> 4)];
264 left = (enc & 15) << 1;
265 mode = 3;
266 break;
268 case 3: // we have four bits
269 strRet += pbase32[left | (enc >> 7)];
270 strRet += pbase32[(enc >> 2) & 31];
271 left = (enc & 3) << 3;
272 mode = 4;
273 break;
275 case 4: // we have two bits
276 strRet += pbase32[left | (enc >> 5)];
277 strRet += pbase32[enc & 31];
278 mode = 0;
282 static const int nPadding[5] = {0, 6, 4, 3, 1};
283 if (mode)
285 strRet += pbase32[left];
286 for (int n=0; n<nPadding[mode]; n++)
287 strRet += '=';
290 return strRet;
293 std::string EncodeBase32(const std::string& str)
295 return EncodeBase32((const unsigned char*)str.c_str(), str.size());
298 std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
300 static const int decode32_table[256] =
302 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
305 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
306 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
307 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
308 23, 24, 25, -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, -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
317 if (pfInvalid)
318 *pfInvalid = false;
320 std::vector<unsigned char> vchRet;
321 vchRet.reserve((strlen(p))*5/8);
323 int mode = 0;
324 int left = 0;
326 while (1)
328 int dec = decode32_table[(unsigned char)*p];
329 if (dec == -1) break;
330 p++;
331 switch (mode)
333 case 0: // we have no bits and get 5
334 left = dec;
335 mode = 1;
336 break;
338 case 1: // we have 5 bits and keep 2
339 vchRet.push_back((left<<3) | (dec>>2));
340 left = dec & 3;
341 mode = 2;
342 break;
344 case 2: // we have 2 bits and keep 7
345 left = left << 5 | dec;
346 mode = 3;
347 break;
349 case 3: // we have 7 bits and keep 4
350 vchRet.push_back((left<<1) | (dec>>4));
351 left = dec & 15;
352 mode = 4;
353 break;
355 case 4: // we have 4 bits, and keep 1
356 vchRet.push_back((left<<4) | (dec>>1));
357 left = dec & 1;
358 mode = 5;
359 break;
361 case 5: // we have 1 bit, and keep 6
362 left = left << 5 | dec;
363 mode = 6;
364 break;
366 case 6: // we have 6 bits, and keep 3
367 vchRet.push_back((left<<2) | (dec>>3));
368 left = dec & 7;
369 mode = 7;
370 break;
372 case 7: // we have 3 bits, and keep 0
373 vchRet.push_back((left<<5) | dec);
374 mode = 0;
375 break;
379 if (pfInvalid)
380 switch (mode)
382 case 0: // 8n base32 characters processed: ok
383 break;
385 case 1: // 8n+1 base32 characters processed: impossible
386 case 3: // +3
387 case 6: // +6
388 *pfInvalid = true;
389 break;
391 case 2: // 8n+2 base32 characters processed: require '======'
392 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
393 *pfInvalid = true;
394 break;
396 case 4: // 8n+4 base32 characters processed: require '===='
397 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
398 *pfInvalid = true;
399 break;
401 case 5: // 8n+5 base32 characters processed: require '==='
402 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
403 *pfInvalid = true;
404 break;
406 case 7: // 8n+7 base32 characters processed: require '='
407 if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
408 *pfInvalid = true;
409 break;
412 return vchRet;
415 std::string DecodeBase32(const std::string& str)
417 std::vector<unsigned char> vchRet = DecodeBase32(str.c_str());
418 return (vchRet.size() == 0) ? std::string() : std::string((const char*)&vchRet[0], vchRet.size());
421 static bool ParsePrechecks(const std::string& str)
423 if (str.empty()) // No empty string allowed
424 return false;
425 if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1]))) // No padding allowed
426 return false;
427 if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
428 return false;
429 return true;
432 bool ParseInt32(const std::string& str, int32_t *out)
434 if (!ParsePrechecks(str))
435 return false;
436 char *endp = NULL;
437 errno = 0; // strtol will not set errno if valid
438 long int n = strtol(str.c_str(), &endp, 10);
439 if(out) *out = (int32_t)n;
440 // Note that strtol returns a *long int*, so even if strtol doesn't report a over/underflow
441 // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
442 // platforms the size of these types may be different.
443 return endp && *endp == 0 && !errno &&
444 n >= std::numeric_limits<int32_t>::min() &&
445 n <= std::numeric_limits<int32_t>::max();
448 bool ParseInt64(const std::string& str, int64_t *out)
450 if (!ParsePrechecks(str))
451 return false;
452 char *endp = NULL;
453 errno = 0; // strtoll will not set errno if valid
454 long long int n = strtoll(str.c_str(), &endp, 10);
455 if(out) *out = (int64_t)n;
456 // Note that strtoll returns a *long long int*, so even if strtol doesn't report a over/underflow
457 // we still have to check that the returned value is within the range of an *int64_t*.
458 return endp && *endp == 0 && !errno &&
459 n >= std::numeric_limits<int64_t>::min() &&
460 n <= std::numeric_limits<int64_t>::max();
463 bool ParseUInt32(const std::string& str, uint32_t *out)
465 if (!ParsePrechecks(str))
466 return false;
467 if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoul accepts these by default if they fit in the range
468 return false;
469 char *endp = NULL;
470 errno = 0; // strtoul will not set errno if valid
471 unsigned long int n = strtoul(str.c_str(), &endp, 10);
472 if(out) *out = (uint32_t)n;
473 // Note that strtoul returns a *unsigned long int*, so even if it doesn't report a over/underflow
474 // we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit
475 // platforms the size of these types may be different.
476 return endp && *endp == 0 && !errno &&
477 n <= std::numeric_limits<uint32_t>::max();
480 bool ParseUInt64(const std::string& str, uint64_t *out)
482 if (!ParsePrechecks(str))
483 return false;
484 if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoull accepts these by default if they fit in the range
485 return false;
486 char *endp = NULL;
487 errno = 0; // strtoull will not set errno if valid
488 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
489 if(out) *out = (uint64_t)n;
490 // Note that strtoull returns a *unsigned long long int*, so even if it doesn't report a over/underflow
491 // we still have to check that the returned value is within the range of an *uint64_t*.
492 return endp && *endp == 0 && !errno &&
493 n <= std::numeric_limits<uint64_t>::max();
497 bool ParseDouble(const std::string& str, double *out)
499 if (!ParsePrechecks(str))
500 return false;
501 if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
502 return false;
503 std::istringstream text(str);
504 text.imbue(std::locale::classic());
505 double result;
506 text >> result;
507 if(out) *out = result;
508 return text.eof() && !text.fail();
511 std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
513 std::stringstream out;
514 size_t ptr = 0;
515 size_t indented = 0;
516 while (ptr < in.size())
518 size_t lineend = in.find_first_of('\n', ptr);
519 if (lineend == std::string::npos) {
520 lineend = in.size();
522 const size_t linelen = lineend - ptr;
523 const size_t rem_width = width - indented;
524 if (linelen <= rem_width) {
525 out << in.substr(ptr, linelen + 1);
526 ptr = lineend + 1;
527 indented = 0;
528 } else {
529 size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
530 if (finalspace == std::string::npos || finalspace < ptr) {
531 // No place to break; just include the entire word and move on
532 finalspace = in.find_first_of("\n ", ptr);
533 if (finalspace == std::string::npos) {
534 // End of the string, just add it and break
535 out << in.substr(ptr);
536 break;
539 out << in.substr(ptr, finalspace - ptr) << "\n";
540 if (in[finalspace] == '\n') {
541 indented = 0;
542 } else if (indent) {
543 out << std::string(indent, ' ');
544 indented = indent;
546 ptr = finalspace + 1;
549 return out.str();
552 std::string i64tostr(int64_t n)
554 return strprintf("%d", n);
557 std::string itostr(int n)
559 return strprintf("%d", n);
562 int64_t atoi64(const char* psz)
564 #ifdef _MSC_VER
565 return _atoi64(psz);
566 #else
567 return strtoll(psz, NULL, 10);
568 #endif
571 int64_t atoi64(const std::string& str)
573 #ifdef _MSC_VER
574 return _atoi64(str.c_str());
575 #else
576 return strtoll(str.c_str(), NULL, 10);
577 #endif
580 int atoi(const std::string& str)
582 return atoi(str.c_str());
585 /** Upper bound for mantissa.
586 * 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer.
587 * Larger integers cannot consist of arbitrary combinations of 0-9:
589 * 999999999999999999 1^18-1
590 * 9223372036854775807 (1<<63)-1 (max int64_t)
591 * 9999999999999999999 1^19-1 (would overflow)
593 static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
595 /** Helper function for ParseFixedPoint */
596 static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
598 if(ch == '0')
599 ++mantissa_tzeros;
600 else {
601 for (int i=0; i<=mantissa_tzeros; ++i) {
602 if (mantissa > (UPPER_BOUND / 10LL))
603 return false; /* overflow */
604 mantissa *= 10;
606 mantissa += ch - '0';
607 mantissa_tzeros = 0;
609 return true;
612 bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
614 int64_t mantissa = 0;
615 int64_t exponent = 0;
616 int mantissa_tzeros = 0;
617 bool mantissa_sign = false;
618 bool exponent_sign = false;
619 int ptr = 0;
620 int end = val.size();
621 int point_ofs = 0;
623 if (ptr < end && val[ptr] == '-') {
624 mantissa_sign = true;
625 ++ptr;
627 if (ptr < end)
629 if (val[ptr] == '0') {
630 /* pass single 0 */
631 ++ptr;
632 } else if (val[ptr] >= '1' && val[ptr] <= '9') {
633 while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') {
634 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
635 return false; /* overflow */
636 ++ptr;
638 } else return false; /* missing expected digit */
639 } else return false; /* empty string or loose '-' */
640 if (ptr < end && val[ptr] == '.')
642 ++ptr;
643 if (ptr < end && val[ptr] >= '0' && val[ptr] <= '9')
645 while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') {
646 if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
647 return false; /* overflow */
648 ++ptr;
649 ++point_ofs;
651 } else return false; /* missing expected digit */
653 if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
655 ++ptr;
656 if (ptr < end && val[ptr] == '+')
657 ++ptr;
658 else if (ptr < end && val[ptr] == '-') {
659 exponent_sign = true;
660 ++ptr;
662 if (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') {
663 while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') {
664 if (exponent > (UPPER_BOUND / 10LL))
665 return false; /* overflow */
666 exponent = exponent * 10 + val[ptr] - '0';
667 ++ptr;
669 } else return false; /* missing expected digit */
671 if (ptr != end)
672 return false; /* trailing garbage */
674 /* finalize exponent */
675 if (exponent_sign)
676 exponent = -exponent;
677 exponent = exponent - point_ofs + mantissa_tzeros;
679 /* finalize mantissa */
680 if (mantissa_sign)
681 mantissa = -mantissa;
683 /* convert to one 64-bit fixed-point value */
684 exponent += decimals;
685 if (exponent < 0)
686 return false; /* cannot represent values smaller than 10^-decimals */
687 if (exponent >= 18)
688 return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
690 for (int i=0; i < exponent; ++i) {
691 if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
692 return false; /* overflow */
693 mantissa *= 10;
695 if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
696 return false; /* overflow */
698 if (amount_out)
699 *amount_out = mantissa;
701 return true;