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.h 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.
  5. /**
  6. * Utilities for converting data from/to strings.
  7. */
  8. #ifndef BITCOIN_UTILSTRENCODINGS_H
  9. #define BITCOIN_UTILSTRENCODINGS_H
  10. #include <stdint.h>
  11. #include <string>
  12. #include <vector>
  13. #define BEGIN(a) ((char*)&(a))
  14. #define END(a) ((char*)&((&(a))[1]))
  15. #define UBEGIN(a) ((unsigned char*)&(a))
  16. #define UEND(a) ((unsigned char*)&((&(a))[1]))
  17. #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
  18. #define PAIRTYPE(t1, t2) std::pair<t1, t2>
  19. /** Used by SanitizeString() */
  20. enum SafeChars
  21. {
  22. SAFE_CHARS_DEFAULT, //!< The full set of allowed chars
  23. SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
  24. SAFE_CHARS_FILENAME, //!< Chars allowed in filenames
  25. };
  26. /**
  27. * Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email
  28. * addresses, but avoid anything even possibly remotely dangerous like & or >
  29. * @param[in] str The string to sanitize
  30. * @param[in] rule The set of safe chars to choose (default: least restrictive)
  31. * @return A new string without unsafe chars
  32. */
  33. std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
  34. std::vector<unsigned char> ParseHex(const char* psz);
  35. std::vector<unsigned char> ParseHex(const std::string& str);
  36. signed char HexDigit(char c);
  37. bool IsHex(const std::string& str);
  38. std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
  39. std::string DecodeBase64(const std::string& str);
  40. std::string EncodeBase64(const unsigned char* pch, size_t len);
  41. std::string EncodeBase64(const std::string& str);
  42. std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
  43. std::string DecodeBase32(const std::string& str);
  44. std::string EncodeBase32(const unsigned char* pch, size_t len);
  45. std::string EncodeBase32(const std::string& str);
  46. std::string i64tostr(int64_t n);
  47. std::string itostr(int n);
  48. int64_t atoi64(const char* psz);
  49. int64_t atoi64(const std::string& str);
  50. int atoi(const std::string& str);
  51. /**
  52. * Convert string to signed 32-bit integer with strict parse error feedback.
  53. * @returns true if the entire string could be parsed as valid integer,
  54. * false if not the entire string could be parsed or when overflow or underflow occurred.
  55. */
  56. bool ParseInt32(const std::string& str, int32_t *out);
  57. /**
  58. * Convert string to signed 64-bit integer with strict parse error feedback.
  59. * @returns true if the entire string could be parsed as valid integer,
  60. * false if not the entire string could be parsed or when overflow or underflow occurred.
  61. */
  62. bool ParseInt64(const std::string& str, int64_t *out);
  63. /**
  64. * Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
  65. * @returns true if the entire string could be parsed as valid integer,
  66. * false if not the entire string could be parsed or when overflow or underflow occurred.
  67. */
  68. bool ParseUInt32(const std::string& str, uint32_t *out);
  69. /**
  70. * Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
  71. * @returns true if the entire string could be parsed as valid integer,
  72. * false if not the entire string could be parsed or when overflow or underflow occurred.
  73. */
  74. bool ParseUInt64(const std::string& str, uint64_t *out);
  75. /**
  76. * Convert string to double with strict parse error feedback.
  77. * @returns true if the entire string could be parsed as valid double,
  78. * false if not the entire string could be parsed or when overflow or underflow occurred.
  79. */
  80. bool ParseDouble(const std::string& str, double *out);
  81. template<typename T>
  82. std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
  83. {
  84. std::string rv;
  85. static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
  86. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  87. rv.reserve((itend-itbegin)*3);
  88. for(T it = itbegin; it < itend; ++it)
  89. {
  90. unsigned char val = (unsigned char)(*it);
  91. if(fSpaces && it != itbegin)
  92. rv.push_back(' ');
  93. rv.push_back(hexmap[val>>4]);
  94. rv.push_back(hexmap[val&15]);
  95. }
  96. return rv;
  97. }
  98. template<typename T>
  99. inline std::string HexStr(const T& vch, bool fSpaces=false)
  100. {
  101. return HexStr(vch.begin(), vch.end(), fSpaces);
  102. }
  103. /**
  104. * Format a paragraph of text to a fixed width, adding spaces for
  105. * indentation to any added line.
  106. */
  107. std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
  108. /**
  109. * Timing-attack-resistant comparison.
  110. * Takes time proportional to length
  111. * of first argument.
  112. */
  113. template <typename T>
  114. bool TimingResistantEqual(const T& a, const T& b)
  115. {
  116. if (b.size() == 0) return a.size() == 0;
  117. size_t accumulator = a.size() ^ b.size();
  118. for (size_t i = 0; i < a.size(); i++)
  119. accumulator |= a[i] ^ b[i%b.size()];
  120. return accumulator == 0;
  121. }
  122. /** Parse number as fixed point according to JSON number syntax.
  123. * See http://json.org/number.gif
  124. * @returns true on success, false on error.
  125. * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
  126. */
  127. bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
  128. #endif // BITCOIN_UTILSTRENCODINGS_H