Bitcoin Core  0.20.99
P2P Digital Currency
uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 #ifndef BITCOIN_UINT256_H
7 #define BITCOIN_UINT256_H
8 
9 #include <assert.h>
10 #include <cstring>
11 #include <stdint.h>
12 #include <string>
13 #include <vector>
14 
16 template<unsigned int BITS>
17 class base_blob
18 {
19 protected:
20  static constexpr int WIDTH = BITS / 8;
21  uint8_t m_data[WIDTH];
22 public:
24  {
25  memset(m_data, 0, sizeof(m_data));
26  }
27 
28  explicit base_blob(const std::vector<unsigned char>& vch);
29 
30  bool IsNull() const
31  {
32  for (int i = 0; i < WIDTH; i++)
33  if (m_data[i] != 0)
34  return false;
35  return true;
36  }
37 
38  void SetNull()
39  {
40  memset(m_data, 0, sizeof(m_data));
41  }
42 
43  inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); }
44 
45  friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
46  friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
47  friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
48 
49  std::string GetHex() const;
50  void SetHex(const char* psz);
51  void SetHex(const std::string& str);
52  std::string ToString() const;
53 
54  const unsigned char* data() const { return m_data; }
55  unsigned char* data() { return m_data; }
56 
57  unsigned char* begin()
58  {
59  return &m_data[0];
60  }
61 
62  unsigned char* end()
63  {
64  return &m_data[WIDTH];
65  }
66 
67  const unsigned char* begin() const
68  {
69  return &m_data[0];
70  }
71 
72  const unsigned char* end() const
73  {
74  return &m_data[WIDTH];
75  }
76 
77  unsigned int size() const
78  {
79  return sizeof(m_data);
80  }
81 
82  uint64_t GetUint64(int pos) const
83  {
84  const uint8_t* ptr = m_data + pos * 8;
85  return ((uint64_t)ptr[0]) | \
86  ((uint64_t)ptr[1]) << 8 | \
87  ((uint64_t)ptr[2]) << 16 | \
88  ((uint64_t)ptr[3]) << 24 | \
89  ((uint64_t)ptr[4]) << 32 | \
90  ((uint64_t)ptr[5]) << 40 | \
91  ((uint64_t)ptr[6]) << 48 | \
92  ((uint64_t)ptr[7]) << 56;
93  }
94 
95  template<typename Stream>
96  void Serialize(Stream& s) const
97  {
98  s.write((char*)m_data, sizeof(m_data));
99  }
100 
101  template<typename Stream>
102  void Unserialize(Stream& s)
103  {
104  s.read((char*)m_data, sizeof(m_data));
105  }
106 };
107 
112 class uint160 : public base_blob<160> {
113 public:
114  uint160() {}
115  explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
116 };
117 
123 class uint256 : public base_blob<256> {
124 public:
125  uint256() {}
126  explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
127 };
128 
129 /* uint256 from const char *.
130  * This is a separate function because the constructor uint256(const char*) can result
131  * in dangerously catching uint256(0).
132  */
133 inline uint256 uint256S(const char *str)
134 {
135  uint256 rv;
136  rv.SetHex(str);
137  return rv;
138 }
139 /* uint256 from std::string.
140  * This is a separate function because the constructor uint256(const std::string &str) can result
141  * in dangerously catching uint256(0) via std::string(const char*).
142  */
143 inline uint256 uint256S(const std::string& str)
144 {
145  uint256 rv;
146  rv.SetHex(str);
147  return rv;
148 }
149 
151 
152 #endif // BITCOIN_UINT256_H
base_blob()
Definition: uint256.h:23
void SetNull()
Definition: uint256.h:38
unsigned char * data()
Definition: uint256.h:55
const unsigned char * begin() const
Definition: uint256.h:67
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:45
void Serialize(Stream &s) const
Definition: uint256.h:96
const unsigned char * data() const
Definition: uint256.h:54
uint160()
Definition: uint256.h:114
unsigned char * begin()
Definition: uint256.h:57
bool IsNull() const
Definition: uint256.h:30
unsigned char * end()
Definition: uint256.h:62
int Compare(const base_blob &other) const
Definition: uint256.h:43
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: uint256.h:46
const unsigned char * end() const
Definition: uint256.h:72
uint256(const std::vector< unsigned char > &vch)
Definition: uint256.h:126
void Unserialize(Stream &s)
Definition: uint256.h:102
uint256 uint256S(const char *str)
Definition: uint256.h:133
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: uint256.h:47
std::string ToString() const
Definition: uint256.cpp:60
unsigned int size() const
Definition: uint256.h:77
uint256()
Definition: uint256.h:125
uint256 & UINT256_ONE()
Definition: uint256.cpp:79
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:17
256-bit opaque blob.
Definition: uint256.h:123
static constexpr int WIDTH
Definition: uint256.h:20
std::string GetHex() const
Definition: uint256.cpp:20
160-bit opaque blob.
Definition: uint256.h:112
uint160(const std::vector< unsigned char > &vch)
Definition: uint256.h:115
uint64_t GetUint64(int pos) const
Definition: uint256.h:82
void SetHex(const char *psz)
Definition: uint256.cpp:26
uint8_t m_data[WIDTH]
Definition: uint256.h:21