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, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
559 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
560 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
561 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
562 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
563 
567 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
568 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
569 
573 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
574 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
575 
579 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
580 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
581 
585 template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
586 template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
587 
591 template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
592 template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
593 
594 
595 
599 template<typename Stream, typename T>
600 inline void Serialize(Stream& os, const T& a)
601 {
602  a.Serialize(os);
603 }
604 
605 template<typename Stream, typename T>
606 inline void Unserialize(Stream& is, T&& a)
607 {
608  a.Unserialize(is);
609 }
610 
611 
612 
613 
614 
618 template<typename Stream, typename C>
619 void Serialize(Stream& os, const std::basic_string<C>& str)
620 {
621  WriteCompactSize(os, str.size());
622  if (!str.empty())
623  os.write((char*)str.data(), str.size() * sizeof(C));
624 }
625 
626 template<typename Stream, typename C>
627 void Unserialize(Stream& is, std::basic_string<C>& str)
628 {
629  unsigned int nSize = ReadCompactSize(is);
630  str.resize(nSize);
631  if (nSize != 0)
632  is.read((char*)str.data(), nSize * sizeof(C));
633 }
634 
635 
636 
640 template<typename Stream, unsigned int N, typename T>
641 void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
642 {
643  WriteCompactSize(os, v.size());
644  if (!v.empty())
645  os.write((char*)v.data(), v.size() * sizeof(T));
646 }
647 
648 template<typename Stream, unsigned int N, typename T, typename V>
649 void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
650 {
651  WriteCompactSize(os, v.size());
652  for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
653  ::Serialize(os, (*vi));
654 }
655 
656 template<typename Stream, unsigned int N, typename T>
657 inline void Serialize(Stream& os, const prevector<N, T>& v)
658 {
659  Serialize_impl(os, v, T());
660 }
661 
662 
663 template<typename Stream, unsigned int N, typename T>
664 void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
665 {
666  // Limit size per read so bogus size value won't cause out of memory
667  v.clear();
668  unsigned int nSize = ReadCompactSize(is);
669  unsigned int i = 0;
670  while (i < nSize)
671  {
672  unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
673  v.resize(i + blk);
674  is.read((char*)&v[i], blk * sizeof(T));
675  i += blk;
676  }
677 }
678 
679 template<typename Stream, unsigned int N, typename T, typename V>
680 void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
681 {
682  v.clear();
683  unsigned int nSize = ReadCompactSize(is);
684  unsigned int i = 0;
685  unsigned int nMid = 0;
686  while (nMid < nSize)
687  {
688  nMid += 5000000 / sizeof(T);
689  if (nMid > nSize)
690  nMid = nSize;
691  v.resize(nMid);
692  for (; i < nMid; i++)
693  Unserialize(is, v[i]);
694  }
695 }
696 
697 template<typename Stream, unsigned int N, typename T>
698 inline void Unserialize(Stream& is, prevector<N, T>& v)
699 {
700  Unserialize_impl(is, v, T());
701 }
702 
703 
704 
708 template<typename Stream, typename T, typename A>
709 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
710 {
711  WriteCompactSize(os, v.size());
712  if (!v.empty())
713  os.write((char*)v.data(), v.size() * sizeof(T));
714 }
715 
716 template<typename Stream, typename T, typename A, typename V>
717 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
718 {
719  WriteCompactSize(os, v.size());
720  for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
721  ::Serialize(os, (*vi));
722 }
723 
724 template<typename Stream, typename T, typename A>
725 inline void Serialize(Stream& os, const std::vector<T, A>& v)
726 {
727  Serialize_impl(os, v, T());
728 }
729 
730 
731 template<typename Stream, typename T, typename A>
732 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
733 {
734  // Limit size per read so bogus size value won't cause out of memory
735  v.clear();
736  unsigned int nSize = ReadCompactSize(is);
737  unsigned int i = 0;
738  while (i < nSize)
739  {
740  unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
741  v.resize(i + blk);
742  is.read((char*)&v[i], blk * sizeof(T));
743  i += blk;
744  }
745 }
746 
747 template<typename Stream, typename T, typename A, typename V>
748 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
749 {
750  v.clear();
751  unsigned int nSize = ReadCompactSize(is);
752  unsigned int i = 0;
753  unsigned int nMid = 0;
754  while (nMid < nSize)
755  {
756  nMid += 5000000 / sizeof(T);
757  if (nMid > nSize)
758  nMid = nSize;
759  v.resize(nMid);
760  for (; i < nMid; i++)
761  Unserialize(is, v[i]);
762  }
763 }
764 
765 template<typename Stream, typename T, typename A>
766 inline void Unserialize(Stream& is, std::vector<T, A>& v)
767 {
768  Unserialize_impl(is, v, T());
769 }
770 
771 
772 
776 template<typename Stream, typename K, typename T>
777 void Serialize(Stream& os, const std::pair<K, T>& item)
778 {
779  Serialize(os, item.first);
780  Serialize(os, item.second);
781 }
782 
783 template<typename Stream, typename K, typename T>
784 void Unserialize(Stream& is, std::pair<K, T>& item)
785 {
786  Unserialize(is, item.first);
787  Unserialize(is, item.second);
788 }
789 
790 
791 
795 template<typename Stream, typename K, typename T, typename Pred, typename A>
796 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
797 {
798  WriteCompactSize(os, m.size());
799  for (const auto& entry : m)
800  Serialize(os, entry);
801 }
802 
803 template<typename Stream, typename K, typename T, typename Pred, typename A>
804 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
805 {
806  m.clear();
807  unsigned int nSize = ReadCompactSize(is);
808  typename std::map<K, T, Pred, A>::iterator mi = m.begin();
809  for (unsigned int i = 0; i < nSize; i++)
810  {
811  std::pair<K, T> item;
812  Unserialize(is, item);
813  mi = m.insert(mi, item);
814  }
815 }
816 
817 
818 
822 template<typename Stream, typename K, typename Pred, typename A>
823 void Serialize(Stream& os, const std::set<K, Pred, A>& m)
824 {
825  WriteCompactSize(os, m.size());
826  for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
827  Serialize(os, (*it));
828 }
829 
830 template<typename Stream, typename K, typename Pred, typename A>
831 void Unserialize(Stream& is, std::set<K, Pred, A>& m)
832 {
833  m.clear();
834  unsigned int nSize = ReadCompactSize(is);
835  typename std::set<K, Pred, A>::iterator it = m.begin();
836  for (unsigned int i = 0; i < nSize; i++)
837  {
838  K key;
839  Unserialize(is, key);
840  it = m.insert(it, key);
841  }
842 }
843 
844 
845 
849 template<typename Stream, typename T> void
850 Serialize(Stream& os, const std::unique_ptr<const T>& p)
851 {
852  Serialize(os, *p);
853 }
854 
855 template<typename Stream, typename T>
856 void Unserialize(Stream& is, std::unique_ptr<const T>& p)
857 {
858  p.reset(new T(deserialize, is));
859 }
860 
861 
862 
866 template<typename Stream, typename T> void
867 Serialize(Stream& os, const std::shared_ptr<const T>& p)
868 {
869  Serialize(os, *p);
870 }
871 
872 template<typename Stream, typename T>
873 void Unserialize(Stream& is, std::shared_ptr<const T>& p)
874 {
875  p = std::make_shared<const T>(deserialize, is);
876 }
877 
878 
879 
884 {
885  constexpr bool ForRead() const { return false; }
886 };
888 {
889  constexpr bool ForRead() const { return true; }
890 };
891 
892 
893 
894 
895 
896 
897 
898 
899 /* ::GetSerializeSize implementations
900  *
901  * Computing the serialized size of objects is done through a special stream
902  * object of type CSizeComputer, which only records the number of bytes written
903  * to it.
904  *
905  * If your Serialize or SerializationOp method has non-trivial overhead for
906  * serialization, it may be worthwhile to implement a specialized version for
907  * CSizeComputer, which uses the s.seek() method to record bytes that would
908  * be written instead.
909  */
911 {
912 protected:
913  size_t nSize;
914 
915  const int nVersion;
916 public:
917  explicit CSizeComputer(int nVersionIn) : nSize(0), nVersion(nVersionIn) {}
918 
919  void write(const char *psz, size_t _nSize)
920  {
921  this->nSize += _nSize;
922  }
923 
925  void seek(size_t _nSize)
926  {
927  this->nSize += _nSize;
928  }
929 
930  template<typename T>
931  CSizeComputer& operator<<(const T& obj)
932  {
933  ::Serialize(*this, obj);
934  return (*this);
935  }
936 
937  size_t size() const {
938  return nSize;
939  }
940 
941  int GetVersion() const { return nVersion; }
942 };
943 
944 template<typename Stream>
945 void SerializeMany(Stream& s)
946 {
947 }
948 
949 template<typename Stream, typename Arg, typename... Args>
950 void SerializeMany(Stream& s, const Arg& arg, const Args&... args)
951 {
952  ::Serialize(s, arg);
953  ::SerializeMany(s, args...);
954 }
955 
956 template<typename Stream>
957 inline void UnserializeMany(Stream& s)
958 {
959 }
960 
961 template<typename Stream, typename Arg, typename... Args>
962 inline void UnserializeMany(Stream& s, Arg&& arg, Args&&... args)
963 {
964  ::Unserialize(s, arg);
965  ::UnserializeMany(s, args...);
966 }
967 
968 template<typename Stream, typename... Args>
969 inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, const Args&... args)
970 {
971  ::SerializeMany(s, args...);
972 }
973 
974 template<typename Stream, typename... Args>
975 inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&&... args)
976 {
977  ::UnserializeMany(s, args...);
978 }
979 
980 template<typename I>
981 inline void WriteVarInt(CSizeComputer &s, I n)
982 {
983  s.seek(GetSizeOfVarInt<I>(n));
984 }
985 
986 inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
987 {
988  s.seek(GetSizeOfCompactSize(nSize));
989 }
990 
991 template <typename T>
992 size_t GetSerializeSize(const T& t, int nVersion = 0)
993 {
994  return (CSizeComputer(nVersion) << t).size();
995 }
996 
997 template <typename... T>
998 size_t GetSerializeSizeMany(int nVersion, const T&... t)
999 {
1000  CSizeComputer sc(nVersion);
1001  SerializeMany(sc, t...);
1002  return sc.size();
1003 }
1004 
1005 #endif // BITCOIN_SERIALIZE_H
uint64_t ser_double_to_uint64(double x)
Definition: serialize.h:138
CSizeComputer(int nVersionIn)
Definition: serialize.h:917
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
void resize(size_type new_size)
Definition: prevector.h:311
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:981
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:986
size_t size() const
Definition: serialize.h:937
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
BigEndian(I &val)
Definition: serialize.h:460
const int nVersion
Definition: serialize.h:915
void UnserializeMany(Stream &s)
Definition: serialize.h:957
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:503
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:992
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:931
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:969
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:889
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:883
constexpr bool ForRead() const
Definition: serialize.h:885
void SerializeMany(Stream &s)
Definition: serialize.h:945
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:493
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:925
uint16_t htole16(uint16_t host_16bits)
Definition: endian.h:163
uint16_t ser_readdata16be(Stream &s)
Definition: serialize.h:114
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:941
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:919
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:998
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:641
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:664
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:913
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