6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
32 static constexpr uint64_t
MAX_SIZE = 0x02000000;
143 template <
class Out,
class In>
146 static_assert(std::is_base_of_v<Out, In>);
149 template <
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, typename std::remove_const<Type>::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 FORMATTER_METHODS_PARAMS(cls, obj, paramcls, paramobj) \
218 template <typename Stream> \
219 static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, ActionSerialize{}, s.GetParams()); } \
220 template <typename Stream> \
221 static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, ActionUnserialize{}, s.GetParams()); } \
222 template <typename Stream, typename Type, typename Operation> \
223 static void SerializationOps(Type& obj, Stream& s, Operation ser_action, const paramcls& paramobj)
225 #define BASE_SERIALIZE_METHODS(cls) \
226 template <typename Stream> \
227 void Serialize(Stream& s) const \
229 static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
232 template <typename Stream> \
233 void Unserialize(Stream& s) \
235 static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
246 #define SERIALIZE_METHODS(cls, obj) \
247 BASE_SERIALIZE_METHODS(cls) \
248 FORMATTER_METHODS(cls, obj)
255 #define SERIALIZE_METHODS_PARAMS(cls, obj, paramcls, paramobj) \
256 BASE_SERIALIZE_METHODS(cls) \
257 FORMATTER_METHODS_PARAMS(cls, obj, paramcls, paramobj)
268 concept
CharNotInt8 = std::same_as<T, char> && !std::same_as<T, int8_t>;
270 template <
typename Stream, CharNotInt8 V>
void Serialize(Stream&, V) =
delete;
281 template <
typename Stream, BasicByte B, std::
size_t N>
void Serialize(Stream& s,
const std::array<B, N>& a) { s.write(
MakeByteSpan(a)); }
284 template <
typename Stream, CharNotInt8 V>
void Unserialize(Stream&, V) =
delete;
312 if (
nSize < 253)
return sizeof(
unsigned char);
313 else if (
nSize <= std::numeric_limits<uint16_t>::max())
return sizeof(
unsigned char) +
sizeof(uint16_t);
314 else if (
nSize <= std::numeric_limits<unsigned int>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned int);
315 else return sizeof(
unsigned char) +
sizeof(uint64_t);
320 template<
typename Stream>
327 else if (
nSize <= std::numeric_limits<uint16_t>::max())
332 else if (
nSize <= std::numeric_limits<unsigned int>::max())
351 template<
typename Stream>
355 uint64_t nSizeRet = 0;
360 else if (chSize == 253)
364 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
366 else if (chSize == 254)
369 if (nSizeRet < 0x10000u)
370 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
375 if (nSizeRet < 0x100000000ULL)
376 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
378 if (range_check && nSizeRet >
MAX_SIZE) {
379 throw std::ios_base::failure(
"ReadCompactSize(): size too large");
420 template <VarIntMode Mode,
typename I>
424 static_assert(Mode !=
VarIntMode::DEFAULT || std::is_unsigned<I>::value,
"Unsigned type required with mode DEFAULT.");
429 template<VarIntMode Mode,
typename I>
446 template<
typename Stream, VarIntMode Mode,
typename I>
450 unsigned char tmp[(
sizeof(n)*8+6)/7];
453 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
464 template<
typename Stream, VarIntMode Mode,
typename I>
471 if (n > (std::numeric_limits<I>::max() >> 7)) {
472 throw std::ios_base::failure(
"ReadVarInt(): size too large");
474 n = (n << 7) | (chData & 0x7F);
476 if (n == std::numeric_limits<I>::max()) {
477 throw std::ios_base::failure(
"ReadVarInt(): size too large");
487 template<
typename Formatter,
typename T>
490 static_assert(std::is_lvalue_reference<T>::value,
"Wrapper needs an lvalue reference type T");
509 template<
typename Formatter,
typename T>
512 #define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj)
513 #define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj)
514 #define COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj)
515 #define LIMITED_STRING(obj,n) Using<LimitedStringFormatter<n>>(obj)
518 template<VarIntMode Mode>
521 template<
typename Stream,
typename I>
void Ser(Stream &s, I v)
523 WriteVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s, v);
526 template<
typename Stream,
typename I>
void Unser(Stream& s, I& v)
528 v = ReadVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s);
541 template<
int Bytes,
bool BigEndian = false>
544 static_assert(Bytes > 0 && Bytes <= 8,
"CustomUintFormatter Bytes out of range");
545 static constexpr uint64_t
MAX = 0xffffffffffffffff >> (8 * (8 - Bytes));
547 template <
typename Stream,
typename I>
void Ser(Stream& s, I v)
549 if (v < 0 || v >
MAX)
throw std::ios_base::failure(
"CustomUintFormatter value out of range");
559 template <
typename Stream,
typename I>
void Unser(Stream& s, I& v)
561 using U =
typename std::conditional<std::is_enum<I>::value, std::underlying_type<I>, std::common_type<I>>::type::type;
562 static_assert(std::numeric_limits<U>::max() >=
MAX && std::numeric_limits<U>::min() <= 0,
"Assigned type too small");
577 template<
bool RangeCheck>
580 template<
typename Stream,
typename I>
583 uint64_t n = ReadCompactSize<Stream>(s, RangeCheck);
584 if (n < std::numeric_limits<I>::min() || n > std::numeric_limits<I>::max()) {
585 throw std::ios_base::failure(
"CompactSize exceeds limit of type");
590 template<
typename Stream,
typename I>
593 static_assert(std::is_unsigned<I>::value,
"CompactSize only supported for unsigned integers");
594 static_assert(std::numeric_limits<I>::max() <= std::numeric_limits<uint64_t>::max(),
"CompactSize only supports 64-bit integers and below");
596 WriteCompactSize<Stream>(s, v);
600 template <
typename U,
bool LOSSY = false>
602 template <
typename Stream,
typename Tp>
608 tp = Tp{
typename Tp::duration{
typename Tp::duration::rep{u}}};
610 template <
typename Stream,
typename Tp>
611 void Ser(Stream& s, Tp tp)
613 if constexpr (LOSSY) {
614 s << U(tp.time_since_epoch().count());
616 s << U{tp.time_since_epoch().count()};
620 template <
typename U>
630 template<
typename Stream>
632 WriteCompactSize<Stream>(s,
n);
636 template<
size_t Limit>
639 template<
typename Stream>
640 void Unser(Stream& s, std::string& v)
644 throw std::ios_base::failure(
"String length limit exceeded");
650 template<
typename Stream>
651 void Ser(Stream& s,
const std::string& v)
670 template<
class Formatter>
673 template<
typename Stream,
typename V>
674 void Ser(Stream& s,
const V& v)
678 for (
const typename V::value_type& elem : v) {
679 formatter.Ser(s, elem);
683 template<
typename Stream,
typename V>
689 size_t allocated = 0;
690 while (allocated < size) {
694 static_assert(
sizeof(
typename V::value_type) <=
MAX_VECTOR_ALLOCATE,
"Vector element size too large");
695 allocated = std::min(size, allocated +
MAX_VECTOR_ALLOCATE /
sizeof(
typename V::value_type));
696 v.reserve(allocated);
697 while (v.size() < allocated) {
699 formatter.Unser(s, v.back());
712 template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str);
713 template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str);
724 template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v);
725 template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v);
730 template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item);
731 template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item);
736 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m);
737 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
742 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m);
743 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m);
748 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::shared_ptr<const T>& p);
749 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::shared_ptr<const T>& p);
754 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::unique_ptr<const T>& p);
755 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::unique_ptr<const T>& p);
761 template <
class T,
class Stream>
763 template <
typename Stream,
typename T>
764 requires Serializable<T, Stream>
770 template <
class T,
class Stream>
772 template <
typename Stream,
typename T>
773 requires Unserializable<T, Stream>
786 template<
typename Stream,
typename T>
789 template<
typename Stream,
typename T>
800 template<
typename Stream,
typename C>
801 void Serialize(Stream& os,
const std::basic_string<C>& str)
808 template<
typename Stream,
typename C>
822 template <
typename Stream,
unsigned int N,
typename T>
825 if constexpr (BasicByte<T>) {
834 template <
typename Stream,
unsigned int N,
typename T>
837 if constexpr (BasicByte<T>) {
843 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
857 template <
typename Stream,
typename T,
typename A>
860 if constexpr (BasicByte<T>) {
863 }
else if constexpr (std::is_same_v<T, bool>) {
868 for (
bool elem : v) {
877 template <
typename Stream,
typename T,
typename A>
880 if constexpr (BasicByte<T>) {
886 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
900 template<
typename Stream,
typename K,
typename T>
907 template<
typename Stream,
typename K,
typename T>
919 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
920 void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m)
923 for (
const auto& entry :
m)
927 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
932 typename std::map<K, T, Pred, A>::iterator mi =
m.begin();
933 for (
unsigned int i = 0; i < nSize; i++)
935 std::pair<K, T> item;
937 mi =
m.insert(mi, item);
946 template<
typename Stream,
typename K,
typename Pred,
typename A>
947 void Serialize(Stream& os,
const std::set<K, Pred, A>& m)
950 for (
typename std::set<K, Pred, A>::const_iterator it =
m.begin(); it !=
m.end(); ++it)
954 template<
typename Stream,
typename K,
typename Pred,
typename A>
959 typename std::set<K, Pred, A>::iterator it =
m.begin();
960 for (
unsigned int i = 0; i < nSize; i++)
964 it =
m.insert(it, key);
973 template<
typename Stream,
typename T>
void
974 Serialize(Stream& os,
const std::unique_ptr<const T>& p)
979 template<
typename Stream,
typename T>
990 template<
typename Stream,
typename T>
void
991 Serialize(Stream& os,
const std::shared_ptr<const T>& p)
996 template<
typename Stream,
typename T>
1006 template <
typename Stream,
typename... Args>
1012 template <
typename Stream,
typename... Args>
1024 template<
typename Stream,
typename... Args>
1030 template<
typename Stream,
typename Type,
typename Fn>
1035 template<
typename Stream,
typename Type,
typename Fn>
1038 fn(s, std::forward<Type>(obj));
1044 template<
typename Stream,
typename... Args>
1050 template<
typename Stream,
typename Type,
typename Fn>
1051 static void SerRead(Stream& s, Type&& obj, Fn&& fn)
1053 fn(s, std::forward<Type>(obj));
1056 template<
typename Stream,
typename Type,
typename Fn>
1089 this->
nSize += _nSize;
1092 template<
typename T>
1104 template<
typename I>
1107 s.
seek(GetSizeOfVarInt<I>(n));
1115 template <
typename T>
1122 template <
typename Params,
typename SubStream>
1143 template <
typename Params,
typename T>
1152 template <
typename Stream>
1158 template <
typename Stream>
1175 #define SER_PARAMS_OPFUNC \
1181 template <typename T> \
1182 auto operator()(T&& t) const \
1184 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 (and hides GetVersion/GetType).
ParamsStream(const Params ¶ms LIFETIMEBOUND, SubStream &substream LIFETIMEBOUND)
void write(Span< const std::byte > src)
void read(Span< std::byte > dst)
ParamsStream & operator>>(U &&obj)
const Params & GetParams() const
ParamsStream & operator<<(const U &obj)
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(Span< const std::byte > src)
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
SizeComputer & operator<<(const T &obj)
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
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)
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)
Out & AsBase(In &x)
Convert any argument to a reference to X, maintaining constness.
void Unserialize(Stream &, V)=delete
void UnserializeMany(Stream &s, Args &&... args)
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
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.
unsigned int GetSizeOfVarInt(I n)
concept Serializable
If none of the specialized versions above matched, default to calling member function.
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)
Span< std::byte > AsWritableBytes(Span< T > s) noexcept
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Span< const std::byte > AsBytes(Span< T > s) noexcept
Span< std::byte > 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.