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 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 /** 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>
25 std::string SanitizeFilename(const std::string& str);
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);
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);
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,
49 * false if not the entire string could be parsed or when overflow or underflow occurred.
51 bool ParseInt32(const std::string& str, int32_t *out);
54 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
57 static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
58 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
59 rv.reserve((itend-itbegin)*3);
60 for(T it = itbegin; it < itend; ++it)
62 unsigned char val = (unsigned char)(*it);
63 if(fSpaces && it != itbegin)
65 rv.push_back(hexmap[val>>4]);
66 rv.push_back(hexmap[val&15]);
73 inline std::string HexStr(const T& vch, bool fSpaces=false)
75 return HexStr(vch.begin(), vch.end(), fSpaces);
79 * Format a paragraph of text to a fixed width, adding spaces for
80 * indentation to any added line.
82 std::string FormatParagraph(const std::string in, size_t width=79, size_t indent=0);
85 * Timing-attack-resistant comparison.
86 * Takes time proportional to length
90 bool TimingResistantEqual(const T& a, const T& b)
92 if (b.size() == 0) return a.size() == 0;
93 size_t accumulator = a.size() ^ b.size();
94 for (size_t i = 0; i < a.size(); i++)
95 accumulator |= a[i] ^ b[i%b.size()];
96 return accumulator == 0;
99 #endif // BITCOIN_UTILSTRENCODINGS_H