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 bool IsHex(const std::string
& str
);
42 std::vector
<unsigned char> DecodeBase64(const char* p
, bool* pfInvalid
= nullptr);
43 std::string
DecodeBase64(const std::string
& str
);
44 std::string
EncodeBase64(const unsigned char* pch
, size_t len
);
45 std::string
EncodeBase64(const std::string
& str
);
46 std::vector
<unsigned char> DecodeBase32(const char* p
, bool* pfInvalid
= nullptr);
47 std::string
DecodeBase32(const std::string
& str
);
48 std::string
EncodeBase32(const unsigned char* pch
, size_t len
);
49 std::string
EncodeBase32(const std::string
& str
);
51 void SplitHostPort(std::string in
, int &portOut
, std::string
&hostOut
);
52 std::string
i64tostr(int64_t n
);
53 std::string
itostr(int n
);
54 int64_t atoi64(const char* psz
);
55 int64_t atoi64(const std::string
& str
);
56 int atoi(const std::string
& str
);
59 * Convert string to signed 32-bit integer with strict parse error feedback.
60 * @returns true if the entire string could be parsed as valid integer,
61 * false if not the entire string could be parsed or when overflow or underflow occurred.
63 bool ParseInt32(const std::string
& str
, int32_t *out
);
66 * Convert string to signed 64-bit integer with strict parse error feedback.
67 * @returns true if the entire string could be parsed as valid integer,
68 * false if not the entire string could be parsed or when overflow or underflow occurred.
70 bool ParseInt64(const std::string
& str
, int64_t *out
);
73 * Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
74 * @returns true if the entire string could be parsed as valid integer,
75 * false if not the entire string could be parsed or when overflow or underflow occurred.
77 bool ParseUInt32(const std::string
& str
, uint32_t *out
);
80 * Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
81 * @returns true if the entire string could be parsed as valid integer,
82 * false if not the entire string could be parsed or when overflow or underflow occurred.
84 bool ParseUInt64(const std::string
& str
, uint64_t *out
);
87 * Convert string to double with strict parse error feedback.
88 * @returns true if the entire string could be parsed as valid double,
89 * false if not the entire string could be parsed or when overflow or underflow occurred.
91 bool ParseDouble(const std::string
& str
, double *out
);
94 std::string
HexStr(const T itbegin
, const T itend
, bool fSpaces
=false)
97 static const char hexmap
[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
98 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
99 rv
.reserve((itend
-itbegin
)*3);
100 for(T it
= itbegin
; it
< itend
; ++it
)
102 unsigned char val
= (unsigned char)(*it
);
103 if(fSpaces
&& it
!= itbegin
)
105 rv
.push_back(hexmap
[val
>>4]);
106 rv
.push_back(hexmap
[val
&15]);
113 inline std::string
HexStr(const T
& vch
, bool fSpaces
=false)
115 return HexStr(vch
.begin(), vch
.end(), fSpaces
);
119 * Format a paragraph of text to a fixed width, adding spaces for
120 * indentation to any added line.
122 std::string
FormatParagraph(const std::string
& in
, size_t width
= 79, size_t indent
= 0);
125 * Timing-attack-resistant comparison.
126 * Takes time proportional to length
129 template <typename T
>
130 bool TimingResistantEqual(const T
& a
, const T
& b
)
132 if (b
.size() == 0) return a
.size() == 0;
133 size_t accumulator
= a
.size() ^ b
.size();
134 for (size_t i
= 0; i
< a
.size(); i
++)
135 accumulator
|= a
[i
] ^ b
[i
%b
.size()];
136 return accumulator
== 0;
139 /** Parse number as fixed point according to JSON number syntax.
140 * See http://json.org/number.gif
141 * @returns true on success, false on error.
142 * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
144 bool ParseFixedPoint(const std::string
&val
, int decimals
, int64_t *amount_out
);
146 #endif // BITCOIN_UTILSTRENCODINGS_H