6 #ifndef ANONCOIN_SERIALIZE_H
7 #define ANONCOIN_SERIALIZE_H
11 #if defined(HAVE_CONFIG_H)
29 #include <boost/tuple/tuple.hpp>
30 #include <boost/type_traits/is_fundamental.hpp>
36 static const unsigned int MAX_SIZE = 0x02000000;
41 inline T&
REF(
const T& val)
43 return const_cast<T&
>(val);
59 SER_IPADDRONLY = (1 << 18),
63 #define IMPLEMENT_SERIALIZE(statements) \
64 unsigned int GetSerializeSize(int nType, int nVersion) const \
66 CSerActionGetSerializeSize ser_action; \
67 const bool fGetSize = true; \
68 const bool fWrite = false; \
69 const bool fRead = false; \
70 unsigned int nSerSize = 0; \
71 ser_streamplaceholder s; \
72 assert(fGetSize||fWrite||fRead); \
74 s.nVersion = nVersion; \
78 template<typename Stream> \
79 void Serialize(Stream& s, int nType, int nVersion) const \
81 CSerActionSerialize ser_action; \
82 const bool fGetSize = false; \
83 const bool fWrite = true; \
84 const bool fRead = false; \
85 unsigned int nSerSize = 0; \
86 assert(fGetSize||fWrite||fRead); \
89 template<typename Stream> \
90 void Unserialize(Stream& s, int nType, int nVersion) \
92 CSerActionUnserialize ser_action; \
93 const bool fGetSize = false; \
94 const bool fWrite = false; \
95 const bool fRead = true; \
96 unsigned int nSerSize = 0; \
97 assert(fGetSize||fWrite||fRead); \
101 #define READWRITE(obj) (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))
111 #define WRITEDATA(s, obj) s.write((char*)&(obj), sizeof(obj))
112 #define READDATA(s, obj) s.read((char*)&(obj), sizeof(obj))
124 inline unsigned int GetSerializeSize(
unsigned long long a,
int,
int=0) {
return sizeof(a); }
129 template<
typename Stream>
inline void Serialize(Stream& s,
signed char a,
int,
int=0) {
WRITEDATA(s, a); }
130 template<
typename Stream>
inline void Serialize(Stream& s,
unsigned char a,
int,
int=0) {
WRITEDATA(s, a); }
131 template<
typename Stream>
inline void Serialize(Stream& s,
signed short a,
int,
int=0) {
WRITEDATA(s, a); }
132 template<
typename Stream>
inline void Serialize(Stream& s,
unsigned short a,
int,
int=0) {
WRITEDATA(s, a); }
133 template<
typename Stream>
inline void Serialize(Stream& s,
signed int a,
int,
int=0) {
WRITEDATA(s, a); }
134 template<
typename Stream>
inline void Serialize(Stream& s,
unsigned int a,
int,
int=0) {
WRITEDATA(s, a); }
135 template<
typename Stream>
inline void Serialize(Stream& s,
signed long a,
int,
int=0) {
WRITEDATA(s, a); }
136 template<
typename Stream>
inline void Serialize(Stream& s,
unsigned long a,
int,
int=0) {
WRITEDATA(s, a); }
137 template<
typename Stream>
inline void Serialize(Stream& s,
signed long long a,
int,
int=0) {
WRITEDATA(s, a); }
138 template<
typename Stream>
inline void Serialize(Stream& s,
unsigned long long a,
int,
int=0) {
WRITEDATA(s, a); }
140 template<
typename Stream>
inline void Serialize(Stream& s,
double a,
int,
int=0) {
WRITEDATA(s, a); }
143 template<
typename Stream>
inline void Unserialize(Stream& s,
signed char& a,
int,
int=0) {
READDATA(s, a); }
144 template<
typename Stream>
inline void Unserialize(Stream& s,
unsigned char& a,
int,
int=0) {
READDATA(s, a); }
145 template<
typename Stream>
inline void Unserialize(Stream& s,
signed short& a,
int,
int=0) {
READDATA(s, a); }
146 template<
typename Stream>
inline void Unserialize(Stream& s,
unsigned short& a,
int,
int=0) {
READDATA(s, a); }
147 template<
typename Stream>
inline void Unserialize(Stream& s,
signed int& a,
int,
int=0) {
READDATA(s, a); }
148 template<
typename Stream>
inline void Unserialize(Stream& s,
unsigned int& a,
int,
int=0) {
READDATA(s, a); }
149 template<
typename Stream>
inline void Unserialize(Stream& s,
signed long& a,
int,
int=0) {
READDATA(s, a); }
150 template<
typename Stream>
inline void Unserialize(Stream& s,
unsigned long& a,
int,
int=0) {
READDATA(s, a); }
151 template<
typename Stream>
inline void Unserialize(Stream& s,
signed long long& a,
int,
int=0) {
READDATA(s, a); }
152 template<
typename Stream>
inline void Unserialize(Stream& s,
unsigned long long& a,
int,
int=0) {
READDATA(s, a); }
157 template<
typename Stream>
inline void Serialize(Stream& s,
bool a,
int,
int=0) {
char f=a;
WRITEDATA(s, f); }
158 template<
typename Stream>
inline void Unserialize(Stream& s,
bool& a,
int,
int=0) {
char f;
READDATA(s, f); a=f; }
174 if (nSize < 253)
return sizeof(
unsigned char);
175 else if (nSize <= std::numeric_limits<unsigned short>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned short);
176 else if (nSize <= std::numeric_limits<unsigned int>::max())
return sizeof(
unsigned char) +
sizeof(
unsigned int);
177 else return sizeof(
unsigned char) +
sizeof(uint64_t);
180 template<
typename Stream>
185 unsigned char chSize = nSize;
188 else if (nSize <= std::numeric_limits<unsigned short>::max())
190 unsigned char chSize = 253;
191 unsigned short xSize = nSize;
195 else if (nSize <= std::numeric_limits<unsigned int>::max())
197 unsigned char chSize = 254;
198 unsigned int xSize = nSize;
204 unsigned char chSize = 255;
205 uint64_t xSize = nSize;
212 template<
typename Stream>
215 unsigned char chSize;
217 uint64_t nSizeRet = 0;
222 else if (chSize == 253)
224 unsigned short xSize;
228 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
230 else if (chSize == 254)
235 if (nSizeRet < 0x10000u)
236 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
243 if (nSizeRet < 0x100000000LLu)
244 throw std::ios_base::failure(
"non-canonical ReadCompactSize()");
246 if (nSizeRet > (uint64_t)MAX_SIZE)
247 throw std::ios_base::failure(
"ReadCompactSize() : size too large");
286 template<
typename Stream,
typename I>
289 unsigned char tmp[(
sizeof(n)*8+6)/7];
292 tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
303 template<
typename Stream,
typename I>
308 unsigned char chData;
310 n = (n << 7) | (chData & 0x7F);
318 #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
319 #define VARINT(obj) REF(WrapVarInt(REF(obj)))
320 #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
330 CFlatData(
void* pbeginIn,
void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
341 template<
typename Stream>
344 s.write(pbegin, pend - pbegin);
347 template<
typename Stream>
350 s.read(pbegin, pend - pbegin);
363 return GetSizeOfVarInt<I>(
n);
366 template<
typename Stream>
368 WriteVarInt<Stream,I>(s,
n);
371 template<
typename Stream>
373 n = ReadVarInt<Stream,I>(s);
377 template<
size_t Limit>
385 template<
typename Stream>
390 throw std::ios_base::failure(
"String length limit exceeded");
394 s.read((
char*)&
string[0], size);
397 template<
typename Stream>
402 s.write((
char*)&
string[0],
string.size());
419 template<
typename C>
unsigned int GetSerializeSize(
const std::basic_string<C>& str,
int,
int=0);
420 template<
typename Stream,
typename C>
void Serialize(Stream& os,
const std::basic_string<C>& str,
int,
int=0);
421 template<
typename Stream,
typename C>
void Unserialize(Stream& is, std::basic_string<C>& str,
int,
int=0);
424 template<
typename T,
typename A>
unsigned int GetSerializeSize_impl(
const std::vector<T, A>& v,
int nType,
int nVersion,
const boost::true_type&);
425 template<
typename T,
typename A>
unsigned int GetSerializeSize_impl(
const std::vector<T, A>& v,
int nType,
int nVersion,
const boost::false_type&);
426 template<
typename T,
typename A>
inline unsigned int GetSerializeSize(
const std::vector<T, A>& v,
int nType,
int nVersion);
427 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&);
428 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&);
429 template<
typename Stream,
typename T,
typename A>
inline void Serialize(Stream& os,
const std::vector<T, A>& v,
int nType,
int nVersion);
430 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&);
431 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&);
432 template<
typename Stream,
typename T,
typename A>
inline void Unserialize(Stream& is, std::vector<T, A>& v,
int nType,
int nVersion);
436 template<
typename Stream>
void Serialize(Stream& os,
const CScript& v,
int nType,
int nVersion);
437 template<
typename Stream>
void Unserialize(Stream& is,
CScript& v,
int nType,
int nVersion);
440 template<
typename K,
typename T>
unsigned int GetSerializeSize(
const std::pair<K, T>& item,
int nType,
int nVersion);
441 template<
typename Stream,
typename K,
typename T>
void Serialize(Stream& os,
const std::pair<K, T>& item,
int nType,
int nVersion);
442 template<
typename Stream,
typename K,
typename T>
void Unserialize(Stream& is, std::pair<K, T>& item,
int nType,
int nVersion);
445 template<
typename T0,
typename T1,
typename T2>
unsigned int GetSerializeSize(
const boost::tuple<T0, T1, T2>& item,
int nType,
int nVersion);
446 template<
typename Stream,
typename T0,
typename T1,
typename T2>
void Serialize(Stream& os,
const boost::tuple<T0, T1, T2>& item,
int nType,
int nVersion);
447 template<
typename Stream,
typename T0,
typename T1,
typename T2>
void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item,
int nType,
int nVersion);
450 template<
typename T0,
typename T1,
typename T2,
typename T3>
unsigned int GetSerializeSize(
const boost::tuple<T0, T1, T2, T3>& item,
int nType,
int nVersion);
451 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);
452 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);
455 template<
typename K,
typename T,
typename Pred,
typename A>
unsigned int GetSerializeSize(
const std::map<K, T, Pred, A>& m,
int nType,
int nVersion);
456 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);
457 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);
460 template<
typename K,
typename Pred,
typename A>
unsigned int GetSerializeSize(
const std::set<K, Pred, A>& m,
int nType,
int nVersion);
461 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Serialize(Stream& os,
const std::set<K, Pred, A>& m,
int nType,
int nVersion);
462 template<
typename Stream,
typename K,
typename Pred,
typename A>
void Unserialize(Stream& is, std::set<K, Pred, A>& m,
int nType,
int nVersion);
477 return a.GetSerializeSize((
int)nType, nVersion);
480 template<
typename Stream,
typename T>
481 inline void Serialize(Stream& os,
const T& a,
long nType,
int nVersion)
483 a.Serialize(os, (
int)nType, nVersion);
486 template<
typename Stream,
typename T>
487 inline void Unserialize(Stream& is, T& a,
long nType,
int nVersion)
489 a.Unserialize(is, (
int)nType, nVersion);
505 template<
typename Stream,
typename C>
506 void Serialize(Stream& os,
const std::basic_string<C>& str,
int,
int)
510 os.write((
char*)&str[0], str.size() *
sizeof(str[0]));
513 template<
typename Stream,
typename C>
519 is.read((
char*)&str[0], nSize *
sizeof(str[0]));
527 template<
typename T,
typename A>
533 template<
typename T,
typename A>
537 for (
typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
542 template<
typename T,
typename A>
549 template<
typename Stream,
typename T,
typename A>
550 void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
int nType,
int nVersion,
const boost::true_type&)
554 os.write((
char*)&v[0], v.size() *
sizeof(T));
557 template<
typename Stream,
typename T,
typename A>
558 void Serialize_impl(Stream& os,
const std::vector<T, A>& v,
int nType,
int nVersion,
const boost::false_type&)
561 for (
typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
565 template<
typename Stream,
typename T,
typename A>
566 inline void Serialize(Stream& os,
const std::vector<T, A>& v,
int nType,
int nVersion)
568 Serialize_impl(os, v, nType, nVersion, boost::is_fundamental<T>());
572 template<
typename Stream,
typename T,
typename A>
573 void Unserialize_impl(Stream& is, std::vector<T, A>& v,
int nType,
int nVersion,
const boost::true_type&)
581 unsigned int blk = std::min(nSize - i, (
unsigned int)(1 + 4999999 /
sizeof(T)));
583 is.read((
char*)&v[i], blk *
sizeof(T));
588 template<
typename Stream,
typename T,
typename A>
589 void Unserialize_impl(Stream& is, std::vector<T, A>& v,
int nType,
int nVersion,
const boost::false_type&)
594 unsigned int nMid = 0;
597 nMid += 5000000 /
sizeof(T);
601 for (; i < nMid; i++)
606 template<
typename Stream,
typename T,
typename A>
607 inline void Unserialize(Stream& is, std::vector<T, A>& v,
int nType,
int nVersion)
619 return GetSerializeSize((
const std::vector<unsigned char>&)v, nType, nVersion);
622 template<
typename Stream>
625 Serialize(os, (
const std::vector<unsigned char>&)v, nType, nVersion);
628 template<
typename Stream>
631 Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
639 template<
typename K,
typename T>
645 template<
typename Stream,
typename K,
typename T>
646 void Serialize(Stream& os,
const std::pair<K, T>& item,
int nType,
int nVersion)
648 Serialize(os, item.first, nType, nVersion);
649 Serialize(os, item.second, nType, nVersion);
652 template<
typename Stream,
typename K,
typename T>
653 void Unserialize(Stream& is, std::pair<K, T>& item,
int nType,
int nVersion)
664 template<
typename T0,
typename T1,
typename T2>
665 unsigned int GetSerializeSize(
const boost::tuple<T0, T1, T2>& item,
int nType,
int nVersion)
667 unsigned int nSize = 0;
674 template<
typename Stream,
typename T0,
typename T1,
typename T2>
675 void Serialize(Stream& os,
const boost::tuple<T0, T1, T2>& item,
int nType,
int nVersion)
677 Serialize(os, boost::get<0>(item), nType, nVersion);
678 Serialize(os, boost::get<1>(item), nType, nVersion);
679 Serialize(os, boost::get<2>(item), nType, nVersion);
682 template<
typename Stream,
typename T0,
typename T1,
typename T2>
683 void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item,
int nType,
int nVersion)
685 Unserialize(is, boost::get<0>(item), nType, nVersion);
686 Unserialize(is, boost::get<1>(item), nType, nVersion);
687 Unserialize(is, boost::get<2>(item), nType, nVersion);
695 template<
typename T0,
typename T1,
typename T2,
typename T3>
696 unsigned int GetSerializeSize(
const boost::tuple<T0, T1, T2, T3>& item,
int nType,
int nVersion)
698 unsigned int nSize = 0;
706 template<
typename Stream,
typename T0,
typename T1,
typename T2,
typename T3>
707 void Serialize(Stream& os,
const boost::tuple<T0, T1, T2, T3>& item,
int nType,
int nVersion)
709 Serialize(os, boost::get<0>(item), nType, nVersion);
710 Serialize(os, boost::get<1>(item), nType, nVersion);
711 Serialize(os, boost::get<2>(item), nType, nVersion);
712 Serialize(os, boost::get<3>(item), nType, nVersion);
715 template<
typename Stream,
typename T0,
typename T1,
typename T2,
typename T3>
716 void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item,
int nType,
int nVersion)
718 Unserialize(is, boost::get<0>(item), nType, nVersion);
719 Unserialize(is, boost::get<1>(item), nType, nVersion);
720 Unserialize(is, boost::get<2>(item), nType, nVersion);
721 Unserialize(is, boost::get<3>(item), nType, nVersion);
729 template<
typename K,
typename T,
typename Pred,
typename A>
733 for (
typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
738 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
739 void Serialize(Stream& os,
const std::map<K, T, Pred, A>& m,
int nType,
int nVersion)
742 for (
typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
746 template<
typename Stream,
typename K,
typename T,
typename Pred,
typename A>
747 void Unserialize(Stream& is, std::map<K, T, Pred, A>& m,
int nType,
int nVersion)
751 typename std::map<K, T, Pred, A>::iterator mi = m.begin();
752 for (
unsigned int i = 0; i < nSize; i++)
754 std::pair<K, T> item;
756 mi = m.insert(mi, item);
765 template<
typename K,
typename Pred,
typename A>
769 for (
typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
774 template<
typename Stream,
typename K,
typename Pred,
typename A>
775 void Serialize(Stream& os,
const std::set<K, Pred, A>& m,
int nType,
int nVersion)
778 for (
typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
782 template<
typename Stream,
typename K,
typename Pred,
typename A>
783 void Unserialize(Stream& is, std::set<K, Pred, A>& m,
int nType,
int nVersion)
787 typename std::set<K, Pred, A>::iterator it = m.begin();
788 for (
unsigned int i = 0; i < nSize; i++)
792 it = m.insert(it, key);
805 template<
typename Stream,
typename T>
811 template<
typename Stream,
typename T>
818 template<
typename Stream,
typename T>
872 Init(nTypeIn, nVersionIn);
875 CDataStream(const_iterator pbegin, const_iterator pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
877 Init(nTypeIn, nVersionIn);
880 #if !defined(_MSC_VER) || _MSC_VER >= 1300
881 CDataStream(
const char* pbegin,
const char* pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
883 Init(nTypeIn, nVersionIn);
889 Init(nTypeIn, nVersionIn);
892 CDataStream(
const std::vector<char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.
begin(), vchIn.
end())
894 Init(nTypeIn, nVersionIn);
897 CDataStream(
const std::vector<unsigned char>& vchIn,
int nTypeIn,
int nVersionIn) : vch((char*)&vchIn.
begin()[0], (char*)&vchIn.
end()[0])
899 Init(nTypeIn, nVersionIn);
902 void Init(
int nTypeIn,
int nVersionIn)
906 nVersion = nVersionIn;
908 exceptmask = std::ios::badbit | std::ios::failbit;
926 return (std::string(
begin(),
end()));
935 const_iterator
end()
const {
return vch.end(); }
936 iterator
end() {
return vch.end(); }
939 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
940 void reserve(size_type n) { vch.reserve(n + nReadPos); }
943 void clear() { vch.clear(); nReadPos = 0; }
944 iterator
insert(iterator it,
const char& x=
char()) {
return vch.insert(it, x); }
945 void insert(iterator it, size_type n,
const char& x) { vch.insert(it, n, x); }
947 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
949 assert(last - first >= 0);
950 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <=
nReadPos)
953 nReadPos -= (last - first);
954 memcpy(&vch[nReadPos], &first[0], last - first);
957 vch.insert(it, first, last);
960 #if !defined(_MSC_VER) || _MSC_VER >= 1300
961 void insert(iterator it,
const char* first,
const char* last)
963 assert(last - first >= 0);
964 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <=
nReadPos)
967 nReadPos -= (last - first);
968 memcpy(&vch[nReadPos], &first[0], last - first);
971 vch.insert(it, first, last);
980 if (++nReadPos >= vch.size())
984 return vch.erase(vch.begin(), vch.end());
989 return vch.erase(it);
992 iterator
erase(iterator first, iterator last)
994 if (first == vch.begin() +
nReadPos)
997 if (last == vch.end())
1000 return vch.erase(vch.begin(), vch.end());
1004 nReadPos = (last - vch.begin());
1009 return vch.erase(first, last);
1014 vch.erase(vch.begin(), vch.begin() +
nReadPos);
1034 if (state & exceptmask)
1035 throw std::ios_base::failure(psz);
1039 bool fail()
const {
return state & (std::ios::badbit | std::ios::failbit); }
1040 bool good()
const {
return !
eof() && (state == 0); }
1058 unsigned int nReadPosNext = nReadPos + nSize;
1059 if (nReadPosNext >= vch.size())
1061 if (nReadPosNext > vch.size())
1063 setstate(std::ios::failbit,
"CDataStream::read() : end of data");
1064 memset(pch, 0, nSize);
1067 memcpy(pch, &vch[nReadPos], nSize);
1072 memcpy(pch, &vch[nReadPos], nSize);
1073 nReadPos = nReadPosNext;
1081 unsigned int nReadPosNext = nReadPos + nSize;
1082 if (nReadPosNext >= vch.size())
1084 if (nReadPosNext > vch.size())
1085 setstate(std::ios::failbit,
"CDataStream::ignore() : end of data");
1090 nReadPos = nReadPosNext;
1098 vch.insert(vch.end(), pch, pch + nSize);
1102 template<
typename Stream>
1107 s.write((
char*)&vch[0], vch.size() *
sizeof(vch[0]));
1110 template<
typename T>
1117 template<
typename T>
1125 template<
typename T>
1134 data.insert(data.end(),
begin(),
end());
1168 nVersion = nVersionIn;
1170 exceptmask = std::ios::badbit | std::ios::failbit;
1180 if (file != NULL && file != stdin && file != stdout && file != stderr)
1200 if (state & exceptmask)
1201 throw std::ios_base::failure(psz);
1204 bool fail()
const {
return state & (std::ios::badbit | std::ios::failbit); }
1205 bool good()
const {
return state == 0; }
1220 throw std::ios_base::failure(
"CAutoFile::read : file handle is NULL");
1221 if (fread(pch, 1, nSize, file) != nSize)
1222 setstate(std::ios::failbit, feof(file) ?
"CAutoFile::read : end of file" :
"CAutoFile::read : fread failed");
1229 throw std::ios_base::failure(
"CAutoFile::write : file handle is NULL");
1230 if (fwrite(pch, 1, nSize, file) != nSize)
1231 setstate(std::ios::failbit,
"CAutoFile::write : write failed");
1235 template<
typename T>
1242 template<
typename T>
1247 throw std::ios_base::failure(
"CAutoFile::operator<< : file handle is NULL");
1252 template<
typename T>
1257 throw std::ios_base::failure(
"CAutoFile::operator>> : file handle is NULL");
1282 if (state & exceptmask)
1283 throw std::ios_base::failure(psz);
1288 unsigned int pos = nSrcPos % vchBuf.size();
1289 unsigned int readNow = vchBuf.size() - pos;
1290 unsigned int nAvail = vchBuf.size() - (nSrcPos -
nReadPos) - nRewind;
1291 if (nAvail < readNow)
1295 size_t read = fread((
void*)&vchBuf[pos], 1, readNow, src);
1297 setstate(std::ios_base::failbit, feof(src) ?
"CBufferedFile::Fill : end of file" :
"CBufferedFile::Fill : fread failed");
1309 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn) :
1310 src(fileIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0),
1311 state(0), exceptmask(
std::ios_base::badbit |
std::ios_base::failbit), nType(nTypeIn), nVersion(nVersionIn) {
1321 return nReadPos == nSrcPos && feof(src);
1326 if (nSize + nReadPos > nReadLimit)
1327 throw std::ios_base::failure(
"Read attempted past buffer limit");
1328 if (nSize + nRewind > vchBuf.size())
1329 throw std::ios_base::failure(
"Read larger than buffer size");
1331 if (nReadPos == nSrcPos)
1333 unsigned int pos = nReadPos % vchBuf.size();
1334 size_t nNow = nSize;
1335 if (nNow + pos > vchBuf.size())
1336 nNow = vchBuf.size() - pos;
1337 if (nNow + nReadPos > nSrcPos)
1339 memcpy(pch, &vchBuf[pos], nNow);
1355 if (nReadPos + nRewind < nSrcPos) {
1358 }
else if (nReadPos > nSrcPos) {
1367 long nLongPos = nPos;
1368 if (nPos != (uint64_t)nLongPos)
1370 if (fseek(src, nLongPos, SEEK_SET))
1372 nLongPos = ftell(src);
1374 nReadPos = nLongPos;
1382 if (nPos < nReadPos)
1388 template<
typename T>
1398 if (nReadPos == nSrcPos)
1400 if (vchBuf[nReadPos % vchBuf.size()] == ch)
void setstate(short bits, const char *psz)
void Serialize(Stream &s, int nType, int nVersion) const
CAutoFile & read(char *pch, size_t nSize)
void setstate(short bits, const char *psz)
void Init(int nTypeIn, int nVersionIn)
CSerializeData vector_type
vector_type::iterator iterator
LimitedString(std::string &string)
unsigned int SerReadWrite(Stream &s, const T &obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
vector_type::allocator_type allocator_type
const_iterator begin() const
void GetAndClear(CSerializeData &data)
CAutoFile & operator>>(T &obj)
uint64_t ReadCompactSize(Stream &is)
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
CDataStream & ignore(int nSize)
CDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
vector_type::size_type size_type
void resize(size_type n, value_type c=0)
void Serialize(Stream &s, char a, int, int=0)
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
unsigned int GetSizeOfCompactSize(uint64_t nSize)
CDataStream & read(char *pch, int nSize)
vector_type::reference reference
vector_type::value_type value_type
CBufferedFile & read(char *pch, size_t nSize)
short exceptions(short mask)
Double ended buffer combining vector and stream-like interfaces.
unsigned int GetSerializeSize(const T &obj)
vector_type::reverse_iterator reverse_iterator
iterator erase(iterator it)
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
friend CDataStream operator+(const CDataStream &a, const CDataStream &b)
#define WRITEDATA(s, obj)
void WriteVarInt(Stream &os, I n)
CDataStream(int nTypeIn, int nVersionIn)
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
vector_type::const_reference const_reference
bool SetLimit(uint64_t nPos=(uint64_t)(-1))
void setstate(short bits, const char *psz)
unsigned int GetSerializeSize(int, int) const
const char * begin() const
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
vector_type::const_iterator const_iterator
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
unsigned int GetSerializeSize(char a, int, int=0)
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
void Unserialize(Stream &s, char &a, int, int=0)
CDataStream & operator+=(const CDataStream &b)
std::vector< char, zero_after_free_allocator< char > > CSerializeData
CDataStream & operator>>(T &obj)
void Serialize(Stream &s, int, int=0) const
void Unserialize(Stream &s, int, int=0)
void Serialize(Stream &s, int, int) const
reference operator[](size_type pos)
bool SetPos(uint64_t nPos)
unsigned int GetSizeOfVarInt(I n)
unsigned int GetSerializeSize(int, int=0) const
unsigned int GetSerializeSize(int, int=0) const
void Unserialize(Stream &s, int, int=0)
void Unserialize(Stream &s, int, int)
CFlatData(void *pbeginIn, void *pendIn)
CAutoFile & write(const char *pch, size_t nSize)
void Unserialize_impl(Stream &is, std::vector< T, A > &v, int nType, int nVersion, const boost::true_type &)
FILE * operator=(FILE *pnew)
iterator insert(iterator it, const char &x=char())
CDataStream & write(const char *pch, int nSize)
CDataStream & operator<<(const T &obj)
void reserve(size_type n)
Serialized script, used inside transaction inputs and outputs.
void * memcpy(void *a, const void *b, size_t c)
void insert(iterator it, size_type n, const char &x)
short exceptions(short mask)
vector_type::difference_type difference_type
const_reference operator[](size_type pos) const
unsigned int GetSerializeSize(const T &obj)
void insert(iterator it, const char *first, const char *last)
std::vector< char > vchBuf
iterator erase(iterator first, iterator last)
void Serialize(Stream &s, int, int=0) const
void Serialize_impl(Stream &os, const std::vector< T, A > &v, int nType, int nVersion, const boost::true_type &)
Wrapper around a FILE* that implements a ring buffer to deserialize from.
void WriteCompactSize(Stream &os, uint64_t nSize)
CBufferedFile & operator>>(T &obj)
CAutoFile & operator<<(const T &obj)
CVarInt< I > WrapVarInt(I &n)
Wrapper for serializing arrays and POD.
unsigned int GetSerializeSize_impl(const std::vector< T, A > &v, int nType, int nVersion, const boost::true_type &)
const_iterator end() const