Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
serialize.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_SERIALIZE_H
6 #define BITCOIN_SERIALIZE_H
7 
8 #include <string>
9 #include <vector>
10 #include <map>
11 #include <set>
12 #include <cassert>
13 #include <limits>
14 #include <cstring>
15 #include <cstdio>
16 
17 #include <boost/type_traits/is_fundamental.hpp>
18 #include <boost/tuple/tuple.hpp>
19 #include <boost/tuple/tuple_comparison.hpp>
20 #include <boost/tuple/tuple_io.hpp>
21 
22 #include "allocators.h"
23 #include "version.h"
24 
25 typedef long long int64;
26 typedef unsigned long long uint64;
27 
28 class CScript;
29 class CDataStream;
30 class CAutoFile;
31 static const unsigned int MAX_SIZE = 0x02000000;
32 
33 // Used to bypass the rule against non-const reference to temporary
34 // where it makes sense with wrappers such as CFlatData or CTxDB
35 template<typename T>
36 inline T& REF(const T& val)
37 {
38  return const_cast<T&>(val);
39 }
40 
42 //
43 // Templates for serializing to anything that looks like a stream,
44 // i.e. anything that supports .read(char*, int) and .write(char*, int)
45 //
46 
47 enum
48 {
49  // primary actions
50  SER_NETWORK = (1 << 0),
51  SER_DISK = (1 << 1),
52  SER_GETHASH = (1 << 2),
53 };
54 
55 #define IMPLEMENT_SERIALIZE(statements) \
56  unsigned int GetSerializeSize(int nType, int nVersion) 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  assert(fGetSize||fWrite||fRead); /* suppress warning */ \
65  s.nType = nType; \
66  s.nVersion = nVersion; \
67  {statements} \
68  return nSerSize; \
69  } \
70  template<typename Stream> \
71  void Serialize(Stream& s, int nType, int nVersion) const \
72  { \
73  CSerActionSerialize ser_action; \
74  const bool fGetSize = false; \
75  const bool fWrite = true; \
76  const bool fRead = false; \
77  unsigned int nSerSize = 0; \
78  assert(fGetSize||fWrite||fRead); /* suppress warning */ \
79  {statements} \
80  } \
81  template<typename Stream> \
82  void Unserialize(Stream& s, int nType, int nVersion) \
83  { \
84  CSerActionUnserialize ser_action; \
85  const bool fGetSize = false; \
86  const bool fWrite = false; \
87  const bool fRead = true; \
88  unsigned int nSerSize = 0; \
89  assert(fGetSize||fWrite||fRead); /* suppress warning */ \
90  {statements} \
91  }
92 
93 #define READWRITE(obj) (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))
94 
95 
96 
97 
98 
99 
100 //
101 // Basic types
102 //
103 #define WRITEDATA(s, obj) s.write((char*)&(obj), sizeof(obj))
104 #define READDATA(s, obj) s.read((char*)&(obj), sizeof(obj))
105 
106 inline unsigned int GetSerializeSize(char a, int, int=0) { return sizeof(a); }
107 inline unsigned int GetSerializeSize(signed char a, int, int=0) { return sizeof(a); }
108 inline unsigned int GetSerializeSize(unsigned char a, int, int=0) { return sizeof(a); }
109 inline unsigned int GetSerializeSize(signed short a, int, int=0) { return sizeof(a); }
110 inline unsigned int GetSerializeSize(unsigned short a, int, int=0) { return sizeof(a); }
111 inline unsigned int GetSerializeSize(signed int a, int, int=0) { return sizeof(a); }
112 inline unsigned int GetSerializeSize(unsigned int a, int, int=0) { return sizeof(a); }
113 inline unsigned int GetSerializeSize(signed long a, int, int=0) { return sizeof(a); }
114 inline unsigned int GetSerializeSize(unsigned long a, int, int=0) { return sizeof(a); }
115 inline unsigned int GetSerializeSize(int64 a, int, int=0) { return sizeof(a); }
116 inline unsigned int GetSerializeSize(uint64 a, int, int=0) { return sizeof(a); }
117 inline unsigned int GetSerializeSize(float a, int, int=0) { return sizeof(a); }
118 inline unsigned int GetSerializeSize(double a, int, int=0) { return sizeof(a); }
119 
120 template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { WRITEDATA(s, a); }
121 template<typename Stream> inline void Serialize(Stream& s, signed char a, int, int=0) { WRITEDATA(s, a); }
122 template<typename Stream> inline void Serialize(Stream& s, unsigned char a, int, int=0) { WRITEDATA(s, a); }
123 template<typename Stream> inline void Serialize(Stream& s, signed short a, int, int=0) { WRITEDATA(s, a); }
124 template<typename Stream> inline void Serialize(Stream& s, unsigned short a, int, int=0) { WRITEDATA(s, a); }
125 template<typename Stream> inline void Serialize(Stream& s, signed int a, int, int=0) { WRITEDATA(s, a); }
126 template<typename Stream> inline void Serialize(Stream& s, unsigned int a, int, int=0) { WRITEDATA(s, a); }
127 template<typename Stream> inline void Serialize(Stream& s, signed long a, int, int=0) { WRITEDATA(s, a); }
128 template<typename Stream> inline void Serialize(Stream& s, unsigned long a, int, int=0) { WRITEDATA(s, a); }
129 template<typename Stream> inline void Serialize(Stream& s, int64 a, int, int=0) { WRITEDATA(s, a); }
130 template<typename Stream> inline void Serialize(Stream& s, uint64 a, int, int=0) { WRITEDATA(s, a); }
131 template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { WRITEDATA(s, a); }
132 template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { WRITEDATA(s, a); }
133 
134 template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { READDATA(s, a); }
135 template<typename Stream> inline void Unserialize(Stream& s, signed char& a, int, int=0) { READDATA(s, a); }
136 template<typename Stream> inline void Unserialize(Stream& s, unsigned char& a, int, int=0) { READDATA(s, a); }
137 template<typename Stream> inline void Unserialize(Stream& s, signed short& a, int, int=0) { READDATA(s, a); }
138 template<typename Stream> inline void Unserialize(Stream& s, unsigned short& a, int, int=0) { READDATA(s, a); }
139 template<typename Stream> inline void Unserialize(Stream& s, signed int& a, int, int=0) { READDATA(s, a); }
140 template<typename Stream> inline void Unserialize(Stream& s, unsigned int& a, int, int=0) { READDATA(s, a); }
141 template<typename Stream> inline void Unserialize(Stream& s, signed long& a, int, int=0) { READDATA(s, a); }
142 template<typename Stream> inline void Unserialize(Stream& s, unsigned long& a, int, int=0) { READDATA(s, a); }
143 template<typename Stream> inline void Unserialize(Stream& s, int64& a, int, int=0) { READDATA(s, a); }
144 template<typename Stream> inline void Unserialize(Stream& s, uint64& a, int, int=0) { READDATA(s, a); }
145 template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { READDATA(s, a); }
146 template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { READDATA(s, a); }
147 
148 inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
149 template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; WRITEDATA(s, f); }
150 template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f; READDATA(s, f); a=f; }
151 
152 
153 
154 
155 
156 
157 //
158 // Compact size
159 // size < 253 -- 1 byte
160 // size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
161 // size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
162 // size > UINT_MAX -- 9 bytes (255 + 8 bytes)
163 //
164 inline unsigned int GetSizeOfCompactSize(uint64 nSize)
165 {
166  if (nSize < 253) return sizeof(unsigned char);
167  else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
168  else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
169  else return sizeof(unsigned char) + sizeof(uint64);
170 }
171 
172 template<typename Stream>
173 void WriteCompactSize(Stream& os, uint64 nSize)
174 {
175  if (nSize < 253)
176  {
177  unsigned char chSize = nSize;
178  WRITEDATA(os, chSize);
179  }
180  else if (nSize <= std::numeric_limits<unsigned short>::max())
181  {
182  unsigned char chSize = 253;
183  unsigned short xSize = nSize;
184  WRITEDATA(os, chSize);
185  WRITEDATA(os, xSize);
186  }
187  else if (nSize <= std::numeric_limits<unsigned int>::max())
188  {
189  unsigned char chSize = 254;
190  unsigned int xSize = nSize;
191  WRITEDATA(os, chSize);
192  WRITEDATA(os, xSize);
193  }
194  else
195  {
196  unsigned char chSize = 255;
197  uint64 xSize = nSize;
198  WRITEDATA(os, chSize);
199  WRITEDATA(os, xSize);
200  }
201  return;
202 }
203 
204 template<typename Stream>
206 {
207  unsigned char chSize;
208  READDATA(is, chSize);
209  uint64 nSizeRet = 0;
210  if (chSize < 253)
211  {
212  nSizeRet = chSize;
213  }
214  else if (chSize == 253)
215  {
216  unsigned short xSize;
217  READDATA(is, xSize);
218  nSizeRet = xSize;
219  }
220  else if (chSize == 254)
221  {
222  unsigned int xSize;
223  READDATA(is, xSize);
224  nSizeRet = xSize;
225  }
226  else
227  {
228  uint64 xSize;
229  READDATA(is, xSize);
230  nSizeRet = xSize;
231  }
232  if (nSizeRet > (uint64)MAX_SIZE)
233  throw std::ios_base::failure("ReadCompactSize() : size too large");
234  return nSizeRet;
235 }
236 
237 // Variable-length integers: bytes are a MSB base-128 encoding of the number.
238 // The high bit in each byte signifies whether another digit follows. To make
239 // the encoding is one-to-one, one is subtracted from all but the last digit.
240 // Thus, the byte sequence a[] with length len, where all but the last byte
241 // has bit 128 set, encodes the number:
242 //
243 // (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
244 //
245 // Properties:
246 // * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
247 // * Every integer has exactly one encoding
248 // * Encoding does not depend on size of original integer type
249 // * No redundancy: every (infinite) byte sequence corresponds to a list
250 // of encoded integers.
251 //
252 // 0: [0x00] 256: [0x81 0x00]
253 // 1: [0x01] 16383: [0xFE 0x7F]
254 // 127: [0x7F] 16384: [0xFF 0x00]
255 // 128: [0x80 0x00] 16511: [0x80 0xFF 0x7F]
256 // 255: [0x80 0x7F] 65535: [0x82 0xFD 0x7F]
257 // 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
258 
259 template<typename I>
260 inline unsigned int GetSizeOfVarInt(I n)
261 {
262  int nRet = 0;
263  while(true) {
264  nRet++;
265  if (n <= 0x7F)
266  break;
267  n = (n >> 7) - 1;
268  }
269  return nRet;
270 }
271 
272 template<typename Stream, typename I>
273 void WriteVarInt(Stream& os, I n)
274 {
275  unsigned char tmp[(sizeof(n)*8+6)/7];
276  int len=0;
277  while(true) {
278  tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
279  if (n <= 0x7F)
280  break;
281  n = (n >> 7) - 1;
282  len++;
283  }
284  do {
285  WRITEDATA(os, tmp[len]);
286  } while(len--);
287 }
288 
289 template<typename Stream, typename I>
290 I ReadVarInt(Stream& is)
291 {
292  I n = 0;
293  while(true) {
294  unsigned char chData;
295  READDATA(is, chData);
296  n = (n << 7) | (chData & 0x7F);
297  if (chData & 0x80)
298  n++;
299  else
300  return n;
301  }
302 }
303 
304 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
305 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
306 
310 {
311 protected:
312  char* pbegin;
313  char* pend;
314 public:
315  CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
316  char* begin() { return pbegin; }
317  const char* begin() const { return pbegin; }
318  char* end() { return pend; }
319  const char* end() const { return pend; }
320 
321  unsigned int GetSerializeSize(int, int=0) const
322  {
323  return pend - pbegin;
324  }
325 
326  template<typename Stream>
327  void Serialize(Stream& s, int, int=0) const
328  {
329  s.write(pbegin, pend - pbegin);
330  }
331 
332  template<typename Stream>
333  void Unserialize(Stream& s, int, int=0)
334  {
335  s.read(pbegin, pend - pbegin);
336  }
337 };
338 
339 template<typename I>
340 class CVarInt
341 {
342 protected:
343  I &n;
344 public:
345  CVarInt(I& nIn) : n(nIn) { }
346 
347  unsigned int GetSerializeSize(int, int) const {
348  return GetSizeOfVarInt<I>(n);
349  }
350 
351  template<typename Stream>
352  void Serialize(Stream &s, int, int) const {
353  WriteVarInt<Stream,I>(s, n);
354  }
355 
356  template<typename Stream>
357  void Unserialize(Stream& s, int, int) {
358  n = ReadVarInt<Stream,I>(s);
359  }
360 };
361 
362 template<typename I>
363 CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
364 
365 //
366 // Forward declarations
367 //
368 
369 // string
370 template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
371 template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
372 template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
373 
374 // vector
375 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
376 template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
377 template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
378 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&);
379 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&);
380 template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
381 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&);
382 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&);
383 template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
384 
385 // others derived from vector
386 extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion);
387 template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion);
388 template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion);
389 
390 // pair
391 template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
392 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
393 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
394 
395 // 3 tuple
396 template<typename T0, typename T1, typename T2> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion);
397 template<typename Stream, typename T0, typename T1, typename T2> void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion);
398 template<typename Stream, typename T0, typename T1, typename T2> void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion);
399 
400 // 4 tuple
401 template<typename T0, typename T1, typename T2, typename T3> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion);
402 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);
403 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);
404 
405 // map
406 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
407 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);
408 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);
409 
410 // set
411 template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
412 template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
413 template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
414 
415 
416 
417 
418 
419 //
420 // If none of the specialized versions above matched, default to calling member function.
421 // "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
422 // The compiler will only cast int to long if none of the other templates matched.
423 // Thanks to Boost serialization for this idea.
424 //
425 template<typename T>
426 inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
427 {
428  return a.GetSerializeSize((int)nType, nVersion);
429 }
430 
431 template<typename Stream, typename T>
432 inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
433 {
434  a.Serialize(os, (int)nType, nVersion);
435 }
436 
437 template<typename Stream, typename T>
438 inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
439 {
440  a.Unserialize(is, (int)nType, nVersion);
441 }
442 
443 
444 
445 
446 
447 //
448 // string
449 //
450 template<typename C>
451 unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
452 {
453  return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
454 }
455 
456 template<typename Stream, typename C>
457 void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
458 {
459  WriteCompactSize(os, str.size());
460  if (!str.empty())
461  os.write((char*)&str[0], str.size() * sizeof(str[0]));
462 }
463 
464 template<typename Stream, typename C>
465 void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
466 {
467  unsigned int nSize = ReadCompactSize(is);
468  str.resize(nSize);
469  if (nSize != 0)
470  is.read((char*)&str[0], nSize * sizeof(str[0]));
471 }
472 
473 
474 
475 //
476 // vector
477 //
478 template<typename T, typename A>
479 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
480 {
481  return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
482 }
483 
484 template<typename T, typename A>
485 unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
486 {
487  unsigned int nSize = GetSizeOfCompactSize(v.size());
488  for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
489  nSize += GetSerializeSize((*vi), nType, nVersion);
490  return nSize;
491 }
492 
493 template<typename T, typename A>
494 inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
495 {
496  return GetSerializeSize_impl(v, nType, nVersion, boost::is_fundamental<T>());
497 }
498 
499 
500 template<typename Stream, typename T, typename A>
501 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
502 {
503  WriteCompactSize(os, v.size());
504  if (!v.empty())
505  os.write((char*)&v[0], v.size() * sizeof(T));
506 }
507 
508 template<typename Stream, typename T, typename A>
509 void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
510 {
511  WriteCompactSize(os, v.size());
512  for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
513  ::Serialize(os, (*vi), nType, nVersion);
514 }
515 
516 template<typename Stream, typename T, typename A>
517 inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
518 {
519  Serialize_impl(os, v, nType, nVersion, boost::is_fundamental<T>());
520 }
521 
522 
523 template<typename Stream, typename T, typename A>
524 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
525 {
526  // Limit size per read so bogus size value won't cause out of memory
527  v.clear();
528  unsigned int nSize = ReadCompactSize(is);
529  unsigned int i = 0;
530  while (i < nSize)
531  {
532  unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
533  v.resize(i + blk);
534  is.read((char*)&v[i], blk * sizeof(T));
535  i += blk;
536  }
537 }
538 
539 template<typename Stream, typename T, typename A>
540 void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
541 {
542  v.clear();
543  unsigned int nSize = ReadCompactSize(is);
544  unsigned int i = 0;
545  unsigned int nMid = 0;
546  while (nMid < nSize)
547  {
548  nMid += 5000000 / sizeof(T);
549  if (nMid > nSize)
550  nMid = nSize;
551  v.resize(nMid);
552  for (; i < nMid; i++)
553  Unserialize(is, v[i], nType, nVersion);
554  }
555 }
556 
557 template<typename Stream, typename T, typename A>
558 inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
559 {
560  Unserialize_impl(is, v, nType, nVersion, boost::is_fundamental<T>());
561 }
562 
563 
564 
565 //
566 // others derived from vector
567 //
568 inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
569 {
570  return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
571 }
572 
573 template<typename Stream>
574 void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
575 {
576  Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
577 }
578 
579 template<typename Stream>
580 void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
581 {
582  Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
583 }
584 
585 
586 
587 //
588 // pair
589 //
590 template<typename K, typename T>
591 unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
592 {
593  return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
594 }
595 
596 template<typename Stream, typename K, typename T>
597 void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
598 {
599  Serialize(os, item.first, nType, nVersion);
600  Serialize(os, item.second, nType, nVersion);
601 }
602 
603 template<typename Stream, typename K, typename T>
604 void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
605 {
606  Unserialize(is, item.first, nType, nVersion);
607  Unserialize(is, item.second, nType, nVersion);
608 }
609 
610 
611 
612 //
613 // 3 tuple
614 //
615 template<typename T0, typename T1, typename T2>
616 unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
617 {
618  unsigned int nSize = 0;
619  nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
620  nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
621  nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
622  return nSize;
623 }
624 
625 template<typename Stream, typename T0, typename T1, typename T2>
626 void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
627 {
628  Serialize(os, boost::get<0>(item), nType, nVersion);
629  Serialize(os, boost::get<1>(item), nType, nVersion);
630  Serialize(os, boost::get<2>(item), nType, nVersion);
631 }
632 
633 template<typename Stream, typename T0, typename T1, typename T2>
634 void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
635 {
636  Unserialize(is, boost::get<0>(item), nType, nVersion);
637  Unserialize(is, boost::get<1>(item), nType, nVersion);
638  Unserialize(is, boost::get<2>(item), nType, nVersion);
639 }
640 
641 
642 
643 //
644 // 4 tuple
645 //
646 template<typename T0, typename T1, typename T2, typename T3>
647 unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
648 {
649  unsigned int nSize = 0;
650  nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
651  nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
652  nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
653  nSize += GetSerializeSize(boost::get<3>(item), nType, nVersion);
654  return nSize;
655 }
656 
657 template<typename Stream, typename T0, typename T1, typename T2, typename T3>
658 void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
659 {
660  Serialize(os, boost::get<0>(item), nType, nVersion);
661  Serialize(os, boost::get<1>(item), nType, nVersion);
662  Serialize(os, boost::get<2>(item), nType, nVersion);
663  Serialize(os, boost::get<3>(item), nType, nVersion);
664 }
665 
666 template<typename Stream, typename T0, typename T1, typename T2, typename T3>
667 void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
668 {
669  Unserialize(is, boost::get<0>(item), nType, nVersion);
670  Unserialize(is, boost::get<1>(item), nType, nVersion);
671  Unserialize(is, boost::get<2>(item), nType, nVersion);
672  Unserialize(is, boost::get<3>(item), nType, nVersion);
673 }
674 
675 
676 
677 //
678 // map
679 //
680 template<typename K, typename T, typename Pred, typename A>
681 unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
682 {
683  unsigned int nSize = GetSizeOfCompactSize(m.size());
684  for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
685  nSize += GetSerializeSize((*mi), nType, nVersion);
686  return nSize;
687 }
688 
689 template<typename Stream, typename K, typename T, typename Pred, typename A>
690 void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
691 {
692  WriteCompactSize(os, m.size());
693  for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
694  Serialize(os, (*mi), nType, nVersion);
695 }
696 
697 template<typename Stream, typename K, typename T, typename Pred, typename A>
698 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
699 {
700  m.clear();
701  unsigned int nSize = ReadCompactSize(is);
702  typename std::map<K, T, Pred, A>::iterator mi = m.begin();
703  for (unsigned int i = 0; i < nSize; i++)
704  {
705  std::pair<K, T> item;
706  Unserialize(is, item, nType, nVersion);
707  mi = m.insert(mi, item);
708  }
709 }
710 
711 
712 
713 //
714 // set
715 //
716 template<typename K, typename Pred, typename A>
717 unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
718 {
719  unsigned int nSize = GetSizeOfCompactSize(m.size());
720  for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
721  nSize += GetSerializeSize((*it), nType, nVersion);
722  return nSize;
723 }
724 
725 template<typename Stream, typename K, typename Pred, typename A>
726 void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
727 {
728  WriteCompactSize(os, m.size());
729  for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
730  Serialize(os, (*it), nType, nVersion);
731 }
732 
733 template<typename Stream, typename K, typename Pred, typename A>
734 void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
735 {
736  m.clear();
737  unsigned int nSize = ReadCompactSize(is);
738  typename std::set<K, Pred, A>::iterator it = m.begin();
739  for (unsigned int i = 0; i < nSize; i++)
740  {
741  K key;
742  Unserialize(is, key, nType, nVersion);
743  it = m.insert(it, key);
744  }
745 }
746 
747 
748 
749 //
750 // Support for IMPLEMENT_SERIALIZE and READWRITE macro
751 //
755 
756 template<typename Stream, typename T>
757 inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
758 {
759  return ::GetSerializeSize(obj, nType, nVersion);
760 }
761 
762 template<typename Stream, typename T>
763 inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
764 {
765  ::Serialize(s, obj, nType, nVersion);
766  return 0;
767 }
768 
769 template<typename Stream, typename T>
770 inline unsigned int SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
771 {
772  ::Unserialize(s, obj, nType, nVersion);
773  return 0;
774 }
775 
777 {
778  int nType;
779  int nVersion;
780 };
781 
782 
783 
784 
785 
786 
787 
788 
789 
790 
791 
792 typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;
793 
800 {
801 protected:
802  typedef CSerializeData vector_type;
803  vector_type vch;
804  unsigned int nReadPos;
805  short state;
806  short exceptmask;
807 public:
808  int nType;
809  int nVersion;
810 
811  typedef vector_type::allocator_type allocator_type;
812  typedef vector_type::size_type size_type;
813  typedef vector_type::difference_type difference_type;
814  typedef vector_type::reference reference;
815  typedef vector_type::const_reference const_reference;
816  typedef vector_type::value_type value_type;
817  typedef vector_type::iterator iterator;
818  typedef vector_type::const_iterator const_iterator;
819  typedef vector_type::reverse_iterator reverse_iterator;
820 
821  explicit CDataStream(int nTypeIn, int nVersionIn)
822  {
823  Init(nTypeIn, nVersionIn);
824  }
825 
826  CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
827  {
828  Init(nTypeIn, nVersionIn);
829  }
830 
831 #if !defined(_MSC_VER) || _MSC_VER >= 1300
832  CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
833  {
834  Init(nTypeIn, nVersionIn);
835  }
836 #endif
837 
838  CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
839  {
840  Init(nTypeIn, nVersionIn);
841  }
842 
843  CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
844  {
845  Init(nTypeIn, nVersionIn);
846  }
847 
848  CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
849  {
850  Init(nTypeIn, nVersionIn);
851  }
852 
853  void Init(int nTypeIn, int nVersionIn)
854  {
855  nReadPos = 0;
856  nType = nTypeIn;
857  nVersion = nVersionIn;
858  state = 0;
859  exceptmask = std::ios::badbit | std::ios::failbit;
860  }
861 
863  {
864  vch.insert(vch.end(), b.begin(), b.end());
865  return *this;
866  }
867 
868  friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
869  {
870  CDataStream ret = a;
871  ret += b;
872  return (ret);
873  }
874 
875  std::string str() const
876  {
877  return (std::string(begin(), end()));
878  }
879 
880 
881  //
882  // Vector subset
883  //
884  const_iterator begin() const { return vch.begin() + nReadPos; }
885  iterator begin() { return vch.begin() + nReadPos; }
886  const_iterator end() const { return vch.end(); }
887  iterator end() { return vch.end(); }
888  size_type size() const { return vch.size() - nReadPos; }
889  bool empty() const { return vch.size() == nReadPos; }
890  void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
891  void reserve(size_type n) { vch.reserve(n + nReadPos); }
892  const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
893  reference operator[](size_type pos) { return vch[pos + nReadPos]; }
894  void clear() { vch.clear(); nReadPos = 0; }
895  iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
896  void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
897 
898  void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
899  {
900  assert(last - first >= 0);
901  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
902  {
903  // special case for inserting at the front when there's room
904  nReadPos -= (last - first);
905  memcpy(&vch[nReadPos], &first[0], last - first);
906  }
907  else
908  vch.insert(it, first, last);
909  }
910 
911 #if !defined(_MSC_VER) || _MSC_VER >= 1300
912  void insert(iterator it, const char* first, const char* last)
913  {
914  assert(last - first >= 0);
915  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
916  {
917  // special case for inserting at the front when there's room
918  nReadPos -= (last - first);
919  memcpy(&vch[nReadPos], &first[0], last - first);
920  }
921  else
922  vch.insert(it, first, last);
923  }
924 #endif
925 
926  iterator erase(iterator it)
927  {
928  if (it == vch.begin() + nReadPos)
929  {
930  // special case for erasing from the front
931  if (++nReadPos >= vch.size())
932  {
933  // whenever we reach the end, we take the opportunity to clear the buffer
934  nReadPos = 0;
935  return vch.erase(vch.begin(), vch.end());
936  }
937  return vch.begin() + nReadPos;
938  }
939  else
940  return vch.erase(it);
941  }
942 
943  iterator erase(iterator first, iterator last)
944  {
945  if (first == vch.begin() + nReadPos)
946  {
947  // special case for erasing from the front
948  if (last == vch.end())
949  {
950  nReadPos = 0;
951  return vch.erase(vch.begin(), vch.end());
952  }
953  else
954  {
955  nReadPos = (last - vch.begin());
956  return last;
957  }
958  }
959  else
960  return vch.erase(first, last);
961  }
962 
963  inline void Compact()
964  {
965  vch.erase(vch.begin(), vch.begin() + nReadPos);
966  nReadPos = 0;
967  }
968 
969  bool Rewind(size_type n)
970  {
971  // Rewind by n characters if the buffer hasn't been compacted yet
972  if (n > nReadPos)
973  return false;
974  nReadPos -= n;
975  return true;
976  }
977 
978 
979  //
980  // Stream subset
981  //
982  void setstate(short bits, const char* psz)
983  {
984  state |= bits;
985  if (state & exceptmask)
986  throw std::ios_base::failure(psz);
987  }
988 
989  bool eof() const { return size() == 0; }
990  bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
991  bool good() const { return !eof() && (state == 0); }
992  void clear(short n) { state = n; } // name conflict with vector clear()
993  short exceptions() { return exceptmask; }
994  short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
995  CDataStream* rdbuf() { return this; }
996  int in_avail() { return size(); }
997 
998  void SetType(int n) { nType = n; }
999  int GetType() { return nType; }
1000  void SetVersion(int n) { nVersion = n; }
1001  int GetVersion() { return nVersion; }
1002  void ReadVersion() { *this >> nVersion; }
1003  void WriteVersion() { *this << nVersion; }
1004 
1005  CDataStream& read(char* pch, int nSize)
1006  {
1007  // Read from the beginning of the buffer
1008  assert(nSize >= 0);
1009  unsigned int nReadPosNext = nReadPos + nSize;
1010  if (nReadPosNext >= vch.size())
1011  {
1012  if (nReadPosNext > vch.size())
1013  {
1014  setstate(std::ios::failbit, "CDataStream::read() : end of data");
1015  memset(pch, 0, nSize);
1016  nSize = vch.size() - nReadPos;
1017  }
1018  memcpy(pch, &vch[nReadPos], nSize);
1019  nReadPos = 0;
1020  vch.clear();
1021  return (*this);
1022  }
1023  memcpy(pch, &vch[nReadPos], nSize);
1024  nReadPos = nReadPosNext;
1025  return (*this);
1026  }
1027 
1028  CDataStream& ignore(int nSize)
1029  {
1030  // Ignore from the beginning of the buffer
1031  assert(nSize >= 0);
1032  unsigned int nReadPosNext = nReadPos + nSize;
1033  if (nReadPosNext >= vch.size())
1034  {
1035  if (nReadPosNext > vch.size())
1036  {
1037  setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
1038  nSize = vch.size() - nReadPos;
1039  }
1040  nReadPos = 0;
1041  vch.clear();
1042  return (*this);
1043  }
1044  nReadPos = nReadPosNext;
1045  return (*this);
1046  }
1047 
1048  CDataStream& write(const char* pch, int nSize)
1049  {
1050  // Write to the end of the buffer
1051  assert(nSize >= 0);
1052  vch.insert(vch.end(), pch, pch + nSize);
1053  return (*this);
1054  }
1055 
1056  template<typename Stream>
1057  void Serialize(Stream& s, int nType, int nVersion) const
1058  {
1059  // Special case: stream << stream concatenates like stream += stream
1060  if (!vch.empty())
1061  s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
1062  }
1063 
1064  template<typename T>
1065  unsigned int GetSerializeSize(const T& obj)
1066  {
1067  // Tells the size of the object if serialized to this stream
1068  return ::GetSerializeSize(obj, nType, nVersion);
1069  }
1070 
1071  template<typename T>
1072  CDataStream& operator<<(const T& obj)
1073  {
1074  // Serialize to this stream
1075  ::Serialize(*this, obj, nType, nVersion);
1076  return (*this);
1077  }
1078 
1079  template<typename T>
1081  {
1082  // Unserialize from this stream
1083  ::Unserialize(*this, obj, nType, nVersion);
1084  return (*this);
1085  }
1086 
1087  void GetAndClear(CSerializeData &data) {
1088  vch.swap(data);
1089  CSerializeData().swap(vch);
1090  }
1091 };
1092 
1093 
1094 
1095 
1096 
1097 
1098 
1099 
1100 
1101 
1109 {
1110 protected:
1111  FILE* file;
1112  short state;
1113  short exceptmask;
1114 public:
1115  int nType;
1117 
1118  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
1119  {
1120  file = filenew;
1121  nType = nTypeIn;
1122  nVersion = nVersionIn;
1123  state = 0;
1124  exceptmask = std::ios::badbit | std::ios::failbit;
1125  }
1126 
1128  {
1129  fclose();
1130  }
1131 
1132  void fclose()
1133  {
1134  if (file != NULL && file != stdin && file != stdout && file != stderr)
1135  ::fclose(file);
1136  file = NULL;
1137  }
1138 
1139  FILE* release() { FILE* ret = file; file = NULL; return ret; }
1140  operator FILE*() { return file; }
1141  FILE* operator->() { return file; }
1142  FILE& operator*() { return *file; }
1143  FILE** operator&() { return &file; }
1144  FILE* operator=(FILE* pnew) { return file = pnew; }
1145  bool operator!() { return (file == NULL); }
1146 
1147 
1148  //
1149  // Stream subset
1150  //
1151  void setstate(short bits, const char* psz)
1152  {
1153  state |= bits;
1154  if (state & exceptmask)
1155  throw std::ios_base::failure(psz);
1156  }
1157 
1158  bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
1159  bool good() const { return state == 0; }
1160  void clear(short n = 0) { state = n; }
1161  short exceptions() { return exceptmask; }
1162  short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
1163 
1164  void SetType(int n) { nType = n; }
1165  int GetType() { return nType; }
1166  void SetVersion(int n) { nVersion = n; }
1167  int GetVersion() { return nVersion; }
1168  void ReadVersion() { *this >> nVersion; }
1169  void WriteVersion() { *this << nVersion; }
1170 
1171  CAutoFile& read(char* pch, size_t nSize)
1172  {
1173  if (!file)
1174  throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
1175  if (fread(pch, 1, nSize, file) != nSize)
1176  setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
1177  return (*this);
1178  }
1179 
1180  CAutoFile& write(const char* pch, size_t nSize)
1181  {
1182  if (!file)
1183  throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
1184  if (fwrite(pch, 1, nSize, file) != nSize)
1185  setstate(std::ios::failbit, "CAutoFile::write : write failed");
1186  return (*this);
1187  }
1188 
1189  template<typename T>
1190  unsigned int GetSerializeSize(const T& obj)
1191  {
1192  // Tells the size of the object if serialized to this stream
1193  return ::GetSerializeSize(obj, nType, nVersion);
1194  }
1195 
1196  template<typename T>
1197  CAutoFile& operator<<(const T& obj)
1198  {
1199  // Serialize to this stream
1200  if (!file)
1201  throw std::ios_base::failure("CAutoFile::operator<< : file handle is NULL");
1202  ::Serialize(*this, obj, nType, nVersion);
1203  return (*this);
1204  }
1205 
1206  template<typename T>
1208  {
1209  // Unserialize from this stream
1210  if (!file)
1211  throw std::ios_base::failure("CAutoFile::operator>> : file handle is NULL");
1212  ::Unserialize(*this, obj, nType, nVersion);
1213  return (*this);
1214  }
1215 };
1216 
1221 {
1222 private:
1223  FILE *src; // source file
1224  uint64 nSrcPos; // how many bytes have been read from source
1225  uint64 nReadPos; // how many bytes have been read from this
1226  uint64 nReadLimit; // up to which position we're allowed to read
1227  uint64 nRewind; // how many bytes we guarantee to rewind
1228  std::vector<char> vchBuf; // the buffer
1229 
1230  short state;
1231  short exceptmask;
1232 
1233 protected:
1234  void setstate(short bits, const char *psz) {
1235  state |= bits;
1236  if (state & exceptmask)
1237  throw std::ios_base::failure(psz);
1238  }
1239 
1240  // read data from the source to fill the buffer
1241  bool Fill() {
1242  unsigned int pos = nSrcPos % vchBuf.size();
1243  unsigned int readNow = vchBuf.size() - pos;
1244  unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
1245  if (nAvail < readNow)
1246  readNow = nAvail;
1247  if (readNow == 0)
1248  return false;
1249  size_t read = fread((void*)&vchBuf[pos], 1, readNow, src);
1250  if (read == 0) {
1251  setstate(std::ios_base::failbit, feof(src) ? "CBufferedFile::Fill : end of file" : "CBufferedFile::Fill : fread failed");
1252  return false;
1253  } else {
1254  nSrcPos += read;
1255  return true;
1256  }
1257  }
1258 
1259 public:
1260  int nType;
1262 
1263  CBufferedFile(FILE *fileIn, uint64 nBufSize, uint64 nRewindIn, int nTypeIn, int nVersionIn) :
1264  src(fileIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0),
1265  state(0), exceptmask(std::ios_base::badbit | std::ios_base::failbit), nType(nTypeIn), nVersion(nVersionIn) {
1266  }
1267 
1268  // check whether no error occurred
1269  bool good() const {
1270  return state == 0;
1271  }
1272 
1273  // check whether we're at the end of the source file
1274  bool eof() const {
1275  return nReadPos == nSrcPos && feof(src);
1276  }
1277 
1278  // read a number of bytes
1279  CBufferedFile& read(char *pch, size_t nSize) {
1280  if (nSize + nReadPos > nReadLimit)
1281  throw std::ios_base::failure("Read attempted past buffer limit");
1282  if (nSize + nRewind > vchBuf.size())
1283  throw std::ios_base::failure("Read larger than buffer size");
1284  while (nSize > 0) {
1285  if (nReadPos == nSrcPos)
1286  Fill();
1287  unsigned int pos = nReadPos % vchBuf.size();
1288  size_t nNow = nSize;
1289  if (nNow + pos > vchBuf.size())
1290  nNow = vchBuf.size() - pos;
1291  if (nNow + nReadPos > nSrcPos)
1292  nNow = nSrcPos - nReadPos;
1293  memcpy(pch, &vchBuf[pos], nNow);
1294  nReadPos += nNow;
1295  pch += nNow;
1296  nSize -= nNow;
1297  }
1298  return (*this);
1299  }
1300 
1301  // return the current reading position
1303  return nReadPos;
1304  }
1305 
1306  // rewind to a given reading position
1307  bool SetPos(uint64 nPos) {
1308  nReadPos = nPos;
1309  if (nReadPos + nRewind < nSrcPos) {
1310  nReadPos = nSrcPos - nRewind;
1311  return false;
1312  } else if (nReadPos > nSrcPos) {
1313  nReadPos = nSrcPos;
1314  return false;
1315  } else {
1316  return true;
1317  }
1318  }
1319 
1320  bool Seek(uint64 nPos) {
1321  long nLongPos = nPos;
1322  if (nPos != (uint64)nLongPos)
1323  return false;
1324  if (fseek(src, nLongPos, SEEK_SET))
1325  return false;
1326  nLongPos = ftell(src);
1327  nSrcPos = nLongPos;
1328  nReadPos = nLongPos;
1329  state = 0;
1330  return true;
1331  }
1332 
1333  // prevent reading beyond a certain position
1334  // no argument removes the limit
1335  bool SetLimit(uint64 nPos = (uint64)(-1)) {
1336  if (nPos < nReadPos)
1337  return false;
1338  nReadLimit = nPos;
1339  return true;
1340  }
1341 
1342  template<typename T>
1344  // Unserialize from this stream
1345  ::Unserialize(*this, obj, nType, nVersion);
1346  return (*this);
1347  }
1348 
1349  // search for a given byte in the stream, and remain positioned on it
1350  void FindByte(char ch) {
1351  while (true) {
1352  if (nReadPos == nSrcPos)
1353  Fill();
1354  if (vchBuf[nReadPos % vchBuf.size()] == ch)
1355  break;
1356  nReadPos++;
1357  }
1358  }
1359 };
1360 
1361 #endif
void setstate(short bits, const char *psz)
Definition: serialize.h:982
short state
Definition: serialize.h:805
bool Seek(uint64 nPos)
Definition: serialize.h:1320
void Serialize(Stream &s, int nType, int nVersion) const
Definition: serialize.h:1057
LRUHandle * prev
Definition: cache.cc:30
bool good() const
Definition: serialize.h:991
CAutoFile & read(char *pch, size_t nSize)
Definition: serialize.h:1171
void setstate(short bits, const char *psz)
Definition: serialize.h:1151
void Init(int nTypeIn, int nVersionIn)
Definition: serialize.h:853
CSerializeData vector_type
Definition: serialize.h:802
bool good() const
Definition: serialize.h:1269
bool fail() const
Definition: serialize.h:1158
vector_type::iterator iterator
Definition: serialize.h:817
void ReadVersion()
Definition: serialize.h:1168
unsigned int SerReadWrite(Stream &s, const T &obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
Definition: serialize.h:757
vector_type::allocator_type allocator_type
Definition: serialize.h:811
vector_type vch
Definition: serialize.h:803
I & n
Definition: serialize.h:343
int GetVersion()
Definition: serialize.h:1167
const_iterator begin() const
Definition: serialize.h:884
void Compact()
Definition: serialize.h:963
void GetAndClear(CSerializeData &data)
Definition: serialize.h:1087
CAutoFile & operator>>(T &obj)
Definition: serialize.h:1207
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
Definition: serialize.h:826
unsigned int nReadPos
Definition: serialize.h:804
void clear(short n=0)
Definition: serialize.h:1160
short exceptmask
Definition: serialize.h:1231
CDataStream & ignore(int nSize)
Definition: serialize.h:1028
CDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
Definition: serialize.h:848
vector_type::size_type size_type
Definition: serialize.h:812
uint64 nSrcPos
Definition: serialize.h:1224
void resize(size_type n, value_type c=0)
Definition: serialize.h:890
void Serialize(Stream &s, char a, int, int=0)
Definition: serialize.h:120
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
Definition: serialize.h:838
CDataStream & read(char *pch, int nSize)
Definition: serialize.h:1005
vector_type::reference reference
Definition: serialize.h:814
bool SetLimit(uint64 nPos=(uint64)(-1))
Definition: serialize.h:1335
bool SetPos(uint64 nPos)
Definition: serialize.h:1307
vector_type::value_type value_type
Definition: serialize.h:816
CBufferedFile & read(char *pch, size_t nSize)
Definition: serialize.h:1279
short exceptions(short mask)
Definition: serialize.h:1162
void clear(short n)
Definition: serialize.h:992
CBufferedFile(FILE *fileIn, uint64 nBufSize, uint64 nRewindIn, int nTypeIn, int nVersionIn)
Definition: serialize.h:1263
char * end()
Definition: serialize.h:318
int GetType()
Definition: serialize.h:999
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:799
bool eof() const
Definition: serialize.h:1274
#define MAX_SIZE
Definition: mruset_tests.cpp:9
unsigned long long uint64
Definition: serialize.h:26
unsigned int GetSerializeSize(const T &obj)
Definition: serialize.h:1190
char * begin()
Definition: serialize.h:316
vector_type::reverse_iterator reverse_iterator
Definition: serialize.h:819
iterator erase(iterator it)
Definition: serialize.h:926
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: serialize.h:1118
iterator end()
Definition: serialize.h:887
unsigned int GetSizeOfCompactSize(uint64 nSize)
Definition: serialize.h:164
friend CDataStream operator+(const CDataStream &a, const CDataStream &b)
Definition: serialize.h:868
#define WRITEDATA(s, obj)
Definition: serialize.h:103
CDataStream * rdbuf()
Definition: serialize.h:995
void WriteVarInt(Stream &os, I n)
Definition: serialize.h:273
CDataStream(int nTypeIn, int nVersionIn)
Definition: serialize.h:821
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
Definition: serialize.h:898
FILE * release()
Definition: serialize.h:1139
short exceptions()
Definition: serialize.h:993
vector_type::const_reference const_reference
Definition: serialize.h:815
I ReadVarInt(Stream &is)
Definition: serialize.h:290
int GetType()
Definition: serialize.h:1165
std::string str() const
Definition: serialize.h:875
void fclose()
Definition: serialize.h:1132
int nVersion
Definition: serialize.h:1116
void setstate(short bits, const char *psz)
Definition: serialize.h:1234
unsigned int GetSerializeSize(int, int) const
Definition: serialize.h:347
const char * begin() const
Definition: serialize.h:317
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
Definition: serialize.h:843
vector_type::const_iterator const_iterator
Definition: serialize.h:818
const char * end() const
Definition: serialize.h:319
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:106
short state
Definition: serialize.h:1112
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
Definition: serialize.h:832
size_type size() const
Definition: serialize.h:888
void Unserialize(Stream &s, char &a, int, int=0)
Definition: serialize.h:134
CDataStream & operator+=(const CDataStream &b)
Definition: serialize.h:862
std::vector< char, zero_after_free_allocator< char > > CSerializeData
Definition: serialize.h:792
CDataStream & operator>>(T &obj)
Definition: serialize.h:1080
CVarInt(I &nIn)
Definition: serialize.h:345
FILE & operator*()
Definition: serialize.h:1142
iterator begin()
Definition: serialize.h:885
short exceptmask
Definition: serialize.h:1113
void WriteVersion()
Definition: serialize.h:1003
char * pend
Definition: serialize.h:313
void Unserialize(Stream &s, int, int=0)
Definition: serialize.h:333
void Serialize(Stream &s, int, int) const
Definition: serialize.h:352
reference operator[](size_type pos)
Definition: serialize.h:893
unsigned int GetSizeOfVarInt(I n)
Definition: serialize.h:260
unsigned int GetSerializeSize(int, int=0) const
Definition: serialize.h:321
bool Rewind(size_type n)
Definition: serialize.h:969
uint64 nRewind
Definition: serialize.h:1227
void WriteCompactSize(Stream &os, uint64 nSize)
Definition: serialize.h:173
void Unserialize(Stream &s, int, int)
Definition: serialize.h:357
CFlatData(void *pbeginIn, void *pendIn)
Definition: serialize.h:315
CAutoFile & write(const char *pch, size_t nSize)
Definition: serialize.h:1180
bool eof() const
Definition: serialize.h:989
void Unserialize_impl(Stream &is, std::vector< T, A > &v, int nType, int nVersion, const boost::true_type &)
Definition: serialize.h:524
int GetVersion()
Definition: serialize.h:1001
FILE * operator=(FILE *pnew)
Definition: serialize.h:1144
iterator insert(iterator it, const char &x=char())
Definition: serialize.h:895
CDataStream & write(const char *pch, int nSize)
Definition: serialize.h:1048
short exceptions()
Definition: serialize.h:1161
CDataStream & operator<<(const T &obj)
Definition: serialize.h:1072
void FindByte(char ch)
Definition: serialize.h:1350
void reserve(size_type n)
Definition: serialize.h:891
FILE ** operator&()
Definition: serialize.h:1143
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:244
void insert(iterator it, size_type n, const char &x)
Definition: serialize.h:896
void SetType(int n)
Definition: serialize.h:1164
void ReadVersion()
Definition: serialize.h:1002
bool operator!()
Definition: serialize.h:1145
int nVersion
Definition: serialize.h:809
short exceptions(short mask)
Definition: serialize.h:994
void SetVersion(int n)
Definition: serialize.h:1166
vector_type::difference_type difference_type
Definition: serialize.h:813
int in_avail()
Definition: serialize.h:996
bool good() const
Definition: serialize.h:1159
FILE * operator->()
Definition: serialize.h:1141
const_reference operator[](size_type pos) const
Definition: serialize.h:892
unsigned int GetSerializeSize(const T &obj)
Definition: serialize.h:1065
uint64 nReadLimit
Definition: serialize.h:1226
void insert(iterator it, const char *first, const char *last)
Definition: serialize.h:912
void clear()
Definition: serialize.h:894
uint64 ReadCompactSize(Stream &is)
Definition: serialize.h:205
char * pbegin
Definition: serialize.h:312
FILE * file
Definition: serialize.h:1111
std::vector< char > vchBuf
Definition: serialize.h:1228
iterator erase(iterator first, iterator last)
Definition: serialize.h:943
void WriteVersion()
Definition: serialize.h:1169
void Serialize(Stream &s, int, int=0) const
Definition: serialize.h:327
void Serialize_impl(Stream &os, const std::vector< T, A > &v, int nType, int nVersion, const boost::true_type &)
Definition: serialize.h:501
Wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: serialize.h:1220
void SetVersion(int n)
Definition: serialize.h:1000
uint64 GetPos()
Definition: serialize.h:1302
CBufferedFile & operator>>(T &obj)
Definition: serialize.h:1343
T & REF(const T &val)
Definition: serialize.h:36
bool fail() const
Definition: serialize.h:990
CAutoFile & operator<<(const T &obj)
Definition: serialize.h:1197
uint64 nReadPos
Definition: serialize.h:1225
short exceptmask
Definition: serialize.h:806
CVarInt< I > WrapVarInt(I &n)
Definition: serialize.h:363
RAII wrapper for FILE*.
Definition: serialize.h:1108
Wrapper for serializing arrays and POD.
Definition: serialize.h:309
unsigned int GetSerializeSize_impl(const std::vector< T, A > &v, int nType, int nVersion, const boost::true_type &)
Definition: serialize.h:479
bool empty() const
Definition: serialize.h:889
const_iterator end() const
Definition: serialize.h:886
void SetType(int n)
Definition: serialize.h:998
long long int64
Definition: serialize.h:25
#define READDATA(s, obj)
Definition: serialize.h:104