Bitcoin Core  0.18.99
P2P Digital Currency
serialize.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
8 
9 #include <compat/endian.h>
10 
11 #include <algorithm>
12 #include <assert.h>
13 #include <ios>
14 #include <limits>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <stdint.h>
19 #include <string>
20 #include <string.h>
21 #include <utility>
22 #include <vector>
23 
24 #include <prevector.h>
25 #include <span.h>
26 
27 static const unsigned int MAX_SIZE = 0x02000000;
28 
40 struct deserialize_type {};
42 
47 template<typename T>
48 inline T& REF(const T& val)
49 {
50  return const_cast<T&>(val);
51 }
52 
57 template<typename T>
58 inline T* NCONST_PTR(const T* val)
59 {
60  return const_cast<T*>(val);
61 }
62 
64 inline char* CharCast(char* c) { return c; }
65 inline char* CharCast(unsigned char* c) { return (char*)c; }
66 inline const char* CharCast(const char* c) { return c; }
67 inline const char* CharCast(const unsigned char* c) { return (const char*)c; }
68 
69 /*
70  * Lowest-level serialization and conversion.
71  * @note Sizes of these types are verified in the tests
72  */
73 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
74 {
75  s.write((char*)&obj, 1);
76 }
77 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
78 {
79  obj = htole16(obj);
80  s.write((char*)&obj, 2);
81 }
82 template<typename Stream> inline void ser_writedata16be(Stream &s, uint16_t obj)
83 {
84  obj = htobe16(obj);
85  s.write((char*)&obj, 2);
86 }
87 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
88 {
89  obj = htole32(obj);
90  s.write((char*)&obj, 4);
91 }
92 template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
93 {
94  obj = htobe32(obj);
95  s.write((char*)&obj, 4);
96 }
97 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
98 {
99  obj = htole64(obj);
100  s.write((char*)&obj, 8);
101 }
102 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
103 {
104  uint8_t obj;
105  s.read((char*)&obj, 1);
106  return obj;
107 }
108 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
109 {
110  uint16_t obj;
111  s.read((char*)&obj, 2);
112  return le16toh(obj);
113 }
114 template<typename Stream> inline uint16_t ser_readdata16be(Stream &s)
115 {
116  uint16_t obj;
117  s.read((char*)&obj, 2);
118  return be16toh(obj);
119 }
120 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
121 {
122  uint32_t obj;
123  s.read((char*)&obj, 4);
124  return le32toh(obj);
125 }
126 template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
127 {
128  uint32_t obj;
129  s.read((char*)&obj, 4);
130  return be32toh(obj);
131 }
132 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
133 {
134  uint64_t obj;
135  s.read((char*)&obj, 8);
136  return le64toh(obj);
137 }
138 inline uint64_t ser_double_to_uint64(double x)
139 {
140  union { double x; uint64_t y; } tmp;
141  tmp.x = x;
142  return tmp.y;
143 }
144 inline uint32_t ser_float_to_uint32(float x)
145 {
146  union { float x; uint32_t y; } tmp;
147  tmp.x = x;
148  return tmp.y;
149 }
150 inline double ser_uint64_to_double(uint64_t y)
151 {
152  union { double x; uint64_t y; } tmp;
153  tmp.y = y;
154  return tmp.x;
155 }
156 inline float ser_uint32_to_float(uint32_t y)
157 {
158  union { float x; uint32_t y; } tmp;
159  tmp.y = y;
160  return tmp.x;
161 }
162 
163 
165 //
166 // Templates for serializing to anything that looks like a stream,
167 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
168 //
169 
170 class CSizeComputer;
171 
172 enum
173 {
174  // primary actions
175  SER_NETWORK = (1 << 0),
176  SER_DISK = (1 << 1),
177  SER_GETHASH = (1 << 2),
178 };
179 
181 template<typename X> X& ReadWriteAsHelper(X& x) { return x; }
182 template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
183 
184 #define READWRITE(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
185 #define READWRITEAS(type, obj) (::SerReadWriteMany(s, ser_action, ReadWriteAsHelper<type>(obj)))
186 
193 #define ADD_SERIALIZE_METHODS \
194  template<typename Stream> \
195  void Serialize(Stream& s) const { \
196  NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
197  } \
198  template<typename Stream> \
199  void Unserialize(Stream& s) { \
200  SerializationOp(s, CSerActionUnserialize()); \
201  }
202 
203 #ifndef CHAR_EQUALS_INT8
204 template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
205 #endif
206 template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
207 template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
208 template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
209 template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
210 template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
211 template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
212 template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
213 template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
214 template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); }
215 template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
216 template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); }
217 template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); }
218 template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
219 template<typename Stream> inline void Serialize(Stream& s, const Span<unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
220 
221 #ifndef CHAR_EQUALS_INT8
222 template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
223 #endif
224 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
225 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
226 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
227 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
228 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); }
229 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
230 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
231 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
232 template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); }
233 template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
234 template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); }
235 template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); }
236 template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); }
237 
238 template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
239 template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
240 
241 
242 
243 
244 
245 
253 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
254 {
255  if (nSize < 253) return sizeof(unsigned char);
256  else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
257  else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
258  else return sizeof(unsigned char) + sizeof(uint64_t);
259 }
260 
261 inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
262 
263 template<typename Stream>
264 void WriteCompactSize(Stream& os, uint64_t nSize)
265 {
266  if (nSize < 253)
267  {
268  ser_writedata8(os, nSize);
269  }
270  else if (nSize <= std::numeric_limits<unsigned short>::max())
271  {
272  ser_writedata8(os, 253);
273  ser_writedata16(os, nSize);
274  }
275  else if (nSize <= std::numeric_limits<unsigned int>::max())
276  {
277  ser_writedata8(os, 254);
278  ser_writedata32(os, nSize);
279  }
280  else
281  {
282  ser_writedata8(os, 255);
283  ser_writedata64(os, nSize);
284  }
285  return;
286 }
287 
288 template<typename Stream>
289 uint64_t ReadCompactSize(Stream& is)
290 {
291  uint8_t chSize = ser_readdata8(is);
292  uint64_t nSizeRet = 0;
293  if (chSize < 253)
294  {
295  nSizeRet = chSize;
296  }
297  else if (chSize == 253)
298  {
299  nSizeRet = ser_readdata16(is);
300  if (nSizeRet < 253)
301  throw std::ios_base::failure("non-canonical ReadCompactSize()");
302  }
303  else if (chSize == 254)
304  {
305  nSizeRet = ser_readdata32(is);
306  if (nSizeRet < 0x10000u)
307  throw std::ios_base::failure("non-canonical ReadCompactSize()");
308  }
309  else
310  {
311  nSizeRet = ser_readdata64(is);
312  if (nSizeRet < 0x100000000ULL)
313  throw std::ios_base::failure("non-canonical ReadCompactSize()");
314  }
315  if (nSizeRet > (uint64_t)MAX_SIZE)
316  throw std::ios_base::failure("ReadCompactSize(): size too large");
317  return nSizeRet;
318 }
319 
355 
356 template <VarIntMode Mode, typename I>
358  constexpr CheckVarIntMode()
359  {
360  static_assert(Mode != VarIntMode::DEFAULT || std::is_unsigned<I>::value, "Unsigned type required with mode DEFAULT.");
361  static_assert(Mode != VarIntMode::NONNEGATIVE_SIGNED || std::is_signed<I>::value, "Signed type required with mode NONNEGATIVE_SIGNED.");
362  }
363 };
364 
365 template<VarIntMode Mode, typename I>
366 inline unsigned int GetSizeOfVarInt(I n)
367 {
369  int nRet = 0;
370  while(true) {
371  nRet++;
372  if (n <= 0x7F)
373  break;
374  n = (n >> 7) - 1;
375  }
376  return nRet;
377 }
378 
379 template<typename I>
380 inline void WriteVarInt(CSizeComputer& os, I n);
381 
382 template<typename Stream, VarIntMode Mode, typename I>
383 void WriteVarInt(Stream& os, I n)
384 {
386  unsigned char tmp[(sizeof(n)*8+6)/7];
387  int len=0;
388  while(true) {
389  tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
390  if (n <= 0x7F)
391  break;
392  n = (n >> 7) - 1;
393  len++;
394  }
395  do {
396  ser_writedata8(os, tmp[len]);
397  } while(len--);
398 }
399 
400 template<typename Stream, VarIntMode Mode, typename I>
401 I ReadVarInt(Stream& is)
402 {
404  I n = 0;
405  while(true) {
406  unsigned char chData = ser_readdata8(is);
407  if (n > (std::numeric_limits<I>::max() >> 7)) {
408  throw std::ios_base::failure("ReadVarInt(): size too large");
409  }
410  n = (n << 7) | (chData & 0x7F);
411  if (chData & 0x80) {
412  if (n == std::numeric_limits<I>::max()) {
413  throw std::ios_base::failure("ReadVarInt(): size too large");
414  }
415  n++;
416  } else {
417  return n;
418  }
419  }
420 }
421 
422 #define VARINT(obj, ...) WrapVarInt<__VA_ARGS__>(REF(obj))
423 #define COMPACTSIZE(obj) CCompactSize(REF(obj))
424 #define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
425 
426 template<VarIntMode Mode, typename I>
427 class CVarInt
428 {
429 protected:
430  I &n;
431 public:
432  explicit CVarInt(I& nIn) : n(nIn) { }
433 
434  template<typename Stream>
435  void Serialize(Stream &s) const {
436  WriteVarInt<Stream,Mode,I>(s, n);
437  }
438 
439  template<typename Stream>
440  void Unserialize(Stream& s) {
441  n = ReadVarInt<Stream,Mode,I>(s);
442  }
443 };
444 
454 template<typename I>
456 {
457 protected:
458  I& m_val;
459 public:
460  explicit BigEndian(I& val) : m_val(val)
461  {
462  static_assert(std::is_unsigned<I>::value, "BigEndian type must be unsigned integer");
463  static_assert(sizeof(I) == 2 && std::numeric_limits<I>::min() == 0 && std::numeric_limits<I>::max() == std::numeric_limits<uint16_t>::max(), "Unsupported BigEndian size");
464  }
465 
466  template<typename Stream>
467  void Serialize(Stream& s) const
468  {
469  ser_writedata16be(s, m_val);
470  }
471 
472  template<typename Stream>
473  void Unserialize(Stream& s)
474  {
475  m_val = ser_readdata16be(s);
476  }
477 };
478 
480 {
481 protected:
482  uint64_t &n;
483 public:
484  explicit CCompactSize(uint64_t& nIn) : n(nIn) { }
485 
486  template<typename Stream>
487  void Serialize(Stream &s) const {
488  WriteCompactSize<Stream>(s, n);
489  }
490 
491  template<typename Stream>
492  void Unserialize(Stream& s) {
493  n = ReadCompactSize<Stream>(s);
494  }
495 };
496 
497 template<size_t Limit>
499 {
500 protected:
501  std::string& string;
502 public:
503  explicit LimitedString(std::string& _string) : string(_string) {}
504 
505  template<typename Stream>
506  void Unserialize(Stream& s)
507  {
508  size_t size = ReadCompactSize(s);
509  if (size > Limit) {
510  throw std::ios_base::failure("String length limit exceeded");
511  }
512  string.resize(size);
513  if (size != 0)
514  s.read((char*)string.data(), size);
515  }
516 
517  template<typename Stream>
518  void Serialize(Stream& s) const
519  {
520  WriteCompactSize(s, string.size());
521  if (!string.empty())
522  s.write((char*)string.data(), string.size());
523  }
524 };
525 
526 template<VarIntMode Mode=VarIntMode::DEFAULT, typename I>
528 
529 template<typename I>
531 
539 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
540 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
541 
546 template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
547 template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
548 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
549 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
550 template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
551 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
552 
557 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
558 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&);
559 template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
560 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
561 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
562 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
563 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
564 
568 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
569 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
570 
574 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
575 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
576 
580 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
581 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
582 
586 template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
587 template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
588 
592 template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
593 template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
594 
595 
596 
600 template<typename Stream, typename T>
601 inline void Serialize(Stream& os, const T& a)
602 {
603  a.Serialize(os);
604 }
605 
606 template<typename Stream, typename T>
607 inline void Unserialize(Stream& is, T&& a)
608 {
609  a.Unserialize(is);
610 }
611 
612 
613 
614 
615 
619 template<typename Stream, typename C>
620 void Serialize(Stream& os, const std::basic_string<C>& str)
621 {
622  WriteCompactSize(os, str.size());
623  if (!str.empty())
624  os.write((char*)str.data(), str.size() * sizeof(C));
625 }
626 
627 template<typename Stream, typename C>
628 void Unserialize(Stream& is, std::basic_string<C>& str)
629 {
630  unsigned int nSize = ReadCompactSize(is);
631  str.resize(nSize);
632  if (nSize != 0)
633  is.read((char*)str.data(), nSize * sizeof(C));
634 }
635 
636 
637 
641 template<typename Stream, unsigned int N, typename T>
642 void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
643 {
644  WriteCompactSize(os, v.size());
645  if (!v.empty())
646  os.write((char*)v.data(), v.size() * sizeof(T));
647 }
648 
649 template<typename Stream, unsigned int N, typename T, typename V>
650 void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
651 {
652  WriteCompactSize(os, v.size());
653  for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
654  ::Serialize(os, (*vi));
655 }
656 
657 template<typename Stream, unsigned int N, typename T>
658 inline void Serialize(Stream& os, const prevector<N, T>& v)
659 {
660  Serialize_impl(os, v, T());
661 }
662 
663 
664 template<typename Stream, unsigned int N, typename T>
665 void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
666 {
667  // Limit size per read so bogus size value won't cause out of memory
668  v.clear();
669  unsigned int nSize = ReadCompactSize(is);
670  unsigned int i = 0;
671  while (i < nSize)
672  {
673  unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
674  v.resize_uninitialized(i + blk);
675  is.read((char*)&v[i], blk * sizeof(T));
676  i += blk;
677  }
678 }
679 
680 template<typename Stream, unsigned int N, typename T, typename V>
681 void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
682 {
683  v.clear();
684  unsigned int nSize = ReadCompactSize(is);
685  unsigned int i = 0;
686  unsigned int nMid = 0;
687  while (nMid < nSize)
688  {
689  nMid += 5000000 / sizeof(T);
690  if (nMid > nSize)
691  nMid = nSize;
692  v.resize_uninitialized(nMid);
693  for (; i < nMid; ++i)
694  Unserialize(is, v[i]);
695  }
696 }
697 
698 template<typename Stream, unsigned int N, typename T>
699 inline void Unserialize(Stream& is, prevector<N, T>& v)
700 {
701  Unserialize_impl(is, v, T());
702 }
703 
704 
705 
709 template<typename Stream, typename T, typename A>
710 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
711 {
712  WriteCompactSize(os, v.size());
713  if (!v.empty())
714  os.write((char*)v.data(), v.size() * sizeof(T));
715 }
716 
717 template<typename Stream, typename T, typename A>
718 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&)
719 {
720  // A special case for std::vector<bool>, as dereferencing
721  // std::vector<bool>::const_iterator does not result in a const bool&
722  // due to std::vector's special casing for bool arguments.
723  WriteCompactSize(os, v.size());
724  for (bool elem : v) {
725  ::Serialize(os, elem);
726  }
727 }
728 
729 template<typename Stream, typename T, typename A, typename V>
730 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
731 {
732  WriteCompactSize(os, v.size());
733  for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
734  ::Serialize(os, (*vi));
735 }
736 
737 template<typename Stream, typename T, typename A>
738 inline void Serialize(Stream& os, const std::vector<T, A>& v)
739 {
740  Serialize_impl(os, v, T());
741 }
742 
743 
744 template<typename Stream, typename T, typename A>
745 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
746 {
747  // Limit size per read so bogus size value won't cause out of memory
748  v.clear();
749  unsigned int nSize = ReadCompactSize(is);
750  unsigned int i = 0;
751  while (i < nSize)
752  {
753  unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
754  v.resize(i + blk);
755  is.read((char*)&v[i], blk * sizeof(T));
756  i += blk;
757  }
758 }
759 
760 template<typename Stream, typename T, typename A, typename V>
761 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
762 {
763  v.clear();
764  unsigned int nSize = ReadCompactSize(is);
765  unsigned int i = 0;
766  unsigned int nMid = 0;
767  while (nMid < nSize)
768  {
769  nMid += 5000000 / sizeof(T);
770  if (nMid > nSize)
771  nMid = nSize;
772  v.resize(nMid);
773  for (; i < nMid; i++)
774  Unserialize(is, v[i]);
775  }
776 }
777 
778 template<typename Stream, typename T, typename A>
779 inline void Unserialize(Stream& is, std::vector<T, A>& v)
780 {
781  Unserialize_impl(is, v, T());
782 }
783 
784 
785 
789 template<typename Stream, typename K, typename T>
790 void Serialize(Stream& os, const std::pair<K, T>& item)
791 {
792  Serialize(os, item.first);
793  Serialize(os, item.second);
794 }
795 
796 template<typename Stream, typename K, typename T>
797 void Unserialize(Stream& is, std::pair<K, T>& item)
798 {
799  Unserialize(is, item.first);
800  Unserialize(is, item.second);
801 }
802 
803 
804 
808 template<typename Stream, typename K, typename T, typename Pred, typename A>
809 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
810 {
811  WriteCompactSize(os, m.size());
812  for (const auto& entry : m)
813  Serialize(os, entry);
814 }
815 
816 template<typename Stream, typename K, typename T, typename Pred, typename A>
817 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
818 {
819  m.clear();
820  unsigned int nSize = ReadCompactSize(is);
821  typename std::map<K, T, Pred, A>::iterator mi = m.begin();
822  for (unsigned int i = 0; i < nSize; i++)
823  {
824  std::pair<K, T> item;
825  Unserialize(is, item);
826  mi = m.insert(mi, item);
827  }
828 }
829 
830 
831 
835 template<typename Stream, typename K, typename Pred, typename A>
836 void Serialize(Stream& os, const std::set<K, Pred, A>& m)
837 {
838  WriteCompactSize(os, m.size());
839  for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
840  Serialize(os, (*it));
841 }
842 
843 template<typename Stream, typename K, typename Pred, typename A>
844 void Unserialize(Stream& is, std::set<K, Pred, A>& m)
845 {
846  m.clear();
847  unsigned int nSize = ReadCompactSize(is);
848  typename std::set<K, Pred, A>::iterator it = m.begin();
849  for (unsigned int i = 0; i < nSize; i++)
850  {
851  K key;
852  Unserialize(is, key);
853  it = m.insert(it, key);
854  }
855 }
856 
857 
858 
862 template<typename Stream, typename T> void
863 Serialize(Stream& os, const std::unique_ptr<const T>& p)
864 {
865  Serialize(os, *p);
866 }
867 
868 template<typename Stream, typename T>
869 void Unserialize(Stream& is, std::unique_ptr<const T>& p)
870 {
871  p.reset(new T(deserialize, is));
872 }
873 
874 
875 
879 template<typename Stream, typename T> void
880 Serialize(Stream& os, const std::shared_ptr<const T>& p)
881 {
882  Serialize(os, *p);
883 }
884 
885 template<typename Stream, typename T>
886 void Unserialize(Stream& is, std::shared_ptr<const T>& p)
887 {
888  p = std::make_shared<const T>(deserialize, is);
889 }
890 
891 
892 
897 {
898  constexpr bool ForRead() const { return false; }
899 };
901 {
902  constexpr bool ForRead() const { return true; }
903 };
904 
905 
906 
907 
908 
909 
910 
911 
912 /* ::GetSerializeSize implementations
913  *
914  * Computing the serialized size of objects is done through a special stream
915  * object of type CSizeComputer, which only records the number of bytes written
916  * to it.
917  *
918  * If your Serialize or SerializationOp method has non-trivial overhead for
919  * serialization, it may be worthwhile to implement a specialized version for
920  * CSizeComputer, which uses the s.seek() method to record bytes that would
921  * be written instead.
922  */
924 {
925 protected:
926  size_t nSize;
927 
928  const int nVersion;
929 public:
930  explicit CSizeComputer(int nVersionIn) : nSize(0), nVersion(nVersionIn) {}
931 
932  void write(const char *psz, size_t _nSize)
933  {
934  this->nSize += _nSize;
935  }
936 
938  void seek(size_t _nSize)
939  {
940  this->nSize += _nSize;
941  }
942 
943  template<typename T>
944  CSizeComputer& operator<<(const T& obj)
945  {
946  ::Serialize(*this, obj);
947  return (*this);
948  }
949 
950  size_t size() const {
951  return nSize;
952  }
953 
954  int GetVersion() const { return nVersion; }
955 };
956 
957 template<typename Stream>
958 void SerializeMany(Stream& s)
959 {
960 }
961 
962 template<typename Stream, typename Arg, typename... Args>
963 void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
964 {
965  ::Serialize(s, arg);
966  ::SerializeMany(s, args...);
967 }
968 
969 template<typename Stream>
970 inline void UnserializeMany(Stream& s)
971 {
972 }
973 
974 template<typename Stream, typename Arg, typename... Args>
975 inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
976 {
977  ::Unserialize(s, arg);
978  ::UnserializeMany(s, args...);
979 }
980 
981 template<typename Stream, typename... Args>
982 inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, const Args&... args)
983 {
984  ::SerializeMany(s, args...);
985 }
986 
987 template<typename Stream, typename... Args>
988 inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&... args)
989 {
990  ::UnserializeMany(s, args...);
991 }
992 
993 template<typename I>
994 inline void WriteVarInt(CSizeComputer &s, I n)
995 {
996  s.seek(GetSizeOfVarInt<I>(n));
997 }
998 
999 inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
1000 {
1001  s.seek(GetSizeOfCompactSize(nSize));
1002 }
1003 
1004 template <typename T>
1005 size_t GetSerializeSize(const T& t, int nVersion = 0)
1006 {
1007  return (CSizeComputer(nVersion) << t).size();
1008 }
1009 
1010 template <typename... T>
1011 size_t GetSerializeSizeMany(int nVersion, const T&... t)
1012 {
1013  CSizeComputer sc(nVersion);
1014  SerializeMany(sc, t...);
1015  return sc.size();
1016 }
1017 
1018 #endif // BITCOIN_SERIALIZE_H
uint64_t ser_double_to_uint64(double x)
Definition: serialize.h:138
CSizeComputer(int nVersionIn)
Definition: serialize.h:930
std::string & string
Definition: serialize.h:501
X & ReadWriteAsHelper(X &x)
Convert the reference base type to X, without changing constness or reference type.
Definition: serialize.h:181
void Unserialize(Stream &s)
Definition: serialize.h:440
constexpr std::ptrdiff_t size() const noexcept
Definition: span.h:30
uint32_t ser_float_to_uint32(float x)
Definition: serialize.h:144
uint8_t ser_readdata8(Stream &s)
Definition: serialize.h:102
void clear()
Definition: prevector.h:338
void ser_writedata64(Stream &s, uint64_t obj)
Definition: serialize.h:97
uint64_t & n
Definition: serialize.h:482
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:289
void WriteVarInt(CSizeComputer &os, I n)
Definition: serialize.h:994
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:999
size_t size() const
Definition: serialize.h:950
void ser_writedata32(Stream &s, uint32_t obj)
Definition: serialize.h:87
constexpr deserialize_type deserialize
Definition: serialize.h:41
unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 ...
Definition: serialize.h:253
CCompactSize(uint64_t &nIn)
Definition: serialize.h:484
void Unserialize(Stream &s)
Definition: serialize.h:492
uint32_t be32toh(uint32_t big_endian_32bits)
Definition: endian.h:198
void resize_uninitialized(size_type new_size)
Definition: prevector.h:381
BigEndian(I &val)
Definition: serialize.h:460
const int nVersion
Definition: serialize.h:928
void UnserializeMany(Stream &s)
Definition: serialize.h:970
BigEndian< I > WrapBigEndian(I &n)
Definition: serialize.h:530
uint32_t htole32(uint32_t host_32bits)
Definition: endian.h:191
Dummy data type to identify deserializing constructors.
Definition: serialize.h:40
char * CharCast(char *c)
Safely convert odd char pointer types to standard ones.
Definition: serialize.h:64
value_type * data()
Definition: prevector.h:518
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1005
void Serialize(Stream &s, char a)
Definition: serialize.h:204
uint32_t htobe32(uint32_t host_32bits)
Definition: endian.h:184
CVarInt< Mode, I > WrapVarInt(I &n)
Definition: serialize.h:527
void Unserialize(Stream &s)
Definition: serialize.h:506
CSizeComputer & operator<<(const T &obj)
Definition: serialize.h:944
iterator end()
Definition: prevector.h:287
void Serialize(Stream &s) const
Definition: serialize.h:518
void SerReadWriteMany(Stream &s, CSerActionSerialize ser_action, const Args &... args)
Definition: serialize.h:982
uint16_t ser_readdata16(Stream &s)
Definition: serialize.h:108
uint32_t ser_readdata32(Stream &s)
Definition: serialize.h:120
void Serialize(Stream &s) const
Definition: serialize.h:487
Serialization wrapper class for big-endian integers.
Definition: serialize.h:455
constexpr CheckVarIntMode()
Definition: serialize.h:358
void ser_writedata16(Stream &s, uint16_t obj)
Definition: serialize.h:77
uint16_t htobe16(uint16_t host_16bits)
Definition: endian.h:156
constexpr bool ForRead() const
Definition: serialize.h:902
I & n
Definition: serialize.h:430
VarIntMode
Variable-length integers: bytes are a MSB base-128 encoding of the number.
Definition: serialize.h:354
I ReadVarInt(Stream &is)
Definition: serialize.h:401
uint16_t le16toh(uint16_t little_endian_16bits)
Definition: endian.h:177
void ser_writedata32be(Stream &s, uint32_t obj)
Definition: serialize.h:92
double ser_uint64_to_double(uint64_t y)
Definition: serialize.h:150
Support for ADD_SERIALIZE_METHODS and READWRITE macro.
Definition: serialize.h:896
constexpr bool ForRead() const
Definition: serialize.h:898
void SerializeMany(Stream &s)
Definition: serialize.h:958
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
uint64_t ser_readdata64(Stream &s)
Definition: serialize.h:132
CVarInt(I &nIn)
Definition: serialize.h:432
#define X(name)
Definition: net.cpp:495
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:938
uint16_t htole16(uint16_t host_16bits)
Definition: endian.h:163
uint16_t ser_readdata16be(Stream &s)
Definition: serialize.h:114
static const unsigned int MAX_SIZE
Definition: serialize.h:27
uint64_t le64toh(uint64_t little_endian_64bits)
Definition: endian.h:233
uint16_t be16toh(uint16_t big_endian_16bits)
Definition: endian.h:170
constexpr C * data() const noexcept
Definition: span.h:27
int GetVersion() const
Definition: serialize.h:954
void Unserialize(Stream &s)
Definition: serialize.h:473
bool empty() const
Definition: prevector.h:281
uint64_t htole64(uint64_t host_64bits)
Definition: endian.h:219
void write(const char *psz, size_t _nSize)
Definition: serialize.h:932
void Serialize(Stream &s) const
Definition: serialize.h:435
uint32_t ser_readdata32be(Stream &s)
Definition: serialize.h:126
unsigned int GetSizeOfVarInt(I n)
Definition: serialize.h:366
size_t GetSerializeSizeMany(int nVersion, const T &... t)
Definition: serialize.h:1011
I & m_val
Definition: serialize.h:458
void Unserialize(Stream &s, char &a)
Definition: serialize.h:222
iterator begin()
Definition: prevector.h:285
void ser_writedata16be(Stream &s, uint16_t obj)
Definition: serialize.h:82
size_type size() const
Definition: prevector.h:277
void Serialize(Stream &s) const
Definition: serialize.h:467
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:17
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...
Definition: serialize.h:642
T * NCONST_PTR(const T *val)
Used to acquire a non-const pointer "this" to generate bodies of const serialization operations from ...
Definition: serialize.h:58
void Unserialize_impl(Stream &is, prevector< N, T > &v, const unsigned char &)
Definition: serialize.h:665
auto it
Definition: validation.cpp:366
uint32_t le32toh(uint32_t little_endian_32bits)
Definition: endian.h:205
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers...
Definition: serialize.h:48
size_t nSize
Definition: serialize.h:926
LimitedString(std::string &_string)
Definition: serialize.h:503
void ser_writedata8(Stream &s, uint8_t obj)
Definition: serialize.h:73
float ser_uint32_to_float(uint32_t y)
Definition: serialize.h:156