PRCYCoin  2.0.0.7rc1
P2P Digital Currency
blob_uint256.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2014-2015 The Dash developers
4 // Copyright (c) 2015-2018 The PIVX developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #ifndef PRCY_BLOB_UINT256_H
9 #define PRCY_BLOB_UINT256_H
10 
11 #include <assert.h>
12 #include <cstring>
13 #include <stdexcept>
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17 
19 template<unsigned int BITS>
20 class base_blob
21 {
22 public:
23  // todo: make this protected
24  enum { WIDTH=BITS/8 };
25  uint8_t data[WIDTH];
26 
28  {
29  SetNull();
30  }
31 
32  explicit base_blob(const std::vector<unsigned char>& vch);
33 
34  bool IsNull() const
35  {
36  for (int i = 0; i < WIDTH; i++)
37  if (data[i] != 0)
38  return false;
39  return true;
40  }
41 
42  void SetNull()
43  {
44  memset(data, 0, sizeof(data));
45  }
46 
47  friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; }
48  friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; }
49  friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; }
50 
51  std::string GetHex() const;
52  void SetHex(const char* psz);
53  void SetHex(const std::string& str);
54  std::string ToString() const;
55 
56  unsigned char* begin()
57  {
58  return &data[0];
59  }
60 
61  unsigned char* end()
62  {
63  return &data[WIDTH];
64  }
65 
66  const unsigned char* begin() const
67  {
68  return &data[0];
69  }
70 
71  const unsigned char* end() const
72  {
73  return &data[WIDTH];
74  }
75 
76  unsigned int size() const
77  {
78  return sizeof(data);
79  }
80 
81  unsigned int GetSerializeSize(int nType, int nVersion) const
82  {
83  return sizeof(data);
84  }
85 
86  template<typename Stream>
87  void Serialize(Stream& s, int nType, int nVersion) const
88  {
89  s.write((char*)data, sizeof(data));
90  }
91 
92  template<typename Stream>
93  void Unserialize(Stream& s, int nType, int nVersion)
94  {
95  s.read((char*)data, sizeof(data));
96  }
97 };
98 
103 class blob_uint160 : public base_blob<160> {
104 public:
107  explicit blob_uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
108 };
109 
115 class blob_uint256 : public base_blob<256> {
116 public:
119  explicit blob_uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
120 
127  uint64_t GetCheapHash() const
128  {
129  uint64_t result;
130  memcpy((void*)&result, (void*)data, 8);
131  return result;
132  }
133 
137  uint64_t GetHash(const blob_uint256& salt) const;
138 };
139 
140 /* uint256 from const char *.
141  * This is a separate function because the constructor uint256(const char*) can result
142  * in dangerously catching uint256(0).
143  */
144 inline blob_uint256 blob_uint256S(const char *str)
145 {
146  blob_uint256 rv;
147  rv.SetHex(str);
148  return rv;
149 }
150 /* uint256 from std::string.
151  * This is a separate function because the constructor uint256(const std::string &str) can result
152  * in dangerously catching uint256(0) via std::string(const char*).
153  */
154 inline blob_uint256 blob_uint256S(const std::string& str)
155 {
156  return blob_uint256S(str.c_str());
157 }
158 
161 const blob_uint256 BLOB_UINT256_ONE = blob_uint256S("0000000000000000000000000000000000000000000000000000000000000001");
162 
163 #endif // PRCY_BLOB_UINT256_H
BLOB_UINT256_ONE
const blob_uint256 BLOB_UINT256_ONE
Definition: blob_uint256.h:161
blob_uint256::GetHash
uint64_t GetHash(const blob_uint256 &salt) const
A more secure, salted hash function.
Definition: blob_uint256.cpp:126
blob_uint256S
blob_uint256 blob_uint256S(const char *str)
Definition: blob_uint256.h:144
blob_uint160::blob_uint160
blob_uint160(const base_blob< 160 > &b)
Definition: blob_uint256.h:106
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
blob_uint256::blob_uint256
blob_uint256(const base_blob< 256 > &b)
Definition: blob_uint256.h:118
base_blob::operator!=
friend bool operator!=(const base_blob &a, const base_blob &b)
Definition: blob_uint256.h:48
base_blob::Unserialize
void Unserialize(Stream &s, int nType, int nVersion)
Definition: blob_uint256.h:93
blob_uint160::blob_uint160
blob_uint160(const std::vector< unsigned char > &vch)
Definition: blob_uint256.h:107
base_blob::base_blob
base_blob()
Definition: blob_uint256.h:27
blob_uint256::GetCheapHash
uint64_t GetCheapHash() const
A cheap hash function that just returns 64 bits from the result, it can be used when the contents are...
Definition: blob_uint256.h:127
base_blob::SetNull
void SetNull()
Definition: blob_uint256.h:42
base_blob::Serialize
void Serialize(Stream &s, int nType, int nVersion) const
Definition: blob_uint256.h:87
base_blob
Template base class for fixed-sized opaque blobs.
Definition: blob_uint256.h:20
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:15
blob_uint160
160-bit opaque blob.
Definition: blob_uint256.h:103
blob_uint256::blob_uint256
blob_uint256()
Definition: blob_uint256.h:117
base_blob::GetSerializeSize
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: blob_uint256.h:81
base_blob::GetHex
std::string GetHex() const
Definition: blob_uint256.cpp:21
base_blob::operator<
friend bool operator<(const base_blob &a, const base_blob &b)
Definition: blob_uint256.h:49
base_blob::operator==
friend bool operator==(const base_blob &a, const base_blob &b)
Definition: blob_uint256.h:47
base_blob::ToString
std::string ToString() const
Definition: blob_uint256.cpp:65
base_blob::end
const unsigned char * end() const
Definition: blob_uint256.h:71
base_blob::IsNull
bool IsNull() const
Definition: blob_uint256.h:34
blob_uint256::blob_uint256
blob_uint256(const std::vector< unsigned char > &vch)
Definition: blob_uint256.h:119
blob_uint160::blob_uint160
blob_uint160()
Definition: blob_uint256.h:105
blob_uint256
256-bit opaque blob.
Definition: blob_uint256.h:115
base_blob::WIDTH
@ WIDTH
Definition: blob_uint256.h:24
base_blob::begin
const unsigned char * begin() const
Definition: blob_uint256.h:66
base_blob::data
uint8_t data[WIDTH]
Definition: blob_uint256.h:25
base_blob::size
unsigned int size() const
Definition: blob_uint256.h:76
base_blob::SetHex
void SetHex(const char *psz)
Definition: blob_uint256.cpp:30
BLOB_UINT256_ZERO
const blob_uint256 BLOB_UINT256_ZERO
constant uint256 instances
Definition: blob_uint256.h:160
base_blob::begin
unsigned char * begin()
Definition: blob_uint256.h:56
base_blob::end
unsigned char * end()
Definition: blob_uint256.h:61