BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
utilstrencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 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 
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 constexpr bool IsDigit(char c)
70 {
71  return c >= '0' && c <= '9';
72 }
73 
85 constexpr inline bool IsSpace(char c) noexcept {
86  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
87 }
88 
94 bool ParseInt32(const std::string& str, int32_t *out);
95 
101 bool ParseInt64(const std::string& str, int64_t *out);
102 
108 bool ParseUInt32(const std::string& str, uint32_t *out);
109 
115 bool ParseUInt64(const std::string& str, uint64_t *out);
116 
122 bool ParseDouble(const std::string& str, double *out);
123 
124 template<typename T>
125 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
126 {
127  std::string rv;
128  static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
129  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
130  rv.reserve((itend-itbegin)*3);
131  for(T it = itbegin; it < itend; ++it)
132  {
133  unsigned char val = (unsigned char)(*it);
134  if(fSpaces && it != itbegin)
135  rv.push_back(' ');
136  rv.push_back(hexmap[val>>4]);
137  rv.push_back(hexmap[val&15]);
138  }
139 
140  return rv;
141 }
142 
143 template<typename T>
144 inline std::string HexStr(const T& vch, bool fSpaces=false)
145 {
146  return HexStr(vch.begin(), vch.end(), fSpaces);
147 }
148 
153 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
154 
160 template <typename T>
161 bool TimingResistantEqual(const T& a, const T& b)
162 {
163  if (b.size() == 0) return a.size() == 0;
164  size_t accumulator = a.size() ^ b.size();
165  for (size_t i = 0; i < a.size(); i++)
166  accumulator |= a[i] ^ b[i%b.size()];
167  return accumulator == 0;
168 }
169 
175 bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
176 
178 template<int frombits, int tobits, bool pad, typename O, typename I>
179 bool ConvertBits(const O& outfn, I it, I end) {
180  size_t acc = 0;
181  size_t bits = 0;
182  constexpr size_t maxv = (1 << tobits) - 1;
183  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
184  while (it != end) {
185  acc = ((acc << frombits) | *it) & max_acc;
186  bits += frombits;
187  while (bits >= tobits) {
188  bits -= tobits;
189  outfn((acc >> bits) & maxv);
190  }
191  ++it;
192  }
193  if (pad) {
194  if (bits) outfn((acc << (tobits - bits)) & maxv);
195  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
196  return false;
197  }
198  return true;
199 }
200 
202 bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);
203 
212 constexpr unsigned char ToLower(unsigned char c)
213 {
214  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
215 }
216 
223 void Downcase(std::string& str);
224 
233 constexpr unsigned char ToUpper(unsigned char c)
234 {
235  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
236 }
237 
246 std::string Capitalize(std::string str);
247 
248 #endif // BITCOIN_UTILSTRENCODINGS_H
void Downcase(std::string &str)
Converts the given string to its lowercase equivalent.
constexpr unsigned char ToLower(unsigned char c)
Converts the given character to its lowercase equivalent.
std::string itostr(int n)
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
The full set of allowed chars.
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)
constexpr unsigned char ToUpper(unsigned char c)
Converts the given character to its uppercase equivalent.
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.
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.
auto end
Definition: rpcwallet.cpp:1068
bool IsHex(const std::string &str)
BIP-0014 subset.
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)
bool ParseHDKeypath(const std::string &keypath_str, std::vector< uint32_t > &keypath)
Parse an HD keypaths like "m/7/0&#39;/2000".
std::string EncodeBase32(const unsigned char *pch, size_t len)
SafeChars
Used by SanitizeString()
int atoi(const std::string &str)
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
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...