Bitcoin Core  0.18.99
P2P Digital Currency
uint256.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <uint256.h>
7 
8 #include <util/strencodings.h>
9 
10 #include <stdio.h>
11 #include <string.h>
12 
13 template <unsigned int BITS>
14 base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
15 {
16  assert(vch.size() == sizeof(data));
17  memcpy(data, vch.data(), sizeof(data));
18 }
19 
20 template <unsigned int BITS>
21 std::string base_blob<BITS>::GetHex() const
22 {
23  return HexStr(std::reverse_iterator<const uint8_t*>(data + sizeof(data)), std::reverse_iterator<const uint8_t*>(data));
24 }
25 
26 template <unsigned int BITS>
27 void base_blob<BITS>::SetHex(const char* psz)
28 {
29  memset(data, 0, sizeof(data));
30 
31  // skip leading spaces
32  while (IsSpace(*psz))
33  psz++;
34 
35  // skip 0x
36  if (psz[0] == '0' && ToLower(psz[1]) == 'x')
37  psz += 2;
38 
39  // hex string to uint
40  const char* pbegin = psz;
41  while (::HexDigit(*psz) != -1)
42  psz++;
43  psz--;
44  unsigned char* p1 = (unsigned char*)data;
45  unsigned char* pend = p1 + WIDTH;
46  while (psz >= pbegin && p1 < pend) {
47  *p1 = ::HexDigit(*psz--);
48  if (psz >= pbegin) {
49  *p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
50  p1++;
51  }
52  }
53 }
54 
55 template <unsigned int BITS>
56 void base_blob<BITS>::SetHex(const std::string& str)
57 {
58  SetHex(str.c_str());
59 }
60 
61 template <unsigned int BITS>
62 std::string base_blob<BITS>::ToString() const
63 {
64  return (GetHex());
65 }
66 
67 // Explicit instantiations for base_blob<160>
68 template base_blob<160>::base_blob(const std::vector<unsigned char>&);
69 template std::string base_blob<160>::GetHex() const;
70 template std::string base_blob<160>::ToString() const;
71 template void base_blob<160>::SetHex(const char*);
72 template void base_blob<160>::SetHex(const std::string&);
73 
74 // Explicit instantiations for base_blob<256>
75 template base_blob<256>::base_blob(const std::vector<unsigned char>&);
76 template std::string base_blob<256>::GetHex() const;
77 template std::string base_blob<256>::ToString() const;
78 template void base_blob<256>::SetHex(const char*);
79 template void base_blob<256>::SetHex(const std::string&);
base_blob()
Definition: uint256.h:24
signed char HexDigit(char c)
std::string ToString() const
Definition: uint256.cpp:62
void * memcpy(void *a, const void *b, size_t c)
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:85
std::string GetHex() const
Definition: uint256.cpp:21
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:125
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
Definition: strencodings.h:206
void SetHex(const char *psz)
Definition: uint256.cpp:27