]> Git Repo - VerusCoin.git/blob - src/serialize.h
Replace http with https: in links to the MIT license.
[VerusCoin.git] / src / serialize.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://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 <array>
13 #include <assert.h>
14 #include <ios>
15 #include <limits>
16 #include <list>
17 #include <map>
18 #include <memory>
19 #include <set>
20 #include <stdint.h>
21 #include <string>
22 #include <string.h>
23 #include <utility>
24 #include <vector>
25
26 #include <boost/optional.hpp>
27
28 #include "prevector.h"
29
30 static const unsigned int MAX_SIZE = 0x02000000;
31
32 /**
33  * Dummy data type to identify deserializing constructors.
34  *
35  * By convention, a constructor of a type T with signature
36  *
37  *   template <typename Stream> T::T(deserialize_type, Stream& s)
38  *
39  * is a deserializing constructor, which builds the type by
40  * deserializing it from s. If T contains const fields, this
41  * is likely the only way to do so.
42  */
43 struct deserialize_type {};
44 constexpr deserialize_type deserialize {};
45
46 /**
47  * Used to bypass the rule against non-const reference to temporary
48  * where it makes sense with wrappers such as CFlatData or CTxDB
49  */
50 template<typename T>
51 inline T& REF(const T& val)
52 {
53     return const_cast<T&>(val);
54 }
55
56 /**
57  * Used to acquire a non-const pointer "this" to generate bodies
58  * of const serialization operations from a template
59  */
60 template<typename T>
61 inline T* NCONST_PTR(const T* val)
62 {
63     return const_cast<T*>(val);
64 }
65
66 /** 
67  * Get begin pointer of vector (non-const version).
68  * @note These functions avoid the undefined case of indexing into an empty
69  * vector, as well as that of indexing after the end of the vector.
70  */
71 template <typename V>
72 inline typename V::value_type* begin_ptr(V& v)
73 {
74     return v.empty() ? NULL : &v[0];
75 }
76 /** Get begin pointer of vector (const version) */
77 template <typename V>
78 inline const typename V::value_type* begin_ptr(const V& v)
79 {
80     return v.empty() ? NULL : &v[0];
81 }
82 /** Get end pointer of vector (non-const version) */
83 template <typename V>
84 inline typename V::value_type* end_ptr(V& v)
85 {
86     return v.empty() ? NULL : (&v[0] + v.size());
87 }
88 /** Get end pointer of vector (const version) */
89 template <typename V>
90 inline const typename V::value_type* end_ptr(const V& v)
91 {
92     return v.empty() ? NULL : (&v[0] + v.size());
93 }
94
95 /*
96  * Lowest-level serialization and conversion.
97  * @note Sizes of these types are verified in the tests
98  */
99 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
100 {
101     s.write((char*)&obj, 1);
102 }
103 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
104 {
105     obj = htole16(obj);
106     s.write((char*)&obj, 2);
107 }
108 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
109 {
110     obj = htole32(obj);
111     s.write((char*)&obj, 4);
112 }
113 template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
114 {
115     obj = htobe32(obj);
116     s.write((char*)&obj, 4);
117 }
118 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
119 {
120     obj = htole64(obj);
121     s.write((char*)&obj, 8);
122 }
123 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
124 {
125     uint8_t obj;
126     s.read((char*)&obj, 1);
127     return obj;
128 }
129 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
130 {
131     uint16_t obj;
132     s.read((char*)&obj, 2);
133     return le16toh(obj);
134 }
135 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
136 {
137     uint32_t obj;
138     s.read((char*)&obj, 4);
139     return le32toh(obj);
140 }
141 template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
142 {
143     uint32_t obj;
144     s.read((char*)&obj, 4);
145     return be32toh(obj);
146 }
147 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
148 {
149     uint64_t obj;
150     s.read((char*)&obj, 8);
151     return le64toh(obj);
152 }
153 inline uint64_t ser_double_to_uint64(double x)
154 {
155     union { double x; uint64_t y; } tmp;
156     tmp.x = x;
157     return tmp.y;
158 }
159 inline uint32_t ser_float_to_uint32(float x)
160 {
161     union { float x; uint32_t y; } tmp;
162     tmp.x = x;
163     return tmp.y;
164 }
165 inline double ser_uint64_to_double(uint64_t y)
166 {
167     union { double x; uint64_t y; } tmp;
168     tmp.y = y;
169     return tmp.x;
170 }
171 inline float ser_uint32_to_float(uint32_t y)
172 {
173     union { float x; uint32_t y; } tmp;
174     tmp.y = y;
175     return tmp.x;
176 }
177
178
179 /////////////////////////////////////////////////////////////////
180 //
181 // Templates for serializing to anything that looks like a stream,
182 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
183 //
184
185 class CSizeComputer;
186
187 enum
188 {
189     // primary actions
190     SER_NETWORK         = (1 << 0),
191     SER_DISK            = (1 << 1),
192     SER_GETHASH         = (1 << 2),
193 };
194
195 #define READWRITE(obj)      (::SerReadWrite(s, (obj), ser_action))
196 #define READWRITEMANY(...)      (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
197
198 /** 
199  * Implement three methods for serializable objects. These are actually wrappers over
200  * "SerializationOp" template, which implements the body of each class' serialization
201  * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
202  * added as members. 
203  */
204 #define ADD_SERIALIZE_METHODS                                         \
205     template<typename Stream>                                         \
206     void Serialize(Stream& s) const {                                 \
207         NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize());  \
208     }                                                                 \
209     template<typename Stream>                                         \
210     void Unserialize(Stream& s) {                                     \
211         SerializationOp(s, CSerActionUnserialize());                  \
212     }
213
214 template<typename Stream> inline void Serialize(Stream& s, char a    ) { ser_writedata8(s, a); } // TODO Get rid of bare char
215 template<typename Stream> inline void Serialize(Stream& s, int8_t a  ) { ser_writedata8(s, a); }
216 template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
217 template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
218 template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
219 template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
220 template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
221 template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
222 template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
223 template<typename Stream> inline void Serialize(Stream& s, float a   ) { ser_writedata32(s, ser_float_to_uint32(a)); }
224 template<typename Stream> inline void Serialize(Stream& s, double a  ) { ser_writedata64(s, ser_double_to_uint64(a)); }
225
226 template<typename Stream> inline void Unserialize(Stream& s, char& a    ) { a = ser_readdata8(s); } // TODO Get rid of bare char
227 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a  ) { a = ser_readdata8(s); }
228 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
229 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
230 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
231 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); }
232 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
233 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
234 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
235 template<typename Stream> inline void Unserialize(Stream& s, float& a   ) { a = ser_uint32_to_float(ser_readdata32(s)); }
236 template<typename Stream> inline void Unserialize(Stream& s, double& a  ) { a = ser_uint64_to_double(ser_readdata64(s)); }
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
246 /**
247  * Compact Size
248  * size <  253        -- 1 byte
249  * size <= 0xFFFF     -- 3 bytes  (253 + 2 bytes)
250  * size <= 0xFFFFFFFF -- 5 bytes  (254 + 4 bytes)
251  * size >  0xFFFFFFFF -- 9 bytes  (255 + 8 bytes)
252  */
253 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
254 {
255     if (nSize < 253)                return 1;
256     else if (nSize <= 0xFFFFu)      return 3;
257     else if (nSize <= 0xFFFFFFFFu)  return 5;
258     else                            return 9;
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 <= 0xFFFFu)
271     {
272         ser_writedata8(os, 253);
273         ser_writedata16(os, nSize);
274     }
275     else if (nSize <= 0xFFFFFFFFu)
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 }
286
287 template<typename Stream>
288 uint64_t ReadCompactSize(Stream& is)
289 {
290     uint8_t chSize = ser_readdata8(is);
291     uint64_t nSizeRet = 0;
292     if (chSize < 253)
293     {
294         nSizeRet = chSize;
295     }
296     else if (chSize == 253)
297     {
298         nSizeRet = ser_readdata16(is);
299         if (nSizeRet < 253)
300             throw std::ios_base::failure("non-canonical ReadCompactSize()");
301     }
302     else if (chSize == 254)
303     {
304         nSizeRet = ser_readdata32(is);
305         if (nSizeRet < 0x10000u)
306             throw std::ios_base::failure("non-canonical ReadCompactSize()");
307     }
308     else
309     {
310         nSizeRet = ser_readdata64(is);
311         if (nSizeRet < 0x100000000ULL)
312             throw std::ios_base::failure("non-canonical ReadCompactSize()");
313     }
314     if (nSizeRet > (uint64_t)MAX_SIZE)
315         throw std::ios_base::failure("ReadCompactSize(): size too large");
316     return nSizeRet;
317 }
318
319 /**
320  * Variable-length integers: bytes are a MSB base-128 encoding of the number.
321  * The high bit in each byte signifies whether another digit follows. To make
322  * sure the encoding is one-to-one, one is subtracted from all but the last digit.
323  * Thus, the byte sequence a[] with length len, where all but the last byte
324  * has bit 128 set, encodes the number:
325  * 
326  *  (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
327  * 
328  * Properties:
329  * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
330  * * Every integer has exactly one encoding
331  * * Encoding does not depend on size of original integer type
332  * * No redundancy: every (infinite) byte sequence corresponds to a list
333  *   of encoded integers.
334  * 
335  * 0:         [0x00]  256:        [0x81 0x00]
336  * 1:         [0x01]  16383:      [0xFE 0x7F]
337  * 127:       [0x7F]  16384:      [0xFF 0x00]
338  * 128:  [0x80 0x00]  16511: [0x80 0xFF 0x7F]
339  * 255:  [0x80 0x7F]  65535: [0x82 0xFD 0x7F]
340  * 2^32:           [0x8E 0xFE 0xFE 0xFF 0x00]
341  */
342
343 template<typename I>
344 inline unsigned int GetSizeOfVarInt(I n)
345 {
346     int nRet = 0;
347     while(true) {
348         nRet++;
349         if (n <= 0x7F)
350             break;
351         n = (n >> 7) - 1;
352     }
353     return nRet;
354 }
355
356 template<typename I>
357 inline void WriteVarInt(CSizeComputer& os, I n);
358
359 template<typename Stream, typename I>
360 void WriteVarInt(Stream& os, I n)
361 {
362     unsigned char tmp[(sizeof(n)*8+6)/7];
363     int len=0;
364     while(true) {
365         tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
366         if (n <= 0x7F)
367             break;
368         n = (n >> 7) - 1;
369         len++;
370     }
371     do {
372         ser_writedata8(os, tmp[len]);
373     } while(len--);
374 }
375
376 template<typename Stream, typename I>
377 I ReadVarInt(Stream& is)
378 {
379     I n = 0;
380     while(true) {
381         unsigned char chData = ser_readdata8(is);
382         n = (n << 7) | (chData & 0x7F);
383         if (chData & 0x80)
384             n++;
385         else
386             return n;
387     }
388 }
389
390 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
391 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
392 #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
393 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
394
395 /** 
396  * Wrapper for serializing arrays and POD.
397  */
398 class CFlatData
399 {
400 protected:
401     char* pbegin;
402     char* pend;
403 public:
404     CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
405     template <class T, class TAl>
406     explicit CFlatData(std::vector<T,TAl> &v)
407     {
408         pbegin = (char*)begin_ptr(v);
409         pend = (char*)end_ptr(v);
410     }
411     template <unsigned int N, typename T, typename S, typename D>
412     explicit CFlatData(prevector<N, T, S, D> &v)
413     {
414         pbegin = (char*)begin_ptr(v);
415         pend = (char*)end_ptr(v);
416     }
417     char* begin() { return pbegin; }
418     const char* begin() const { return pbegin; }
419     char* end() { return pend; }
420     const char* end() const { return pend; }
421
422     template<typename Stream>
423     void Serialize(Stream& s) const
424     {
425         s.write(pbegin, pend - pbegin);
426     }
427
428     template<typename Stream>
429     void Unserialize(Stream& s)
430     {
431         s.read(pbegin, pend - pbegin);
432     }
433 };
434
435 template<typename I>
436 class CVarInt
437 {
438 protected:
439     I &n;
440 public:
441     CVarInt(I& nIn) : n(nIn) { }
442
443     template<typename Stream>
444     void Serialize(Stream &s) const {
445         WriteVarInt<Stream,I>(s, n);
446     }
447
448     template<typename Stream>
449     void Unserialize(Stream& s) {
450         n = ReadVarInt<Stream,I>(s);
451     }
452 };
453
454 class CCompactSize
455 {
456 protected:
457     uint64_t &n;
458 public:
459     CCompactSize(uint64_t& nIn) : n(nIn) { }
460
461     template<typename Stream>
462     void Serialize(Stream &s) const {
463         WriteCompactSize<Stream>(s, n);
464     }
465
466     template<typename Stream>
467     void Unserialize(Stream& s) {
468         n = ReadCompactSize<Stream>(s);
469     }
470 };
471
472 template<size_t Limit>
473 class LimitedString
474 {
475 protected:
476     std::string& string;
477 public:
478     LimitedString(std::string& _string) : string(_string) {}
479
480     template<typename Stream>
481     void Unserialize(Stream& s)
482     {
483         size_t size = ReadCompactSize(s);
484         if (size > Limit) {
485             throw std::ios_base::failure("String length limit exceeded");
486         }
487         string.resize(size);
488         if (size != 0)
489             s.read((char*)&string[0], size);
490     }
491
492     template<typename Stream>
493     void Serialize(Stream& s) const
494     {
495         WriteCompactSize(s, string.size());
496         if (!string.empty())
497             s.write((char*)&string[0], string.size());
498     }
499 };
500
501 template<typename I>
502 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
503
504 /**
505  * Forward declarations
506  */
507
508 /**
509  *  string
510  */
511 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
512 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
513
514 /**
515  * prevector
516  * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
517  */
518 template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
519 template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
520 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
521 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
522 template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
523 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
524
525 /**
526  * vector
527  * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
528  */
529 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
530 template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
531 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
532 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
533 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
534 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
535
536 /**
537  * optional
538  */
539 template<typename Stream, typename T> void Serialize(Stream& os, const boost::optional<T>& item);
540 template<typename Stream, typename T> void Unserialize(Stream& is, boost::optional<T>& item);
541
542 /**
543  * array
544  */
545 template<typename Stream, typename T, std::size_t N> void Serialize(Stream& os, const std::array<T, N>& item);
546 template<typename Stream, typename T, std::size_t N> void Unserialize(Stream& is, std::array<T, N>& item);
547
548 /**
549  * pair
550  */
551 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
552 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
553
554 /**
555  * map
556  */
557 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
558 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
559
560 /**
561  * set
562  */
563 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
564 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
565
566 /**
567  * list
568  */
569 template<typename Stream, typename T, typename A> void Serialize(Stream& os, const std::list<T, A>& m);
570 template<typename Stream, typename T, typename A> void Unserialize(Stream& is, std::list<T, A>& m);
571
572 /**
573  * shared_ptr
574  */
575 template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
576 template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
577
578 /**
579  * unique_ptr
580  */
581 template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
582 template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
583
584
585
586 /**
587  * If none of the specialized versions above matched, default to calling member function.
588  */
589 template<typename Stream, typename T>
590 inline void Serialize(Stream& os, const T& a)
591 {
592     a.Serialize(os);
593 }
594
595 template<typename Stream, typename T>
596 inline void Unserialize(Stream& is, T& a)
597 {
598     a.Unserialize(is);
599 }
600
601
602
603
604
605 /**
606  * string
607  */
608 template<typename Stream, typename C>
609 void Serialize(Stream& os, const std::basic_string<C>& str)
610 {
611     WriteCompactSize(os, str.size());
612     if (!str.empty())
613         os.write((char*)&str[0], str.size() * sizeof(str[0]));
614 }
615
616 template<typename Stream, typename C>
617 void Unserialize(Stream& is, std::basic_string<C>& str)
618 {
619     unsigned int nSize = ReadCompactSize(is);
620     str.resize(nSize);
621     if (nSize != 0)
622         is.read((char*)&str[0], nSize * sizeof(str[0]));
623 }
624
625
626
627 /**
628  * prevector
629  */
630 template<typename Stream, unsigned int N, typename T>
631 void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
632 {
633     WriteCompactSize(os, v.size());
634     if (!v.empty())
635         os.write((char*)&v[0], v.size() * sizeof(T));
636 }
637
638 template<typename Stream, unsigned int N, typename T, typename V>
639 void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
640 {
641     WriteCompactSize(os, v.size());
642     for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
643         ::Serialize(os, (*vi));
644 }
645
646 template<typename Stream, unsigned int N, typename T>
647 inline void Serialize(Stream& os, const prevector<N, T>& v)
648 {
649     Serialize_impl(os, v, T());
650 }
651
652
653 template<typename Stream, unsigned int N, typename T>
654 void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
655 {
656     // Limit size per read so bogus size value won't cause out of memory
657     v.clear();
658     unsigned int nSize = ReadCompactSize(is);
659     unsigned int i = 0;
660     while (i < nSize)
661     {
662         unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
663         v.resize(i + blk);
664         is.read((char*)&v[i], blk * sizeof(T));
665         i += blk;
666     }
667 }
668
669 template<typename Stream, unsigned int N, typename T, typename V>
670 void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
671 {
672     v.clear();
673     unsigned int nSize = ReadCompactSize(is);
674     unsigned int i = 0;
675     unsigned int nMid = 0;
676     while (nMid < nSize)
677     {
678         nMid += 5000000 / sizeof(T);
679         if (nMid > nSize)
680             nMid = nSize;
681         v.resize(nMid);
682         for (; i < nMid; i++)
683             Unserialize(is, v[i]);
684     }
685 }
686
687 template<typename Stream, unsigned int N, typename T>
688 inline void Unserialize(Stream& is, prevector<N, T>& v)
689 {
690     Unserialize_impl(is, v, T());
691 }
692
693
694
695 /**
696  * vector
697  */
698 template<typename Stream, typename T, typename A>
699 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
700 {
701     WriteCompactSize(os, v.size());
702     if (!v.empty())
703         os.write((char*)&v[0], v.size() * sizeof(T));
704 }
705
706 template<typename Stream, typename T, typename A, typename V>
707 void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
708 {
709     WriteCompactSize(os, v.size());
710     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
711         ::Serialize(os, (*vi));
712 }
713
714 template<typename Stream, typename T, typename A>
715 inline void Serialize(Stream& os, const std::vector<T, A>& v)
716 {
717     Serialize_impl(os, v, T());
718 }
719
720
721 template<typename Stream, typename T, typename A>
722 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
723 {
724     // Limit size per read so bogus size value won't cause out of memory
725     v.clear();
726     unsigned int nSize = ReadCompactSize(is);
727     unsigned int i = 0;
728     while (i < nSize)
729     {
730         unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
731         v.resize(i + blk);
732         is.read((char*)&v[i], blk * sizeof(T));
733         i += blk;
734     }
735 }
736
737 template<typename Stream, typename T, typename A, typename V>
738 void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
739 {
740     v.clear();
741     unsigned int nSize = ReadCompactSize(is);
742     unsigned int i = 0;
743     unsigned int nMid = 0;
744     while (nMid < nSize)
745     {
746         nMid += 5000000 / sizeof(T);
747         if (nMid > nSize)
748             nMid = nSize;
749         v.resize(nMid);
750         for (; i < nMid; i++)
751             Unserialize(is, v[i]);
752     }
753 }
754
755 template<typename Stream, typename T, typename A>
756 inline void Unserialize(Stream& is, std::vector<T, A>& v)
757 {
758     Unserialize_impl(is, v, T());
759 }
760
761
762
763 /**
764  * optional
765  */
766 template<typename Stream, typename T>
767 void Serialize(Stream& os, const boost::optional<T>& item)
768 {
769     // If the value is there, put 0x01 and then serialize the value.
770     // If it's not, put 0x00.
771     if (item) {
772         unsigned char discriminant = 0x01;
773         Serialize(os, discriminant);
774         Serialize(os, *item);
775     } else {
776         unsigned char discriminant = 0x00;
777         Serialize(os, discriminant);
778     }
779 }
780
781 template<typename Stream, typename T>
782 void Unserialize(Stream& is, boost::optional<T>& item)
783 {
784     unsigned char discriminant = 0x00;
785     Unserialize(is, discriminant);
786
787     if (discriminant == 0x00) {
788         item = boost::none;
789     } else if (discriminant == 0x01) {
790         T object;
791         Unserialize(is, object);
792         item = object;
793     } else {
794         throw std::ios_base::failure("non-canonical optional discriminant");
795     }
796 }
797
798
799
800 /**
801  * array
802  */
803 template<typename Stream, typename T, std::size_t N>
804 void Serialize(Stream& os, const std::array<T, N>& item)
805 {
806     for (size_t i = 0; i < N; i++) {
807         Serialize(os, item[i]);
808     }
809 }
810
811 template<typename Stream, typename T, std::size_t N>
812 void Unserialize(Stream& is, std::array<T, N>& item)
813 {
814     for (size_t i = 0; i < N; i++) {
815         Unserialize(is, item[i]);
816     }
817 }
818
819
820 /**
821  * pair
822  */
823 template<typename Stream, typename K, typename T>
824 void Serialize(Stream& os, const std::pair<K, T>& item)
825 {
826     Serialize(os, item.first);
827     Serialize(os, item.second);
828 }
829
830 template<typename Stream, typename K, typename T>
831 void Unserialize(Stream& is, std::pair<K, T>& item)
832 {
833     Unserialize(is, item.first);
834     Unserialize(is, item.second);
835 }
836
837
838
839 /**
840  * map
841  */
842 template<typename Stream, typename K, typename T, typename Pred, typename A>
843 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
844 {
845     WriteCompactSize(os, m.size());
846     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
847         Serialize(os, (*mi));
848 }
849
850 template<typename Stream, typename K, typename T, typename Pred, typename A>
851 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
852 {
853     m.clear();
854     unsigned int nSize = ReadCompactSize(is);
855     typename std::map<K, T, Pred, A>::iterator mi = m.begin();
856     for (unsigned int i = 0; i < nSize; i++)
857     {
858         std::pair<K, T> item;
859         Unserialize(is, item);
860         mi = m.insert(mi, item);
861     }
862 }
863
864
865
866 /**
867  * set
868  */
869 template<typename Stream, typename K, typename Pred, typename A>
870 void Serialize(Stream& os, const std::set<K, Pred, A>& m)
871 {
872     WriteCompactSize(os, m.size());
873     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
874         Serialize(os, (*it));
875 }
876
877 template<typename Stream, typename K, typename Pred, typename A>
878 void Unserialize(Stream& is, std::set<K, Pred, A>& m)
879 {
880     m.clear();
881     unsigned int nSize = ReadCompactSize(is);
882     typename std::set<K, Pred, A>::iterator it = m.begin();
883     for (unsigned int i = 0; i < nSize; i++)
884     {
885         K key;
886         Unserialize(is, key);
887         it = m.insert(it, key);
888     }
889 }
890
891
892
893 /**
894  * list
895  */
896 template<typename Stream, typename T, typename A>
897 void Serialize(Stream& os, const std::list<T, A>& l)
898 {
899     WriteCompactSize(os, l.size());
900     for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
901         Serialize(os, (*it));
902 }
903
904 template<typename Stream, typename T, typename A>
905 void Unserialize(Stream& is, std::list<T, A>& l)
906 {
907     l.clear();
908     unsigned int nSize = ReadCompactSize(is);
909     typename std::list<T, A>::iterator it = l.begin();
910     for (unsigned int i = 0; i < nSize; i++)
911     {
912         T item;
913         Unserialize(is, item);
914         l.push_back(item);
915     }
916 }
917
918
919
920 /**
921  * unique_ptr
922  */
923 template<typename Stream, typename T> void
924 Serialize(Stream& os, const std::unique_ptr<const T>& p)
925 {
926     Serialize(os, *p);
927 }
928
929 template<typename Stream, typename T>
930 void Unserialize(Stream& is, std::unique_ptr<const T>& p)
931 {
932     p.reset(new T(deserialize, is));
933 }
934
935
936
937 /**
938  * shared_ptr
939  */
940 template<typename Stream, typename T> void
941 Serialize(Stream& os, const std::shared_ptr<const T>& p)
942 {
943     Serialize(os, *p);
944 }
945
946 template<typename Stream, typename T>
947 void Unserialize(Stream& is, std::shared_ptr<const T>& p)
948 {
949     p = std::make_shared<const T>(deserialize, is);
950 }
951
952
953
954 /**
955  * Support for ADD_SERIALIZE_METHODS and READWRITE macro
956  */
957 struct CSerActionSerialize
958 {
959     constexpr bool ForRead() const { return false; }
960 };
961 struct CSerActionUnserialize
962 {
963     constexpr bool ForRead() const { return true; }
964 };
965
966 template<typename Stream, typename T>
967 inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action)
968 {
969     ::Serialize(s, obj);
970 }
971
972 template<typename Stream, typename T>
973 inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action)
974 {
975     ::Unserialize(s, obj);
976 }
977
978
979
980
981
982
983
984
985
986 /* ::GetSerializeSize implementations
987  *
988  * Computing the serialized size of objects is done through a special stream
989  * object of type CSizeComputer, which only records the number of bytes written
990  * to it.
991  *
992  * If your Serialize or SerializationOp method has non-trivial overhead for
993  * serialization, it may be worthwhile to implement a specialized version for
994  * CSizeComputer, which uses the s.seek() method to record bytes that would
995  * be written instead.
996  */
997 class CSizeComputer
998 {
999 protected:
1000     size_t nSize;
1001
1002     const int nType;
1003     const int nVersion;
1004 public:
1005     CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
1006
1007     void write(const char *psz, size_t _nSize)
1008     {
1009         this->nSize += _nSize;
1010     }
1011
1012     /** Pretend _nSize bytes are written, without specifying them. */
1013     void seek(size_t _nSize)
1014     {
1015         this->nSize += _nSize;
1016     }
1017
1018     template<typename T>
1019     CSizeComputer& operator<<(const T& obj)
1020     {
1021         ::Serialize(*this, obj);
1022         return (*this);
1023     }
1024
1025     size_t size() const {
1026         return nSize;
1027     }
1028
1029     int GetVersion() const { return nVersion; }
1030     int GetType() const { return nType; }
1031 };
1032
1033 template<typename Stream>
1034 void SerializeMany(Stream& s)
1035 {
1036 }
1037
1038 template<typename Stream, typename Arg>
1039 void SerializeMany(Stream& s, Arg&& arg)
1040 {
1041     ::Serialize(s, std::forward<Arg>(arg));
1042 }
1043
1044 template<typename Stream, typename Arg, typename... Args>
1045 void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
1046 {
1047     ::Serialize(s, std::forward<Arg>(arg));
1048     ::SerializeMany(s, std::forward<Args>(args)...);
1049 }
1050
1051 template<typename Stream>
1052 inline void UnserializeMany(Stream& s)
1053 {
1054 }
1055
1056 template<typename Stream, typename Arg>
1057 inline void UnserializeMany(Stream& s, Arg& arg)
1058 {
1059     ::Unserialize(s, arg);
1060 }
1061
1062 template<typename Stream, typename Arg, typename... Args>
1063 inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
1064 {
1065     ::Unserialize(s, arg);
1066     ::UnserializeMany(s, args...);
1067 }
1068
1069 template<typename Stream, typename... Args>
1070 inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
1071 {
1072     ::SerializeMany(s, std::forward<Args>(args)...);
1073 }
1074
1075 template<typename Stream, typename... Args>
1076 inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
1077 {
1078     ::UnserializeMany(s, args...);
1079 }
1080
1081 template<typename I>
1082 inline void WriteVarInt(CSizeComputer &s, I n)
1083 {
1084     s.seek(GetSizeOfVarInt<I>(n));
1085 }
1086
1087 inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
1088 {
1089     s.seek(GetSizeOfCompactSize(nSize));
1090 }
1091
1092 template <typename T>
1093 size_t GetSerializeSize(const T& t, int nType, int nVersion = 0)
1094 {
1095     return (CSizeComputer(nType, nVersion) << t).size();
1096 }
1097
1098 template <typename S, typename T>
1099 size_t GetSerializeSize(const S& s, const T& t)
1100 {
1101     return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
1102 }
1103
1104 #endif // BITCOIN_SERIALIZE_H
This page took 0.08773 seconds and 4 git commands to generate.