6#ifndef BITCOIN_SERIALIZE_H
7#define BITCOIN_SERIALIZE_H
31static constexpr uint64_t
MAX_SIZE = 0x02000000;
52inline char*
CharCast(
unsigned char* c) {
return (
char*)c; }
53inline const char*
CharCast(
const char* c) {
return c; }
54inline const char*
CharCast(
const unsigned char* c) {
return (
const char*)c; }
62 s.write((
char*)&obj, 1);
67 s.write((
char*)&obj, 2);
72 s.write((
char*)&obj, 2);
77 s.write((
char*)&obj, 4);
82 s.write((
char*)&obj, 4);
87 s.write((
char*)&obj, 8);
92 s.read((
char*)&obj, 1);
98 s.read((
char*)&obj, 2);
104 s.read((
char*)&obj, 2);
110 s.read((
char*)&obj, 4);
116 s.read((
char*)&obj, 4);
122 s.read((
char*)&obj, 8);
147#define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
148#define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj)))
149#define SER_READ(obj, code) ::SerRead(s, ser_action, obj, [&](Stream& s, typename std::remove_const<Type>::type& obj) { code; })
150#define SER_WRITE(obj, code) ::SerWrite(s, ser_action, obj, [&](Stream& s, const Type& obj) { code; })
168#define FORMATTER_METHODS(cls, obj) \
169 template<typename Stream> \
170 static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, CSerActionSerialize()); } \
171 template<typename Stream> \
172 static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, CSerActionUnserialize()); } \
173 template<typename Stream, typename Type, typename Operation> \
174 static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \
183#define SERIALIZE_METHODS(cls, obj) \
184 template<typename Stream> \
185 void Serialize(Stream& s) const \
187 static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
190 template<typename Stream> \
191 void Unserialize(Stream& s) \
193 static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
196 FORMATTER_METHODS(cls, obj)
198#ifndef CHAR_EQUALS_INT8
209template<
typename Stream,
int N>
inline void Serialize(Stream& s,
const char (&a)[N]) { s.write(a, N); }
210template<
typename Stream,
int N>
inline void Serialize(Stream& s,
const unsigned char (&a)[N]) { s.write(
CharCast(a), N); }
214#ifndef CHAR_EQUALS_INT8
225template<
typename Stream,
int N>
inline void Unserialize(Stream& s,
char (&a)[N]) { s.read(a, N); }
226template<
typename Stream,
int N>
inline void Unserialize(Stream& s,
unsigned char (&a)[N]) { s.read(
CharCast(a), N); }
242 if (
nSize < 253)
return sizeof(
unsigned char);
243 else if (
nSize <= std::numeric_limits<uint16_t>::max())
return sizeof(
unsigned char) +
sizeof(uint16_t);
244 else if (
nSize <= std::numeric_limits<unsigned int>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned int);
245 else return sizeof(
unsigned char) +
sizeof(uint64_t);
250template<
typename Stream>
257 else if (
nSize <= std::numeric_limits<uint16_t>::max())
262 else if (
nSize <= std::numeric_limits<unsigned int>::max())
281template<
typename Stream>
285 uint64_t nSizeRet = 0;
290 else if (chSize == 253)
294 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
296 else if (chSize == 254)
299 if (nSizeRet < 0x10000u)
300 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
305 if (nSizeRet < 0x100000000ULL)
306 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
308 if (range_check && nSizeRet >
MAX_SIZE) {
309 throw std::ios_base::failure(
"ReadCompactSize(): size too large");
350template <VarIntMode Mode,
typename I>
354 static_assert(Mode !=
VarIntMode::DEFAULT || std::is_unsigned<I>::value,
"Unsigned type required with mode DEFAULT.");
359template<VarIntMode Mode,
typename I>
376template<
typename Stream, VarIntMode Mode,
typename I>
380 unsigned char tmp[(
sizeof(n)*8+6)/7];
383 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
394template<
typename Stream, VarIntMode Mode,
typename I>
401 if (n > (std::numeric_limits<I>::max() >> 7)) {
402 throw std::ios_base::failure(
"ReadVarInt(): size too large");
404 n = (n << 7) | (chData & 0x7F);
406 if (n == std::numeric_limits<I>::max()) {
407 throw std::ios_base::failure(
"ReadVarInt(): size too large");
417template<
typename Formatter,
typename T>
420 static_assert(std::is_lvalue_reference<T>::value,
"Wrapper needs an lvalue reference type T");
439template<
typename Formatter,
typename T>
442#define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj)
443#define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj)
444#define COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj)
445#define LIMITED_STRING(obj,n) Using<LimitedStringFormatter<n>>(obj)
448template<VarIntMode Mode>
451 template<
typename Stream,
typename I>
void Ser(Stream &s, I v)
453 WriteVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s, v);
456 template<
typename Stream,
typename I>
void Unser(Stream& s, I& v)
458 v = ReadVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s);
471template<
int Bytes,
bool BigEndian = false>
474 static_assert(Bytes > 0 && Bytes <= 8,
"CustomUintFormatter Bytes out of range");
475 static constexpr uint64_t
MAX = 0xffffffffffffffff >> (8 * (8 - Bytes));
477 template <
typename Stream,
typename I>
void Ser(Stream& s, I v)
479 if (v < 0 || v >
MAX)
throw std::ios_base::failure(
"CustomUintFormatter value out of range");
482 s.write(((
const char*)&raw) + 8 - Bytes, Bytes);
485 s.write((
const char*)&raw, Bytes);
489 template <
typename Stream,
typename I>
void Unser(Stream& s, I& v)
491 using U =
typename std::conditional<std::is_enum<I>::value, std::underlying_type<I>, std::common_type<I>>::type::type;
492 static_assert(std::numeric_limits<U>::max() >=
MAX && std::numeric_limits<U>::min() <= 0,
"Assigned type too small");
495 s.read(((
char*)&raw) + 8 - Bytes, Bytes);
496 v =
static_cast<I
>(
be64toh(raw));
498 s.read((
char*)&raw, Bytes);
499 v =
static_cast<I
>(
le64toh(raw));
507template<
bool RangeCheck>
510 template<
typename Stream,
typename I>
513 uint64_t n = ReadCompactSize<Stream>(s, RangeCheck);
514 if (n < std::numeric_limits<I>::min() || n > std::numeric_limits<I>::max()) {
515 throw std::ios_base::failure(
"CompactSize exceeds limit of type");
520 template<
typename Stream,
typename I>
523 static_assert(std::is_unsigned<I>::value,
"CompactSize only supported for unsigned integers");
524 static_assert(std::numeric_limits<I>::max() <= std::numeric_limits<uint64_t>::max(),
"CompactSize only supports 64-bit integers and below");
526 WriteCompactSize<Stream>(s, v);
530template<
size_t Limit>
533 template<
typename Stream>
534 void Unser(Stream& s, std::string& v)
538 throw std::ios_base::failure(
"String length limit exceeded");
541 if (size != 0) s.read((
char*)v.data(), size);
544 template<
typename Stream>
545 void Ser(Stream& s,
const std::string& v)
564template<
class Formatter>
567 template<
typename Stream,
typename V>
568 void Ser(Stream& s,
const V& v)
572 for (
const typename V::value_type& elem : v) {
573 formatter.Ser(s, elem);
577 template<
typename Stream,
typename V>
583 size_t allocated = 0;
584 while (allocated < size) {
588 static_assert(
sizeof(
typename V::value_type) <=
MAX_VECTOR_ALLOCATE,
"Vector element size too large");
589 allocated = std::min(size, allocated +
MAX_VECTOR_ALLOCATE /
sizeof(
typename V::value_type));
590 v.reserve(allocated);
591 while (v.size() < allocated) {
593 formatter.Unser(s, v.back());
606template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str);
607template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str);
624template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&);
625template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const bool&);
626template<
typename Stream,
typename T,
typename A,
typename V>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const V&);
627template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v);
628template<
typename Stream,
typename T,
typename A>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const unsigned char&);
629template<
typename Stream,
typename T,
typename A,
typename V>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const V&);
630template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v);
635template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item);
636template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item);
641template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m);
642template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
647template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m);
648template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m);
653template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::shared_ptr<const T>& p);
654template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::shared_ptr<const T>& p);
659template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::unique_ptr<const T>& p);
660template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::unique_ptr<const T>& p);
667template<
typename Stream,
typename T>
673template<
typename Stream,
typename T>
686 template<
typename Stream,
typename T>
689 template<
typename Stream,
typename T>
700template<
typename Stream,
typename C>
701void Serialize(Stream& os,
const std::basic_string<C>& str)
705 os.write((
char*)str.data(), str.size() *
sizeof(C));
708template<
typename Stream,
typename C>
714 is.read((
char*)str.data(), nSize *
sizeof(C));
722template<
typename Stream,
unsigned int N,
typename T>
727 os.write((
char*)v.
data(), v.
size() *
sizeof(
T));
730template<
typename Stream,
unsigned int N,
typename T,
typename V>
736template<
typename Stream,
unsigned int N,
typename T>
743template<
typename Stream,
unsigned int N,
typename T>
752 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
754 is.read((
char*)&v[i], blk *
sizeof(
T));
759template<
typename Stream,
unsigned int N,
typename T,
typename V>
765template<
typename Stream,
unsigned int N,
typename T>
776template<
typename Stream,
typename T,
typename A>
781 os.write((
char*)v.data(), v.size() *
sizeof(
T));
784template<
typename Stream,
typename T,
typename A>
791 for (
bool elem : v) {
796template<
typename Stream,
typename T,
typename A,
typename V>
802template<
typename Stream,
typename T,
typename A>
803inline void Serialize(Stream& os,
const std::vector<T, A>& v)
809template<
typename Stream,
typename T,
typename A>
818 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
820 is.read((
char*)&v[i], blk *
sizeof(
T));
825template<
typename Stream,
typename T,
typename A,
typename V>
831template<
typename Stream,
typename T,
typename A>
842template<
typename Stream,
typename K,
typename T>
849template<
typename Stream,
typename K,
typename T>
861template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
862void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m)
865 for (
const auto& entry : m)
869template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
874 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
875 for (
unsigned int i = 0; i < nSize; i++)
877 std::pair<K, T> item;
879 mi = m.insert(mi, item);
888template<
typename Stream,
typename K,
typename Pred,
typename A>
889void Serialize(Stream& os,
const std::set<K, Pred, A>& m)
892 for (
typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
896template<
typename Stream,
typename K,
typename Pred,
typename A>
901 typename std::set<K, Pred, A>::iterator it = m.begin();
902 for (
unsigned int i = 0; i < nSize; i++)
906 it = m.insert(it, key);
915template<
typename Stream,
typename T>
void
921template<
typename Stream,
typename T>
932template<
typename Stream,
typename T>
void
938template<
typename Stream,
typename T>
951 constexpr bool ForRead()
const {
return false; }
955 constexpr bool ForRead()
const {
return true; }
985 void write(
const char *psz,
size_t _nSize)
987 this->nSize += _nSize;
993 this->nSize += _nSize;
1010template<
typename Stream>
1015template<
typename Stream,
typename Arg,
typename... Args>
1022template<
typename Stream>
1027template<
typename Stream,
typename Arg,
typename... Args>
1034template<
typename Stream,
typename... Args>
1040template<
typename Stream,
typename... Args>
1046template<
typename Stream,
typename Type,
typename Fn>
1051template<
typename Stream,
typename Type,
typename Fn>
1054 fn(s, std::forward<Type>(obj));
1057template<
typename Stream,
typename Type,
typename Fn>
1060 fn(s, std::forward<Type>(obj));
1063template<
typename Stream,
typename Type,
typename Fn>
1071 s.
seek(GetSizeOfVarInt<I>(n));
1079template <
typename T>
1085template <
typename...
T>
CSizeComputer & operator<<(const T &obj)
void write(const char *psz, size_t _nSize)
CSizeComputer(int nVersionIn)
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
constexpr C * data() 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)
uint16_t be16toh(uint16_t big_endian_16bits)
uint16_t htobe16(uint16_t host_16bits)
uint32_t le32toh(uint32_t little_endian_32bits)
uint32_t htobe32(uint32_t host_32bits)
uint16_t le16toh(uint16_t little_endian_16bits)
uint64_t htobe64(uint64_t host_64bits)
uint64_t be64toh(uint64_t big_endian_64bits)
uint32_t be32toh(uint32_t big_endian_32bits)
uint64_t htole64(uint64_t host_64bits)
uint32_t htole32(uint32_t host_32bits)
uint16_t htole16(uint16_t host_16bits)
uint64_t le64toh(uint64_t little_endian_64bits)
#define T(expected, seed, data)
void SerializeMany(Stream &s)
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)
void WriteVarInt(CSizeComputer &os, I n)
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...
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...
constexpr deserialize_type deserialize
void Serialize(Stream &s, char a)
void ser_writedata16(Stream &s, uint16_t obj)
void Serialize_impl(Stream &os, const prevector< N, T > &v, const unsigned char &)
prevector prevectors of unsigned char are a special case and are intended to be serialized as a singl...
void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, const Args &... args)
char * CharCast(char *c)
Safely convert odd char pointer types to standard ones.
void Unserialize_impl(Stream &is, prevector< N, T > &v, const unsigned char &)
void Unserialize(Stream &s, char &a)
X & ReadWriteAsHelper(X &x)
Convert the reference base type to X, without changing constness or reference type.
void SerWrite(Stream &s, CSerActionSerialize ser_action, Type &&obj, Fn &&fn)
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.
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 SerRead(Stream &s, CSerActionSerialize ser_action, Type &&, Fn &&)
size_t GetSerializeSize(const T &t, int nVersion=0)
void UnserializeMany(Stream &s)
size_t GetSerializeSizeMany(int nVersion, const T &... t)
void ser_writedata64(Stream &s, uint64_t obj)
uint32_t ser_readdata32be(Stream &s)
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Support for SERIALIZE_METHODS and READWRITE macro.
constexpr bool ForRead() const
constexpr bool ForRead() const
constexpr CheckVarIntMode()
Dummy data type to identify deserializing constructors.