Changeset View
Changeset View
Standalone View
Standalone View
src/uint256.h
Show All 10 Lines | |||||
#include <cstring> | #include <cstring> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
/** Template base class for fixed-sized opaque blobs. */ | /** Template base class for fixed-sized opaque blobs. */ | ||||
template <unsigned int BITS> class base_blob { | template <unsigned int BITS> class base_blob { | ||||
protected: | protected: | ||||
static constexpr int WIDTH = BITS / 8; | static constexpr int WIDTH = BITS / 8; | ||||
uint8_t data[WIDTH]; | uint8_t m_data[WIDTH]; | ||||
public: | public: | ||||
base_blob() { memset(data, 0, sizeof(data)); } | base_blob() { memset(m_data, 0, sizeof(m_data)); } | ||||
explicit base_blob(const std::vector<uint8_t> &vch); | explicit base_blob(const std::vector<uint8_t> &vch); | ||||
bool IsNull() const { | bool IsNull() const { | ||||
for (int i = 0; i < WIDTH; i++) { | for (int i = 0; i < WIDTH; i++) { | ||||
if (data[i] != 0) { | if (m_data[i] != 0) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
void SetNull() { memset(data, 0, sizeof(data)); } | void SetNull() { memset(m_data, 0, sizeof(m_data)); } | ||||
inline int Compare(const base_blob &other) const { | inline int Compare(const base_blob &other) const { | ||||
for (size_t i = 0; i < sizeof(data); i++) { | for (size_t i = 0; i < sizeof(m_data); i++) { | ||||
uint8_t a = data[sizeof(data) - 1 - i]; | uint8_t a = m_data[sizeof(m_data) - 1 - i]; | ||||
uint8_t b = other.data[sizeof(data) - 1 - i]; | uint8_t b = other.m_data[sizeof(m_data) - 1 - i]; | ||||
if (a > b) { | if (a > b) { | ||||
return 1; | return 1; | ||||
} | } | ||||
if (a < b) { | if (a < b) { | ||||
return -1; | return -1; | ||||
} | } | ||||
} | } | ||||
Show All 19 Lines | friend inline bool operator>=(const base_blob &a, const base_blob &b) { | ||||
return a.Compare(b) >= 0; | return a.Compare(b) >= 0; | ||||
} | } | ||||
std::string GetHex() const; | std::string GetHex() const; | ||||
void SetHex(const char *psz); | void SetHex(const char *psz); | ||||
void SetHex(const std::string &str); | void SetHex(const std::string &str); | ||||
std::string ToString() const { return GetHex(); } | std::string ToString() const { return GetHex(); } | ||||
uint8_t *begin() { return &data[0]; } | uint8_t *begin() { return &m_data[0]; } | ||||
uint8_t *end() { return &data[WIDTH]; } | uint8_t *end() { return &m_data[WIDTH]; } | ||||
const uint8_t *begin() const { return &data[0]; } | const uint8_t *begin() const { return &m_data[0]; } | ||||
const uint8_t *end() const { return &data[WIDTH]; } | const uint8_t *end() const { return &m_data[WIDTH]; } | ||||
unsigned int size() const { return sizeof(data); } | unsigned int size() const { return sizeof(m_data); } | ||||
uint64_t GetUint64(int pos) const { | uint64_t GetUint64(int pos) const { | ||||
const uint8_t *ptr = data + pos * 8; | const uint8_t *ptr = m_data + pos * 8; | ||||
return uint64_t(ptr[0]) | (uint64_t(ptr[1]) << 8) | | return uint64_t(ptr[0]) | (uint64_t(ptr[1]) << 8) | | ||||
(uint64_t(ptr[2]) << 16) | (uint64_t(ptr[3]) << 24) | | (uint64_t(ptr[2]) << 16) | (uint64_t(ptr[3]) << 24) | | ||||
(uint64_t(ptr[4]) << 32) | (uint64_t(ptr[5]) << 40) | | (uint64_t(ptr[4]) << 32) | (uint64_t(ptr[5]) << 40) | | ||||
(uint64_t(ptr[6]) << 48) | (uint64_t(ptr[7]) << 56); | (uint64_t(ptr[6]) << 48) | (uint64_t(ptr[7]) << 56); | ||||
} | } | ||||
template <typename Stream> void Serialize(Stream &s) const { | template <typename Stream> void Serialize(Stream &s) const { | ||||
s.write((char *)data, sizeof(data)); | s.write((char *)m_data, sizeof(m_data)); | ||||
} | } | ||||
template <typename Stream> void Unserialize(Stream &s) { | template <typename Stream> void Unserialize(Stream &s) { | ||||
s.read((char *)data, sizeof(data)); | s.read((char *)m_data, sizeof(m_data)); | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* 160-bit opaque blob. | * 160-bit opaque blob. | ||||
* @note This type is called uint160 for historical reasons only. It is an | * @note This type is called uint160 for historical reasons only. It is an | ||||
* opaque blob of 160 bits and has no integer operations. | * opaque blob of 160 bits and has no integer operations. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |