X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fserialize.h;h=4bfff0a1a29ccf81dd8bf0eab603d8f43c2e9224;hb=21d9f36781604e4ca9fc35dc65265593423b73e9;hp=c55775242b083c1773150f9ab824246b875a2d44;hpb=498a2c9b162dd5e7281e80e364eb82f3e2b333cb;p=novacoin.git diff --git a/src/serialize.h b/src/serialize.h index c557752..4bfff0a 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -5,12 +5,14 @@ #ifndef BITCOIN_SERIALIZE_H #define BITCOIN_SERIALIZE_H +#include + #include #include #include #include #include -#include +#include #include #include @@ -19,18 +21,8 @@ #include #include -#if defined(_MSC_VER) || defined(__BORLANDC__) -typedef __int64 int64; -typedef unsigned __int64 uint64; -#else -typedef long long int64; -typedef unsigned long long uint64; -#endif -#if defined(_MSC_VER) && _MSC_VER < 1300 -#define for if (false) ; else for -#endif - -#ifdef __WXMSW__ +#ifdef WIN32 +#include // This is used to attempt to keep keying material out of swap // Note that VirtualLock does not provide this as a guarantee on Windows, // but, in practice, memory that has been VirtualLock'd almost never gets written to @@ -59,14 +51,15 @@ class CDataStream; class CAutoFile; static const unsigned int MAX_SIZE = 0x02000000; -static const int VERSION = 32500; -static const char* pszSubVer = ""; -static const bool VERSION_IS_BETA = true; - - - - +static const int PROTOCOL_VERSION = 60000; +// Used to bypass the rule against non-const reference to temporary +// where it makes sense with wrappers such as CFlatData or CTxDB +template +inline T& REF(const T& val) +{ + return const_cast(val); +} ///////////////////////////////////////////////////////////////// // @@ -87,7 +80,7 @@ enum }; #define IMPLEMENT_SERIALIZE(statements) \ - unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const \ + unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const \ { \ CSerActionGetSerializeSize ser_action; \ const bool fGetSize = true; \ @@ -101,7 +94,7 @@ enum return nSerSize; \ } \ template \ - void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const \ + void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const \ { \ CSerActionSerialize ser_action; \ const bool fGetSize = false; \ @@ -111,7 +104,7 @@ enum {statements} \ } \ template \ - void Unserialize(Stream& s, int nType=0, int nVersion=VERSION) \ + void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) \ { \ CSerActionUnserialize ser_action; \ const bool fGetSize = false; \ @@ -143,8 +136,8 @@ inline unsigned int GetSerializeSize(signed int a, int, int=0) { return size inline unsigned int GetSerializeSize(unsigned int a, int, int=0) { return sizeof(a); } inline unsigned int GetSerializeSize(signed long a, int, int=0) { return sizeof(a); } inline unsigned int GetSerializeSize(unsigned long a, int, int=0) { return sizeof(a); } -inline unsigned int GetSerializeSize(int64 a, int, int=0) { return sizeof(a); } -inline unsigned int GetSerializeSize(uint64 a, int, int=0) { return sizeof(a); } +inline unsigned int GetSerializeSize(int64_t a, int, int=0) { return sizeof(a); } +inline unsigned int GetSerializeSize(uint64_t a, int, int=0) { return sizeof(a); } inline unsigned int GetSerializeSize(float a, int, int=0) { return sizeof(a); } inline unsigned int GetSerializeSize(double a, int, int=0) { return sizeof(a); } @@ -157,8 +150,8 @@ template inline void Serialize(Stream& s, signed int a, int template inline void Serialize(Stream& s, unsigned int a, int, int=0) { WRITEDATA(s, a); } template inline void Serialize(Stream& s, signed long a, int, int=0) { WRITEDATA(s, a); } template inline void Serialize(Stream& s, unsigned long a, int, int=0) { WRITEDATA(s, a); } -template inline void Serialize(Stream& s, int64 a, int, int=0) { WRITEDATA(s, a); } -template inline void Serialize(Stream& s, uint64 a, int, int=0) { WRITEDATA(s, a); } +template inline void Serialize(Stream& s, int64_t a, int, int=0) { WRITEDATA(s, a); } +template inline void Serialize(Stream& s, uint64_t a, int, int=0) { WRITEDATA(s, a); } template inline void Serialize(Stream& s, float a, int, int=0) { WRITEDATA(s, a); } template inline void Serialize(Stream& s, double a, int, int=0) { WRITEDATA(s, a); } @@ -171,8 +164,8 @@ template inline void Unserialize(Stream& s, signed int& a, template inline void Unserialize(Stream& s, unsigned int& a, int, int=0) { READDATA(s, a); } template inline void Unserialize(Stream& s, signed long& a, int, int=0) { READDATA(s, a); } template inline void Unserialize(Stream& s, unsigned long& a, int, int=0) { READDATA(s, a); } -template inline void Unserialize(Stream& s, int64& a, int, int=0) { READDATA(s, a); } -template inline void Unserialize(Stream& s, uint64& a, int, int=0) { READDATA(s, a); } +template inline void Unserialize(Stream& s, int64_t& a, int, int=0) { READDATA(s, a); } +template inline void Unserialize(Stream& s, uint64_t& a, int, int=0) { READDATA(s, a); } template inline void Unserialize(Stream& s, float& a, int, int=0) { READDATA(s, a); } template inline void Unserialize(Stream& s, double& a, int, int=0) { READDATA(s, a); } @@ -192,30 +185,30 @@ template inline void Unserialize(Stream& s, bool& a, int, int=0 // size <= UINT_MAX -- 5 bytes (254 + 4 bytes) // size > UINT_MAX -- 9 bytes (255 + 8 bytes) // -inline unsigned int GetSizeOfCompactSize(uint64 nSize) +inline unsigned int GetSizeOfCompactSize(uint64_t nSize) { if (nSize < 253) return sizeof(unsigned char); - else if (nSize <= USHRT_MAX) return sizeof(unsigned char) + sizeof(unsigned short); - else if (nSize <= UINT_MAX) return sizeof(unsigned char) + sizeof(unsigned int); - else return sizeof(unsigned char) + sizeof(uint64); + else if (nSize <= std::numeric_limits::max()) return sizeof(unsigned char) + sizeof(unsigned short); + else if (nSize <= std::numeric_limits::max()) return sizeof(unsigned char) + sizeof(unsigned int); + else return sizeof(unsigned char) + sizeof(uint64_t); } template -void WriteCompactSize(Stream& os, uint64 nSize) +void WriteCompactSize(Stream& os, uint64_t nSize) { if (nSize < 253) { unsigned char chSize = nSize; WRITEDATA(os, chSize); } - else if (nSize <= USHRT_MAX) + else if (nSize <= std::numeric_limits::max()) { unsigned char chSize = 253; unsigned short xSize = nSize; WRITEDATA(os, chSize); WRITEDATA(os, xSize); } - else if (nSize <= UINT_MAX) + else if (nSize <= std::numeric_limits::max()) { unsigned char chSize = 254; unsigned int xSize = nSize; @@ -225,7 +218,7 @@ void WriteCompactSize(Stream& os, uint64 nSize) else { unsigned char chSize = 255; - uint64 xSize = nSize; + uint64_t xSize = nSize; WRITEDATA(os, chSize); WRITEDATA(os, xSize); } @@ -233,11 +226,11 @@ void WriteCompactSize(Stream& os, uint64 nSize) } template -uint64 ReadCompactSize(Stream& is) +uint64_t ReadCompactSize(Stream& is) { unsigned char chSize; READDATA(is, chSize); - uint64 nSizeRet = 0; + uint64_t nSizeRet = 0; if (chSize < 253) { nSizeRet = chSize; @@ -256,11 +249,11 @@ uint64 ReadCompactSize(Stream& is) } else { - uint64 xSize; + uint64_t xSize; READDATA(is, xSize); nSizeRet = xSize; } - if (nSizeRet > (uint64)MAX_SIZE) + if (nSizeRet > (uint64_t)MAX_SIZE) throw std::ios_base::failure("ReadCompactSize() : size too large"); return nSizeRet; } @@ -358,43 +351,43 @@ template void Unserialize(Stream& is, std::basic_st // vector template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const boost::true_type&); template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const boost::false_type&); -template inline unsigned int GetSerializeSize(const std::vector& v, int nType, int nVersion=VERSION); +template inline unsigned int GetSerializeSize(const std::vector& v, int nType, int nVersion=PROTOCOL_VERSION); template void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const boost::true_type&); template void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const boost::false_type&); -template inline void Serialize(Stream& os, const std::vector& v, int nType, int nVersion=VERSION); +template inline void Serialize(Stream& os, const std::vector& v, int nType, int nVersion=PROTOCOL_VERSION); template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const boost::true_type&); template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const boost::false_type&); -template inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersion=VERSION); +template inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersion=PROTOCOL_VERSION); // others derived from vector -extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion=VERSION); -template void Serialize(Stream& os, const CScript& v, int nType, int nVersion=VERSION); -template void Unserialize(Stream& is, CScript& v, int nType, int nVersion=VERSION); +extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion=PROTOCOL_VERSION); +template void Serialize(Stream& os, const CScript& v, int nType, int nVersion=PROTOCOL_VERSION); +template void Unserialize(Stream& is, CScript& v, int nType, int nVersion=PROTOCOL_VERSION); // pair -template unsigned int GetSerializeSize(const std::pair& item, int nType, int nVersion=VERSION); -template void Serialize(Stream& os, const std::pair& item, int nType, int nVersion=VERSION); -template void Unserialize(Stream& is, std::pair& item, int nType, int nVersion=VERSION); +template unsigned int GetSerializeSize(const std::pair& item, int nType, int nVersion=PROTOCOL_VERSION); +template void Serialize(Stream& os, const std::pair& item, int nType, int nVersion=PROTOCOL_VERSION); +template void Unserialize(Stream& is, std::pair& item, int nType, int nVersion=PROTOCOL_VERSION); // 3 tuple -template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion=VERSION); -template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion=VERSION); -template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion=VERSION); +template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion=PROTOCOL_VERSION); +template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion=PROTOCOL_VERSION); +template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion=PROTOCOL_VERSION); // 4 tuple -template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion=VERSION); -template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion=VERSION); -template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion=VERSION); +template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion=PROTOCOL_VERSION); +template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion=PROTOCOL_VERSION); +template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion=PROTOCOL_VERSION); // map -template unsigned int GetSerializeSize(const std::map& m, int nType, int nVersion=VERSION); -template void Serialize(Stream& os, const std::map& m, int nType, int nVersion=VERSION); -template void Unserialize(Stream& is, std::map& m, int nType, int nVersion=VERSION); +template unsigned int GetSerializeSize(const std::map& m, int nType, int nVersion=PROTOCOL_VERSION); +template void Serialize(Stream& os, const std::map& m, int nType, int nVersion=PROTOCOL_VERSION); +template void Unserialize(Stream& is, std::map& m, int nType, int nVersion=PROTOCOL_VERSION); // set -template unsigned int GetSerializeSize(const std::set& m, int nType, int nVersion=VERSION); -template void Serialize(Stream& os, const std::set& m, int nType, int nVersion=VERSION); -template void Unserialize(Stream& is, std::set& m, int nType, int nVersion=VERSION); +template unsigned int GetSerializeSize(const std::set& m, int nType, int nVersion=PROTOCOL_VERSION); +template void Serialize(Stream& os, const std::set& m, int nType, int nVersion=PROTOCOL_VERSION); +template void Unserialize(Stream& is, std::set& m, int nType, int nVersion=PROTOCOL_VERSION); @@ -407,19 +400,19 @@ template void Unserializ // Thanks to Boost serialization for this idea. // template -inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion=VERSION) +inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion=PROTOCOL_VERSION) { return a.GetSerializeSize((int)nType, nVersion); } template -inline void Serialize(Stream& os, const T& a, long nType, int nVersion=VERSION) +inline void Serialize(Stream& os, const T& a, long nType, int nVersion=PROTOCOL_VERSION) { a.Serialize(os, (int)nType, nVersion); } template -inline void Unserialize(Stream& is, T& a, long nType, int nVersion=VERSION) +inline void Unserialize(Stream& is, T& a, long nType, int nVersion=PROTOCOL_VERSION) { a.Unserialize(is, (int)nType, nVersion); } @@ -853,39 +846,39 @@ public: typedef vector_type::const_iterator const_iterator; typedef vector_type::reverse_iterator reverse_iterator; - explicit CDataStream(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) + explicit CDataStream(int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) { Init(nTypeIn, nVersionIn); } - CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend) + CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch(pbegin, pend) { Init(nTypeIn, nVersionIn); } #if !defined(_MSC_VER) || _MSC_VER >= 1300 - CDataStream(const char* pbegin, const char* pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend) + CDataStream(const char* pbegin, const char* pend, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch(pbegin, pend) { Init(nTypeIn, nVersionIn); } #endif - CDataStream(const vector_type& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end()) + CDataStream(const vector_type& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch(vchIn.begin(), vchIn.end()) { Init(nTypeIn, nVersionIn); } - CDataStream(const std::vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end()) + CDataStream(const std::vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch(vchIn.begin(), vchIn.end()) { Init(nTypeIn, nVersionIn); } - CDataStream(const std::vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0]) + CDataStream(const std::vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0]) { Init(nTypeIn, nVersionIn); } - void Init(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) + void Init(int nTypeIn=SER_NETWORK, int nVersionIn=PROTOCOL_VERSION) { nReadPos = 0; nType = nTypeIn; @@ -1099,7 +1092,7 @@ public: } template - void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const + void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const { // Special case: stream << stream concatenates like stream += stream if (!vch.empty()) @@ -1208,7 +1201,7 @@ public: typedef FILE element_type; - CAutoFile(FILE* filenew=NULL, int nTypeIn=SER_DISK, int nVersionIn=VERSION) + CAutoFile(FILE* filenew=NULL, int nTypeIn=SER_DISK, int nVersionIn=PROTOCOL_VERSION) { file = filenew; nType = nTypeIn;