Bitcoin ABC  0.22.12
P2P Digital Currency
strencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 
22 enum SafeChars {
31 };
32 
41 std::string SanitizeString(const std::string &str,
42  int rule = SAFE_CHARS_DEFAULT);
43 std::vector<uint8_t> ParseHex(const char *psz);
44 std::vector<uint8_t> ParseHex(const std::string &str);
45 signed char HexDigit(char c);
50 bool IsHex(const std::string &str);
54 bool IsHexNumber(const std::string &str);
55 std::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid = nullptr);
56 std::string DecodeBase64(const std::string &str, bool *pf_invalid = nullptr);
57 std::string EncodeBase64(const uint8_t *pch, size_t len);
58 std::string EncodeBase64(const std::string &str);
59 std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid = nullptr);
60 std::string DecodeBase32(const std::string &str, bool *pf_invalid = nullptr);
61 std::string EncodeBase32(const uint8_t *pch, size_t len);
62 std::string EncodeBase32(const std::string &str);
63 
64 void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
65 int64_t atoi64(const char *psz);
66 int64_t atoi64(const std::string &str);
67 int atoi(const std::string &str);
68 
74 constexpr bool IsDigit(char c) {
75  return c >= '0' && c <= '9';
76 }
77 
90 constexpr inline bool IsSpace(char c) noexcept {
91  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
92  c == '\v';
93 }
94 
100 NODISCARD bool ParseInt32(const std::string &str, int32_t *out);
101 
107 NODISCARD bool ParseInt64(const std::string &str, int64_t *out);
108 
115 NODISCARD bool ParseUInt32(const std::string &str, uint32_t *out);
116 
123 NODISCARD bool ParseUInt64(const std::string &str, uint64_t *out);
124 
130 NODISCARD bool ParseDouble(const std::string &str, double *out);
131 
132 template <typename T> std::string HexStr(const T itbegin, const T itend) {
133  std::string rv;
134  static const char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
135  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
136  rv.reserve(std::distance(itbegin, itend) * 2);
137  for (T it = itbegin; it < itend; ++it) {
138  uint8_t val = uint8_t(*it);
139  rv.push_back(hexmap[val >> 4]);
140  rv.push_back(hexmap[val & 15]);
141  }
142  return rv;
143 }
144 
145 template <typename T> inline std::string HexStr(const T &vch) {
146  return HexStr(vch.begin(), vch.end());
147 }
148 
153 std::string FormatParagraph(const std::string &in, size_t width = 79,
154  size_t indent = 0);
155 
160 template <typename T> bool TimingResistantEqual(const T &a, const T &b) {
161  if (b.size() == 0) return a.size() == 0;
162  size_t accumulator = a.size() ^ b.size();
163  for (size_t i = 0; i < a.size(); i++)
164  accumulator |= a[i] ^ b[i % b.size()];
165  return accumulator == 0;
166 }
167 
175 NODISCARD bool ParseFixedPoint(const std::string &val, int decimals,
176  int64_t *amount_out);
177 
184 template <int frombits, int tobits, bool pad, typename O, typename I>
185 bool ConvertBits(const O &outfn, I it, I end) {
186  size_t acc = 0;
187  size_t bits = 0;
188  constexpr size_t maxv = (1 << tobits) - 1;
189  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
190  while (it != end) {
191  acc = ((acc << frombits) | *it) & max_acc;
192  bits += frombits;
193  while (bits >= tobits) {
194  bits -= tobits;
195  outfn((acc >> bits) & maxv);
196  }
197  ++it;
198  }
199 
200  if (pad) {
201  if (bits) {
202  outfn((acc << (tobits - bits)) & maxv);
203  }
204  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
205  return false;
206  }
207 
208  return true;
209 }
210 
221 constexpr char ToLower(char c) {
222  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
223 }
224 
234 std::string ToLower(const std::string &str);
235 
246 constexpr char ToUpper(char c) {
247  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
248 }
249 
259 std::string ToUpper(const std::string &str);
260 
270 std::string Capitalize(std::string str);
271 
272 #endif // BITCOIN_UTIL_STRENCODINGS_H
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:160
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:246
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
signed char HexDigit(char c)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:74
std::vector< uint8_t > ParseHex(const char *psz)
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)
Returns true if each character in str is a hex character, and has an even number of hex digits...
#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 EncodeBase32(const uint8_t *pch, size_t len)
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:185
std::vector< uint8_t > DecodeBase64(const char *p, bool *pf_invalid=nullptr)
Chars allowed in filenames.
Definition: strencodings.h:28
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid=nullptr)
NODISCARD bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
std::string EncodeBase64(const uint8_t *pch, size_t len)
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:90
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:132
int atoi(const std::string &str)
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:221
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:30
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.
BIP-0014 subset.
Definition: strencodings.h:26
NODISCARD bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.