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 : 85511806 : template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
56 : : {
57 : 85511806 : s.write(std::as_bytes(std::span{&obj, 1}));
58 : 85511804 : }
59 : 73169 : template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
60 : : {
61 : 73169 : obj = htole16_internal(obj);
62 : 73169 : s.write(std::as_bytes(std::span{&obj, 1}));
63 : 73169 : }
64 : 146377698 : template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
65 : : {
66 : 146377698 : obj = htole32_internal(obj);
67 : 146377698 : s.write(std::as_bytes(std::span{&obj, 1}));
68 : 146377698 : }
69 : 31195 : template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
70 : : {
71 : 31195 : obj = htobe32_internal(obj);
72 : 31195 : s.write(std::as_bytes(std::span{&obj, 1}));
73 : 31195 : }
74 : 66635987 : template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
75 : : {
76 : 66635987 : obj = htole64_internal(obj);
77 : 66635987 : s.write(std::as_bytes(std::span{&obj, 1}));
78 : 66635987 : }
79 : 12046397 : template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
80 : : {
81 : : uint8_t obj;
82 : 12046397 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
83 : 12045412 : return obj;
84 : : }
85 : 16025 : template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
86 : : {
87 : : uint16_t obj;
88 : 16025 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
89 : 16025 : return le16toh_internal(obj);
90 : : }
91 : 5320729 : template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
92 : : {
93 : : uint32_t obj;
94 : 5320729 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
95 : 5318433 : return le32toh_internal(obj);
96 : : }
97 : 3184 : template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
98 : : {
99 : : uint32_t obj;
100 : 3184 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
101 : 3184 : return be32toh_internal(obj);
102 : : }
103 : 23376100 : template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
104 : : {
105 : : uint64_t obj;
106 : 23376100 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
107 : 23375961 : 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 : 1589704 : Out& AsBase(In& x)
135 : : {
136 : : static_assert(std::is_base_of_v<Out, In>);
137 : 1589704 : return x;
138 : : }
139 : : template <class Out, class In>
140 : 31331423 : const Out& AsBase(const In& x)
141 : : {
142 : : static_assert(std::is_base_of_v<Out, In>);
143 : 31331423 : 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 : 8702124 : 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 : 45 : template <typename Stream> void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
251 : 29167425 : template <typename Stream> void Serialize(Stream& s, int32_t a) { ser_writedata32(s, uint32_t(a)); }
252 : 117058943 : template <typename Stream> void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
253 : 24443256 : template <typename Stream> void Serialize(Stream& s, int64_t a) { ser_writedata64(s, uint64_t(a)); }
254 : 42192708 : template <typename Stream> void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
255 : :
256 : 355897 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, const B (&a)[N]) { s.write(MakeByteSpan(a)); }
257 : 416996 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { s.write(MakeByteSpan(a)); }
258 : 80109053 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, std::span<B, N> span) { s.write(std::as_bytes(span)); }
259 : 801142 : 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 : 32 : 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 : 791446 : 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 : 5622 : template <typename Stream> void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
267 : 1258779 : template <typename Stream> void Unserialize(Stream& s, int32_t& a) { a = int32_t(ser_readdata32(s)); }
268 : 4041207 : template <typename Stream> void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
269 : 844587 : template <typename Stream> void Unserialize(Stream& s, int64_t& a) { a = int64_t(ser_readdata64(s)); }
270 : 22531503 : template <typename Stream> void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
271 : :
272 : 325558 : template <typename Stream, BasicByte B, size_t N> void Unserialize(Stream& s, B (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
273 : 338655 : template <typename Stream, BasicByte B, size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { s.read(MakeWritableByteSpan(a)); }
274 : 6110 : 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 : 406300 : template <typename Stream, BasicByte B> void Unserialize(Stream& s, std::span<B> span) { s.read(std::as_writable_bytes(span)); }
276 : :
277 : 53680 : template <typename Stream> void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
278 : 19971 : 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 : 1292211 : constexpr inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
290 : : {
291 [ + + ][ + + : 1279792 : if (nSize < 253) return sizeof(unsigned char);
+ + + + -
+ + + ]
292 [ + + ]: 2279 : else if (nSize <= std::numeric_limits<uint16_t>::max()) return sizeof(unsigned char) + sizeof(uint16_t);
[ - + - + ]
[ - + + +
- + ][ - -
- + - + -
+ - + -
- ]
293 [ - + - - ]: 10 : 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 : 54290897 : void WriteCompactSize(Stream& os, uint64_t nSize)
301 : : {
302 [ + + ]: 54290897 : if (nSize < 253)
303 : : {
304 : 54066468 : ser_writedata8(os, nSize);
305 : : }
306 [ + + ]: 224429 : else if (nSize <= std::numeric_limits<uint16_t>::max())
307 : : {
308 : 73122 : ser_writedata8(os, 253);
309 : 73122 : ser_writedata16(os, nSize);
310 : : }
311 [ + - ]: 151307 : else if (nSize <= std::numeric_limits<unsigned int>::max())
312 : : {
313 : 151307 : ser_writedata8(os, 254);
314 : 151307 : ser_writedata32(os, nSize);
315 : : }
316 : : else
317 : : {
318 : 0 : ser_writedata8(os, 255);
319 : 0 : ser_writedata64(os, nSize);
320 : : }
321 : 54290896 : 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 : 5720601 : uint64_t ReadCompactSize(Stream& is, bool range_check = true)
332 : : {
333 : 5720601 : uint8_t chSize = ser_readdata8(is);
334 : 5720584 : uint64_t nSizeRet = 0;
335 [ + + ]: 5720584 : if (chSize < 253)
336 : : {
337 : 5689428 : nSizeRet = chSize;
338 : : }
339 [ + + ]: 31156 : else if (chSize == 253)
340 : : {
341 : 10403 : nSizeRet = ser_readdata16(is);
342 [ + + ]: 10403 : if (nSizeRet < 253)
343 [ + - ]: 4 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
344 : : }
345 [ + + ]: 20753 : else if (chSize == 254)
346 : : {
347 : 20743 : nSizeRet = ser_readdata32(is);
348 [ + + ]: 20743 : if (nSizeRet < 0x10000u)
349 [ + - ]: 4 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
350 : : }
351 : : else
352 : : {
353 : 10 : nSizeRet = ser_readdata64(is);
354 [ + + ]: 6 : if (nSizeRet < 0x100000000ULL)
355 [ + - ]: 6 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
356 : : }
357 [ + + ]: 5720573 : if (range_check && nSizeRet > MAX_SIZE) {
358 [ + - ]: 6 : throw std::ios_base::failure("ReadCompactSize(): size too large");
359 : : }
360 : 5720570 : 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 : 9304749 : void WriteVarInt(Stream& os, I n)
427 : : {
428 : : CheckVarIntMode<Mode, I>();
429 : : unsigned char tmp[CeilDiv(sizeof(n) * 8, 7u)];
430 : 9304749 : int len=0;
431 : 13129157 : while(true) {
432 [ + + ]: 22433906 : tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
433 [ + + ]: 22433905 : if (n <= 0x7F)
434 : : break;
435 : 13129157 : n = (n >> 7) - 1;
436 : 13129157 : len++;
437 : : }
438 [ + + ]: 22433906 : do {
439 : 22433906 : ser_writedata8(os, tmp[len]);
440 : : } while(len--);
441 : 9304749 : }
442 : :
443 : : template<typename Stream, VarIntMode Mode, typename I>
444 : 3092457 : I ReadVarInt(Stream& is)
445 : : {
446 : : CheckVarIntMode<Mode, I>();
447 : 3092457 : I n = 0;
448 : 2418705 : while(true) {
449 : 5511162 : unsigned char chData = ser_readdata8(is);
450 [ + + ]: 5510224 : if (n > (std::numeric_limits<I>::max() >> 7)) {
451 [ + - ]: 2 : throw std::ios_base::failure("ReadVarInt(): size too large");
452 : : }
453 : 5510223 : n = (n << 7) | (chData & 0x7F);
454 [ + + ]: 5510223 : if (chData & 0x80) {
455 [ - + ]: 2418705 : if (n == std::numeric_limits<I>::max()) {
456 [ # # ]: 0 : throw std::ios_base::failure("ReadVarInt(): size too large");
457 : : }
458 : 2418705 : n++;
459 : : } else {
460 : 3091518 : 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 : 30839451 : explicit Wrapper(T obj) : m_object(obj) {}
474 : 35509977 : template<typename Stream> void Serialize(Stream &s) const { Formatter().Ser(s, m_object); }
475 : 9771884 : 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 [ + - + - : 11048188 : 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 : 9304749 : template<typename Stream, typename I> void Ser(Stream &s, I v)
501 : : {
502 : 9304749 : WriteVarInt<Stream,Mode, std::remove_cv_t<I>>(s, v);
503 : : }
504 : :
505 : 3092457 : template<typename Stream, typename I> void Unser(Stream& s, I& v)
506 : : {
507 : 6183975 : 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 : 127105 : template <typename Stream, typename I> void Ser(Stream& s, I v)
527 : : {
528 [ - + - - ]: 35476 : if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range");
529 : : if (BigEndian) {
530 : 72617 : uint64_t raw = htobe64_internal(v);
531 : 72617 : s.write(std::as_bytes(std::span{&raw, 1}).last(Bytes));
532 : : } else {
533 : 54488 : uint64_t raw = htole64_internal(v);
534 : 54488 : s.write(std::as_bytes(std::span{&raw, 1}).first(Bytes));
535 : : }
536 : 127105 : }
537 : :
538 : 52472 : 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 : 52472 : uint64_t raw = 0;
543 : : if (BigEndian) {
544 : 29075 : s.read(std::as_writable_bytes(std::span{&raw, 1}).last(Bytes));
545 : 29075 : v = static_cast<I>(be64toh_internal(raw));
546 : : } else {
547 : 23397 : s.read(std::as_writable_bytes(std::span{&raw, 1}).first(Bytes));
548 : 23397 : v = static_cast<I>(le64toh_internal(raw));
549 : : }
550 : 52472 : }
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 : 87377 : void Unser(Stream& s, I& v)
561 : : {
562 : 146919 : uint64_t n = ReadCompactSize<Stream>(s, RangeCheck);
563 [ - + ]: 23743 : 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 : 87377 : v = n;
567 : 23743 : }
568 : :
569 : : template<typename Stream, typename I>
570 : 104832 : 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 : 104832 : WriteCompactSize<Stream>(s, v);
576 : : }
577 : : };
578 : :
579 : : template <typename U, bool LOSSY = false>
580 : : struct ChronoFormatter {
581 : : template <typename Stream, typename Tp>
582 : 47959 : void Unser(Stream& s, Tp& tp)
583 : : {
584 : : U u;
585 : 47959 : s >> u;
586 : : // Lossy deserialization does not make sense, so force Wnarrowing
587 : 47959 : tp = Tp{typename Tp::duration{typename Tp::duration::rep{u}}};
588 : 47959 : }
589 : : template <typename Stream, typename Tp>
590 : 119594 : void Ser(Stream& s, Tp tp)
591 : : {
592 : : if constexpr (LOSSY) {
593 : 69322 : s << U(tp.time_since_epoch().count());
594 : : } else {
595 : 50272 : s << U{tp.time_since_epoch().count()};
596 : : }
597 : 119594 : }
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 : 119272 : explicit CompactSizeWriter(uint64_t n_in) : n(n_in) { }
608 : :
609 : : template<typename Stream>
610 : 122129 : void Serialize(Stream &s) const {
611 : 122129 : WriteCompactSize<Stream>(s, n);
612 : : }
613 : : };
614 : :
615 : : template<size_t Limit>
616 : : struct LimitedStringFormatter
617 : : {
618 : : template<typename Stream>
619 : 1615 : void Unser(Stream& s, std::string& v)
620 : : {
621 : 1615 : size_t size = ReadCompactSize(s);
622 [ - + ]: 1614 : if (size > Limit) {
623 [ # # ]: 0 : throw std::ios_base::failure("String length limit exceeded");
624 : : }
625 : 1614 : v.resize(size);
626 [ + + ]: 1614 : if (size != 0) s.read(MakeWritableByteSpan(v));
627 : 1614 : }
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 [ + + ]: 14575346 : void Ser(Stream& s, const V& v)
654 : : {
655 [ - + ]: 584 : Formatter formatter;
656 : 14882118 : WriteCompactSize(s, v.size());
657 [ + + ]: 87119854 : for (const typename V::value_type& elem : v) {
[ + + + + ]
658 : 114075470 : formatter.Ser(s, elem);
659 : : }
660 : 14575346 : }
661 : :
662 : : template<typename Stream, typename V>
663 [ + + ]: 1943658 : void Unser(Stream& s, V& v)
664 : : {
665 [ - + ]: 601 : Formatter formatter;
666 : 1943658 : v.clear();
667 : 1943658 : size_t size = ReadCompactSize(s);
668 : 1943643 : size_t allocated = 0;
669 [ + + ]: 3461756 : 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 [ + - ]: 1518307 : allocated = std::min(size, allocated + MAX_VECTOR_ALLOCATE / sizeof(typename V::value_type));
675 : 1518307 : v.reserve(allocated);
676 [ - + + + ]: 28059843 : while (v.size() < allocated) {
677 : 26541730 : v.emplace_back();
678 : 26541730 : formatter.Unser(s, v.back());
679 : : }
680 : : }
681 : 1943449 : };
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 : 232235328 : void Serialize(Stream& os, const T& a)
745 : : {
746 : 232235327 : a.Serialize(os);
747 : 232235327 : }
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 : 14093484 : void Unserialize(Stream& is, T&& a)
754 : : {
755 : 14091639 : a.Unserialize(is);
756 : 14090719 : }
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 : 30131145 : static void Ser(Stream& s, const T& t) { Serialize(s, t); }
767 : :
768 : : template<typename Stream, typename T>
769 : 3946569 : 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 [ - + ]: 670986 : void Serialize(Stream& os, const std::basic_string<C>& str)
781 : : {
782 [ + + ]: 670987 : WriteCompactSize(os, str.size());
783 [ + + ]: 670985 : if (!str.empty())
784 : 617070 : os.write(MakeByteSpan(str));
785 : 670985 : }
786 : :
787 : : template<typename Stream, typename C>
788 : 87667 : void Unserialize(Stream& is, std::basic_string<C>& str)
789 : : {
790 : 87667 : unsigned int nSize = ReadCompactSize(is);
791 : 87667 : str.resize(nSize);
792 [ + + ]: 87667 : if (nSize != 0)
793 : 77433 : is.read(MakeWritableByteSpan(str));
794 : 87667 : }
795 : :
796 : :
797 : :
798 : : /**
799 : : * prevector
800 : : */
801 : : template <typename Stream, unsigned int N, typename T>
802 [ + + ]: 30377988 : void Serialize(Stream& os, const prevector<N, T>& v)
803 : : {
804 : : if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
805 [ + + ]: 30110501 : WriteCompactSize(os, v.size());
806 [ + + ]: 30110208 : if (!v.empty()) os.write(MakeByteSpan(v));
807 : : } else {
808 : 267780 : Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
809 : : }
810 : 30377988 : }
811 : :
812 : :
813 : : template <typename Stream, unsigned int N, typename T>
814 : 1298772 : 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 : 1298772 : v.clear();
819 : 1298772 : unsigned int nSize = ReadCompactSize(is);
820 : 1298769 : unsigned int i = 0;
821 [ + + ]: 2385875 : while (i < nSize) {
822 [ + - ]: 1087115 : unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
823 [ + + ]: 1087115 : v.resize_uninitialized(i + blk);
824 : 1087115 : is.read(std::as_writable_bytes(std::span{&v[i], blk}));
825 : 1087115 : i += blk;
826 : : }
827 : : } else {
828 : : Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
829 : : }
830 : 1298760 : }
831 : :
832 : :
833 : : /**
834 : : * vector
835 : : */
836 : : template <typename Stream, typename T, typename A>
837 [ - + ]: 23619786 : void Serialize(Stream& os, const std::vector<T, A>& v)
838 : : {
839 : : if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
840 : 10433686 : WriteCompactSize(os, v.size());
841 [ + + ]: 9853594 : 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 : 13945826 : Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
852 : : }
853 : 23619786 : }
854 : :
855 : :
856 : : template <typename Stream, typename T, typename A>
857 [ + + ]: 3872975 : 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 : 2070342 : v.clear();
862 : 2070342 : unsigned int nSize = ReadCompactSize(is);
863 : 2070337 : unsigned int i = 0;
864 [ + + ]: 2724924 : while (i < nSize) {
865 [ + - ]: 654587 : unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
866 : 654587 : v.resize(i + blk);
867 : 654587 : is.read(std::as_writable_bytes(std::span{&v[i], blk}));
868 : 654587 : i += blk;
869 : : }
870 : : } else {
871 : 1802633 : Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
872 : : }
873 : 3872762 : }
874 : :
875 : :
876 : : /**
877 : : * pair
878 : : */
879 : : template<typename Stream, typename K, typename T>
880 : 527936 : void Serialize(Stream& os, const std::pair<K, T>& item)
881 : : {
882 : 527936 : Serialize(os, item.first);
883 : 527936 : Serialize(os, item.second);
884 : 527936 : }
885 : :
886 : : template<typename Stream, typename K, typename T>
887 : 209525 : void Unserialize(Stream& is, std::pair<K, T>& item)
888 : : {
889 : 209525 : Unserialize(is, item.first);
890 : 209525 : Unserialize(is, item.second);
891 : 208752 : }
892 : :
893 : :
894 : :
895 : : /**
896 : : * map
897 : : */
898 : : template<typename Stream, typename K, typename T, typename Pred, typename A>
899 : 25974 : void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
900 : : {
901 : 25974 : WriteCompactSize(os, m.size());
902 [ + + ]: 101764 : for (const auto& entry : m)
903 : 75790 : Serialize(os, entry);
904 : 25974 : }
905 : :
906 : : template<typename Stream, typename K, typename T, typename Pred, typename A>
907 : 8876 : void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
908 : : {
909 : 8876 : m.clear();
910 : 8876 : unsigned int nSize = ReadCompactSize(is);
911 : 8876 : typename std::map<K, T, Pred, A>::iterator mi = m.begin();
912 [ + + ]: 34227 : for (unsigned int i = 0; i < nSize; i++)
913 : : {
914 : 25351 : std::pair<K, T> item;
915 [ + - ]: 25351 : Unserialize(is, item);
916 [ + - ]: 25351 : mi = m.insert(mi, item);
917 : : }
918 : 8876 : }
919 : :
920 : :
921 : :
922 : : /**
923 : : * set
924 : : */
925 : : template<typename Stream, typename K, typename Pred, typename A>
926 : 4781 : void Serialize(Stream& os, const std::set<K, Pred, A>& m)
927 : : {
928 : 4781 : WriteCompactSize(os, m.size());
929 [ + + ]: 7994 : for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
930 : 3213 : Serialize(os, (*it));
931 : 4781 : }
932 : :
933 : : template<typename Stream, typename K, typename Pred, typename A>
934 : 7250 : void Unserialize(Stream& is, std::set<K, Pred, A>& m)
935 : : {
936 : 7250 : m.clear();
937 : 7250 : unsigned int nSize = ReadCompactSize(is);
938 : 7249 : typename std::set<K, Pred, A>::iterator it = m.begin();
939 [ + + ]: 12139 : for (unsigned int i = 0; i < nSize; i++)
940 : : {
941 : 4890 : K key;
942 : 4890 : Unserialize(is, key);
943 : 4890 : it = m.insert(it, key);
944 : : }
945 : 7249 : }
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 : 1422864 : Serialize(Stream& os, const std::shared_ptr<const T>& p)
971 : : {
972 : 1422864 : Serialize(os, *p);
973 : 1422864 : }
974 : :
975 : : template<typename Stream, typename T>
976 : 335594 : void Unserialize(Stream& is, std::shared_ptr<const T>& p)
977 : : {
978 [ - + ]: 335588 : p = std::make_shared<const T>(deserialize, is);
979 : 335588 : }
980 : :
981 : : /**
982 : : * Support for (un)serializing many things at once
983 : : */
984 : :
985 : : template <typename Stream, typename... Args>
986 : 119921879 : void SerializeMany(Stream& s, const Args&... args)
987 : : {
988 : 119921879 : (::Serialize(s, args), ...);
989 : 119921879 : }
990 : :
991 : : template <typename Stream, typename... Args>
992 : 7194654 : inline void UnserializeMany(Stream& s, Args&&... args)
993 : : {
994 : 3443080 : (::Unserialize(s, args), ...);
995 : 1316209 : }
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 : 119566754 : static void SerReadWriteMany(Stream& s, const Args&... args)
1005 : : {
1006 [ + - + - : 119262482 : ::SerializeMany(s, args...);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + - ]
1007 : 2084496 : }
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 : 163131 : static void SerWrite(Stream& s, Type&& obj, Fn&& fn)
1016 : : {
1017 [ + - ]: 163131 : fn(s, std::forward<Type>(obj));
1018 : 100936 : }
1019 : : };
1020 : : struct ActionUnserialize {
1021 : : static constexpr bool ForRead() { return true; }
1022 : :
1023 : : template<typename Stream, typename... Args>
1024 [ + - + - : 5798971 : static void SerReadWriteMany(Stream& s, Args&&... args)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + - ]
1025 : : {
1026 [ + - + - : 3760929 : ::UnserializeMany(s, args...);
+ - + + +
+ + + + -
+ - + - +
- + - + -
+ - ][ + -
+ + - - -
- - - - -
- - - - ]
[ + + + + ]
[ + + + +
+ + + + -
- - - - -
- - ]
1027 : 2263903 : }
1028 : :
1029 : : template<typename Stream, typename Type, typename Fn>
1030 [ + + ]: 24727 : static void SerRead(Stream& s, Type&& obj, Fn&& fn)
1031 : : {
1032 [ + - ]: 24912 : fn(s, std::forward<Type>(obj));
1033 : 24294 : }
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 : 51779811 : void write(std::span<const std::byte> src)
1061 : : {
1062 : 51167461 : m_size += src.size();
1063 : 529134 : }
1064 : :
1065 : : /** Pretend this many bytes are written, without specifying them. */
1066 : 1417546 : void seek(uint64_t num)
1067 : : {
1068 : 1417546 : m_size += num;
1069 : : }
1070 : :
1071 : : template <typename T>
1072 : 2806786 : SizeComputer& operator<<(const T& obj)
1073 : : {
1074 : 1829443 : ::Serialize(*this, obj);
1075 : : return *this;
1076 : : }
1077 : :
1078 : 2544553 : uint64_t size() const
1079 : : {
1080 : 2544553 : 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 : 1066793 : inline void WriteCompactSize(SizeComputer &s, uint64_t nSize)
1091 : : {
1092 [ + + + + ]: 1419695 : s.seek(GetSizeOfCompactSize(nSize));
[ - + + +
+ + + + +
+ ][ + + +
+ - + ]
1093 : : }
1094 : :
1095 : : template <typename T>
1096 : 2529289 : uint64_t GetSerializeSize(const T& t)
1097 : : {
1098 : 2529289 : 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 [ + - + - : 4664541 : 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 [ + - + - ]: 25684807 : template <typename U> ParamsStream& operator<<(const U& obj) { ::Serialize(*this, obj); return *this; }
[ + - + -
+ - + - ]
[ + - - -
- - + - +
- + - + -
- - - - +
- + - +
- ][ # # #
# # # # #
# # ][ - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - -
- + - - -
+ - ]
1127 [ + - - - : 2946299 : template <typename U> ParamsStream& operator>>(U&& obj) { ::Unserialize(*this, obj); return *this; }
- - + - +
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- ][ + - +
- + - + -
+ - + + -
- + - + -
+ - + - +
- + - + -
+ - + + +
- + - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - ][ # # ]
1128 : 126593289 : void write(std::span<const std::byte> src) { GetStream().write(src); }
1129 : 12129950 : void read(std::span<std::byte> dst) { GetStream().read(dst); }
1130 : 4 : void ignore(size_t num) { GetStream().ignore(num); }
1131 : 3 : 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 : 5234384 : const auto& GetParams() const
1137 : : {
1138 : : if constexpr (std::is_convertible_v<Params, P>) {
1139 [ + + ][ + + : 5234382 : return m_params;
- + - + +
+ - - ][ -
- + + + +
+ + + - +
- + - + -
- - - - -
- - - - -
- - + - -
- - - +
- ][ + + +
+ - + - +
+ + - + -
+ # # ][ +
+ + + + +
+ - # # ]
[ - - + - ]
[ + + + +
- - - - -
- - - + -
+ - - - -
- - - - -
+ + - - -
- - - - -
- - ][ - +
- - - + -
- - - - -
- + ]
1140 : : } else {
1141 : 2 : return m_substream.template GetParams<P>();
1142 : : }
1143 : : }
1144 : :
1145 : : //! Get reference to underlying stream.
1146 : 106012067 : auto& GetStream()
1147 : : {
1148 : : if constexpr (ContainsStream<SubStream>) {
1149 [ + - + - : 337244 : return m_substream.GetStream();
+ - ]
1150 : : } else {
1151 : 64076263 : return m_substream;
1152 : : }
1153 : : }
1154 : : const auto& GetStream() const
1155 : : {
1156 : : if constexpr (ContainsStream<SubStream>) {
1157 : : return m_substream.GetStream();
1158 : : } else {
1159 [ - + ]: 3 : 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 [ + - + + : 4165538 : explicit ParamsWrapper(const Params& params, T& obj) : m_params{params}, m_object{obj} {}
+ - ]
[ + + + - ]
[ + - # #
# # ][ + -
+ - + - +
+ + + + +
+ + + - +
+ + + + +
+ - + - +
+ + - + +
+ - + - -
+ - + + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + -
+ + + + +
+ + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ][ +
- + - + +
+ + # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - - + -
- - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- + - ]
1189 : :
1190 : : template <typename Stream>
1191 : 4339946 : void Serialize(Stream& s) const
1192 : : {
1193 : 4339946 : ParamsStream ss{s, m_params};
1194 : 4339946 : ::Serialize(ss, m_object);
1195 : : }
1196 : : template <typename Stream>
1197 : 322452 : void Unserialize(Stream& s)
1198 : : {
1199 : 322452 : ParamsStream ss{s, m_params};
1200 : 322452 : ::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
|