diff --git a/src/uint256.cpp b/src/uint256.cpp index 62841f348..7f0e4879f 100644 --- a/src/uint256.cpp +++ b/src/uint256.cpp @@ -1,74 +1,75 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include template base_blob::base_blob(const std::vector &vch) { - assert(vch.size() == sizeof(data)); - memcpy(data, vch.data(), sizeof(data)); + assert(vch.size() == sizeof(m_data)); + memcpy(m_data, vch.data(), sizeof(m_data)); } template std::string base_blob::GetHex() const { - return HexStr(std::reverse_iterator(data + sizeof(data)), - std::reverse_iterator(data)); + return HexStr( + std::reverse_iterator(m_data + sizeof(m_data)), + std::reverse_iterator(m_data)); } template void base_blob::SetHex(const char *psz) { - memset(data, 0, sizeof(data)); + memset(m_data, 0, sizeof(m_data)); // skip leading spaces while (IsSpace(*psz)) { psz++; } // skip 0x if (psz[0] == '0' && ToLower(psz[1]) == 'x') { psz += 2; } // hex string to uint size_t digits = 0; while (::HexDigit(psz[digits]) != -1) { digits++; } - uint8_t *p1 = (uint8_t *)data; + uint8_t *p1 = (uint8_t *)m_data; uint8_t *pend = p1 + WIDTH; while (digits > 0 && p1 < pend) { *p1 = ::HexDigit(psz[--digits]); if (digits > 0) { *p1 |= uint8_t(::HexDigit(psz[--digits])) << 4; p1++; } } } template void base_blob::SetHex(const std::string &str) { SetHex(str.c_str()); } // Explicit instantiations for base_blob<160> template base_blob<160>::base_blob(const std::vector &); template std::string base_blob<160>::GetHex() const; template std::string base_blob<160>::ToString() const; template void base_blob<160>::SetHex(const char *); template void base_blob<160>::SetHex(const std::string &); // Explicit instantiations for base_blob<256> template base_blob<256>::base_blob(const std::vector &); template std::string base_blob<256>::GetHex() const; template std::string base_blob<256>::ToString() const; template void base_blob<256>::SetHex(const char *); template void base_blob<256>::SetHex(const std::string &); uint256 &UINT256_ONE() { static uint256 one = uint256S( "0000000000000000000000000000000000000000000000000000000000000001"); return one; } diff --git a/src/uint256.h b/src/uint256.h index 5ed65e9fd..1514d8304 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -1,162 +1,162 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_UINT256_H #define BITCOIN_UINT256_H #include #include #include #include #include /** Template base class for fixed-sized opaque blobs. */ template class base_blob { protected: static constexpr int WIDTH = BITS / 8; - uint8_t data[WIDTH]; + uint8_t m_data[WIDTH]; public: - base_blob() { memset(data, 0, sizeof(data)); } + base_blob() { memset(m_data, 0, sizeof(m_data)); } explicit base_blob(const std::vector &vch); bool IsNull() const { for (int i = 0; i < WIDTH; i++) { - if (data[i] != 0) { + if (m_data[i] != 0) { return false; } } 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 { - for (size_t i = 0; i < sizeof(data); i++) { - uint8_t a = data[sizeof(data) - 1 - i]; - uint8_t b = other.data[sizeof(data) - 1 - i]; + for (size_t i = 0; i < sizeof(m_data); i++) { + uint8_t a = m_data[sizeof(m_data) - 1 - i]; + uint8_t b = other.m_data[sizeof(m_data) - 1 - i]; if (a > b) { return 1; } if (a < b) { return -1; } } return 0; } friend inline bool operator==(const base_blob &a, const base_blob &b) { return a.Compare(b) == 0; } friend inline bool operator!=(const base_blob &a, const base_blob &b) { return a.Compare(b) != 0; } friend inline bool operator<(const base_blob &a, const base_blob &b) { return a.Compare(b) < 0; } friend inline bool operator<=(const base_blob &a, const base_blob &b) { return a.Compare(b) <= 0; } friend inline bool operator>(const base_blob &a, const base_blob &b) { return a.Compare(b) > 0; } friend inline bool operator>=(const base_blob &a, const base_blob &b) { return a.Compare(b) >= 0; } std::string GetHex() const; void SetHex(const char *psz); void SetHex(const std::string &str); 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 { - 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) | (uint64_t(ptr[2]) << 16) | (uint64_t(ptr[3]) << 24) | (uint64_t(ptr[4]) << 32) | (uint64_t(ptr[5]) << 40) | (uint64_t(ptr[6]) << 48) | (uint64_t(ptr[7]) << 56); } template void Serialize(Stream &s) const { - s.write((char *)data, sizeof(data)); + s.write((char *)m_data, sizeof(m_data)); } template void Unserialize(Stream &s) { - s.read((char *)data, sizeof(data)); + s.read((char *)m_data, sizeof(m_data)); } }; /** * 160-bit opaque blob. * @note This type is called uint160 for historical reasons only. It is an * opaque blob of 160 bits and has no integer operations. */ class uint160 : public base_blob<160> { public: uint160() {} explicit uint160(const std::vector &vch) : base_blob<160>(vch) {} }; /** * 256-bit opaque blob. * @note This type is called uint256 for historical reasons only. It is an * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if * those are required. */ class uint256 : public base_blob<256> { public: uint256() {} explicit uint256(const std::vector &vch) : base_blob<256>(vch) {} }; /** * uint256 from const char *. * This is a separate function because the constructor uint256(const char*) can * result in dangerously catching uint256(0). */ inline uint256 uint256S(const char *str) { uint256 rv; rv.SetHex(str); return rv; } /** * uint256 from std::string. * This is a separate function because the constructor uint256(const std::string * &str) can result in dangerously catching uint256(0) via std::string(const * char*). */ inline uint256 uint256S(const std::string &str) { uint256 rv; rv.SetHex(str); return rv; } inline uint160 uint160S(const char *str) { uint160 rv; rv.SetHex(str); return rv; } inline uint160 uint160S(const std::string &str) { uint160 rv; rv.SetHex(str); return rv; } uint256 &UINT256_ONE(); #endif // BITCOIN_UINT256_H