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 : 42538280 : template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
57 : : {
58 : 42538280 : s.write(std::as_bytes(std::span{&obj, 1}));
59 : 42538276 : }
60 : 4186 : template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
61 : : {
62 : 4186 : obj = htole16_internal(obj);
63 : 4186 : s.write(std::as_bytes(std::span{&obj, 1}));
64 : 4186 : }
65 : 39358197 : template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
66 : : {
67 : 39358197 : obj = htole32_internal(obj);
68 : 39358197 : s.write(std::as_bytes(std::span{&obj, 1}));
69 : 39358197 : }
70 : 1191 : template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
71 : : {
72 : 1191 : obj = htobe32_internal(obj);
73 : 1191 : s.write(std::as_bytes(std::span{&obj, 1}));
74 : 1191 : }
75 : 15736091 : template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
76 : : {
77 : 15736091 : obj = htole64_internal(obj);
78 : 15736091 : s.write(std::as_bytes(std::span{&obj, 1}));
79 : 15736091 : }
80 : 1866074 : template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
81 : : {
82 : : uint8_t obj;
83 : 1866074 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
84 : 1865113 : return obj;
85 : : }
86 : 115 : template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
87 : : {
88 : : uint16_t obj;
89 : 115 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
90 : 113 : return le16toh_internal(obj);
91 : : }
92 : 81881 : template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
93 : : {
94 : : uint32_t obj;
95 : 81881 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
96 : 81855 : return le32toh_internal(obj);
97 : : }
98 : 443 : template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
99 : : {
100 : : uint32_t obj;
101 : 443 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
102 : 443 : return be32toh_internal(obj);
103 : : }
104 : 20805 : template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
105 : : {
106 : : uint64_t obj;
107 : 14572 : s.read(std::as_writable_bytes(std::span{&obj, 1}));
108 : 20805 : 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 : 37986 : Out& AsBase(In& x)
136 : : {
137 : : static_assert(std::is_base_of_v<Out, In>);
138 : 37986 : return x;
139 : : }
140 : : template <class Out, class In>
141 : 16939665 : const Out& AsBase(const In& x)
142 : : {
143 : : static_assert(std::is_base_of_v<Out, In>);
144 : 16939665 : 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 : 2 : template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); }
248 : 2 : template <typename Stream> void Serialize(Stream& s, int8_t a) { ser_writedata8(s, uint8_t(a)); }
249 : 5347144 : template <typename Stream> void Serialize(Stream& s, uint8_t a) { ser_writedata8(s, a); }
250 : 2 : template <typename Stream> void Serialize(Stream& s, int16_t a) { ser_writedata16(s, uint16_t(a)); }
251 : 35 : template <typename Stream> void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
252 : 4928886 : template <typename Stream> void Serialize(Stream& s, int32_t a) { ser_writedata32(s, uint32_t(a)); }
253 : 34429252 : template <typename Stream> void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
254 : 15667316 : template <typename Stream> void Serialize(Stream& s, int64_t a) { ser_writedata64(s, uint64_t(a)); }
255 : 68769 : template <typename Stream> void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
256 : :
257 : 354 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, const B (&a)[N]) { s.write(MakeByteSpan(a)); }
258 : 17204 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { s.write(MakeByteSpan(a)); }
259 : 27880708 : template <typename Stream, BasicByte B, size_t N> void Serialize(Stream& s, std::span<B, N> span) { s.write(std::as_bytes(span)); }
260 : 92209 : 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 : 17 : template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte(ser_readdata8(s)); }
264 : 1 : template <typename Stream> void Unserialize(Stream& s, int8_t& a) { a = int8_t(ser_readdata8(s)); }
265 : 47207 : template <typename Stream> void Unserialize(Stream& s, uint8_t& a) { a = ser_readdata8(s); }
266 : : template <typename Stream> void Unserialize(Stream& s, int16_t& a) { a = int16_t(ser_readdata16(s)); }
267 : 36 : template <typename Stream> void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
268 : 14592 : template <typename Stream> void Unserialize(Stream& s, int32_t& a) { a = int32_t(ser_readdata32(s)); }
269 : 67267 : template <typename Stream> void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
270 : 19429 : template <typename Stream> void Unserialize(Stream& s, int64_t& a) { a = int64_t(ser_readdata64(s)); }
271 : 1374 : template <typename Stream> void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
272 : :
273 : 1966 : template <typename Stream, BasicByte B, size_t N> void Unserialize(Stream& s, B (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
274 : 5315 : template <typename Stream, BasicByte B, size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { s.read(MakeWritableByteSpan(a)); }
275 : 1 : template <typename Stream, BasicByte B, size_t N> void Unserialize(Stream& s, std::span<B, N> span) { s.read(std::as_writable_bytes(span)); }
276 : 88065 : template <typename Stream, BasicByte B> void Unserialize(Stream& s, std::span<B> span) { s.read(std::as_writable_bytes(span)); }
277 : :
278 : 895 : template <typename Stream> void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
279 : 12 : 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 : 29395 : constexpr inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
291 : : {
292 [ + + ][ - + : 16990 : if (nSize < 253) return sizeof(unsigned char);
- + - + -
+ - + ]
293 [ # # # # ]: 91 : else if (nSize <= std::numeric_limits<uint16_t>::max()) return sizeof(unsigned char) + sizeof(uint16_t);
[ + + ][ - +
- - - - ]
[ # # # #
# # # # #
# # # ]
294 [ # # # # ]: 2 : 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 : 21564194 : void WriteCompactSize(Stream& os, uint64_t nSize)
302 : : {
303 [ + + ]: 21564194 : if (nSize < 253)
304 : : {
305 : 21559992 : ser_writedata8(os, nSize);
306 : : }
307 [ + + ]: 4202 : else if (nSize <= std::numeric_limits<uint16_t>::max())
308 : : {
309 : 4149 : ser_writedata8(os, 253);
310 : 4149 : ser_writedata16(os, nSize);
311 : : }
312 [ + - ]: 53 : else if (nSize <= std::numeric_limits<unsigned int>::max())
313 : : {
314 : 53 : ser_writedata8(os, 254);
315 : 53 : ser_writedata32(os, nSize);
316 : : }
317 : : else
318 : : {
319 : 0 : ser_writedata8(os, 255);
320 : 0 : ser_writedata64(os, nSize);
321 : : }
322 : 21564193 : 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 : 91164 : uint64_t ReadCompactSize(Stream& is, bool range_check = true)
333 : : {
334 : 91164 : uint8_t chSize = ser_readdata8(is);
335 : 91157 : uint64_t nSizeRet = 0;
336 [ + + ]: 91157 : if (chSize < 253)
337 : : {
338 : 91054 : nSizeRet = chSize;
339 : : }
340 [ + + ]: 103 : else if (chSize == 253)
341 : : {
342 : 79 : nSizeRet = ser_readdata16(is);
343 [ + + ]: 79 : if (nSizeRet < 253)
344 [ + - ]: 4 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
345 : : }
346 [ + + ]: 24 : else if (chSize == 254)
347 : : {
348 : 22 : nSizeRet = ser_readdata32(is);
349 [ + + ]: 22 : if (nSizeRet < 0x10000u)
350 [ + - ]: 4 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
351 : : }
352 : : else
353 : : {
354 : 2 : nSizeRet = ser_readdata64(is);
355 [ + - ]: 2 : if (nSizeRet < 0x100000000ULL)
356 [ + - ]: 4 : throw std::ios_base::failure("non-canonical ReadCompactSize()");
357 : : }
358 [ - + ]: 91151 : if (range_check && nSizeRet > MAX_SIZE) {
359 [ # # ]: 0 : throw std::ios_base::failure("ReadCompactSize(): size too large");
360 : : }
361 : 91151 : 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 : 5798494 : void WriteVarInt(Stream& os, I n)
428 : : {
429 : : CheckVarIntMode<Mode, I>();
430 : : unsigned char tmp[CeilDiv(sizeof(n) * 8, 7u)];
431 : 5798494 : int len=0;
432 : 9826358 : while(true) {
433 [ + + ]: 15624852 : tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
434 [ + + ]: 15624851 : if (n <= 0x7F)
435 : : break;
436 : 9826358 : n = (n >> 7) - 1;
437 : 9826358 : len++;
438 : : }
439 [ + + ]: 15624852 : do {
440 : 15624852 : ser_writedata8(os, tmp[len]);
441 : : } while(len--);
442 : 5798494 : }
443 : :
444 : : template<typename Stream, VarIntMode Mode, typename I>
445 : 835907 : I ReadVarInt(Stream& is)
446 : : {
447 : : CheckVarIntMode<Mode, I>();
448 : 835907 : I n = 0;
449 : 891323 : while(true) {
450 : 1727230 : unsigned char chData = ser_readdata8(is);
451 [ - + ]: 1726292 : if (n > (std::numeric_limits<I>::max() >> 7)) {
452 [ # # ]: 0 : throw std::ios_base::failure("ReadVarInt(): size too large");
453 : : }
454 : 1726292 : n = (n << 7) | (chData & 0x7F);
455 [ + + ]: 1726292 : if (chData & 0x80) {
456 [ - + ]: 891323 : if (n == std::numeric_limits<I>::max()) {
457 [ # # ]: 0 : throw std::ios_base::failure("ReadVarInt(): size too large");
458 : : }
459 : 891323 : n++;
460 : : } else {
461 : 834969 : 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 : 11192511 : explicit Wrapper(T obj) : m_object(obj) {}
475 : 16110518 : template<typename Stream> void Serialize(Stream &s) const { Formatter().Ser(s, m_object); }
476 : 2063831 : 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 [ + - + - : 5554881 : 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 : 5798494 : template<typename Stream, typename I> void Ser(Stream &s, I v)
502 : : {
503 : 5798494 : WriteVarInt<Stream,Mode, std::remove_cv_t<I>>(s, v);
504 : : }
505 : :
506 : 835907 : template<typename Stream, typename I> void Unser(Stream& s, I& v)
507 : : {
508 : 1670876 : 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 : 75 : template <typename Stream, typename I> void Ser(Stream& s, I v)
528 : : {
529 [ - + - - ]: 32 : if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range");
530 : : if (BigEndian) {
531 : 35 : uint64_t raw = htobe64_internal(v);
532 : 35 : s.write(std::as_bytes(std::span{&raw, 1}).last(Bytes));
533 : : } else {
534 : 40 : uint64_t raw = htole64_internal(v);
535 : 40 : s.write(std::as_bytes(std::span{&raw, 1}).first(Bytes));
536 : : }
537 : 75 : }
538 : :
539 : 59 : 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 : 59 : uint64_t raw = 0;
544 : : if (BigEndian) {
545 : 27 : s.read(std::as_writable_bytes(std::span{&raw, 1}).last(Bytes));
546 : 27 : v = static_cast<I>(be64toh_internal(raw));
547 : : } else {
548 : 32 : s.read(std::as_writable_bytes(std::span{&raw, 1}).first(Bytes));
549 : 32 : v = static_cast<I>(le64toh_internal(raw));
550 : : }
551 : 59 : }
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 : 82 : void Unser(Stream& s, I& v)
562 : : {
563 : 153 : uint64_t n = ReadCompactSize<Stream>(s, RangeCheck);
564 [ - + ]: 11 : if (n < std::numeric_limits<I>::min() || n > std::numeric_limits<I>::max()) {
565 [ # # ]: 0 : throw std::ios_base::failure("CompactSize exceeds limit of type");
566 : : }
567 : 82 : v = n;
568 : 11 : }
569 : :
570 : : template<typename Stream, typename I>
571 : 44 : 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 : 44 : WriteCompactSize<Stream>(s, v);
577 : : }
578 : : };
579 : :
580 : : template <typename U, bool LOSSY = false>
581 : : struct ChronoFormatter {
582 : : template <typename Stream, typename Tp>
583 : 41 : void Unser(Stream& s, Tp& tp)
584 : : {
585 : : U u;
586 : 41 : s >> u;
587 : : // Lossy deserialization does not make sense, so force Wnarrowing
588 : 41 : tp = Tp{typename Tp::duration{typename Tp::duration::rep{u}}};
589 : 41 : }
590 : : template <typename Stream, typename Tp>
591 : 41 : void Ser(Stream& s, Tp tp)
592 : : {
593 : : if constexpr (LOSSY) {
594 : 24 : s << U(tp.time_since_epoch().count());
595 : : } else {
596 : 17 : s << U{tp.time_since_epoch().count()};
597 : : }
598 : 41 : }
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 [ + - + - ]: 20 : explicit CompactSizeReader(uint64_t& n_in) : n(n_in) {}
609 : :
610 : : template<typename Stream>
611 : 20 : void Unserialize(Stream &s) const {
612 : 40 : n = ReadCompactSize<Stream>(s);
613 : : }
614 : : };
615 : :
616 : : class CompactSizeWriter
617 : : {
618 : : protected:
619 : : uint64_t n;
620 : : public:
621 : 4430 : explicit CompactSizeWriter(uint64_t n_in) : n(n_in) { }
622 : :
623 : : template<typename Stream>
624 : 4445 : void Serialize(Stream &s) const {
625 : 4445 : WriteCompactSize<Stream>(s, n);
626 : : }
627 : : };
628 : :
629 : : template<size_t Limit>
630 : : struct LimitedStringFormatter
631 : : {
632 : : template<typename Stream>
633 : 5 : void Unser(Stream& s, std::string& v)
634 : : {
635 : 5 : size_t size = ReadCompactSize(s);
636 [ - + ]: 4 : if (size > Limit) {
637 [ # # ]: 0 : throw std::ios_base::failure("String length limit exceeded");
638 : : }
639 : 4 : v.resize(size);
640 [ + + ]: 4 : if (size != 0) s.read(MakeWritableByteSpan(v));
641 : 4 : }
642 : :
643 : : template<typename Stream>
644 : 3 : void Ser(Stream& s, const std::string& v)
645 : : {
646 : 2 : 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 [ + + ]: 4170144 : void Ser(Stream& s, const V& v)
668 : : {
669 [ - + ]: 2 : Formatter formatter;
670 : 4182002 : WriteCompactSize(s, v.size());
671 [ + + ]: 11973387 : for (const typename V::value_type& elem : v) {
[ + + + + ]
672 : 7573824 : formatter.Ser(s, elem);
673 : : }
674 : 4170144 : }
675 : :
676 : : template<typename Stream, typename V>
677 [ + + ]: 37969 : void Unser(Stream& s, V& v)
678 : : {
679 [ - + ]: 3 : Formatter formatter;
680 : 37969 : v.clear();
681 : 37969 : size_t size = ReadCompactSize(s);
682 : 37963 : size_t allocated = 0;
683 [ + + ]: 68831 : 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 [ + - ]: 30870 : allocated = std::min(size, allocated + MAX_VECTOR_ALLOCATE / sizeof(typename V::value_type));
689 : 30870 : v.reserve(allocated);
690 [ - + + + ]: 122762 : while (v.size() < allocated) {
691 : 91894 : v.emplace_back();
692 : 91894 : formatter.Unser(s, v.back());
693 : : }
694 : : }
695 : 37961 : };
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 : 102461875 : void Serialize(Stream& os, const T& a)
759 : : {
760 : 102461874 : a.Serialize(os);
761 : 102461874 : }
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 : 1420003 : void Unserialize(Stream& is, T&& a)
768 : : {
769 : 1419014 : a.Unserialize(is);
770 : 1418992 : }
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 : 7496178 : static void Ser(Stream& s, const T& t) { Serialize(s, t); }
781 : :
782 : : template<typename Stream, typename T>
783 : 91878 : 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 [ - + ]: 70610 : void Serialize(Stream& os, const std::basic_string<C>& str)
795 : : {
796 [ + + ]: 70611 : WriteCompactSize(os, str.size());
797 [ + + ]: 70609 : if (!str.empty())
798 : 63997 : os.write(MakeByteSpan(str));
799 : 70609 : }
800 : :
801 : : template<typename Stream, typename C>
802 : 306 : void Unserialize(Stream& is, std::basic_string<C>& str)
803 : : {
804 : 306 : unsigned int nSize = ReadCompactSize(is);
805 : 306 : str.resize(nSize);
806 [ + + ]: 306 : if (nSize != 0)
807 : 278 : is.read(MakeWritableByteSpan(str));
808 : 306 : }
809 : :
810 : :
811 : :
812 : : /**
813 : : * prevector
814 : : */
815 : : template <typename Stream, unsigned int N, typename T>
816 [ + + ]: 17115842 : void Serialize(Stream& os, const prevector<N, T>& v)
817 : : {
818 : : if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
819 [ + + ]: 16847846 : WriteCompactSize(os, v.size());
820 [ + + ]: 16847792 : if (!v.empty()) os.write(MakeByteSpan(v));
821 : : } else {
822 : 268050 : Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
823 : : }
824 : 17115842 : }
825 : :
826 : :
827 : : template <typename Stream, unsigned int N, typename T>
828 : 31935 : 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 : 31935 : v.clear();
833 : 31935 : unsigned int nSize = ReadCompactSize(is);
834 : 31935 : unsigned int i = 0;
835 [ + + ]: 58495 : while (i < nSize) {
836 [ + - ]: 26560 : unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
837 [ + + ]: 26560 : v.resize_uninitialized(i + blk);
838 : 26560 : is.read(std::as_writable_bytes(std::span{&v[i], blk}));
839 : 26560 : i += blk;
840 : : }
841 : : } else {
842 : : Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
843 : : }
844 : 31935 : }
845 : :
846 : :
847 : : /**
848 : : * vector
849 : : */
850 : : template <typename Stream, typename T, typename A>
851 [ - + ]: 4103902 : void Serialize(Stream& os, const std::vector<T, A>& v)
852 : : {
853 : : if constexpr (BasicByte<T>) { // Use optimized version for unformatted basic bytes
854 : 211211 : WriteCompactSize(os, v.size());
855 [ + + ]: 208344 : 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 : 1 : WriteCompactSize(os, v.size());
861 [ - + - + ]: 29 : for (bool elem : v) {
862 : 27 : ::Serialize(os, elem);
863 : : }
864 : : } else {
865 : 3901692 : Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
866 : : }
867 : 4103902 : }
868 : :
869 : :
870 : : template <typename Stream, typename T, typename A>
871 [ + + ]: 54167 : 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 : 16214 : v.clear();
876 : 16214 : unsigned int nSize = ReadCompactSize(is);
877 : 16214 : unsigned int i = 0;
878 [ + + ]: 32368 : while (i < nSize) {
879 [ + - ]: 16154 : unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
880 : 16154 : v.resize(i + blk);
881 : 16154 : is.read(std::as_writable_bytes(std::span{&v[i], blk}));
882 : 16154 : i += blk;
883 : : }
884 : : } else {
885 : 37953 : Unserialize(is, Using<VectorFormatter<DefaultFormatter>>(v));
886 : : }
887 : 54160 : }
888 : :
889 : :
890 : : /**
891 : : * pair
892 : : */
893 : : template<typename Stream, typename K, typename T>
894 : 36418 : void Serialize(Stream& os, const std::pair<K, T>& item)
895 : : {
896 : 36418 : Serialize(os, item.first);
897 : 36418 : Serialize(os, item.second);
898 : 36418 : }
899 : :
900 : : template<typename Stream, typename K, typename T>
901 : 1957 : void Unserialize(Stream& is, std::pair<K, T>& item)
902 : : {
903 : 1957 : Unserialize(is, item.first);
904 : 1957 : Unserialize(is, item.second);
905 : 1948 : }
906 : :
907 : :
908 : :
909 : : /**
910 : : * map
911 : : */
912 : : template<typename Stream, typename K, typename T, typename Pred, typename A>
913 : 427 : void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
914 : : {
915 : 427 : WriteCompactSize(os, m.size());
916 [ + + ]: 1705 : for (const auto& entry : m)
917 : 1278 : Serialize(os, entry);
918 : 427 : }
919 : :
920 : : template<typename Stream, typename K, typename T, typename Pred, typename A>
921 : 2 : void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
922 : : {
923 : 2 : m.clear();
924 : 2 : unsigned int nSize = ReadCompactSize(is);
925 : 2 : typename std::map<K, T, Pred, A>::iterator mi = m.begin();
926 [ + + ]: 8 : for (unsigned int i = 0; i < nSize; i++)
927 : : {
928 : 6 : std::pair<K, T> item;
929 [ + - ]: 6 : Unserialize(is, item);
930 [ + - ]: 6 : mi = m.insert(mi, item);
931 : : }
932 : 2 : }
933 : :
934 : :
935 : :
936 : : /**
937 : : * set
938 : : */
939 : : template<typename Stream, typename K, typename Pred, typename A>
940 : 1 : void Serialize(Stream& os, const std::set<K, Pred, A>& m)
941 : : {
942 : 1 : WriteCompactSize(os, m.size());
943 [ - + ]: 1 : for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
944 : 0 : Serialize(os, (*it));
945 : 1 : }
946 : :
947 : : template<typename Stream, typename K, typename Pred, typename A>
948 : 0 : void Unserialize(Stream& is, std::set<K, Pred, A>& m)
949 : : {
950 : 0 : m.clear();
951 : 0 : unsigned int nSize = ReadCompactSize(is);
952 : 0 : typename std::set<K, Pred, A>::iterator it = m.begin();
953 [ # # ]: 0 : for (unsigned int i = 0; i < nSize; i++)
954 : : {
955 : 0 : K key;
956 : 0 : Unserialize(is, key);
957 : 0 : it = m.insert(it, key);
958 : : }
959 : 0 : }
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 : 93634 : Serialize(Stream& os, const std::shared_ptr<const T>& p)
985 : : {
986 : 93634 : Serialize(os, *p);
987 : 93634 : }
988 : :
989 : : template<typename Stream, typename T>
990 : 6848 : void Unserialize(Stream& is, std::shared_ptr<const T>& p)
991 : : {
992 [ - + ]: 6848 : p = std::make_shared<const T>(deserialize, is);
993 : 6848 : }
994 : :
995 : : /**
996 : : * Support for (un)serializing many things at once
997 : : */
998 : :
999 : : template <typename Stream, typename... Args>
1000 : 60941880 : void SerializeMany(Stream& s, const Args&... args)
1001 : : {
1002 : 60941880 : (::Serialize(s, args), ...);
1003 : 60941879 : }
1004 : :
1005 : : template <typename Stream, typename... Args>
1006 : 204880 : inline void UnserializeMany(Stream& s, Args&&... args)
1007 : : {
1008 : 156066 : (::Unserialize(s, args), ...);
1009 : 28694 : }
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 : 60941726 : static void SerReadWriteMany(Stream& s, const Args&... args)
1019 : : {
1020 [ + - + - : 60939197 : ::SerializeMany(s, args...);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + - ]
1021 : 42899 : }
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 : 13659 : static void SerWrite(Stream& s, Type&& obj, Fn&& fn)
1030 : : {
1031 [ + - ]: 13659 : fn(s, std::forward<Type>(obj));
1032 : 13437 : }
1033 : : };
1034 : : struct ActionUnserialize {
1035 : : static constexpr bool ForRead() { return true; }
1036 : :
1037 : : template<typename Stream, typename... Args>
1038 [ + - + - : 175855 : static void SerReadWriteMany(Stream& s, Args&&... args)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + - ]
1039 : : {
1040 [ + + + + : 123481 : ::UnserializeMany(s, args...);
+ + + + -
- - - - -
- - # # #
# # # # #
# # ][ + -
+ - + - +
+ + + + +
+ - + - +
- + - + -
+ - + - ]
[ + - + +
# # # # #
# # # # #
# # ][ + + ]
1041 : 15665 : }
1042 : :
1043 : : template<typename Stream, typename Type, typename Fn>
1044 [ + + ]: 211 : static void SerRead(Stream& s, Type&& obj, Fn&& fn)
1045 : : {
1046 [ + - ]: 377 : fn(s, std::forward<Type>(obj));
1047 : 205 : }
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 : 5664587 : void write(std::span<const std::byte> src)
1075 : : {
1076 : 5664580 : m_size += src.size();
1077 : 6773 : }
1078 : :
1079 : : /** Pretend this many bytes are written, without specifying them. */
1080 : 21656 : void seek(uint64_t num)
1081 : : {
1082 : 21656 : m_size += num;
1083 : : }
1084 : :
1085 : : template <typename T>
1086 : 437288 : SizeComputer& operator<<(const T& obj)
1087 : : {
1088 : 426994 : ::Serialize(*this, obj);
1089 : : return *this;
1090 : : }
1091 : :
1092 : 436971 : uint64_t size() const
1093 : : {
1094 : 436971 : 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 : 20915 : inline void WriteCompactSize(SizeComputer &s, uint64_t nSize)
1105 : : {
1106 [ - + + + ]: 21734 : s.seek(GetSizeOfCompactSize(nSize));
[ - + + +
- - - - -
- ][ + + +
+ - + ]
1107 : : }
1108 : :
1109 : : template <typename T>
1110 : 436954 : uint64_t GetSerializeSize(const T& t)
1111 : : {
1112 : 436954 : 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 : 1 : 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 [ + - + - : 1677340 : 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 : 3 : ParamsStream(NestedSubstream&& s, const Params1& params1 LIFETIMEBOUND, const Params2& params2 LIFETIMEBOUND, const NestedParams&... params LIFETIMEBOUND)
1138 [ + - ]: 3 : : ParamsStream{::ParamsStream{std::forward<NestedSubstream>(s), params2, params...}, params1} {}
1139 : :
1140 [ + - + - ]: 6452603 : template <typename U> ParamsStream& operator<<(const U& obj) { ::Serialize(*this, obj); return *this; }
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
- - - - +
- - - +
- ][ + - +
- - - -
- ][ # # #
# # # # #
# # ][ # #
# # # # ]
[ + - - -
- - + - +
- + - + -
- - - - +
- + - +
- ]
1141 [ + - + - : 46935 : template <typename U> ParamsStream& operator>>(U&& obj) { ::Unserialize(*this, obj); return *this; }
+ - + - +
- + + - -
+ - + - +
- + - + -
+ - + - +
- + + + -
+ - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
- - - - +
- + - +
- ]
1142 : 29497546 : void write(std::span<const std::byte> src) { GetStream().write(src); }
1143 : 188364 : void read(std::span<std::byte> dst) { GetStream().read(dst); }
1144 : 3 : 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 : 1679179 : const auto& GetParams() const
1151 : : {
1152 : : if constexpr (std::is_convertible_v<Params, P>) {
1153 [ + + ][ + + : 1679177 : return m_params;
+ + - - -
- - - - -
+ - + - -
- - - - -
- - + + -
- - - - -
- - - - ]
[ # # # #
# # # # #
# # # #
# ][ - + -
- - + - -
- - - - -
+ # # # #
# # # # #
# # # # #
# # # # #
# # # ]
[ - - + - ]
[ + + - +
- + + + -
- ][ - - +
+ + + + +
+ - + - +
- + - ][ +
+ + + + +
# # # # #
# # # #
# ]
1154 : : } else {
1155 : 2 : return m_substream.template GetParams<P>();
1156 : : }
1157 : : }
1158 : :
1159 : : //! Get reference to underlying stream.
1160 : 17776709 : auto& GetStream()
1161 : : {
1162 : : if constexpr (ContainsStream<SubStream>) {
1163 [ + - + - : 173 : return m_substream.GetStream();
+ - ]
1164 : : } else {
1165 : 13099067 : 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 [ + - ][ + + : 1545814 : explicit ParamsWrapper(const Params& params, T& obj) : m_params{params}, m_object{obj} {}
+ - # # ]
[ + - + +
+ - ][ - -
- - + - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
+ - ][ + -
+ - + + +
+ ][ - - +
- + - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - +
- + - - +
- + - + +
- + - - +
+ - + - +
- + - - +
+ - - + +
- + - - +
- + + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ][ +
- + - + -
+ - + - +
- ]
1203 : :
1204 : : template <typename Stream>
1205 : 1670715 : void Serialize(Stream& s) const
1206 : : {
1207 : 1670715 : ParamsStream ss{s, m_params};
1208 : 1670715 : ::Serialize(ss, m_object);
1209 : : }
1210 : : template <typename Stream>
1211 : 6613 : void Unserialize(Stream& s)
1212 : : {
1213 : 6613 : ParamsStream ss{s, m_params};
1214 : 6613 : ::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
|