]>
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 | ||
9064d73b | 25 | std::string SanitizeFilename(const std::string& str); |
ad49c256 WL |
26 | std::string SanitizeString(const std::string& str); |
27 | std::vector<unsigned char> ParseHex(const char* psz); | |
28 | std::vector<unsigned char> ParseHex(const std::string& str); | |
29 | signed char HexDigit(char c); | |
30 | bool IsHex(const std::string& str); | |
31 | std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL); | |
32 | std::string DecodeBase64(const std::string& str); | |
33 | std::string EncodeBase64(const unsigned char* pch, size_t len); | |
34 | std::string EncodeBase64(const std::string& str); | |
35 | std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL); | |
36 | std::string DecodeBase32(const std::string& str); | |
37 | std::string EncodeBase32(const unsigned char* pch, size_t len); | |
38 | std::string EncodeBase32(const std::string& str); | |
39 | ||
40 | std::string i64tostr(int64_t n); | |
41 | std::string itostr(int n); | |
42 | int64_t atoi64(const char* psz); | |
43 | int64_t atoi64(const std::string& str); | |
44 | int atoi(const std::string& str); | |
45 | ||
46 | /** | |
47 | * Convert string to signed 32-bit integer with strict parse error feedback. | |
48 | * @returns true if the entire string could be parsed as valid integer, | |
c63a73d1 | 49 | * false if not the entire string could be parsed or when overflow or underflow occurred. |
ad49c256 WL |
50 | */ |
51 | bool ParseInt32(const std::string& str, int32_t *out); | |
52 | ||
5960d700 WL |
53 | /** |
54 | * Convert string to signed 64-bit integer with strict parse error feedback. | |
55 | * @returns true if the entire string could be parsed as valid integer, | |
56 | * false if not the entire string could be parsed or when overflow or underflow occurred. | |
57 | */ | |
58 | bool ParseInt64(const std::string& str, int64_t *out); | |
59 | ||
60 | /** | |
61 | * Convert string to double with strict parse error feedback. | |
62 | * @returns true if the entire string could be parsed as valid double, | |
63 | * false if not the entire string could be parsed or when overflow or underflow occurred. | |
64 | */ | |
65 | bool ParseDouble(const std::string& str, double *out); | |
66 | ||
ad49c256 WL |
67 | template<typename T> |
68 | std::string HexStr(const T itbegin, const T itend, bool fSpaces=false) | |
69 | { | |
70 | std::string rv; | |
71 | static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', | |
72 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; | |
73 | rv.reserve((itend-itbegin)*3); | |
74 | for(T it = itbegin; it < itend; ++it) | |
75 | { | |
76 | unsigned char val = (unsigned char)(*it); | |
77 | if(fSpaces && it != itbegin) | |
78 | rv.push_back(' '); | |
79 | rv.push_back(hexmap[val>>4]); | |
80 | rv.push_back(hexmap[val&15]); | |
81 | } | |
82 | ||
83 | return rv; | |
84 | } | |
85 | ||
86 | template<typename T> | |
87 | inline std::string HexStr(const T& vch, bool fSpaces=false) | |
88 | { | |
89 | return HexStr(vch.begin(), vch.end(), fSpaces); | |
90 | } | |
91 | ||
c63a73d1 MF |
92 | /** |
93 | * Format a paragraph of text to a fixed width, adding spaces for | |
ad49c256 WL |
94 | * indentation to any added line. |
95 | */ | |
96 | std::string FormatParagraph(const std::string in, size_t width=79, size_t indent=0); | |
97 | ||
98 | /** | |
99 | * Timing-attack-resistant comparison. | |
100 | * Takes time proportional to length | |
101 | * of first argument. | |
102 | */ | |
103 | template <typename T> | |
104 | bool TimingResistantEqual(const T& a, const T& b) | |
105 | { | |
106 | if (b.size() == 0) return a.size() == 0; | |
107 | size_t accumulator = a.size() ^ b.size(); | |
108 | for (size_t i = 0; i < a.size(); i++) | |
109 | accumulator |= a[i] ^ b[i%b.size()]; | |
110 | return accumulator == 0; | |
111 | } | |
112 | ||
fed500e2 WL |
113 | /** Parse number as fixed point according to JSON number syntax. |
114 | * See http://json.org/number.gif | |
115 | * @returns true on success, false on error. | |
116 | * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger. | |
117 | */ | |
118 | bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); | |
119 | ||
ad49c256 | 120 | #endif // BITCOIN_UTILSTRENCODINGS_H |