]>
Commit | Line | Data |
---|---|---|
ad49c256 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
c63a73d1 | 3 | // Distributed under the MIT software license, see the accompanying |
ad49c256 WL |
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_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 | ||
c63a73d1 | 22 | /** This is needed because the foreach macro can't get over the comma in pair<t1, t2> */ |
ad49c256 WL |
23 | #define PAIRTYPE(t1, t2) std::pair<t1, t2> |
24 | ||
3c1db170 M |
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 | }; | |
31 | ||
9064d73b | 32 | std::string SanitizeFilename(const std::string& str); |
3c1db170 M |
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 | |
39 | */ | |
40 | std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT); | |
b174b7e3 | 41 | std::string HexInt(uint32_t val); |
072099d7 | 42 | uint32_t ParseHexToUInt32(const std::string& str); |
ad49c256 WL |
43 | std::vector<unsigned char> ParseHex(const char* psz); |
44 | std::vector<unsigned char> ParseHex(const std::string& str); | |
45 | signed char HexDigit(char c); | |
46 | bool IsHex(const std::string& str); | |
47 | std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL); | |
48 | std::string DecodeBase64(const std::string& str); | |
49 | std::string EncodeBase64(const unsigned char* pch, size_t len); | |
50 | std::string EncodeBase64(const std::string& str); | |
51 | std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL); | |
52 | std::string DecodeBase32(const std::string& str); | |
53 | std::string EncodeBase32(const unsigned char* pch, size_t len); | |
54 | std::string EncodeBase32(const std::string& str); | |
55 | ||
56 | std::string i64tostr(int64_t n); | |
57 | std::string itostr(int n); | |
58 | int64_t atoi64(const char* psz); | |
59 | int64_t atoi64(const std::string& str); | |
60 | int atoi(const std::string& str); | |
61 | ||
62 | /** | |
63 | * Convert string to signed 32-bit integer with strict parse error feedback. | |
64 | * @returns true if the entire string could be parsed as valid integer, | |
c63a73d1 | 65 | * false if not the entire string could be parsed or when overflow or underflow occurred. |
ad49c256 WL |
66 | */ |
67 | bool ParseInt32(const std::string& str, int32_t *out); | |
68 | ||
5960d700 WL |
69 | /** |
70 | * Convert string to signed 64-bit integer with strict parse error feedback. | |
71 | * @returns true if the entire string could be parsed as valid integer, | |
72 | * false if not the entire string could be parsed or when overflow or underflow occurred. | |
73 | */ | |
74 | bool ParseInt64(const std::string& str, int64_t *out); | |
75 | ||
76 | /** | |
77 | * Convert string to double with strict parse error feedback. | |
78 | * @returns true if the entire string could be parsed as valid double, | |
79 | * false if not the entire string could be parsed or when overflow or underflow occurred. | |
80 | */ | |
81 | bool ParseDouble(const std::string& str, double *out); | |
82 | ||
ad49c256 WL |
83 | template<typename T> |
84 | std::string HexStr(const T itbegin, const T itend, bool fSpaces=false) | |
85 | { | |
86 | std::string rv; | |
87 | static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', | |
88 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; | |
89 | rv.reserve((itend-itbegin)*3); | |
90 | for(T it = itbegin; it < itend; ++it) | |
91 | { | |
92 | unsigned char val = (unsigned char)(*it); | |
93 | if(fSpaces && it != itbegin) | |
94 | rv.push_back(' '); | |
95 | rv.push_back(hexmap[val>>4]); | |
96 | rv.push_back(hexmap[val&15]); | |
97 | } | |
98 | ||
99 | return rv; | |
100 | } | |
101 | ||
102 | template<typename T> | |
103 | inline std::string HexStr(const T& vch, bool fSpaces=false) | |
104 | { | |
105 | return HexStr(vch.begin(), vch.end(), fSpaces); | |
106 | } | |
107 | ||
db954a65 | 108 | /** |
c63a73d1 | 109 | * Format a paragraph of text to a fixed width, adding spaces for |
ad49c256 WL |
110 | * indentation to any added line. |
111 | */ | |
db954a65 | 112 | std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0); |
ad49c256 WL |
113 | |
114 | /** | |
115 | * Timing-attack-resistant comparison. | |
116 | * Takes time proportional to length | |
117 | * of first argument. | |
118 | */ | |
119 | template <typename T> | |
120 | bool TimingResistantEqual(const T& a, const T& b) | |
121 | { | |
122 | if (b.size() == 0) return a.size() == 0; | |
123 | size_t accumulator = a.size() ^ b.size(); | |
124 | for (size_t i = 0; i < a.size(); i++) | |
125 | accumulator |= a[i] ^ b[i%b.size()]; | |
126 | return accumulator == 0; | |
127 | } | |
128 | ||
fed500e2 WL |
129 | /** Parse number as fixed point according to JSON number syntax. |
130 | * See http://json.org/number.gif | |
131 | * @returns true on success, false on error. | |
132 | * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger. | |
133 | */ | |
134 | bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); | |
135 | ||
ad49c256 | 136 | #endif // BITCOIN_UTILSTRENCODINGS_H |