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"
24 #include <boost/array.hpp>
25 #include <boost/optional.hpp>
27 #include "prevector.h"
29 static const unsigned int MAX_SIZE = 0x02000000;
32 * Used to bypass the rule against non-const reference to temporary
33 * where it makes sense with wrappers such as CFlatData or CTxDB
36 inline T& REF(const T& val)
38 return const_cast<T&>(val);
42 * Used to acquire a non-const pointer "this" to generate bodies
43 * of const serialization operations from a template
46 inline T* NCONST_PTR(const T* val)
48 return const_cast<T*>(val);
52 * Get begin pointer of vector (non-const version).
53 * @note These functions avoid the undefined case of indexing into an empty
54 * vector, as well as that of indexing after the end of the vector.
57 inline typename V::value_type* begin_ptr(V& v)
59 return v.empty() ? NULL : &v[0];
61 /** Get begin pointer of vector (const version) */
63 inline const typename V::value_type* begin_ptr(const V& v)
65 return v.empty() ? NULL : &v[0];
67 /** Get end pointer of vector (non-const version) */
69 inline typename V::value_type* end_ptr(V& v)
71 return v.empty() ? NULL : (&v[0] + v.size());
73 /** Get end pointer of vector (const version) */
75 inline const typename V::value_type* end_ptr(const V& v)
77 return v.empty() ? NULL : (&v[0] + v.size());
81 * Lowest-level serialization and conversion.
82 * @note Sizes of these types are verified in the tests
84 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
86 s.write((char*)&obj, 1);
88 template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
91 s.write((char*)&obj, 2);
93 template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
96 s.write((char*)&obj, 4);
98 template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
101 s.write((char*)&obj, 8);
103 template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
106 s.read((char*)&obj, 1);
109 template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
112 s.read((char*)&obj, 2);
115 template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
118 s.read((char*)&obj, 4);
121 template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
124 s.read((char*)&obj, 8);
127 inline uint64_t ser_double_to_uint64(double x)
129 union { double x; uint64_t y; } tmp;
133 inline uint32_t ser_float_to_uint32(float x)
135 union { float x; uint32_t y; } tmp;
139 inline double ser_uint64_to_double(uint64_t y)
141 union { double x; uint64_t y; } tmp;
145 inline float ser_uint32_to_float(uint32_t y)
147 union { float x; uint32_t y; } tmp;
153 /////////////////////////////////////////////////////////////////
155 // Templates for serializing to anything that looks like a stream,
156 // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
162 SER_NETWORK = (1 << 0),
164 SER_GETHASH = (1 << 2),
167 #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
170 * Implement three methods for serializable objects. These are actually wrappers over
171 * "SerializationOp" template, which implements the body of each class' serialization
172 * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
175 #define ADD_SERIALIZE_METHODS \
176 size_t GetSerializeSize(int nType, int nVersion) const { \
177 CSizeComputer s(nType, nVersion); \
178 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
181 template<typename Stream> \
182 void Serialize(Stream& s, int nType, int nVersion) const { \
183 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
185 template<typename Stream> \
186 void Unserialize(Stream& s, int nType, int nVersion) { \
187 SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
193 inline unsigned int GetSerializeSize(char a, int, int=0) { return 1; }
194 inline unsigned int GetSerializeSize(int8_t a, int, int=0) { return 1; }
195 inline unsigned int GetSerializeSize(uint8_t a, int, int=0) { return 1; }
196 inline unsigned int GetSerializeSize(int16_t a, int, int=0) { return 2; }
197 inline unsigned int GetSerializeSize(uint16_t a, int, int=0) { return 2; }
198 inline unsigned int GetSerializeSize(int32_t a, int, int=0) { return 4; }
199 inline unsigned int GetSerializeSize(uint32_t a, int, int=0) { return 4; }
200 inline unsigned int GetSerializeSize(int64_t a, int, int=0) { return 8; }
201 inline unsigned int GetSerializeSize(uint64_t a, int, int=0) { return 8; }
202 inline unsigned int GetSerializeSize(float a, int, int=0) { return 4; }
203 inline unsigned int GetSerializeSize(double a, int, int=0) { return 8; }
205 template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char
206 template<typename Stream> inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); }
207 template<typename Stream> inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); }
208 template<typename Stream> inline void Serialize(Stream& s, int16_t a, int, int=0) { ser_writedata16(s, a); }
209 template<typename Stream> inline void Serialize(Stream& s, uint16_t a, int, int=0) { ser_writedata16(s, a); }
210 template<typename Stream> inline void Serialize(Stream& s, int32_t a, int, int=0) { ser_writedata32(s, a); }
211 template<typename Stream> inline void Serialize(Stream& s, uint32_t a, int, int=0) { ser_writedata32(s, a); }
212 template<typename Stream> inline void Serialize(Stream& s, int64_t a, int, int=0) { ser_writedata64(s, a); }
213 template<typename Stream> inline void Serialize(Stream& s, uint64_t a, int, int=0) { ser_writedata64(s, a); }
214 template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { ser_writedata32(s, ser_float_to_uint32(a)); }
215 template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { ser_writedata64(s, ser_double_to_uint64(a)); }
217 template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { a = ser_readdata8(s); } // TODO Get rid of bare char
218 template<typename Stream> inline void Unserialize(Stream& s, int8_t& a, int, int=0) { a = ser_readdata8(s); }
219 template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a, int, int=0) { a = ser_readdata8(s); }
220 template<typename Stream> inline void Unserialize(Stream& s, int16_t& a, int, int=0) { a = ser_readdata16(s); }
221 template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a, int, int=0) { a = ser_readdata16(s); }
222 template<typename Stream> inline void Unserialize(Stream& s, int32_t& a, int, int=0) { a = ser_readdata32(s); }
223 template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a, int, int=0) { a = ser_readdata32(s); }
224 template<typename Stream> inline void Unserialize(Stream& s, int64_t& a, int, int=0) { a = ser_readdata64(s); }
225 template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a, int, int=0) { a = ser_readdata64(s); }
226 template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); }
227 template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); }
229 inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
230 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); }
231 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; }
240 * size < 253 -- 1 byte
241 * size <= 0xFFFF -- 3 bytes (253 + 2 bytes)
242 * size <= 0xFFFFFFFF -- 5 bytes (254 + 4 bytes)
243 * size > 0xFFFFFFFF -- 9 bytes (255 + 8 bytes)
245 inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
247 if (nSize < 253) return 1;
248 else if (nSize <= 0xFFFFu) return 3;
249 else if (nSize <= 0xFFFFFFFFu) return 5;
253 template<typename Stream>
254 void WriteCompactSize(Stream& os, uint64_t nSize)
258 ser_writedata8(os, nSize);
260 else if (nSize <= 0xFFFFu)
262 ser_writedata8(os, 253);
263 ser_writedata16(os, nSize);
265 else if (nSize <= 0xFFFFFFFFu)
267 ser_writedata8(os, 254);
268 ser_writedata32(os, nSize);
272 ser_writedata8(os, 255);
273 ser_writedata64(os, nSize);
277 template<typename Stream>
278 uint64_t ReadCompactSize(Stream& is)
280 uint8_t chSize = ser_readdata8(is);
281 uint64_t nSizeRet = 0;
286 else if (chSize == 253)
288 nSizeRet = ser_readdata16(is);
290 throw std::ios_base::failure("non-canonical ReadCompactSize()");
292 else if (chSize == 254)
294 nSizeRet = ser_readdata32(is);
295 if (nSizeRet < 0x10000u)
296 throw std::ios_base::failure("non-canonical ReadCompactSize()");
300 nSizeRet = ser_readdata64(is);
301 if (nSizeRet < 0x100000000ULL)
302 throw std::ios_base::failure("non-canonical ReadCompactSize()");
304 if (nSizeRet > (uint64_t)MAX_SIZE)
305 throw std::ios_base::failure("ReadCompactSize(): size too large");
310 * Variable-length integers: bytes are a MSB base-128 encoding of the number.
311 * The high bit in each byte signifies whether another digit follows. To make
312 * sure the encoding is one-to-one, one is subtracted from all but the last digit.
313 * Thus, the byte sequence a[] with length len, where all but the last byte
314 * has bit 128 set, encodes the number:
316 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
319 * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
320 * * Every integer has exactly one encoding
321 * * Encoding does not depend on size of original integer type
322 * * No redundancy: every (infinite) byte sequence corresponds to a list
323 * of encoded integers.
325 * 0: [0x00] 256: [0x81 0x00]
326 * 1: [0x01] 16383: [0xFE 0x7F]
327 * 127: [0x7F] 16384: [0xFF 0x00]
328 * 128: [0x80 0x00] 16511: [0x80 0xFF 0x7F]
329 * 255: [0x80 0x7F] 65535: [0x82 0xFD 0x7F]
330 * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
334 inline unsigned int GetSizeOfVarInt(I n)
346 template<typename Stream, typename I>
347 void WriteVarInt(Stream& os, I n)
349 unsigned char tmp[(sizeof(n)*8+6)/7];
352 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
359 ser_writedata8(os, tmp[len]);
363 template<typename Stream, typename I>
364 I ReadVarInt(Stream& is)
368 unsigned char chData = ser_readdata8(is);
369 n = (n << 7) | (chData & 0x7F);
377 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
378 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
379 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
382 * Wrapper for serializing arrays and POD.
390 CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
391 template <class T, class TAl>
392 explicit CFlatData(std::vector<T,TAl> &v)
394 pbegin = (char*)begin_ptr(v);
395 pend = (char*)end_ptr(v);
397 template <unsigned int N, typename T, typename S, typename D>
398 explicit CFlatData(prevector<N, T, S, D> &v)
400 pbegin = (char*)begin_ptr(v);
401 pend = (char*)end_ptr(v);
403 char* begin() { return pbegin; }
404 const char* begin() const { return pbegin; }
405 char* end() { return pend; }
406 const char* end() const { return pend; }
408 unsigned int GetSerializeSize(int, int=0) const
410 return pend - pbegin;
413 template<typename Stream>
414 void Serialize(Stream& s, int, int=0) const
416 s.write(pbegin, pend - pbegin);
419 template<typename Stream>
420 void Unserialize(Stream& s, int, int=0)
422 s.read(pbegin, pend - pbegin);
432 CVarInt(I& nIn) : n(nIn) { }
434 unsigned int GetSerializeSize(int, int) const {
435 return GetSizeOfVarInt<I>(n);
438 template<typename Stream>
439 void Serialize(Stream &s, int, int) const {
440 WriteVarInt<Stream,I>(s, n);
443 template<typename Stream>
444 void Unserialize(Stream& s, int, int) {
445 n = ReadVarInt<Stream,I>(s);
449 template<size_t Limit>
455 LimitedString(std::string& string) : string(string) {}
457 template<typename Stream>
458 void Unserialize(Stream& s, int, int=0)
460 size_t size = ReadCompactSize(s);
462 throw std::ios_base::failure("String length limit exceeded");
466 s.read((char*)&string[0], size);
469 template<typename Stream>
470 void Serialize(Stream& s, int, int=0) const
472 WriteCompactSize(s, string.size());
474 s.write((char*)&string[0], string.size());
477 unsigned int GetSerializeSize(int, int=0) const
479 return GetSizeOfCompactSize(string.size()) + string.size();
484 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
487 * Forward declarations
493 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
494 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
495 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
499 * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
501 template<unsigned int N, typename T> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
502 template<unsigned int N, typename T, typename V> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&);
503 template<unsigned int N, typename T> inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion);
504 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&);
505 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&);
506 template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion);
507 template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
508 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&);
509 template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion);
513 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
515 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
516 template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
517 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
518 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&);
519 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&);
520 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
521 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
522 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&);
523 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
528 template<typename T> unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion);
529 template<typename Stream, typename T> void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion);
530 template<typename Stream, typename T> void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion);
535 template<typename T, std::size_t N> unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion);
536 template<typename Stream, typename T, std::size_t N> void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion);
537 template<typename Stream, typename T, std::size_t N> void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion);
542 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
543 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
544 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
549 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
550 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);
551 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);
556 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
557 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
558 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
563 template<typename T, typename A> unsigned int GetSerializeSize(const std::list<T, A>& m, int nType, int nVersion);
564 template<typename Stream, typename T, typename A> void Serialize(Stream& os, const std::list<T, A>& m, int nType, int nVersion);
565 template<typename Stream, typename T, typename A> void Unserialize(Stream& is, std::list<T, A>& m, int nType, int nVersion);
572 * If none of the specialized versions above matched, default to calling member function.
573 * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
574 * The compiler will only cast int to long if none of the other templates matched.
575 * Thanks to Boost serialization for this idea.
578 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
580 return a.GetSerializeSize((int)nType, nVersion);
583 template<typename Stream, typename T>
584 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
586 a.Serialize(os, (int)nType, nVersion);
589 template<typename Stream, typename T>
590 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
592 a.Unserialize(is, (int)nType, nVersion);
603 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
605 return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
608 template<typename Stream, typename C>
609 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
611 WriteCompactSize(os, str.size());
613 os.write((char*)&str[0], str.size() * sizeof(str[0]));
616 template<typename Stream, typename C>
617 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
619 unsigned int nSize = ReadCompactSize(is);
622 is.read((char*)&str[0], nSize * sizeof(str[0]));
630 template<unsigned int N, typename T>
631 unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
633 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
636 template<unsigned int N, typename T, typename V>
637 unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&)
639 unsigned int nSize = GetSizeOfCompactSize(v.size());
640 for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
641 nSize += GetSerializeSize((*vi), nType, nVersion);
645 template<unsigned int N, typename T>
646 inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion)
648 return GetSerializeSize_impl(v, nType, nVersion, T());
652 template<typename Stream, unsigned int N, typename T>
653 void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
655 WriteCompactSize(os, v.size());
657 os.write((char*)&v[0], v.size() * sizeof(T));
660 template<typename Stream, unsigned int N, typename T, typename V>
661 void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&)
663 WriteCompactSize(os, v.size());
664 for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
665 ::Serialize(os, (*vi), nType, nVersion);
668 template<typename Stream, unsigned int N, typename T>
669 inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion)
671 Serialize_impl(os, v, nType, nVersion, T());
675 template<typename Stream, unsigned int N, typename T>
676 void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
678 // Limit size per read so bogus size value won't cause out of memory
680 unsigned int nSize = ReadCompactSize(is);
684 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
686 is.read((char*)&v[i], blk * sizeof(T));
691 template<typename Stream, unsigned int N, typename T, typename V>
692 void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&)
695 unsigned int nSize = ReadCompactSize(is);
697 unsigned int nMid = 0;
700 nMid += 5000000 / sizeof(T);
704 for (; i < nMid; i++)
705 Unserialize(is, v[i], nType, nVersion);
709 template<typename Stream, unsigned int N, typename T>
710 inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion)
712 Unserialize_impl(is, v, nType, nVersion, T());
720 template<typename T, typename A>
721 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
723 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
726 template<typename T, typename A, typename V>
727 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
729 unsigned int nSize = GetSizeOfCompactSize(v.size());
730 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
731 nSize += GetSerializeSize((*vi), nType, nVersion);
735 template<typename T, typename A>
736 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
738 return GetSerializeSize_impl(v, nType, nVersion, T());
742 template<typename Stream, typename T, typename A>
743 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
745 WriteCompactSize(os, v.size());
747 os.write((char*)&v[0], v.size() * sizeof(T));
750 template<typename Stream, typename T, typename A, typename V>
751 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
753 WriteCompactSize(os, v.size());
754 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
755 ::Serialize(os, (*vi), nType, nVersion);
758 template<typename Stream, typename T, typename A>
759 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
761 Serialize_impl(os, v, nType, nVersion, T());
765 template<typename Stream, typename T, typename A>
766 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
768 // Limit size per read so bogus size value won't cause out of memory
770 unsigned int nSize = ReadCompactSize(is);
774 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
776 is.read((char*)&v[i], blk * sizeof(T));
781 template<typename Stream, typename T, typename A, typename V>
782 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
785 unsigned int nSize = ReadCompactSize(is);
787 unsigned int nMid = 0;
790 nMid += 5000000 / sizeof(T);
794 for (; i < nMid; i++)
795 Unserialize(is, v[i], nType, nVersion);
799 template<typename Stream, typename T, typename A>
800 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
802 Unserialize_impl(is, v, nType, nVersion, T());
811 unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion)
814 return 1 + GetSerializeSize(*item, nType, nVersion);
820 template<typename Stream, typename T>
821 void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion)
823 // If the value is there, put 0x01 and then serialize the value.
824 // If it's not, put 0x00.
826 unsigned char discriminant = 0x01;
827 Serialize(os, discriminant, nType, nVersion);
828 Serialize(os, *item, nType, nVersion);
830 unsigned char discriminant = 0x00;
831 Serialize(os, discriminant, nType, nVersion);
835 template<typename Stream, typename T>
836 void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion)
838 unsigned char discriminant = 0x00;
839 Unserialize(is, discriminant, nType, nVersion);
841 if (discriminant == 0x00) {
843 } else if (discriminant == 0x01) {
845 Unserialize(is, object, nType, nVersion);
848 throw std::ios_base::failure("non-canonical optional discriminant");
857 template<typename T, std::size_t N>
858 unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion)
860 unsigned int size = 0;
861 for (size_t i = 0; i < N; i++) {
862 size += GetSerializeSize(item[0], nType, nVersion);
867 template<typename Stream, typename T, std::size_t N>
868 void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion)
870 for (size_t i = 0; i < N; i++) {
871 Serialize(os, item[i], nType, nVersion);
875 template<typename Stream, typename T, std::size_t N>
876 void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion)
878 for (size_t i = 0; i < N; i++) {
879 Unserialize(is, item[i], nType, nVersion);
887 template<typename K, typename T>
888 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
890 return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
893 template<typename Stream, typename K, typename T>
894 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
896 Serialize(os, item.first, nType, nVersion);
897 Serialize(os, item.second, nType, nVersion);
900 template<typename Stream, typename K, typename T>
901 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
903 Unserialize(is, item.first, nType, nVersion);
904 Unserialize(is, item.second, nType, nVersion);
912 template<typename K, typename T, typename Pred, typename A>
913 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
915 unsigned int nSize = GetSizeOfCompactSize(m.size());
916 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
917 nSize += GetSerializeSize((*mi), nType, nVersion);
921 template<typename Stream, typename K, typename T, typename Pred, typename A>
922 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
924 WriteCompactSize(os, m.size());
925 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
926 Serialize(os, (*mi), nType, nVersion);
929 template<typename Stream, typename K, typename T, typename Pred, typename A>
930 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
933 unsigned int nSize = ReadCompactSize(is);
934 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
935 for (unsigned int i = 0; i < nSize; i++)
937 std::pair<K, T> item;
938 Unserialize(is, item, nType, nVersion);
939 mi = m.insert(mi, item);
948 template<typename K, typename Pred, typename A>
949 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
951 unsigned int nSize = GetSizeOfCompactSize(m.size());
952 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
953 nSize += GetSerializeSize((*it), nType, nVersion);
957 template<typename Stream, typename K, typename Pred, typename A>
958 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
960 WriteCompactSize(os, m.size());
961 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
962 Serialize(os, (*it), nType, nVersion);
965 template<typename Stream, typename K, typename Pred, typename A>
966 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
969 unsigned int nSize = ReadCompactSize(is);
970 typename std::set<K, Pred, A>::iterator it = m.begin();
971 for (unsigned int i = 0; i < nSize; i++)
974 Unserialize(is, key, nType, nVersion);
975 it = m.insert(it, key);
984 template<typename T, typename A>
985 unsigned int GetSerializeSize(const std::list<T, A>& l, int nType, int nVersion)
987 unsigned int nSize = GetSizeOfCompactSize(l.size());
988 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
989 nSize += GetSerializeSize((*it), nType, nVersion);
993 template<typename Stream, typename T, typename A>
994 void Serialize(Stream& os, const std::list<T, A>& l, int nType, int nVersion)
996 WriteCompactSize(os, l.size());
997 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
998 Serialize(os, (*it), nType, nVersion);
1001 template<typename Stream, typename T, typename A>
1002 void Unserialize(Stream& is, std::list<T, A>& l, int nType, int nVersion)
1005 unsigned int nSize = ReadCompactSize(is);
1006 typename std::list<T, A>::iterator it = l.begin();
1007 for (unsigned int i = 0; i < nSize; i++)
1010 Unserialize(is, item, nType, nVersion);
1018 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
1020 struct CSerActionSerialize
1022 bool ForRead() const { return false; }
1024 struct CSerActionUnserialize
1026 bool ForRead() const { return true; }
1029 template<typename Stream, typename T>
1030 inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
1032 ::Serialize(s, obj, nType, nVersion);
1035 template<typename Stream, typename T>
1036 inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
1038 ::Unserialize(s, obj, nType, nVersion);
1058 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
1060 CSizeComputer& write(const char *psz, size_t nSize)
1062 this->nSize += nSize;
1066 template<typename T>
1067 CSizeComputer& operator<<(const T& obj)
1069 ::Serialize(*this, obj, nType, nVersion);
1073 size_t size() const {
1078 #endif // BITCOIN_SERIALIZE_H