Update luke-jr's PGP key
[bitcoinplatinum.git] / src / serialize.h
blob5c2db9d332ce37875e919f2540f2eafa68244585
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 #include "prevector.h"
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 <typename V>
53 inline typename V::value_type* begin_ptr(V& v)
55 return v.empty() ? NULL : &v[0];
57 /** Get begin pointer of vector (const version) */
58 template <typename V>
59 inline const typename V::value_type* begin_ptr(const V& v)
61 return v.empty() ? NULL : &v[0];
63 /** Get end pointer of vector (non-const version) */
64 template <typename V>
65 inline typename V::value_type* end_ptr(V& v)
67 return v.empty() ? NULL : (&v[0] + v.size());
69 /** Get end pointer of vector (const version) */
70 template <typename V>
71 inline const typename V::value_type* end_ptr(const V& 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 template <unsigned int N, typename T, typename S, typename D>
395 explicit CFlatData(prevector<N, T, S, D> &v)
397 pbegin = (char*)begin_ptr(v);
398 pend = (char*)end_ptr(v);
400 char* begin() { return pbegin; }
401 const char* begin() const { return pbegin; }
402 char* end() { return pend; }
403 const char* end() const { return pend; }
405 unsigned int GetSerializeSize(int, int=0) const
407 return pend - pbegin;
410 template<typename Stream>
411 void Serialize(Stream& s, int, int=0) const
413 s.write(pbegin, pend - pbegin);
416 template<typename Stream>
417 void Unserialize(Stream& s, int, int=0)
419 s.read(pbegin, pend - pbegin);
423 template<typename I>
424 class CVarInt
426 protected:
427 I &n;
428 public:
429 CVarInt(I& nIn) : n(nIn) { }
431 unsigned int GetSerializeSize(int, int) const {
432 return GetSizeOfVarInt<I>(n);
435 template<typename Stream>
436 void Serialize(Stream &s, int, int) const {
437 WriteVarInt<Stream,I>(s, n);
440 template<typename Stream>
441 void Unserialize(Stream& s, int, int) {
442 n = ReadVarInt<Stream,I>(s);
446 template<size_t Limit>
447 class LimitedString
449 protected:
450 std::string& string;
451 public:
452 LimitedString(std::string& string) : string(string) {}
454 template<typename Stream>
455 void Unserialize(Stream& s, int, int=0)
457 size_t size = ReadCompactSize(s);
458 if (size > Limit) {
459 throw std::ios_base::failure("String length limit exceeded");
461 string.resize(size);
462 if (size != 0)
463 s.read((char*)&string[0], size);
466 template<typename Stream>
467 void Serialize(Stream& s, int, int=0) const
469 WriteCompactSize(s, string.size());
470 if (!string.empty())
471 s.write((char*)&string[0], string.size());
474 unsigned int GetSerializeSize(int, int=0) const
476 return GetSizeOfCompactSize(string.size()) + string.size();
480 template<typename I>
481 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
484 * Forward declarations
488 * string
490 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
491 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
492 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
495 * prevector
496 * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
498 template<unsigned int N, typename T> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
499 template<unsigned int N, typename T, typename V> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&);
500 template<unsigned int N, typename T> inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion);
501 template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
502 template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&);
503 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion);
504 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
505 template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&);
506 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion);
509 * vector
510 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
512 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
513 template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
514 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
515 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&);
516 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&);
517 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
518 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
519 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&);
520 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
523 * pair
525 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
526 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
527 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
530 * map
532 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
533 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);
534 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);
537 * set
539 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
540 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
541 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
548 * If none of the specialized versions above matched, default to calling member function.
549 * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
550 * The compiler will only cast int to long if none of the other templates matched.
551 * Thanks to Boost serialization for this idea.
553 template<typename T>
554 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
556 return a.GetSerializeSize((int)nType, nVersion);
559 template<typename Stream, typename T>
560 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
562 a.Serialize(os, (int)nType, nVersion);
565 template<typename Stream, typename T>
566 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
568 a.Unserialize(is, (int)nType, nVersion);
576 * string
578 template<typename C>
579 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
581 return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
584 template<typename Stream, typename C>
585 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
587 WriteCompactSize(os, str.size());
588 if (!str.empty())
589 os.write((char*)&str[0], str.size() * sizeof(str[0]));
592 template<typename Stream, typename C>
593 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
595 unsigned int nSize = ReadCompactSize(is);
596 str.resize(nSize);
597 if (nSize != 0)
598 is.read((char*)&str[0], nSize * sizeof(str[0]));
604 * prevector
606 template<unsigned int N, typename T>
607 unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
609 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
612 template<unsigned int N, typename T, typename V>
613 unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&)
615 unsigned int nSize = GetSizeOfCompactSize(v.size());
616 for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
617 nSize += GetSerializeSize((*vi), nType, nVersion);
618 return nSize;
621 template<unsigned int N, typename T>
622 inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion)
624 return GetSerializeSize_impl(v, nType, nVersion, T());
628 template<typename Stream, unsigned int N, typename T>
629 void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
631 WriteCompactSize(os, v.size());
632 if (!v.empty())
633 os.write((char*)&v[0], v.size() * sizeof(T));
636 template<typename Stream, unsigned int N, typename T, typename V>
637 void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&)
639 WriteCompactSize(os, v.size());
640 for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
641 ::Serialize(os, (*vi), nType, nVersion);
644 template<typename Stream, unsigned int N, typename T>
645 inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion)
647 Serialize_impl(os, v, nType, nVersion, T());
651 template<typename Stream, unsigned int N, typename T>
652 void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
654 // Limit size per read so bogus size value won't cause out of memory
655 v.clear();
656 unsigned int nSize = ReadCompactSize(is);
657 unsigned int i = 0;
658 while (i < nSize)
660 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
661 v.resize(i + blk);
662 is.read((char*)&v[i], blk * sizeof(T));
663 i += blk;
667 template<typename Stream, unsigned int N, typename T, typename V>
668 void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&)
670 v.clear();
671 unsigned int nSize = ReadCompactSize(is);
672 unsigned int i = 0;
673 unsigned int nMid = 0;
674 while (nMid < nSize)
676 nMid += 5000000 / sizeof(T);
677 if (nMid > nSize)
678 nMid = nSize;
679 v.resize(nMid);
680 for (; i < nMid; i++)
681 Unserialize(is, v[i], nType, nVersion);
685 template<typename Stream, unsigned int N, typename T>
686 inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion)
688 Unserialize_impl(is, v, nType, nVersion, T());
694 * vector
696 template<typename T, typename A>
697 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
699 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
702 template<typename T, typename A, typename V>
703 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
705 unsigned int nSize = GetSizeOfCompactSize(v.size());
706 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
707 nSize += GetSerializeSize((*vi), nType, nVersion);
708 return nSize;
711 template<typename T, typename A>
712 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
714 return GetSerializeSize_impl(v, nType, nVersion, T());
718 template<typename Stream, typename T, typename A>
719 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
721 WriteCompactSize(os, v.size());
722 if (!v.empty())
723 os.write((char*)&v[0], v.size() * sizeof(T));
726 template<typename Stream, typename T, typename A, typename V>
727 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
729 WriteCompactSize(os, v.size());
730 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
731 ::Serialize(os, (*vi), nType, nVersion);
734 template<typename Stream, typename T, typename A>
735 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
737 Serialize_impl(os, v, nType, nVersion, T());
741 template<typename Stream, typename T, typename A>
742 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
744 // Limit size per read so bogus size value won't cause out of memory
745 v.clear();
746 unsigned int nSize = ReadCompactSize(is);
747 unsigned int i = 0;
748 while (i < nSize)
750 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
751 v.resize(i + blk);
752 is.read((char*)&v[i], blk * sizeof(T));
753 i += blk;
757 template<typename Stream, typename T, typename A, typename V>
758 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
760 v.clear();
761 unsigned int nSize = ReadCompactSize(is);
762 unsigned int i = 0;
763 unsigned int nMid = 0;
764 while (nMid < nSize)
766 nMid += 5000000 / sizeof(T);
767 if (nMid > nSize)
768 nMid = nSize;
769 v.resize(nMid);
770 for (; i < nMid; i++)
771 Unserialize(is, v[i], nType, nVersion);
775 template<typename Stream, typename T, typename A>
776 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
778 Unserialize_impl(is, v, nType, nVersion, T());
784 * pair
786 template<typename K, typename T>
787 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
789 return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
792 template<typename Stream, typename K, typename T>
793 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
795 Serialize(os, item.first, nType, nVersion);
796 Serialize(os, item.second, nType, nVersion);
799 template<typename Stream, typename K, typename T>
800 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
802 Unserialize(is, item.first, nType, nVersion);
803 Unserialize(is, item.second, nType, nVersion);
809 * map
811 template<typename K, typename T, typename Pred, typename A>
812 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
814 unsigned int nSize = GetSizeOfCompactSize(m.size());
815 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
816 nSize += GetSerializeSize((*mi), nType, nVersion);
817 return nSize;
820 template<typename Stream, typename K, typename T, typename Pred, typename A>
821 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
823 WriteCompactSize(os, m.size());
824 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
825 Serialize(os, (*mi), nType, nVersion);
828 template<typename Stream, typename K, typename T, typename Pred, typename A>
829 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
831 m.clear();
832 unsigned int nSize = ReadCompactSize(is);
833 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
834 for (unsigned int i = 0; i < nSize; i++)
836 std::pair<K, T> item;
837 Unserialize(is, item, nType, nVersion);
838 mi = m.insert(mi, item);
845 * set
847 template<typename K, typename Pred, typename A>
848 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
850 unsigned int nSize = GetSizeOfCompactSize(m.size());
851 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
852 nSize += GetSerializeSize((*it), nType, nVersion);
853 return nSize;
856 template<typename Stream, typename K, typename Pred, typename A>
857 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
859 WriteCompactSize(os, m.size());
860 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
861 Serialize(os, (*it), nType, nVersion);
864 template<typename Stream, typename K, typename Pred, typename A>
865 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
867 m.clear();
868 unsigned int nSize = ReadCompactSize(is);
869 typename std::set<K, Pred, A>::iterator it = m.begin();
870 for (unsigned int i = 0; i < nSize; i++)
872 K key;
873 Unserialize(is, key, nType, nVersion);
874 it = m.insert(it, key);
881 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
883 struct CSerActionSerialize
885 bool ForRead() const { return false; }
887 struct CSerActionUnserialize
889 bool ForRead() const { return true; }
892 template<typename Stream, typename T>
893 inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
895 ::Serialize(s, obj, nType, nVersion);
898 template<typename Stream, typename T>
899 inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
901 ::Unserialize(s, obj, nType, nVersion);
912 class CSizeComputer
914 protected:
915 size_t nSize;
917 public:
918 int nType;
919 int nVersion;
921 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
923 CSizeComputer& write(const char *psz, size_t nSize)
925 this->nSize += nSize;
926 return *this;
929 template<typename T>
930 CSizeComputer& operator<<(const T& obj)
932 ::Serialize(*this, obj, nType, nVersion);
933 return (*this);
936 size_t size() const {
937 return nSize;
941 #endif // BITCOIN_SERIALIZE_H