Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2020 The Bitcoin Core developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #ifndef BITCOIN_SERIALIZE_H
# 7 : : #define BITCOIN_SERIALIZE_H
# 8 : :
# 9 : : #include <compat/endian.h>
# 10 : :
# 11 : : #include <algorithm>
# 12 : : #include <cstdint>
# 13 : : #include <cstring>
# 14 : : #include <ios>
# 15 : : #include <limits>
# 16 : : #include <map>
# 17 : : #include <memory>
# 18 : : #include <set>
# 19 : : #include <string>
# 20 : : #include <string.h>
# 21 : : #include <utility>
# 22 : : #include <vector>
# 23 : :
# 24 : : #include <prevector.h>
# 25 : : #include <span.h>
# 26 : :
# 27 : : /**
# 28 : : * The maximum size of a serialized object in bytes or number of elements
# 29 : : * (for eg vectors) when the size is encoded as CompactSize.
# 30 : : */
# 31 : : static constexpr uint64_t MAX_SIZE = 0x02000000;
# 32 : :
# 33 : : /** Maximum amount of memory (in bytes) to allocate at once when deserializing vectors. */
# 34 : : static const unsigned int MAX_VECTOR_ALLOCATE = 5000000;
# 35 : :
# 36 : : /**
# 37 : : * Dummy data type to identify deserializing constructors.
# 38 : : *
# 39 : : * By convention, a constructor of a type T with signature
# 40 : : *
# 41 : : * template <typename Stream> T::T(deserialize_type, Stream& s)
# 42 : : *
# 43 : : * is a deserializing constructor, which builds the type by
# 44 : : * deserializing it from s. If T contains const fields, this
# 45 : : * is likely the only way to do so.
# 46 : : */
# 47 : : struct deserialize_type {};
# 48 : : constexpr deserialize_type deserialize {};
# 49 : :
# 50 : : //! Safely convert odd char pointer types to standard ones.
# 51 : 0 : inline char* CharCast(char* c) { return c; }
# 52 : 551870 : inline char* CharCast(unsigned char* c) { return (char*)c; }
# 53 : 0 : inline const char* CharCast(const char* c) { return c; }
# 54 : 723024 : inline const char* CharCast(const unsigned char* c) { return (const char*)c; }
# 55 : :
# 56 : : /*
# 57 : : * Lowest-level serialization and conversion.
# 58 : : * @note Sizes of these types are verified in the tests
# 59 : : */
# 60 : : template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
# 61 : 671776351 : {
# 62 : 671776351 : s.write((char*)&obj, 1);
# 63 : 671776351 : }
# 64 : : template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
# 65 : 2969941 : {
# 66 : 2969941 : obj = htole16(obj);
# 67 : 2969941 : s.write((char*)&obj, 2);
# 68 : 2969941 : }
# 69 : : template<typename Stream> inline void ser_writedata16be(Stream &s, uint16_t obj)
# 70 : : {
# 71 : : obj = htobe16(obj);
# 72 : : s.write((char*)&obj, 2);
# 73 : : }
# 74 : : template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
# 75 : 398160348 : {
# 76 : 398160348 : obj = htole32(obj);
# 77 : 398160348 : s.write((char*)&obj, 4);
# 78 : 398160348 : }
# 79 : : template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
# 80 : 13616 : {
# 81 : 13616 : obj = htobe32(obj);
# 82 : 13616 : s.write((char*)&obj, 4);
# 83 : 13616 : }
# 84 : : template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
# 85 : 63610623 : {
# 86 : 63610623 : obj = htole64(obj);
# 87 : 63610623 : s.write((char*)&obj, 8);
# 88 : 63610623 : }
# 89 : : template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
# 90 : 9212474 : {
# 91 : 9212474 : uint8_t obj;
# 92 : 9212474 : s.read((char*)&obj, 1);
# 93 : 9212474 : return obj;
# 94 : 9212474 : }
# 95 : : template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
# 96 : 641638 : {
# 97 : 641638 : uint16_t obj;
# 98 : 641638 : s.read((char*)&obj, 2);
# 99 : 641638 : return le16toh(obj);
# 100 : 641638 : }
# 101 : : template<typename Stream> inline uint16_t ser_readdata16be(Stream &s)
# 102 : : {
# 103 : : uint16_t obj;
# 104 : : s.read((char*)&obj, 2);
# 105 : : return be16toh(obj);
# 106 : : }
# 107 : : template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
# 108 : 2932690 : {
# 109 : 2932690 : uint32_t obj;
# 110 : 2932690 : s.read((char*)&obj, 4);
# 111 : 2932690 : return le32toh(obj);
# 112 : 2932690 : }
# 113 : : template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
# 114 : 2926 : {
# 115 : 2926 : uint32_t obj;
# 116 : 2926 : s.read((char*)&obj, 4);
# 117 : 2926 : return be32toh(obj);
# 118 : 2926 : }
# 119 : : template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
# 120 : 9452017 : {
# 121 : 9452017 : uint64_t obj;
# 122 : 9452017 : s.read((char*)&obj, 8);
# 123 : 9452017 : return le64toh(obj);
# 124 : 9452017 : }
# 125 : :
# 126 : :
# 127 : : /////////////////////////////////////////////////////////////////
# 128 : : //
# 129 : : // Templates for serializing to anything that looks like a stream,
# 130 : : // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
# 131 : : //
# 132 : :
# 133 : : class CSizeComputer;
# 134 : :
# 135 : : enum
# 136 : : {
# 137 : : // primary actions
# 138 : : SER_NETWORK = (1 << 0),
# 139 : : SER_DISK = (1 << 1),
# 140 : : SER_GETHASH = (1 << 2),
# 141 : : };
# 142 : :
# 143 : : //! Convert the reference base type to X, without changing constness or reference type.
# 144 : 1883781 : template<typename X> X& ReadWriteAsHelper(X& x) { return x; }
# 145 : 52390339 : template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
# 146 : :
# 147 : 119109771 : #define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
# 148 : 54274260 : #define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj)))
# 149 [ + - ]: 189606 : #define SER_READ(obj, code) ::SerRead(s, ser_action, obj, [&](Stream& s, typename std::remove_const<Type>::type& obj) { code; })
# 150 : 111250 : #define SER_WRITE(obj, code) ::SerWrite(s, ser_action, obj, [&](Stream& s, const Type& obj) { code; })
# 151 : :
# 152 : : /**
# 153 : : * Implement the Ser and Unser methods needed for implementing a formatter (see Using below).
# 154 : : *
# 155 : : * Both Ser and Unser are delegated to a single static method SerializationOps, which is polymorphic
# 156 : : * in the serialized/deserialized type (allowing it to be const when serializing, and non-const when
# 157 : : * deserializing).
# 158 : : *
# 159 : : * Example use:
# 160 : : * struct FooFormatter {
# 161 : : * FORMATTER_METHODS(Class, obj) { READWRITE(obj.val1, VARINT(obj.val2)); }
# 162 : : * }
# 163 : : * would define a class FooFormatter that defines a serialization of Class objects consisting
# 164 : : * of serializing its val1 member using the default serialization, and its val2 member using
# 165 : : * VARINT serialization. That FooFormatter can then be used in statements like
# 166 : : * READWRITE(Using<FooFormatter>(obj.bla)).
# 167 : : */
# 168 : : #define FORMATTER_METHODS(cls, obj) \
# 169 : : template<typename Stream> \
# 170 : 165617216 : static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, CSerActionSerialize()); } \
# 171 : : template<typename Stream> \
# 172 : 4844823 : static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, CSerActionUnserialize()); } \
# 173 : : template<typename Stream, typename Type, typename Operation> \
# 174 : : static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \
# 175 : :
# 176 : : /**
# 177 : : * Implement the Serialize and Unserialize methods by delegating to a single templated
# 178 : : * static method that takes the to-be-(de)serialized object as a parameter. This approach
# 179 : : * has the advantage that the constness of the object becomes a template parameter, and
# 180 : : * thus allows a single implementation that sees the object as const for serializing
# 181 : : * and non-const for deserializing, without casts.
# 182 : : */
# 183 : : #define SERIALIZE_METHODS(cls, obj) \
# 184 : : template<typename Stream> \
# 185 : : void Serialize(Stream& s) const \
# 186 : 165020225 : { \
# 187 : 165020225 : static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
# 188 : 165020225 : Ser(s, *this); \
# 189 : 165020225 : } \
# 190 : : template<typename Stream> \
# 191 : : void Unserialize(Stream& s) \
# 192 : 4664849 : { \
# 193 : 4664849 : static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
# 194 : 4664849 : Unser(s, *this); \
# 195 : 4664849 : } \
# 196 : : FORMATTER_METHODS(cls, obj)
# 197 : :
# 198 : : #ifndef CHAR_EQUALS_INT8
# 199 : 295093591 : template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
# 200 : : #endif
# 201 : 4 : template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
# 202 : 1271600 : template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
# 203 : 4 : template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
# 204 : 4 : template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
# 205 : 305633592 : template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
# 206 : 92524112 : template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
# 207 : 42634663 : template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
# 208 : 20976136 : template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
# 209 : 234582 : template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); }
# 210 : 295185 : template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); }
# 211 : 427839 : template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
# 212 : 224823 : template<typename Stream> inline void Serialize(Stream& s, const Span<unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
# 213 : :
# 214 : : #ifndef CHAR_EQUALS_INT8
# 215 : 107646 : template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
# 216 : : #endif
# 217 : 2 : template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
# 218 : 204982 : template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
# 219 : : template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
# 220 : 2 : template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
# 221 : 825272 : template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); }
# 222 : 2104102 : template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
# 223 : 1396256 : template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
# 224 : 8055756 : template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
# 225 : 223988 : template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); }
# 226 : 144424 : template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); }
# 227 : 182623 : template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); }
# 228 : :
# 229 : 383575 : template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
# 230 : 75306 : template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
# 231 : :
# 232 : :
# 233 : :
# 234 : :
# 235 : :
# 236 : :
# 237 : : /**
# 238 : : * Compact Size
# 239 : : * size < 253 -- 1 byte
# 240 : : * size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
# 241 : : * size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
# 242 : : * size > UINT_MAX -- 9 bytes (255 + 8 bytes)
# 243 : : */
# 244 : : inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
# 245 : 20213847 : {
# 246 [ + + ]: 20213847 : if (nSize < 253) return sizeof(unsigned char);
# 247 [ + + ]: 500657 : else if (nSize <= std::numeric_limits<uint16_t>::max()) return sizeof(unsigned char) + sizeof(uint16_t);
# 248 [ + - ]: 8826 : else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
# 249 : 0 : else return sizeof(unsigned char) + sizeof(uint64_t);
# 250 : 20213847 : }
# 251 : :
# 252 : : inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
# 253 : :
# 254 : : template<typename Stream>
# 255 : : void WriteCompactSize(Stream& os, uint64_t nSize)
# 256 : 348696917 : {
# 257 [ + + ][ + + ]: 348696917 : if (nSize < 253)
# [ + + ][ + + ]
# [ + - ][ + - ]
# [ + + ][ + - ]
# 258 : 345722948 : {
# 259 : 345722948 : ser_writedata8(os, nSize);
# 260 : 345722948 : }
# 261 [ + + ][ + + ]: 2973969 : else if (nSize <= std::numeric_limits<uint16_t>::max())
# [ + - ][ # # ]
# [ + + ][ # # ]
# [ + - ][ # # ]
# 262 : 2969933 : {
# 263 : 2969933 : ser_writedata8(os, 253);
# 264 : 2969933 : ser_writedata16(os, nSize);
# 265 : 2969933 : }
# 266 [ # # ][ # # ]: 4036 : else if (nSize <= std::numeric_limits<unsigned int>::max())
# [ + - ][ + - ]
# [ # # ][ # # ]
# [ + + ][ # # ]
# 267 : 4404 : {
# 268 : 4404 : ser_writedata8(os, 254);
# 269 : 4404 : ser_writedata32(os, nSize);
# 270 : 4404 : }
# 271 :>1844*10^16 : else
# 272 :>1844*10^16 : {
# 273 :>1844*10^16 : ser_writedata8(os, 255);
# 274 :>1844*10^16 : ser_writedata64(os, nSize);
# 275 :>1844*10^16 : }
# 276 : 348696917 : return;
# 277 : 348696917 : }
# 278 : :
# 279 : : /**
# 280 : : * Decode a CompactSize-encoded variable-length integer.
# 281 : : *
# 282 : : * As these are primarily used to encode the size of vector-like serializations, by default a range
# 283 : : * check is performed. When used as a generic number encoding, range_check should be set to false.
# 284 : : */
# 285 : : template<typename Stream>
# 286 : : uint64_t ReadCompactSize(Stream& is, bool range_check = true)
# 287 : 6199183 : {
# 288 : 6199183 : uint8_t chSize = ser_readdata8(is);
# 289 : 6199183 : uint64_t nSizeRet = 0;
# 290 [ + + ][ + + ]: 6199183 : if (chSize < 253)
# [ + + ][ + - ]
# [ + + ][ + + ]
# [ + - ][ + + ]
# [ + + ]
# 291 : 5554204 : {
# 292 : 5554204 : nSizeRet = chSize;
# 293 : 5554204 : }
# 294 [ + + ][ + + ]: 644979 : else if (chSize == 253)
# [ + - ][ + + ]
# [ # # ][ + + ]
# [ - + ][ # # ]
# [ + + ]
# 295 : 641636 : {
# 296 : 641636 : nSizeRet = ser_readdata16(is);
# 297 [ + + ][ - + ]: 641636 : if (nSizeRet < 253)
# [ - + ][ # # ]
# [ # # ][ - + ]
# [ # # ][ - + ]
# [ - + ]
# 298 : 4 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
# 299 : 3343 : }
# 300 [ + + ][ - + ]: 3343 : else if (chSize == 254)
# [ # # ][ - + ]
# [ + + ][ # # ]
# [ - + ][ - + ]
# [ # # ]
# 301 : 3316 : {
# 302 : 3316 : nSizeRet = ser_readdata32(is);
# 303 [ # # ][ # # ]: 3316 : if (nSizeRet < 0x10000u)
# [ # # ][ # # ]
# [ - + ][ # # ]
# [ # # ][ # # ]
# [ + + ]
# 304 : 4 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
# 305 : 27 : }
# 306 : 27 : else
# 307 : 27 : {
# 308 : 27 : nSizeRet = ser_readdata64(is);
# 309 [ # # ][ - + ]: 27 : if (nSizeRet < 0x100000000ULL)
# [ - + ][ # # ]
# [ - + ][ - + ]
# [ # # ][ - + ]
# [ + + ]
# 310 : 5 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
# 311 : 6199170 : }
# 312 [ + + ][ + + ]: 6199170 : if (range_check && nSizeRet > MAX_SIZE) {
# [ + - ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# 313 : 0 : throw std::ios_base::failure("ReadCompactSize(): size too large");
# 314 : 0 : }
# 315 : 6199170 : return nSizeRet;
# 316 : 6199170 : }
# 317 : :
# 318 : : /**
# 319 : : * Variable-length integers: bytes are a MSB base-128 encoding of the number.
# 320 : : * The high bit in each byte signifies whether another digit follows. To make
# 321 : : * sure the encoding is one-to-one, one is subtracted from all but the last digit.
# 322 : : * Thus, the byte sequence a[] with length len, where all but the last byte
# 323 : : * has bit 128 set, encodes the number:
# 324 : : *
# 325 : : * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
# 326 : : *
# 327 : : * Properties:
# 328 : : * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
# 329 : : * * Every integer has exactly one encoding
# 330 : : * * Encoding does not depend on size of original integer type
# 331 : : * * No redundancy: every (infinite) byte sequence corresponds to a list
# 332 : : * of encoded integers.
# 333 : : *
# 334 : : * 0: [0x00] 256: [0x81 0x00]
# 335 : : * 1: [0x01] 16383: [0xFE 0x7F]
# 336 : : * 127: [0x7F] 16384: [0xFF 0x00]
# 337 : : * 128: [0x80 0x00] 16511: [0xFF 0x7F]
# 338 : : * 255: [0x80 0x7F] 65535: [0x82 0xFE 0x7F]
# 339 : : * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
# 340 : : */
# 341 : :
# 342 : : /**
# 343 : : * Mode for encoding VarInts.
# 344 : : *
# 345 : : * Currently there is no support for signed encodings. The default mode will not
# 346 : : * compile with signed values, and the legacy "nonnegative signed" mode will
# 347 : : * accept signed values, but improperly encode and decode them if they are
# 348 : : * negative. In the future, the DEFAULT mode could be extended to support
# 349 : : * negative numbers in a backwards compatible way, and additional modes could be
# 350 : : * added to support different varint formats (e.g. zigzag encoding).
# 351 : : */
# 352 : : enum class VarIntMode { DEFAULT, NONNEGATIVE_SIGNED };
# 353 : :
# 354 : : template <VarIntMode Mode, typename I>
# 355 : : struct CheckVarIntMode {
# 356 : : constexpr CheckVarIntMode()
# 357 : 11839125 : {
# 358 : 11839125 : static_assert(Mode != VarIntMode::DEFAULT || std::is_unsigned<I>::value, "Unsigned type required with mode DEFAULT.");
# 359 : 11839125 : static_assert(Mode != VarIntMode::NONNEGATIVE_SIGNED || std::is_signed<I>::value, "Signed type required with mode NONNEGATIVE_SIGNED.");
# 360 : 11839125 : }
# 361 : : };
# 362 : :
# 363 : : template<VarIntMode Mode, typename I>
# 364 : : inline unsigned int GetSizeOfVarInt(I n)
# 365 : : {
# 366 : : CheckVarIntMode<Mode, I>();
# 367 : : int nRet = 0;
# 368 : : while(true) {
# 369 : : nRet++;
# 370 : : if (n <= 0x7F)
# 371 : : break;
# 372 : : n = (n >> 7) - 1;
# 373 : : }
# 374 : : return nRet;
# 375 : : }
# 376 : :
# 377 : : template<typename I>
# 378 : : inline void WriteVarInt(CSizeComputer& os, I n);
# 379 : :
# 380 : : template<typename Stream, VarIntMode Mode, typename I>
# 381 : : void WriteVarInt(Stream& os, I n)
# 382 : 10615068 : {
# 383 : 10615068 : CheckVarIntMode<Mode, I>();
# 384 : 10615068 : unsigned char tmp[(sizeof(n)*8+6)/7];
# 385 : 10615068 : int len=0;
# 386 : 26316867 : while(true) {
# 387 [ + + ][ + + ]: 26316867 : tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
# [ - + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ]
# 388 [ + + ][ + + ]: 26316867 : if (n <= 0x7F)
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + - ][ + + ]
# [ + + ]
# 389 : 10615068 : break;
# 390 : 15701799 : n = (n >> 7) - 1;
# 391 : 15701799 : len++;
# 392 : 15701799 : }
# 393 : 26316867 : do {
# 394 : 26316867 : ser_writedata8(os, tmp[len]);
# 395 [ + + ][ + + ]: 26316867 : } while(len--);
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ - + ][ + + ]
# [ + + ]
# 396 : 10615068 : }
# 397 : :
# 398 : : template<typename Stream, VarIntMode Mode, typename I>
# 399 : : I ReadVarInt(Stream& is)
# 400 : 1224057 : {
# 401 : 1224057 : CheckVarIntMode<Mode, I>();
# 402 : 1224057 : I n = 0;
# 403 : 2622428 : while(true) {
# 404 : 2622428 : unsigned char chData = ser_readdata8(is);
# 405 [ - + ][ - + ]: 2622428 : if (n > (std::numeric_limits<I>::max() >> 7)) {
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# 406 : 0 : throw std::ios_base::failure("ReadVarInt(): size too large");
# 407 : 0 : }
# 408 : 2622428 : n = (n << 7) | (chData & 0x7F);
# 409 [ + + ][ + + ]: 2622428 : if (chData & 0x80) {
# [ + + ][ - + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# 410 [ - + ][ - + ]: 1398371 : if (n == std::numeric_limits<I>::max()) {
# [ - + ][ - + ]
# [ # # ][ - + ]
# [ - + ][ - + ]
# 411 : 0 : throw std::ios_base::failure("ReadVarInt(): size too large");
# 412 : 0 : }
# 413 : 1398371 : n++;
# 414 : 1398371 : } else {
# 415 : 1224057 : return n;
# 416 : 1224057 : }
# 417 : 2622428 : }
# 418 : 1224057 : }
# 419 : :
# 420 : : /** Simple wrapper class to serialize objects using a formatter; used by Using(). */
# 421 : : template<typename Formatter, typename T>
# 422 : : class Wrapper
# 423 : : {
# 424 : : static_assert(std::is_lvalue_reference<T>::value, "Wrapper needs an lvalue reference type T");
# 425 : : protected:
# 426 : : T m_object;
# 427 : : public:
# 428 : 28136353 : explicit Wrapper(T obj) : m_object(obj) {}
# 429 : 25056905 : template<typename Stream> void Serialize(Stream &s) const { Formatter().Ser(s, m_object); }
# 430 : 3079485 : template<typename Stream> void Unserialize(Stream &s) { Formatter().Unser(s, m_object); }
# 431 : : };
# 432 : :
# 433 : : /** Cause serialization/deserialization of an object to be done using a specified formatter class.
# 434 : : *
# 435 : : * To use this, you need a class Formatter that has public functions Ser(stream, const object&) for
# 436 : : * serialization, and Unser(stream, object&) for deserialization. Serialization routines (inside
# 437 : : * READWRITE, or directly with << and >> operators), can then use Using<Formatter>(object).
# 438 : : *
# 439 : : * This works by constructing a Wrapper<Formatter, T>-wrapped version of object, where T is
# 440 : : * const during serialization, and non-const during deserialization, which maintains const
# 441 : : * correctness.
# 442 : : */
# 443 : : template<typename Formatter, typename T>
# 444 : 28136390 : static inline Wrapper<Formatter, T&> Using(T&& t) { return Wrapper<Formatter, T&>(t); }
# 445 : :
# 446 : 601279 : #define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj)
# 447 : 2142197 : #define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj)
# 448 : 2671 : #define COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj)
# 449 : 962 : #define LIMITED_STRING(obj,n) Using<LimitedStringFormatter<n>>(obj)
# 450 : :
# 451 : : /** Serialization wrapper class for integers in VarInt format. */
# 452 : : template<VarIntMode Mode>
# 453 : : struct VarIntFormatter
# 454 : : {
# 455 : : template<typename Stream, typename I> void Ser(Stream &s, I v)
# 456 : 10615068 : {
# 457 : 10615068 : WriteVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s, v);
# 458 : 10615068 : }
# 459 : :
# 460 : : template<typename Stream, typename I> void Unser(Stream& s, I& v)
# 461 : 1224057 : {
# 462 : 1224057 : v = ReadVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s);
# 463 : 1224057 : }
# 464 : : };
# 465 : :
# 466 : : /** Serialization wrapper class for custom integers and enums.
# 467 : : *
# 468 : : * It permits specifying the serialized size (1 to 8 bytes) and endianness.
# 469 : : *
# 470 : : * Use the big endian mode for values that are stored in memory in native
# 471 : : * byte order, but serialized in big endian notation. This is only intended
# 472 : : * to implement serializers that are compatible with existing formats, and
# 473 : : * its use is not recommended for new data structures.
# 474 : : */
# 475 : : template<int Bytes, bool BigEndian = false>
# 476 : : struct CustomUintFormatter
# 477 : : {
# 478 : : static_assert(Bytes > 0 && Bytes <= 8, "CustomUintFormatter Bytes out of range");
# 479 : : static constexpr uint64_t MAX = 0xffffffffffffffff >> (8 * (8 - Bytes));
# 480 : :
# 481 : : template <typename Stream, typename I> void Ser(Stream& s, I v)
# 482 : 120574 : {
# 483 [ - + ][ # # ]: 120574 : if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range");
# [ - + ][ - + ]
# [ # # ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ # # ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ # # ][ - + ]
# [ - + ][ - + ]
# [ # # ][ - + ]
# [ - + ][ # # ]
# 484 : 120574 : if (BigEndian) {
# 485 : 72795 : uint64_t raw = htobe64(v);
# 486 : 72795 : s.write(((const char*)&raw) + 8 - Bytes, Bytes);
# 487 : 72795 : } else {
# 488 : 47779 : uint64_t raw = htole64(v);
# 489 : 47779 : s.write((const char*)&raw, Bytes);
# 490 : 47779 : }
# 491 : 120574 : }
# 492 : :
# 493 : : template <typename Stream, typename I> void Unser(Stream& s, I& v)
# 494 : 32293 : {
# 495 : 32293 : using U = typename std::conditional<std::is_enum<I>::value, std::underlying_type<I>, std::common_type<I>>::type::type;
# 496 : 32293 : static_assert(std::numeric_limits<U>::max() >= MAX && std::numeric_limits<U>::min() <= 0, "Assigned type too small");
# 497 : 32293 : uint64_t raw = 0;
# 498 : 32293 : if (BigEndian) {
# 499 : 4776 : s.read(((char*)&raw) + 8 - Bytes, Bytes);
# 500 : 4776 : v = static_cast<I>(be64toh(raw));
# 501 : 27517 : } else {
# 502 : 27517 : s.read((char*)&raw, Bytes);
# 503 : 27517 : v = static_cast<I>(le64toh(raw));
# 504 : 27517 : }
# 505 : 32293 : }
# 506 : : };
# 507 : :
# 508 : : template<int Bytes> using BigEndianFormatter = CustomUintFormatter<Bytes, true>;
# 509 : :
# 510 : : /** Formatter for integers in CompactSize format. */
# 511 : : template<bool RangeCheck>
# 512 : : struct CompactSizeFormatter
# 513 : : {
# 514 : : template<typename Stream, typename I>
# 515 : : void Unser(Stream& s, I& v)
# 516 : 25266 : {
# 517 : 25266 : uint64_t n = ReadCompactSize<Stream>(s, RangeCheck);
# 518 [ - + ][ - + ]: 25266 : if (n < std::numeric_limits<I>::min() || n > std::numeric_limits<I>::max()) {
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ # # ][ # # ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ - + ][ - + ]
# [ # # ][ - + ]
# [ - + ][ # # ]
# [ - + ][ - + ]
# 519 : 0 : throw std::ios_base::failure("CompactSize exceeds limit of type");
# 520 : 0 : }
# 521 : 25266 : v = n;
# 522 : 25266 : }
# 523 : :
# 524 : : template<typename Stream, typename I>
# 525 : : void Ser(Stream& s, I v)
# 526 : 102466 : {
# 527 : 102466 : static_assert(std::is_unsigned<I>::value, "CompactSize only supported for unsigned integers");
# 528 : 102466 : static_assert(std::numeric_limits<I>::max() <= std::numeric_limits<uint64_t>::max(), "CompactSize only supports 64-bit integers and below");
# 529 : :
# 530 : 102466 : WriteCompactSize<Stream>(s, v);
# 531 : 102466 : }
# 532 : : };
# 533 : :
# 534 : : template<size_t Limit>
# 535 : : struct LimitedStringFormatter
# 536 : : {
# 537 : : template<typename Stream>
# 538 : : void Unser(Stream& s, std::string& v)
# 539 : 960 : {
# 540 : 960 : size_t size = ReadCompactSize(s);
# 541 [ - + ][ - + ]: 960 : if (size > Limit) {
# 542 : 0 : throw std::ios_base::failure("String length limit exceeded");
# 543 : 0 : }
# 544 : 960 : v.resize(size);
# 545 [ + - ][ + + ]: 960 : if (size != 0) s.read((char*)v.data(), size);
# 546 : 960 : }
# 547 : :
# 548 : : template<typename Stream>
# 549 : : void Ser(Stream& s, const std::string& v)
# 550 : 6 : {
# 551 : 6 : s << v;
# 552 : 6 : }
# 553 : : };
# 554 : :
# 555 : : /** Formatter to serialize/deserialize vector elements using another formatter
# 556 : : *
# 557 : : * Example:
# 558 : : * struct X {
# 559 : : * std::vector<uint64_t> v;
# 560 : : * SERIALIZE_METHODS(X, obj) { READWRITE(Using<VectorFormatter<VarInt>>(obj.v)); }
# 561 : : * };
# 562 : : * will define a struct that contains a vector of uint64_t, which is serialized
# 563 : : * as a vector of VarInt-encoded integers.
# 564 : : *
# 565 : : * V is not required to be an std::vector type. It works for any class that
# 566 : : * exposes a value_type, size, reserve, emplace_back, back, and const iterators.
# 567 : : */
# 568 : : template<class Formatter>
# 569 : : struct VectorFormatter
# 570 : : {
# 571 : : template<typename Stream, typename V>
# 572 : : void Ser(Stream& s, const V& v)
# 573 : 12523727 : {
# 574 : 12523727 : Formatter formatter;
# 575 : 12523727 : WriteCompactSize(s, v.size());
# 576 [ + + ][ + + ]: 59766900 : for (const typename V::value_type& elem : v) {
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ - + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ # # ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ - + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ - + ][ + + ]
# 577 : 59766900 : formatter.Ser(s, elem);
# 578 : 59766900 : }
# 579 : 12523727 : }
# 580 : :
# 581 : : template<typename Stream, typename V>
# 582 : : void Unser(Stream& s, V& v)
# 583 : 1300150 : {
# 584 : 1300150 : Formatter formatter;
# 585 : 1300150 : v.clear();
# 586 : 1300150 : size_t size = ReadCompactSize(s);
# 587 : 1300150 : size_t allocated = 0;
# 588 [ + + ][ + + ]: 2326653 : while (allocated < size) {
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ - + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ - + ][ + + ]
# [ # # ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ # # ]
# [ - + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ]
# 589 : : // For DoS prevention, do not blindly allocate as much as the stream claims to contain.
# 590 : : // Instead, allocate in 5MiB batches, so that an attacker actually needs to provide
# 591 : : // X MiB of data to make us allocate X+5 Mib.
# 592 : 1026503 : static_assert(sizeof(typename V::value_type) <= MAX_VECTOR_ALLOCATE, "Vector element size too large");
# 593 : 1026503 : allocated = std::min(size, allocated + MAX_VECTOR_ALLOCATE / sizeof(typename V::value_type));
# 594 : 1026503 : v.reserve(allocated);
# 595 [ + + ][ + + ]: 14261692 : while (v.size() < allocated) {
# [ + + ][ + + ]
# [ + + ][ # # ]
# [ + + ][ + + ]
# [ # # ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ # # ][ # # ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ # # ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ + + ]
# 596 : 13235189 : v.emplace_back();
# 597 : 13235189 : formatter.Unser(s, v.back());
# 598 : 13235189 : }
# 599 : 1026503 : }
# 600 : 1300150 : };
# 601 : : };
# 602 : :
# 603 : : /**
# 604 : : * Forward declarations
# 605 : : */
# 606 : :
# 607 : : /**
# 608 : : * string
# 609 : : */
# 610 : : template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
# 611 : : template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
# 612 : :
# 613 : : /**
# 614 : : * prevector
# 615 : : * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
# 616 : : */
# 617 : : template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
# 618 : : template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
# 619 : : template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
# 620 : : template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
# 621 : : template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
# 622 : : template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
# 623 : :
# 624 : : /**
# 625 : : * vector
# 626 : : * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
# 627 : : */
# 628 : : template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
# 629 : : template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&);
# 630 : : template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
# 631 : : template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
# 632 : : template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
# 633 : : template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
# 634 : : template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
# 635 : :
# 636 : : /**
# 637 : : * pair
# 638 : : */
# 639 : : template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
# 640 : : template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
# 641 : :
# 642 : : /**
# 643 : : * map
# 644 : : */
# 645 : : template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
# 646 : : template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
# 647 : :
# 648 : : /**
# 649 : : * set
# 650 : : */
# 651 : : template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
# 652 : : template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
# 653 : :
# 654 : : /**
# 655 : : * shared_ptr
# 656 : : */
# 657 : : template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
# 658 : : template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
# 659 : :
# 660 : : /**
# 661 : : * unique_ptr
# 662 : : */
# 663 : : template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
# 664 : : template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
# 665 : :
# 666 : :
# 667 : :
# 668 : : /**
# 669 : : * If none of the specialized versions above matched, default to calling member function.
# 670 : : */
# 671 : : template<typename Stream, typename T>
# 672 : : inline void Serialize(Stream& os, const T& a)
# 673 : 552416770 : {
# 674 : 552416770 : a.Serialize(os);
# 675 : 552416770 : }
# 676 : :
# 677 : : template<typename Stream, typename T>
# 678 : : inline void Unserialize(Stream& is, T&& a)
# 679 : 9317901 : {
# 680 : 9317901 : a.Unserialize(is);
# 681 : 9317901 : }
# 682 : :
# 683 : : /** Default formatter. Serializes objects as themselves.
# 684 : : *
# 685 : : * The vector/prevector serialization code passes this to VectorFormatter
# 686 : : * to enable reusing that logic. It shouldn't be needed elsewhere.
# 687 : : */
# 688 : : struct DefaultFormatter
# 689 : : {
# 690 : : template<typename Stream, typename T>
# 691 : 39801466 : static void Ser(Stream& s, const T& t) { Serialize(s, t); }
# 692 : :
# 693 : : template<typename Stream, typename T>
# 694 : 5145111 : static void Unser(Stream& s, T& t) { Unserialize(s, t); }
# 695 : : };
# 696 : :
# 697 : :
# 698 : :
# 699 : :
# 700 : :
# 701 : : /**
# 702 : : * string
# 703 : : */
# 704 : : template<typename Stream, typename C>
# 705 : : void Serialize(Stream& os, const std::basic_string<C>& str)
# 706 : 1645319 : {
# 707 : 1645319 : WriteCompactSize(os, str.size());
# 708 [ + - ][ + + ]: 1645319 : if (!str.empty())
# [ + - ][ + + ]
# [ + + ]
# 709 : 1485344 : os.write((char*)str.data(), str.size() * sizeof(C));
# 710 : 1645319 : }
# 711 : :
# 712 : : template<typename Stream, typename C>
# 713 : : void Unserialize(Stream& is, std::basic_string<C>& str)
# 714 : 153272 : {
# 715 : 153272 : unsigned int nSize = ReadCompactSize(is);
# 716 : 153272 : str.resize(nSize);
# 717 [ + + ][ + - ]: 153272 : if (nSize != 0)
# 718 : 138682 : is.read((char*)str.data(), nSize * sizeof(C));
# 719 : 153272 : }
# 720 : :
# 721 : :
# 722 : :
# 723 : : /**
# 724 : : * prevector
# 725 : : */
# 726 : : template<typename Stream, unsigned int N, typename T>
# 727 : : void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
# 728 : 51667311 : {
# 729 : 51667311 : WriteCompactSize(os, v.size());
# 730 [ + + ][ + - ]: 51667311 : if (!v.empty())
# [ + + ][ + - ]
# [ + - ][ # # ]
# [ # # ][ + + ]
# [ + - ][ + + ]
# [ + + ][ + - ]
# [ + + ]
# 731 : 44737706 : os.write((char*)v.data(), v.size() * sizeof(T));
# 732 : 51667311 : }
# 733 : :
# 734 : : template<typename Stream, unsigned int N, typename T, typename V>
# 735 : : void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
# 736 : 532352 : {
# 737 : 532352 : Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
# 738 : 532352 : }
# 739 : :
# 740 : : template<typename Stream, unsigned int N, typename T>
# 741 : : inline void Serialize(Stream& os, const prevector<N, T>& v)
# 742 : 52199456 : {
# 743 : 52199456 : Serialize_impl(os, v, T());
# 744 : 52199456 : }
# 745 : :
# 746 : :
# 747 : : template<typename Stream, unsigned int N, typename T>
# 748 : : void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
# 749 : 1741530 : {
# 750 : : // Limit size per read so bogus size value won't cause out of memory
# 751 : 1741530 : v.clear();
# 752 : 1741530 : unsigned int nSize = ReadCompactSize(is);
# 753 : 1741530 : unsigned int i = 0;
# 754 [ + + ][ + + ]: 3323073 : while (i < nSize)
# [ + + ][ + + ]
# [ + + ][ + + ]
# 755 : 1581543 : {
# 756 : 1581543 : unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
# 757 : 1581543 : v.resize_uninitialized(i + blk);
# 758 : 1581543 : is.read((char*)&v[i], blk * sizeof(T));
# 759 : 1581543 : i += blk;
# 760 : 1581543 : }
# 761 : 1741530 : }
# 762 : :
# 763 : : template<typename Stream, unsigned int N, typename T, typename V>
# 764 : : void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
# 765 : : {
# 766 : : Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
# 767 : : }
# 768 : :
# 769 : : template<typename Stream, unsigned int N, typename T>
# 770 : : inline void Unserialize(Stream& is, prevector<N, T>& v)
# 771 : 1741530 : {
# 772 : 1741530 : Unserialize_impl(is, v, T());
# 773 : 1741530 : }
# 774 : :
# 775 : :
# 776 : :
# 777 : : /**
# 778 : : * vector
# 779 : : */
# 780 : : template<typename Stream, typename T, typename A>
# 781 : : void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
# 782 : 301657181 : {
# 783 : 301657181 : WriteCompactSize(os, v.size());
# 784 [ + + ][ + + ]: 301657181 : if (!v.empty())
# [ + + ][ + + ]
# [ + - ][ # # ]
# [ + + ]
# 785 : 291782559 : os.write((char*)v.data(), v.size() * sizeof(T));
# 786 : 301657181 : }
# 787 : :
# 788 : : template<typename Stream, typename T, typename A>
# 789 : : void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&)
# 790 : 28 : {
# 791 : : // A special case for std::vector<bool>, as dereferencing
# 792 : : // std::vector<bool>::const_iterator does not result in a const bool&
# 793 : : // due to std::vector's special casing for bool arguments.
# 794 : 28 : WriteCompactSize(os, v.size());
# 795 [ + + ]: 12326 : for (bool elem : v) {
# 796 : 12326 : ::Serialize(os, elem);
# 797 : 12326 : }
# 798 : 28 : }
# 799 : :
# 800 : : template<typename Stream, typename T, typename A, typename V>
# 801 : : void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
# 802 : 11630615 : {
# 803 : 11630615 : Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
# 804 : 11630615 : }
# 805 : :
# 806 : : template<typename Stream, typename T, typename A>
# 807 : : inline void Serialize(Stream& os, const std::vector<T, A>& v)
# 808 : 313287824 : {
# 809 : 313287824 : Serialize_impl(os, v, T());
# 810 : 313287824 : }
# 811 : :
# 812 : :
# 813 : : template<typename Stream, typename T, typename A>
# 814 : : void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
# 815 : 2885375 : {
# 816 : : // Limit size per read so bogus size value won't cause out of memory
# 817 : 2885375 : v.clear();
# 818 : 2885375 : unsigned int nSize = ReadCompactSize(is);
# 819 : 2885375 : unsigned int i = 0;
# 820 [ + + ][ + + ]: 3315446 : while (i < nSize)
# [ # # ][ + + ]
# [ + + ][ + + ]
# [ + + ]
# 821 : 430071 : {
# 822 : 430071 : unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
# 823 : 430071 : v.resize(i + blk);
# 824 : 430071 : is.read((char*)&v[i], blk * sizeof(T));
# 825 : 430071 : i += blk;
# 826 : 430071 : }
# 827 : 2885375 : }
# 828 : :
# 829 : : template<typename Stream, typename T, typename A, typename V>
# 830 : : void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
# 831 : 1205678 : {
# 832 : 1205678 : Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
# 833 : 1205678 : }
# 834 : :
# 835 : : template<typename Stream, typename T, typename A>
# 836 : : inline void Unserialize(Stream& is, std::vector<T, A>& v)
# 837 : 4091053 : {
# 838 : 4091053 : Unserialize_impl(is, v, T());
# 839 : 4091053 : }
# 840 : :
# 841 : :
# 842 : :
# 843 : : /**
# 844 : : * pair
# 845 : : */
# 846 : : template<typename Stream, typename K, typename T>
# 847 : : void Serialize(Stream& os, const std::pair<K, T>& item)
# 848 : 891908 : {
# 849 : 891908 : Serialize(os, item.first);
# 850 : 891908 : Serialize(os, item.second);
# 851 : 891908 : }
# 852 : :
# 853 : : template<typename Stream, typename K, typename T>
# 854 : : void Unserialize(Stream& is, std::pair<K, T>& item)
# 855 : 101938 : {
# 856 : 101938 : Unserialize(is, item.first);
# 857 : 101938 : Unserialize(is, item.second);
# 858 : 101938 : }
# 859 : :
# 860 : :
# 861 : :
# 862 : : /**
# 863 : : * map
# 864 : : */
# 865 : : template<typename Stream, typename K, typename T, typename Pred, typename A>
# 866 : : void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
# 867 : 144967 : {
# 868 : 144967 : WriteCompactSize(os, m.size());
# 869 [ + + ][ + + ]: 144967 : for (const auto& entry : m)
# [ + + ][ + + ]
# 870 : 430380 : Serialize(os, entry);
# 871 : 144967 : }
# 872 : :
# 873 : : template<typename Stream, typename K, typename T, typename Pred, typename A>
# 874 : : void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
# 875 : 10412 : {
# 876 : 10412 : m.clear();
# 877 : 10412 : unsigned int nSize = ReadCompactSize(is);
# 878 : 10412 : typename std::map<K, T, Pred, A>::iterator mi = m.begin();
# 879 [ + + ][ + + ]: 40338 : for (unsigned int i = 0; i < nSize; i++)
# [ - + ]
# 880 : 29926 : {
# 881 : 29926 : std::pair<K, T> item;
# 882 : 29926 : Unserialize(is, item);
# 883 : 29926 : mi = m.insert(mi, item);
# 884 : 29926 : }
# 885 : 10412 : }
# 886 : :
# 887 : :
# 888 : :
# 889 : : /**
# 890 : : * set
# 891 : : */
# 892 : : template<typename Stream, typename K, typename Pred, typename A>
# 893 : : void Serialize(Stream& os, const std::set<K, Pred, A>& m)
# 894 : 616 : {
# 895 : 616 : WriteCompactSize(os, m.size());
# 896 [ + + ]: 888 : for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
# 897 : 272 : Serialize(os, (*it));
# 898 : 616 : }
# 899 : :
# 900 : : template<typename Stream, typename K, typename Pred, typename A>
# 901 : : void Unserialize(Stream& is, std::set<K, Pred, A>& m)
# 902 : 245 : {
# 903 : 245 : m.clear();
# 904 : 245 : unsigned int nSize = ReadCompactSize(is);
# 905 : 245 : typename std::set<K, Pred, A>::iterator it = m.begin();
# 906 [ + + ]: 303 : for (unsigned int i = 0; i < nSize; i++)
# 907 : 58 : {
# 908 : 58 : K key;
# 909 : 58 : Unserialize(is, key);
# 910 : 58 : it = m.insert(it, key);
# 911 : 58 : }
# 912 : 245 : }
# 913 : :
# 914 : :
# 915 : :
# 916 : : /**
# 917 : : * unique_ptr
# 918 : : */
# 919 : : template<typename Stream, typename T> void
# 920 : : Serialize(Stream& os, const std::unique_ptr<const T>& p)
# 921 : : {
# 922 : : Serialize(os, *p);
# 923 : : }
# 924 : :
# 925 : : template<typename Stream, typename T>
# 926 : : void Unserialize(Stream& is, std::unique_ptr<const T>& p)
# 927 : : {
# 928 : : p.reset(new T(deserialize, is));
# 929 : : }
# 930 : :
# 931 : :
# 932 : :
# 933 : : /**
# 934 : : * shared_ptr
# 935 : : */
# 936 : : template<typename Stream, typename T> void
# 937 : : Serialize(Stream& os, const std::shared_ptr<const T>& p)
# 938 : 1259751 : {
# 939 : 1259751 : Serialize(os, *p);
# 940 : 1259751 : }
# 941 : :
# 942 : : template<typename Stream, typename T>
# 943 : : void Unserialize(Stream& is, std::shared_ptr<const T>& p)
# 944 : 248764 : {
# 945 : 248764 : p = std::make_shared<const T>(deserialize, is);
# 946 : 248764 : }
# 947 : :
# 948 : :
# 949 : :
# 950 : : /**
# 951 : : * Support for SERIALIZE_METHODS and READWRITE macro.
# 952 : : */
# 953 : : struct CSerActionSerialize
# 954 : : {
# 955 : 0 : constexpr bool ForRead() const { return false; }
# 956 : : };
# 957 : : struct CSerActionUnserialize
# 958 : : {
# 959 : 0 : constexpr bool ForRead() const { return true; }
# 960 : : };
# 961 : :
# 962 : :
# 963 : :
# 964 : :
# 965 : :
# 966 : :
# 967 : :
# 968 : :
# 969 : : /* ::GetSerializeSize implementations
# 970 : : *
# 971 : : * Computing the serialized size of objects is done through a special stream
# 972 : : * object of type CSizeComputer, which only records the number of bytes written
# 973 : : * to it.
# 974 : : *
# 975 : : * If your Serialize or SerializationOp method has non-trivial overhead for
# 976 : : * serialization, it may be worthwhile to implement a specialized version for
# 977 : : * CSizeComputer, which uses the s.seek() method to record bytes that would
# 978 : : * be written instead.
# 979 : : */
# 980 : : class CSizeComputer
# 981 : : {
# 982 : : protected:
# 983 : : size_t nSize;
# 984 : :
# 985 : : const int nVersion;
# 986 : : public:
# 987 : 2897576 : explicit CSizeComputer(int nVersionIn) : nSize(0), nVersion(nVersionIn) {}
# 988 : :
# 989 : : void write(const char *psz, size_t _nSize)
# 990 : 31979797 : {
# 991 : 31979797 : this->nSize += _nSize;
# 992 : 31979797 : }
# 993 : :
# 994 : : /** Pretend _nSize bytes are written, without specifying them. */
# 995 : : void seek(size_t _nSize)
# 996 : 20212433 : {
# 997 : 20212433 : this->nSize += _nSize;
# 998 : 20212433 : }
# 999 : :
# 1000 : : template<typename T>
# 1001 : : CSizeComputer& operator<<(const T& obj)
# 1002 : 10505139 : {
# 1003 : 10505139 : ::Serialize(*this, obj);
# 1004 : 10505139 : return (*this);
# 1005 : 10505139 : }
# 1006 : :
# 1007 : 2897577 : size_t size() const {
# 1008 : 2897577 : return nSize;
# 1009 : 2897577 : }
# 1010 : :
# 1011 : 1481436 : int GetVersion() const { return nVersion; }
# 1012 : : };
# 1013 : :
# 1014 : : template<typename Stream>
# 1015 : : void SerializeMany(Stream& s)
# 1016 : 167841036 : {
# 1017 : 167841036 : }
# 1018 : :
# 1019 : : template<typename Stream, typename Arg, typename... Args>
# 1020 : : void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
# 1021 : 306268372 : {
# 1022 : 306268372 : ::Serialize(s, arg);
# 1023 : 306268372 : ::SerializeMany(s, args...);
# 1024 : 306268372 : }
# 1025 : :
# 1026 : : template<typename Stream>
# 1027 : : inline void UnserializeMany(Stream& s)
# 1028 : 5782146 : {
# 1029 : 5782146 : }
# 1030 : :
# 1031 : : template<typename Stream, typename Arg, typename... Args>
# 1032 : : inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
# 1033 : 10049881 : {
# 1034 : 10049881 : ::Unserialize(s, arg);
# 1035 : 10049881 : ::UnserializeMany(s, args...);
# 1036 : 10049881 : }
# 1037 : :
# 1038 : : template<typename Stream, typename... Args>
# 1039 : : inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, const Args&... args)
# 1040 : 167603321 : {
# 1041 : 167603321 : ::SerializeMany(s, args...);
# 1042 : 167603321 : }
# 1043 : :
# 1044 : : template<typename Stream, typename... Args>
# 1045 : : inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&... args)
# 1046 : 5781252 : {
# 1047 : 5781252 : ::UnserializeMany(s, args...);
# 1048 : 5781252 : }
# 1049 : :
# 1050 : : template<typename Stream, typename Type, typename Fn>
# 1051 : : inline void SerRead(Stream& s, CSerActionSerialize ser_action, Type&&, Fn&&)
# 1052 : 181517 : {
# 1053 : 181517 : }
# 1054 : :
# 1055 : : template<typename Stream, typename Type, typename Fn>
# 1056 : : inline void SerRead(Stream& s, CSerActionUnserialize ser_action, Type&& obj, Fn&& fn)
# 1057 : 8089 : {
# 1058 : 8089 : fn(s, std::forward<Type>(obj));
# 1059 : 8089 : }
# 1060 : :
# 1061 : : template<typename Stream, typename Type, typename Fn>
# 1062 : : inline void SerWrite(Stream& s, CSerActionSerialize ser_action, Type&& obj, Fn&& fn)
# 1063 : 108302 : {
# 1064 : 108302 : fn(s, std::forward<Type>(obj));
# 1065 : 108302 : }
# 1066 : :
# 1067 : : template<typename Stream, typename Type, typename Fn>
# 1068 : : inline void SerWrite(Stream& s, CSerActionUnserialize ser_action, Type&&, Fn&&)
# 1069 : 2948 : {
# 1070 : 2948 : }
# 1071 : :
# 1072 : : template<typename I>
# 1073 : : inline void WriteVarInt(CSizeComputer &s, I n)
# 1074 : : {
# 1075 : : s.seek(GetSizeOfVarInt<I>(n));
# 1076 : : }
# 1077 : :
# 1078 : : inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
# 1079 : 20212433 : {
# 1080 : 20212433 : s.seek(GetSizeOfCompactSize(nSize));
# 1081 : 20212433 : }
# 1082 : :
# 1083 : : template <typename T>
# 1084 : : size_t GetSerializeSize(const T& t, int nVersion = 0)
# 1085 : 2893693 : {
# 1086 : 2893693 : return (CSizeComputer(nVersion) << t).size();
# 1087 : 2893693 : }
# 1088 : :
# 1089 : : template <typename... T>
# 1090 : : size_t GetSerializeSizeMany(int nVersion, const T&... t)
# 1091 : 3885 : {
# 1092 : 3885 : CSizeComputer sc(nVersion);
# 1093 : 3885 : SerializeMany(sc, t...);
# 1094 : 3885 : return sc.size();
# 1095 : 3885 : }
# 1096 : :
# 1097 : : #endif // BITCOIN_SERIALIZE_H
|