/* * This file is part of the Flowee project * Copyright (C) 2009-2010 Satoshi Nakamoto * Copyright (C) 2009-2015 The Bitcoin Core developers * Copyright (C) 2019 Tom Zander * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef FLOWEE_UINT256_H #define FLOWEE_UINT256_H #include #include "Logger.h" #include #include #include #include #include #include /** Template base class for fixed-sized opaque blobs. */ template class base_blob { protected: enum { WIDTH=BITS/8 }; uint8_t data[WIDTH]; public: base_blob() { memset(data, 0, sizeof(data)); } explicit base_blob(const std::vector& vch); explicit base_blob(const char *_data) { memcpy(data, _data, WIDTH); } bool IsNull() const { for (int i = 0; i < WIDTH; i++) if (data[i] != 0) return false; return true; } void SetNull() { memset(data, 0, sizeof(data)); } inline int Compare(const base_blob &other) const { for (size_t i = sizeof(data); i > 0; --i) { const uint8_t a = data[i - 1]; const uint8_t b = other.data[i - 1]; 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 memcmp(a.data, b.data, sizeof(a.data)) == 0; } friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; } friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; } std::string GetHex() const; void SetHex(const char* psz); void SetHex(const std::string& str); std::string ToString() const; unsigned char* begin() { return &data[0]; } unsigned char* end() { return &data[WIDTH]; } const unsigned char* begin() const { return &data[0]; } const unsigned char* end() const { return &data[WIDTH]; } unsigned int size() const { return sizeof(data); } unsigned int GetSerializeSize(int nType, int nVersion) const { return sizeof(data); } template void Serialize(Stream& s, int nType, int nVersion) const { s.write((char*)data, sizeof(data)); } template void Unserialize(Stream& s, int nType, int nVersion) { s.read((char*)data, sizeof(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() {} uint160(const base_blob<160>& b) : base_blob<160>(b) {} explicit uint160(const char *d) : base_blob<160>(d) {} 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() {} uint256(const char *data) : base_blob<256>(data) {} uint256(const base_blob<256>& b) : base_blob<256>(b) {} explicit uint256(const std::vector& vch) : base_blob<256>(vch) {} /** A cheap hash function that just returns 64 bits from the result, it can be * used when the contents are considered uniformly random. It is not appropriate * when the value can easily be influenced from outside as e.g. a network adversary could * provide values to trigger worst-case behavior. */ uint64_t GetCheapHash() const { return ReadLE64(data); } }; /* 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; } /** * Useful structure to use for maps. * For instance: * * typedef boost::unordered_map MyMap; */ struct HashShortener { inline size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); } }; template inline Log::Item operator<<(Log::Item item, const base_blob &data) { if (item.isEnabled()) item << data.ToString().c_str(); return item; } template inline Log::SilentItem operator<<(Log::SilentItem item, const base_blob &data) { return item; } #endif