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