You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utilstrencodings.cpp 23KB

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