2014-06-28 17:35:22 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2009-2018 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
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#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
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
assert(vch.size() == sizeof(data));
|
2017-07-11 11:37:53 +02:00
|
|
|
memcpy(data, vch.data(), sizeof(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
|
|
|
{
|
2017-02-27 07:43:37 +01:00
|
|
|
return HexStr(std::reverse_iterator<const uint8_t*>(data + sizeof(data)), std::reverse_iterator<const uint8_t*>(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
|
|
|
void base_blob<BITS>::SetHex(const char* psz)
|
2014-06-28 17:35:22 +02:00
|
|
|
{
|
2014-12-15 10:22:19 +01:00
|
|
|
memset(data, 0, sizeof(data));
|
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++;
|
2014-12-15 10:22:19 +01:00
|
|
|
unsigned char* p1 = (unsigned char*)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 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&);
|