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