Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-11-17 04:04:01 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/strencodings.h>
|
|
|
|
#include <util/string.h>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <tinyformat.h>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2021-07-15 20:35:06 +02:00
|
|
|
#include <algorithm>
|
2014-10-10 19:24:12 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2021-09-18 06:30:30 +02:00
|
|
|
#include <optional>
|
2014-09-14 12:43:56 +02:00
|
|
|
|
2017-03-09 08:10:09 +01:00
|
|
|
static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
2015-04-03 00:51:08 +02:00
|
|
|
|
2017-03-09 08:10:09 +01:00
|
|
|
static const std::string SAFE_CHARS[] =
|
2015-09-09 14:24:56 +02:00
|
|
|
{
|
2015-09-23 12:06:00 +02:00
|
|
|
CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT
|
2017-03-03 13:37:19 +01:00
|
|
|
CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
|
|
|
|
CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
|
2018-11-05 13:40:45 +01:00
|
|
|
CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
|
2015-09-09 14:24:56 +02:00
|
|
|
};
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2017-03-09 08:10:09 +01:00
|
|
|
std::string SanitizeString(const std::string& str, int rule)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2017-03-09 08:10:09 +01:00
|
|
|
std::string strResult;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
for (std::string::size_type i = 0; i < str.size(); i++)
|
|
|
|
{
|
2015-09-09 14:24:56 +02:00
|
|
|
if (SAFE_CHARS[rule].find(str[i]) != std::string::npos)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
strResult.push_back(str[i]);
|
|
|
|
}
|
|
|
|
return strResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
const signed char p_util_hexdigit[256] =
|
|
|
|
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
|
|
|
|
|
|
|
|
signed char HexDigit(char c)
|
|
|
|
{
|
|
|
|
return p_util_hexdigit[(unsigned char)c];
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:10:09 +01:00
|
|
|
bool IsHex(const std::string& str)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2014-10-10 19:24:12 +02:00
|
|
|
for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2014-10-10 19:24:12 +02:00
|
|
|
if (HexDigit(*it) < 0)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (str.size() > 0) && (str.size()%2 == 0);
|
|
|
|
}
|
|
|
|
|
2017-09-06 18:49:43 +02:00
|
|
|
bool IsHexNumber(const std::string& str)
|
|
|
|
{
|
|
|
|
size_t starting_location = 0;
|
|
|
|
if (str.size() > 2 && *str.begin() == '0' && *(str.begin()+1) == 'x') {
|
|
|
|
starting_location = 2;
|
|
|
|
}
|
2018-09-04 15:36:09 +02:00
|
|
|
for (const char c : str.substr(starting_location)) {
|
2017-09-06 18:49:43 +02:00
|
|
|
if (HexDigit(c) < 0) return false;
|
|
|
|
}
|
|
|
|
// Return false for empty string or "0x".
|
|
|
|
return (str.size() > starting_location);
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:10:09 +01:00
|
|
|
std::vector<unsigned char> ParseHex(const char* psz)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
// convert hex dump to vector
|
2017-03-09 08:10:09 +01:00
|
|
|
std::vector<unsigned char> vch;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
while (true)
|
|
|
|
{
|
2018-10-28 11:48:46 +01:00
|
|
|
while (IsSpace(*psz))
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
psz++;
|
|
|
|
signed char c = HexDigit(*psz++);
|
|
|
|
if (c == (signed char)-1)
|
|
|
|
break;
|
|
|
|
unsigned char n = (c << 4);
|
|
|
|
c = HexDigit(*psz++);
|
|
|
|
if (c == (signed char)-1)
|
|
|
|
break;
|
|
|
|
n |= c;
|
|
|
|
vch.push_back(n);
|
|
|
|
}
|
|
|
|
return vch;
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:10:09 +01:00
|
|
|
std::vector<unsigned char> ParseHex(const std::string& str)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
return ParseHex(str.c_str());
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:35:28 +01:00
|
|
|
void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut)
|
|
|
|
{
|
2017-07-15 22:16:56 +02:00
|
|
|
size_t colon = in.find_last_of(':');
|
|
|
|
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
|
|
|
|
bool fHaveColon = colon != in.npos;
|
2021-03-01 21:35:28 +01:00
|
|
|
bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
|
|
|
|
bool fMultiColon = fHaveColon && (in.find_last_of(':', colon - 1) != in.npos);
|
|
|
|
if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
|
|
|
|
uint16_t n;
|
|
|
|
if (ParseUInt16(in.substr(colon + 1), &n)) {
|
2017-07-15 22:16:56 +02:00
|
|
|
in = in.substr(0, colon);
|
|
|
|
portOut = n;
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 21:35:28 +01:00
|
|
|
if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') {
|
|
|
|
hostOut = in.substr(1, in.size() - 2);
|
|
|
|
} else {
|
2017-07-15 22:16:56 +02:00
|
|
|
hostOut = in;
|
2021-03-01 21:35:28 +01:00
|
|
|
}
|
2017-07-15 22:16:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-07 19:55:51 +02:00
|
|
|
std::string EncodeBase64(Span<const unsigned char> input)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
2018-03-07 16:28:45 +01:00
|
|
|
std::string str;
|
2020-08-07 19:55:51 +02:00
|
|
|
str.reserve(((input.size() + 2) / 3) * 4);
|
|
|
|
ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
|
2018-03-07 16:28:45 +01:00
|
|
|
while (str.size() % 4) str += '=';
|
|
|
|
return str;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:10:09 +01:00
|
|
|
std::string EncodeBase64(const std::string& str)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2020-08-07 19:55:51 +02:00
|
|
|
return EncodeBase64(MakeUCharSpan(str));
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 07:44:11 +01:00
|
|
|
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
static const int decode64_table[256] =
|
|
|
|
{
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
|
|
|
|
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
|
|
|
|
49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
|
|
|
|
};
|
|
|
|
|
2018-03-07 16:28:45 +01:00
|
|
|
const char* e = p;
|
|
|
|
std::vector<uint8_t> val;
|
|
|
|
val.reserve(strlen(p));
|
|
|
|
while (*p != 0) {
|
|
|
|
int x = decode64_table[(unsigned char)*p];
|
|
|
|
if (x == -1) break;
|
|
|
|
val.push_back(x);
|
|
|
|
++p;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 16:28:45 +01:00
|
|
|
std::vector<unsigned char> ret;
|
|
|
|
ret.reserve((val.size() * 3) / 4);
|
|
|
|
bool valid = ConvertBits<6, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
|
|
|
|
|
|
|
|
const char* q = p;
|
|
|
|
while (valid && *p != 0) {
|
|
|
|
if (*p != '=') {
|
|
|
|
valid = false;
|
|
|
|
break;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
2018-03-07 16:28:45 +01:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
valid = valid && (p - e) % 4 == 0 && p - q < 4;
|
2019-01-29 07:44:11 +01:00
|
|
|
if (pf_invalid) *pf_invalid = !valid;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2018-03-07 16:28:45 +01:00
|
|
|
return ret;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 07:44:11 +01:00
|
|
|
std::string DecodeBase64(const std::string& str, bool* pf_invalid)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2019-12-16 16:15:51 +01:00
|
|
|
if (!ValidAsCString(str)) {
|
|
|
|
if (pf_invalid) {
|
|
|
|
*pf_invalid = true;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-29 07:44:11 +01:00
|
|
|
std::vector<unsigned char> vchRet = DecodeBase64(str.c_str(), pf_invalid);
|
2017-07-13 01:23:59 +02:00
|
|
|
return std::string((const char*)vchRet.data(), vchRet.size());
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:43:13 +02:00
|
|
|
std::string EncodeBase32(Span<const unsigned char> input, bool pad)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
|
|
|
|
|
2018-03-07 16:28:45 +01:00
|
|
|
std::string str;
|
2020-08-07 19:55:51 +02:00
|
|
|
str.reserve(((input.size() + 4) / 5) * 8);
|
|
|
|
ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end());
|
2021-05-25 15:43:13 +02:00
|
|
|
if (pad) {
|
|
|
|
while (str.size() % 8) {
|
|
|
|
str += '=';
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 16:28:45 +01:00
|
|
|
return str;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:43:13 +02:00
|
|
|
std::string EncodeBase32(const std::string& str, bool pad)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2021-05-25 15:43:13 +02:00
|
|
|
return EncodeBase32(MakeUCharSpan(str), pad);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 07:44:11 +01:00
|
|
|
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
static const int decode32_table[256] =
|
|
|
|
{
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
|
|
|
|
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
|
|
|
|
23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
|
|
|
|
};
|
|
|
|
|
2018-03-07 16:28:45 +01:00
|
|
|
const char* e = p;
|
|
|
|
std::vector<uint8_t> val;
|
|
|
|
val.reserve(strlen(p));
|
|
|
|
while (*p != 0) {
|
|
|
|
int x = decode32_table[(unsigned char)*p];
|
|
|
|
if (x == -1) break;
|
|
|
|
val.push_back(x);
|
|
|
|
++p;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 16:28:45 +01:00
|
|
|
std::vector<unsigned char> ret;
|
|
|
|
ret.reserve((val.size() * 5) / 8);
|
|
|
|
bool valid = ConvertBits<5, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end());
|
|
|
|
|
|
|
|
const char* q = p;
|
|
|
|
while (valid && *p != 0) {
|
|
|
|
if (*p != '=') {
|
|
|
|
valid = false;
|
|
|
|
break;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
2018-03-07 16:28:45 +01:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
valid = valid && (p - e) % 8 == 0 && p - q < 8;
|
2019-01-29 07:44:11 +01:00
|
|
|
if (pf_invalid) *pf_invalid = !valid;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2018-03-07 16:28:45 +01:00
|
|
|
return ret;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 07:44:11 +01:00
|
|
|
std::string DecodeBase32(const std::string& str, bool* pf_invalid)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2019-12-16 16:15:51 +01:00
|
|
|
if (!ValidAsCString(str)) {
|
|
|
|
if (pf_invalid) {
|
|
|
|
*pf_invalid = true;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2019-01-29 07:44:11 +01:00
|
|
|
std::vector<unsigned char> vchRet = DecodeBase32(str.c_str(), pf_invalid);
|
2017-07-13 01:23:59 +02:00
|
|
|
return std::string((const char*)vchRet.data(), vchRet.size());
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 06:30:30 +02:00
|
|
|
[[nodiscard]] static bool ParsePrechecks(const std::string&);
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
template <typename T>
|
|
|
|
bool ParseIntegral(const std::string& str, T* out)
|
|
|
|
{
|
|
|
|
static_assert(std::is_integral<T>::value);
|
|
|
|
if (!ParsePrechecks(str)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
|
|
|
|
// handling leading +/- for backwards compatibility.
|
|
|
|
if (str.length() >= 2 && str[0] == '+' && str[1] == '-') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] == '+') ? str.substr(1) : str);
|
|
|
|
if (!opt_int) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (out != nullptr) {
|
|
|
|
*out = *opt_int;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}; // namespace
|
|
|
|
|
2021-06-16 10:01:16 +02:00
|
|
|
[[nodiscard]] static bool ParsePrechecks(const std::string& str)
|
2015-06-04 12:03:09 +02:00
|
|
|
{
|
|
|
|
if (str.empty()) // No empty string allowed
|
|
|
|
return false;
|
2018-10-28 11:48:46 +01:00
|
|
|
if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size()-1]))) // No padding allowed
|
2015-06-04 12:03:09 +02:00
|
|
|
return false;
|
2019-12-11 12:00:52 +01:00
|
|
|
if (!ValidAsCString(str)) // No embedded NUL characters allowed
|
2015-06-04 12:03:09 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-18 06:30:30 +02:00
|
|
|
bool ParseInt32(const std::string& str, int32_t* out)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2021-09-18 06:30:30 +02:00
|
|
|
return ParseIntegral<int32_t>(str, out);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 06:30:30 +02:00
|
|
|
bool ParseInt64(const std::string& str, int64_t* out)
|
2015-06-04 12:03:09 +02:00
|
|
|
{
|
2021-09-18 06:30:30 +02:00
|
|
|
return ParseIntegral<int64_t>(str, out);
|
2015-06-04 12:03:09 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 06:30:30 +02:00
|
|
|
bool ParseUInt8(const std::string& str, uint8_t* out)
|
2020-08-24 21:03:31 +02:00
|
|
|
{
|
2021-09-18 06:30:30 +02:00
|
|
|
return ParseIntegral<uint8_t>(str, out);
|
2020-08-24 21:03:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:35:28 +01:00
|
|
|
bool ParseUInt16(const std::string& str, uint16_t* out)
|
|
|
|
{
|
2021-09-18 06:30:30 +02:00
|
|
|
return ParseIntegral<uint16_t>(str, out);
|
2021-03-01 21:35:28 +01:00
|
|
|
}
|
|
|
|
|
2021-09-18 06:30:30 +02:00
|
|
|
bool ParseUInt32(const std::string& str, uint32_t* out)
|
2016-06-09 07:37:01 +02:00
|
|
|
{
|
2021-09-18 06:30:30 +02:00
|
|
|
return ParseIntegral<uint32_t>(str, out);
|
2016-06-09 07:37:01 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 06:30:30 +02:00
|
|
|
bool ParseUInt64(const std::string& str, uint64_t* out)
|
2016-06-09 07:37:01 +02:00
|
|
|
{
|
2021-09-18 06:30:30 +02:00
|
|
|
return ParseIntegral<uint64_t>(str, out);
|
2016-06-09 07:37:01 +02:00
|
|
|
}
|
|
|
|
|
2015-06-04 12:03:09 +02:00
|
|
|
bool ParseDouble(const std::string& str, double *out)
|
|
|
|
{
|
|
|
|
if (!ParsePrechecks(str))
|
|
|
|
return false;
|
|
|
|
if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
|
|
|
|
return false;
|
2015-07-18 08:16:21 +02:00
|
|
|
std::istringstream text(str);
|
|
|
|
text.imbue(std::locale::classic());
|
|
|
|
double result;
|
|
|
|
text >> result;
|
|
|
|
if(out) *out = result;
|
|
|
|
return text.eof() && !text.fail();
|
2015-06-04 12:03:09 +02:00
|
|
|
}
|
|
|
|
|
2015-05-31 15:36:44 +02:00
|
|
|
std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2022-02-10 08:14:11 +01:00
|
|
|
assert(width >= indent);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
std::stringstream out;
|
|
|
|
size_t ptr = 0;
|
2016-02-04 13:41:58 +01:00
|
|
|
size_t indented = 0;
|
|
|
|
while (ptr < in.size())
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2016-02-04 13:41:58 +01:00
|
|
|
size_t lineend = in.find_first_of('\n', ptr);
|
|
|
|
if (lineend == std::string::npos) {
|
|
|
|
lineend = in.size();
|
|
|
|
}
|
|
|
|
const size_t linelen = lineend - ptr;
|
|
|
|
const size_t rem_width = width - indented;
|
|
|
|
if (linelen <= rem_width) {
|
|
|
|
out << in.substr(ptr, linelen + 1);
|
|
|
|
ptr = lineend + 1;
|
|
|
|
indented = 0;
|
|
|
|
} else {
|
|
|
|
size_t finalspace = in.find_last_of(" \n", ptr + rem_width);
|
|
|
|
if (finalspace == std::string::npos || finalspace < ptr) {
|
|
|
|
// No place to break; just include the entire word and move on
|
|
|
|
finalspace = in.find_first_of("\n ", ptr);
|
|
|
|
if (finalspace == std::string::npos) {
|
|
|
|
// End of the string, just add it and break
|
|
|
|
out << in.substr(ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out << in.substr(ptr, finalspace - ptr) << "\n";
|
|
|
|
if (in[finalspace] == '\n') {
|
|
|
|
indented = 0;
|
|
|
|
} else if (indent) {
|
|
|
|
out << std::string(indent, ' ');
|
|
|
|
indented = indent;
|
|
|
|
}
|
|
|
|
ptr = finalspace + 1;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2015-07-06 10:49:24 +02:00
|
|
|
/** Upper bound for mantissa.
|
|
|
|
* 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer.
|
|
|
|
* Larger integers cannot consist of arbitrary combinations of 0-9:
|
|
|
|
*
|
|
|
|
* 999999999999999999 1^18-1
|
|
|
|
* 9223372036854775807 (1<<63)-1 (max int64_t)
|
|
|
|
* 9999999999999999999 1^19-1 (would overflow)
|
|
|
|
*/
|
|
|
|
static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
|
|
|
|
|
|
|
|
/** Helper function for ParseFixedPoint */
|
|
|
|
static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
|
|
|
|
{
|
|
|
|
if(ch == '0')
|
|
|
|
++mantissa_tzeros;
|
|
|
|
else {
|
|
|
|
for (int i=0; i<=mantissa_tzeros; ++i) {
|
|
|
|
if (mantissa > (UPPER_BOUND / 10LL))
|
|
|
|
return false; /* overflow */
|
|
|
|
mantissa *= 10;
|
|
|
|
}
|
|
|
|
mantissa += ch - '0';
|
|
|
|
mantissa_tzeros = 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
|
|
|
|
{
|
|
|
|
int64_t mantissa = 0;
|
|
|
|
int64_t exponent = 0;
|
|
|
|
int mantissa_tzeros = 0;
|
|
|
|
bool mantissa_sign = false;
|
|
|
|
bool exponent_sign = false;
|
|
|
|
int ptr = 0;
|
|
|
|
int end = val.size();
|
|
|
|
int point_ofs = 0;
|
|
|
|
|
|
|
|
if (ptr < end && val[ptr] == '-') {
|
|
|
|
mantissa_sign = true;
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
if (ptr < end)
|
|
|
|
{
|
|
|
|
if (val[ptr] == '0') {
|
|
|
|
/* pass single 0 */
|
|
|
|
++ptr;
|
|
|
|
} else if (val[ptr] >= '1' && val[ptr] <= '9') {
|
2018-07-24 20:49:54 +02:00
|
|
|
while (ptr < end && IsDigit(val[ptr])) {
|
2015-07-06 10:49:24 +02:00
|
|
|
if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
|
|
|
|
return false; /* overflow */
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
} else return false; /* missing expected digit */
|
|
|
|
} else return false; /* empty string or loose '-' */
|
|
|
|
if (ptr < end && val[ptr] == '.')
|
|
|
|
{
|
|
|
|
++ptr;
|
2018-07-24 20:49:54 +02:00
|
|
|
if (ptr < end && IsDigit(val[ptr]))
|
2015-07-06 10:49:24 +02:00
|
|
|
{
|
2018-07-24 20:49:54 +02:00
|
|
|
while (ptr < end && IsDigit(val[ptr])) {
|
2015-07-06 10:49:24 +02:00
|
|
|
if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
|
|
|
|
return false; /* overflow */
|
|
|
|
++ptr;
|
|
|
|
++point_ofs;
|
|
|
|
}
|
|
|
|
} else return false; /* missing expected digit */
|
|
|
|
}
|
|
|
|
if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
|
|
|
|
{
|
|
|
|
++ptr;
|
|
|
|
if (ptr < end && val[ptr] == '+')
|
|
|
|
++ptr;
|
|
|
|
else if (ptr < end && val[ptr] == '-') {
|
|
|
|
exponent_sign = true;
|
|
|
|
++ptr;
|
|
|
|
}
|
2018-07-24 20:49:54 +02:00
|
|
|
if (ptr < end && IsDigit(val[ptr])) {
|
|
|
|
while (ptr < end && IsDigit(val[ptr])) {
|
2015-07-06 10:49:24 +02:00
|
|
|
if (exponent > (UPPER_BOUND / 10LL))
|
|
|
|
return false; /* overflow */
|
|
|
|
exponent = exponent * 10 + val[ptr] - '0';
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
} else return false; /* missing expected digit */
|
|
|
|
}
|
|
|
|
if (ptr != end)
|
|
|
|
return false; /* trailing garbage */
|
|
|
|
|
|
|
|
/* finalize exponent */
|
|
|
|
if (exponent_sign)
|
|
|
|
exponent = -exponent;
|
|
|
|
exponent = exponent - point_ofs + mantissa_tzeros;
|
|
|
|
|
|
|
|
/* finalize mantissa */
|
|
|
|
if (mantissa_sign)
|
|
|
|
mantissa = -mantissa;
|
|
|
|
|
|
|
|
/* convert to one 64-bit fixed-point value */
|
|
|
|
exponent += decimals;
|
|
|
|
if (exponent < 0)
|
|
|
|
return false; /* cannot represent values smaller than 10^-decimals */
|
|
|
|
if (exponent >= 18)
|
|
|
|
return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
|
|
|
|
|
|
|
|
for (int i=0; i < exponent; ++i) {
|
|
|
|
if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
|
|
|
|
return false; /* overflow */
|
|
|
|
mantissa *= 10;
|
|
|
|
}
|
|
|
|
if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
|
|
|
|
return false; /* overflow */
|
|
|
|
|
|
|
|
if (amount_out)
|
|
|
|
*amount_out = mantissa;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-24 17:26:47 +02:00
|
|
|
|
|
|
|
std::string HexStr(const Span<const uint8_t> s)
|
|
|
|
{
|
2021-05-19 10:07:25 +02:00
|
|
|
std::string rv(s.size() * 2, '\0');
|
2020-06-24 17:26:47 +02:00
|
|
|
static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
2021-05-19 10:07:25 +02:00
|
|
|
auto it = rv.begin();
|
|
|
|
for (uint8_t v : s) {
|
|
|
|
*it++ = hexmap[v >> 4];
|
|
|
|
*it++ = hexmap[v & 15];
|
2020-06-24 17:26:47 +02:00
|
|
|
}
|
2021-05-19 10:07:25 +02:00
|
|
|
assert(it == rv.end());
|
2020-06-24 17:26:47 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2021-07-15 20:35:06 +02:00
|
|
|
|
2019-08-07 06:42:54 +02:00
|
|
|
std::string ToLower(const std::string& str)
|
2021-07-15 20:35:06 +02:00
|
|
|
{
|
2019-08-07 06:42:54 +02:00
|
|
|
std::string r;
|
|
|
|
for (auto ch : str) r += ToLower((unsigned char)ch);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToUpper(const std::string& str)
|
|
|
|
{
|
|
|
|
std::string r;
|
|
|
|
for (auto ch : str) r += ToUpper((unsigned char)ch);
|
|
|
|
return r;
|
2021-07-15 20:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Capitalize(std::string str)
|
|
|
|
{
|
|
|
|
if (str.empty()) return str;
|
|
|
|
str[0] = ToUpper(str.front());
|
|
|
|
return str;
|
|
|
|
}
|