Raven Core  3.0.0
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
utilstrencodings.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 // Copyright (c) 2017-2019 The Raven 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 RAVEN_UTILSTRENCODINGS_H
11 #define RAVEN_UTILSTRENCODINGS_H
12 
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 
17 #define BEGIN(a) ((char*)&(a))
18 #define END(a) ((char*)&((&(a))[1]))
19 #define UBEGIN(a) ((unsigned char*)&(a))
20 #define UEND(a) ((unsigned char*)&((&(a))[1]))
21 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
22 
25 {
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* pfInvalid = nullptr);
50 std::string DecodeBase64(const std::string& str);
51 std::string EncodeBase64(const unsigned char* pch, size_t len);
52 std::string EncodeBase64(const std::string& str);
53 std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = nullptr);
54 std::string DecodeBase32(const std::string& str);
55 std::string EncodeBase32(const unsigned char* pch, size_t len);
56 std::string EncodeBase32(const std::string& str);
57 
58 void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
59 std::string i64tostr(int64_t n);
60 std::string itostr(int n);
61 int64_t atoi64(const char* psz);
62 int64_t atoi64(const std::string& str);
63 int atoi(const std::string& str);
64 
70 bool ParseInt32(const std::string& str, int32_t *out);
71 
77 bool ParseInt64(const std::string& str, int64_t *out);
78 
84 bool ParseUInt32(const std::string& str, uint32_t *out);
85 
91 bool ParseUInt64(const std::string& str, uint64_t *out);
92 
98 bool ParseDouble(const std::string& str, double *out);
99 
100 template<typename T>
101 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
102 {
103  std::string rv;
104  static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
105  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
106  rv.reserve((itend-itbegin)*3);
107  for(T it = itbegin; it < itend; ++it)
108  {
109  unsigned char val = (unsigned char)(*it);
110  if(fSpaces && it != itbegin)
111  rv.push_back(' ');
112  rv.push_back(hexmap[val>>4]);
113  rv.push_back(hexmap[val&15]);
114  }
115 
116  return rv;
117 }
118 
119 template<typename T>
120 inline std::string HexStr(const T& vch, bool fSpaces=false)
121 {
122  return HexStr(vch.begin(), vch.end(), fSpaces);
123 }
124 
129 std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
130 
136 template <typename T>
137 bool TimingResistantEqual(const T& a, const T& b)
138 {
139  if (b.size() == 0) return a.size() == 0;
140  size_t accumulator = a.size() ^ b.size();
141  for (size_t i = 0; i < a.size(); i++)
142  accumulator |= a[i] ^ b[i%b.size()];
143  return accumulator == 0;
144 }
145 
151 bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
152 
153 #endif // RAVEN_UTILSTRENCODINGS_H
std::string itostr(int n)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
The full set of allowed chars.
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
std::string EncodeBase64(const unsigned char *pch, size_t len)
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
int64_t atoi64(const char *psz)
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid=nullptr)
signed char HexDigit(char c)
std::vector< unsigned char > ParseHex(const char *psz)
std::string i64tostr(int64_t n)
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.
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
Chars allowed in filenames.
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid=nullptr)
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)
BIP-0014 subset.
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
std::string EncodeBase32(const unsigned char *pch, size_t len)
SafeChars
Used by SanitizeString()
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...