]> Git Repo - VerusCoin.git/blame_incremental - src/utilstrencodings.h
Testnet fixes
[VerusCoin.git] / src / utilstrencodings.h
... / ...
CommitLineData
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2014 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or https://www.opensource.org/licenses/mit-license.php .
5
6/**
7 * Utilities for converting data from/to strings.
8 */
9#ifndef BITCOIN_UTILSTRENCODINGS_H
10#define BITCOIN_UTILSTRENCODINGS_H
11
12#include <stdint.h>
13#include <string>
14#include <vector>
15
16#define BEGIN(a) ((char*)&(a))
17#define END(a) ((char*)&((&(a))[1]))
18#define UBEGIN(a) ((unsigned char*)&(a))
19#define UEND(a) ((unsigned char*)&((&(a))[1]))
20#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
21
22/** This is needed because the foreach macro can't get over the comma in pair<t1, t2> */
23#define PAIRTYPE(t1, t2) std::pair<t1, t2>
24
25/** Used by SanitizeString() */
26enum SafeChars
27{
28 SAFE_CHARS_DEFAULT, //!< The full set of allowed chars
29 SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
30 SAFE_CHARS_URI //!< Chars allowed in URIs (RFC 3986)
31};
32
33std::string SanitizeFilename(const std::string& str);
34/**
35* Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email
36* addresses, but avoid anything even possibly remotely dangerous like & or >
37* @param[in] str The string to sanitize
38* @param[in] rule The set of safe chars to choose (default: least restrictive)
39* @return A new string without unsafe chars
40*/
41std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
42std::string HexInt(uint32_t val);
43uint32_t ParseHexToUInt32(const std::string& str);
44std::vector<unsigned char> ParseHex(const char* psz);
45std::vector<unsigned char> ParseHex(const std::string& str);
46signed char HexDigit(char c);
47bool IsHex(const std::string& str);
48std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
49std::string DecodeBase64(const std::string& str);
50std::string EncodeBase64(const unsigned char* pch, size_t len);
51std::string EncodeBase64(const std::string& str);
52std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
53std::string DecodeBase32(const std::string& str);
54std::string EncodeBase32(const unsigned char* pch, size_t len);
55std::string EncodeBase32(const std::string& str);
56
57std::string i64tostr(int64_t n);
58std::string itostr(int n);
59int64_t atoi64(const char* psz);
60int64_t atoi64(const std::string& str);
61int atoi(const std::string& str);
62
63/**
64 * Convert string to signed 32-bit integer with strict parse error feedback.
65 * @returns true if the entire string could be parsed as valid integer,
66 * false if not the entire string could be parsed or when overflow or underflow occurred.
67 */
68bool ParseInt32(const std::string& str, int32_t *out);
69
70/**
71 * Convert string to signed 64-bit integer with strict parse error feedback.
72 * @returns true if the entire string could be parsed as valid integer,
73 * false if not the entire string could be parsed or when overflow or underflow occurred.
74 */
75bool ParseInt64(const std::string& str, int64_t *out);
76
77/**
78 * Convert string to double with strict parse error feedback.
79 * @returns true if the entire string could be parsed as valid double,
80 * false if not the entire string could be parsed or when overflow or underflow occurred.
81 */
82bool ParseDouble(const std::string& str, double *out);
83
84template<typename T>
85std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
86{
87 std::string rv;
88 static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
89 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
90 rv.reserve((itend-itbegin)*3);
91 for(T it = itbegin; it < itend; ++it)
92 {
93 unsigned char val = (unsigned char)(*it);
94 if(fSpaces && it != itbegin)
95 rv.push_back(' ');
96 rv.push_back(hexmap[val>>4]);
97 rv.push_back(hexmap[val&15]);
98 }
99
100 return rv;
101}
102
103template<typename T>
104inline std::string HexStr(const T& vch, bool fSpaces=false)
105{
106 return HexStr(vch.begin(), vch.end(), fSpaces);
107}
108
109/**
110 * Format a paragraph of text to a fixed width, adding spaces for
111 * indentation to any added line.
112 */
113std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
114
115/**
116 * Timing-attack-resistant comparison.
117 * Takes time proportional to length
118 * of first argument.
119 */
120template <typename T>
121bool TimingResistantEqual(const T& a, const T& b)
122{
123 if (b.size() == 0) return a.size() == 0;
124 size_t accumulator = a.size() ^ b.size();
125 for (size_t i = 0; i < a.size(); i++)
126 accumulator |= a[i] ^ b[i%b.size()];
127 return accumulator == 0;
128}
129
130/** Parse number as fixed point according to JSON number syntax.
131 * See http://json.org/number.gif
132 * @returns true on success, false on error.
133 * @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
134 */
135bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
136
137/**
138 * Convert from one power-of-2 number base to another.
139 *
140 * Examples using ConvertBits<8, 5, true>():
141 * 000000 -> 0000000000
142 * 202020 -> 0400100200
143 * 757575 -> 0e151a170a
144 * abcdef -> 150f061e1e
145 * ffffff -> 1f1f1f1f1e
146 */
147template<int frombits, int tobits, bool pad, typename O, typename I>
148bool ConvertBits(const O& outfn, I it, I end) {
149 size_t acc = 0;
150 size_t bits = 0;
151 constexpr size_t maxv = (1 << tobits) - 1;
152 constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
153 while (it != end) {
154 acc = ((acc << frombits) | *it) & max_acc;
155 bits += frombits;
156 while (bits >= tobits) {
157 bits -= tobits;
158 outfn((acc >> bits) & maxv);
159 }
160 ++it;
161 }
162 if (pad) {
163 if (bits) outfn((acc << (tobits - bits)) & maxv);
164 } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
165 return false;
166 }
167 return true;
168}
169
170#endif // BITCOIN_UTILSTRENCODINGS_H
This page took 0.022754 seconds and 4 git commands to generate.