Merge branch 'dev' into jl777
[VerusCoin.git] / src / serialize.h
CommitLineData
0a61b0df 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2014 The Bitcoin Core developers
1c0aa911 3// Distributed under the MIT software license, see the accompanying
3a25a2b9 4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
51ed9ec9 5
223b6f1b
WL
6#ifndef BITCOIN_SERIALIZE_H
7#define BITCOIN_SERIALIZE_H
0a61b0df 8
5207f33f
PK
9#include "compat/endian.h"
10
51ed9ec9
BD
11#include <algorithm>
12#include <assert.h>
51ed9ec9 13#include <ios>
611116d4 14#include <limits>
be74c80d 15#include <list>
0a61b0df 16#include <map>
d743f035 17#include <set>
51ed9ec9
BD
18#include <stdint.h>
19#include <string>
20#include <string.h>
21#include <utility>
22#include <vector>
223b6f1b 23
5884044b 24#include <boost/array.hpp>
291b191b 25#include <boost/optional.hpp>
5884044b 26
51ed9ec9
BD
27class CScript;
28
0a61b0df 29static const unsigned int MAX_SIZE = 0x02000000;
30
1c0aa911
MF
31/**
32 * Used to bypass the rule against non-const reference to temporary
33 * where it makes sense with wrappers such as CFlatData or CTxDB
34 */
82dc6426
GS
35template<typename T>
36inline T& REF(const T& val)
37{
38 return const_cast<T&>(val);
39}
0a61b0df 40
1c0aa911
MF
41/**
42 * Used to acquire a non-const pointer "this" to generate bodies
43 * of const serialization operations from a template
44 */
3d796f89 45template<typename T>
84881f8c 46inline T* NCONST_PTR(const T* val)
3d796f89 47{
84881f8c 48 return const_cast<T*>(val);
3d796f89
KD
49}
50
1c0aa911
MF
51/**
52 * Get begin pointer of vector (non-const version).
fa126eff
WL
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.
55 */
56template <class T, class TAl>
57inline T* begin_ptr(std::vector<T,TAl>& v)
58{
59 return v.empty() ? NULL : &v[0];
60}
61/** Get begin pointer of vector (const version) */
62template <class T, class TAl>
63inline const T* begin_ptr(const std::vector<T,TAl>& v)
64{
65 return v.empty() ? NULL : &v[0];
66}
67/** Get end pointer of vector (non-const version) */
68template <class T, class TAl>
69inline T* end_ptr(std::vector<T,TAl>& v)
70{
71 return v.empty() ? NULL : (&v[0] + v.size());
72}
73/** Get end pointer of vector (const version) */
74template <class T, class TAl>
75inline const T* end_ptr(const std::vector<T,TAl>& v)
76{
77 return v.empty() ? NULL : (&v[0] + v.size());
78}
79
01f9c344
WL
80/*
81 * Lowest-level serialization and conversion.
82 * @note Sizes of these types are verified in the tests
83 */
84template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
85{
86 s.write((char*)&obj, 1);
87}
88template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
89{
90 obj = htole16(obj);
91 s.write((char*)&obj, 2);
92}
93template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
94{
95 obj = htole32(obj);
96 s.write((char*)&obj, 4);
97}
98template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
99{
100 obj = htole64(obj);
101 s.write((char*)&obj, 8);
102}
103template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
104{
105 uint8_t obj;
106 s.read((char*)&obj, 1);
107 return obj;
108}
109template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
110{
111 uint16_t obj;
112 s.read((char*)&obj, 2);
113 return le16toh(obj);
114}
115template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
116{
117 uint32_t obj;
118 s.read((char*)&obj, 4);
119 return le32toh(obj);
120}
121template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
122{
123 uint64_t obj;
124 s.read((char*)&obj, 8);
125 return le64toh(obj);
126}
127inline uint64_t ser_double_to_uint64(double x)
128{
129 union { double x; uint64_t y; } tmp;
130 tmp.x = x;
131 return tmp.y;
132}
133inline uint32_t ser_float_to_uint32(float x)
134{
135 union { float x; uint32_t y; } tmp;
136 tmp.x = x;
137 return tmp.y;
138}
139inline double ser_uint64_to_double(uint64_t y)
140{
141 union { double x; uint64_t y; } tmp;
142 tmp.y = y;
143 return tmp.x;
144}
145inline float ser_uint32_to_float(uint32_t y)
146{
147 union { float x; uint32_t y; } tmp;
148 tmp.y = y;
149 return tmp.x;
150}
151
152
0a61b0df 153/////////////////////////////////////////////////////////////////
154//
155// Templates for serializing to anything that looks like a stream,
8695a393 156// i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
0a61b0df 157//
158
159enum
160{
161 // primary actions
162 SER_NETWORK = (1 << 0),
163 SER_DISK = (1 << 1),
164 SER_GETHASH = (1 << 2),
0a61b0df 165};
166
31e9a838 167#define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
0a61b0df 168
1c0aa911
MF
169/**
170 * Implement three methods for serializable objects. These are actually wrappers over
3d796f89 171 * "SerializationOp" template, which implements the body of each class' serialization
3f6540ad 172 * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
1c0aa911
MF
173 * added as members.
174 */
3f6540ad 175#define ADD_SERIALIZE_METHODS \
31e9a838
PW
176 size_t GetSerializeSize(int nType, int nVersion) const { \
177 CSizeComputer s(nType, nVersion); \
178 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
179 return s.size(); \
180 } \
181 template<typename Stream> \
182 void Serialize(Stream& s, int nType, int nVersion) const { \
183 NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
184 } \
185 template<typename Stream> \
186 void Unserialize(Stream& s, int nType, int nVersion) { \
187 SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
0a61b0df 188 }
189
1c0aa911
MF
190/*
191 * Basic Types
192 */
01f9c344
WL
193inline unsigned int GetSerializeSize(char a, int, int=0) { return 1; }
194inline unsigned int GetSerializeSize(int8_t a, int, int=0) { return 1; }
195inline unsigned int GetSerializeSize(uint8_t a, int, int=0) { return 1; }
196inline unsigned int GetSerializeSize(int16_t a, int, int=0) { return 2; }
197inline unsigned int GetSerializeSize(uint16_t a, int, int=0) { return 2; }
198inline unsigned int GetSerializeSize(int32_t a, int, int=0) { return 4; }
199inline unsigned int GetSerializeSize(uint32_t a, int, int=0) { return 4; }
200inline unsigned int GetSerializeSize(int64_t a, int, int=0) { return 8; }
201inline unsigned int GetSerializeSize(uint64_t a, int, int=0) { return 8; }
202inline unsigned int GetSerializeSize(float a, int, int=0) { return 4; }
203inline unsigned int GetSerializeSize(double a, int, int=0) { return 8; }
204
205template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char
206template<typename Stream> inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); }
207template<typename Stream> inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); }
208template<typename Stream> inline void Serialize(Stream& s, int16_t a, int, int=0) { ser_writedata16(s, a); }
209template<typename Stream> inline void Serialize(Stream& s, uint16_t a, int, int=0) { ser_writedata16(s, a); }
210template<typename Stream> inline void Serialize(Stream& s, int32_t a, int, int=0) { ser_writedata32(s, a); }
211template<typename Stream> inline void Serialize(Stream& s, uint32_t a, int, int=0) { ser_writedata32(s, a); }
212template<typename Stream> inline void Serialize(Stream& s, int64_t a, int, int=0) { ser_writedata64(s, a); }
213template<typename Stream> inline void Serialize(Stream& s, uint64_t a, int, int=0) { ser_writedata64(s, a); }
214template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { ser_writedata32(s, ser_float_to_uint32(a)); }
215template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { ser_writedata64(s, ser_double_to_uint64(a)); }
216
217template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { a = ser_readdata8(s); } // TODO Get rid of bare char
218template<typename Stream> inline void Unserialize(Stream& s, int8_t& a, int, int=0) { a = ser_readdata8(s); }
219template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a, int, int=0) { a = ser_readdata8(s); }
220template<typename Stream> inline void Unserialize(Stream& s, int16_t& a, int, int=0) { a = ser_readdata16(s); }
221template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a, int, int=0) { a = ser_readdata16(s); }
222template<typename Stream> inline void Unserialize(Stream& s, int32_t& a, int, int=0) { a = ser_readdata32(s); }
223template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a, int, int=0) { a = ser_readdata32(s); }
224template<typename Stream> inline void Unserialize(Stream& s, int64_t& a, int, int=0) { a = ser_readdata64(s); }
225template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a, int, int=0) { a = ser_readdata64(s); }
226template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); }
227template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); }
0a61b0df 228
229inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
01f9c344
WL
230template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); }
231template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; }
0a61b0df 232
233
234
235
236
237
1c0aa911
MF
238/**
239 * Compact Size
240 * size < 253 -- 1 byte
d76ed723
DH
241 * size <= 0xFFFF -- 3 bytes (253 + 2 bytes)
242 * size <= 0xFFFFFFFF -- 5 bytes (254 + 4 bytes)
243 * size > 0xFFFFFFFF -- 9 bytes (255 + 8 bytes)
1c0aa911 244 */
51ed9ec9 245inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
0a61b0df 246{
d76ed723
DH
247 if (nSize < 253) return 1;
248 else if (nSize <= 0xFFFFu) return 3;
249 else if (nSize <= 0xFFFFFFFFu) return 5;
250 else return 9;
0a61b0df 251}
252
253template<typename Stream>
51ed9ec9 254void WriteCompactSize(Stream& os, uint64_t nSize)
0a61b0df 255{
a790fa46 256 if (nSize < 253)
0a61b0df 257 {
01f9c344 258 ser_writedata8(os, nSize);
0a61b0df 259 }
d76ed723 260 else if (nSize <= 0xFFFFu)
0a61b0df 261 {
01f9c344
WL
262 ser_writedata8(os, 253);
263 ser_writedata16(os, nSize);
0a61b0df 264 }
d76ed723 265 else if (nSize <= 0xFFFFFFFFu)
0a61b0df 266 {
01f9c344
WL
267 ser_writedata8(os, 254);
268 ser_writedata32(os, nSize);
0a61b0df 269 }
270 else
271 {
01f9c344
WL
272 ser_writedata8(os, 255);
273 ser_writedata64(os, nSize);
0a61b0df 274 }
0a61b0df 275}
276
277template<typename Stream>
51ed9ec9 278uint64_t ReadCompactSize(Stream& is)
0a61b0df 279{
01f9c344 280 uint8_t chSize = ser_readdata8(is);
51ed9ec9 281 uint64_t nSizeRet = 0;
a790fa46 282 if (chSize < 253)
0a61b0df 283 {
284 nSizeRet = chSize;
285 }
a790fa46 286 else if (chSize == 253)
0a61b0df 287 {
01f9c344 288 nSizeRet = ser_readdata16(is);
8dc206a1
GA
289 if (nSizeRet < 253)
290 throw std::ios_base::failure("non-canonical ReadCompactSize()");
0a61b0df 291 }
a790fa46 292 else if (chSize == 254)
0a61b0df 293 {
01f9c344 294 nSizeRet = ser_readdata32(is);
8dc206a1
GA
295 if (nSizeRet < 0x10000u)
296 throw std::ios_base::failure("non-canonical ReadCompactSize()");
0a61b0df 297 }
298 else
299 {
01f9c344 300 nSizeRet = ser_readdata64(is);
775b7b8d 301 if (nSizeRet < 0x100000000ULL)
8dc206a1 302 throw std::ios_base::failure("non-canonical ReadCompactSize()");
0a61b0df 303 }
51ed9ec9 304 if (nSizeRet > (uint64_t)MAX_SIZE)
5262fde0 305 throw std::ios_base::failure("ReadCompactSize(): size too large");
0a61b0df 306 return nSizeRet;
307}
308
1c0aa911
MF
309/**
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:
315 *
316 * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
317 *
318 * Properties:
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.
324 *
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]
331 */
4d6144f9
PW
332
333template<typename I>
334inline unsigned int GetSizeOfVarInt(I n)
335{
336 int nRet = 0;
337 while(true) {
338 nRet++;
339 if (n <= 0x7F)
340 break;
341 n = (n >> 7) - 1;
342 }
343 return nRet;
344}
345
346template<typename Stream, typename I>
347void WriteVarInt(Stream& os, I n)
348{
349 unsigned char tmp[(sizeof(n)*8+6)/7];
350 int len=0;
351 while(true) {
352 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
353 if (n <= 0x7F)
354 break;
355 n = (n >> 7) - 1;
356 len++;
357 }
358 do {
01f9c344 359 ser_writedata8(os, tmp[len]);
4d6144f9
PW
360 } while(len--);
361}
0a61b0df 362
4d6144f9
PW
363template<typename Stream, typename I>
364I ReadVarInt(Stream& is)
365{
366 I n = 0;
367 while(true) {
01f9c344 368 unsigned char chData = ser_readdata8(is);
4d6144f9
PW
369 n = (n << 7) | (chData & 0x7F);
370 if (chData & 0x80)
371 n++;
372 else
373 return n;
374 }
375}
0a61b0df 376
216e9a44
PW
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)))
6b8de05d 380
1c0aa911
MF
381/**
382 * Wrapper for serializing arrays and POD.
6b8de05d 383 */
0a61b0df 384class CFlatData
385{
386protected:
387 char* pbegin;
388 char* pend;
389public:
390 CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
fa126eff
WL
391 template <class T, class TAl>
392 explicit CFlatData(std::vector<T,TAl> &v)
393 {
394 pbegin = (char*)begin_ptr(v);
395 pend = (char*)end_ptr(v);
396 }
0a61b0df 397 char* begin() { return pbegin; }
398 const char* begin() const { return pbegin; }
399 char* end() { return pend; }
400 const char* end() const { return pend; }
401
402 unsigned int GetSerializeSize(int, int=0) const
403 {
404 return pend - pbegin;
405 }
406
407 template<typename Stream>
408 void Serialize(Stream& s, int, int=0) const
409 {
410 s.write(pbegin, pend - pbegin);
411 }
412
413 template<typename Stream>
414 void Unserialize(Stream& s, int, int=0)
415 {
416 s.read(pbegin, pend - pbegin);
417 }
418};
419
4d6144f9
PW
420template<typename I>
421class CVarInt
422{
423protected:
424 I &n;
425public:
426 CVarInt(I& nIn) : n(nIn) { }
427
428 unsigned int GetSerializeSize(int, int) const {
429 return GetSizeOfVarInt<I>(n);
430 }
431
432 template<typename Stream>
433 void Serialize(Stream &s, int, int) const {
434 WriteVarInt<Stream,I>(s, n);
435 }
436
437 template<typename Stream>
438 void Unserialize(Stream& s, int, int) {
439 n = ReadVarInt<Stream,I>(s);
440 }
441};
442
216e9a44
PW
443template<size_t Limit>
444class LimitedString
445{
446protected:
447 std::string& string;
448public:
449 LimitedString(std::string& string) : string(string) {}
450
451 template<typename Stream>
452 void Unserialize(Stream& s, int, int=0)
453 {
454 size_t size = ReadCompactSize(s);
455 if (size > Limit) {
456 throw std::ios_base::failure("String length limit exceeded");
457 }
458 string.resize(size);
459 if (size != 0)
460 s.read((char*)&string[0], size);
461 }
462
463 template<typename Stream>
464 void Serialize(Stream& s, int, int=0) const
465 {
466 WriteCompactSize(s, string.size());
467 if (!string.empty())
468 s.write((char*)&string[0], string.size());
469 }
470
471 unsigned int GetSerializeSize(int, int=0) const
472 {
473 return GetSizeOfCompactSize(string.size()) + string.size();
474 }
475};
476
4d6144f9
PW
477template<typename I>
478CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
479
1c0aa911
MF
480/**
481 * Forward declarations
482 */
0a61b0df 483
1c0aa911
MF
484/**
485 * string
486 */
223b6f1b
WL
487template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
488template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
489template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
0a61b0df 490
1c0aa911
MF
491/**
492 * vector
493 * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
494 */
1d9b86d5
CF
495template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
496template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
6b6aaa16 497template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
1d9b86d5
CF
498template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
499template<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&);
6b6aaa16 500template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
1d9b86d5
CF
501template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
502template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&);
6b6aaa16 503template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
0a61b0df 504
1c0aa911
MF
505/**
506 * others derived from vector
507 */
6b6aaa16
PW
508extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion);
509template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion);
510template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion);
0a61b0df 511
291b191b
SB
512/**
513 * optional
514 */
515template<typename T> unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion);
516template<typename Stream, typename T> void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion);
517template<typename Stream, typename T> void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion);
518
5884044b
SB
519/**
520 * array
521 */
522template<typename T, std::size_t N> unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion);
523template<typename Stream, typename T, std::size_t N> void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion);
524template<typename Stream, typename T, std::size_t N> void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion);
525
1c0aa911
MF
526/**
527 * pair
528 */
6b6aaa16
PW
529template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
530template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
531template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
0a61b0df 532
1c0aa911
MF
533/**
534 * map
535 */
6b6aaa16
PW
536template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
537template<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);
538template<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);
0a61b0df 539
1c0aa911
MF
540/**
541 * set
542 */
6b6aaa16
PW
543template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
544template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
545template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
0a61b0df 546
be74c80d
JG
547/**
548 * list
549 */
550template<typename T, typename A> unsigned int GetSerializeSize(const std::list<T, A>& m, int nType, int nVersion);
551template<typename Stream, typename T, typename A> void Serialize(Stream& os, const std::list<T, A>& m, int nType, int nVersion);
552template<typename Stream, typename T, typename A> void Unserialize(Stream& is, std::list<T, A>& m, int nType, int nVersion);
553
0a61b0df 554
555
556
557
1c0aa911
MF
558/**
559 * If none of the specialized versions above matched, default to calling member function.
560 * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
561 * The compiler will only cast int to long if none of the other templates matched.
562 * Thanks to Boost serialization for this idea.
563 */
0a61b0df 564template<typename T>
6b6aaa16 565inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
0a61b0df 566{
567 return a.GetSerializeSize((int)nType, nVersion);
568}
569
570template<typename Stream, typename T>
6b6aaa16 571inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
0a61b0df 572{
573 a.Serialize(os, (int)nType, nVersion);
574}
575
576template<typename Stream, typename T>
6b6aaa16 577inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
0a61b0df 578{
579 a.Unserialize(is, (int)nType, nVersion);
580}
581
582
583
584
585
1c0aa911
MF
586/**
587 * string
588 */
0a61b0df 589template<typename C>
223b6f1b 590unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
0a61b0df 591{
592 return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
593}
594
595template<typename Stream, typename C>
223b6f1b 596void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
0a61b0df 597{
598 WriteCompactSize(os, str.size());
599 if (!str.empty())
600 os.write((char*)&str[0], str.size() * sizeof(str[0]));
601}
602
603template<typename Stream, typename C>
223b6f1b 604void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
0a61b0df 605{
606 unsigned int nSize = ReadCompactSize(is);
607 str.resize(nSize);
608 if (nSize != 0)
609 is.read((char*)&str[0], nSize * sizeof(str[0]));
610}
611
612
613
1c0aa911
MF
614/**
615 * vector
616 */
0a61b0df 617template<typename T, typename A>
1d9b86d5 618unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
0a61b0df 619{
620 return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
621}
622
1d9b86d5
CF
623template<typename T, typename A, typename V>
624unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
0a61b0df 625{
626 unsigned int nSize = GetSizeOfCompactSize(v.size());
627 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
628 nSize += GetSerializeSize((*vi), nType, nVersion);
629 return nSize;
630}
631
632template<typename T, typename A>
633inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
634{
1d9b86d5 635 return GetSerializeSize_impl(v, nType, nVersion, T());
0a61b0df 636}
637
638
639template<typename Stream, typename T, typename A>
1d9b86d5 640void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
0a61b0df 641{
642 WriteCompactSize(os, v.size());
643 if (!v.empty())
644 os.write((char*)&v[0], v.size() * sizeof(T));
645}
646
1d9b86d5
CF
647template<typename Stream, typename T, typename A, typename V>
648void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
0a61b0df 649{
650 WriteCompactSize(os, v.size());
651 for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
652 ::Serialize(os, (*vi), nType, nVersion);
653}
654
655template<typename Stream, typename T, typename A>
656inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
657{
1d9b86d5 658 Serialize_impl(os, v, nType, nVersion, T());
0a61b0df 659}
660
661
662template<typename Stream, typename T, typename A>
1d9b86d5 663void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
0a61b0df 664{
0a61b0df 665 // Limit size per read so bogus size value won't cause out of memory
666 v.clear();
667 unsigned int nSize = ReadCompactSize(is);
668 unsigned int i = 0;
669 while (i < nSize)
670 {
223b6f1b 671 unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
0a61b0df 672 v.resize(i + blk);
673 is.read((char*)&v[i], blk * sizeof(T));
674 i += blk;
675 }
676}
677
1d9b86d5
CF
678template<typename Stream, typename T, typename A, typename V>
679void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
0a61b0df 680{
0a61b0df 681 v.clear();
682 unsigned int nSize = ReadCompactSize(is);
683 unsigned int i = 0;
684 unsigned int nMid = 0;
685 while (nMid < nSize)
686 {
687 nMid += 5000000 / sizeof(T);
688 if (nMid > nSize)
689 nMid = nSize;
690 v.resize(nMid);
691 for (; i < nMid; i++)
692 Unserialize(is, v[i], nType, nVersion);
693 }
694}
695
696template<typename Stream, typename T, typename A>
697inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
698{
1d9b86d5 699 Unserialize_impl(is, v, nType, nVersion, T());
0a61b0df 700}
701
702
703
1c0aa911
MF
704/**
705 * others derived from vector
706 */
0a61b0df 707inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
708{
223b6f1b 709 return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
0a61b0df 710}
711
712template<typename Stream>
713void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
714{
223b6f1b 715 Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
0a61b0df 716}
717
718template<typename Stream>
719void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
720{
223b6f1b 721 Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
0a61b0df 722}
723
724
291b191b
SB
725
726/**
727 * optional
728 */
729template<typename T>
730unsigned int GetSerializeSize(const boost::optional<T> &item, int nType, int nVersion)
731{
732 if (item) {
733 return 1 + GetSerializeSize(*item, nType, nVersion);
734 } else {
735 return 1;
736 }
737}
738
739template<typename Stream, typename T>
740void Serialize(Stream& os, const boost::optional<T>& item, int nType, int nVersion)
741{
742 // If the value is there, put 0x01 and then serialize the value.
743 // If it's not, put 0x00.
744 if (item) {
745 unsigned char discriminant = 0x01;
746 Serialize(os, discriminant, nType, nVersion);
747 Serialize(os, *item, nType, nVersion);
748 } else {
749 unsigned char discriminant = 0x00;
750 Serialize(os, discriminant, nType, nVersion);
751 }
752}
753
754template<typename Stream, typename T>
755void Unserialize(Stream& is, boost::optional<T>& item, int nType, int nVersion)
756{
757 unsigned char discriminant = 0x00;
758 Unserialize(is, discriminant, nType, nVersion);
759
760 if (discriminant == 0x00) {
761 item = boost::none;
e1ff849d 762 } else if (discriminant == 0x01) {
291b191b
SB
763 T object;
764 Unserialize(is, object, nType, nVersion);
765 item = object;
e1ff849d
SB
766 } else {
767 throw std::ios_base::failure("non-canonical optional discriminant");
291b191b
SB
768 }
769}
770
771
772
5884044b
SB
773/**
774 * array
775 */
776template<typename T, std::size_t N>
777unsigned int GetSerializeSize(const boost::array<T, N> &item, int nType, int nVersion)
778{
779 unsigned int size = 0;
780 for (size_t i = 0; i < N; i++) {
781 size += GetSerializeSize(item[0], nType, nVersion);
782 }
783 return size;
784}
785
786template<typename Stream, typename T, std::size_t N>
787void Serialize(Stream& os, const boost::array<T, N>& item, int nType, int nVersion)
788{
789 for (size_t i = 0; i < N; i++) {
790 Serialize(os, item[i], nType, nVersion);
791 }
792}
793
794template<typename Stream, typename T, std::size_t N>
795void Unserialize(Stream& is, boost::array<T, N>& item, int nType, int nVersion)
796{
797 for (size_t i = 0; i < N; i++) {
798 Unserialize(is, item[i], nType, nVersion);
799 }
800}
801
0a61b0df 802
1c0aa911
MF
803/**
804 * pair
805 */
0a61b0df 806template<typename K, typename T>
807unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
808{
809 return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
810}
811
812template<typename Stream, typename K, typename T>
813void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
814{
815 Serialize(os, item.first, nType, nVersion);
816 Serialize(os, item.second, nType, nVersion);
817}
818
819template<typename Stream, typename K, typename T>
820void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
821{
822 Unserialize(is, item.first, nType, nVersion);
823 Unserialize(is, item.second, nType, nVersion);
824}
825
826
827
1c0aa911
MF
828/**
829 * map
830 */
0a61b0df 831template<typename K, typename T, typename Pred, typename A>
832unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
833{
834 unsigned int nSize = GetSizeOfCompactSize(m.size());
835 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
836 nSize += GetSerializeSize((*mi), nType, nVersion);
837 return nSize;
838}
839
840template<typename Stream, typename K, typename T, typename Pred, typename A>
841void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
842{
843 WriteCompactSize(os, m.size());
844 for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
845 Serialize(os, (*mi), nType, nVersion);
846}
847
848template<typename Stream, typename K, typename T, typename Pred, typename A>
849void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
850{
851 m.clear();
852 unsigned int nSize = ReadCompactSize(is);
853 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
854 for (unsigned int i = 0; i < nSize; i++)
855 {
223b6f1b 856 std::pair<K, T> item;
0a61b0df 857 Unserialize(is, item, nType, nVersion);
858 mi = m.insert(mi, item);
859 }
860}
861
862
863
1c0aa911
MF
864/**
865 * set
866 */
0a61b0df 867template<typename K, typename Pred, typename A>
868unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
869{
870 unsigned int nSize = GetSizeOfCompactSize(m.size());
871 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
872 nSize += GetSerializeSize((*it), nType, nVersion);
873 return nSize;
874}
875
876template<typename Stream, typename K, typename Pred, typename A>
877void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
878{
879 WriteCompactSize(os, m.size());
880 for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
881 Serialize(os, (*it), nType, nVersion);
882}
883
884template<typename Stream, typename K, typename Pred, typename A>
885void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
886{
887 m.clear();
888 unsigned int nSize = ReadCompactSize(is);
889 typename std::set<K, Pred, A>::iterator it = m.begin();
890 for (unsigned int i = 0; i < nSize; i++)
891 {
892 K key;
893 Unserialize(is, key, nType, nVersion);
894 it = m.insert(it, key);
895 }
896}
897
898
899
be74c80d
JG
900/**
901 * list
902 */
903template<typename T, typename A>
904unsigned int GetSerializeSize(const std::list<T, A>& l, int nType, int nVersion)
905{
906 unsigned int nSize = GetSizeOfCompactSize(l.size());
907 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
908 nSize += GetSerializeSize((*it), nType, nVersion);
909 return nSize;
910}
911
912template<typename Stream, typename T, typename A>
913void Serialize(Stream& os, const std::list<T, A>& l, int nType, int nVersion)
914{
915 WriteCompactSize(os, l.size());
916 for (typename std::list<T, A>::const_iterator it = l.begin(); it != l.end(); ++it)
917 Serialize(os, (*it), nType, nVersion);
918}
919
920template<typename Stream, typename T, typename A>
921void Unserialize(Stream& is, std::list<T, A>& l, int nType, int nVersion)
922{
923 l.clear();
924 unsigned int nSize = ReadCompactSize(is);
925 typename std::list<T, A>::iterator it = l.begin();
926 for (unsigned int i = 0; i < nSize; i++)
927 {
928 T item;
929 Unserialize(is, item, nType, nVersion);
930 l.push_back(item);
931 }
932}
933
934
935
1c0aa911
MF
936/**
937 * Support for ADD_SERIALIZE_METHODS and READWRITE macro
938 */
31e9a838 939struct CSerActionSerialize
0a61b0df 940{
31e9a838
PW
941 bool ForRead() const { return false; }
942};
943struct CSerActionUnserialize
944{
945 bool ForRead() const { return true; }
946};
0a61b0df 947
948template<typename Stream, typename T>
31e9a838 949inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
0a61b0df 950{
951 ::Serialize(s, obj, nType, nVersion);
0a61b0df 952}
953
954template<typename Stream, typename T>
31e9a838 955inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
0a61b0df 956{
957 ::Unserialize(s, obj, nType, nVersion);
0a61b0df 958}
959
0a61b0df 960
961
962
963
964
965
f7a9a113
MC
966
967
b069750d
PW
968class CSizeComputer
969{
970protected:
971 size_t nSize;
972
973public:
974 int nType;
975 int nVersion;
976
977 CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
978
8695a393 979 CSizeComputer& write(const char *psz, size_t nSize)
b069750d
PW
980 {
981 this->nSize += nSize;
982 return *this;
983 }
984
985 template<typename T>
986 CSizeComputer& operator<<(const T& obj)
987 {
988 ::Serialize(*this, obj, nType, nVersion);
989 return (*this);
990 }
991
992 size_t size() const {
993 return nSize;
994 }
995};
996
093303a8 997#endif // BITCOIN_SERIALIZE_H
This page took 0.38106 seconds and 4 git commands to generate.