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 1145406 : base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch) 15 : { 16 1145406 : assert(vch.size() == sizeof(m_data)); 17 1145406 : memcpy(m_data, vch.data(), sizeof(m_data)); 18 1145406 : } 19 : 20 : template <unsigned int BITS> 21 3270482 : std::string base_blob<BITS>::GetHex() const 22 : { 23 : uint8_t m_data_rev[WIDTH]; 24 107920786 : for (int i = 0; i < WIDTH; ++i) { 25 104650444 : m_data_rev[i] = m_data[WIDTH - 1 - i]; 26 : } 27 3270482 : return HexStr(m_data_rev); 28 : } 29 : 30 : template <unsigned int BITS> 31 175912 : void base_blob<BITS>::SetHex(const char* psz) 32 : { 33 175912 : memset(m_data, 0, sizeof(m_data)); 34 : 35 : // skip leading spaces 36 175918 : while (isspace(*psz)) 37 6 : psz++; 38 : 39 : // skip 0x 40 175912 : if (psz[0] == '0' && tolower(psz[1]) == 'x') 41 8289 : psz += 2; 42 : 43 : // hex string to uint 44 175912 : size_t digits = 0; 45 11379816 : while (::HexDigit(psz[digits]) != -1) 46 11203914 : digits++; 47 175912 : unsigned char* p1 = (unsigned char*)m_data; 48 175912 : unsigned char* pend = p1 + WIDTH; 49 5778292 : while (digits > 0 && p1 < pend) { 50 5602380 : *p1 = ::HexDigit(psz[--digits]); 51 5602380 : if (digits > 0) { 52 5601500 : *p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4); 53 5601500 : p1++; 54 : } 55 : } 56 175912 : } 57 : 58 : template <unsigned int BITS> 59 1771 : void base_blob<BITS>::SetHex(const std::string& str) 60 : { 61 1771 : SetHex(str.c_str()); 62 1771 : } 63 : 64 : template <unsigned int BITS> 65 3201731 : std::string base_blob<BITS>::ToString() const 66 : { 67 3201731 : 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 :