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