2014-06-28 17:35:22 +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-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.
|
|
|
|
|
2020-03-19 23:46:56 +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
|
|
|
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/strencodings.h>
|
2014-06-28 17:35:22 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <unsigned int BITS>
|
2014-12-15 10:22:19 +01:00
|
|
|
base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
2021-05-19 17:39:18 +02:00
|
|
|
assert(vch.size() == sizeof(m_data));
|
|
|
|
memcpy(m_data, vch.data(), sizeof(m_data));
|
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) {
|
2021-05-19 17:39:18 +02:00
|
|
|
m_data_rev[i] = m_data[WIDTH - 1 - i];
|
2020-06-24 17:26:47 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2021-05-19 17:39:18 +02:00
|
|
|
memset(m_data, 0, sizeof(m_data));
|
2014-06-28 17:35:22 +02:00
|
|
|
|
|
|
|
// skip leading spaces
|
2018-10-28 11:48:46 +01:00
|
|
|
while (IsSpace(*psz))
|
2014-06-28 17:35:22 +02:00
|
|
|
psz++;
|
|
|
|
|
|
|
|
// skip 0x
|
2019-01-09 18:10:22 +01:00
|
|
|
if (psz[0] == '0' && ToLower((unsigned char)psz[1]) == 'x')
|
2014-06-28 17:35:22 +02:00
|
|
|
psz += 2;
|
|
|
|
|
|
|
|
// hex string to uint
|
2019-07-03 14:06:45 +02:00
|
|
|
size_t digits = 0;
|
|
|
|
while (::HexDigit(psz[digits]) != -1)
|
|
|
|
digits++;
|
2021-05-19 17:39:18 +02:00
|
|
|
unsigned char* p1 = (unsigned char*)m_data;
|
2015-03-12 15:13:49 +01:00
|
|
|
unsigned char* pend = p1 + WIDTH;
|
2019-07-03 14:06:45 +02: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 base_blob<160>::base_blob(const std::vector<unsigned char>&);
|
|
|
|
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 base_blob<256>::base_blob(const std::vector<unsigned char>&);
|
|
|
|
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&);
|
2022-05-10 11:26:56 +02:00
|
|
|
|
2023-03-05 20:45:22 +01:00
|
|
|
const uint256 uint256::ZERO(0);
|
2022-05-07 18:20:42 +02:00
|
|
|
const uint256 uint256::ONE(1);
|
|
|
|
const uint256 uint256::TWO(2);
|