]> Git Repo - VerusCoin.git/blame - src/utilstrencodings.h
Update libsecp256k1
[VerusCoin.git] / src / utilstrencodings.h
CommitLineData
ad49c256
WL
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2014 The Bitcoin developers
c63a73d1 3// Distributed under the MIT software license, see the accompanying
ad49c256
WL
4// file COPYING or http://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
c63a73d1 22/** This is needed because the foreach macro can't get over the comma in pair<t1, t2> */
ad49c256
WL
23#define PAIRTYPE(t1, t2) std::pair<t1, t2>
24
25std::string SanitizeString(const std::string& str);
26std::vector<unsigned char> ParseHex(const char* psz);
27std::vector<unsigned char> ParseHex(const std::string& str);
28signed char HexDigit(char c);
29bool IsHex(const std::string& str);
30std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
31std::string DecodeBase64(const std::string& str);
32std::string EncodeBase64(const unsigned char* pch, size_t len);
33std::string EncodeBase64(const std::string& str);
34std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
35std::string DecodeBase32(const std::string& str);
36std::string EncodeBase32(const unsigned char* pch, size_t len);
37std::string EncodeBase32(const std::string& str);
38
39std::string i64tostr(int64_t n);
40std::string itostr(int n);
41int64_t atoi64(const char* psz);
42int64_t atoi64(const std::string& str);
43int atoi(const std::string& str);
44
45/**
46 * Convert string to signed 32-bit integer with strict parse error feedback.
47 * @returns true if the entire string could be parsed as valid integer,
c63a73d1 48 * false if not the entire string could be parsed or when overflow or underflow occurred.
ad49c256
WL
49 */
50bool ParseInt32(const std::string& str, int32_t *out);
51
52template<typename T>
53std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
54{
55 std::string rv;
56 static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
57 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
58 rv.reserve((itend-itbegin)*3);
59 for(T it = itbegin; it < itend; ++it)
60 {
61 unsigned char val = (unsigned char)(*it);
62 if(fSpaces && it != itbegin)
63 rv.push_back(' ');
64 rv.push_back(hexmap[val>>4]);
65 rv.push_back(hexmap[val&15]);
66 }
67
68 return rv;
69}
70
71template<typename T>
72inline std::string HexStr(const T& vch, bool fSpaces=false)
73{
74 return HexStr(vch.begin(), vch.end(), fSpaces);
75}
76
c63a73d1
MF
77/**
78 * Format a paragraph of text to a fixed width, adding spaces for
ad49c256
WL
79 * indentation to any added line.
80 */
81std::string FormatParagraph(const std::string in, size_t width=79, size_t indent=0);
82
83/**
84 * Timing-attack-resistant comparison.
85 * Takes time proportional to length
86 * of first argument.
87 */
88template <typename T>
89bool TimingResistantEqual(const T& a, const T& b)
90{
91 if (b.size() == 0) return a.size() == 0;
92 size_t accumulator = a.size() ^ b.size();
93 for (size_t i = 0; i < a.size(); i++)
94 accumulator |= a[i] ^ b[i%b.size()];
95 return accumulator == 0;
96}
97
98#endif // BITCOIN_UTILSTRENCODINGS_H
This page took 0.046031 seconds and 4 git commands to generate.