Fabcoin Core  0.16.2
P2P Digital Currency
utilstrencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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.
5 
9 #ifndef FABCOIN_UTILSTRENCODINGS_H
10 #define FABCOIN_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 
24 {
28 };
29 
37 std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
38 std::vector<unsigned char> ParseHex(const char* psz);
39 std::vector<unsigned char> ParseHex(const std::string& str);
40 signed char HexDigit(char c);
41 /* Returns true if each character in str is a hex character, and has an even
42  * number of hex digits.*/
43 bool IsHex(const std::string& str);
47 bool IsHexNumber(const std::string& str);
48 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = nullptr);
49 std::string DecodeBase64(const std::string& str);
50 std::string EncodeBase64(const unsigned char* pch, size_t len);
51 std::string EncodeBase64(const std::string& str);
52 std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = nullptr);
53 std::string DecodeBase32(const std::string& str);
54 std::string EncodeBase32(const unsigned char* pch, size_t len);
55 std::string EncodeBase32(const std::string& str);
56 
57 void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
58 std::string i64tostr(int64_t n);
59 std::string itostr(int n);
60 int64_t atoi64(const char* psz);
61 int64_t atoi64(const std::string& str);
62 int atoi(const std::string& str);
63 
69 bool ParseInt32(const std::string& str, int32_t *out);
70 
76 bool ParseInt64(const std::string& str, int64_t *out);
77 
83 bool ParseUInt32(const std::string& str, uint32_t *out);
84 
90 bool ParseUInt64(const std::string& str, uint64_t *out);
91 
97 bool ParseDouble(const std::string& str, double *out);
98 
99 template<typename T>
100 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
101 {
102  std::string rv;
103  static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
104  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
105  rv.reserve((itend-itbegin)*3);
106  for(T it = itbegin; it < itend; ++it)
107  {
108  unsigned char val = (unsigned char)(*it);
109  if(fSpaces && it != itbegin)
110  rv.push_back(' ');
111  rv.push_back(hexmap[val>>4]);
112  rv.push_back(hexmap[val&15]);
113  }
114 
115  return rv;
116 }
117 
118 template<typename T>
119 inline std::string HexStr(const T& vch, bool fSpaces=false)
120 {
121  return HexStr(vch.begin(), vch.end(), fSpaces);
122 }
123 
128 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
129 
135 template <typename T>
136 bool TimingResistantEqual(const T& a, const T& b)
137 {
138  if (b.size() == 0) return a.size() == 0;
139  size_t accumulator = a.size() ^ b.size();
140  for (size_t i = 0; i < a.size(); i++)
141  accumulator |= a[i] ^ b[i%b.size()];
142  return accumulator == 0;
143 }
144 
150 bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
151 
152 #endif // FABCOIN_UTILSTRENCODINGS_H
std::string itostr(int n)
#define T(i, x)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
The full set of allowed chars.
#define c(i)
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::string EncodeBase64(const unsigned char *pch, size_t len)
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
int64_t atoi64(const char *psz)
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid=nullptr)
signed char HexDigit(char c)
std::vector< unsigned char > ParseHex(const char *psz)
std::string i64tostr(int64_t n)
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.
#define a(i)
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
Chars allowed in filenames.
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid=nullptr)
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)
BIP-0014 subset.
#define b(i, j)
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
std::string EncodeBase32(const unsigned char *pch, size_t len)
SafeChars
Used by SanitizeString()
int atoi(const std::string &str)
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...