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