6#ifndef BITCOIN_SERIALIZE_H
7#define BITCOIN_SERIALIZE_H
32static constexpr uint64_t
MAX_SIZE = 0x02000000;
56 s.write(std::as_bytes(std::span{&obj, 1}));
61 s.write(std::as_bytes(std::span{&obj, 1}));
66 s.write(std::as_bytes(std::span{&obj, 1}));
71 s.write(std::as_bytes(std::span{&obj, 1}));
76 s.write(std::as_bytes(std::span{&obj, 1}));
81 s.write(std::as_bytes(std::span{&obj, 1}));
86 s.read(std::as_writable_bytes(std::span{&obj, 1}));
92 s.read(std::as_writable_bytes(std::span{&obj, 1}));
98 s.read(std::as_writable_bytes(std::span{&obj, 1}));
104 s.read(std::as_writable_bytes(std::span{&obj, 1}));
110 s.read(std::as_writable_bytes(std::span{&obj, 1}));
116 s.read(std::as_writable_bytes(std::span{&obj, 1}));
143template <
class Out,
class In>
146 static_assert(std::is_base_of_v<Out, In>);
149template <
class Out,
class In>
152 static_assert(std::is_base_of_v<Out, In>);
156#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
157#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
158#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
176#define FORMATTER_METHODS(cls, obj) \
177 template<typename Stream> \
178 static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, ActionSerialize{}); } \
179 template<typename Stream> \
180 static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, ActionUnserialize{}); } \
181 template<typename Stream, typename Type, typename Operation> \
182 static void SerializationOps(Type& obj, Stream& s, Operation ser_action)
217#define SER_PARAMS(type) (s.template GetParams<type>())
219#define BASE_SERIALIZE_METHODS(cls) \
220 template <typename Stream> \
221 void Serialize(Stream& s) const \
223 static_assert(std::is_same_v<const cls&, decltype(*this)>, "Serialize type mismatch"); \
226 template <typename Stream> \
227 void Unserialize(Stream& s) \
229 static_assert(std::is_same_v<cls&, decltype(*this)>, "Unserialize type mismatch"); \
240#define SERIALIZE_METHODS(cls, obj) \
241 BASE_SERIALIZE_METHODS(cls) \
242 FORMATTER_METHODS(cls, obj)
252concept CharNotInt8 = std::same_as<T, char> && !std::same_as<T, int8_t>;
255template <
typename Stream, CharNotInt8 V>
void Serialize(Stream&, V) =
delete;
267template <
typename Stream, BasicByte B,
size_t N>
void Serialize(Stream&
s,
const std::array<B, N>& a) {
s.write(
MakeByteSpan(a)); }
268template <
typename Stream, BasicByte B,
size_t N>
void Serialize(Stream&
s, std::span<B, N> span) {
s.write(std::as_bytes(span)); }
269template <
typename Stream, BasicByte B>
void Serialize(Stream&
s, std::span<B> span) {
s.write(std::as_bytes(span)); }
271template <
typename Stream, CharNotInt8 V>
void Unserialize(Stream&, V) =
delete;
284template <
typename Stream, BasicByte B,
size_t N>
void Unserialize(Stream&
s, std::span<B, N> span) {
s.read(std::as_writable_bytes(span)); }
285template <
typename Stream, BasicByte B>
void Unserialize(Stream&
s, std::span<B> span) {
s.read(std::as_writable_bytes(span)); }
301 if (
nSize < 253)
return sizeof(
unsigned char);
302 else if (
nSize <= std::numeric_limits<uint16_t>::max())
return sizeof(
unsigned char) +
sizeof(uint16_t);
303 else if (
nSize <= std::numeric_limits<unsigned int>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned int);
304 else return sizeof(
unsigned char) +
sizeof(uint64_t);
309template<
typename Stream>
316 else if (
nSize <= std::numeric_limits<uint16_t>::max())
321 else if (
nSize <= std::numeric_limits<unsigned int>::max())
340template<
typename Stream>
344 uint64_t nSizeRet = 0;
349 else if (chSize == 253)
353 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
355 else if (chSize == 254)
358 if (nSizeRet < 0x10000u)
359 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
364 if (nSizeRet < 0x100000000ULL)
365 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
367 if (range_check && nSizeRet >
MAX_SIZE) {
368 throw std::ios_base::failure(
"ReadCompactSize(): size too large");
409template <VarIntMode Mode,
typename I>
413 static_assert(Mode !=
VarIntMode::DEFAULT || std::is_unsigned_v<I>,
"Unsigned type required with mode DEFAULT.");
418template<VarIntMode Mode,
typename I>
435template<
typename Stream, VarIntMode Mode,
typename I>
439 unsigned char tmp[(
sizeof(n)*8+6)/7];
442 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
453template<
typename Stream, VarIntMode Mode,
typename I>
460 if (n > (std::numeric_limits<I>::max() >> 7)) {
461 throw std::ios_base::failure(
"ReadVarInt(): size too large");
463 n = (n << 7) | (chData & 0x7F);
465 if (n == std::numeric_limits<I>::max()) {
466 throw std::ios_base::failure(
"ReadVarInt(): size too large");
476template<
typename Formatter,
typename T>
479 static_assert(std::is_lvalue_reference_v<T>,
"Wrapper needs an lvalue reference type T");
498template<
typename Formatter,
typename T>
501#define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj)
502#define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj)
503#define COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj)
504#define LIMITED_STRING(obj,n) Using<LimitedStringFormatter<n>>(obj)
507template<VarIntMode Mode>
510 template<
typename Stream,
typename I>
void Ser(Stream &
s, I v)
512 WriteVarInt<Stream,Mode, std::remove_cv_t<I>>(
s, v);
515 template<
typename Stream,
typename I>
void Unser(Stream&
s, I& v)
517 v = ReadVarInt<Stream,Mode, std::remove_cv_t<I>>(
s);
530template<
int Bytes,
bool BigEndian = false>
533 static_assert(Bytes > 0 && Bytes <= 8,
"CustomUintFormatter Bytes out of range");
534 static constexpr uint64_t
MAX = 0xffffffffffffffff >> (8 * (8 - Bytes));
536 template <
typename Stream,
typename I>
void Ser(Stream&
s, I v)
538 if (v < 0 || v >
MAX)
throw std::ios_base::failure(
"CustomUintFormatter value out of range");
541 s.write(std::as_bytes(std::span{&raw, 1}).last(Bytes));
544 s.write(std::as_bytes(std::span{&raw, 1}).first(Bytes));
548 template <
typename Stream,
typename I>
void Unser(Stream&
s, I& v)
550 using U =
typename std::conditional_t<std::is_enum_v<I>, std::underlying_type<I>, std::common_type<I>>::type;
551 static_assert(std::numeric_limits<U>::max() >=
MAX && std::numeric_limits<U>::min() <= 0,
"Assigned type too small");
554 s.read(std::as_writable_bytes(std::span{&raw, 1}).last(Bytes));
557 s.read(std::as_writable_bytes(std::span{&raw, 1}).first(Bytes));
566template<
bool RangeCheck>
569 template<
typename Stream,
typename I>
572 uint64_t n = ReadCompactSize<Stream>(
s, RangeCheck);
573 if (n < std::numeric_limits<I>::min() || n > std::numeric_limits<I>::max()) {
574 throw std::ios_base::failure(
"CompactSize exceeds limit of type");
579 template<
typename Stream,
typename I>
582 static_assert(std::is_unsigned_v<I>,
"CompactSize only supported for unsigned integers");
583 static_assert(std::numeric_limits<I>::max() <= std::numeric_limits<uint64_t>::max(),
"CompactSize only supports 64-bit integers and below");
585 WriteCompactSize<Stream>(
s, v);
589template <
typename U,
bool LOSSY = false>
591 template <
typename Stream,
typename Tp>
597 tp = Tp{
typename Tp::duration{
typename Tp::duration::rep{u}}};
599 template <
typename Stream,
typename Tp>
602 if constexpr (LOSSY) {
603 s << U(tp.time_since_epoch().count());
605 s << U{tp.time_since_epoch().count()};
619 template<
typename Stream>
621 WriteCompactSize<Stream>(
s,
n);
625template<
size_t Limit>
628 template<
typename Stream>
633 throw std::ios_base::failure(
"String length limit exceeded");
639 template<
typename Stream>
640 void Ser(Stream&
s,
const std::string& v)
659template<
class Formatter>
662 template<
typename Stream,
typename V>
663 void Ser(Stream&
s,
const V& v)
667 for (
const typename V::value_type& elem : v) {
668 formatter.Ser(
s, elem);
672 template<
typename Stream,
typename V>
678 size_t allocated = 0;
679 while (allocated < size) {
683 static_assert(
sizeof(
typename V::value_type) <=
MAX_VECTOR_ALLOCATE,
"Vector element size too large");
684 allocated = std::min(size, allocated +
MAX_VECTOR_ALLOCATE /
sizeof(
typename V::value_type));
685 v.reserve(allocated);
686 while (v.size() < allocated) {
688 formatter.Unser(
s, v.back());
701template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str);
702template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str);
713template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v);
714template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v);
719template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item);
720template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item);
725template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m);
726template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
731template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m);
732template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m);
737template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::shared_ptr<const T>& p);
738template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::shared_ptr<const T>& p);
743template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::unique_ptr<const T>& p);
744template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::unique_ptr<const T>& p);
750template <
class T,
class Stream>
752template <
typename Stream,
typename T>
759template <
class T,
class Stream>
761template <
typename Stream,
typename T>
775 template<
typename Stream,
typename T>
778 template<
typename Stream,
typename T>
789template<
typename Stream,
typename C>
790void Serialize(Stream& os,
const std::basic_string<C>& str)
797template<
typename Stream,
typename C>
811template <
typename Stream,
unsigned int N,
typename T>
823template <
typename Stream,
unsigned int N,
typename T>
832 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
834 is.read(std::as_writable_bytes(std::span{&v[i], blk}));
846template <
typename Stream,
typename T,
typename A>
852 }
else if constexpr (std::is_same_v<T, bool>) {
857 for (
bool elem : v) {
866template <
typename Stream,
typename T,
typename A>
875 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
877 is.read(std::as_writable_bytes(std::span{&v[i], blk}));
889template<
typename Stream,
typename K,
typename T>
896template<
typename Stream,
typename K,
typename T>
908template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
909void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m)
912 for (
const auto& entry :
m)
916template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
921 typename std::map<K, T, Pred, A>::iterator mi =
m.begin();
922 for (
unsigned int i = 0; i < nSize; i++)
924 std::pair<K, T> item;
926 mi =
m.insert(mi, item);
935template<
typename Stream,
typename K,
typename Pred,
typename A>
936void Serialize(Stream& os,
const std::set<K, Pred, A>& m)
939 for (
typename std::set<K, Pred, A>::const_iterator it =
m.begin(); it !=
m.end(); ++it)
943template<
typename Stream,
typename K,
typename Pred,
typename A>
948 typename std::set<K, Pred, A>::iterator it =
m.begin();
949 for (
unsigned int i = 0; i < nSize; i++)
953 it =
m.insert(it, key);
962template<
typename Stream,
typename T>
void
968template<
typename Stream,
typename T>
979template<
typename Stream,
typename T>
void
985template<
typename Stream,
typename T>
995template <
typename Stream,
typename... Args>
1001template <
typename Stream,
typename... Args>
1013 template<
typename Stream,
typename... Args>
1019 template<
typename Stream,
typename Type,
typename Fn>
1024 template<
typename Stream,
typename Type,
typename Fn>
1027 fn(
s, std::forward<Type>(obj));
1033 template<
typename Stream,
typename... Args>
1039 template<
typename Stream,
typename Type,
typename Fn>
1042 fn(
s, std::forward<Type>(obj));
1045 template<
typename Stream,
typename Type,
typename Fn>
1070 void write(std::span<const std::byte> src)
1072 this->
nSize += src.size();
1078 this->
nSize += _nSize;
1081 template<
typename T>
1096 s.seek(GetSizeOfVarInt<I>(n));
1104template <
typename T>
1115template <
typename SubStream,
typename Params>
1131 template <
typename NestedSubstream,
typename Params1,
typename Params2,
typename... NestedParams>
1144 template <
typename P>
1147 if constexpr (std::is_convertible_v<Params, P>) {
1178template <
typename Substream,
typename Params>
1185template <
typename Substream,
typename Params1,
typename Params2,
typename...
Params>
1190template <
typename Params,
typename T>
1199 template <
typename Stream>
1205 template <
typename Stream>
1222#define SER_PARAMS_OPFUNC \
1228 template <typename T> \
1229 auto operator()(T&& t) const \
1231 return ParamsWrapper{*this, t}; \
const CChainParams & Params()
Return the currently selected parameters.
void Serialize(Stream &s) const
CompactSizeWriter(uint64_t n_in)
Wrapper that overrides the GetParams() function of a stream.
ParamsStream & operator<<(const U &obj)
ParamsStream & operator>>(U &&obj)
ParamsStream(NestedSubstream &&s, const Params1 ¶ms1 LIFETIMEBOUND, const Params2 ¶ms2 LIFETIMEBOUND, const NestedParams &... params LIFETIMEBOUND)
auto & GetStream()
Get reference to underlying stream.
const auto & GetParams() const
Get reference to stream parameters.
void write(std::span< const std::byte > src)
void read(std::span< std::byte > dst)
const auto & GetStream() const
ParamsStream(SubStream &&substream, const Params ¶ms LIFETIMEBOUND)
Wrapper that serializes objects with the specified parameters.
void Unserialize(Stream &s)
void Serialize(Stream &s) const
ParamsWrapper(const Params ¶ms, T &obj)
void write(std::span< const std::byte > src)
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
SizeComputer & operator<<(const T &obj)
Simple wrapper class to serialize objects using a formatter; used by Using().
void Serialize(Stream &s) const
void Unserialize(Stream &s)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
void resize_uninitialized(size_type new_size)
Check if type contains a stream by seeing if has a GetStream() method.
If none of the specialized versions above matched, default to calling member function.
BSWAP_CONSTEXPR uint32_t be32toh_internal(uint32_t big_endian_32bits)
BSWAP_CONSTEXPR uint16_t be16toh_internal(uint16_t big_endian_16bits)
BSWAP_CONSTEXPR uint64_t htobe64_internal(uint64_t host_64bits)
BSWAP_CONSTEXPR uint16_t htobe16_internal(uint16_t host_16bits)
BSWAP_CONSTEXPR uint32_t htole32_internal(uint32_t host_32bits)
BSWAP_CONSTEXPR uint16_t htole16_internal(uint16_t host_16bits)
BSWAP_CONSTEXPR uint64_t be64toh_internal(uint64_t big_endian_64bits)
BSWAP_CONSTEXPR uint16_t le16toh_internal(uint16_t little_endian_16bits)
BSWAP_CONSTEXPR uint64_t htole64_internal(uint64_t host_64bits)
BSWAP_CONSTEXPR uint64_t le64toh_internal(uint64_t little_endian_64bits)
BSWAP_CONSTEXPR uint32_t le32toh_internal(uint32_t little_endian_32bits)
BSWAP_CONSTEXPR uint32_t htobe32_internal(uint32_t host_32bits)
#define T(expected, seed, data)
size_t GetSerializeSize(const T &t)
void Serialize(Stream &, V)=delete
constexpr unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
void SerializeMany(Stream &s, const Args &... args)
Support for (un)serializing many things at once.
static const unsigned int MAX_VECTOR_ALLOCATE
Maximum amount of memory (in bytes) to allocate at once when deserializing vectors.
uint8_t ser_readdata8(Stream &s)
void ser_writedata32be(Stream &s, uint32_t obj)
VarIntMode
Variable-length integers: bytes are a MSB base-128 encoding of the number.
void ser_writedata32(Stream &s, uint32_t obj)
static constexpr uint64_t MAX_SIZE
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size...
constexpr deserialize_type deserialize
void ser_writedata16(Stream &s, uint16_t obj)
void Unserialize(Stream &, V)=delete
void UnserializeMany(Stream &s, Args &&... args)
Out & AsBase(In &x)
Convert any argument to a reference to X, maintaining constness.
void WriteVarInt(SizeComputer &os, I n)
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
void ser_writedata16be(Stream &s, uint16_t obj)
uint16_t ser_readdata16(Stream &s)
uint64_t ser_readdata64(Stream &s)
void ser_writedata8(Stream &s, uint8_t obj)
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
ParamsStream(Substream &&, const Params &) -> ParamsStream< Substream, Params >
Explicit template deduction guide is required for single-parameter constructor so Substream&& is trea...
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
unsigned int GetSizeOfVarInt(I n)
uint32_t ser_readdata32(Stream &s)
uint16_t ser_readdata16be(Stream &s)
void ser_writedata64(Stream &s, uint64_t obj)
uint32_t ser_readdata32be(Stream &s)
auto MakeByteSpan(const V &v) noexcept
auto MakeWritableByteSpan(V &&v) noexcept
Support for all macros providing or using the ser_action parameter of the SerializationOps method.
static void SerReadWriteMany(Stream &s, const Args &... args)
static constexpr bool ForRead()
static void SerWrite(Stream &s, Type &&obj, Fn &&fn)
static void SerRead(Stream &s, Type &&, Fn &&)
static constexpr bool ForRead()
static void SerReadWriteMany(Stream &s, Args &&... args)
static void SerRead(Stream &s, Type &&obj, Fn &&fn)
static void SerWrite(Stream &s, Type &&, Fn &&)
constexpr CheckVarIntMode()
Dummy data type to identify deserializing constructors.