]>
Commit | Line | Data |
---|---|---|
1 | // Copyright (c) 2009-2010 Satoshi Nakamoto | |
2 | // Copyright (c) 2009-2014 The Bitcoin Core developers | |
3 | // Distributed under the MIT software license, see the accompanying | |
4 | // file COPYING or https://www.opensource.org/licenses/mit-license.php . | |
5 | ||
6 | /** | |
7 | * Utilities for converting data from/to strings. | |
8 | */ | |
9 | #ifndef BITCOIN_UTILSTRENCODINGS_H | |
10 | #define BITCOIN_UTILSTRENCODINGS_H | |
11 | ||
12 | #include <stdint.h> | |
13 | #include <string> | |
14 | #include <vector> | |
15 | ||
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])) | |
21 | ||
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> | |
24 | ||
25 | /** Used by SanitizeString() */ | |
26 | enum SafeChars | |
27 | { | |
28 | SAFE_CHARS_DEFAULT, //!< The full set of allowed chars | |
29 | SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset | |
30 | SAFE_CHARS_URI //!< Chars allowed in URIs (RFC 3986) | |
31 | }; | |
32 | ||
33 | std::string SanitizeFilename(const std::string& str); | |
34 | /** | |
35 | * Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email | |
36 | * addresses, but avoid anything even possibly remotely dangerous like & or > | |
37 | * @param[in] str The string to sanitize | |
38 | * @param[in] rule The set of safe chars to choose (default: least restrictive) | |
39 | * @return A new string without unsafe chars | |
40 | */ | |
41 | std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT); | |
42 | std::string HexInt(uint32_t val); | |
43 | uint32_t ParseHexToUInt32(const std::string& str); | |
44 | std::vector<unsigned char> ParseHex(const char* psz); | |
45 | std::vector<unsigned char> ParseHex(const std::string& str); | |
46 | signed char HexDigit(char c); | |
47 | bool IsHex(const std::string& str); | |
48 | std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL); | |
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 = NULL); | |
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); | |
56 | ||
57 | std::string i64tostr(int64_t n); | |
58 | std::string itostr(int n); | |
59 | int64_t atoi64(const char* psz); | |
60 | int64_t atoi64(const std::string& str); | |
61 | int atoi(const std::string& str); | |
62 | ||
63 | /** | |
64 | * Convert string to signed 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 ParseInt32(const std::string& str, int32_t *out); | |
69 | ||
70 | /** | |
71 | * Convert string to signed 64-bit integer with strict parse error feedback. | |
72 | * @returns true if the entire string could be parsed as valid integer, | |
73 | * false if not the entire string could be parsed or when overflow or underflow occurred. | |
74 | */ | |
75 | bool ParseInt64(const std::string& str, int64_t *out); | |
76 | ||
77 | /** | |
78 | * Convert string to double with strict parse error feedback. | |
79 | * @returns true if the entire string could be parsed as valid double, | |
80 | * false if not the entire string could be parsed or when overflow or underflow occurred. | |
81 | */ | |
82 | bool ParseDouble(const std::string& str, double *out); | |
83 | ||
84 | template<typename T> | |
85 | std::string HexStr(const T itbegin, const T itend, bool fSpaces=false) | |
86 | { | |
87 | std::string rv; | |
88 | static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', | |
89 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; | |
90 | rv.reserve((itend-itbegin)*3); | |
91 | for(T it = itbegin; it < itend; ++it) | |
92 | { | |
93 | unsigned char val = (unsigned char)(*it); | |
94 | if(fSpaces && it != itbegin) | |
95 | rv.push_back(' '); | |
96 | rv.push_back(hexmap[val>>4]); | |
97 | rv.push_back(hexmap[val&15]); | |
98 | } | |
99 | ||
100 | return rv; | |
101 | } | |
102 | ||
103 | template<typename T> | |
104 | inline std::string HexStr(const T& vch, bool fSpaces=false) | |
105 | { | |
106 | return HexStr(vch.begin(), vch.end(), fSpaces); | |
107 | } | |
108 | ||
109 | /** | |
110 | * Format a paragraph of text to a fixed width, adding spaces for | |
111 | * indentation to any added line. | |
112 | */ | |
113 | std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0); | |
114 | ||
115 | /** | |
116 | * Timing-attack-resistant comparison. | |
117 | * Takes time proportional to length | |
118 | * of first argument. | |
119 | */ | |
120 | template <typename T> | |
121 | bool TimingResistantEqual(const T& a, const T& b) | |
122 | { | |
123 | if (b.size() == 0) return a.size() == 0; | |
124 | size_t accumulator = a.size() ^ b.size(); | |
125 | for (size_t i = 0; i < a.size(); i++) | |
126 | accumulator |= a[i] ^ b[i%b.size()]; | |
127 | return accumulator == 0; | |
128 | } | |
129 | ||
130 | /** Parse number as fixed point according to JSON number syntax. | |
131 | * See http://json.org/number.gif | |
132 | * @returns true on success, false on error. | |
133 | * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger. | |
134 | */ | |
135 | bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); | |
136 | ||
137 | /** | |
138 | * Convert from one power-of-2 number base to another. | |
139 | * | |
140 | * Examples using ConvertBits<8, 5, true>(): | |
141 | * 000000 -> 0000000000 | |
142 | * 202020 -> 0400100200 | |
143 | * 757575 -> 0e151a170a | |
144 | * abcdef -> 150f061e1e | |
145 | * ffffff -> 1f1f1f1f1e | |
146 | */ | |
147 | template<int frombits, int tobits, bool pad, typename O, typename I> | |
148 | bool ConvertBits(const O& outfn, I it, I end) { | |
149 | size_t acc = 0; | |
150 | size_t bits = 0; | |
151 | constexpr size_t maxv = (1 << tobits) - 1; | |
152 | constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1; | |
153 | while (it != end) { | |
154 | acc = ((acc << frombits) | *it) & max_acc; | |
155 | bits += frombits; | |
156 | while (bits >= tobits) { | |
157 | bits -= tobits; | |
158 | outfn((acc >> bits) & maxv); | |
159 | } | |
160 | ++it; | |
161 | } | |
162 | if (pad) { | |
163 | if (bits) outfn((acc << (tobits - bits)) & maxv); | |
164 | } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) { | |
165 | return false; | |
166 | } | |
167 | return true; | |
168 | } | |
169 | ||
170 | #endif // BITCOIN_UTILSTRENCODINGS_H |