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 #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 
24 {
29 };
30 
38 std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
39 std::vector<unsigned char> ParseHex(const char* psz);
40 std::vector<unsigned char> ParseHex(const std::string& str);
41 signed char HexDigit(char c);
42 /* Returns true if each character in str is a hex character, and has an even
43  * number of hex digits.*/
44 bool IsHex(const std::string& str);
48 bool IsHexNumber(const std::string& str);
49 std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
50 std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
51 std::string EncodeBase64(Span<const unsigned char> input);
52 std::string EncodeBase64(const std::string& str);
53 std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
54 std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
55 
61 std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
62 
68 std::string EncodeBase32(const std::string& str, bool pad = true);
69 
70 void SplitHostPort(std::string in, int& portOut, std::string& hostOut);
71 int64_t atoi64(const std::string& str);
72 int atoi(const std::string& str);
73 
79 constexpr bool IsDigit(char c)
80 {
81  return c >= '0' && c <= '9';
82 }
83 
95 constexpr inline bool IsSpace(char c) noexcept {
96  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
97 }
98 
104 NODISCARD bool ParseInt32(const std::string& str, int32_t *out);
105 
111 NODISCARD bool ParseInt64(const std::string& str, int64_t *out);
112 
118 NODISCARD bool ParseUInt8(const std::string& str, uint8_t *out);
119 
125 NODISCARD bool ParseUInt32(const std::string& str, uint32_t *out);
126 
132 NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
133 
139 NODISCARD bool ParseDouble(const std::string& str, double *out);
140 
144 std::string HexStr(const Span<const uint8_t> s);
145 inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
146 
151 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
152 
158 template <typename T>
159 bool TimingResistantEqual(const T& a, const T& b)
160 {
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 
173 NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
174 
176 template<int frombits, int tobits, bool pad, typename O, typename I>
177 bool ConvertBits(const O& outfn, I it, I end) {
178  size_t acc = 0;
179  size_t bits = 0;
180  constexpr size_t maxv = (1 << tobits) - 1;
181  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
182  while (it != end) {
183  acc = ((acc << frombits) | *it) & max_acc;
184  bits += frombits;
185  while (bits >= tobits) {
186  bits -= tobits;
187  outfn((acc >> bits) & maxv);
188  }
189  ++it;
190  }
191  if (pad) {
192  if (bits) outfn((acc << (tobits - bits)) & maxv);
193  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
194  return false;
195  }
196  return true;
197 }
198 
209 constexpr char ToLower(char c)
210 {
211  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
212 }
213 
223 std::string ToLower(const std::string& str);
224 
235 constexpr char ToUpper(char c)
236 {
237  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
238 }
239 
249 std::string ToUpper(const std::string& str);
250 
260 std::string Capitalize(std::string str);
261 
262 #endif // BITCOIN_UTIL_STRENCODINGS_H
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:159
The full set of allowed chars.
Definition: strencodings.h:25
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
Definition: strencodings.h:235
std::deque< CInv >::iterator it
std::string EncodeBase64(Span< const unsigned char > input)
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:79
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 EncodeBase32(Span< const unsigned char > input, bool pad=true)
Base32 encode.
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid=nullptr)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
Definition: span.h:220
#define NODISCARD
Definition: attributes.h:18
SafeChars
Used by SanitizeString()
Definition: strencodings.h:23
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
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:177
Chars allowed in filenames.
Definition: strencodings.h:27
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:95
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.
NODISCARD bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
int atoi(const std::string &str)
int64_t atoi64(const std::string &str)
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:209
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:82
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:28
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
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.