Bitcoin Core  0.18.99
P2P Digital Currency
strencodings.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_UTIL_STRENCODINGS_H
10 #define BITCOIN_UTIL_STRENCODINGS_H
11 
12 #include <attributes.h>
13 
14 #include <cstdint>
15 #include <iterator>
16 #include <string>
17 #include <vector>
18 
19 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
20 
23 {
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* pf_invalid = nullptr);
49 std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
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* pf_invalid = nullptr);
53 std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
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 NODISCARD bool ParseInt32(const std::string& str, int32_t *out);
95 
101 NODISCARD bool ParseInt64(const std::string& str, int64_t *out);
102 
108 NODISCARD bool ParseUInt32(const std::string& str, uint32_t *out);
109 
115 NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
116 
122 NODISCARD bool ParseDouble(const std::string& str, double *out);
123 
124 template<typename T>
125 std::string HexStr(const T itbegin, const T itend)
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(std::distance(itbegin, itend) * 2);
131  for(T it = itbegin; it < itend; ++it)
132  {
133  unsigned char val = (unsigned char)(*it);
134  rv.push_back(hexmap[val>>4]);
135  rv.push_back(hexmap[val&15]);
136  }
137  return rv;
138 }
139 
140 template<typename T>
141 inline std::string HexStr(const T& vch)
142 {
143  return HexStr(vch.begin(), vch.end());
144 }
145 
150 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
151 
157 template <typename T>
158 bool TimingResistantEqual(const T& a, const T& b)
159 {
160  if (b.size() == 0) return a.size() == 0;
161  size_t accumulator = a.size() ^ b.size();
162  for (size_t i = 0; i < a.size(); i++)
163  accumulator |= a[i] ^ b[i%b.size()];
164  return accumulator == 0;
165 }
166 
172 NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
173 
175 template<int frombits, int tobits, bool pad, typename O, typename I>
176 bool ConvertBits(const O& outfn, I it, I end) {
177  size_t acc = 0;
178  size_t bits = 0;
179  constexpr size_t maxv = (1 << tobits) - 1;
180  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
181  while (it != end) {
182  acc = ((acc << frombits) | *it) & max_acc;
183  bits += frombits;
184  while (bits >= tobits) {
185  bits -= tobits;
186  outfn((acc >> bits) & maxv);
187  }
188  ++it;
189  }
190  if (pad) {
191  if (bits) outfn((acc << (tobits - bits)) & maxv);
192  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
193  return false;
194  }
195  return true;
196 }
197 
206 constexpr char ToLower(char c)
207 {
208  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
209 }
210 
217 void Downcase(std::string& str);
218 
227 constexpr char ToUpper(char c)
228 {
229  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
230 }
231 
240 std::string Capitalize(std::string str);
241 
242 #endif // BITCOIN_UTIL_STRENCODINGS_H
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:158
The full set of allowed chars.
Definition: strencodings.h:24
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
Definition: strencodings.h:227
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
std::vector< unsigned char > ParseHex(const char *psz)
void Downcase(std::string &str)
Converts the given string to its lowercase equivalent.
signed char HexDigit(char c)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:69
NODISCARD 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)
std::string i64tostr(int64_t n)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid=nullptr)
std::string EncodeBase32(const unsigned char *pch, size_t len)
#define NODISCARD
Definition: attributes.h:18
SafeChars
Used by SanitizeString()
Definition: strencodings.h:22
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
Definition: strencodings.h:176
Chars allowed in filenames.
Definition: strencodings.h:26
NODISCARD bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
NODISCARD bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
std::string itostr(int n)
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:85
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
NODISCARD bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
int64_t atoi64(const char *psz)
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:125
std::string EncodeBase64(const unsigned char *pch, size_t len)
int atoi(const std::string &str)
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:206
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:27
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...
NODISCARD bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
auto it
Definition: validation.cpp:355
BIP-0014 subset.
Definition: strencodings.h:25
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid=nullptr)
NODISCARD bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.