2014-06-28 17:35:22 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-12-31 09:48:25 +01:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-06-28 17:35:22 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <uint256.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
|
|
|
|
2018-10-23 00:51:11 +02:00
|
|
|
#include <util/strencodings.h>
|
2014-06-28 17:35:22 +02:00
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
std::string base_blob<BITS>::GetHex() const
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
2020-06-24 17:26:47 +02:00
|
|
|
uint8_t m_data_rev[WIDTH];
|
|
|
|
for (int i = 0; i < WIDTH; ++i) {
|
|
|
|
m_data_rev[i] = m_data[WIDTH - 1 - i];
|
|
|
|
}
|
|
|
|
return HexStr(m_data_rev);
|
2014-06-28 17:35:22 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
void base_blob<BITS>::SetHex(const char* psz)
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
2022-10-19 22:16:04 +02:00
|
|
|
std::fill(m_data.begin(), m_data.end(), 0);
|
2014-06-28 17:35:22 +02:00
|
|
|
|
|
|
|
// skip leading spaces
|
2018-10-26 18:54:30 +02:00
|
|
|
while (IsSpace(*psz))
|
2014-06-28 17:35:22 +02:00
|
|
|
psz++;
|
|
|
|
|
|
|
|
// skip 0x
|
2019-01-10 01:46:32 +01:00
|
|
|
if (psz[0] == '0' && ToLower(psz[1]) == 'x')
|
2014-06-28 17:35:22 +02:00
|
|
|
psz += 2;
|
|
|
|
|
|
|
|
// hex string to uint
|
2018-11-16 02:21:28 +01:00
|
|
|
size_t digits = 0;
|
|
|
|
while (::HexDigit(psz[digits]) != -1)
|
|
|
|
digits++;
|
2022-10-19 22:16:04 +02:00
|
|
|
unsigned char* p1 = m_data.data();
|
2015-03-12 15:13:49 +01:00
|
|
|
unsigned char* pend = p1 + WIDTH;
|
2018-11-16 02:21:28 +01:00
|
|
|
while (digits > 0 && p1 < pend) {
|
|
|
|
*p1 = ::HexDigit(psz[--digits]);
|
|
|
|
if (digits > 0) {
|
|
|
|
*p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4);
|
2014-06-28 17:35:22 +02:00
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
void base_blob<BITS>::SetHex(const std::string& str)
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
|
|
|
SetHex(str.c_str());
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
std::string base_blob<BITS>::ToString() const
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
|
|
|
return (GetHex());
|
|
|
|
}
|
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
// Explicit instantiations for base_blob<160>
|
|
|
|
template std::string base_blob<160>::GetHex() const;
|
|
|
|
template std::string base_blob<160>::ToString() const;
|
|
|
|
template void base_blob<160>::SetHex(const char*);
|
|
|
|
template void base_blob<160>::SetHex(const std::string&);
|
2014-06-28 17:35:22 +02:00
|
|
|
|
2014-12-15 10:22:19 +01:00
|
|
|
// Explicit instantiations for base_blob<256>
|
|
|
|
template std::string base_blob<256>::GetHex() const;
|
|
|
|
template std::string base_blob<256>::ToString() const;
|
|
|
|
template void base_blob<256>::SetHex(const char*);
|
|
|
|
template void base_blob<256>::SetHex(const std::string&);
|
2019-12-30 20:05:27 +01:00
|
|
|
|
2020-09-21 06:17:29 +02:00
|
|
|
const uint256 uint256::ZERO(0);
|
2020-09-25 07:01:38 +02:00
|
|
|
const uint256 uint256::ONE(1);
|