]> Git Repo - VerusCoin.git/blob - src/serialize.h
Update libsecp256k1
[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 http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
8
9 #include <algorithm>
10 #include <assert.h>
11 #include <ios>
12 #include <limits>
13 #include <map>
14 #include <set>
15 #include <stdint.h>
16 #include <string>
17 #include <string.h>
18 #include <utility>
19 #include <vector>
20
21 #include "compat/endian.h"
22
23 class CScript;
24
25 static const unsigned int MAX_SIZE = 0x02000000;
26
27 /**
28  * Used to bypass the rule against non-const reference to temporary
29  * where it makes sense with wrappers such as CFlatData or CTxDB
30  */
31 template<typename T>
32 inline T& REF(const T& val)
33 {
34     return const_cast<T&>(val);
35 }
36
37 /**
38  * Used to acquire a non-const pointer "this" to generate bodies
39  * of const serialization operations from a template
40  */
41 template<typename T>
42 inline T* NCONST_PTR(const T* val)
43 {
44     return const_cast<T*>(val);
45 }
46
47 /** 
48  * Get begin pointer of vector (non-const version).
49  * @note These functions avoid the undefined case of indexing into an empty
50  * vector, as well as that of indexing after the end of the vector.
51  */
52 template <class T, class TAl>
53 inline T* begin_ptr(std::vector<T,TAl>& v)
54 {
55     return v.empty() ? NULL : &v[0];
56 }
57 /** Get begin pointer of vector (const version) */
58 template <class T, class TAl>
59 inline const T* begin_ptr(const std::vector<T,TAl>& v)
60 {
61     return v.empty() ? NULL : &v[0];
62 }
63 /** Get end pointer of vector (non-const version) */
64 template <class T, class TAl>
65 inline T* end_ptr(std::vector<T,TAl>& v)
66 {
67     return v.empty() ? NULL : (&v[0] + v.size());
68 }
69 /** Get end pointer of vector (const version) */
70 template <class T, class TAl>
71 inline const T* end_ptr(const std::vector<T,TAl>& v)
72 {
73     return v.empty() ? NULL : (&v[0] + v.size());
74 }
75
76 /*
77  * Lowest-level serialization and conversion.
78  * @note Sizes of these types are verified in the tests
79  */
80 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
81 {
82     s.write((char*)&obj, 1);
83 }
84 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
85 {
86     obj = htole16(obj);
87     s.write((char*)&obj, 2);
88 }
89 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
90 {
91     obj = htole32(obj);
92     s.write((char*)&obj, 4);
93 }
94 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
95 {
96     obj = htole64(obj);
97     s.write((char*)&obj, 8);
98 }
99 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
100 {
101     uint8_t obj;
102     s.read((char*)&obj, 1);
103     return obj;
104 }
105 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
106 {
107     uint16_t obj;
108     s.read((char*)&obj, 2);
109     return le16toh(obj);
110 }
111 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
112 {
113     uint32_t obj;
114     s.read((char*)&obj, 4);
115     return le32toh(obj);
116 }
117 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
118 {
119     uint64_t obj;
120     s.read((char*)&obj, 8);
121     return le64toh(obj);
122 }
123 inline uint64_t ser_double_to_uint64(double x)
124 {
125     union { double x; uint64_t y; } tmp;
126     tmp.x = x;
127     return tmp.y;
128 }
129 inline uint32_t ser_float_to_uint32(float x)
130 {
131     union { float x; uint32_t y; } tmp;
132     tmp.x = x;
133     return tmp.y;
134 }
135 inline double ser_uint64_to_double(uint64_t y)
136 {
137     union { double x; uint64_t y; } tmp;
138     tmp.y = y;
139     return tmp.x;
140 }
141 inline float ser_uint32_to_float(uint32_t y)
142 {
143     union { float x; uint32_t y; } tmp;
144     tmp.y = y;
145     return tmp.x;
146 }
147
148
149 /////////////////////////////////////////////////////////////////
150 //
151 // Templates for serializing to anything that looks like a stream,
152 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
153 //
154
155 enum
156 {
157     // primary actions
158     SER_NETWORK         = (1 << 0),
159     SER_DISK            = (1 << 1),
160     SER_GETHASH         = (1 << 2),
161 };
162
163 #define READWRITE(obj)      (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
164
165 /** 
166  * Implement three methods for serializable objects. These are actually wrappers over
167  * "SerializationOp" template, which implements the body of each class' serialization
168  * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
169  * added as members. 
170  */
171 #define ADD_SERIALIZE_METHODS                                                          \
172     size_t GetSerializeSize(int nType, int nVersion) const {                         \
173         CSizeComputer s(nType, nVersion);                                            \
174         NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
175         return s.size();                                                             \
176     }                                                                                \
177     template<typename Stream>                                                        \
178     void Serialize(Stream& s, int nType, int nVersion) const {                       \
179         NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
180     }                                                                                \
181     template<typename Stream>                                                        \
182     void Unserialize(Stream& s, int nType, int nVersion) {                           \
183         SerializationOp(s, CSerActionUnserialize(), nType, nVersion);                \
184     }
185
186 /*
187  * Basic Types
188  */
189 inline unsigned int GetSerializeSize(char a,      int, int=0) { return 1; }
190 inline unsigned int GetSerializeSize(int8_t a,    int, int=0) { return 1; }
191 inline unsigned int GetSerializeSize(uint8_t a,   int, int=0) { return 1; }
192 inline unsigned int GetSerializeSize(int16_t a,   int, int=0) { return 2; }
193 inline unsigned int GetSerializeSize(uint16_t a,  int, int=0) { return 2; }
194 inline unsigned int GetSerializeSize(int32_t a,   int, int=0) { return 4; }
195 inline unsigned int GetSerializeSize(uint32_t a,  int, int=0) { return 4; }
196 inline unsigned int GetSerializeSize(int64_t a,   int, int=0) { return 8; }
197 inline unsigned int GetSerializeSize(uint64_t a,  int, int=0) { return 8; }
198 inline unsigned int GetSerializeSize(float a,     int, int=0) { return 4; }
199 inline unsigned int GetSerializeSize(double a,    int, int=0) { return 8; }
200
201 template<typename Stream> inline void Serialize(Stream& s, char a,         int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char
202 template<typename Stream> inline void Serialize(Stream& s, int8_t a,       int, int=0) { ser_writedata8(s, a); }
203 template<typename Stream> inline void Serialize(Stream& s, uint8_t a,      int, int=0) { ser_writedata8(s, a); }
204 template<typename Stream> inline void Serialize(Stream& s, int16_t a,      int, int=0) { ser_writedata16(s, a); }
205 template<typename Stream> inline void Serialize(Stream& s, uint16_t a,     int, int=0) { ser_writedata16(s, a); }
206 template<typename Stream> inline void Serialize(Stream& s, int32_t a,      int, int=0) { ser_writedata32(s, a); }
207 template<typename Stream> inline void Serialize(Stream& s, uint32_t a,     int, int=0) { ser_writedata32(s, a); }
208 template<typename Stream> inline void Serialize(Stream& s, int64_t a,      int, int=0) { ser_writedata64(s, a); }
209 template<typename Stream> inline void Serialize(Stream& s, uint64_t a,     int, int=0) { ser_writedata64(s, a); }
210 template<typename Stream> inline void Serialize(Stream& s, float a,        int, int=0) { ser_writedata32(s, ser_float_to_uint32(a)); }
211 template<typename Stream> inline void Serialize(Stream& s, double a,       int, int=0) { ser_writedata64(s, ser_double_to_uint64(a)); }
212
213 template<typename Stream> inline void Unserialize(Stream& s, char& a,      int, int=0) { a = ser_readdata8(s); } // TODO Get rid of bare char
214 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a,    int, int=0) { a = ser_readdata8(s); }
215 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a,   int, int=0) { a = ser_readdata8(s); }
216 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a,   int, int=0) { a = ser_readdata16(s); }
217 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a,  int, int=0) { a = ser_readdata16(s); }
218 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a,   int, int=0) { a = ser_readdata32(s); }
219 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a,  int, int=0) { a = ser_readdata32(s); }
220 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a,   int, int=0) { a = ser_readdata64(s); }
221 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a,  int, int=0) { a = ser_readdata64(s); }
222 template<typename Stream> inline void Unserialize(Stream& s, float& a,     int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); }
223 template<typename Stream> inline void Unserialize(Stream& s, double& a,    int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); }
224
225 inline unsigned int GetSerializeSize(bool a, int, int=0)                          { return sizeof(char); }
226 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0)    { char f=a; ser_writedata8(s, f); }
227 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; }
228
229
230
231
232
233
234 /**
235  * Compact Size
236  * size <  253        -- 1 byte
237  * size <= USHRT_MAX  -- 3 bytes  (253 + 2 bytes)
238  * size <= UINT_MAX   -- 5 bytes  (254 + 4 bytes)
239  * size >  UINT_MAX   -- 9 bytes  (255 + 8 bytes)
240  */
241 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
242 {
243     if (nSize < 253)             return sizeof(unsigned char);
244     else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
245     else if (nSize <= std::numeric_limits<unsigned int>::max())  return sizeof(unsigned char) + sizeof(unsigned int);
246     else                         return sizeof(unsigned char) + sizeof(uint64_t);
247 }
248
249 template<typename Stream>
250 void WriteCompactSize(Stream& os, uint64_t nSize)
251 {
252     if (nSize < 253)
253     {
254         ser_writedata8(os, nSize);
255     }
256     else if (nSize <= std::numeric_limits<unsigned short>::max())
257     {
258         ser_writedata8(os, 253);
259         ser_writedata16(os, nSize);
260     }
261     else if (nSize <= std::numeric_limits<unsigned int>::max())
262     {
263         ser_writedata8(os, 254);
264         ser_writedata32(os, nSize);
265     }
266     else
267     {
268         ser_writedata8(os, 255);
269         ser_writedata64(os, nSize);
270     }
271     return;
272 }
273
274 template<typename Stream>
275 uint64_t ReadCompactSize(Stream& is)
276 {
277     uint8_t chSize = ser_readdata8(is);
278     uint64_t nSizeRet = 0;
279     if (chSize < 253)
280     {
281         nSizeRet = chSize;
282     }
283     else if (chSize == 253)
284     {
285         nSizeRet = ser_readdata16(is);
286         if (nSizeRet < 253)
287             throw std::ios_base::failure("non-canonical ReadCompactSize()");
288     }
289     else if (chSize == 254)
290     {
291         nSizeRet = ser_readdata32(is);
292         if (nSizeRet < 0x10000u)
293             throw std::ios_base::failure("non-canonical ReadCompactSize()");
294     }
295     else
296     {
297         nSizeRet = ser_readdata64(is);
298         if (nSizeRet < 0x100000000ULL)
299             throw std::ios_base::failure("non-canonical ReadCompactSize()");
300     }
301     if (nSizeRet > (uint64_t)MAX_SIZE)
302         throw std::ios_base::failure("ReadCompactSize(): size too large");
303     return nSizeRet;
304 }
305
306 /**
307  * Variable-length integers: bytes are a MSB base-128 encoding of the number.
308  * The high bit in each byte signifies whether another digit follows. To make
309  * sure the encoding is one-to-one, one is subtracted from all but the last digit.
310  * Thus, the byte sequence a[] with length len, where all but the last byte
311  * has bit 128 set, encodes the number:
312  * 
313  *  (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
314  * 
315  * Properties:
316  * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
317  * * Every integer has exactly one encoding
318  * * Encoding does not depend on size of original integer type
319  * * No redundancy: every (infinite) byte sequence corresponds to a list
320  *   of encoded integers.
321  * 
322  * 0:         [0x00]  256:        [0x81 0x00]
323  * 1:         [0x01]  16383:      [0xFE 0x7F]
324  * 127:       [0x7F]  16384:      [0xFF 0x00]
325  * 128:  [0x80 0x00]  16511: [0x80 0xFF 0x7F]
326  * 255:  [0x80 0x7F]  65535: [0x82 0xFD 0x7F]
327  * 2^32:           [0x8E 0xFE 0xFE 0xFF 0x00]
328  */
329
330 template<typename I>
331 inline unsigned int GetSizeOfVarInt(I n)
332 {
333     int nRet = 0;
334     while(true) {
335         nRet++;
336         if (n <= 0x7F)
337             break;
338         n = (n >> 7) - 1;
339     }
340     return nRet;
341 }
342
343 template<typename Stream, typename I>
344 void WriteVarInt(Stream& os, I n)
345 {
346     unsigned char tmp[(sizeof(n)*8+6)/7];
347     int len=0;
348     while(true) {
349         tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
350         if (n <= 0x7F)
351             break;
352         n = (n >> 7) - 1;
353         len++;
354     }
355     do {
356         ser_writedata8(os, tmp[len]);
357     } while(len--);
358 }
359
360 template<typename Stream, typename I>
361 I ReadVarInt(Stream& is)
362 {
363     I n = 0;
364     while(true) {
365         unsigned char chData = ser_readdata8(is);
366         n = (n << 7) | (chData & 0x7F);
367         if (chData & 0x80)
368             n++;
369         else
370             return n;
371     }
372 }
373
374 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
375 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
376 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
377
378 /** 
379  * Wrapper for serializing arrays and POD.
380  */
381 class CFlatData
382 {
383 protected:
384     char* pbegin;
385     char* pend;
386 public:
387     CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
388     template <class T, class TAl>
389     explicit CFlatData(std::vector<T,TAl> &v)
390     {
391         pbegin = (char*)begin_ptr(v);
392         pend = (char*)end_ptr(v);
393     }
394     char* begin() { return pbegin; }
395     const char* begin() const { return pbegin; }
396     char* end() { return pend; }
397     const char* end() const { return pend; }
398
399     unsigned int GetSerializeSize(int, int=0) const
400     {
401         return pend - pbegin;
402     }
403
404     template<typename Stream>
405     void Serialize(Stream& s, int, int=0) const
406     {
407         s.write(pbegin, pend - pbegin);
408     }
409
410     template<typename Stream>
411     void Unserialize(Stream& s, int, int=0)
412     {
413         s.read(pbegin, pend - pbegin);
414     }
415 };
416
417 template<typename I>
418 class CVarInt
419 {
420 protected:
421     I &n;
422 public:
423     CVarInt(I& nIn) : n(nIn) { }
424
425     unsigned int GetSerializeSize(int, int) const {
426         return GetSizeOfVarInt<I>(n);
427     }
428
429     template<typename Stream>
430     void Serialize(Stream &s, int, int) const {
431         WriteVarInt<Stream,I>(s, n);
432     }
433
434     template<typename Stream>
435     void Unserialize(Stream& s, int, int) {
436         n = ReadVarInt<Stream,I>(s);
437     }
438 };
439
440 template<size_t Limit>
441 class LimitedString
442 {
443 protected:
444     std::string& string;
445 public:
446     LimitedString(std::string& string) : string(string) {}
447
448     template<typename Stream>
449     void Unserialize(Stream& s, int, int=0)
450     {
451         size_t size = ReadCompactSize(s);
452         if (size > Limit) {
453             throw std::ios_base::failure("String length limit exceeded");
454         }
455         string.resize(size);
456         if (size != 0)
457             s.read((char*)&string[0], size);
458     }
459
460     template<typename Stream>
461     void Serialize(Stream& s, int, int=0) const
462     {
463         WriteCompactSize(s, string.size());
464         if (!string.empty())
465             s.write((char*)&string[0], string.size());
466     }
467
468     unsigned int GetSerializeSize(int, int=0) const
469     {
470         return GetSizeOfCompactSize(string.size()) + string.size();
471     }
472 };
473
474 template<typename I>
475 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
476
477 /**
478  * Forward declarations
479  */
480
481 /**
482  *  string
483  */
484 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
485 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
486 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
487
488 /**
489  * vector
490  * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
491  */
492 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
493 template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
494 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
495 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
496 template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&);
497 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
498 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
499 template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&);
500 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
501
502 /**
503  * others derived from vector
504  */
505 extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion);
506 template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion);
507 template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion);
508
509 /**
510  * pair
511  */
512 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
513 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
514 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
515
516 /**
517  * map
518  */
519 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
520 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion);
521 template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion);
522
523 /**
524  * set
525  */
526 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
527 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
528 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
529
530
531
532
533
534 /**
535  * If none of the specialized versions above matched, default to calling member function.
536  * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
537  * The compiler will only cast int to long if none of the other templates matched.
538  * Thanks to Boost serialization for this idea.
539  */
540 template<typename T>
541 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
542 {
543     return a.GetSerializeSize((int)nType, nVersion);
544 }
545
546 template<typename Stream, typename T>
547 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
548 {
549     a.Serialize(os, (int)nType, nVersion);
550 }
551
552 template<typename Stream, typename T>
553 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
554 {
555     a.Unserialize(is, (int)nType, nVersion);
556 }
557
558
559
560
561
562 /**
563  * string
564  */
565 template<typename C>
566 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
567 {
568     return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
569 }
570
571 template<typename Stream, typename C>
572 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
573 {
574     WriteCompactSize(os, str.size());
575     if (!str.empty())
576         os.write((char*)&str[0], str.size() * sizeof(str[0]));
577 }
578
579 template<typename Stream, typename C>
580 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
581 {
582     unsigned int nSize = ReadCompactSize(is);
583     str.resize(nSize);
584     if (nSize != 0)
585         is.read((char*)&str[0], nSize * sizeof(str[0]));
586 }
587
588
589
590 /**
591  * vector
592  */
593 template<typename T, typename A>
594 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
595 {
596     return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
597 }
598
599 template<typename T, typename A, typename V>
600 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
601 {
602     unsigned int nSize = GetSizeOfCompactSize(v.size());
603     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
604         nSize += GetSerializeSize((*vi), nType, nVersion);
605     return nSize;
606 }
607
608 template<typename T, typename A>
609 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
610 {
611     return GetSerializeSize_impl(v, nType, nVersion, T());
612 }
613
614
615 template<typename Stream, typename T, typename A>
616 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
617 {
618     WriteCompactSize(os, v.size());
619     if (!v.empty())
620         os.write((char*)&v[0], v.size() * sizeof(T));
621 }
622
623 template<typename Stream, typename T, typename A, typename V>
624 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
625 {
626     WriteCompactSize(os, v.size());
627     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
628         ::Serialize(os, (*vi), nType, nVersion);
629 }
630
631 template<typename Stream, typename T, typename A>
632 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
633 {
634     Serialize_impl(os, v, nType, nVersion, T());
635 }
636
637
638 template<typename Stream, typename T, typename A>
639 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
640 {
641     // Limit size per read so bogus size value won't cause out of memory
642     v.clear();
643     unsigned int nSize = ReadCompactSize(is);
644     unsigned int i = 0;
645     while (i < nSize)
646     {
647         unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
648         v.resize(i + blk);
649         is.read((char*)&v[i], blk * sizeof(T));
650         i += blk;
651     }
652 }
653
654 template<typename Stream, typename T, typename A, typename V>
655 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
656 {
657     v.clear();
658     unsigned int nSize = ReadCompactSize(is);
659     unsigned int i = 0;
660     unsigned int nMid = 0;
661     while (nMid < nSize)
662     {
663         nMid += 5000000 / sizeof(T);
664         if (nMid > nSize)
665             nMid = nSize;
666         v.resize(nMid);
667         for (; i < nMid; i++)
668             Unserialize(is, v[i], nType, nVersion);
669     }
670 }
671
672 template<typename Stream, typename T, typename A>
673 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
674 {
675     Unserialize_impl(is, v, nType, nVersion, T());
676 }
677
678
679
680 /**
681  * others derived from vector
682  */
683 inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
684 {
685     return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
686 }
687
688 template<typename Stream>
689 void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
690 {
691     Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
692 }
693
694 template<typename Stream>
695 void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
696 {
697     Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
698 }
699
700
701
702 /**
703  * pair
704  */
705 template<typename K, typename T>
706 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
707 {
708     return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
709 }
710
711 template<typename Stream, typename K, typename T>
712 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
713 {
714     Serialize(os, item.first, nType, nVersion);
715     Serialize(os, item.second, nType, nVersion);
716 }
717
718 template<typename Stream, typename K, typename T>
719 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
720 {
721     Unserialize(is, item.first, nType, nVersion);
722     Unserialize(is, item.second, nType, nVersion);
723 }
724
725
726
727 /**
728  * map
729  */
730 template<typename K, typename T, typename Pred, typename A>
731 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
732 {
733     unsigned int nSize = GetSizeOfCompactSize(m.size());
734     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
735         nSize += GetSerializeSize((*mi), nType, nVersion);
736     return nSize;
737 }
738
739 template<typename Stream, typename K, typename T, typename Pred, typename A>
740 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
741 {
742     WriteCompactSize(os, m.size());
743     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
744         Serialize(os, (*mi), nType, nVersion);
745 }
746
747 template<typename Stream, typename K, typename T, typename Pred, typename A>
748 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
749 {
750     m.clear();
751     unsigned int nSize = ReadCompactSize(is);
752     typename std::map<K, T, Pred, A>::iterator mi = m.begin();
753     for (unsigned int i = 0; i < nSize; i++)
754     {
755         std::pair<K, T> item;
756         Unserialize(is, item, nType, nVersion);
757         mi = m.insert(mi, item);
758     }
759 }
760
761
762
763 /**
764  * set
765  */
766 template<typename K, typename Pred, typename A>
767 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
768 {
769     unsigned int nSize = GetSizeOfCompactSize(m.size());
770     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
771         nSize += GetSerializeSize((*it), nType, nVersion);
772     return nSize;
773 }
774
775 template<typename Stream, typename K, typename Pred, typename A>
776 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
777 {
778     WriteCompactSize(os, m.size());
779     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
780         Serialize(os, (*it), nType, nVersion);
781 }
782
783 template<typename Stream, typename K, typename Pred, typename A>
784 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
785 {
786     m.clear();
787     unsigned int nSize = ReadCompactSize(is);
788     typename std::set<K, Pred, A>::iterator it = m.begin();
789     for (unsigned int i = 0; i < nSize; i++)
790     {
791         K key;
792         Unserialize(is, key, nType, nVersion);
793         it = m.insert(it, key);
794     }
795 }
796
797
798
799 /**
800  * Support for ADD_SERIALIZE_METHODS and READWRITE macro
801  */
802 struct CSerActionSerialize
803 {
804     bool ForRead() const { return false; }
805 };
806 struct CSerActionUnserialize
807 {
808     bool ForRead() const { return true; }
809 };
810
811 template<typename Stream, typename T>
812 inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
813 {
814     ::Serialize(s, obj, nType, nVersion);
815 }
816
817 template<typename Stream, typename T>
818 inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
819 {
820     ::Unserialize(s, obj, nType, nVersion);
821 }
822
823
824
825
826
827
828
829
830
831 class CSizeComputer
832 {
833 protected:
834     size_t nSize;
835
836 public:
837     int nType;
838     int nVersion;
839
840     CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
841
842     CSizeComputer& write(const char *psz, size_t nSize)
843     {
844         this->nSize += nSize;
845         return *this;
846     }
847
848     template<typename T>
849     CSizeComputer& operator<<(const T& obj)
850     {
851         ::Serialize(*this, obj, nType, nVersion);
852         return (*this);
853     }
854
855     size_t size() const {
856         return nSize;
857     }
858 };
859
860 #endif // BITCOIN_SERIALIZE_H
This page took 0.072023 seconds and 4 git commands to generate.