Bitcoin ABC  0.24.7
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 #include <span.h>
14 
15 #include <cstdint>
16 #include <iterator>
17 #include <string>
18 #include <vector>
19 
20 #define ARRAYLEN(array) (sizeof(array) / sizeof((array)[0]))
21 
23 enum SafeChars {
32 };
33 
42 std::string SanitizeString(const std::string &str,
43  int rule = SAFE_CHARS_DEFAULT);
44 std::vector<uint8_t> ParseHex(const char *psz);
45 std::vector<uint8_t> ParseHex(const std::string &str);
46 signed char HexDigit(char c);
51 bool IsHex(const std::string &str);
55 bool IsHexNumber(const std::string &str);
56 std::vector<uint8_t> DecodeBase64(const char *p, bool *pf_invalid = nullptr);
57 std::string DecodeBase64(const std::string &str, bool *pf_invalid = nullptr);
58 std::string EncodeBase64(Span<const uint8_t> input);
59 std::string EncodeBase64(const std::string &str);
60 std::vector<uint8_t> DecodeBase32(const char *p, bool *pf_invalid = nullptr);
61 std::string DecodeBase32(const std::string &str, bool *pf_invalid = nullptr);
62 
68 std::string EncodeBase32(Span<const uint8_t> input, bool pad = true);
69 
75 std::string EncodeBase32(const std::string &str, bool pad = true);
76 
77 void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
78 int64_t atoi64(const std::string &str);
79 int atoi(const std::string &str);
80 
86 constexpr bool IsDigit(char c) {
87  return c >= '0' && c <= '9';
88 }
89 
102 constexpr inline bool IsSpace(char c) noexcept {
103  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
104  c == '\v';
105 }
106 
112 NODISCARD bool ParseInt32(const std::string &str, int32_t *out);
113 
119 NODISCARD bool ParseInt64(const std::string &str, int64_t *out);
120 
128 NODISCARD bool ParseUInt8(const std::string &str, uint8_t *out);
129 
136 NODISCARD bool ParseUInt32(const std::string &str, uint32_t *out);
137 
144 NODISCARD bool ParseUInt64(const std::string &str, uint64_t *out);
145 
151 NODISCARD bool ParseDouble(const std::string &str, double *out);
152 
156 std::string HexStr(const Span<const uint8_t> s);
157 inline std::string HexStr(const Span<const char> s) {
158  return HexStr(MakeUCharSpan(s));
159 }
160 
165 std::string FormatParagraph(const std::string &in, size_t width = 79,
166  size_t indent = 0);
167 
172 template <typename T> bool TimingResistantEqual(const T &a, const T &b) {
173  if (b.size() == 0) return a.size() == 0;
174  size_t accumulator = a.size() ^ b.size();
175  for (size_t i = 0; i < a.size(); i++)
176  accumulator |= a[i] ^ b[i % b.size()];
177  return accumulator == 0;
178 }
179 
187 NODISCARD bool ParseFixedPoint(const std::string &val, int decimals,
188  int64_t *amount_out);
189 
196 template <int frombits, int tobits, bool pad, typename O, typename I>
197 bool ConvertBits(const O &outfn, I it, I end) {
198  size_t acc = 0;
199  size_t bits = 0;
200  constexpr size_t maxv = (1 << tobits) - 1;
201  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
202  while (it != end) {
203  acc = ((acc << frombits) | *it) & max_acc;
204  bits += frombits;
205  while (bits >= tobits) {
206  bits -= tobits;
207  outfn((acc >> bits) & maxv);
208  }
209  ++it;
210  }
211 
212  if (pad) {
213  if (bits) {
214  outfn((acc << (tobits - bits)) & maxv);
215  }
216  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
217  return false;
218  }
219 
220  return true;
221 }
222 
233 constexpr char ToLower(char c) {
234  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
235 }
236 
246 std::string ToLower(const std::string &str);
247 
258 constexpr char ToUpper(char c) {
259  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
260 }
261 
271 std::string ToUpper(const std::string &str);
272 
282 std::string Capitalize(std::string str);
283 
284 #endif // BITCOIN_UTIL_STRENCODINGS_H
SanitizeString
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
Definition: strencodings.cpp:31
ConvertBits
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
Definition: strencodings.h:197
Capitalize
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
Definition: strencodings.cpp:648
ParseUInt64
NODISCARD bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:388
MakeUCharSpan
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) uint8_t member types only.
Definition: span.h:280
ParseUInt32
NODISCARD bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:364
DecodeBase64
std::vector< uint8_t > DecodeBase64(const char *p, bool *pf_invalid=nullptr)
Definition: strencodings.cpp:155
NODISCARD
#define NODISCARD
Definition: attributes.h:18
IsHexNumber
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
Definition: strencodings.cpp:73
ParseInt64
NODISCARD bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:334
DecodeBase32
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid=nullptr)
Definition: strencodings.cpp:236
SAFE_CHARS_UA_COMMENT
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
Definition: strencodings.h:27
ToLower
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:233
IsSpace
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:102
SAFE_CHARS_FILENAME
@ SAFE_CHARS_FILENAME
Chars allowed in filenames.
Definition: strencodings.h:29
SAFE_CHARS_DEFAULT
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
Definition: strencodings.h:25
ParseUInt8
NODISCARD bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
Definition: strencodings.cpp:353
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:83
FormatParagraph
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.
Definition: strencodings.cpp:429
atoi
int atoi(const std::string &str)
Definition: strencodings.cpp:477
atoi64
int64_t atoi64(const std::string &str)
Definition: strencodings.cpp:469
ParseInt32
NODISCARD bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:314
span.h
ToUpper
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
Definition: strencodings.h:258
ParseDouble
NODISCARD bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
Definition: strencodings.cpp:411
SplitHostPort
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
Definition: strencodings.cpp:113
SAFE_CHARS_URI
@ SAFE_CHARS_URI
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:31
TimingResistantEqual
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:172
attributes.h
IsDigit
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:86
ParseHex
std::vector< uint8_t > ParseHex(const char *psz)
Definition: strencodings.cpp:87
ParseFixedPoint
NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
Definition: strencodings.cpp:511
IsHex
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.
Definition: strencodings.cpp:64
HexDigit
signed char HexDigit(char c)
Definition: strencodings.cpp:60
EncodeBase32
std::string EncodeBase32(Span< const uint8_t > input, bool pad=true)
Base32 encode.
Definition: strencodings.cpp:217
EncodeBase64
std::string EncodeBase64(Span< const uint8_t > input)
Definition: strencodings.cpp:137
SafeChars
SafeChars
Used by SanitizeString()
Definition: strencodings.h:23
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:656