PIVX Core  5.6.99
P2P Digital Currency
utilstrencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2017-2022 The PIVX Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
10 #ifndef PIVX_UTILSTRENCODINGS_H
11 #define PIVX_UTILSTRENCODINGS_H
12 
14 #include "span.h"
15 
16 #include <algorithm>
17 #include <iterator>
18 #include <stdint.h>
19 #include <string>
20 #include <vector>
21 
22 #define BEGIN(a) ((char*)&(a))
23 #define END(a) ((char*)&((&(a))[1]))
24 #define UBEGIN(a) ((unsigned char*)&(a))
25 #define UEND(a) ((unsigned char*)&((&(a))[1]))
26 #define ARRAYLEN(array) (sizeof(array) / sizeof((array)[0]))
27 
30 {
34 };
35 
43 std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
44 
52 bool validateURL(const std::string& strURL, std::string& strErr, unsigned int maxSize = 64);
53 bool validateURL(const std::string& strURL);
54 
55 std::vector<unsigned char> ParseHex(const char* psz);
56 std::vector<unsigned char> ParseHex(const std::string& str);
57 signed char HexDigit(char c);
58 bool IsHex(const std::string& str);
59 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = nullptr);
60 std::string DecodeBase64(const std::string& str);
61 std::string EncodeBase64(Span<const unsigned char> input);
62 std::string EncodeBase64(const std::string& str);
63 std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = nullptr);
64 std::string DecodeBase32(const std::string& str);
65 
71 std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
72 
78 std::string EncodeBase32(const std::string& str, bool pad = true);
79 
80 std::string i64tostr(int64_t n);
81 std::string itostr(int n);
82 int64_t atoi64(const char* psz);
83 int64_t atoi64(const std::string& str);
84 int atoi(const std::string& str);
85 
91 constexpr bool IsDigit(char c)
92 {
93  return c >= '0' && c <= '9';
94 }
95 
101 bool ParseInt32(const std::string& str, int32_t *out);
102 
108 bool ParseInt64(const std::string& str, int64_t *out);
109 
115 bool ParseUInt8(const std::string& str, uint8_t *out);
116 
122 bool ParseUInt32(const std::string& str, uint32_t *out);
123 
129 bool ParseDouble(const std::string& str, double *out);
130 
131 /* Return iterator to first non zero element, or itend */
132 template <typename T>
133 T FindFirstNonZero(T itbegin, T itend)
134 {
135  return std::find_if(itbegin, itend, [](unsigned char v) { return v != 0; });
136 }
137 
141 std::string HexStr(const Span<const uint8_t> s);
142 inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
143 
145 inline std::string ReverseEndianString(std::string in)
146 {
147  std::string out = "";
148  unsigned int s = in.size();
149  for (unsigned int i = 0; i < s; i += 2) {
150  out += in.substr(s - i - 2, 2);
151  }
152 
153  return out;
154 }
155 
160 std::string FormatParagraph(const std::string in, size_t width = 79, size_t indent = 0);
161 
167 template <typename T>
168 bool TimingResistantEqual(const T& a, const T& b)
169 {
170  if (b.size() == 0) return a.size() == 0;
171  size_t accumulator = a.size() ^ b.size();
172  for (size_t i = 0; i < a.size(); i++)
173  accumulator |= a[i] ^ b[i % b.size()];
174  return accumulator == 0;
175 }
176 
187 template<int frombits, int tobits, bool pad, typename O, typename I>
188 bool ConvertBits(const O& outfn, I it, I end) {
189  size_t acc = 0;
190  size_t bits = 0;
191  constexpr size_t maxv = (1 << tobits) - 1;
192  constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
193  while (it != end) {
194  acc = ((acc << frombits) | *it) & max_acc;
195  bits += frombits;
196  while (bits >= tobits) {
197  bits -= tobits;
198  outfn((acc >> bits) & maxv);
199  }
200  ++it;
201  }
202  if (pad) {
203  if (bits) outfn((acc << (tobits - bits)) & maxv);
204  } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
205  return false;
206  }
207  return true;
208 }
209 
210 
216 bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
217 
226 constexpr unsigned char ToLower(unsigned char c)
227 {
228  return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
229 }
230 
240 std::string ToLower(const std::string& str);
241 
248 void Downcase(std::string& str);
249 
258 constexpr unsigned char ToUpper(unsigned char c)
259 {
260  return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
261 }
262 
272 std::string ToUpper(const std::string& str);
273 
282 std::string Capitalize(std::string str);
283 
289 bool IsValidUTF8(const std::string& str);
290 
291 #endif // PIVX_UTILSTRENCODINGS_H
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
#define T(expected, seed, data)
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:248
void Downcase(std::string &str)
Converts the given string to its lowercase equivalent.
bool validateURL(const std::string &strURL, std::string &strErr, unsigned int maxSize=64)
Check URL format for conformance for validity to a defined pattern.
std::string EncodeBase32(Span< const unsigned char > input, bool pad=true)
Base32 encode.
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.
int64_t atoi64(const char *psz)
constexpr unsigned char ToUpper(unsigned char c)
Converts the given character to its uppercase equivalent.
T FindFirstNonZero(T itbegin, T itend)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
std::string EncodeBase64(Span< const unsigned char > input)
std::string itostr(int n)
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
std::vector< unsigned char > ParseHex(const char *psz)
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid=nullptr)
std::string ReverseEndianString(std::string in)
Reverse the endianness of a string.
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.
bool IsValidUTF8(const std::string &str)
Checks for valid 4-byte UTF-8 encoding in a string.
constexpr unsigned char ToLower(unsigned char c)
Converts the given character to its lowercase equivalent.
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.
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid=nullptr)
bool IsHex(const std::string &str)
signed char HexDigit(char c)
int atoi(const std::string &str)
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 i64tostr(int64_t n)
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.
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
SafeChars
Used by SanitizeString()
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
@ SAFE_CHARS_FILENAME
Chars allowed in filenames.