Fabcoin Core  0.16.2
P2P Digital Currency
uint256.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 The Fabcoin 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 <utilstrencodings.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[0], 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 std::string base_blob<BITS>::GetReverseHex() const
28 {
29  char psz[sizeof(data) * 2 + 1];
30  int j=0;
31  for (int i = sizeof(data)-1; i >= 0; i--) {
32  sprintf(psz + j * 2, "%02x", data[sizeof(data) - i - 1]);
33  j++;
34  }
35  return std::string(psz, psz + sizeof(data) * 2);
36 }
37 
38 template <unsigned int BITS>
39 void base_blob<BITS>::SetHex(const char* psz)
40 {
41  memset(data, 0, sizeof(data));
42 
43  // skip leading spaces
44  while (isspace(*psz))
45  psz++;
46 
47  // skip 0x
48  if (psz[0] == '0' && tolower(psz[1]) == 'x')
49  psz += 2;
50 
51  // hex string to uint
52  const char* pbegin = psz;
53  while (::HexDigit(*psz) != -1)
54  psz++;
55  psz--;
56  unsigned char* p1 = (unsigned char*)data;
57  unsigned char* pend = p1 + WIDTH;
58  while (psz >= pbegin && p1 < pend) {
59  *p1 = ::HexDigit(*psz--);
60  if (psz >= pbegin) {
61  *p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
62  p1++;
63  }
64  }
65 }
66 
67 template <unsigned int BITS>
68 void base_blob<BITS>::SetHex(const std::string& str)
69 {
70  SetHex(str.c_str());
71 }
72 
73 template <unsigned int BITS>
74 void base_blob<BITS>::SetReverseHex(const char* psz)
75 {
76  SetHex(psz);
77  //reverse in-place
78  int left = 0;
79  int right = size()-1;
80  unsigned char* p1 = (unsigned char*)data;
81  while(left < right){
82  int temp=p1[left];
83  p1[left++] = p1[right];
84  p1[right--] = temp;
85  }
86 }
87 
88 template <unsigned int BITS>
89 void base_blob<BITS>::SetReverseHex(const std::string& str)
90 {
91  SetReverseHex(str.c_str());
92 }
93 
94 template <unsigned int BITS>
95 std::string base_blob<BITS>::ToString() const
96 {
97  return (GetHex());
98 }
99 
100 // Explicit instantiations for base_blob<160>
101 template base_blob<160>::base_blob(const std::vector<unsigned char>&);
102 template std::string base_blob<160>::GetHex() const;
103 template std::string base_blob<160>::ToString() const;
104 template void base_blob<160>::SetHex(const char*);
105 template void base_blob<160>::SetHex(const std::string&);
106 template std::string base_blob<160>::GetReverseHex() const;
107 template void base_blob<160>::SetReverseHex(const char*);
108 template void base_blob<160>::SetReverseHex(const std::string&);
109 
110 // Explicit instantiations for base_blob<256>
111 template base_blob<256>::base_blob(const std::vector<unsigned char>&);
112 template std::string base_blob<256>::GetHex() const;
113 template std::string base_blob<256>::ToString() const;
114 template void base_blob<256>::SetHex(const char*);
115 template void base_blob<256>::SetHex(const std::string&);
116 template std::string base_blob<256>::GetReverseHex() const;
117 template void base_blob<256>::SetReverseHex(const char*);
118 template void base_blob<256>::SetReverseHex(const std::string&);
base_blob()
Definition: uint256.h:31
std::string GetHex() const
Definition: uint256.cpp:21
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
assert(len-trim+(2 *lenIndices)<=WIDTH)
std::string ToString() const
Definition: uint256.cpp:95
void SetReverseHex(const char *psz)
Definition: uint256.cpp:74
std::string GetReverseHex() const
Definition: uint256.cpp:27
uint8_t const size_t const size
Definition: sha3.h:20
void * memcpy(void *a, const void *b, size_t c)
signed char HexDigit(char c)
void SetHex(const char *psz)
Definition: uint256.cpp:39
uint8_t const * data
Definition: sha3.h:19