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