6 #ifndef BITCOIN_SERIALIZE_H 7 #define BITCOIN_SERIALIZE_H 31 static constexpr uint64_t
MAX_SIZE = 0x02000000;
52 inline char*
CharCast(
unsigned char* c) {
return (
char*)c; }
53 inline const char*
CharCast(
const char* c) {
return c; }
54 inline 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 209 template<
typename Stream,
int N>
inline void Serialize(Stream& s,
const char (&a)[N]) { s.write(a, N); }
210 template<
typename Stream,
int N>
inline void Serialize(Stream& s,
const unsigned char (&a)[N]) { s.write(
CharCast(a), N); }
214 #ifndef CHAR_EQUALS_INT8 225 template<
typename Stream,
int N>
inline void Unserialize(Stream& s,
char (&a)[N]) { s.read(a, N); }
226 template<
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);
250 template<
typename Stream>
257 else if (
nSize <= std::numeric_limits<uint16_t>::max())
262 else if (
nSize <= std::numeric_limits<unsigned int>::max())
281 template<
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");
350 template <VarIntMode Mode,
typename I>
354 static_assert(Mode !=
VarIntMode::DEFAULT || std::is_unsigned<I>::value,
"Unsigned type required with mode DEFAULT.");
359 template<VarIntMode Mode,
typename I>
376 template<
typename Stream, VarIntMode Mode,
typename I>
380 unsigned char tmp[(
sizeof(n)*8+6)/7];
383 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
394 template<
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");
417 template<
typename Formatter,
typename T>
420 static_assert(std::is_lvalue_reference<T>::value,
"Wrapper needs an lvalue reference type T");
439 template<
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) 448 template<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);
471 template<
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));
507 template<
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);
530 template<
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)
564 template<
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());
606 template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str);
607 template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str);
624 template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&);
625 template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const bool&);
626 template<
typename Stream,
typename T,
typename A,
typename V>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const V&);
627 template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v);
628 template<
typename Stream,
typename T,
typename A>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const unsigned char&);
629 template<
typename Stream,
typename T,
typename A,
typename V>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const V&);
630 template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v);
635 template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item);
636 template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item);
641 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m);
642 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
647 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m);
648 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m);
653 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::shared_ptr<const T>& p);
654 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::shared_ptr<const T>& p);
659 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::unique_ptr<const T>& p);
660 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::unique_ptr<const T>& p);
667 template<
typename Stream,
typename T>
673 template<
typename Stream,
typename T>
686 template<
typename Stream,
typename T>
689 template<
typename Stream,
typename T>
700 template<
typename Stream,
typename C>
701 void Serialize(Stream& os,
const std::basic_string<C>& str)
705 os.write((
char*)str.data(), str.size() *
sizeof(C));
708 template<
typename Stream,
typename C>
714 is.read((
char*)str.data(), nSize *
sizeof(C));
722 template<
typename Stream,
unsigned int N,
typename T>
727 os.write((
char*)v.
data(), v.
size() *
sizeof(T));
730 template<
typename Stream,
unsigned int N,
typename T,
typename V>
736 template<
typename Stream,
unsigned int N,
typename T>
743 template<
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));
759 template<
typename Stream,
unsigned int N,
typename T,
typename V>
765 template<
typename Stream,
unsigned int N,
typename T>
776 template<
typename Stream,
typename T,
typename A>
777 void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&)
781 os.write((
char*)v.data(), v.size() *
sizeof(T));
784 template<
typename Stream,
typename T,
typename A>
791 for (
bool elem : v) {
796 template<
typename Stream,
typename T,
typename A,
typename V>
802 template<
typename Stream,
typename T,
typename A>
803 inline void Serialize(Stream& os,
const std::vector<T, A>& v)
809 template<
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));
825 template<
typename Stream,
typename T,
typename A,
typename V>
831 template<
typename Stream,
typename T,
typename A>
842 template<
typename Stream,
typename K,
typename T>
849 template<
typename Stream,
typename K,
typename T>
861 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
862 void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m)
865 for (
const auto& entry : m)
869 template<
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);
888 template<
typename Stream,
typename K,
typename Pred,
typename A>
889 void 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)
896 template<
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);
915 template<
typename Stream,
typename T>
void 916 Serialize(Stream& os,
const std::unique_ptr<const T>& p)
921 template<
typename Stream,
typename T>
932 template<
typename Stream,
typename T>
void 933 Serialize(Stream& os,
const std::shared_ptr<const T>& p)
938 template<
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;
1010 template<
typename Stream>
1015 template<
typename Stream,
typename Arg,
typename... Args>
1022 template<
typename Stream>
1027 template<
typename Stream,
typename Arg,
typename... Args>
1034 template<
typename Stream,
typename... Args>
1040 template<
typename Stream,
typename... Args>
1046 template<
typename Stream,
typename Type,
typename Fn>
1051 template<
typename Stream,
typename Type,
typename Fn>
1054 fn(s, std::forward<Type>(obj));
1057 template<
typename Stream,
typename Type,
typename Fn>
1060 fn(s, std::forward<Type>(obj));
1063 template<
typename Stream,
typename Type,
typename Fn>
1068 template<
typename I>
1071 s.
seek(GetSizeOfVarInt<I>(n));
1079 template <
typename T>
1085 template <
typename... T>
1093 #endif // BITCOIN_SERIALIZE_H CSizeComputer(int nVersionIn)
X & ReadWriteAsHelper(X &x)
Convert the reference base type to X, without changing constness or reference type.
uint8_t ser_readdata8(Stream &s)
void ser_writedata64(Stream &s, uint64_t obj)
void WriteVarInt(CSizeComputer &os, I n)
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
void SerRead(Stream &s, CSerActionSerialize ser_action, Type &&, Fn &&)
uint64_t htobe64(uint64_t host_64bits)
static const unsigned int MAX_VECTOR_ALLOCATE
Maximum amount of memory (in bytes) to allocate at once when deserializing vectors.
void ser_writedata32(Stream &s, uint32_t obj)
constexpr deserialize_type deserialize
constexpr std::size_t size() const noexcept
unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 ...
uint32_t be32toh(uint32_t big_endian_32bits)
void resize_uninitialized(size_type new_size)
void UnserializeMany(Stream &s)
Simple wrapper class to serialize objects using a formatter; used by Using().
uint32_t htole32(uint32_t host_32bits)
Dummy data type to identify deserializing constructors.
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class...
char * CharCast(char *c)
Safely convert odd char pointer types to standard ones.
size_t GetSerializeSize(const T &t, int nVersion=0)
void Serialize(Stream &s, char a)
uint32_t htobe32(uint32_t host_32bits)
void Serialize(Stream &s) const
CSizeComputer & operator<<(const T &obj)
uint64_t be64toh(uint64_t big_endian_64bits)
void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, const Args &... args)
uint16_t ser_readdata16(Stream &s)
uint32_t ser_readdata32(Stream &s)
constexpr CheckVarIntMode()
void ser_writedata16(Stream &s, uint16_t obj)
uint16_t htobe16(uint16_t host_16bits)
constexpr bool ForRead() const
VarIntMode
Variable-length integers: bytes are a MSB base-128 encoding of the number.
uint16_t le16toh(uint16_t little_endian_16bits)
void ser_writedata32be(Stream &s, uint32_t obj)
Support for SERIALIZE_METHODS and READWRITE macro.
constexpr bool ForRead() const
void SerializeMany(Stream &s)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
uint64_t ser_readdata64(Stream &s)
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
uint16_t htole16(uint16_t host_16bits)
uint16_t ser_readdata16be(Stream &s)
uint64_t le64toh(uint64_t little_endian_64bits)
uint16_t be16toh(uint16_t big_endian_16bits)
constexpr C * data() const noexcept
uint64_t htole64(uint64_t host_64bits)
void write(const char *psz, size_t _nSize)
uint32_t ser_readdata32be(Stream &s)
void SerWrite(Stream &s, CSerActionSerialize ser_action, Type &&obj, Fn &&fn)
unsigned int GetSizeOfVarInt(I n)
size_t GetSerializeSizeMany(int nVersion, const T &... t)
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...
void Unserialize(Stream &s, char &a)
void ser_writedata16be(Stream &s, uint16_t obj)
void Unserialize(Stream &s)
A Span is an object that can refer to a contiguous sequence of objects.
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 Unserialize_impl(Stream &is, prevector< N, T > &v, const unsigned char &)
uint32_t le32toh(uint32_t little_endian_32bits)
void ser_writedata8(Stream &s, uint8_t obj)