Merge pull request #6599
[bitcoinplatinum.git] / src / serialize.h
blob53d8af142f6bd6d16243d574abbb8027fa0c2c00
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.
6 #ifndef BITCOIN_SERIALIZE_H
7 #define BITCOIN_SERIALIZE_H
9 #include "compat/endian.h"
11 #include <algorithm>
12 #include <assert.h>
13 #include <ios>
14 #include <limits>
15 #include <map>
16 #include <set>
17 #include <stdint.h>
18 #include <string>
19 #include <string.h>
20 #include <utility>
21 #include <vector>
23 class CScript;
25 static const unsigned int MAX_SIZE = 0x02000000;
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
31 template<typename T>
32 inline T& REF(const T& val)
34 return const_cast<T&>(val);
37 /**
38 * Used to acquire a non-const pointer "this" to generate bodies
39 * of const serialization operations from a template
41 template<typename T>
42 inline T* NCONST_PTR(const T* val)
44 return const_cast<T*>(val);
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.
52 template <class T, class TAl>
53 inline T* begin_ptr(std::vector<T,TAl>& v)
55 return v.empty() ? NULL : &v[0];
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)
61 return v.empty() ? NULL : &v[0];
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)
67 return v.empty() ? NULL : (&v[0] + v.size());
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)
73 return v.empty() ? NULL : (&v[0] + v.size());
77 * Lowest-level serialization and conversion.
78 * @note Sizes of these types are verified in the tests
80 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
82 s.write((char*)&obj, 1);
84 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
86 obj = htole16(obj);
87 s.write((char*)&obj, 2);
89 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
91 obj = htole32(obj);
92 s.write((char*)&obj, 4);
94 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
96 obj = htole64(obj);
97 s.write((char*)&obj, 8);
99 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
101 uint8_t obj;
102 s.read((char*)&obj, 1);
103 return obj;
105 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
107 uint16_t obj;
108 s.read((char*)&obj, 2);
109 return le16toh(obj);
111 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
113 uint32_t obj;
114 s.read((char*)&obj, 4);
115 return le32toh(obj);
117 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
119 uint64_t obj;
120 s.read((char*)&obj, 8);
121 return le64toh(obj);
123 inline uint64_t ser_double_to_uint64(double x)
125 union { double x; uint64_t y; } tmp;
126 tmp.x = x;
127 return tmp.y;
129 inline uint32_t ser_float_to_uint32(float x)
131 union { float x; uint32_t y; } tmp;
132 tmp.x = x;
133 return tmp.y;
135 inline double ser_uint64_to_double(uint64_t y)
137 union { double x; uint64_t y; } tmp;
138 tmp.y = y;
139 return tmp.x;
141 inline float ser_uint32_to_float(uint32_t y)
143 union { float x; uint32_t y; } tmp;
144 tmp.y = y;
145 return tmp.x;
149 /////////////////////////////////////////////////////////////////
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)
155 enum
157 // primary actions
158 SER_NETWORK = (1 << 0),
159 SER_DISK = (1 << 1),
160 SER_GETHASH = (1 << 2),
163 #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
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.
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(); \
177 template<typename Stream> \
178 void Serialize(Stream& s, int nType, int nVersion) const { \
179 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
181 template<typename Stream> \
182 void Unserialize(Stream& s, int nType, int nVersion) { \
183 SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
187 * Basic Types
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; }
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)); }
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)); }
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; }
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)
241 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
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);
249 template<typename Stream>
250 void WriteCompactSize(Stream& os, uint64_t nSize)
252 if (nSize < 253)
254 ser_writedata8(os, nSize);
256 else if (nSize <= std::numeric_limits<unsigned short>::max())
258 ser_writedata8(os, 253);
259 ser_writedata16(os, nSize);
261 else if (nSize <= std::numeric_limits<unsigned int>::max())
263 ser_writedata8(os, 254);
264 ser_writedata32(os, nSize);
266 else
268 ser_writedata8(os, 255);
269 ser_writedata64(os, nSize);
271 return;
274 template<typename Stream>
275 uint64_t ReadCompactSize(Stream& is)
277 uint8_t chSize = ser_readdata8(is);
278 uint64_t nSizeRet = 0;
279 if (chSize < 253)
281 nSizeRet = chSize;
283 else if (chSize == 253)
285 nSizeRet = ser_readdata16(is);
286 if (nSizeRet < 253)
287 throw std::ios_base::failure("non-canonical ReadCompactSize()");
289 else if (chSize == 254)
291 nSizeRet = ser_readdata32(is);
292 if (nSizeRet < 0x10000u)
293 throw std::ios_base::failure("non-canonical ReadCompactSize()");
295 else
297 nSizeRet = ser_readdata64(is);
298 if (nSizeRet < 0x100000000ULL)
299 throw std::ios_base::failure("non-canonical ReadCompactSize()");
301 if (nSizeRet > (uint64_t)MAX_SIZE)
302 throw std::ios_base::failure("ReadCompactSize(): size too large");
303 return nSizeRet;
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:
313 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
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.
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]
330 template<typename I>
331 inline unsigned int GetSizeOfVarInt(I n)
333 int nRet = 0;
334 while(true) {
335 nRet++;
336 if (n <= 0x7F)
337 break;
338 n = (n >> 7) - 1;
340 return nRet;
343 template<typename Stream, typename I>
344 void WriteVarInt(Stream& os, I n)
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++;
355 do {
356 ser_writedata8(os, tmp[len]);
357 } while(len--);
360 template<typename Stream, typename I>
361 I ReadVarInt(Stream& is)
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;
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)))
378 /**
379 * Wrapper for serializing arrays and POD.
381 class CFlatData
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)
391 pbegin = (char*)begin_ptr(v);
392 pend = (char*)end_ptr(v);
394 char* begin() { return pbegin; }
395 const char* begin() const { return pbegin; }
396 char* end() { return pend; }
397 const char* end() const { return pend; }
399 unsigned int GetSerializeSize(int, int=0) const
401 return pend - pbegin;
404 template<typename Stream>
405 void Serialize(Stream& s, int, int=0) const
407 s.write(pbegin, pend - pbegin);
410 template<typename Stream>
411 void Unserialize(Stream& s, int, int=0)
413 s.read(pbegin, pend - pbegin);
417 template<typename I>
418 class CVarInt
420 protected:
421 I &n;
422 public:
423 CVarInt(I& nIn) : n(nIn) { }
425 unsigned int GetSerializeSize(int, int) const {
426 return GetSizeOfVarInt<I>(n);
429 template<typename Stream>
430 void Serialize(Stream &s, int, int) const {
431 WriteVarInt<Stream,I>(s, n);
434 template<typename Stream>
435 void Unserialize(Stream& s, int, int) {
436 n = ReadVarInt<Stream,I>(s);
440 template<size_t Limit>
441 class LimitedString
443 protected:
444 std::string& string;
445 public:
446 LimitedString(std::string& string) : string(string) {}
448 template<typename Stream>
449 void Unserialize(Stream& s, int, int=0)
451 size_t size = ReadCompactSize(s);
452 if (size > Limit) {
453 throw std::ios_base::failure("String length limit exceeded");
455 string.resize(size);
456 if (size != 0)
457 s.read((char*)&string[0], size);
460 template<typename Stream>
461 void Serialize(Stream& s, int, int=0) const
463 WriteCompactSize(s, string.size());
464 if (!string.empty())
465 s.write((char*)&string[0], string.size());
468 unsigned int GetSerializeSize(int, int=0) const
470 return GetSizeOfCompactSize(string.size()) + string.size();
474 template<typename I>
475 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
478 * Forward declarations
482 * string
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);
489 * vector
490 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
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);
503 * others derived from vector
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);
510 * pair
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);
517 * map
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);
524 * set
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);
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.
540 template<typename T>
541 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
543 return a.GetSerializeSize((int)nType, nVersion);
546 template<typename Stream, typename T>
547 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
549 a.Serialize(os, (int)nType, nVersion);
552 template<typename Stream, typename T>
553 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
555 a.Unserialize(is, (int)nType, nVersion);
563 * string
565 template<typename C>
566 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
568 return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
571 template<typename Stream, typename C>
572 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
574 WriteCompactSize(os, str.size());
575 if (!str.empty())
576 os.write((char*)&str[0], str.size() * sizeof(str[0]));
579 template<typename Stream, typename C>
580 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
582 unsigned int nSize = ReadCompactSize(is);
583 str.resize(nSize);
584 if (nSize != 0)
585 is.read((char*)&str[0], nSize * sizeof(str[0]));
591 * vector
593 template<typename T, typename A>
594 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
596 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
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&)
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;
608 template<typename T, typename A>
609 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
611 return GetSerializeSize_impl(v, nType, nVersion, T());
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&)
618 WriteCompactSize(os, v.size());
619 if (!v.empty())
620 os.write((char*)&v[0], v.size() * sizeof(T));
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&)
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);
631 template<typename Stream, typename T, typename A>
632 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
634 Serialize_impl(os, v, nType, nVersion, T());
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&)
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)
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;
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&)
657 v.clear();
658 unsigned int nSize = ReadCompactSize(is);
659 unsigned int i = 0;
660 unsigned int nMid = 0;
661 while (nMid < nSize)
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);
672 template<typename Stream, typename T, typename A>
673 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
675 Unserialize_impl(is, v, nType, nVersion, T());
681 * others derived from vector
683 inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
685 return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
688 template<typename Stream>
689 void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
691 Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
694 template<typename Stream>
695 void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
697 Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
703 * pair
705 template<typename K, typename T>
706 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
708 return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
711 template<typename Stream, typename K, typename T>
712 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
714 Serialize(os, item.first, nType, nVersion);
715 Serialize(os, item.second, nType, nVersion);
718 template<typename Stream, typename K, typename T>
719 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
721 Unserialize(is, item.first, nType, nVersion);
722 Unserialize(is, item.second, nType, nVersion);
728 * map
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)
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;
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)
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);
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)
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++)
755 std::pair<K, T> item;
756 Unserialize(is, item, nType, nVersion);
757 mi = m.insert(mi, item);
764 * set
766 template<typename K, typename Pred, typename A>
767 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
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;
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)
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);
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)
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++)
791 K key;
792 Unserialize(is, key, nType, nVersion);
793 it = m.insert(it, key);
800 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
802 struct CSerActionSerialize
804 bool ForRead() const { return false; }
806 struct CSerActionUnserialize
808 bool ForRead() const { return true; }
811 template<typename Stream, typename T>
812 inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
814 ::Serialize(s, obj, nType, nVersion);
817 template<typename Stream, typename T>
818 inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
820 ::Unserialize(s, obj, nType, nVersion);
831 class CSizeComputer
833 protected:
834 size_t nSize;
836 public:
837 int nType;
838 int nVersion;
840 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
842 CSizeComputer& write(const char *psz, size_t nSize)
844 this->nSize += nSize;
845 return *this;
848 template<typename T>
849 CSizeComputer& operator<<(const T& obj)
851 ::Serialize(*this, obj, nType, nVersion);
852 return (*this);
855 size_t size() const {
856 return nSize;
860 #endif // BITCOIN_SERIALIZE_H