6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
26 static const unsigned int MAX_SIZE = 0x02000000;
47 inline T&
REF(
const T& val)
49 return const_cast<T&
>(val);
59 return const_cast<T*
>(val);
68 s.write((
char*)&obj, 1);
73 s.write((
char*)&obj, 2);
78 s.write((
char*)&obj, 4);
83 s.write((
char*)&obj, 8);
88 s.read((
char*)&obj, 1);
94 s.read((
char*)&obj, 2);
100 s.read((
char*)&obj, 4);
106 s.read((
char*)&obj, 8);
111 union {
double x; uint64_t y; } tmp;
117 union {
float x; uint32_t y; } tmp;
123 union {
double x; uint64_t y; } tmp;
129 union {
float x; uint32_t y; } tmp;
151 #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
152 #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
160 #define ADD_SERIALIZE_METHODS \
161 template<typename Stream> \
162 void Serialize(Stream& s) const { \
163 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
165 template<typename Stream> \
166 void Unserialize(Stream& s) { \
167 SerializationOp(s, CSerActionUnserialize()); \
211 if (
nSize < 253)
return sizeof(
unsigned char);
212 else if (
nSize <= std::numeric_limits<unsigned short>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned short);
213 else if (
nSize <= std::numeric_limits<unsigned int>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned int);
214 else return sizeof(
unsigned char) +
sizeof(uint64_t);
219 template<
typename Stream>
226 else if (
nSize <= std::numeric_limits<unsigned short>::max())
231 else if (
nSize <= std::numeric_limits<unsigned int>::max())
244 template<
typename Stream>
248 uint64_t nSizeRet = 0;
253 else if (chSize == 253)
257 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
259 else if (chSize == 254)
262 if (nSizeRet < 0x10000u)
263 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
268 if (nSizeRet < 0x100000000ULL)
269 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
271 if (nSizeRet > (uint64_t)MAX_SIZE)
272 throw std::ios_base::failure(
"ReadCompactSize(): size too large");
316 template<
typename Stream,
typename I>
319 unsigned char tmp[(
sizeof(n)*8+6)/7];
322 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
333 template<
typename Stream,
typename I>
339 n = (n << 7) | (chData & 0x7F);
347 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
348 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
349 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
350 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
362 template <
class T,
class TAl>
366 pend = (
char*)(v.data() + v.size());
368 template <
unsigned int N,
typename T,
typename S,
typename D>
379 template<
typename Stream>
385 template<
typename Stream>
400 template<
typename Stream>
402 WriteVarInt<Stream,I>(s,
n);
405 template<
typename Stream>
407 n = ReadVarInt<Stream,I>(s);
418 template<
typename Stream>
420 WriteCompactSize<Stream>(s,
n);
423 template<
typename Stream>
425 n = ReadCompactSize<Stream>(s);
429 template<
size_t Limit>
437 template<
typename Stream>
442 throw std::ios_base::failure(
"String length limit exceeded");
446 s.read((
char*)&
string[0], size);
449 template<
typename Stream>
454 s.write((
char*)&
string[0],
string.size());
468 template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str);
469 template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str);
486 template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&);
487 template<
typename Stream,
typename T,
typename A,
typename V>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const V&);
488 template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v);
489 template<
typename Stream,
typename T,
typename A>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const unsigned char&);
490 template<
typename Stream,
typename T,
typename A,
typename V>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const V&);
491 template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v);
496 template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item);
497 template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item);
502 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m);
503 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
508 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m);
509 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m);
514 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::shared_ptr<const T>& p);
515 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::shared_ptr<const T>& p);
520 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::unique_ptr<const T>& p);
521 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::unique_ptr<const T>& p);
528 template<
typename Stream,
typename T>
534 template<
typename Stream,
typename T>
547 template<
typename Stream,
typename C>
548 void Serialize(Stream& os,
const std::basic_string<C>& str)
552 os.write((
char*)&str[0], str.size() *
sizeof(str[0]));
555 template<
typename Stream,
typename C>
561 is.read((
char*)&str[0], nSize *
sizeof(str[0]));
569 template<
typename Stream,
unsigned int N,
typename T>
574 os.write((
char*)&v[0], v.
size() *
sizeof(T));
577 template<
typename Stream,
unsigned int N,
typename T,
typename V>
585 template<
typename Stream,
unsigned int N,
typename T>
592 template<
typename Stream,
unsigned int N,
typename T>
601 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(T)));
603 is.read((
char*)&v[i], blk *
sizeof(T));
608 template<
typename Stream,
unsigned int N,
typename T,
typename V>
614 unsigned int nMid = 0;
617 nMid += 5000000 /
sizeof(T);
621 for (; i < nMid; i++)
626 template<
typename Stream,
unsigned int N,
typename T>
637 template<
typename Stream,
typename T,
typename A>
638 void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&)
642 os.write((
char*)&v[0], v.size() *
sizeof(T));
645 template<
typename Stream,
typename T,
typename A,
typename V>
649 for (
typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
653 template<
typename Stream,
typename T,
typename A>
654 inline void Serialize(Stream& os,
const std::vector<T, A>& v)
660 template<
typename Stream,
typename T,
typename A>
669 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(T)));
671 is.read((
char*)&v[i], blk *
sizeof(T));
676 template<
typename Stream,
typename T,
typename A,
typename V>
682 unsigned int nMid = 0;
685 nMid += 5000000 /
sizeof(T);
689 for (; i < nMid; i++)
694 template<
typename Stream,
typename T,
typename A>
705 template<
typename Stream,
typename K,
typename T>
712 template<
typename Stream,
typename K,
typename T>
724 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
725 void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m)
728 for (
typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
732 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
737 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
738 for (
unsigned int i = 0; i < nSize; i++)
740 std::pair<K, T> item;
742 mi = m.insert(mi, item);
751 template<
typename Stream,
typename K,
typename Pred,
typename A>
752 void Serialize(Stream& os,
const std::set<K, Pred, A>& m)
755 for (
typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
759 template<
typename Stream,
typename K,
typename Pred,
typename A>
764 typename std::set<K, Pred, A>::iterator it = m.begin();
765 for (
unsigned int i = 0; i < nSize; i++)
769 it = m.insert(it, key);
778 template<
typename Stream,
typename T>
void
779 Serialize(Stream& os,
const std::unique_ptr<const T>& p)
784 template<
typename Stream,
typename T>
795 template<
typename Stream,
typename T>
void
796 Serialize(Stream& os,
const std::shared_ptr<const T>& p)
801 template<
typename Stream,
typename T>
814 constexpr
bool ForRead()
const {
return false; }
818 constexpr
bool ForRead()
const {
return true; }
821 template<
typename Stream,
typename T>
827 template<
typename Stream,
typename T>
862 void write(
const char *psz,
size_t _nSize)
864 this->nSize += _nSize;
870 this->nSize += _nSize;
888 template<
typename Stream>
893 template<
typename Stream,
typename Arg>
899 template<
typename Stream,
typename Arg,
typename... Args>
906 template<
typename Stream>
911 template<
typename Stream,
typename Arg>
917 template<
typename Stream,
typename Arg,
typename... Args>
924 template<
typename Stream,
typename... Args>
930 template<
typename Stream,
typename... Args>
939 s.
seek(GetSizeOfVarInt<I>(n));
947 template <
typename T>
953 template <
typename S,
typename T>
956 return (
CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
void Unserialize(Stream &s)
void Serialize(Stream &s) const
CCompactSize(uint64_t &nIn)
Wrapper for serializing arrays and POD.
void Unserialize(Stream &s)
CFlatData(prevector< N, T, S, D > &v)
const char * begin() const
void Serialize(Stream &s) const
CFlatData(std::vector< T, TAl > &v)
CFlatData(void *pbeginIn, void *pendIn)
CSizeComputer & operator<<(const T &obj)
void write(const char *psz, size_t _nSize)
CSizeComputer(int nTypeIn, int nVersionIn)
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
void Unserialize(Stream &s)
void Serialize(Stream &s) const
void Serialize(Stream &s) const
LimitedString(std::string &_string)
void Unserialize(Stream &s)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
void resize(size_type new_size)
uint32_t le32toh(uint32_t little_endian_32bits)
uint16_t le16toh(uint16_t little_endian_16bits)
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)
void SerReadWrite(Stream &s, const T &obj, CSerActionSerialize ser_action)
void SerializeMany(Stream &s)
uint8_t ser_readdata8(Stream &s)
float ser_uint32_to_float(uint32_t y)
void WriteVarInt(CSizeComputer &os, I n)
void ser_writedata32(Stream &s, uint32_t obj)
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)
CVarInt< I > WrapVarInt(I &n)
uint32_t ser_float_to_uint32(float x)
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 &)
void Unserialize(Stream &s, char &a)
void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, Args &&... args)
uint16_t ser_readdata16(Stream &s)
uint64_t ser_readdata64(Stream &s)
double ser_uint64_to_double(uint64_t y)
void ser_writedata8(Stream &s, uint8_t obj)
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers s...
uint32_t ser_readdata32(Stream &s)
void UnserializeMany(Stream &s)
uint64_t ser_double_to_uint64(double x)
uint64_t ReadCompactSize(Stream &is)
T * NCONST_PTR(const T *val)
Used to acquire a non-const pointer "this" to generate bodies of const serialization operations from ...
void ser_writedata64(Stream &s, uint64_t obj)
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
unsigned int GetSizeOfVarInt(I n)
Variable-length integers: bytes are a MSB base-128 encoding of the number.
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Support for ADD_SERIALIZE_METHODS and READWRITE macro.
constexpr bool ForRead() const
constexpr bool ForRead() const
Dummy data type to identify deserializing constructors.