Add const bool VERSION_IS_BETA to serialize.h and use it when formatting the version...
[novacoin.git] / serialize.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <string>
6 #include <vector>
7 #include <map>
8 #include <set>
9 #include <boost/type_traits/is_fundamental.hpp>
10 #include <boost/tuple/tuple.hpp>
11 #include <boost/tuple/tuple_comparison.hpp>
12 #include <boost/tuple/tuple_io.hpp>
13 #if defined(_MSC_VER) || defined(__BORLANDC__)
14 typedef __int64  int64;
15 typedef unsigned __int64  uint64;
16 #else
17 typedef long long  int64;
18 typedef unsigned long long  uint64;
19 #endif
20 #if defined(_MSC_VER) && _MSC_VER < 1300
21 #define for  if (false) ; else for
22 #endif
23 class CScript;
24 class CDataStream;
25 class CAutoFile;
26 static const unsigned int MAX_SIZE = 0x02000000;
27
28 static const int VERSION = 31902;
29 static const char* pszSubVer = "";
30 static const bool VERSION_IS_BETA = true;
31
32
33
34
35
36
37 /////////////////////////////////////////////////////////////////
38 //
39 // Templates for serializing to anything that looks like a stream,
40 // i.e. anything that supports .read(char*, int) and .write(char*, int)
41 //
42
43 enum
44 {
45     // primary actions
46     SER_NETWORK         = (1 << 0),
47     SER_DISK            = (1 << 1),
48     SER_GETHASH         = (1 << 2),
49
50     // modifiers
51     SER_SKIPSIG         = (1 << 16),
52     SER_BLOCKHEADERONLY = (1 << 17),
53 };
54
55 #define IMPLEMENT_SERIALIZE(statements)    \
56     unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const  \
57     {                                           \
58         CSerActionGetSerializeSize ser_action;  \
59         const bool fGetSize = true;             \
60         const bool fWrite = false;              \
61         const bool fRead = false;               \
62         unsigned int nSerSize = 0;              \
63         ser_streamplaceholder s;                \
64         s.nType = nType;                        \
65         s.nVersion = nVersion;                  \
66         {statements}                            \
67         return nSerSize;                        \
68     }                                           \
69     template<typename Stream>                   \
70     void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const  \
71     {                                           \
72         CSerActionSerialize ser_action;         \
73         const bool fGetSize = false;            \
74         const bool fWrite = true;               \
75         const bool fRead = false;               \
76         unsigned int nSerSize = 0;              \
77         {statements}                            \
78     }                                           \
79     template<typename Stream>                   \
80     void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)  \
81     {                                           \
82         CSerActionUnserialize ser_action;       \
83         const bool fGetSize = false;            \
84         const bool fWrite = false;              \
85         const bool fRead = true;                \
86         unsigned int nSerSize = 0;              \
87         {statements}                            \
88     }
89
90 #define READWRITE(obj)      (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))
91
92
93
94
95
96
97 //
98 // Basic types
99 //
100 #define WRITEDATA(s, obj)   s.write((char*)&(obj), sizeof(obj))
101 #define READDATA(s, obj)    s.read((char*)&(obj), sizeof(obj))
102
103 inline unsigned int GetSerializeSize(char a,           int, int=0) { return sizeof(a); }
104 inline unsigned int GetSerializeSize(signed char a,    int, int=0) { return sizeof(a); }
105 inline unsigned int GetSerializeSize(unsigned char a,  int, int=0) { return sizeof(a); }
106 inline unsigned int GetSerializeSize(signed short a,   int, int=0) { return sizeof(a); }
107 inline unsigned int GetSerializeSize(unsigned short a, int, int=0) { return sizeof(a); }
108 inline unsigned int GetSerializeSize(signed int a,     int, int=0) { return sizeof(a); }
109 inline unsigned int GetSerializeSize(unsigned int a,   int, int=0) { return sizeof(a); }
110 inline unsigned int GetSerializeSize(signed long a,    int, int=0) { return sizeof(a); }
111 inline unsigned int GetSerializeSize(unsigned long a,  int, int=0) { return sizeof(a); }
112 inline unsigned int GetSerializeSize(int64 a,          int, int=0) { return sizeof(a); }
113 inline unsigned int GetSerializeSize(uint64 a,         int, int=0) { return sizeof(a); }
114 inline unsigned int GetSerializeSize(float a,          int, int=0) { return sizeof(a); }
115 inline unsigned int GetSerializeSize(double a,         int, int=0) { return sizeof(a); }
116
117 template<typename Stream> inline void Serialize(Stream& s, char a,           int, int=0) { WRITEDATA(s, a); }
118 template<typename Stream> inline void Serialize(Stream& s, signed char a,    int, int=0) { WRITEDATA(s, a); }
119 template<typename Stream> inline void Serialize(Stream& s, unsigned char a,  int, int=0) { WRITEDATA(s, a); }
120 template<typename Stream> inline void Serialize(Stream& s, signed short a,   int, int=0) { WRITEDATA(s, a); }
121 template<typename Stream> inline void Serialize(Stream& s, unsigned short a, int, int=0) { WRITEDATA(s, a); }
122 template<typename Stream> inline void Serialize(Stream& s, signed int a,     int, int=0) { WRITEDATA(s, a); }
123 template<typename Stream> inline void Serialize(Stream& s, unsigned int a,   int, int=0) { WRITEDATA(s, a); }
124 template<typename Stream> inline void Serialize(Stream& s, signed long a,    int, int=0) { WRITEDATA(s, a); }
125 template<typename Stream> inline void Serialize(Stream& s, unsigned long a,  int, int=0) { WRITEDATA(s, a); }
126 template<typename Stream> inline void Serialize(Stream& s, int64 a,          int, int=0) { WRITEDATA(s, a); }
127 template<typename Stream> inline void Serialize(Stream& s, uint64 a,         int, int=0) { WRITEDATA(s, a); }
128 template<typename Stream> inline void Serialize(Stream& s, float a,          int, int=0) { WRITEDATA(s, a); }
129 template<typename Stream> inline void Serialize(Stream& s, double a,         int, int=0) { WRITEDATA(s, a); }
130
131 template<typename Stream> inline void Unserialize(Stream& s, char& a,           int, int=0) { READDATA(s, a); }
132 template<typename Stream> inline void Unserialize(Stream& s, signed char& a,    int, int=0) { READDATA(s, a); }
133 template<typename Stream> inline void Unserialize(Stream& s, unsigned char& a,  int, int=0) { READDATA(s, a); }
134 template<typename Stream> inline void Unserialize(Stream& s, signed short& a,   int, int=0) { READDATA(s, a); }
135 template<typename Stream> inline void Unserialize(Stream& s, unsigned short& a, int, int=0) { READDATA(s, a); }
136 template<typename Stream> inline void Unserialize(Stream& s, signed int& a,     int, int=0) { READDATA(s, a); }
137 template<typename Stream> inline void Unserialize(Stream& s, unsigned int& a,   int, int=0) { READDATA(s, a); }
138 template<typename Stream> inline void Unserialize(Stream& s, signed long& a,    int, int=0) { READDATA(s, a); }
139 template<typename Stream> inline void Unserialize(Stream& s, unsigned long& a,  int, int=0) { READDATA(s, a); }
140 template<typename Stream> inline void Unserialize(Stream& s, int64& a,          int, int=0) { READDATA(s, a); }
141 template<typename Stream> inline void Unserialize(Stream& s, uint64& a,         int, int=0) { READDATA(s, a); }
142 template<typename Stream> inline void Unserialize(Stream& s, float& a,          int, int=0) { READDATA(s, a); }
143 template<typename Stream> inline void Unserialize(Stream& s, double& a,         int, int=0) { READDATA(s, a); }
144
145 inline unsigned int GetSerializeSize(bool a, int, int=0)                          { return sizeof(char); }
146 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0)    { char f=a; WRITEDATA(s, f); }
147 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f; READDATA(s, f); a=f; }
148
149
150
151
152
153
154 //
155 // Compact size
156 //  size <  253        -- 1 byte
157 //  size <= USHRT_MAX  -- 3 bytes  (253 + 2 bytes)
158 //  size <= UINT_MAX   -- 5 bytes  (254 + 4 bytes)
159 //  size >  UINT_MAX   -- 9 bytes  (255 + 8 bytes)
160 //
161 inline unsigned int GetSizeOfCompactSize(uint64 nSize)
162 {
163     if (nSize < 253)             return sizeof(unsigned char);
164     else if (nSize <= USHRT_MAX) return sizeof(unsigned char) + sizeof(unsigned short);
165     else if (nSize <= UINT_MAX)  return sizeof(unsigned char) + sizeof(unsigned int);
166     else                         return sizeof(unsigned char) + sizeof(uint64);
167 }
168
169 template<typename Stream>
170 void WriteCompactSize(Stream& os, uint64 nSize)
171 {
172     if (nSize < 253)
173     {
174         unsigned char chSize = nSize;
175         WRITEDATA(os, chSize);
176     }
177     else if (nSize <= USHRT_MAX)
178     {
179         unsigned char chSize = 253;
180         unsigned short xSize = nSize;
181         WRITEDATA(os, chSize);
182         WRITEDATA(os, xSize);
183     }
184     else if (nSize <= UINT_MAX)
185     {
186         unsigned char chSize = 254;
187         unsigned int xSize = nSize;
188         WRITEDATA(os, chSize);
189         WRITEDATA(os, xSize);
190     }
191     else
192     {
193         unsigned char chSize = 255;
194         uint64 xSize = nSize;
195         WRITEDATA(os, chSize);
196         WRITEDATA(os, xSize);
197     }
198     return;
199 }
200
201 template<typename Stream>
202 uint64 ReadCompactSize(Stream& is)
203 {
204     unsigned char chSize;
205     READDATA(is, chSize);
206     uint64 nSizeRet = 0;
207     if (chSize < 253)
208     {
209         nSizeRet = chSize;
210     }
211     else if (chSize == 253)
212     {
213         unsigned short xSize;
214         READDATA(is, xSize);
215         nSizeRet = xSize;
216     }
217     else if (chSize == 254)
218     {
219         unsigned int xSize;
220         READDATA(is, xSize);
221         nSizeRet = xSize;
222     }
223     else
224     {
225         uint64 xSize;
226         READDATA(is, xSize);
227         nSizeRet = xSize;
228     }
229     if (nSizeRet > (uint64)MAX_SIZE)
230         throw std::ios_base::failure("ReadCompactSize() : size too large");
231     return nSizeRet;
232 }
233
234
235
236 //
237 // Wrapper for serializing arrays and POD
238 // There's a clever template way to make arrays serialize normally, but MSVC6 doesn't support it
239 //
240 #define FLATDATA(obj)   REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
241 class CFlatData
242 {
243 protected:
244     char* pbegin;
245     char* pend;
246 public:
247     CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
248     char* begin() { return pbegin; }
249     const char* begin() const { return pbegin; }
250     char* end() { return pend; }
251     const char* end() const { return pend; }
252
253     unsigned int GetSerializeSize(int, int=0) const
254     {
255         return pend - pbegin;
256     }
257
258     template<typename Stream>
259     void Serialize(Stream& s, int, int=0) const
260     {
261         s.write(pbegin, pend - pbegin);
262     }
263
264     template<typename Stream>
265     void Unserialize(Stream& s, int, int=0)
266     {
267         s.read(pbegin, pend - pbegin);
268     }
269 };
270
271
272
273 //
274 // string stored as a fixed length field
275 //
276 template<std::size_t LEN>
277 class CFixedFieldString
278 {
279 protected:
280     const string* pcstr;
281     string* pstr;
282 public:
283     explicit CFixedFieldString(const string& str) : pcstr(&str), pstr(NULL) { }
284     explicit CFixedFieldString(string& str) : pcstr(&str), pstr(&str) { }
285
286     unsigned int GetSerializeSize(int, int=0) const
287     {
288         return LEN;
289     }
290
291     template<typename Stream>
292     void Serialize(Stream& s, int, int=0) const
293     {
294         char pszBuf[LEN];
295         strncpy(pszBuf, pcstr->c_str(), LEN);
296         s.write(pszBuf, LEN);
297     }
298
299     template<typename Stream>
300     void Unserialize(Stream& s, int, int=0)
301     {
302         if (pstr == NULL)
303             throw std::ios_base::failure("CFixedFieldString::Unserialize : trying to unserialize to const string");
304         char pszBuf[LEN+1];
305         s.read(pszBuf, LEN);
306         pszBuf[LEN] = '\0';
307         *pstr = pszBuf;
308     }
309 };
310
311
312
313
314
315 //
316 // Forward declarations
317 //
318
319 // string
320 template<typename C> unsigned int GetSerializeSize(const basic_string<C>& str, int, int=0);
321 template<typename Stream, typename C> void Serialize(Stream& os, const basic_string<C>& str, int, int=0);
322 template<typename Stream, typename C> void Unserialize(Stream& is, basic_string<C>& str, int, int=0);
323
324 // vector
325 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
326 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
327 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion=VERSION);
328 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
329 template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
330 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion=VERSION);
331 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
332 template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
333 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion=VERSION);
334
335 // others derived from vector
336 extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion=VERSION);
337 template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion=VERSION);
338 template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion=VERSION);
339
340 // pair
341 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion=VERSION);
342 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion=VERSION);
343 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion=VERSION);
344
345 // 3 tuple
346 template<typename T0, typename T1, typename T2> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
347 template<typename Stream, typename T0, typename T1, typename T2> void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
348 template<typename Stream, typename T0, typename T1, typename T2> void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
349
350 // 4 tuple
351 template<typename T0, typename T1, typename T2, typename T3> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=VERSION);
352 template<typename Stream, typename T0, typename T1, typename T2, typename T3> void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=VERSION);
353 template<typename Stream, typename T0, typename T1, typename T2, typename T3> void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=VERSION);
354
355 // map
356 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion=VERSION);
357 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=VERSION);
358 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=VERSION);
359
360 // set
361 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion=VERSION);
362 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion=VERSION);
363 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion=VERSION);
364
365
366
367
368
369 //
370 // If none of the specialized versions above matched, default to calling member function.
371 // "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
372 // The compiler will only cast int to long if none of the other templates matched.
373 // Thanks to Boost serialization for this idea.
374 //
375 template<typename T>
376 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion=VERSION)
377 {
378     return a.GetSerializeSize((int)nType, nVersion);
379 }
380
381 template<typename Stream, typename T>
382 inline void Serialize(Stream& os, const T& a, long nType, int nVersion=VERSION)
383 {
384     a.Serialize(os, (int)nType, nVersion);
385 }
386
387 template<typename Stream, typename T>
388 inline void Unserialize(Stream& is, T& a, long nType, int nVersion=VERSION)
389 {
390     a.Unserialize(is, (int)nType, nVersion);
391 }
392
393
394
395
396
397 //
398 // string
399 //
400 template<typename C>
401 unsigned int GetSerializeSize(const basic_string<C>& str, int, int)
402 {
403     return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
404 }
405
406 template<typename Stream, typename C>
407 void Serialize(Stream& os, const basic_string<C>& str, int, int)
408 {
409     WriteCompactSize(os, str.size());
410     if (!str.empty())
411         os.write((char*)&str[0], str.size() * sizeof(str[0]));
412 }
413
414 template<typename Stream, typename C>
415 void Unserialize(Stream& is, basic_string<C>& str, int, int)
416 {
417     unsigned int nSize = ReadCompactSize(is);
418     str.resize(nSize);
419     if (nSize != 0)
420         is.read((char*)&str[0], nSize * sizeof(str[0]));
421 }
422
423
424
425 //
426 // vector
427 //
428 template<typename T, typename A>
429 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
430 {
431     return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
432 }
433
434 template<typename T, typename A>
435 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
436 {
437     unsigned int nSize = GetSizeOfCompactSize(v.size());
438     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
439         nSize += GetSerializeSize((*vi), nType, nVersion);
440     return nSize;
441 }
442
443 template<typename T, typename A>
444 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
445 {
446     return GetSerializeSize_impl(v, nType, nVersion, boost::is_fundamental<T>());
447 }
448
449
450 template<typename Stream, typename T, typename A>
451 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
452 {
453     WriteCompactSize(os, v.size());
454     if (!v.empty())
455         os.write((char*)&v[0], v.size() * sizeof(T));
456 }
457
458 template<typename Stream, typename T, typename A>
459 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
460 {
461     WriteCompactSize(os, v.size());
462     for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
463         ::Serialize(os, (*vi), nType, nVersion);
464 }
465
466 template<typename Stream, typename T, typename A>
467 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
468 {
469     Serialize_impl(os, v, nType, nVersion, boost::is_fundamental<T>());
470 }
471
472
473 template<typename Stream, typename T, typename A>
474 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
475 {
476     //unsigned int nSize = ReadCompactSize(is);
477     //v.resize(nSize);
478     //is.read((char*)&v[0], nSize * sizeof(T));
479
480     // Limit size per read so bogus size value won't cause out of memory
481     v.clear();
482     unsigned int nSize = ReadCompactSize(is);
483     unsigned int i = 0;
484     while (i < nSize)
485     {
486         unsigned int blk = min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
487         v.resize(i + blk);
488         is.read((char*)&v[i], blk * sizeof(T));
489         i += blk;
490     }
491 }
492
493 template<typename Stream, typename T, typename A>
494 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
495 {
496     //unsigned int nSize = ReadCompactSize(is);
497     //v.resize(nSize);
498     //for (std::vector<T, A>::iterator vi = v.begin(); vi != v.end(); ++vi)
499     //    Unserialize(is, (*vi), nType, nVersion);
500
501     v.clear();
502     unsigned int nSize = ReadCompactSize(is);
503     unsigned int i = 0;
504     unsigned int nMid = 0;
505     while (nMid < nSize)
506     {
507         nMid += 5000000 / sizeof(T);
508         if (nMid > nSize)
509             nMid = nSize;
510         v.resize(nMid);
511         for (; i < nMid; i++)
512             Unserialize(is, v[i], nType, nVersion);
513     }
514 }
515
516 template<typename Stream, typename T, typename A>
517 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
518 {
519     Unserialize_impl(is, v, nType, nVersion, boost::is_fundamental<T>());
520 }
521
522
523
524 //
525 // others derived from vector
526 //
527 inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
528 {
529     return GetSerializeSize((const vector<unsigned char>&)v, nType, nVersion);
530 }
531
532 template<typename Stream>
533 void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
534 {
535     Serialize(os, (const vector<unsigned char>&)v, nType, nVersion);
536 }
537
538 template<typename Stream>
539 void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
540 {
541     Unserialize(is, (vector<unsigned char>&)v, nType, nVersion);
542 }
543
544
545
546 //
547 // pair
548 //
549 template<typename K, typename T>
550 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
551 {
552     return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
553 }
554
555 template<typename Stream, typename K, typename T>
556 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
557 {
558     Serialize(os, item.first, nType, nVersion);
559     Serialize(os, item.second, nType, nVersion);
560 }
561
562 template<typename Stream, typename K, typename T>
563 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
564 {
565     Unserialize(is, item.first, nType, nVersion);
566     Unserialize(is, item.second, nType, nVersion);
567 }
568
569
570
571 //
572 // 3 tuple
573 //
574 template<typename T0, typename T1, typename T2>
575 unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
576 {
577     unsigned int nSize = 0;
578     nSize += GetSerializeSize(get<0>(item), nType, nVersion);
579     nSize += GetSerializeSize(get<1>(item), nType, nVersion);
580     nSize += GetSerializeSize(get<2>(item), nType, nVersion);
581     return nSize;
582 }
583
584 template<typename Stream, typename T0, typename T1, typename T2>
585 void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
586 {
587     Serialize(os, get<0>(item), nType, nVersion);
588     Serialize(os, get<1>(item), nType, nVersion);
589     Serialize(os, get<2>(item), nType, nVersion);
590 }
591
592 template<typename Stream, typename T0, typename T1, typename T2>
593 void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
594 {
595     Unserialize(is, get<0>(item), nType, nVersion);
596     Unserialize(is, get<1>(item), nType, nVersion);
597     Unserialize(is, get<2>(item), nType, nVersion);
598 }
599
600
601
602 //
603 // 4 tuple
604 //
605 template<typename T0, typename T1, typename T2, typename T3>
606 unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
607 {
608     unsigned int nSize = 0;
609     nSize += GetSerializeSize(get<0>(item), nType, nVersion);
610     nSize += GetSerializeSize(get<1>(item), nType, nVersion);
611     nSize += GetSerializeSize(get<2>(item), nType, nVersion);
612     nSize += GetSerializeSize(get<3>(item), nType, nVersion);
613     return nSize;
614 }
615
616 template<typename Stream, typename T0, typename T1, typename T2, typename T3>
617 void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
618 {
619     Serialize(os, get<0>(item), nType, nVersion);
620     Serialize(os, get<1>(item), nType, nVersion);
621     Serialize(os, get<2>(item), nType, nVersion);
622     Serialize(os, get<3>(item), nType, nVersion);
623 }
624
625 template<typename Stream, typename T0, typename T1, typename T2, typename T3>
626 void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
627 {
628     Unserialize(is, get<0>(item), nType, nVersion);
629     Unserialize(is, get<1>(item), nType, nVersion);
630     Unserialize(is, get<2>(item), nType, nVersion);
631     Unserialize(is, get<3>(item), nType, nVersion);
632 }
633
634
635
636 //
637 // map
638 //
639 template<typename K, typename T, typename Pred, typename A>
640 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
641 {
642     unsigned int nSize = GetSizeOfCompactSize(m.size());
643     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
644         nSize += GetSerializeSize((*mi), nType, nVersion);
645     return nSize;
646 }
647
648 template<typename Stream, typename K, typename T, typename Pred, typename A>
649 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
650 {
651     WriteCompactSize(os, m.size());
652     for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
653         Serialize(os, (*mi), nType, nVersion);
654 }
655
656 template<typename Stream, typename K, typename T, typename Pred, typename A>
657 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
658 {
659     m.clear();
660     unsigned int nSize = ReadCompactSize(is);
661     typename std::map<K, T, Pred, A>::iterator mi = m.begin();
662     for (unsigned int i = 0; i < nSize; i++)
663     {
664         pair<K, T> item;
665         Unserialize(is, item, nType, nVersion);
666         mi = m.insert(mi, item);
667     }
668 }
669
670
671
672 //
673 // set
674 //
675 template<typename K, typename Pred, typename A>
676 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
677 {
678     unsigned int nSize = GetSizeOfCompactSize(m.size());
679     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
680         nSize += GetSerializeSize((*it), nType, nVersion);
681     return nSize;
682 }
683
684 template<typename Stream, typename K, typename Pred, typename A>
685 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
686 {
687     WriteCompactSize(os, m.size());
688     for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
689         Serialize(os, (*it), nType, nVersion);
690 }
691
692 template<typename Stream, typename K, typename Pred, typename A>
693 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
694 {
695     m.clear();
696     unsigned int nSize = ReadCompactSize(is);
697     typename std::set<K, Pred, A>::iterator it = m.begin();
698     for (unsigned int i = 0; i < nSize; i++)
699     {
700         K key;
701         Unserialize(is, key, nType, nVersion);
702         it = m.insert(it, key);
703     }
704 }
705
706
707
708 //
709 // Support for IMPLEMENT_SERIALIZE and READWRITE macro
710 //
711 class CSerActionGetSerializeSize { };
712 class CSerActionSerialize { };
713 class CSerActionUnserialize { };
714
715 template<typename Stream, typename T>
716 inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
717 {
718     return ::GetSerializeSize(obj, nType, nVersion);
719 }
720
721 template<typename Stream, typename T>
722 inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
723 {
724     ::Serialize(s, obj, nType, nVersion);
725     return 0;
726 }
727
728 template<typename Stream, typename T>
729 inline unsigned int SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
730 {
731     ::Unserialize(s, obj, nType, nVersion);
732     return 0;
733 }
734
735 struct ser_streamplaceholder
736 {
737     int nType;
738     int nVersion;
739 };
740
741
742
743
744
745
746
747
748
749 //
750 // Allocator that clears its contents before deletion
751 //
752 template<typename T>
753 struct secure_allocator : public std::allocator<T>
754 {
755     // MSVC8 default copy constructor is broken
756     typedef std::allocator<T> base;
757     typedef typename base::size_type size_type;
758     typedef typename base::difference_type  difference_type;
759     typedef typename base::pointer pointer;
760     typedef typename base::const_pointer const_pointer;
761     typedef typename base::reference reference;
762     typedef typename base::const_reference const_reference;
763     typedef typename base::value_type value_type;
764     secure_allocator() throw() {}
765     secure_allocator(const secure_allocator& a) throw() : base(a) {}
766     ~secure_allocator() throw() {}
767     template<typename _Other> struct rebind
768     { typedef secure_allocator<_Other> other; };
769
770     void deallocate(T* p, std::size_t n)
771     {
772         if (p != NULL)
773             memset(p, 0, sizeof(T) * n);
774         allocator<T>::deallocate(p, n);
775     }
776 };
777
778
779
780 //
781 // Double ended buffer combining vector and stream-like interfaces.
782 // >> and << read and write unformatted data using the above serialization templates.
783 // Fills with data in linear time; some stringstream implementations take N^2 time.
784 //
785 class CDataStream
786 {
787 protected:
788     typedef vector<char, secure_allocator<char> > vector_type;
789     vector_type vch;
790     unsigned int nReadPos;
791     short state;
792     short exceptmask;
793 public:
794     int nType;
795     int nVersion;
796
797     typedef vector_type::allocator_type   allocator_type;
798     typedef vector_type::size_type        size_type;
799     typedef vector_type::difference_type  difference_type;
800     typedef vector_type::reference        reference;
801     typedef vector_type::const_reference  const_reference;
802     typedef vector_type::value_type       value_type;
803     typedef vector_type::iterator         iterator;
804     typedef vector_type::const_iterator   const_iterator;
805     typedef vector_type::reverse_iterator reverse_iterator;
806
807     explicit CDataStream(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
808     {
809         Init(nTypeIn, nVersionIn);
810     }
811
812     CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
813     {
814         Init(nTypeIn, nVersionIn);
815     }
816
817 #if !defined(_MSC_VER) || _MSC_VER >= 1300
818     CDataStream(const char* pbegin, const char* pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
819     {
820         Init(nTypeIn, nVersionIn);
821     }
822 #endif
823
824     CDataStream(const vector_type& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
825     {
826         Init(nTypeIn, nVersionIn);
827     }
828
829     CDataStream(const vector<char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
830     {
831         Init(nTypeIn, nVersionIn);
832     }
833
834     CDataStream(const vector<unsigned char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
835     {
836         Init(nTypeIn, nVersionIn);
837     }
838
839     void Init(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
840     {
841         nReadPos = 0;
842         nType = nTypeIn;
843         nVersion = nVersionIn;
844         state = 0;
845         exceptmask = ios::badbit | ios::failbit;
846     }
847
848     CDataStream& operator+=(const CDataStream& b)
849     {
850         vch.insert(vch.end(), b.begin(), b.end());
851         return *this;
852     }
853
854     friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
855     {
856         CDataStream ret = a;
857         ret += b;
858         return (ret);
859     }
860
861     string str() const
862     {
863         return (string(begin(), end()));
864     }
865
866
867     //
868     // Vector subset
869     //
870     const_iterator begin() const                     { return vch.begin() + nReadPos; }
871     iterator begin()                                 { return vch.begin() + nReadPos; }
872     const_iterator end() const                       { return vch.end(); }
873     iterator end()                                   { return vch.end(); }
874     size_type size() const                           { return vch.size() - nReadPos; }
875     bool empty() const                               { return vch.size() == nReadPos; }
876     void resize(size_type n, value_type c=0)         { vch.resize(n + nReadPos, c); }
877     void reserve(size_type n)                        { vch.reserve(n + nReadPos); }
878     const_reference operator[](size_type pos) const  { return vch[pos + nReadPos]; }
879     reference operator[](size_type pos)              { return vch[pos + nReadPos]; }
880     void clear()                                     { vch.clear(); nReadPos = 0; }
881     iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
882     void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
883
884     void insert(iterator it, const_iterator first, const_iterator last)
885     {
886         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
887         {
888             // special case for inserting at the front when there's room
889             nReadPos -= (last - first);
890             memcpy(&vch[nReadPos], &first[0], last - first);
891         }
892         else
893             vch.insert(it, first, last);
894     }
895
896     void insert(iterator it, vector<char>::const_iterator first, vector<char>::const_iterator last)
897     {
898         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
899         {
900             // special case for inserting at the front when there's room
901             nReadPos -= (last - first);
902             memcpy(&vch[nReadPos], &first[0], last - first);
903         }
904         else
905             vch.insert(it, first, last);
906     }
907
908 #if !defined(_MSC_VER) || _MSC_VER >= 1300
909     void insert(iterator it, const char* first, const char* last)
910     {
911         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
912         {
913             // special case for inserting at the front when there's room
914             nReadPos -= (last - first);
915             memcpy(&vch[nReadPos], &first[0], last - first);
916         }
917         else
918             vch.insert(it, first, last);
919     }
920 #endif
921
922     iterator erase(iterator it)
923     {
924         if (it == vch.begin() + nReadPos)
925         {
926             // special case for erasing from the front
927             if (++nReadPos >= vch.size())
928             {
929                 // whenever we reach the end, we take the opportunity to clear the buffer
930                 nReadPos = 0;
931                 return vch.erase(vch.begin(), vch.end());
932             }
933             return vch.begin() + nReadPos;
934         }
935         else
936             return vch.erase(it);
937     }
938
939     iterator erase(iterator first, iterator last)
940     {
941         if (first == vch.begin() + nReadPos)
942         {
943             // special case for erasing from the front
944             if (last == vch.end())
945             {
946                 nReadPos = 0;
947                 return vch.erase(vch.begin(), vch.end());
948             }
949             else
950             {
951                 nReadPos = (last - vch.begin());
952                 return last;
953             }
954         }
955         else
956             return vch.erase(first, last);
957     }
958
959     inline void Compact()
960     {
961         vch.erase(vch.begin(), vch.begin() + nReadPos);
962         nReadPos = 0;
963     }
964
965     bool Rewind(size_type n)
966     {
967         // Rewind by n characters if the buffer hasn't been compacted yet
968         if (n > nReadPos)
969             return false;
970         nReadPos -= n;
971         return true;
972     }
973
974
975     //
976     // Stream subset
977     //
978     void setstate(short bits, const char* psz)
979     {
980         state |= bits;
981         if (state & exceptmask)
982             throw std::ios_base::failure(psz);
983     }
984
985     bool eof() const             { return size() == 0; }
986     bool fail() const            { return state & (ios::badbit | ios::failbit); }
987     bool good() const            { return !eof() && (state == 0); }
988     void clear(short n)          { state = n; }  // name conflict with vector clear()
989     short exceptions()           { return exceptmask; }
990     short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
991     CDataStream* rdbuf()         { return this; }
992     int in_avail()               { return size(); }
993
994     void SetType(int n)          { nType = n; }
995     int GetType()                { return nType; }
996     void SetVersion(int n)       { nVersion = n; }
997     int GetVersion()             { return nVersion; }
998     void ReadVersion()           { *this >> nVersion; }
999     void WriteVersion()          { *this << nVersion; }
1000
1001     CDataStream& read(char* pch, int nSize)
1002     {
1003         // Read from the beginning of the buffer
1004         assert(nSize >= 0);
1005         unsigned int nReadPosNext = nReadPos + nSize;
1006         if (nReadPosNext >= vch.size())
1007         {
1008             if (nReadPosNext > vch.size())
1009             {
1010                 setstate(ios::failbit, "CDataStream::read() : end of data");
1011                 memset(pch, 0, nSize);
1012                 nSize = vch.size() - nReadPos;
1013             }
1014             memcpy(pch, &vch[nReadPos], nSize);
1015             nReadPos = 0;
1016             vch.clear();
1017             return (*this);
1018         }
1019         memcpy(pch, &vch[nReadPos], nSize);
1020         nReadPos = nReadPosNext;
1021         return (*this);
1022     }
1023
1024     CDataStream& ignore(int nSize)
1025     {
1026         // Ignore from the beginning of the buffer
1027         assert(nSize >= 0);
1028         unsigned int nReadPosNext = nReadPos + nSize;
1029         if (nReadPosNext >= vch.size())
1030         {
1031             if (nReadPosNext > vch.size())
1032             {
1033                 setstate(ios::failbit, "CDataStream::ignore() : end of data");
1034                 nSize = vch.size() - nReadPos;
1035             }
1036             nReadPos = 0;
1037             vch.clear();
1038             return (*this);
1039         }
1040         nReadPos = nReadPosNext;
1041         return (*this);
1042     }
1043
1044     CDataStream& write(const char* pch, int nSize)
1045     {
1046         // Write to the end of the buffer
1047         assert(nSize >= 0);
1048         vch.insert(vch.end(), pch, pch + nSize);
1049         return (*this);
1050     }
1051
1052     template<typename Stream>
1053     void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
1054     {
1055         // Special case: stream << stream concatenates like stream += stream
1056         if (!vch.empty())
1057             s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
1058     }
1059
1060     template<typename T>
1061     unsigned int GetSerializeSize(const T& obj)
1062     {
1063         // Tells the size of the object if serialized to this stream
1064         return ::GetSerializeSize(obj, nType, nVersion);
1065     }
1066
1067     template<typename T>
1068     CDataStream& operator<<(const T& obj)
1069     {
1070         // Serialize to this stream
1071         ::Serialize(*this, obj, nType, nVersion);
1072         return (*this);
1073     }
1074
1075     template<typename T>
1076     CDataStream& operator>>(T& obj)
1077     {
1078         // Unserialize from this stream
1079         ::Unserialize(*this, obj, nType, nVersion);
1080         return (*this);
1081     }
1082 };
1083
1084 #ifdef TESTCDATASTREAM
1085 // VC6sp6
1086 // CDataStream:
1087 // n=1000       0 seconds
1088 // n=2000       0 seconds
1089 // n=4000       0 seconds
1090 // n=8000       0 seconds
1091 // n=16000      0 seconds
1092 // n=32000      0 seconds
1093 // n=64000      1 seconds
1094 // n=128000     1 seconds
1095 // n=256000     2 seconds
1096 // n=512000     4 seconds
1097 // n=1024000    8 seconds
1098 // n=2048000    16 seconds
1099 // n=4096000    32 seconds
1100 // stringstream:
1101 // n=1000       1 seconds
1102 // n=2000       1 seconds
1103 // n=4000       13 seconds
1104 // n=8000       87 seconds
1105 // n=16000      400 seconds
1106 // n=32000      1660 seconds
1107 // n=64000      6749 seconds
1108 // n=128000     27241 seconds
1109 // n=256000     109804 seconds
1110 #include <iostream>
1111 int main(int argc, char *argv[])
1112 {
1113     vector<unsigned char> vch(0xcc, 250);
1114     printf("CDataStream:\n");
1115     for (int n = 1000; n <= 4500000; n *= 2)
1116     {
1117         CDataStream ss;
1118         time_t nStart = time(NULL);
1119         for (int i = 0; i < n; i++)
1120             ss.write((char*)&vch[0], vch.size());
1121         printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
1122     }
1123     printf("stringstream:\n");
1124     for (int n = 1000; n <= 4500000; n *= 2)
1125     {
1126         stringstream ss;
1127         time_t nStart = time(NULL);
1128         for (int i = 0; i < n; i++)
1129             ss.write((char*)&vch[0], vch.size());
1130         printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
1131     }
1132 }
1133 #endif
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144 //
1145 // Automatic closing wrapper for FILE*
1146 //  - Will automatically close the file when it goes out of scope if not null.
1147 //  - If you're returning the file pointer, return file.release().
1148 //  - If you need to close the file early, use file.fclose() instead of fclose(file).
1149 //
1150 class CAutoFile
1151 {
1152 protected:
1153     FILE* file;
1154     short state;
1155     short exceptmask;
1156 public:
1157     int nType;
1158     int nVersion;
1159
1160     typedef FILE element_type;
1161
1162     CAutoFile(FILE* filenew=NULL, int nTypeIn=SER_DISK, int nVersionIn=VERSION)
1163     {
1164         file = filenew;
1165         nType = nTypeIn;
1166         nVersion = nVersionIn;
1167         state = 0;
1168         exceptmask = ios::badbit | ios::failbit;
1169     }
1170
1171     ~CAutoFile()
1172     {
1173         fclose();
1174     }
1175
1176     void fclose()
1177     {
1178         if (file != NULL && file != stdin && file != stdout && file != stderr)
1179             ::fclose(file);
1180         file = NULL;
1181     }
1182
1183     FILE* release()             { FILE* ret = file; file = NULL; return ret; }
1184     operator FILE*()            { return file; }
1185     FILE* operator->()          { return file; }
1186     FILE& operator*()           { return *file; }
1187     FILE** operator&()          { return &file; }
1188     FILE* operator=(FILE* pnew) { return file = pnew; }
1189     bool operator!()            { return (file == NULL); }
1190
1191
1192     //
1193     // Stream subset
1194     //
1195     void setstate(short bits, const char* psz)
1196     {
1197         state |= bits;
1198         if (state & exceptmask)
1199             throw std::ios_base::failure(psz);
1200     }
1201
1202     bool fail() const            { return state & (ios::badbit | ios::failbit); }
1203     bool good() const            { return state == 0; }
1204     void clear(short n = 0)      { state = n; }
1205     short exceptions()           { return exceptmask; }
1206     short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
1207
1208     void SetType(int n)          { nType = n; }
1209     int GetType()                { return nType; }
1210     void SetVersion(int n)       { nVersion = n; }
1211     int GetVersion()             { return nVersion; }
1212     void ReadVersion()           { *this >> nVersion; }
1213     void WriteVersion()          { *this << nVersion; }
1214
1215     CAutoFile& read(char* pch, int nSize)
1216     {
1217         if (!file)
1218             throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
1219         if (fread(pch, 1, nSize, file) != nSize)
1220             setstate(ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
1221         return (*this);
1222     }
1223
1224     CAutoFile& write(const char* pch, int nSize)
1225     {
1226         if (!file)
1227             throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
1228         if (fwrite(pch, 1, nSize, file) != nSize)
1229             setstate(ios::failbit, "CAutoFile::write : write failed");
1230         return (*this);
1231     }
1232
1233     template<typename T>
1234     unsigned int GetSerializeSize(const T& obj)
1235     {
1236         // Tells the size of the object if serialized to this stream
1237         return ::GetSerializeSize(obj, nType, nVersion);
1238     }
1239
1240     template<typename T>
1241     CAutoFile& operator<<(const T& obj)
1242     {
1243         // Serialize to this stream
1244         if (!file)
1245             throw std::ios_base::failure("CAutoFile::operator<< : file handle is NULL");
1246         ::Serialize(*this, obj, nType, nVersion);
1247         return (*this);
1248     }
1249
1250     template<typename T>
1251     CAutoFile& operator>>(T& obj)
1252     {
1253         // Unserialize from this stream
1254         if (!file)
1255             throw std::ios_base::failure("CAutoFile::operator>> : file handle is NULL");
1256         ::Unserialize(*this, obj, nType, nVersion);
1257         return (*this);
1258     }
1259 };