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