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.
7 * Utilities for converting data from/to strings.
9 #ifndef BITCOIN_UTILSTRENCODINGS_H
10 #define BITCOIN_UTILSTRENCODINGS_H
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 /** Used by SanitizeString() */
25 SAFE_CHARS_DEFAULT
, //!< The full set of allowed chars
26 SAFE_CHARS_UA_COMMENT
, //!< BIP-0014 subset
27 SAFE_CHARS_FILENAME
, //!< Chars allowed in filenames
31 * Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email
32 * addresses, but avoid anything even possibly remotely dangerous like & or >
33 * @param[in] str The string to sanitize
34 * @param[in] rule The set of safe chars to choose (default: least restrictive)
35 * @return A new string without unsafe chars
37 std::string
SanitizeString(const std::string
& str
, int rule
= SAFE_CHARS_DEFAULT
);
38 std::vector
<unsigned char> ParseHex(const char* psz
);
39 std::vector
<unsigned char> ParseHex(const std::string
& str
);
40 signed char HexDigit(char c
);
41 /* Returns true if each character in str is a hex character, and has an even
42 * number of hex digits.*/
43 bool IsHex(const std::string
& str
);
45 * Return true if the string is a hex number, optionally prefixed with "0x"
47 bool IsHexNumber(const std::string
& str
);
48 std::vector
<unsigned char> DecodeBase64(const char* p
, bool* pfInvalid
= nullptr);
49 std::string
DecodeBase64(const std::string
& str
);
50 std::string
EncodeBase64(const unsigned char* pch
, size_t len
);
51 std::string
EncodeBase64(const std::string
& str
);
52 std::vector
<unsigned char> DecodeBase32(const char* p
, bool* pfInvalid
= nullptr);
53 std::string
DecodeBase32(const std::string
& str
);
54 std::string
EncodeBase32(const unsigned char* pch
, size_t len
);
55 std::string
EncodeBase32(const std::string
& str
);
57 void SplitHostPort(std::string in
, int &portOut
, std::string
&hostOut
);
58 std::string
i64tostr(int64_t n
);
59 std::string
itostr(int n
);
60 int64_t atoi64(const char* psz
);
61 int64_t atoi64(const std::string
& str
);
62 int atoi(const std::string
& str
);
65 * Convert string to signed 32-bit integer with strict parse error feedback.
66 * @returns true if the entire string could be parsed as valid integer,
67 * false if not the entire string could be parsed or when overflow or underflow occurred.
69 bool ParseInt32(const std::string
& str
, int32_t *out
);
72 * Convert string to signed 64-bit integer with strict parse error feedback.
73 * @returns true if the entire string could be parsed as valid integer,
74 * false if not the entire string could be parsed or when overflow or underflow occurred.
76 bool ParseInt64(const std::string
& str
, int64_t *out
);
79 * Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
80 * @returns true if the entire string could be parsed as valid integer,
81 * false if not the entire string could be parsed or when overflow or underflow occurred.
83 bool ParseUInt32(const std::string
& str
, uint32_t *out
);
86 * Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
87 * @returns true if the entire string could be parsed as valid integer,
88 * false if not the entire string could be parsed or when overflow or underflow occurred.
90 bool ParseUInt64(const std::string
& str
, uint64_t *out
);
93 * Convert string to double with strict parse error feedback.
94 * @returns true if the entire string could be parsed as valid double,
95 * false if not the entire string could be parsed or when overflow or underflow occurred.
97 bool ParseDouble(const std::string
& str
, double *out
);
100 std::string
HexStr(const T itbegin
, const T itend
, bool fSpaces
=false)
103 static const char hexmap
[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
104 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
105 rv
.reserve((itend
-itbegin
)*3);
106 for(T it
= itbegin
; it
< itend
; ++it
)
108 unsigned char val
= (unsigned char)(*it
);
109 if(fSpaces
&& it
!= itbegin
)
111 rv
.push_back(hexmap
[val
>>4]);
112 rv
.push_back(hexmap
[val
&15]);
119 inline std::string
HexStr(const T
& vch
, bool fSpaces
=false)
121 return HexStr(vch
.begin(), vch
.end(), fSpaces
);
125 * Format a paragraph of text to a fixed width, adding spaces for
126 * indentation to any added line.
128 std::string
FormatParagraph(const std::string
& in
, size_t width
= 79, size_t indent
= 0);
131 * Timing-attack-resistant comparison.
132 * Takes time proportional to length
135 template <typename T
>
136 bool TimingResistantEqual(const T
& a
, const T
& b
)
138 if (b
.size() == 0) return a
.size() == 0;
139 size_t accumulator
= a
.size() ^ b
.size();
140 for (size_t i
= 0; i
< a
.size(); i
++)
141 accumulator
|= a
[i
] ^ b
[i
%b
.size()];
142 return accumulator
== 0;
145 /** Parse number as fixed point according to JSON number syntax.
146 * See http://json.org/number.gif
147 * @returns true on success, false on error.
148 * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
150 bool ParseFixedPoint(const std::string
&val
, int decimals
, int64_t *amount_out
);
152 /** Convert from one power-of-2 number base to another. */
153 template<int frombits
, int tobits
, bool pad
, typename O
, typename I
>
154 bool ConvertBits(O
& out
, I it
, I end
) {
157 constexpr size_t maxv
= (1 << tobits
) - 1;
158 constexpr size_t max_acc
= (1 << (frombits
+ tobits
- 1)) - 1;
160 acc
= ((acc
<< frombits
) | *it
) & max_acc
;
162 while (bits
>= tobits
) {
164 out
.push_back((acc
>> bits
) & maxv
);
169 if (bits
) out
.push_back((acc
<< (tobits
- bits
)) & maxv
);
170 } else if (bits
>= frombits
|| ((acc
<< (tobits
- bits
)) & maxv
)) {
176 #endif // BITCOIN_UTILSTRENCODINGS_H