diff --git a/src/serialize.h b/src/serialize.h index 877ef8640..ad38a3fa2 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1,6 +1,6 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2013 The Bitcoin developers -// Distributed under the MIT/X11 software license, see the accompanying +// Copyright (c) 2009-2014 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_SERIALIZE_H @@ -22,23 +22,28 @@ class CScript; static const unsigned int MAX_SIZE = 0x02000000; -// Used to bypass the rule against non-const reference to temporary -// where it makes sense with wrappers such as CFlatData or CTxDB +/** + * 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); } -// Used to acquire a non-const pointer "this" to generate bodies -// of const serialization operations from a template +/** + * Used to acquire a non-const pointer "this" to generate bodies + * of const serialization operations from a template + */ template inline T* NCONST_PTR(const T* val) { return const_cast(val); } -/** Get begin pointer of vector (non-const version). +/** + * Get begin pointer of vector (non-const version). * @note These functions avoid the undefined case of indexing into an empty * vector, as well as that of indexing after the end of the vector. */ @@ -82,10 +87,12 @@ enum #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action)) -/* Implement three methods for serializable objects. These are actually wrappers over +/** + * Implement three methods for serializable objects. These are actually wrappers over * "SerializationOp" template, which implements the body of each class' serialization * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be - * added as members. */ + * added as members. + */ #define ADD_SERIALIZE_METHODS \ size_t GetSerializeSize(int nType, int nVersion) const { \ CSizeComputer s(nType, nVersion); \ @@ -103,9 +110,9 @@ enum -// -// Basic types -// +/* + * Basic Types + */ #define WRITEDATA(s, obj) s.write((char*)&(obj), sizeof(obj)) #define READDATA(s, obj) s.read((char*)&(obj), sizeof(obj)) @@ -160,13 +167,13 @@ template inline void Unserialize(Stream& s, bool& a, int, int=0 -// -// Compact size -// size < 253 -- 1 byte -// size <= USHRT_MAX -- 3 bytes (253 + 2 bytes) -// size <= UINT_MAX -- 5 bytes (254 + 4 bytes) -// size > UINT_MAX -- 9 bytes (255 + 8 bytes) -// +/** + * Compact Size + * size < 253 -- 1 byte + * size <= USHRT_MAX -- 3 bytes (253 + 2 bytes) + * size <= UINT_MAX -- 5 bytes (254 + 4 bytes) + * size > UINT_MAX -- 9 bytes (255 + 8 bytes) + */ inline unsigned int GetSizeOfCompactSize(uint64_t nSize) { if (nSize < 253) return sizeof(unsigned char); @@ -246,27 +253,29 @@ uint64_t ReadCompactSize(Stream& is) return nSizeRet; } -// Variable-length integers: bytes are a MSB base-128 encoding of the number. -// The high bit in each byte signifies whether another digit follows. To make -// the encoding is one-to-one, one is subtracted from all but the last digit. -// Thus, the byte sequence a[] with length len, where all but the last byte -// has bit 128 set, encodes the number: -// -// (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1)) -// -// Properties: -// * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes) -// * Every integer has exactly one encoding -// * Encoding does not depend on size of original integer type -// * No redundancy: every (infinite) byte sequence corresponds to a list -// of encoded integers. -// -// 0: [0x00] 256: [0x81 0x00] -// 1: [0x01] 16383: [0xFE 0x7F] -// 127: [0x7F] 16384: [0xFF 0x00] -// 128: [0x80 0x00] 16511: [0x80 0xFF 0x7F] -// 255: [0x80 0x7F] 65535: [0x82 0xFD 0x7F] -// 2^32: [0x8E 0xFE 0xFE 0xFF 0x00] +/** + * Variable-length integers: bytes are a MSB base-128 encoding of the number. + * The high bit in each byte signifies whether another digit follows. To make + * sure the encoding is one-to-one, one is subtracted from all but the last digit. + * Thus, the byte sequence a[] with length len, where all but the last byte + * has bit 128 set, encodes the number: + * + * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1)) + * + * Properties: + * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes) + * * Every integer has exactly one encoding + * * Encoding does not depend on size of original integer type + * * No redundancy: every (infinite) byte sequence corresponds to a list + * of encoded integers. + * + * 0: [0x00] 256: [0x81 0x00] + * 1: [0x01] 16383: [0xFE 0x7F] + * 127: [0x7F] 16384: [0xFF 0x00] + * 128: [0x80 0x00] 16511: [0x80 0xFF 0x7F] + * 255: [0x80 0x7F] 65535: [0x82 0xFD 0x7F] + * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00] + */ template inline unsigned int GetSizeOfVarInt(I n) @@ -317,7 +326,8 @@ I ReadVarInt(Stream& is) #define VARINT(obj) REF(WrapVarInt(REF(obj))) #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj))) -/** Wrapper for serializing arrays and POD. +/** + * Wrapper for serializing arrays and POD. */ class CFlatData { @@ -415,17 +425,21 @@ public: template CVarInt WrapVarInt(I& n) { return CVarInt(n); } -// -// Forward declarations -// +/** + * Forward declarations + */ -// string +/** + * string + */ template unsigned int GetSerializeSize(const std::basic_string& str, int, int=0); template void Serialize(Stream& os, const std::basic_string& str, int, int=0); template void Unserialize(Stream& is, std::basic_string& str, int, int=0); -// vector -// vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. +/** + * vector + * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. + */ template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const unsigned char&); template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const V&); template inline unsigned int GetSerializeSize(const std::vector& v, int nType, int nVersion); @@ -436,22 +450,30 @@ template void Unserialize_impl(Stream& template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const V&); template inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersion); -// others derived from vector +/** + * others derived from vector + */ extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion); template void Serialize(Stream& os, const CScript& v, int nType, int nVersion); template void Unserialize(Stream& is, CScript& v, int nType, int nVersion); -// pair +/** + * pair + */ template unsigned int GetSerializeSize(const std::pair& item, int nType, int nVersion); template void Serialize(Stream& os, const std::pair& item, int nType, int nVersion); template void Unserialize(Stream& is, std::pair& item, int nType, int nVersion); -// map +/** + * map + */ template unsigned int GetSerializeSize(const std::map& m, int nType, int nVersion); template void Serialize(Stream& os, const std::map& m, int nType, int nVersion); template void Unserialize(Stream& is, std::map& m, int nType, int nVersion); -// set +/** + * set + */ template unsigned int GetSerializeSize(const std::set& m, int nType, int nVersion); template void Serialize(Stream& os, const std::set& m, int nType, int nVersion); template void Unserialize(Stream& is, std::set& m, int nType, int nVersion); @@ -460,12 +482,12 @@ template void Unserializ -// -// If none of the specialized versions above matched, default to calling member function. -// "int nType" is changed to "long nType" to keep from getting an ambiguous overload error. -// The compiler will only cast int to long if none of the other templates matched. -// Thanks to Boost serialization for this idea. -// +/** + * If none of the specialized versions above matched, default to calling member function. + * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error. + * The compiler will only cast int to long if none of the other templates matched. + * Thanks to Boost serialization for this idea. + */ template inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion) { @@ -488,9 +510,9 @@ inline void Unserialize(Stream& is, T& a, long nType, int nVersion) -// -// string -// +/** + * string + */ template unsigned int GetSerializeSize(const std::basic_string& str, int, int) { @@ -516,9 +538,9 @@ void Unserialize(Stream& is, std::basic_string& str, int, int) -// -// vector -// +/** + * vector + */ template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const unsigned char&) { @@ -606,9 +628,9 @@ inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersio -// -// others derived from vector -// +/** + * others derived from vector + */ inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion) { return GetSerializeSize((const std::vector&)v, nType, nVersion); @@ -628,9 +650,9 @@ void Unserialize(Stream& is, CScript& v, int nType, int nVersion) -// -// pair -// +/** + * pair + */ template unsigned int GetSerializeSize(const std::pair& item, int nType, int nVersion) { @@ -653,9 +675,9 @@ void Unserialize(Stream& is, std::pair& item, int nType, int nVersion) -// -// map -// +/** + * map + */ template unsigned int GetSerializeSize(const std::map& m, int nType, int nVersion) { @@ -689,9 +711,9 @@ void Unserialize(Stream& is, std::map& m, int nType, int nVersion -// -// set -// +/** + * set + */ template unsigned int GetSerializeSize(const std::set& m, int nType, int nVersion) { @@ -725,9 +747,9 @@ void Unserialize(Stream& is, std::set& m, int nType, int nVersion) -// -// Support for ADD_SERIALIZE_METHODS and READWRITE macro -// +/** + * Support for ADD_SERIALIZE_METHODS and READWRITE macro + */ struct CSerActionSerialize { bool ForRead() const { return false; }