LCOV - code coverage report
Current view: top level - src - serialize.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 94.1 % 341 321
Test Date: 2026-03-13 04:56:35 Functions: 73.4 % 1800 1321
Branches: 40.1 % 1112 446

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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 <attributes.h>
      10                 :             : #include <compat/assumptions.h> // IWYU pragma: keep
      11                 :             : #include <compat/endian.h>
      12                 :             : #include <prevector.h>
      13                 :             : #include <span.h>
      14                 :             : #include <util/overflow.h>
      15                 :             : 
      16                 :             : #include <algorithm>
      17                 :             : #include <concepts>
      18                 :             : #include <cstdint>
      19                 :             : #include <cstring>
      20                 :             : #include <ios>
      21                 :             : #include <limits>
      22                 :             : #include <map>
      23                 :             : #include <memory>
      24                 :             : #include <set>
      25                 :             : #include <string>
      26                 :             : #include <utility>
      27                 :             : #include <vector>
      28                 :             : 
      29                 :             : /**
      30                 :             :  * The maximum size of a serialized object in bytes or number of elements
      31                 :             :  * (for eg vectors) when the size is encoded as CompactSize.
      32                 :             :  */
      33                 :             : static constexpr uint64_t MAX_SIZE = 0x02000000;
      34                 :             : 
      35                 :             : /** Maximum amount of memory (in bytes) to allocate at once when deserializing vectors. */
      36                 :             : static const unsigned int MAX_VECTOR_ALLOCATE = 5000000;
      37                 :             : 
      38                 :             : /**
      39                 :             :  * Dummy data type to identify deserializing constructors.
      40                 :             :  *
      41                 :             :  * By convention, a constructor of a type T with signature
      42                 :             :  *
      43                 :             :  *   template <typename Stream> T::T(deserialize_type, Stream& s)
      44                 :             :  *
      45                 :             :  * is a deserializing constructor, which builds the type by
      46                 :             :  * deserializing it from s. If T contains const fields, this
      47                 :             :  * is likely the only way to do so.
      48                 :             :  */
      49                 :             : struct deserialize_type {};
      50                 :             : constexpr deserialize_type deserialize {};
      51                 :             : 
      52                 :             : /*
      53                 :             :  * Lowest-level serialization and conversion.
      54                 :             :  */
      55                 :    41410681 : template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
      56                 :             : {
      57                 :    41410681 :     s.write(std::as_bytes(std::span{&obj, 1}));
      58                 :    41410679 : }
      59                 :        4185 : template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
      60                 :             : {
      61                 :        4185 :     obj = htole16_internal(obj);
      62                 :        4185 :     s.write(std::as_bytes(std::span{&obj, 1}));
      63                 :        4185 : }
      64                 :    39321207 : template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
      65                 :             : {
      66                 :    39321207 :     obj = htole32_internal(obj);
      67                 :    39321207 :     s.write(std::as_bytes(std::span{&obj, 1}));
      68                 :    39321207 : }
      69                 :        1191 : template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
      70                 :             : {
      71                 :        1191 :     obj = htobe32_internal(obj);
      72                 :        1191 :     s.write(std::as_bytes(std::span{&obj, 1}));
      73                 :        1191 : }
      74                 :    15683405 : template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
      75                 :             : {
      76                 :    15683405 :     obj = htole64_internal(obj);
      77                 :    15683405 :     s.write(std::as_bytes(std::span{&obj, 1}));
      78                 :    15683405 : }
      79                 :     1761266 : template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
      80                 :             : {
      81                 :             :     uint8_t obj;
      82                 :     1761266 :     s.read(std::as_writable_bytes(std::span{&obj, 1}));
      83                 :     1760306 :     return obj;
      84                 :             : }
      85                 :         112 : template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
      86                 :             : {
      87                 :             :     uint16_t obj;
      88                 :         112 :     s.read(std::as_writable_bytes(std::span{&obj, 1}));
      89                 :         112 :     return le16toh_internal(obj);
      90                 :             : }
      91                 :       81230 : template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
      92                 :             : {
      93                 :             :     uint32_t obj;
      94                 :       81230 :     s.read(std::as_writable_bytes(std::span{&obj, 1}));
      95                 :       81204 :     return le32toh_internal(obj);
      96                 :             : }
      97                 :         443 : template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
      98                 :             : {
      99                 :             :     uint32_t obj;
     100                 :         443 :     s.read(std::as_writable_bytes(std::span{&obj, 1}));
     101                 :         443 :     return be32toh_internal(obj);
     102                 :             : }
     103                 :       20576 : template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
     104                 :             : {
     105                 :             :     uint64_t obj;
     106                 :       20576 :     s.read(std::as_writable_bytes(std::span{&obj, 1}));
     107                 :       20576 :     return le64toh_internal(obj);
     108                 :             : }
     109                 :             : 
     110                 :             : 
     111                 :             : class SizeComputer;
     112                 :             : 
     113                 :             : /**
     114                 :             :  * Convert any argument to a reference to X, maintaining constness.
     115                 :             :  *
     116                 :             :  * This can be used in serialization code to invoke a base class's
     117                 :             :  * serialization routines.
     118                 :             :  *
     119                 :             :  * Example use:
     120                 :             :  *   class Base { ... };
     121                 :             :  *   class Child : public Base {
     122                 :             :  *     int m_data;
     123                 :             :  *   public:
     124                 :             :  *     SERIALIZE_METHODS(Child, obj) {
     125                 :             :  *       READWRITE(AsBase<Base>(obj), obj.m_data);
     126                 :             :  *     }
     127                 :             :  *   };
     128                 :             :  *
     129                 :             :  * static_cast cannot easily be used here, as the type of Obj will be const Child&
     130                 :             :  * during serialization and Child& during deserialization. AsBase will convert to
     131                 :             :  * const Base& and Base& appropriately.
     132                 :             :  */
     133                 :             : template <class Out, class In>
     134                 :       37625 : Out& AsBase(In& x)
     135                 :             : {
     136                 :             :     static_assert(std::is_base_of_v<Out, In>);
     137                 :       37625 :     return x;
     138                 :             : }
     139                 :             : template <class Out, class In>
     140                 :    16885248 : const Out& AsBase(const In& x)
     141                 :             : {
     142                 :             :     static_assert(std::is_base_of_v<Out, In>);
     143                 :    16885248 :     return x;
     144                 :             : }
     145                 :             : 
     146                 :             : #define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
     147                 :             : #define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
     148                 :             : #define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
     149                 :             : 
     150                 :             : /**
     151                 :             :  * Implement the Ser and Unser methods needed for implementing a formatter (see Using below).
     152                 :             :  *
     153                 :             :  * Both Ser and Unser are delegated to a single static method SerializationOps, which is polymorphic
     154                 :             :  * in the serialized/deserialized type (allowing it to be const when serializing, and non-const when
     155                 :             :  * deserializing).
     156                 :             :  *
     157                 :             :  * Example use:
     158                 :             :  *   struct FooFormatter {
     159                 :             :  *     FORMATTER_METHODS(Class, obj) { READWRITE(obj.val1, VARINT(obj.val2)); }
     160                 :             :  *   }
     161                 :             :  *   would define a class FooFormatter that defines a serialization of Class objects consisting
     162                 :             :  *   of serializing its val1 member using the default serialization, and its val2 member using
     163                 :             :  *   VARINT serialization. That FooFormatter can then be used in statements like
     164                 :             :  *   READWRITE(Using<FooFormatter>(obj.bla)).
     165                 :             :  */
     166                 :             : #define FORMATTER_METHODS(cls, obj) \
     167                 :             :     template<typename Stream> \
     168                 :             :     static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, ActionSerialize{}); } \
     169                 :             :     template<typename Stream> \
     170                 :             :     static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, ActionUnserialize{}); } \
     171                 :             :     template<typename Stream, typename Type, typename Operation> \
     172                 :             :     static void SerializationOps(Type& obj, Stream& s, Operation ser_action)
     173                 :             : 
     174                 :             : /**
     175                 :             :  * Formatter methods can retrieve parameters attached to a stream using the
     176                 :             :  * SER_PARAMS(type) macro as long as the stream is created directly or
     177                 :             :  * indirectly with a parameter of that type. This permits making serialization
     178                 :             :  * depend on run-time context in a type-safe way.
     179                 :             :  *
     180                 :             :  * Example use:
     181                 :             :  *   struct BarParameter { bool fancy; ... };
     182                 :             :  *   struct Bar { ... };
     183                 :             :  *   struct FooFormatter {
     184                 :             :  *     FORMATTER_METHODS(Bar, obj) {
     185                 :             :  *       auto& param = SER_PARAMS(BarParameter);
     186                 :             :  *       if (param.fancy) {
     187                 :             :  *         READWRITE(VARINT(obj.value));
     188                 :             :  *       } else {
     189                 :             :  *         READWRITE(obj.value);
     190                 :             :  *       }
     191                 :             :  *     }
     192                 :             :  *   };
     193                 :             :  * which would then be invoked as
     194                 :             :  *   READWRITE(BarParameter{...}(Using<FooFormatter>(obj.foo)))
     195                 :             :  *
     196                 :             :  * parameter(obj) can be invoked anywhere in the call stack; it is
     197                 :             :  * passed down recursively into all serialization code, until another
     198                 :             :  * serialization parameter overrides it.
     199                 :             :  *
     200                 :             :  * Parameters will be implicitly converted where appropriate. This means that
     201                 :             :  * "parent" serialization code can use a parameter that derives from, or is
     202                 :             :  * convertible to, a "child" formatter's parameter type.
     203                 :             :  *
     204                 :             :  * Compilation will fail in any context where serialization is invoked but
     205                 :             :  * no parameter of a type convertible to BarParameter is provided.
     206                 :             :  */
     207                 :             : #define SER_PARAMS(type) (s.template GetParams<type>())
     208                 :             : 
     209                 :             : #define BASE_SERIALIZE_METHODS(cls)                                                                 \
     210                 :             :     template <typename Stream>                                                                      \
     211                 :             :     void Serialize(Stream& s) const                                                                 \
     212                 :             :     {                                                                                               \
     213                 :             :         static_assert(std::is_same_v<const cls&, decltype(*this)>, "Serialize type mismatch");      \
     214                 :             :         Ser(s, *this);                                                                              \
     215                 :             :     }                                                                                               \
     216                 :             :     template <typename Stream>                                                                      \
     217                 :             :     void Unserialize(Stream& s)                                                                     \
     218                 :             :     {                                                                                               \
     219                 :             :         static_assert(std::is_same_v<cls&, decltype(*this)>, "Unserialize type mismatch");          \
     220                 :             :         Unser(s, *this);                                                                            \
     221                 :             :     }
     222                 :             : 
     223                 :             : /**
     224                 :             :  * Implement the Serialize and Unserialize methods by delegating to a single templated
     225                 :             :  * static method that takes the to-be-(de)serialized object as a parameter. This approach
     226                 :             :  * has the advantage that the constness of the object becomes a template parameter, and
     227                 :             :  * thus allows a single implementation that sees the object as const for serializing
     228                 :             :  * and non-const for deserializing, without casts.
     229                 :             :  */
     230                 :             : #define SERIALIZE_METHODS(cls, obj) \
     231                 :             :     BASE_SERIALIZE_METHODS(cls)     \
     232                 :             :     FORMATTER_METHODS(cls, obj)
     233                 :             : 
     234                 :             : // Templates for serializing to anything that looks like a stream,
     235                 :             : // i.e. anything that supports .read(std::span<std::byte>) and .write(std::span<const std::byte>)
     236                 :             : //
     237                 :             : 
     238                 :             : // Typically int8_t and char are distinct types, but some systems may define int8_t
     239                 :             : // in terms of char. Forbid serialization of char in the typical case, but allow it if
     240                 :             : // it's the only way to describe an int8_t.
     241                 :             : template<class T>
     242                 :             : concept CharNotInt8 = std::same_as<T, char> && !std::same_as<T, int8_t>;
     243                 :             : 
     244                 :             : // clang-format off
     245                 :             : template <typename Stream, CharNotInt8 V> void Serialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t
     246                 :           2 : template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); }
     247                 :           2 : template <typename Stream> void Serialize(Stream& s, int8_t a)    { ser_writedata8(s, uint8_t(a)); }
     248                 :     5051032 : template <typename Stream> void Serialize(Stream& s, uint8_t a)   { ser_writedata8(s, a); }
     249                 :           2 : template <typename Stream> void Serialize(Stream& s, int16_t a)   { ser_writedata16(s, uint16_t(a)); }
     250                 :          35 : template <typename Stream> void Serialize(Stream& s, uint16_t a)  { ser_writedata16(s, a); }
     251                 :     4929860 : template <typename Stream> void Serialize(Stream& s, int32_t a)   { ser_writedata32(s, uint32_t(a)); }
     252                 :    34391288 : template <typename Stream> void Serialize(Stream& s, uint32_t a)  { ser_writedata32(s, a); }
     253                 :    15614389 : template <typename Stream> void Serialize(Stream& s, int64_t a)   { ser_writedata64(s, uint64_t(a)); }
     254                 :       69010 : template <typename Stream> void Serialize(Stream& s, uint64_t a)  { ser_writedata64(s, a); }
     255                 :             : 
     256                 :         345 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, const B (&a)[N])           { s.write(MakeByteSpan(a)); }
     257                 :       17177 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { s.write(MakeByteSpan(a)); }
     258                 :    27610949 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, std::span<B, N> span)      { s.write(std::as_bytes(span)); }
     259                 :       92063 : template <typename Stream, BasicByte B>           void Serialize(Stream& s, std::span<B> span)         { s.write(std::as_bytes(span)); }
     260                 :             : 
     261                 :             : template <typename Stream, CharNotInt8 V> void Unserialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t
     262                 :          17 : template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte(ser_readdata8(s)); }
     263                 :           1 : template <typename Stream> void Unserialize(Stream& s, int8_t& a)    { a = int8_t(ser_readdata8(s)); }
     264                 :       46561 : template <typename Stream> void Unserialize(Stream& s, uint8_t& a)   { a = ser_readdata8(s); }
     265                 :             : template <typename Stream> void Unserialize(Stream& s, int16_t& a)   { a = int16_t(ser_readdata16(s)); }
     266                 :          34 : template <typename Stream> void Unserialize(Stream& s, uint16_t& a)  { a = ser_readdata16(s); }
     267                 :       14531 : template <typename Stream> void Unserialize(Stream& s, int32_t& a)   { a = int32_t(ser_readdata32(s)); }
     268                 :       66677 : template <typename Stream> void Unserialize(Stream& s, uint32_t& a)  { a = ser_readdata32(s); }
     269                 :       19195 : template <typename Stream> void Unserialize(Stream& s, int64_t& a)   { a = int64_t(ser_readdata64(s)); }
     270                 :        1379 : template <typename Stream> void Unserialize(Stream& s, uint64_t& a)  { a = ser_readdata64(s); }
     271                 :             : 
     272                 :        1953 : template <typename Stream, BasicByte B, size_t N> void Unserialize(Stream& s, B (&a)[N])            { s.read(MakeWritableByteSpan(a)); }
     273                 :        5259 : template <typename Stream, BasicByte B, size_t N> void Unserialize(Stream& s, std::array<B, N>& a)  { s.read(MakeWritableByteSpan(a)); }
     274                 :           1 : template <typename Stream, BasicByte B, size_t N> void Unserialize(Stream& s, std::span<B, N> span) { s.read(std::as_writable_bytes(span)); }
     275                 :       77679 : template <typename Stream, BasicByte B>           void Unserialize(Stream& s, std::span<B> span)    { s.read(std::as_writable_bytes(span)); }
     276                 :             : 
     277                 :         893 : template <typename Stream> void Serialize(Stream& s, bool a)    { uint8_t f = a; ser_writedata8(s, f); }
     278                 :          12 : template <typename Stream> void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; }
     279                 :             : // clang-format on
     280                 :             : 
     281                 :             : 
     282                 :             : /**
     283                 :             :  * Compact Size
     284                 :             :  * size <  253        -- 1 byte
     285                 :             :  * size <= USHRT_MAX  -- 3 bytes  (253 + 2 bytes)
     286                 :             :  * size <= UINT_MAX   -- 5 bytes  (254 + 4 bytes)
     287                 :             :  * size >  UINT_MAX   -- 9 bytes  (255 + 8 bytes)
     288                 :             :  */
     289                 :       29484 : constexpr inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
     290                 :             : {
     291   [ -  +  -  +  :       16998 :     if (nSize < 253)             return sizeof(unsigned char);
          -  +  -  +  -  
              + ][ +  + ]
     292   [ -  +  -  -  :          91 :     else if (nSize <= std::numeric_limits<uint16_t>::max()) return sizeof(unsigned char) + sizeof(uint16_t);
                   -  - ]
           [ #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
                 [ +  + ]
     293   [ #  #  #  # ]:           2 :     else if (nSize <= std::numeric_limits<unsigned int>::max())  return sizeof(unsigned char) + sizeof(unsigned int);
           [ #  #  #  #  
          #  #  #  #  #  
              # ][ -  + ]
           [ #  #  #  #  
                   #  # ]
     294                 :           0 :     else                         return sizeof(unsigned char) + sizeof(uint64_t);
     295                 :             : }
     296                 :             : 
     297                 :             : inline void WriteCompactSize(SizeComputer& os, uint64_t nSize);
     298                 :             : 
     299                 :             : template<typename Stream>
     300                 :    21441783 : void WriteCompactSize(Stream& os, uint64_t nSize)
     301                 :             : {
     302         [ +  + ]:    21441783 :     if (nSize < 253)
     303                 :             :     {
     304                 :    21437582 :         ser_writedata8(os, nSize);
     305                 :             :     }
     306         [ +  + ]:        4201 :     else if (nSize <= std::numeric_limits<uint16_t>::max())
     307                 :             :     {
     308                 :        4148 :         ser_writedata8(os, 253);
     309                 :        4148 :         ser_writedata16(os, nSize);
     310                 :             :     }
     311         [ +  - ]:          53 :     else if (nSize <= std::numeric_limits<unsigned int>::max())
     312                 :             :     {
     313                 :          53 :         ser_writedata8(os, 254);
     314                 :          53 :         ser_writedata32(os, nSize);
     315                 :             :     }
     316                 :             :     else
     317                 :             :     {
     318                 :           0 :         ser_writedata8(os, 255);
     319                 :           0 :         ser_writedata64(os, nSize);
     320                 :             :     }
     321                 :    21441782 :     return;
     322                 :             : }
     323                 :             : 
     324                 :             : /**
     325                 :             :  * Decode a CompactSize-encoded variable-length integer.
     326                 :             :  *
     327                 :             :  * As these are primarily used to encode the size of vector-like serializations, by default a range
     328                 :             :  * check is performed. When used as a generic number encoding, range_check should be set to false.
     329                 :             :  */
     330                 :             : template<typename Stream>
     331                 :       89922 : uint64_t ReadCompactSize(Stream& is, bool range_check = true)
     332                 :             : {
     333                 :       89922 :     uint8_t chSize = ser_readdata8(is);
     334                 :       89916 :     uint64_t nSizeRet = 0;
     335         [ +  + ]:       89916 :     if (chSize < 253)
     336                 :             :     {
     337                 :       89814 :         nSizeRet = chSize;
     338                 :             :     }
     339         [ +  + ]:         102 :     else if (chSize == 253)
     340                 :             :     {
     341                 :          78 :         nSizeRet = ser_readdata16(is);
     342         [ +  + ]:          78 :         if (nSizeRet < 253)
     343         [ +  - ]:           4 :             throw std::ios_base::failure("non-canonical ReadCompactSize()");
     344                 :             :     }
     345         [ +  + ]:          24 :     else if (chSize == 254)
     346                 :             :     {
     347                 :          22 :         nSizeRet = ser_readdata32(is);
     348         [ +  + ]:          22 :         if (nSizeRet < 0x10000u)
     349         [ +  - ]:           4 :             throw std::ios_base::failure("non-canonical ReadCompactSize()");
     350                 :             :     }
     351                 :             :     else
     352                 :             :     {
     353                 :           2 :         nSizeRet = ser_readdata64(is);
     354         [ +  - ]:           2 :         if (nSizeRet < 0x100000000ULL)
     355         [ +  - ]:           4 :             throw std::ios_base::failure("non-canonical ReadCompactSize()");
     356                 :             :     }
     357         [ -  + ]:       89910 :     if (range_check && nSizeRet > MAX_SIZE) {
     358         [ #  # ]:           0 :         throw std::ios_base::failure("ReadCompactSize(): size too large");
     359                 :             :     }
     360                 :       89910 :     return nSizeRet;
     361                 :             : }
     362                 :             : 
     363                 :             : /**
     364                 :             :  * Variable-length integers: bytes are a MSB base-128 encoding of the number.
     365                 :             :  * The high bit in each byte signifies whether another digit follows. To make
     366                 :             :  * sure the encoding is one-to-one, one is subtracted from all but the last digit.
     367                 :             :  * Thus, the byte sequence a[] with length len, where all but the last byte
     368                 :             :  * has bit 128 set, encodes the number:
     369                 :             :  *
     370                 :             :  *  (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
     371                 :             :  *
     372                 :             :  * Properties:
     373                 :             :  * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
     374                 :             :  * * Every integer has exactly one encoding
     375                 :             :  * * Encoding does not depend on size of original integer type
     376                 :             :  * * No redundancy: every (infinite) byte sequence corresponds to a list
     377                 :             :  *   of encoded integers.
     378                 :             :  *
     379                 :             :  * 0:         [0x00]  256:        [0x81 0x00]
     380                 :             :  * 1:         [0x01]  16383:      [0xFE 0x7F]
     381                 :             :  * 127:       [0x7F]  16384:      [0xFF 0x00]
     382                 :             :  * 128:  [0x80 0x00]  16511:      [0xFF 0x7F]
     383                 :             :  * 255:  [0x80 0x7F]  65535: [0x82 0xFE 0x7F]
     384                 :             :  * 2^32:           [0x8E 0xFE 0xFE 0xFF 0x00]
     385                 :             :  */
     386                 :             : 
     387                 :             : /**
     388                 :             :  * Mode for encoding VarInts.
     389                 :             :  *
     390                 :             :  * Currently there is no support for signed encodings. The default mode will not
     391                 :             :  * compile with signed values, and the legacy "nonnegative signed" mode will
     392                 :             :  * accept signed values, but improperly encode and decode them if they are
     393                 :             :  * negative. In the future, the DEFAULT mode could be extended to support
     394                 :             :  * negative numbers in a backwards compatible way, and additional modes could be
     395                 :             :  * added to support different varint formats (e.g. zigzag encoding).
     396                 :             :  */
     397                 :             : enum class VarIntMode { DEFAULT, NONNEGATIVE_SIGNED };
     398                 :             : 
     399                 :             : template <VarIntMode Mode, typename I>
     400                 :             : struct CheckVarIntMode {
     401                 :             :     constexpr CheckVarIntMode()
     402                 :             :     {
     403                 :             :         static_assert(Mode != VarIntMode::DEFAULT || std::is_unsigned_v<I>, "Unsigned type required with mode DEFAULT.");
     404                 :             :         static_assert(Mode != VarIntMode::NONNEGATIVE_SIGNED || std::is_signed_v<I>, "Signed type required with mode NONNEGATIVE_SIGNED.");
     405                 :             :     }
     406                 :             : };
     407                 :             : 
     408                 :             : template<VarIntMode Mode, typename I>
     409                 :             : inline unsigned int GetSizeOfVarInt(I n)
     410                 :             : {
     411                 :             :     CheckVarIntMode<Mode, I>();
     412                 :             :     int nRet = 0;
     413                 :             :     while(true) {
     414                 :             :         nRet++;
     415                 :             :         if (n <= 0x7F)
     416                 :             :             break;
     417                 :             :         n = (n >> 7) - 1;
     418                 :             :     }
     419                 :             :     return nRet;
     420                 :             : }
     421                 :             : 
     422                 :             : template<typename I>
     423                 :             : inline void WriteVarInt(SizeComputer& os, I n);
     424                 :             : 
     425                 :             : template<typename Stream, VarIntMode Mode, typename I>
     426                 :     5501814 : void WriteVarInt(Stream& os, I n)
     427                 :             : {
     428                 :             :     CheckVarIntMode<Mode, I>();
     429                 :             :     unsigned char tmp[CeilDiv(sizeof(n) * 8, 7u)];
     430                 :     5501814 :     int len=0;
     431                 :     9413964 :     while(true) {
     432         [ +  + ]:    14915778 :         tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
     433         [ +  + ]:    14915777 :         if (n <= 0x7F)
     434                 :             :             break;
     435                 :     9413964 :         n = (n >> 7) - 1;
     436                 :     9413964 :         len++;
     437                 :             :     }
     438         [ +  + ]:    14915778 :     do {
     439                 :    14915778 :         ser_writedata8(os, tmp[len]);
     440                 :             :     } while(len--);
     441                 :     5501814 : }
     442                 :             : 
     443                 :             : template<typename Stream, VarIntMode Mode, typename I>
     444                 :      804764 : I ReadVarInt(Stream& is)
     445                 :             : {
     446                 :             :     CheckVarIntMode<Mode, I>();
     447                 :      804764 :     I n = 0;
     448                 :      819546 :     while(true) {
     449                 :     1624310 :         unsigned char chData = ser_readdata8(is);
     450         [ -  + ]:     1623372 :         if (n > (std::numeric_limits<I>::max() >> 7)) {
     451         [ #  # ]:           0 :            throw std::ios_base::failure("ReadVarInt(): size too large");
     452                 :             :         }
     453                 :     1623372 :         n = (n << 7) | (chData & 0x7F);
     454         [ +  + ]:     1623372 :         if (chData & 0x80) {
     455         [ -  + ]:      819546 :             if (n == std::numeric_limits<I>::max()) {
     456         [ #  # ]:           0 :                 throw std::ios_base::failure("ReadVarInt(): size too large");
     457                 :             :             }
     458                 :      819546 :             n++;
     459                 :             :         } else {
     460                 :      803826 :             return n;
     461                 :             :         }
     462                 :             :     }
     463                 :             : }
     464                 :             : 
     465                 :             : /** Simple wrapper class to serialize objects using a formatter; used by Using(). */
     466                 :             : template<typename Formatter, typename T>
     467                 :             : class Wrapper
     468                 :             : {
     469                 :             :     static_assert(std::is_lvalue_reference_v<T>, "Wrapper needs an lvalue reference type T");
     470                 :             : protected:
     471                 :             :     T m_object;
     472                 :             : public:
     473                 :    10773811 :     explicit Wrapper(T obj) : m_object(obj) {}
     474                 :    15447105 :     template<typename Stream> void Serialize(Stream &s) const { Formatter().Ser(s, m_object); }
     475                 :     1959578 :     template<typename Stream> void Unserialize(Stream &s) { Formatter().Unser(s, m_object); }
     476                 :             : };
     477                 :             : 
     478                 :             : /** Cause serialization/deserialization of an object to be done using a specified formatter class.
     479                 :             :  *
     480                 :             :  * To use this, you need a class Formatter that has public functions Ser(stream, const object&) for
     481                 :             :  * serialization, and Unser(stream, object&) for deserialization. Serialization routines (inside
     482                 :             :  * READWRITE, or directly with << and >> operators), can then use Using<Formatter>(object).
     483                 :             :  *
     484                 :             :  * This works by constructing a Wrapper<Formatter, T>-wrapped version of object, where T is
     485                 :             :  * const during serialization, and non-const during deserialization, which maintains const
     486                 :             :  * correctness.
     487                 :             :  */
     488                 :             : template<typename Formatter, typename T>
     489   [ +  -  +  -  :     5453696 : static inline Wrapper<Formatter, T&> Using(T&& t) { return Wrapper<Formatter, T&>(t); }
             +  -  +  - ]
           [ +  -  +  - ]
           [ +  -  #  #  
             #  #  #  # ]
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  +  +  -  
          +  -  +  -  +  
          +  +  -  +  -  
          +  -  +  +  +  
          -  +  -  +  -  
          +  +  +  -  +  
          -  +  -  +  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  -  
          +  +  -  +  -  
          +  -  +  -  -  
          +  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     490                 :             : 
     491                 :             : #define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj)
     492                 :             : #define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj)
     493                 :             : #define COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj)
     494                 :             : #define LIMITED_STRING(obj,n) Using<LimitedStringFormatter<n>>(obj)
     495                 :             : 
     496                 :             : /** Serialization wrapper class for integers in VarInt format. */
     497                 :             : template<VarIntMode Mode>
     498                 :             : struct VarIntFormatter
     499                 :             : {
     500                 :     5501814 :     template<typename Stream, typename I> void Ser(Stream &s, I v)
     501                 :             :     {
     502                 :     5501814 :         WriteVarInt<Stream,Mode, std::remove_cv_t<I>>(s, v);
     503                 :             :     }
     504                 :             : 
     505                 :      804764 :     template<typename Stream, typename I> void Unser(Stream& s, I& v)
     506                 :             :     {
     507                 :     1608590 :         v = ReadVarInt<Stream,Mode, std::remove_cv_t<I>>(s);
     508                 :             :     }
     509                 :             : };
     510                 :             : 
     511                 :             : /** Serialization wrapper class for custom integers and enums.
     512                 :             :  *
     513                 :             :  * It permits specifying the serialized size (1 to 8 bytes) and endianness.
     514                 :             :  *
     515                 :             :  * Use the big endian mode for values that are stored in memory in native
     516                 :             :  * byte order, but serialized in big endian notation. This is only intended
     517                 :             :  * to implement serializers that are compatible with existing formats, and
     518                 :             :  * its use is not recommended for new data structures.
     519                 :             :  */
     520                 :             : template<int Bytes, bool BigEndian = false>
     521                 :             : struct CustomUintFormatter
     522                 :             : {
     523                 :             :     static_assert(Bytes > 0 && Bytes <= 8, "CustomUintFormatter Bytes out of range");
     524                 :             :     static constexpr uint64_t MAX = 0xffffffffffffffff >> (8 * (8 - Bytes));
     525                 :             : 
     526                 :          70 :     template <typename Stream, typename I> void Ser(Stream& s, I v)
     527                 :             :     {
     528   [ -  +  -  - ]:          32 :         if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range");
     529                 :             :         if (BigEndian) {
     530                 :          31 :             uint64_t raw = htobe64_internal(v);
     531                 :          31 :             s.write(std::as_bytes(std::span{&raw, 1}).last(Bytes));
     532                 :             :         } else {
     533                 :          39 :             uint64_t raw = htole64_internal(v);
     534                 :          39 :             s.write(std::as_bytes(std::span{&raw, 1}).first(Bytes));
     535                 :             :         }
     536                 :          70 :     }
     537                 :             : 
     538                 :          57 :     template <typename Stream, typename I> void Unser(Stream& s, I& v)
     539                 :             :     {
     540                 :             :         using U = typename std::conditional_t<std::is_enum_v<I>, std::underlying_type<I>, std::common_type<I>>::type;
     541                 :             :         static_assert(std::numeric_limits<U>::max() >= MAX && std::numeric_limits<U>::min() <= 0, "Assigned type too small");
     542                 :          57 :         uint64_t raw = 0;
     543                 :             :         if (BigEndian) {
     544                 :          26 :             s.read(std::as_writable_bytes(std::span{&raw, 1}).last(Bytes));
     545                 :          26 :             v = static_cast<I>(be64toh_internal(raw));
     546                 :             :         } else {
     547                 :          31 :             s.read(std::as_writable_bytes(std::span{&raw, 1}).first(Bytes));
     548                 :          31 :             v = static_cast<I>(le64toh_internal(raw));
     549                 :             :         }
     550                 :          57 :     }
     551                 :             : };
     552                 :             : 
     553                 :             : template<int Bytes> using BigEndianFormatter = CustomUintFormatter<Bytes, true>;
     554                 :             : 
     555                 :             : /** Formatter for integers in CompactSize format. */
     556                 :             : template<bool RangeCheck>
     557                 :             : struct CompactSizeFormatter
     558                 :             : {
     559                 :             :     template<typename Stream, typename I>
     560                 :          82 :     void Unser(Stream& s, I& v)
     561                 :             :     {
     562                 :         153 :         uint64_t n = ReadCompactSize<Stream>(s, RangeCheck);
     563         [ -  + ]:          11 :         if (n < std::numeric_limits<I>::min() || n > std::numeric_limits<I>::max()) {
     564         [ #  # ]:           0 :             throw std::ios_base::failure("CompactSize exceeds limit of type");
     565                 :             :         }
     566                 :          82 :         v = n;
     567                 :          11 :     }
     568                 :             : 
     569                 :             :     template<typename Stream, typename I>
     570                 :          44 :     void Ser(Stream& s, I v)
     571                 :             :     {
     572                 :             :         static_assert(std::is_unsigned_v<I>, "CompactSize only supported for unsigned integers");
     573                 :             :         static_assert(std::numeric_limits<I>::max() <= std::numeric_limits<uint64_t>::max(), "CompactSize only supports 64-bit integers and below");
     574                 :             : 
     575                 :          44 :         WriteCompactSize<Stream>(s, v);
     576                 :             :     }
     577                 :             : };
     578                 :             : 
     579                 :             : template <typename U, bool LOSSY = false>
     580                 :             : struct ChronoFormatter {
     581                 :             :     template <typename Stream, typename Tp>
     582                 :          41 :     void Unser(Stream& s, Tp& tp)
     583                 :             :     {
     584                 :             :         U u;
     585                 :          41 :         s >> u;
     586                 :             :         // Lossy deserialization does not make sense, so force Wnarrowing
     587                 :          41 :         tp = Tp{typename Tp::duration{typename Tp::duration::rep{u}}};
     588                 :          41 :     }
     589                 :             :     template <typename Stream, typename Tp>
     590                 :          41 :     void Ser(Stream& s, Tp tp)
     591                 :             :     {
     592                 :             :         if constexpr (LOSSY) {
     593                 :          24 :             s << U(tp.time_since_epoch().count());
     594                 :             :         } else {
     595                 :          17 :             s << U{tp.time_since_epoch().count()};
     596                 :             :         }
     597                 :          41 :     }
     598                 :             : };
     599                 :             : template <typename U>
     600                 :             : using LossyChronoFormatter = ChronoFormatter<U, true>;
     601                 :             : 
     602                 :             : class CompactSizeWriter
     603                 :             : {
     604                 :             : protected:
     605                 :             :     uint64_t n;
     606                 :             : public:
     607                 :        4448 :     explicit CompactSizeWriter(uint64_t n_in) : n(n_in) { }
     608                 :             : 
     609                 :             :     template<typename Stream>
     610                 :        4463 :     void Serialize(Stream &s) const {
     611                 :        4463 :         WriteCompactSize<Stream>(s, n);
     612                 :             :     }
     613                 :             : };
     614                 :             : 
     615                 :             : template<size_t Limit>
     616                 :             : struct LimitedStringFormatter
     617                 :             : {
     618                 :             :     template<typename Stream>
     619                 :           5 :     void Unser(Stream& s, std::string& v)
     620                 :             :     {
     621                 :           5 :         size_t size = ReadCompactSize(s);
     622         [ -  + ]:           4 :         if (size > Limit) {
     623         [ #  # ]:           0 :             throw std::ios_base::failure("String length limit exceeded");
     624                 :             :         }
     625                 :           4 :         v.resize(size);
     626         [ +  + ]:           4 :         if (size != 0) s.read(MakeWritableByteSpan(v));
     627                 :           4 :     }
     628                 :             : 
     629                 :             :     template<typename Stream>
     630                 :           3 :     void Ser(Stream& s, const std::string& v)
     631                 :             :     {
     632                 :           2 :         s << v;
     633                 :             :     }
     634                 :             : };
     635                 :             : 
     636                 :             : /** Formatter to serialize/deserialize vector elements using another formatter
     637                 :             :  *
     638                 :             :  * Example:
     639                 :             :  *   struct X {
     640                 :             :  *     std::vector<uint64_t> v;
     641                 :             :  *     SERIALIZE_METHODS(X, obj) { READWRITE(Using<VectorFormatter<VarInt>>(obj.v)); }
     642                 :             :  *   };
     643                 :             :  * will define a struct that contains a vector of uint64_t, which is serialized
     644                 :             :  * as a vector of VarInt-encoded integers.
     645                 :             :  *
     646                 :             :  * V is not required to be an std::vector type. It works for any class that
     647                 :             :  * exposes a value_type, size, reserve, emplace_back, back, and const iterators.
     648                 :             :  */
     649                 :             : template<class Formatter>
     650                 :             : struct VectorFormatter
     651                 :             : {
     652                 :             :     template<typename Stream, typename V>
     653         [ +  + ]:     4100848 :     void Ser(Stream& s, const V& v)
     654                 :             :     {
     655         [ -  + ]:           2 :         Formatter formatter;
     656                 :     4112642 :         WriteCompactSize(s, v.size());
     657         [ +  + ]:    11839609 :         for (const typename V::value_type& elem : v) {
           [ +  +  +  + ]
     658                 :     7509880 :             formatter.Ser(s, elem);
     659                 :             :         }
     660                 :     4100848 :     }
     661                 :             : 
     662                 :             :     template<typename Stream, typename V>
     663         [ +  + ]:       37548 :     void Unser(Stream& s, V& v)
     664                 :             :     {
     665         [ -  + ]:           3 :         Formatter formatter;
     666                 :       37548 :         v.clear();
     667                 :       37548 :         size_t size = ReadCompactSize(s);
     668                 :       37543 :         size_t allocated = 0;
     669         [ +  + ]:       68131 :         while (allocated < size) {
     670                 :             :             // For DoS prevention, do not blindly allocate as much as the stream claims to contain.
     671                 :             :             // Instead, allocate in 5MiB batches, so that an attacker actually needs to provide
     672                 :             :             // X MiB of data to make us allocate X+5 Mib.
     673                 :             :             static_assert(sizeof(typename V::value_type) <= MAX_VECTOR_ALLOCATE, "Vector element size too large");
     674         [ +  - ]:       30590 :             allocated = std::min(size, allocated + MAX_VECTOR_ALLOCATE / sizeof(typename V::value_type));
     675                 :       30590 :             v.reserve(allocated);
     676   [ -  +  +  + ]:      122409 :             while (v.size() < allocated) {
     677                 :       91821 :                 v.emplace_back();
     678                 :       91821 :                 formatter.Unser(s, v.back());
     679                 :             :             }
     680                 :             :         }
     681                 :       37541 :     };
     682                 :             : };
     683                 :             : 
     684                 :             : /**
     685                 :             :  * Forward declarations
     686                 :             :  */
     687                 :             : 
     688                 :             : /**
     689                 :             :  *  string
     690                 :             :  */
     691                 :             : template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
     692                 :             : template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
     693                 :             : 
     694                 :             : /**
     695                 :             :  * prevector
     696                 :             :  */
     697                 :             : template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
     698                 :             : template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
     699                 :             : 
     700                 :             : /**
     701                 :             :  * vector
     702                 :             :  */
     703                 :             : template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
     704                 :             : template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
     705                 :             : 
     706                 :             : /**
     707                 :             :  * pair
     708                 :             :  */
     709                 :             : template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
     710                 :             : template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
     711                 :             : 
     712                 :             : /**
     713                 :             :  * map
     714                 :             :  */
     715                 :             : template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
     716                 :             : template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
     717                 :             : 
     718                 :             : /**
     719                 :             :  * set
     720                 :             :  */
     721                 :             : template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
     722                 :             : template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
     723                 :             : 
     724                 :             : /**
     725                 :             :  * shared_ptr
     726                 :             :  */
     727                 :             : template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
     728                 :             : template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
     729                 :             : 
     730                 :             : /**
     731                 :             :  * unique_ptr
     732                 :             :  */
     733                 :             : template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
     734                 :             : template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
     735                 :             : 
     736                 :             : 
     737                 :             : /**
     738                 :             :  * If none of the specialized versions above matched, default to calling member function.
     739                 :             :  */
     740                 :             : template <class T, class Stream>
     741                 :             : concept Serializable = requires(T a, Stream s) { a.Serialize(s); };
     742                 :             : template <typename Stream, typename T>
     743                 :             :     requires Serializable<T, Stream>
     744                 :   101362955 : void Serialize(Stream& os, const T& a)
     745                 :             : {
     746                 :   101362954 :     a.Serialize(os);
     747                 :   101362954 : }
     748                 :             : 
     749                 :             : template <class T, class Stream>
     750                 :             : concept Unserializable = requires(T a, Stream s) { a.Unserialize(s); };
     751                 :             : template <typename Stream, typename T>
     752                 :             :     requires Unserializable<T, Stream>
     753                 :     1346108 : void Unserialize(Stream& is, T&& a)
     754                 :             : {
     755                 :     1345122 :     a.Unserialize(is);
     756                 :     1345100 : }
     757                 :             : 
     758                 :             : /** Default formatter. Serializes objects as themselves.
     759                 :             :  *
     760                 :             :  * The vector/prevector serialization code passes this to VectorFormatter
     761                 :             :  * to enable reusing that logic. It shouldn't be needed elsewhere.
     762                 :             :  */
     763                 :             : struct DefaultFormatter
     764                 :             : {
     765                 :             :     template<typename Stream, typename T>
     766                 :     7432234 :     static void Ser(Stream& s, const T& t) { Serialize(s, t); }
     767                 :             : 
     768                 :             :     template<typename Stream, typename T>
     769                 :       91805 :     static void Unser(Stream& s, T& t) { Unserialize(s, t); }
     770                 :             : };
     771                 :             : 
     772                 :             : 
     773                 :             : 
     774                 :             : 
     775                 :             : 
     776                 :             : /**
     777                 :             :  * string
     778                 :             :  */
     779                 :             : template<typename Stream, typename C>
     780         [ -  + ]:       71839 : void Serialize(Stream& os, const std::basic_string<C>& str)
     781                 :             : {
     782         [ +  + ]:       71840 :     WriteCompactSize(os, str.size());
     783         [ +  + ]:       71838 :     if (!str.empty())
     784                 :       65137 :         os.write(MakeByteSpan(str));
     785                 :       71838 : }
     786                 :             : 
     787                 :             : template<typename Stream, typename C>
     788                 :         306 : void Unserialize(Stream& is, std::basic_string<C>& str)
     789                 :             : {
     790                 :         306 :     unsigned int nSize = ReadCompactSize(is);
     791                 :         306 :     str.resize(nSize);
     792         [ +  + ]:         306 :     if (nSize != 0)
     793                 :         278 :         is.read(MakeWritableByteSpan(str));
     794                 :         306 : }
     795                 :             : 
     796                 :             : 
     797                 :             : 
     798                 :             : /**
     799                 :             :  * prevector
     800                 :             :  */
     801                 :             : template <typename Stream, unsigned int N, typename T>
     802         [ +  + ]:    17061045 : void Serialize(Stream& os, const prevector<N, T>& v)
     803                 :             : {
     804                 :             :     if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
     805         [ +  + ]:    16793587 :         WriteCompactSize(os, v.size());
     806         [ +  + ]:    16793533 :         if (!v.empty()) os.write(MakeByteSpan(v));
     807                 :             :     } else {
     808                 :      267512 :         Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
     809                 :             :     }
     810                 :    17061045 : }
     811                 :             : 
     812                 :             : 
     813                 :             : template <typename Stream, unsigned int N, typename T>
     814                 :       31629 : void Unserialize(Stream& is, prevector<N, T>& v)
     815                 :             : {
     816                 :             :     if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
     817                 :             :         // Limit size per read so bogus size value won't cause out of memory
     818                 :       31629 :         v.clear();
     819                 :       31629 :         unsigned int nSize = ReadCompactSize(is);
     820                 :       31629 :         unsigned int i = 0;
     821         [ +  + ]:       57886 :         while (i < nSize) {
     822         [ +  - ]:       26257 :             unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
     823         [ +  + ]:       26257 :             v.resize_uninitialized(i + blk);
     824                 :       26257 :             is.read(std::as_writable_bytes(std::span{&v[i], blk}));
     825                 :       26257 :             i += blk;
     826                 :             :         }
     827                 :             :     } else {
     828                 :             :         Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
     829                 :             :     }
     830                 :       31629 : }
     831                 :             : 
     832                 :             : 
     833                 :             : /**
     834                 :             :  * vector
     835                 :             :  */
     836                 :             : template <typename Stream, typename T, typename A>
     837         [ -  + ]:     4034930 : void Serialize(Stream& os, const std::vector<T, A>& v)
     838                 :             : {
     839                 :             :     if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
     840                 :      211060 :         WriteCompactSize(os, v.size());
     841         [ +  + ]:      208199 :         if (!v.empty()) os.write(MakeByteSpan(v));
     842                 :             :     } else if constexpr (std::is_same_v<T, bool>) {
     843                 :             :         // A special case for std::vector<bool>, as dereferencing
     844                 :             :         // std::vector<bool>::const_iterator does not result in a const bool&
     845                 :             :         // due to std::vector's special casing for bool arguments.
     846                 :           1 :         WriteCompactSize(os, v.size());
     847   [ -  +  -  + ]:          29 :         for (bool elem : v) {
     848                 :          27 :             ::Serialize(os, elem);
     849                 :             :         }
     850                 :             :     } else {
     851                 :     3832934 :         Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
     852                 :             :     }
     853                 :     4034930 : }
     854                 :             : 
     855                 :             : 
     856                 :             : template <typename Stream, typename T, typename A>
     857         [ +  + ]:       53496 : void Unserialize(Stream& is, std::vector<T, A>& v)
     858                 :             : {
     859                 :             :     if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
     860                 :             :         // Limit size per read so bogus size value won't cause out of memory
     861                 :       15964 :         v.clear();
     862                 :       15964 :         unsigned int nSize = ReadCompactSize(is);
     863                 :       15964 :         unsigned int i = 0;
     864         [ +  + ]:       31908 :         while (i < nSize) {
     865         [ +  - ]:       15944 :             unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
     866                 :       15944 :             v.resize(i + blk);
     867                 :       15944 :             is.read(std::as_writable_bytes(std::span{&v[i], blk}));
     868                 :       15944 :             i += blk;
     869                 :             :         }
     870                 :             :     } else {
     871                 :       37532 :         Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
     872                 :             :     }
     873                 :       53490 : }
     874                 :             : 
     875                 :             : 
     876                 :             : /**
     877                 :             :  * pair
     878                 :             :  */
     879                 :             : template<typename Stream, typename K, typename T>
     880                 :       37158 : void Serialize(Stream& os, const std::pair<K, T>& item)
     881                 :             : {
     882                 :       37158 :     Serialize(os, item.first);
     883                 :       37158 :     Serialize(os, item.second);
     884                 :       37158 : }
     885                 :             : 
     886                 :             : template<typename Stream, typename K, typename T>
     887                 :        1957 : void Unserialize(Stream& is, std::pair<K, T>& item)
     888                 :             : {
     889                 :        1957 :     Unserialize(is, item.first);
     890                 :        1957 :     Unserialize(is, item.second);
     891                 :        1948 : }
     892                 :             : 
     893                 :             : 
     894                 :             : 
     895                 :             : /**
     896                 :             :  * map
     897                 :             :  */
     898                 :             : template<typename Stream, typename K, typename T, typename Pred, typename A>
     899                 :         427 : void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
     900                 :             : {
     901                 :         427 :     WriteCompactSize(os, m.size());
     902         [ +  + ]:        1705 :     for (const auto& entry : m)
     903                 :        1278 :         Serialize(os, entry);
     904                 :         427 : }
     905                 :             : 
     906                 :             : template<typename Stream, typename K, typename T, typename Pred, typename A>
     907                 :           2 : void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
     908                 :             : {
     909                 :           2 :     m.clear();
     910                 :           2 :     unsigned int nSize = ReadCompactSize(is);
     911                 :           2 :     typename std::map<K, T, Pred, A>::iterator mi = m.begin();
     912         [ +  + ]:           8 :     for (unsigned int i = 0; i < nSize; i++)
     913                 :             :     {
     914                 :           6 :         std::pair<K, T> item;
     915         [ +  - ]:           6 :         Unserialize(is, item);
     916         [ +  - ]:           6 :         mi = m.insert(mi, item);
     917                 :             :     }
     918                 :           2 : }
     919                 :             : 
     920                 :             : 
     921                 :             : 
     922                 :             : /**
     923                 :             :  * set
     924                 :             :  */
     925                 :             : template<typename Stream, typename K, typename Pred, typename A>
     926                 :           1 : void Serialize(Stream& os, const std::set<K, Pred, A>& m)
     927                 :             : {
     928                 :           1 :     WriteCompactSize(os, m.size());
     929         [ -  + ]:           1 :     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
     930                 :           0 :         Serialize(os, (*it));
     931                 :           1 : }
     932                 :             : 
     933                 :             : template<typename Stream, typename K, typename Pred, typename A>
     934                 :           0 : void Unserialize(Stream& is, std::set<K, Pred, A>& m)
     935                 :             : {
     936                 :           0 :     m.clear();
     937                 :           0 :     unsigned int nSize = ReadCompactSize(is);
     938                 :           0 :     typename std::set<K, Pred, A>::iterator it = m.begin();
     939         [ #  # ]:           0 :     for (unsigned int i = 0; i < nSize; i++)
     940                 :             :     {
     941                 :           0 :         K key;
     942                 :           0 :         Unserialize(is, key);
     943                 :           0 :         it = m.insert(it, key);
     944                 :             :     }
     945                 :           0 : }
     946                 :             : 
     947                 :             : 
     948                 :             : 
     949                 :             : /**
     950                 :             :  * unique_ptr
     951                 :             :  */
     952                 :             : template<typename Stream, typename T> void
     953                 :             : Serialize(Stream& os, const std::unique_ptr<const T>& p)
     954                 :             : {
     955                 :             :     Serialize(os, *p);
     956                 :             : }
     957                 :             : 
     958                 :             : template<typename Stream, typename T>
     959                 :             : void Unserialize(Stream& is, std::unique_ptr<const T>& p)
     960                 :             : {
     961                 :             :     p.reset(new T(deserialize, is));
     962                 :             : }
     963                 :             : 
     964                 :             : 
     965                 :             : 
     966                 :             : /**
     967                 :             :  * shared_ptr
     968                 :             :  */
     969                 :             : template<typename Stream, typename T> void
     970                 :       93542 : Serialize(Stream& os, const std::shared_ptr<const T>& p)
     971                 :             : {
     972                 :       93542 :     Serialize(os, *p);
     973                 :       93542 : }
     974                 :             : 
     975                 :             : template<typename Stream, typename T>
     976                 :        6789 : void Unserialize(Stream& is, std::shared_ptr<const T>& p)
     977                 :             : {
     978         [ -  + ]:        6789 :     p = std::make_shared<const T>(deserialize, is);
     979                 :        6789 : }
     980                 :             : 
     981                 :             : /**
     982                 :             :  * Support for (un)serializing many things at once
     983                 :             :  */
     984                 :             : 
     985                 :             : template <typename Stream, typename... Args>
     986                 :    60546410 : void SerializeMany(Stream& s, const Args&... args)
     987                 :             : {
     988                 :    60546410 :     (::Serialize(s, args), ...);
     989                 :    60546410 : }
     990                 :             : 
     991                 :             : template <typename Stream, typename... Args>
     992                 :      193551 : inline void UnserializeMany(Stream& s, Args&&... args)
     993                 :             : {
     994                 :      145084 :     (::Unserialize(s, args), ...);
     995                 :       28560 : }
     996                 :             : 
     997                 :             : /**
     998                 :             :  * Support for all macros providing or using the ser_action parameter of the SerializationOps method.
     999                 :             :  */
    1000                 :             : struct ActionSerialize {
    1001                 :             :     static constexpr bool ForRead() { return false; }
    1002                 :             : 
    1003                 :             :     template<typename Stream, typename... Args>
    1004                 :    60546262 :     static void SerReadWriteMany(Stream& s, const Args&... args)
    1005                 :             :     {
    1006 [ +  - ][ +  -  :    60543733 :         ::SerializeMany(s, args...);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1007                 :       43127 :     }
    1008                 :             : 
    1009                 :             :     template<typename Stream, typename Type, typename Fn>
    1010                 :             :     static void SerRead(Stream& s, Type&&, Fn&&)
    1011                 :             :     {
    1012                 :             :     }
    1013                 :             : 
    1014                 :             :     template<typename Stream, typename Type, typename Fn>
    1015                 :       13887 :     static void SerWrite(Stream& s, Type&& obj, Fn&& fn)
    1016                 :             :     {
    1017         [ +  - ]:       13887 :         fn(s, std::forward<Type>(obj));
    1018                 :       13665 :     }
    1019                 :             : };
    1020                 :             : struct ActionUnserialize {
    1021                 :             :     static constexpr bool ForRead() { return true; }
    1022                 :             : 
    1023                 :             :     template<typename Stream, typename... Args>
    1024 [ +  - ][ +  -  :      164772 :     static void SerReadWriteMany(Stream& s, Args&&... args)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1025                 :             :     {
    1026   [ +  -  +  + ]:      112971 :         ::UnserializeMany(s, args...);
           [ +  +  +  +  
          +  +  +  +  -  
          -  -  -  -  -  
           -  - ][ +  + ]
           [ +  -  +  -  
          +  -  +  +  +  
          +  +  +  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1027                 :       15665 :     }
    1028                 :             : 
    1029                 :             :     template<typename Stream, typename Type, typename Fn>
    1030         [ +  + ]:         211 :     static void SerRead(Stream& s, Type&& obj, Fn&& fn)
    1031                 :             :     {
    1032         [ +  - ]:         377 :         fn(s, std::forward<Type>(obj));
    1033                 :         205 :     }
    1034                 :             : 
    1035                 :             :     template<typename Stream, typename Type, typename Fn>
    1036                 :             :     static void SerWrite(Stream& s, Type&&, Fn&&)
    1037                 :             :     {
    1038                 :             :     }
    1039                 :             : };
    1040                 :             : 
    1041                 :             : /* ::GetSerializeSize implementations
    1042                 :             :  *
    1043                 :             :  * Computing the serialized size of objects is done through a special stream
    1044                 :             :  * object of type SizeComputer, which only records the number of bytes written
    1045                 :             :  * to it.
    1046                 :             :  *
    1047                 :             :  * If your Serialize or SerializationOp method has non-trivial overhead for
    1048                 :             :  * serialization, it may be worthwhile to implement a specialized version for
    1049                 :             :  * SizeComputer, which uses the s.seek() method to record bytes that would
    1050                 :             :  * be written instead.
    1051                 :             :  */
    1052                 :             : class SizeComputer
    1053                 :             : {
    1054                 :             : protected:
    1055                 :             :     uint64_t m_size{0};
    1056                 :             : 
    1057                 :             : public:
    1058                 :             :     SizeComputer() = default;
    1059                 :             : 
    1060                 :     5661023 :     void write(std::span<const std::byte> src)
    1061                 :             :     {
    1062                 :     5661016 :         m_size += src.size();
    1063                 :        6842 :     }
    1064                 :             : 
    1065                 :             :     /** Pretend this many bytes are written, without specifying them. */
    1066                 :       21653 :     void seek(uint64_t num)
    1067                 :             :     {
    1068                 :       21653 :         m_size += num;
    1069                 :             :     }
    1070                 :             : 
    1071                 :             :     template <typename T>
    1072                 :      436972 :     SizeComputer& operator<<(const T& obj)
    1073                 :             :     {
    1074                 :      426765 :         ::Serialize(*this, obj);
    1075                 :             :         return *this;
    1076                 :             :     }
    1077                 :             : 
    1078                 :      436655 :     uint64_t size() const
    1079                 :             :     {
    1080                 :      436655 :         return m_size;
    1081                 :             :     }
    1082                 :             : };
    1083                 :             : 
    1084                 :             : template<typename I>
    1085                 :             : inline void WriteVarInt(SizeComputer &s, I n)
    1086                 :             : {
    1087                 :             :     s.seek(GetSizeOfVarInt<I>(n));
    1088                 :             : }
    1089                 :             : 
    1090                 :       20914 : inline void WriteCompactSize(SizeComputer &s, uint64_t nSize)
    1091                 :             : {
    1092   [ -  +  +  +  :       21731 :     s.seek(GetSizeOfCompactSize(nSize));
          -  -  -  -  -  
           - ][ +  +  +  
                +  -  + ]
           [ -  +  +  + ]
    1093                 :             : }
    1094                 :             : 
    1095                 :             : template <typename T>
    1096                 :      436638 : uint64_t GetSerializeSize(const T& t)
    1097                 :             : {
    1098                 :      436638 :     return (SizeComputer() << t).size();
    1099                 :             : }
    1100                 :             : 
    1101                 :             : //! Check if type contains a stream by seeing if has a GetStream() method.
    1102                 :             : template<typename T>
    1103                 :             : concept ContainsStream = requires(T t) { t.GetStream(); };
    1104                 :             : 
    1105                 :             : /** Wrapper that overrides the GetParams() function of a stream. */
    1106                 :             : template <typename SubStream, typename Params>
    1107                 :           1 : class ParamsStream
    1108                 :             : {
    1109                 :             :     const Params& m_params;
    1110                 :             :     // If ParamsStream constructor is passed an lvalue argument, Substream will
    1111                 :             :     // be a reference type, and m_substream will reference that argument.
    1112                 :             :     // Otherwise m_substream will be a substream instance and move from the
    1113                 :             :     // argument. Letting ParamsStream contain a substream instance instead of
    1114                 :             :     // just a reference is useful to make the ParamsStream object self contained
    1115                 :             :     // and let it do cleanup when destroyed, for example by closing files if
    1116                 :             :     // SubStream is a file stream.
    1117                 :             :     SubStream m_substream;
    1118                 :             : 
    1119                 :             : public:
    1120   [ +  -  +  -  :     1643623 :     ParamsStream(SubStream&& substream, const Params& params LIFETIMEBOUND) : m_params{params}, m_substream{std::forward<SubStream>(substream)} {}
          -  -  -  -  +  
                -  +  - ]
    1121                 :             : 
    1122                 :             :     template <typename NestedSubstream, typename Params1, typename Params2, typename... NestedParams>
    1123                 :           3 :     ParamsStream(NestedSubstream&& s, const Params1& params1 LIFETIMEBOUND, const Params2& params2 LIFETIMEBOUND, const NestedParams&... params LIFETIMEBOUND)
    1124         [ +  - ]:           3 :         : ParamsStream{::ParamsStream{std::forward<NestedSubstream>(s), params2, params...}, params1} {}
    1125                 :             : 
    1126   [ +  -  +  - ]:     6349782 :     template <typename U> ParamsStream& operator<<(const U& obj) { ::Serialize(*this, obj); return *this; }
           [ +  -  +  -  
             -  -  -  - ]
           [ #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
           #  # ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
          -  +  -  -  -  
           +  - ][ +  -  
          -  -  -  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
          -  +  -  +  -  
                   +  - ]
    1127   [ +  -  +  -  :       46534 :     template <typename U> ParamsStream& operator>>(U&& obj) { ::Unserialize(*this, obj); return *this; }
          +  -  +  -  +  
          -  +  +  -  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  +  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
              - ][ #  # ]
           [ +  -  -  -  
          -  -  +  -  +  
                -  +  - ]
    1128                 :    29000858 :     void write(std::span<const std::byte> src) { GetStream().write(src); }
    1129                 :      186376 :     void read(std::span<std::byte> dst) { GetStream().read(dst); }
    1130                 :           3 :     void ignore(size_t num) { GetStream().ignore(num); }
    1131                 :           0 :     bool empty() const { return GetStream().empty(); }
    1132                 :             :     size_t size() const { return GetStream().size(); }
    1133                 :             : 
    1134                 :             :     //! Get reference to stream parameters.
    1135                 :             :     template <typename P>
    1136                 :     1645526 :     const auto& GetParams() const
    1137                 :             :     {
    1138                 :             :         if constexpr (std::is_convertible_v<Params, P>) {
    1139         [ +  + ]:     1645524 :             return m_params;
           [ -  -  +  - ]
           [ +  +  +  +  
          -  -  -  -  -  
          -  -  -  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
          +  +  -  -  -  
          -  -  -  -  -  
           -  - ][ -  +  
          -  -  -  +  -  
          -  -  -  -  -  
           -  + ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ +  +  +  +  
          +  +  #  #  #  
           # ][ +  +  -  
          +  -  +  +  +  
          -  -  #  #  #  
           #  #  # ][ -  
          -  +  +  +  +  
          +  +  +  -  +  
             -  +  -  +  
                      - ]
    1140                 :             :         } else {
    1141                 :           2 :             return m_substream.template GetParams<P>();
    1142                 :             :         }
    1143                 :             :     }
    1144                 :             : 
    1145                 :             :     //! Get reference to underlying stream.
    1146                 :    17525017 :     auto& GetStream()
    1147                 :             :     {
    1148                 :             :         if constexpr (ContainsStream<SubStream>) {
    1149   [ +  -  +  -  :         173 :             return m_substream.GetStream();
                   +  - ]
    1150                 :             :         } else {
    1151                 :    12850622 :             return m_substream;
    1152                 :             :         }
    1153                 :             :     }
    1154                 :             :     const auto& GetStream() const
    1155                 :             :     {
    1156                 :             :         if constexpr (ContainsStream<SubStream>) {
    1157                 :             :             return m_substream.GetStream();
    1158                 :             :         } else {
    1159         [ #  # ]:           0 :             return m_substream;
    1160                 :             :         }
    1161                 :             :     }
    1162                 :             : };
    1163                 :             : 
    1164                 :             : /**
    1165                 :             :  * Explicit template deduction guide is required for single-parameter
    1166                 :             :  * constructor so Substream&& is treated as a forwarding reference, and
    1167                 :             :  * SubStream is deduced as reference type for lvalue arguments.
    1168                 :             :  */
    1169                 :             : template <typename Substream, typename Params>
    1170                 :             : ParamsStream(Substream&&, const Params&) -> ParamsStream<Substream, Params>;
    1171                 :             : 
    1172                 :             : /**
    1173                 :             :  * Template deduction guide for multiple params arguments that creates a nested
    1174                 :             :  * ParamsStream.
    1175                 :             :  */
    1176                 :             : template <typename Substream, typename Params1, typename Params2, typename... Params>
    1177                 :             : ParamsStream(Substream&& s, const Params1& params1, const Params2& params2, const Params&... params) ->
    1178                 :             :     ParamsStream<decltype(ParamsStream{std::forward<Substream>(s), params2, params...}), Params1>;
    1179                 :             : 
    1180                 :             : /** Wrapper that serializes objects with the specified parameters. */
    1181                 :             : template <typename Params, typename T>
    1182                 :             : class ParamsWrapper
    1183                 :             : {
    1184                 :             :     const Params& m_params;
    1185                 :             :     T& m_object;
    1186                 :             : 
    1187                 :             : public:
    1188 [ +  - ][ +  -  :     1512168 :     explicit ParamsWrapper(const Params& params, T& obj) : m_params{params}, m_object{obj} {}
             +  +  +  - ]
           [ +  -  +  -  
             +  +  +  + ]
           [ +  +  +  -  
             #  #  #  # ]
           [ -  -  -  -  
          +  -  -  -  -  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ +  -  +  
          -  +  -  -  +  
          -  +  -  +  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  -  +  +  
          -  +  -  -  +  
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
    1189                 :             : 
    1190                 :             :     template <typename Stream>
    1191                 :     1637058 :     void Serialize(Stream& s) const
    1192                 :             :     {
    1193                 :     1637058 :         ParamsStream ss{s, m_params};
    1194                 :     1637058 :         ::Serialize(ss, m_object);
    1195                 :             :     }
    1196                 :             :     template <typename Stream>
    1197                 :        6553 :     void Unserialize(Stream& s)
    1198                 :             :     {
    1199                 :        6553 :         ParamsStream ss{s, m_params};
    1200                 :        6553 :         ::Unserialize(ss, m_object);
    1201                 :             :     }
    1202                 :             : };
    1203                 :             : 
    1204                 :             : /**
    1205                 :             :  * Helper macro for SerParams structs
    1206                 :             :  *
    1207                 :             :  * Allows you define SerParams instances and then apply them directly
    1208                 :             :  * to an object via function call syntax, eg:
    1209                 :             :  *
    1210                 :             :  *   constexpr SerParams FOO{....};
    1211                 :             :  *   ss << FOO(obj);
    1212                 :             :  */
    1213                 :             : #define SER_PARAMS_OPFUNC                                                                \
    1214                 :             :     /**                                                                                  \
    1215                 :             :      * Return a wrapper around t that (de)serializes it with specified parameter params. \
    1216                 :             :      *                                                                                   \
    1217                 :             :      * See SER_PARAMS for more information on serialization parameters.                  \
    1218                 :             :      */                                                                                  \
    1219                 :             :     template <typename T>                                                                \
    1220                 :             :     auto operator()(T&& t) const                                                         \
    1221                 :             :     {                                                                                    \
    1222                 :             :         return ParamsWrapper{*this, t};                                                  \
    1223                 :             :     }
    1224                 :             : 
    1225                 :             : #endif // BITCOIN_SERIALIZE_H
        

Generated by: LCOV version 2.0-1