1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 /**
7 * Utilities for converting data from/to strings.
8 */
9 #ifndef BITCOIN_UTIL_STRENCODINGS_H
10 #define BITCOIN_UTIL_STRENCODINGS_H
11
12 #include <attributes.h>
13 #include <span.h>
14
15 #include <cstdint>
16 #include <iterator>
17 #include <string>
18 #include <vector>
19
20 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
21
22 /** Used by SanitizeString() */
23 enum SafeChars
24 {
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
28 SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986)
29 };
30
31 /**
32 * Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email
33 * addresses, but avoid anything even possibly remotely dangerous like & or >
34 * @param[in] str The string to sanitize
35 * @param[in] rule The set of safe chars to choose (default: least restrictive)
36 * @return A new string without unsafe chars
37 */
38 std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
39 std::vector<unsigned char> ParseHex(const char* psz);
40 std::vector<unsigned char> ParseHex(const std::string& str);
41 signed char HexDigit(char c);
42 /* Returns true if each character in str is a hex character, and has an even
43 * number of hex digits.*/
44 bool IsHex(const std::string& str);
45 /**
46 * Return true if the string is a hex number, optionally prefixed with "0x"
47 */
48 bool IsHexNumber(const std::string& str);
49 std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
50 std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
51 std::string EncodeBase64(Span<const unsigned char> input);
52 std::string EncodeBase64(const std::string& str);
53 std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
54 std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
55
56 /**
57 * Base32 encode.
58 * If `pad` is true, then the output will be padded with '=' so that its length
59 * is a multiple of 8.
60 */
61 std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
62
63 /**
64 * Base32 encode.
65 * If `pad` is true, then the output will be padded with '=' so that its length
66 * is a multiple of 8.
67 */
68 std::string EncodeBase32(const std::string& str, bool pad = true);
69
70 void SplitHostPort(std::string in, int& portOut, std::string& hostOut);
71 int64_t atoi64(const std::string& str);
72 int atoi(const std::string& str);
73
74 /**
75 * Tests if the given character is a decimal digit.
76 * @param[in] c character to test
77 * @return true if the argument is a decimal digit; otherwise false.
78 */
IsDigit(char c)79 constexpr bool IsDigit(char c)
80 {
81 return c >= '0' && c <= '9';
82 }
83
84 /**
85 * Tests if the given character is a whitespace character. The whitespace characters
86 * are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal
87 * tab ('\t'), and vertical tab ('\v').
88 *
89 * This function is locale independent. Under the C locale this function gives the
90 * same result as std::isspace.
91 *
92 * @param[in] c character to test
93 * @return true if the argument is a whitespace character; otherwise false
94 */
IsSpace(char c)95 constexpr inline bool IsSpace(char c) noexcept {
96 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
97 }
98
99 /**
100 * Convert string to signed 32-bit integer with strict parse error feedback.
101 * @returns true if the entire string could be parsed as valid integer,
102 * false if not the entire string could be parsed or when overflow or underflow occurred.
103 */
104 NODISCARD bool ParseInt32(const std::string& str, int32_t *out);
105
106 /**
107 * Convert string to signed 64-bit integer with strict parse error feedback.
108 * @returns true if the entire string could be parsed as valid integer,
109 * false if not the entire string could be parsed or when overflow or underflow occurred.
110 */
111 NODISCARD bool ParseInt64(const std::string& str, int64_t *out);
112
113 /**
114 * Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
115 * @returns true if the entire string could be parsed as valid integer,
116 * false if not the entire string could be parsed or when overflow or underflow occurred.
117 */
118 NODISCARD bool ParseUInt8(const std::string& str, uint8_t *out);
119
120 /**
121 * Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
122 * @returns true if the entire string could be parsed as valid integer,
123 * false if not the entire string could be parsed or when overflow or underflow occurred.
124 */
125 NODISCARD bool ParseUInt32(const std::string& str, uint32_t *out);
126
127 /**
128 * Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
129 * @returns true if the entire string could be parsed as valid integer,
130 * false if not the entire string could be parsed or when overflow or underflow occurred.
131 */
132 NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
133
134 /**
135 * Convert string to double with strict parse error feedback.
136 * @returns true if the entire string could be parsed as valid double,
137 * false if not the entire string could be parsed or when overflow or underflow occurred.
138 */
139 NODISCARD bool ParseDouble(const std::string& str, double *out);
140
141 /**
142 * Convert a span of bytes to a lower-case hexadecimal string.
143 */
144 std::string HexStr(const Span<const uint8_t> s);
HexStr(const Span<const char> s)145 inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
146
147 /**
148 * Format a paragraph of text to a fixed width, adding spaces for
149 * indentation to any added line.
150 */
151 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
152
153 /**
154 * Timing-attack-resistant comparison.
155 * Takes time proportional to length
156 * of first argument.
157 */
158 template <typename T>
TimingResistantEqual(const T & a,const T & b)159 bool TimingResistantEqual(const T& a, const T& b)
160 {
161 if (b.size() == 0) return a.size() == 0;
162 size_t accumulator = a.size() ^ b.size();
163 for (size_t i = 0; i < a.size(); i++)
164 accumulator |= a[i] ^ b[i%b.size()];
165 return accumulator == 0;
166 }
167
168 /** Parse number as fixed point according to JSON number syntax.
169 * See http://json.org/number.gif
170 * @returns true on success, false on error.
171 * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
172 */
173 NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
174
175 /** Convert from one power-of-2 number base to another. */
176 template<int frombits, int tobits, bool pad, typename O, typename I>
ConvertBits(const O & outfn,I it,I end)177 bool ConvertBits(const O& outfn, I it, I end) {
178 size_t acc = 0;
179 size_t bits = 0;
180 constexpr size_t maxv = (1 << tobits) - 1;
181 constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
182 while (it != end) {
183 acc = ((acc << frombits) | *it) & max_acc;
184 bits += frombits;
185 while (bits >= tobits) {
186 bits -= tobits;
187 outfn((acc >> bits) & maxv);
188 }
189 ++it;
190 }
191 if (pad) {
192 if (bits) outfn((acc << (tobits - bits)) & maxv);
193 } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
194 return false;
195 }
196 return true;
197 }
198
199 /**
200 * Converts the given character to its lowercase equivalent.
201 * This function is locale independent. It only converts uppercase
202 * characters in the standard 7-bit ASCII range.
203 * This is a feature, not a limitation.
204 *
205 * @param[in] c the character to convert to lowercase.
206 * @return the lowercase equivalent of c; or the argument
207 * if no conversion is possible.
208 */
ToLower(char c)209 constexpr char ToLower(char c)
210 {
211 return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
212 }
213
214 /**
215 * Returns the lowercase equivalent of the given string.
216 * This function is locale independent. It only converts uppercase
217 * characters in the standard 7-bit ASCII range.
218 * This is a feature, not a limitation.
219 *
220 * @param[in] str the string to convert to lowercase.
221 * @returns lowercased equivalent of str
222 */
223 std::string ToLower(const std::string& str);
224
225 /**
226 * Converts the given character to its uppercase equivalent.
227 * This function is locale independent. It only converts lowercase
228 * characters in the standard 7-bit ASCII range.
229 * This is a feature, not a limitation.
230 *
231 * @param[in] c the character to convert to uppercase.
232 * @return the uppercase equivalent of c; or the argument
233 * if no conversion is possible.
234 */
ToUpper(char c)235 constexpr char ToUpper(char c)
236 {
237 return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
238 }
239
240 /**
241 * Returns the uppercase equivalent of the given string.
242 * This function is locale independent. It only converts lowercase
243 * characters in the standard 7-bit ASCII range.
244 * This is a feature, not a limitation.
245 *
246 * @param[in] str the string to convert to uppercase.
247 * @returns UPPERCASED EQUIVALENT OF str
248 */
249 std::string ToUpper(const std::string& str);
250
251 /**
252 * Capitalizes the first character of the given string.
253 * This function is locale independent. It only converts lowercase
254 * characters in the standard 7-bit ASCII range.
255 * This is a feature, not a limitation.
256 *
257 * @param[in] str the string to capitalize.
258 * @returns string with the first letter capitalized.
259 */
260 std::string Capitalize(std::string str);
261
262 #endif // BITCOIN_UTIL_STRENCODINGS_H
263