Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2021 The Bitcoin developers 3 : // Copyright (c) 2017-2021 The PIVX Core developers 4 : // Distributed under the MIT/X11 software license, see the accompanying 5 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 : 7 : #include "uint256.h" 8 : 9 : #include "utilstrencodings.h" 10 : 11 : #include <string.h> 12 : 13 : template <unsigned int BITS> 14 12618433 : base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch) 15 : { 16 12618433 : assert(vch.size() == sizeof(m_data)); 17 12618433 : memcpy(m_data, vch.data(), sizeof(m_data)); 18 12618433 : } 19 : 20 : template <unsigned int BITS> 21 7337091 : std::string base_blob<BITS>::GetHex() const 22 : { 23 : uint8_t m_data_rev[WIDTH]; 24 242118605 : for (int i = 0; i < WIDTH; ++i) { 25 234780964 : m_data_rev[i] = m_data[WIDTH - 1 - i]; 26 : } 27 7337091 : return HexStr(m_data_rev); 28 : } 29 : 30 : template <unsigned int BITS> 31 204692 : void base_blob<BITS>::SetHex(const char* psz) 32 : { 33 204692 : memset(m_data, 0, sizeof(m_data)); 34 : 35 : // skip leading spaces 36 204698 : while (isspace(*psz)) 37 6 : psz++; 38 : 39 : // skip 0x 40 204692 : if (psz[0] == '0' && tolower(psz[1]) == 'x') 41 9324 : psz += 2; 42 : 43 : // hex string to uint 44 204692 : size_t digits = 0; 45 13242826 : while (::HexDigit(psz[digits]) != -1) 46 13038144 : digits++; 47 204692 : unsigned char* p1 = (unsigned char*)m_data; 48 204692 : unsigned char* pend = p1 + WIDTH; 49 6724252 : while (digits > 0 && p1 < pend) { 50 6519560 : *p1 = ::HexDigit(psz[--digits]); 51 6519560 : if (digits > 0) { 52 6518560 : *p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4); 53 6518560 : p1++; 54 : } 55 : } 56 204692 : } 57 : 58 : template <unsigned int BITS> 59 7529 : void base_blob<BITS>::SetHex(const std::string& str) 60 : { 61 7529 : SetHex(str.c_str()); 62 7529 : } 63 : 64 : template <unsigned int BITS> 65 5138269 : std::string base_blob<BITS>::ToString() const 66 : { 67 5138269 : return GetHex(); 68 : } 69 : 70 : // Explicit instantiations for base_blob<160> 71 : template base_blob<160>::base_blob(const std::vector<unsigned char>&); 72 : template std::string base_blob<160>::GetHex() const; 73 : template std::string base_blob<160>::ToString() const; 74 : template void base_blob<160>::SetHex(const char*); 75 : template void base_blob<160>::SetHex(const std::string&); 76 : 77 : // Explicit instantiations for base_blob<256> 78 : template base_blob<256>::base_blob(const std::vector<unsigned char>&); 79 : template std::string base_blob<256>::GetHex() const; 80 : template std::string base_blob<256>::ToString() const; 81 : template void base_blob<256>::SetHex(const char*); 82 : template void base_blob<256>::SetHex(const std::string&); 83 : 84 : // Explicit instantiations for base_blob<512> 85 : template base_blob<512>::base_blob(const std::vector<unsigned char>&); 86 : template std::string base_blob<512>::GetHex() const; 87 : template std::string base_blob<512>::ToString() const; 88 : template void base_blob<512>::SetHex(const char*); 89 : template void base_blob<512>::SetHex(const std::string&); 90 :