Bitcoin ABC  0.26.3
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 <span.h>
13 
14 #include <cstdint>
15 #include <iterator>
16 #include <string>
17 #include <vector>
18 
20 enum SafeChars {
29 };
30 
39 std::string SanitizeString(const std::string &str,
40  int rule = SAFE_CHARS_DEFAULT);
41 std::vector<uint8_t> ParseHex(const char *psz);
42 std::vector<uint8_t> ParseHex(const std::string &str);
43 signed char HexDigit(char c);
48 bool IsHex(const std::string &str);
52 bool IsHexNumber(const std::string &str);
53 std::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid = nullptr);
54 std::string DecodeBase64(const std::string &str, bool *pf_invalid = nullptr);
55 std::string EncodeBase64(Span<const uint8_t> input);
56 inline std::string EncodeBase64(Span<const std::byte> input) {
57  return EncodeBase64(MakeUCharSpan(input));
58 }
59 inline std::string EncodeBase64(const std::string &str) {
60  return EncodeBase64(MakeUCharSpan(str));
61 }
62 std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid = nullptr);
63 std::string DecodeBase32(const std::string &str, bool *pf_invalid = nullptr);
64 
70 std::string EncodeBase32(Span<const uint8_t> input, bool pad = true);
71 
77 std::string EncodeBase32(const std::string &str, bool pad = true);
78 
79 void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut);
80 int64_t atoi64(const std::string &str);
81 int atoi(const std::string &str);
82 
88 constexpr bool IsDigit(char c) {
89  return c >= '0' && c <= '9';
90 }
91 
104 constexpr inline bool IsSpace(char c) noexcept {
105  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
106  c == '\v';
107 }
108 
114 [[nodiscard]] bool ParseInt32(const std::string &str, int32_t *out);
115 
121 [[nodiscard]] bool ParseInt64(const std::string &str, int64_t *out);
122 
130 [[nodiscard]] bool ParseUInt8(const std::string &str, uint8_t *out);
131 
139 [[nodiscard]] bool ParseUInt16(const std::string &str, uint16_t *out);
140 
147 [[nodiscard]] bool ParseUInt32(const std::string &str, uint32_t *out);
148 
155 [[nodiscard]] bool ParseUInt64(const std::string &str, uint64_t *out);
156 
162 [[nodiscard]] bool ParseDouble(const std::string &str, double *out);
163 
167 std::string HexStr(const Span<const uint8_t> s);
168 inline std::string HexStr(const Span<const char> s) {
169  return HexStr(MakeUCharSpan(s));
170 }
171 inline std::string HexStr(const Span<const std::byte> s) {
172  return HexStr(MakeUCharSpan(s));
173 }
174 
179 std::string FormatParagraph(const std::string &in, size_t width = 79,
180  size_t indent = 0);
181 
186 template <typename T> bool TimingResistantEqual(const T &a, const T &b) {
187  if (b.size() == 0) {
188  return a.size() == 0;
189  }
190  size_t accumulator = a.size() ^ b.size();
191  for (size_t i = 0; i < a.size(); i++) {
192  accumulator |= size_t(a[i] ^ b[i % b.size()]);
193  }
194  return accumulator == 0;
195 }
196 
204 [[nodiscard]] bool ParseFixedPoint(const std::string &val, int decimals,
205  int64_t *amount_out);
206 
213 template <int frombits, int tobits, bool pad, typename O, typename I>
214 bool ConvertBits(const O &outfn, I it, I end) {
215  size_t acc = 0;
216  size_t bits = 0;
217  constexpr size_t maxv = (1 << tobits) - 1;
218  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
219  while (it != end) {
220  acc = ((acc << frombits) | *it) & max_acc;
221  bits += frombits;
222  while (bits >= tobits) {
223  bits -= tobits;
224  outfn((acc >> bits) & maxv);
225  }
226  ++it;
227  }
228 
229  if (pad) {
230  if (bits) {
231  outfn((acc << (tobits - bits)) & maxv);
232  }
233  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
234  return false;
235  }
236 
237  return true;
238 }
239 
250 constexpr char ToLower(char c) {
251  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
252 }
253 
263 std::string ToLower(const std::string &str);
264 
275 constexpr char ToUpper(char c) {
276  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
277 }
278 
288 std::string ToUpper(const std::string &str);
289 
299 std::string Capitalize(std::string str);
300 
301 #endif // BITCOIN_UTIL_STRENCODINGS_H
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
Definition: span.h:337
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.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string EncodeBase64(Span< const uint8_t > input)
std::string EncodeBase32(Span< const uint8_t > input, bool pad=true)
Base32 encode.
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:250
bool ParseUInt16(const std::string &str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:88
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
Definition: strencodings.h:214
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
Definition: strencodings.h:275
std::vector< uint8_t > DecodeBase64(const char *p, bool *pf_invalid=nullptr)
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid=nullptr)
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.
Definition: strencodings.h:186
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
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.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:104
signed char HexDigit(char c)
int atoi(const std::string &str)
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
int64_t atoi64(const std::string &str)
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
SafeChars
Utilities for converting data from/to strings.
Definition: strencodings.h:20
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
Definition: strencodings.h:22
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
Definition: strencodings.h:24
@ SAFE_CHARS_URI
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:28
@ SAFE_CHARS_FILENAME
Chars allowed in filenames.
Definition: strencodings.h:26
std::vector< uint8_t > ParseHex(const char *psz)