LCOV - code coverage report
Current view: top level - src - serialize.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 436 447 97.5 %
Date: 2022-04-21 14:51:19 Functions: 2934 3716 79.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 630 944 66.7 %

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

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a