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]))
23 #define PAIRTYPE(t1, t2) std::pair<t1, t2>
40 std::vector<unsigned char>
ParseHex(
const char* psz);
41 std::vector<unsigned char>
ParseHex(
const std::string& str);
43 bool IsHex(
const std::string& str);
44 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid = NULL);
46 std::string
EncodeBase64(
const unsigned char* pch,
size_t len);
48 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid = NULL);
50 std::string
EncodeBase32(
const unsigned char* pch,
size_t len);
55 int64_t
atoi64(
const char* psz);
56 int64_t
atoi64(
const std::string& str);
57 int atoi(
const std::string& str);
64 bool ParseInt32(
const std::string& str, int32_t *out);
71 bool ParseInt64(
const std::string& str, int64_t *out);
78 bool ParseUInt32(
const std::string& str, uint32_t *out);
85 bool ParseUInt64(
const std::string& str, uint64_t *out);
92 bool ParseDouble(
const std::string& str,
double *out);
95 std::string
HexStr(
const T itbegin,
const T itend,
bool fSpaces=
false)
98 static const char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
99 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
100 rv.reserve((itend-itbegin)*3);
101 for(T it = itbegin; it < itend; ++it)
103 unsigned char val = (
unsigned char)(*it);
104 if(fSpaces && it != itbegin)
106 rv.push_back(hexmap[val>>4]);
107 rv.push_back(hexmap[val&15]);
114 inline std::string
HexStr(
const T& vch,
bool fSpaces=
false)
116 return HexStr(vch.begin(), vch.end(), fSpaces);
123 std::string
FormatParagraph(
const std::string& in,
size_t width = 79,
size_t indent = 0);
130 template <
typename T>
133 if (b.size() == 0)
return a.size() == 0;
134 size_t accumulator = a.size() ^ b.size();
135 for (
size_t i = 0; i < a.size(); i++)
136 accumulator |= a[i] ^ b[i%b.size()];
137 return accumulator == 0;
145 bool ParseFixedPoint(
const std::string &val,
int decimals, int64_t *amount_out);
std::string FormatParagraph(const std::string &in, size_t width=79, size_t indent=0)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
int64_t atoi64(const char *psz)
std::string itostr(int n)
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid=NULL)
std::vector< unsigned char > ParseHex(const char *psz)
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool IsHex(const std::string &str)
signed char HexDigit(char c)
int atoi(const std::string &str)
std::string EncodeBase64(const unsigned char *pch, size_t len)
std::string i64tostr(int64_t n)
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid=NULL)
SafeChars
Used by SanitizeString()
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
std::string EncodeBase32(const unsigned char *pch, size_t len)