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