Bitcoin Core  22.99.0
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 
22 {
27 };
28 
36 std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
37 std::vector<unsigned char> ParseHex(const char* psz);
38 std::vector<unsigned char> ParseHex(const std::string& str);
39 signed char HexDigit(char c);
40 /* Returns true if each character in str is a hex character, and has an even
41  * number of hex digits.*/
42 bool IsHex(const std::string& str);
46 bool IsHexNumber(const std::string& str);
47 std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
48 std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
49 std::string EncodeBase64(Span<const unsigned char> input);
50 std::string EncodeBase64(const std::string& str);
51 std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
52 std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
53 
59 std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
60 
66 std::string EncodeBase32(const std::string& str, bool pad = true);
67 
68 void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut);
69 int64_t atoi64(const std::string& str);
70 int atoi(const std::string& str);
71 
77 constexpr bool IsDigit(char c)
78 {
79  return c >= '0' && c <= '9';
80 }
81 
93 constexpr inline bool IsSpace(char c) noexcept {
94  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
95 }
96 
102 [[nodiscard]] bool ParseInt32(const std::string& str, int32_t *out);
103 
109 [[nodiscard]] bool ParseInt64(const std::string& str, int64_t *out);
110 
116 [[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out);
117 
123 [[nodiscard]] bool ParseUInt16(const std::string& str, uint16_t* out);
124 
130 [[nodiscard]] bool ParseUInt32(const std::string& str, uint32_t *out);
131 
137 [[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out);
138 
144 [[nodiscard]] bool ParseDouble(const std::string& str, double *out);
145 
149 std::string HexStr(const Span<const uint8_t> s);
150 inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
151 
156 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
157 
163 template <typename T>
164 bool TimingResistantEqual(const T& a, const T& b)
165 {
166  if (b.size() == 0) return a.size() == 0;
167  size_t accumulator = a.size() ^ b.size();
168  for (size_t i = 0; i < a.size(); i++)
169  accumulator |= a[i] ^ b[i%b.size()];
170  return accumulator == 0;
171 }
172 
178 [[nodiscard]] bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
179 
181 template<int frombits, int tobits, bool pad, typename O, typename I>
182 bool ConvertBits(const O& outfn, I it, I end) {
183  size_t acc = 0;
184  size_t bits = 0;
185  constexpr size_t maxv = (1 << tobits) - 1;
186  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
187  while (it != end) {
188  acc = ((acc << frombits) | *it) & max_acc;
189  bits += frombits;
190  while (bits >= tobits) {
191  bits -= tobits;
192  outfn((acc >> bits) & maxv);
193  }
194  ++it;
195  }
196  if (pad) {
197  if (bits) outfn((acc << (tobits - bits)) & maxv);
198  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
199  return false;
200  }
201  return true;
202 }
203 
214 constexpr char ToLower(char c)
215 {
216  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
217 }
218 
228 std::string ToLower(const std::string& str);
229 
240 constexpr char ToUpper(char c)
241 {
242  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
243 }
244 
254 std::string ToUpper(const std::string& str);
255 
265 std::string Capitalize(std::string str);
266 
267 #endif // BITCOIN_UTIL_STRENCODINGS_H
SanitizeString
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
Definition: strencodings.cpp:27
ConvertBits
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
Definition: strencodings.h:182
Capitalize
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
Definition: strencodings.cpp:587
SplitHostPort
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
Definition: strencodings.cpp:110
MakeUCharSpan
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:249
IsHexNumber
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
Definition: strencodings.cpp:71
SAFE_CHARS_UA_COMMENT
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
Definition: strencodings.h:24
ParseFixedPoint
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:481
ParseUInt16
bool ParseUInt16(const std::string &str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
Definition: strencodings.cpp:339
ToLower
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:214
IsSpace
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:93
SAFE_CHARS_FILENAME
@ SAFE_CHARS_FILENAME
Chars allowed in filenames.
Definition: strencodings.h:25
SAFE_CHARS_DEFAULT
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
Definition: strencodings.h:23
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
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:399
atoi
int atoi(const std::string &str)
Definition: strencodings.cpp:449
ParseUInt8
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:327
atoi64
int64_t atoi64(const std::string &str)
Definition: strencodings.cpp:440
ParseUInt64
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:368
DecodeBase32
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid=nullptr)
Definition: strencodings.cpp:226
ParseDouble
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
Definition: strencodings.cpp:385
span.h
ToUpper
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
Definition: strencodings.h:240
ParseUInt32
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:351
SAFE_CHARS_URI
@ SAFE_CHARS_URI
Chars allowed in URIs (RFC 3986)
Definition: strencodings.h:26
TimingResistantEqual
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:164
EncodeBase64
std::string EncodeBase64(Span< const unsigned char > input)
Definition: strencodings.cpp:131
attributes.h
IsDigit
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:77
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: strencodings.cpp:84
ParseInt64
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:312
IsHex
bool IsHex(const std::string &str)
Definition: strencodings.cpp:61
HexDigit
signed char HexDigit(char c)
Definition: strencodings.cpp:56
ParseInt32
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:296
DecodeBase64
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid=nullptr)
Definition: strencodings.cpp:147
T
#define T(expected, seed, data)
EncodeBase32
std::string EncodeBase32(Span< const unsigned char > input, bool pad=true)
Base32 encode.
Definition: strencodings.cpp:206
SafeChars
SafeChars
Utilities for converting data from/to strings.
Definition: strencodings.h:21
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:594