6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
31 static constexpr uint64_t
MAX_SIZE = 0x02000000;
140 #define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
141 #define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj)))
142 #define SER_READ(obj, code) ::SerRead(s, ser_action, obj, [&](Stream& s, typename std::remove_const<Type>::type& obj) { code; })
143 #define SER_WRITE(obj, code) ::SerWrite(s, ser_action, obj, [&](Stream& s, const Type& obj) { code; })
161 #define FORMATTER_METHODS(cls, obj) \
162 template<typename Stream> \
163 static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, CSerActionSerialize()); } \
164 template<typename Stream> \
165 static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, CSerActionUnserialize()); } \
166 template<typename Stream, typename Type, typename Operation> \
167 static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \
176 #define SERIALIZE_METHODS(cls, obj) \
177 template<typename Stream> \
178 void Serialize(Stream& s) const \
180 static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
183 template<typename Stream> \
184 void Unserialize(Stream& s) \
186 static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
189 FORMATTER_METHODS(cls, obj)
191 #ifndef CHAR_EQUALS_INT8
192 template <
typename Stream>
void Serialize(Stream&,
char) =
delete;
203 template<
typename Stream,
int N>
inline void Serialize(Stream& s,
const unsigned char (&a)[N]) { s.write(
MakeByteSpan(a)); }
207 #ifndef CHAR_EQUALS_INT8
208 template <
typename Stream>
void Unserialize(Stream&,
char) =
delete;
235 if (
nSize < 253)
return sizeof(
unsigned char);
236 else if (
nSize <= std::numeric_limits<uint16_t>::max())
return sizeof(
unsigned char) +
sizeof(uint16_t);
237 else if (
nSize <= std::numeric_limits<unsigned int>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned int);
238 else return sizeof(
unsigned char) +
sizeof(uint64_t);
243 template<
typename Stream>
250 else if (
nSize <= std::numeric_limits<uint16_t>::max())
255 else if (
nSize <= std::numeric_limits<unsigned int>::max())
274 template<
typename Stream>
278 uint64_t nSizeRet = 0;
283 else if (chSize == 253)
287 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
289 else if (chSize == 254)
292 if (nSizeRet < 0x10000u)
293 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
298 if (nSizeRet < 0x100000000ULL)
299 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
301 if (range_check && nSizeRet >
MAX_SIZE) {
302 throw std::ios_base::failure(
"ReadCompactSize(): size too large");
343 template <VarIntMode Mode,
typename I>
347 static_assert(Mode !=
VarIntMode::DEFAULT || std::is_unsigned<I>::value,
"Unsigned type required with mode DEFAULT.");
352 template<VarIntMode Mode,
typename I>
369 template<
typename Stream, VarIntMode Mode,
typename I>
373 unsigned char tmp[(
sizeof(n)*8+6)/7];
376 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
387 template<
typename Stream, VarIntMode Mode,
typename I>
394 if (n > (std::numeric_limits<I>::max() >> 7)) {
395 throw std::ios_base::failure(
"ReadVarInt(): size too large");
397 n = (n << 7) | (chData & 0x7F);
399 if (n == std::numeric_limits<I>::max()) {
400 throw std::ios_base::failure(
"ReadVarInt(): size too large");
410 template<
typename Formatter,
typename T>
413 static_assert(std::is_lvalue_reference<T>::value,
"Wrapper needs an lvalue reference type T");
432 template<
typename Formatter,
typename T>
435 #define VARINT_MODE(obj, mode) Using<VarIntFormatter<mode>>(obj)
436 #define VARINT(obj) Using<VarIntFormatter<VarIntMode::DEFAULT>>(obj)
437 #define COMPACTSIZE(obj) Using<CompactSizeFormatter<true>>(obj)
438 #define LIMITED_STRING(obj,n) Using<LimitedStringFormatter<n>>(obj)
441 template<VarIntMode Mode>
444 template<
typename Stream,
typename I>
void Ser(Stream &s, I v)
446 WriteVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s, v);
449 template<
typename Stream,
typename I>
void Unser(Stream& s, I& v)
451 v = ReadVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s);
464 template<
int Bytes,
bool BigEndian = false>
467 static_assert(Bytes > 0 && Bytes <= 8,
"CustomUintFormatter Bytes out of range");
468 static constexpr uint64_t
MAX = 0xffffffffffffffff >> (8 * (8 - Bytes));
470 template <
typename Stream,
typename I>
void Ser(Stream& s, I v)
472 if (v < 0 || v >
MAX)
throw std::ios_base::failure(
"CustomUintFormatter value out of range");
475 s.write({
BytePtr(&raw) + 8 - Bytes, Bytes});
478 s.write({
BytePtr(&raw), Bytes});
482 template <
typename Stream,
typename I>
void Unser(Stream& s, I& v)
484 using U =
typename std::conditional<std::is_enum<I>::value, std::underlying_type<I>, std::common_type<I>>::type::type;
485 static_assert(std::numeric_limits<U>::max() >=
MAX && std::numeric_limits<U>::min() <= 0,
"Assigned type too small");
488 s.read({
BytePtr(&raw) + 8 - Bytes, Bytes});
489 v =
static_cast<I
>(
be64toh(raw));
491 s.read({
BytePtr(&raw), Bytes});
492 v =
static_cast<I
>(
le64toh(raw));
500 template<
bool RangeCheck>
503 template<
typename Stream,
typename I>
506 uint64_t n = ReadCompactSize<Stream>(s, RangeCheck);
507 if (n < std::numeric_limits<I>::min() || n > std::numeric_limits<I>::max()) {
508 throw std::ios_base::failure(
"CompactSize exceeds limit of type");
513 template<
typename Stream,
typename I>
516 static_assert(std::is_unsigned<I>::value,
"CompactSize only supported for unsigned integers");
517 static_assert(std::numeric_limits<I>::max() <= std::numeric_limits<uint64_t>::max(),
"CompactSize only supports 64-bit integers and below");
519 WriteCompactSize<Stream>(s, v);
530 template<
typename Stream>
532 WriteCompactSize<Stream>(s,
n);
536 template<
size_t Limit>
539 template<
typename Stream>
540 void Unser(Stream& s, std::string& v)
544 throw std::ios_base::failure(
"String length limit exceeded");
550 template<
typename Stream>
551 void Ser(Stream& s,
const std::string& v)
570 template<
class Formatter>
573 template<
typename Stream,
typename V>
574 void Ser(Stream& s,
const V& v)
578 for (
const typename V::value_type& elem : v) {
579 formatter.Ser(s, elem);
583 template<
typename Stream,
typename V>
589 size_t allocated = 0;
590 while (allocated < size) {
594 static_assert(
sizeof(
typename V::value_type) <=
MAX_VECTOR_ALLOCATE,
"Vector element size too large");
595 allocated = std::min(size, allocated +
MAX_VECTOR_ALLOCATE /
sizeof(
typename V::value_type));
596 v.reserve(allocated);
597 while (v.size() < allocated) {
599 formatter.Unser(s, v.back());
612 template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str);
613 template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str);
630 template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&);
631 template<
typename Stream,
typename T,
typename A>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const bool&);
632 template<
typename Stream,
typename T,
typename A,
typename V>
void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const V&);
633 template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v);
634 template<
typename Stream,
typename T,
typename A>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const unsigned char&);
635 template<
typename Stream,
typename T,
typename A,
typename V>
void Unserialize_impl(Stream& is, std::vector<T, A>& v,
const V&);
636 template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v);
641 template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item);
642 template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item);
647 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m);
648 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
653 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m);
654 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m);
659 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::shared_ptr<const T>& p);
660 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::shared_ptr<const T>& p);
665 template<
typename Stream,
typename T>
void Serialize(Stream& os,
const std::unique_ptr<const T>& p);
666 template<
typename Stream,
typename T>
void Unserialize(Stream& os, std::unique_ptr<const T>& p);
673 template<
typename Stream,
typename T>
679 template<
typename Stream,
typename T>
692 template<
typename Stream,
typename T>
695 template<
typename Stream,
typename T>
706 template<
typename Stream,
typename C>
707 void Serialize(Stream& os,
const std::basic_string<C>& str)
714 template<
typename Stream,
typename C>
728 template<
typename Stream,
unsigned int N,
typename T>
736 template<
typename Stream,
unsigned int N,
typename T,
typename V>
742 template<
typename Stream,
unsigned int N,
typename T>
749 template<
typename Stream,
unsigned int N,
typename T>
758 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
765 template<
typename Stream,
unsigned int N,
typename T,
typename V>
771 template<
typename Stream,
unsigned int N,
typename T>
782 template<
typename Stream,
typename T,
typename A>
783 void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
const unsigned char&)
790 template<
typename Stream,
typename T,
typename A>
797 for (
bool elem : v) {
802 template<
typename Stream,
typename T,
typename A,
typename V>
808 template<
typename Stream,
typename T,
typename A>
809 inline void Serialize(Stream& os,
const std::vector<T, A>& v)
815 template<
typename Stream,
typename T,
typename A>
824 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(
T)));
831 template<
typename Stream,
typename T,
typename A,
typename V>
837 template<
typename Stream,
typename T,
typename A>
848 template<
typename Stream,
typename K,
typename T>
855 template<
typename Stream,
typename K,
typename T>
867 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
868 void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m)
871 for (
const auto& entry :
m)
875 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
880 typename std::map<K, T, Pred, A>::iterator mi =
m.begin();
881 for (
unsigned int i = 0; i < nSize; i++)
883 std::pair<K, T> item;
885 mi =
m.insert(mi, item);
894 template<
typename Stream,
typename K,
typename Pred,
typename A>
895 void Serialize(Stream& os,
const std::set<K, Pred, A>& m)
898 for (
typename std::set<K, Pred, A>::const_iterator it =
m.begin(); it !=
m.end(); ++it)
902 template<
typename Stream,
typename K,
typename Pred,
typename A>
907 typename std::set<K, Pred, A>::iterator it =
m.begin();
908 for (
unsigned int i = 0; i < nSize; i++)
912 it =
m.insert(it, key);
921 template<
typename Stream,
typename T>
void
922 Serialize(Stream& os,
const std::unique_ptr<const T>& p)
927 template<
typename Stream,
typename T>
938 template<
typename Stream,
typename T>
void
939 Serialize(Stream& os,
const std::shared_ptr<const T>& p)
944 template<
typename Stream,
typename T>
957 constexpr
bool ForRead()
const {
return false; }
961 constexpr
bool ForRead()
const {
return true; }
993 this->nSize += src.
size();
999 this->nSize += _nSize;
1002 template<
typename T>
1016 template<
typename Stream>
1021 template<
typename Stream,
typename Arg,
typename... Args>
1028 template<
typename Stream>
1033 template<
typename Stream,
typename Arg,
typename... Args>
1040 template<
typename Stream,
typename... Args>
1046 template<
typename Stream,
typename... Args>
1052 template<
typename Stream,
typename Type,
typename Fn>
1057 template<
typename Stream,
typename Type,
typename Fn>
1060 fn(s, std::forward<Type>(obj));
1063 template<
typename Stream,
typename Type,
typename Fn>
1066 fn(s, std::forward<Type>(obj));
1069 template<
typename Stream,
typename Type,
typename Fn>
1074 template<
typename I>
1077 s.
seek(GetSizeOfVarInt<I>(n));
1085 template <
typename T>
1091 template <
typename...
T>
1099 #endif // BITCOIN_SERIALIZE_H