Bitcoin Core  0.20.99
P2P Digital Currency
strencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 int64_t atoi64(const char* psz);
59 int64_t atoi64(const std::string& str);
60 int atoi(const std::string& str);
61 
67 constexpr bool IsDigit(char c)
68 {
69  return c >= '0' && c <= '9';
70 }
71 
83 constexpr inline bool IsSpace(char c) noexcept {
84  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
85 }
86 
92 NODISCARD bool ParseInt32(const std::string& str, int32_t *out);
93 
99 NODISCARD bool ParseInt64(const std::string& str, int64_t *out);
100 
106 NODISCARD bool ParseUInt32(const std::string& str, uint32_t *out);
107 
113 NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
114 
120 NODISCARD bool ParseDouble(const std::string& str, double *out);
121 
122 template<typename T>
123 std::string HexStr(const T itbegin, const T itend)
124 {
125  std::string rv;
126  static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
127  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
128  rv.reserve(std::distance(itbegin, itend) * 2);
129  for(T it = itbegin; it < itend; ++it)
130  {
131  unsigned char val = (unsigned char)(*it);
132  rv.push_back(hexmap[val>>4]);
133  rv.push_back(hexmap[val&15]);
134  }
135  return rv;
136 }
137 
138 template<typename T>
139 inline std::string HexStr(const T& vch)
140 {
141  return HexStr(vch.begin(), vch.end());
142 }
143 
148 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
149 
155 template <typename T>
156 bool TimingResistantEqual(const T& a, const T& b)
157 {
158  if (b.size() == 0) return a.size() == 0;
159  size_t accumulator = a.size() ^ b.size();
160  for (size_t i = 0; i < a.size(); i++)
161  accumulator |= a[i] ^ b[i%b.size()];
162  return accumulator == 0;
163 }
164 
170 NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
171 
173 template<int frombits, int tobits, bool pad, typename O, typename I>
174 bool ConvertBits(const O& outfn, I it, I end) {
175  size_t acc = 0;
176  size_t bits = 0;
177  constexpr size_t maxv = (1 << tobits) - 1;
178  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
179  while (it != end) {
180  acc = ((acc << frombits) | *it) & max_acc;
181  bits += frombits;
182  while (bits >= tobits) {
183  bits -= tobits;
184  outfn((acc >> bits) & maxv);
185  }
186  ++it;
187  }
188  if (pad) {
189  if (bits) outfn((acc << (tobits - bits)) & maxv);
190  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
191  return false;
192  }
193  return true;
194 }
195 
206 constexpr char ToLower(char c)
207 {
208  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
209 }
210 
220 std::string ToLower(const std::string& str);
221 
232 constexpr char ToUpper(char c)
233 {
234  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
235 }
236 
246 std::string ToUpper(const std::string& str);
247 
257 std::string Capitalize(std::string str);
258 
259 #endif // BITCOIN_UTIL_STRENCODINGS_H
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:156
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:232
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
std::vector< unsigned char > ParseHex(const char *psz)
signed char HexDigit(char c)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:67
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::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:174
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.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:83
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:123
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:380
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.