2014-06-28 17:35:22 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2009-2017 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
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <utilstrencodings.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
|
|
|
|
while (isspace(*psz))
|
|
|
|
psz++;
|
|
|
|
|
|
|
|
// skip 0x
|
|
|
|
if (psz[0] == '0' && tolower(psz[1]) == 'x')
|
|
|
|
psz += 2;
|
|
|
|
|
|
|
|
// hex string to uint
|
|
|
|
const char* pbegin = psz;
|
|
|
|
while (::HexDigit(*psz) != -1)
|
|
|
|
psz++;
|
|
|
|
psz--;
|
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;
|
2014-06-28 17:35:22 +02:00
|
|
|
while (psz >= pbegin && p1 < pend) {
|
|
|
|
*p1 = ::HexDigit(*psz--);
|
|
|
|
if (psz >= pbegin) {
|
|
|
|
*p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
|
|
|
|
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&);
|