Changeset View
Changeset View
Standalone View
Standalone View
src/uint256.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <uint256.h> | #include <uint256.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
template <unsigned int BITS> | template <unsigned int BITS> | ||||
base_blob<BITS>::base_blob(const std::vector<uint8_t> &vch) { | base_blob<BITS>::base_blob(const std::vector<uint8_t> &vch) { | ||||
assert(vch.size() == sizeof(data)); | assert(vch.size() == sizeof(m_data)); | ||||
memcpy(data, vch.data(), sizeof(data)); | memcpy(m_data, vch.data(), sizeof(m_data)); | ||||
} | } | ||||
template <unsigned int BITS> std::string base_blob<BITS>::GetHex() const { | template <unsigned int BITS> std::string base_blob<BITS>::GetHex() const { | ||||
return HexStr(std::reverse_iterator<const uint8_t *>(data + sizeof(data)), | return HexStr( | ||||
std::reverse_iterator<const uint8_t *>(data)); | std::reverse_iterator<const uint8_t *>(m_data + sizeof(m_data)), | ||||
std::reverse_iterator<const uint8_t *>(m_data)); | |||||
} | } | ||||
template <unsigned int BITS> void base_blob<BITS>::SetHex(const char *psz) { | template <unsigned int BITS> void base_blob<BITS>::SetHex(const char *psz) { | ||||
memset(data, 0, sizeof(data)); | memset(m_data, 0, sizeof(m_data)); | ||||
// skip leading spaces | // skip leading spaces | ||||
while (IsSpace(*psz)) { | while (IsSpace(*psz)) { | ||||
psz++; | psz++; | ||||
} | } | ||||
// skip 0x | // skip 0x | ||||
if (psz[0] == '0' && ToLower(psz[1]) == 'x') { | if (psz[0] == '0' && ToLower(psz[1]) == 'x') { | ||||
psz += 2; | psz += 2; | ||||
} | } | ||||
// hex string to uint | // hex string to uint | ||||
size_t digits = 0; | size_t digits = 0; | ||||
while (::HexDigit(psz[digits]) != -1) { | while (::HexDigit(psz[digits]) != -1) { | ||||
digits++; | digits++; | ||||
} | } | ||||
uint8_t *p1 = (uint8_t *)data; | uint8_t *p1 = (uint8_t *)m_data; | ||||
uint8_t *pend = p1 + WIDTH; | uint8_t *pend = p1 + WIDTH; | ||||
while (digits > 0 && p1 < pend) { | while (digits > 0 && p1 < pend) { | ||||
*p1 = ::HexDigit(psz[--digits]); | *p1 = ::HexDigit(psz[--digits]); | ||||
if (digits > 0) { | if (digits > 0) { | ||||
*p1 |= uint8_t(::HexDigit(psz[--digits])) << 4; | *p1 |= uint8_t(::HexDigit(psz[--digits])) << 4; | ||||
p1++; | p1++; | ||||
} | } | ||||
} | } | ||||
Show All 26 Lines |