Better wording for transaction fee notification messages
[novacoin.git] / src / 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 = 32100;
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     template <typename U>
767     secure_allocator(const secure_allocator<U>& a) throw() : base(a) {}
768     ~secure_allocator() throw() {}
769     template<typename _Other> struct rebind
770     { typedef secure_allocator<_Other> other; };
771
772     void deallocate(T* p, std::size_t n)
773     {
774         if (p != NULL)
775             memset(p, 0, sizeof(T) * n);
776         allocator<T>::deallocate(p, n);
777     }
778 };
779
780
781
782 //
783 // Double ended buffer combining vector and stream-like interfaces.
784 // >> and << read and write unformatted data using the above serialization templates.
785 // Fills with data in linear time; some stringstream implementations take N^2 time.
786 //
787 class CDataStream
788 {
789 protected:
790     typedef vector<char, secure_allocator<char> > vector_type;
791     vector_type vch;
792     unsigned int nReadPos;
793     short state;
794     short exceptmask;
795 public:
796     int nType;
797     int nVersion;
798
799     typedef vector_type::allocator_type   allocator_type;
800     typedef vector_type::size_type        size_type;
801     typedef vector_type::difference_type  difference_type;
802     typedef vector_type::reference        reference;
803     typedef vector_type::const_reference  const_reference;
804     typedef vector_type::value_type       value_type;
805     typedef vector_type::iterator         iterator;
806     typedef vector_type::const_iterator   const_iterator;
807     typedef vector_type::reverse_iterator reverse_iterator;
808
809     explicit CDataStream(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
810     {
811         Init(nTypeIn, nVersionIn);
812     }
813
814     CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
815     {
816         Init(nTypeIn, nVersionIn);
817     }
818
819 #if !defined(_MSC_VER) || _MSC_VER >= 1300
820     CDataStream(const char* pbegin, const char* pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
821     {
822         Init(nTypeIn, nVersionIn);
823     }
824 #endif
825
826     CDataStream(const vector_type& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
827     {
828         Init(nTypeIn, nVersionIn);
829     }
830
831     CDataStream(const vector<char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
832     {
833         Init(nTypeIn, nVersionIn);
834     }
835
836     CDataStream(const vector<unsigned char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
837     {
838         Init(nTypeIn, nVersionIn);
839     }
840
841     void Init(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
842     {
843         nReadPos = 0;
844         nType = nTypeIn;
845         nVersion = nVersionIn;
846         state = 0;
847         exceptmask = ios::badbit | ios::failbit;
848     }
849
850     CDataStream& operator+=(const CDataStream& b)
851     {
852         vch.insert(vch.end(), b.begin(), b.end());
853         return *this;
854     }
855
856     friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
857     {
858         CDataStream ret = a;
859         ret += b;
860         return (ret);
861     }
862
863     string str() const
864     {
865         return (string(begin(), end()));
866     }
867
868
869     //
870     // Vector subset
871     //
872     const_iterator begin() const                     { return vch.begin() + nReadPos; }
873     iterator begin()                                 { return vch.begin() + nReadPos; }
874     const_iterator end() const                       { return vch.end(); }
875     iterator end()                                   { return vch.end(); }
876     size_type size() const                           { return vch.size() - nReadPos; }
877     bool empty() const                               { return vch.size() == nReadPos; }
878     void resize(size_type n, value_type c=0)         { vch.resize(n + nReadPos, c); }
879     void reserve(size_type n)                        { vch.reserve(n + nReadPos); }
880     const_reference operator[](size_type pos) const  { return vch[pos + nReadPos]; }
881     reference operator[](size_type pos)              { return vch[pos + nReadPos]; }
882     void clear()                                     { vch.clear(); nReadPos = 0; }
883     iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
884     void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
885
886     void insert(iterator it, const_iterator first, const_iterator last)
887     {
888         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
889         {
890             // special case for inserting at the front when there's room
891             nReadPos -= (last - first);
892             memcpy(&vch[nReadPos], &first[0], last - first);
893         }
894         else
895             vch.insert(it, first, last);
896     }
897
898     void insert(iterator it, vector<char>::const_iterator first, vector<char>::const_iterator last)
899     {
900         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
901         {
902             // special case for inserting at the front when there's room
903             nReadPos -= (last - first);
904             memcpy(&vch[nReadPos], &first[0], last - first);
905         }
906         else
907             vch.insert(it, first, last);
908     }
909
910 #if !defined(_MSC_VER) || _MSC_VER >= 1300
911     void insert(iterator it, const char* first, const char* last)
912     {
913         if (it == vch.begin() + nReadPos && last - first <= nReadPos)
914         {
915             // special case for inserting at the front when there's room
916             nReadPos -= (last - first);
917             memcpy(&vch[nReadPos], &first[0], last - first);
918         }
919         else
920             vch.insert(it, first, last);
921     }
922 #endif
923
924     iterator erase(iterator it)
925     {
926         if (it == vch.begin() + nReadPos)
927         {
928             // special case for erasing from the front
929             if (++nReadPos >= vch.size())
930             {
931                 // whenever we reach the end, we take the opportunity to clear the buffer
932                 nReadPos = 0;
933                 return vch.erase(vch.begin(), vch.end());
934             }
935             return vch.begin() + nReadPos;
936         }
937         else
938             return vch.erase(it);
939     }
940
941     iterator erase(iterator first, iterator last)
942     {
943         if (first == vch.begin() + nReadPos)
944         {
945             // special case for erasing from the front
946             if (last == vch.end())
947             {
948                 nReadPos = 0;
949                 return vch.erase(vch.begin(), vch.end());
950             }
951             else
952             {
953                 nReadPos = (last - vch.begin());
954                 return last;
955             }
956         }
957         else
958             return vch.erase(first, last);
959     }
960
961     inline void Compact()
962     {
963         vch.erase(vch.begin(), vch.begin() + nReadPos);
964         nReadPos = 0;
965     }
966
967     bool Rewind(size_type n)
968     {
969         // Rewind by n characters if the buffer hasn't been compacted yet
970         if (n > nReadPos)
971             return false;
972         nReadPos -= n;
973         return true;
974     }
975
976
977     //
978     // Stream subset
979     //
980     void setstate(short bits, const char* psz)
981     {
982         state |= bits;
983         if (state & exceptmask)
984             throw std::ios_base::failure(psz);
985     }
986
987     bool eof() const             { return size() == 0; }
988     bool fail() const            { return state & (ios::badbit | ios::failbit); }
989     bool good() const            { return !eof() && (state == 0); }
990     void clear(short n)          { state = n; }  // name conflict with vector clear()
991     short exceptions()           { return exceptmask; }
992     short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
993     CDataStream* rdbuf()         { return this; }
994     int in_avail()               { return size(); }
995
996     void SetType(int n)          { nType = n; }
997     int GetType()                { return nType; }
998     void SetVersion(int n)       { nVersion = n; }
999     int GetVersion()             { return nVersion; }
1000     void ReadVersion()           { *this >> nVersion; }
1001     void WriteVersion()          { *this << nVersion; }
1002
1003     CDataStream& read(char* pch, int nSize)
1004     {
1005         // Read from the beginning of the buffer
1006         assert(nSize >= 0);
1007         unsigned int nReadPosNext = nReadPos + nSize;
1008         if (nReadPosNext >= vch.size())
1009         {
1010             if (nReadPosNext > vch.size())
1011             {
1012                 setstate(ios::failbit, "CDataStream::read() : end of data");
1013                 memset(pch, 0, nSize);
1014                 nSize = vch.size() - nReadPos;
1015             }
1016             memcpy(pch, &vch[nReadPos], nSize);
1017             nReadPos = 0;
1018             vch.clear();
1019             return (*this);
1020         }
1021         memcpy(pch, &vch[nReadPos], nSize);
1022         nReadPos = nReadPosNext;
1023         return (*this);
1024     }
1025
1026     CDataStream& ignore(int nSize)
1027     {
1028         // Ignore from the beginning of the buffer
1029         assert(nSize >= 0);
1030         unsigned int nReadPosNext = nReadPos + nSize;
1031         if (nReadPosNext >= vch.size())
1032         {
1033             if (nReadPosNext > vch.size())
1034             {
1035                 setstate(ios::failbit, "CDataStream::ignore() : end of data");
1036                 nSize = vch.size() - nReadPos;
1037             }
1038             nReadPos = 0;
1039             vch.clear();
1040             return (*this);
1041         }
1042         nReadPos = nReadPosNext;
1043         return (*this);
1044     }
1045
1046     CDataStream& write(const char* pch, int nSize)
1047     {
1048         // Write to the end of the buffer
1049         assert(nSize >= 0);
1050         vch.insert(vch.end(), pch, pch + nSize);
1051         return (*this);
1052     }
1053
1054     template<typename Stream>
1055     void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
1056     {
1057         // Special case: stream << stream concatenates like stream += stream
1058         if (!vch.empty())
1059             s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
1060     }
1061
1062     template<typename T>
1063     unsigned int GetSerializeSize(const T& obj)
1064     {
1065         // Tells the size of the object if serialized to this stream
1066         return ::GetSerializeSize(obj, nType, nVersion);
1067     }
1068
1069     template<typename T>
1070     CDataStream& operator<<(const T& obj)
1071     {
1072         // Serialize to this stream
1073         ::Serialize(*this, obj, nType, nVersion);
1074         return (*this);
1075     }
1076
1077     template<typename T>
1078     CDataStream& operator>>(T& obj)
1079     {
1080         // Unserialize from this stream
1081         ::Unserialize(*this, obj, nType, nVersion);
1082         return (*this);
1083     }
1084 };
1085
1086 #ifdef TESTCDATASTREAM
1087 // VC6sp6
1088 // CDataStream:
1089 // n=1000       0 seconds
1090 // n=2000       0 seconds
1091 // n=4000       0 seconds
1092 // n=8000       0 seconds
1093 // n=16000      0 seconds
1094 // n=32000      0 seconds
1095 // n=64000      1 seconds
1096 // n=128000     1 seconds
1097 // n=256000     2 seconds
1098 // n=512000     4 seconds
1099 // n=1024000    8 seconds
1100 // n=2048000    16 seconds
1101 // n=4096000    32 seconds
1102 // stringstream:
1103 // n=1000       1 seconds
1104 // n=2000       1 seconds
1105 // n=4000       13 seconds
1106 // n=8000       87 seconds
1107 // n=16000      400 seconds
1108 // n=32000      1660 seconds
1109 // n=64000      6749 seconds
1110 // n=128000     27241 seconds
1111 // n=256000     109804 seconds
1112 #include <iostream>
1113 int main(int argc, char *argv[])
1114 {
1115     vector<unsigned char> vch(0xcc, 250);
1116     printf("CDataStream:\n");
1117     for (int n = 1000; n <= 4500000; n *= 2)
1118     {
1119         CDataStream ss;
1120         time_t nStart = time(NULL);
1121         for (int i = 0; i < n; i++)
1122             ss.write((char*)&vch[0], vch.size());
1123         printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
1124     }
1125     printf("stringstream:\n");
1126     for (int n = 1000; n <= 4500000; n *= 2)
1127     {
1128         stringstream ss;
1129         time_t nStart = time(NULL);
1130         for (int i = 0; i < n; i++)
1131             ss.write((char*)&vch[0], vch.size());
1132         printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
1133     }
1134 }
1135 #endif
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146 //
1147 // Automatic closing wrapper for FILE*
1148 //  - Will automatically close the file when it goes out of scope if not null.
1149 //  - If you're returning the file pointer, return file.release().
1150 //  - If you need to close the file early, use file.fclose() instead of fclose(file).
1151 //
1152 class CAutoFile
1153 {
1154 protected:
1155     FILE* file;
1156     short state;
1157     short exceptmask;
1158 public:
1159     int nType;
1160     int nVersion;
1161
1162     typedef FILE element_type;
1163
1164     CAutoFile(FILE* filenew=NULL, int nTypeIn=SER_DISK, int nVersionIn=VERSION)
1165     {
1166         file = filenew;
1167         nType = nTypeIn;
1168         nVersion = nVersionIn;
1169         state = 0;
1170         exceptmask = ios::badbit | ios::failbit;
1171     }
1172
1173     ~CAutoFile()
1174     {
1175         fclose();
1176     }
1177
1178     void fclose()
1179     {
1180         if (file != NULL && file != stdin && file != stdout && file != stderr)
1181             ::fclose(file);
1182         file = NULL;
1183     }
1184
1185     FILE* release()             { FILE* ret = file; file = NULL; return ret; }
1186     operator FILE*()            { return file; }
1187     FILE* operator->()          { return file; }
1188     FILE& operator*()           { return *file; }
1189     FILE** operator&()          { return &file; }
1190     FILE* operator=(FILE* pnew) { return file = pnew; }
1191     bool operator!()            { return (file == NULL); }
1192
1193
1194     //
1195     // Stream subset
1196     //
1197     void setstate(short bits, const char* psz)
1198     {
1199         state |= bits;
1200         if (state & exceptmask)
1201             throw std::ios_base::failure(psz);
1202     }
1203
1204     bool fail() const            { return state & (ios::badbit | ios::failbit); }
1205     bool good() const            { return state == 0; }
1206     void clear(short n = 0)      { state = n; }
1207     short exceptions()           { return exceptmask; }
1208     short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
1209
1210     void SetType(int n)          { nType = n; }
1211     int GetType()                { return nType; }
1212     void SetVersion(int n)       { nVersion = n; }
1213     int GetVersion()             { return nVersion; }
1214     void ReadVersion()           { *this >> nVersion; }
1215     void WriteVersion()          { *this << nVersion; }
1216
1217     CAutoFile& read(char* pch, int nSize)
1218     {
1219         if (!file)
1220             throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
1221         if (fread(pch, 1, nSize, file) != nSize)
1222             setstate(ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
1223         return (*this);
1224     }
1225
1226     CAutoFile& write(const char* pch, int nSize)
1227     {
1228         if (!file)
1229             throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
1230         if (fwrite(pch, 1, nSize, file) != nSize)
1231             setstate(ios::failbit, "CAutoFile::write : write failed");
1232         return (*this);
1233     }
1234
1235     template<typename T>
1236     unsigned int GetSerializeSize(const T& obj)
1237     {
1238         // Tells the size of the object if serialized to this stream
1239         return ::GetSerializeSize(obj, nType, nVersion);
1240     }
1241
1242     template<typename T>
1243     CAutoFile& operator<<(const T& obj)
1244     {
1245         // Serialize to this stream
1246         if (!file)
1247             throw std::ios_base::failure("CAutoFile::operator<< : file handle is NULL");
1248         ::Serialize(*this, obj, nType, nVersion);
1249         return (*this);
1250     }
1251
1252     template<typename T>
1253     CAutoFile& operator>>(T& obj)
1254     {
1255         // Unserialize from this stream
1256         if (!file)
1257             throw std::ios_base::failure("CAutoFile::operator>> : file handle is NULL");
1258         ::Unserialize(*this, obj, nType, nVersion);
1259         return (*this);
1260     }
1261 };