Bitcoin Core  0.19.99
P2P Digital Currency
uint256.h
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 #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 data[WIDTH];
22 public:
24  {
25  memset(data, 0, sizeof(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 (data[i] != 0)
34  return false;
35  return true;
36  }
37 
38  void SetNull()
39  {
40  memset(data, 0, sizeof(data));
41  }
42 
43  inline int Compare(const base_blob& other) const { return memcmp(data, other.data, sizeof(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  unsigned char* begin()
55  {
56  return &data[0];
57  }
58 
59  unsigned char* end()
60  {
61  return &data[WIDTH];
62  }
63 
64  const unsigned char* begin() const
65  {
66  return &data[0];
67  }
68 
69  const unsigned char* end() const
70  {
71  return &data[WIDTH];
72  }
73 
74  unsigned int size() const
75  {
76  return sizeof(data);
77  }
78 
79  uint64_t GetUint64(int pos) const
80  {
81  const uint8_t* ptr = data + pos * 8;
82  return ((uint64_t)ptr[0]) | \
83  ((uint64_t)ptr[1]) << 8 | \
84  ((uint64_t)ptr[2]) << 16 | \
85  ((uint64_t)ptr[3]) << 24 | \
86  ((uint64_t)ptr[4]) << 32 | \
87  ((uint64_t)ptr[5]) << 40 | \
88  ((uint64_t)ptr[6]) << 48 | \
89  ((uint64_t)ptr[7]) << 56;
90  }
91 
92  template<typename Stream>
93  void Serialize(Stream& s) const
94  {
95  s.write((char*)data, sizeof(data));
96  }
97 
98  template<typename Stream>
99  void Unserialize(Stream& s)
100  {
101  s.read((char*)data, sizeof(data));
102  }
103 };
104 
109 class uint160 : public base_blob<160> {
110 public:
111  uint160() {}
112  explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
113 };
114 
120 class uint256 : public base_blob<256> {
121 public:
122  uint256() {}
123  explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
124 };
125 
126 /* uint256 from const char *.
127  * This is a separate function because the constructor uint256(const char*) can result
128  * in dangerously catching uint256(0).
129  */
130 inline uint256 uint256S(const char *str)
131 {
132  uint256 rv;
133  rv.SetHex(str);
134  return rv;
135 }
136 /* uint256 from std::string.
137  * This is a separate function because the constructor uint256(const std::string &str) can result
138  * in dangerously catching uint256(0) via std::string(const char*).
139  */
140 inline uint256 uint256S(const std::string& str)
141 {
142  uint256 rv;
143  rv.SetHex(str);
144  return rv;
145 }
146 
147 #endif // BITCOIN_UINT256_H
uint8_t data[WIDTH]
Definition: uint256.h:21
base_blob()
Definition: uint256.h:23
void SetNull()
Definition: uint256.h:38
const unsigned char * begin() const
Definition: uint256.h:64
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: uint256.h:45
void Serialize(Stream &s) const
Definition: uint256.h:93
uint160()
Definition: uint256.h:111
unsigned char * begin()
Definition: uint256.h:54
bool IsNull() const
Definition: uint256.h:30
unsigned char * end()
Definition: uint256.h:59
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:69
uint256(const std::vector< unsigned char > &vch)
Definition: uint256.h:123
void Unserialize(Stream &s)
Definition: uint256.h:99
uint256 uint256S(const char *str)
Definition: uint256.h:130
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:74
uint256()
Definition: uint256.h:122
Template base class for fixed-sized opaque blobs.
Definition: uint256.h:17
256-bit opaque blob.
Definition: uint256.h:120
static constexpr int WIDTH
Definition: uint256.h:20
std::string GetHex() const
Definition: uint256.cpp:20
160-bit opaque blob.
Definition: uint256.h:109
uint160(const std::vector< unsigned char > &vch)
Definition: uint256.h:112
uint64_t GetUint64(int pos) const
Definition: uint256.h:79
void SetHex(const char *psz)
Definition: uint256.cpp:26