serialize.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_SERIALIZE_H
  3. #define ECOIN_SERIALIZE_H
  4. #include <string>
  5. #include <vector>
  6. #include <map>
  7. #include <set>
  8. #include <cassert>
  9. #include <limits>
  10. #include <cstring>
  11. #include <cstdio>
  12. #include <boost/type_traits/is_fundamental.hpp>
  13. #include <boost/tuple/tuple.hpp>
  14. #include <boost/tuple/tuple_comparison.hpp>
  15. #include <boost/tuple/tuple_io.hpp>
  16. #include "allocators.h"
  17. #include "version.h"
  18. typedef long long int64;
  19. typedef unsigned long long uint64;
  20. class CScript;
  21. class CDataStream;
  22. class CAutoFile;
  23. static const unsigned int MAX_SIZE = 0x02000000;
  24. // Used to bypass the rule against non-const reference to temporary
  25. // where it makes sense with wrappers such as CFlatData or CTxDB
  26. template<typename T>
  27. inline T& REF(const T& val)
  28. {
  29. return const_cast<T&>(val);
  30. }
  31. enum
  32. {
  33. // primary actions
  34. SER_NETWORK = (1 << 0),
  35. SER_DISK = (1 << 1),
  36. SER_GETHASH = (1 << 2),
  37. // modifiers
  38. SER_SKIPSIG = (1 << 16),
  39. SER_BLOCKHEADERONLY = (1 << 17),
  40. };
  41. #define IMPLEMENT_SERIALIZE(statements) \
  42. unsigned int GetSerializeSize(int nType, int nVersion) const \
  43. { \
  44. CSerActionGetSerializeSize ser_action; \
  45. const bool fGetSize = true; \
  46. const bool fWrite = false; \
  47. const bool fRead = false; \
  48. unsigned int nSerSize = 0; \
  49. ser_streamplaceholder s; \
  50. assert(fGetSize||fWrite||fRead); /* suppress warning */ \
  51. s.nType = nType; \
  52. s.nVersion = nVersion; \
  53. {statements} \
  54. return nSerSize; \
  55. } \
  56. template<typename Stream> \
  57. void Serialize(Stream& s, int nType, int nVersion) const \
  58. { \
  59. CSerActionSerialize ser_action; \
  60. const bool fGetSize = false; \
  61. const bool fWrite = true; \
  62. const bool fRead = false; \
  63. unsigned int nSerSize = 0; \
  64. assert(fGetSize||fWrite||fRead); /* suppress warning */ \
  65. {statements} \
  66. } \
  67. template<typename Stream> \
  68. void Unserialize(Stream& s, int nType, int nVersion) \
  69. { \
  70. CSerActionUnserialize ser_action; \
  71. const bool fGetSize = false; \
  72. const bool fWrite = false; \
  73. const bool fRead = true; \
  74. unsigned int nSerSize = 0; \
  75. assert(fGetSize||fWrite||fRead); /* suppress warning */ \
  76. {statements} \
  77. }
  78. #define READWRITE(obj) (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))
  79. // Basic types
  80. #define WRITEDATA(s, obj) s.write((char*)&(obj), sizeof(obj))
  81. #define READDATA(s, obj) s.read((char*)&(obj), sizeof(obj))
  82. inline unsigned int GetSerializeSize(char a, int, int=0) { return sizeof(a); }
  83. inline unsigned int GetSerializeSize(signed char a, int, int=0) { return sizeof(a); }
  84. inline unsigned int GetSerializeSize(unsigned char a, int, int=0) { return sizeof(a); }
  85. inline unsigned int GetSerializeSize(signed short a, int, int=0) { return sizeof(a); }
  86. inline unsigned int GetSerializeSize(unsigned short a, int, int=0) { return sizeof(a); }
  87. inline unsigned int GetSerializeSize(signed int a, int, int=0) { return sizeof(a); }
  88. inline unsigned int GetSerializeSize(unsigned int a, int, int=0) { return sizeof(a); }
  89. inline unsigned int GetSerializeSize(signed long a, int, int=0) { return sizeof(a); }
  90. inline unsigned int GetSerializeSize(unsigned long a, int, int=0) { return sizeof(a); }
  91. inline unsigned int GetSerializeSize(int64 a, int, int=0) { return sizeof(a); }
  92. inline unsigned int GetSerializeSize(uint64 a, int, int=0) { return sizeof(a); }
  93. inline unsigned int GetSerializeSize(float a, int, int=0) { return sizeof(a); }
  94. inline unsigned int GetSerializeSize(double a, int, int=0) { return sizeof(a); }
  95. template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { WRITEDATA(s, a); }
  96. template<typename Stream> inline void Serialize(Stream& s, signed char a, int, int=0) { WRITEDATA(s, a); }
  97. template<typename Stream> inline void Serialize(Stream& s, unsigned char a, int, int=0) { WRITEDATA(s, a); }
  98. template<typename Stream> inline void Serialize(Stream& s, signed short a, int, int=0) { WRITEDATA(s, a); }
  99. template<typename Stream> inline void Serialize(Stream& s, unsigned short a, int, int=0) { WRITEDATA(s, a); }
  100. template<typename Stream> inline void Serialize(Stream& s, signed int a, int, int=0) { WRITEDATA(s, a); }
  101. template<typename Stream> inline void Serialize(Stream& s, unsigned int a, int, int=0) { WRITEDATA(s, a); }
  102. template<typename Stream> inline void Serialize(Stream& s, signed long a, int, int=0) { WRITEDATA(s, a); }
  103. template<typename Stream> inline void Serialize(Stream& s, unsigned long a, int, int=0) { WRITEDATA(s, a); }
  104. template<typename Stream> inline void Serialize(Stream& s, int64 a, int, int=0) { WRITEDATA(s, a); }
  105. template<typename Stream> inline void Serialize(Stream& s, uint64 a, int, int=0) { WRITEDATA(s, a); }
  106. template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { WRITEDATA(s, a); }
  107. template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { WRITEDATA(s, a); }
  108. template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { READDATA(s, a); }
  109. template<typename Stream> inline void Unserialize(Stream& s, signed char& a, int, int=0) { READDATA(s, a); }
  110. template<typename Stream> inline void Unserialize(Stream& s, unsigned char& a, int, int=0) { READDATA(s, a); }
  111. template<typename Stream> inline void Unserialize(Stream& s, signed short& a, int, int=0) { READDATA(s, a); }
  112. template<typename Stream> inline void Unserialize(Stream& s, unsigned short& a, int, int=0) { READDATA(s, a); }
  113. template<typename Stream> inline void Unserialize(Stream& s, signed int& a, int, int=0) { READDATA(s, a); }
  114. template<typename Stream> inline void Unserialize(Stream& s, unsigned int& a, int, int=0) { READDATA(s, a); }
  115. template<typename Stream> inline void Unserialize(Stream& s, signed long& a, int, int=0) { READDATA(s, a); }
  116. template<typename Stream> inline void Unserialize(Stream& s, unsigned long& a, int, int=0) { READDATA(s, a); }
  117. template<typename Stream> inline void Unserialize(Stream& s, int64& a, int, int=0) { READDATA(s, a); }
  118. template<typename Stream> inline void Unserialize(Stream& s, uint64& a, int, int=0) { READDATA(s, a); }
  119. template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { READDATA(s, a); }
  120. template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { READDATA(s, a); }
  121. inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
  122. template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; WRITEDATA(s, f); }
  123. template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f; READDATA(s, f); a=f; }
  124. #ifndef THROW_WITH_STACKTRACE
  125. #define THROW_WITH_STACKTRACE(exception) \
  126. { \
  127. LogStackTrace(); \
  128. throw (exception); \
  129. }
  130. void LogStackTrace();
  131. #endif
  132. // Compact size
  133. // size < 253 -- 1 byte
  134. // size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
  135. // size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
  136. // size > UINT_MAX -- 9 bytes (255 + 8 bytes)
  137. inline unsigned int GetSizeOfCompactSize(uint64 nSize)
  138. {
  139. if (nSize < 253) return sizeof(unsigned char);
  140. else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
  141. else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
  142. else return sizeof(unsigned char) + sizeof(uint64);
  143. }
  144. template<typename Stream>
  145. void WriteCompactSize(Stream& os, uint64 nSize)
  146. {
  147. if (nSize < 253)
  148. {
  149. unsigned char chSize = nSize;
  150. WRITEDATA(os, chSize);
  151. }
  152. else if (nSize <= std::numeric_limits<unsigned short>::max())
  153. {
  154. unsigned char chSize = 253;
  155. unsigned short xSize = nSize;
  156. WRITEDATA(os, chSize);
  157. WRITEDATA(os, xSize);
  158. }
  159. else if (nSize <= std::numeric_limits<unsigned int>::max())
  160. {
  161. unsigned char chSize = 254;
  162. unsigned int xSize = nSize;
  163. WRITEDATA(os, chSize);
  164. WRITEDATA(os, xSize);
  165. }
  166. else
  167. {
  168. unsigned char chSize = 255;
  169. uint64 xSize = nSize;
  170. WRITEDATA(os, chSize);
  171. WRITEDATA(os, xSize);
  172. }
  173. return;
  174. }
  175. template<typename Stream>
  176. uint64 ReadCompactSize(Stream& is)
  177. {
  178. unsigned char chSize;
  179. READDATA(is, chSize);
  180. uint64 nSizeRet = 0;
  181. if (chSize < 253)
  182. {
  183. nSizeRet = chSize;
  184. }
  185. else if (chSize == 253)
  186. {
  187. unsigned short xSize;
  188. READDATA(is, xSize);
  189. nSizeRet = xSize;
  190. }
  191. else if (chSize == 254)
  192. {
  193. unsigned int xSize;
  194. READDATA(is, xSize);
  195. nSizeRet = xSize;
  196. }
  197. else
  198. {
  199. uint64 xSize;
  200. READDATA(is, xSize);
  201. nSizeRet = xSize;
  202. }
  203. if (nSizeRet > (uint64)MAX_SIZE)
  204. THROW_WITH_STACKTRACE(std::ios_base::failure("ReadCompactSize() : size too large"));
  205. return nSizeRet;
  206. }
  207. #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
  208. class CFlatData
  209. {
  210. protected:
  211. char* pbegin;
  212. char* pend;
  213. public:
  214. CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
  215. char* begin() { return pbegin; }
  216. const char* begin() const { return pbegin; }
  217. char* end() { return pend; }
  218. const char* end() const { return pend; }
  219. unsigned int GetSerializeSize(int, int=0) const
  220. {
  221. return pend - pbegin;
  222. }
  223. template<typename Stream>
  224. void Serialize(Stream& s, int, int=0) const
  225. {
  226. s.write(pbegin, pend - pbegin);
  227. }
  228. template<typename Stream>
  229. void Unserialize(Stream& s, int, int=0)
  230. {
  231. s.read(pbegin, pend - pbegin);
  232. }
  233. };
  234. // Forward declarations
  235. // string
  236. template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
  237. template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
  238. template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
  239. // vector
  240. template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
  241. template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
  242. template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
  243. 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&);
  244. 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&);
  245. template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
  246. 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&);
  247. 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&);
  248. template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
  249. // others derived from vector
  250. extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion);
  251. template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion);
  252. template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion);
  253. // pair
  254. template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
  255. template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
  256. template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
  257. // 3 tuple
  258. template<typename T0, typename T1, typename T2> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion);
  259. template<typename Stream, typename T0, typename T1, typename T2> void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion);
  260. template<typename Stream, typename T0, typename T1, typename T2> void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion);
  261. // 4 tuple
  262. template<typename T0, typename T1, typename T2, typename T3> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion);
  263. 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);
  264. 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);
  265. // map
  266. template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
  267. 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);
  268. 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);
  269. // set
  270. template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
  271. template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
  272. template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
  273. template<typename T>
  274. inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
  275. {
  276. return a.GetSerializeSize((int)nType, nVersion);
  277. }
  278. template<typename Stream, typename T>
  279. inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
  280. {
  281. a.Serialize(os, (int)nType, nVersion);
  282. }
  283. template<typename Stream, typename T>
  284. inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
  285. {
  286. a.Unserialize(is, (int)nType, nVersion);
  287. }
  288. // string
  289. template<typename C>
  290. unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
  291. {
  292. return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
  293. }
  294. template<typename Stream, typename C>
  295. void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
  296. {
  297. WriteCompactSize(os, str.size());
  298. if (!str.empty())
  299. os.write((char*)&str[0], str.size() * sizeof(str[0]));
  300. }
  301. template<typename Stream, typename C>
  302. void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
  303. {
  304. unsigned int nSize = ReadCompactSize(is);
  305. str.resize(nSize);
  306. if (nSize != 0)
  307. is.read((char*)&str[0], nSize * sizeof(str[0]));
  308. }
  309. // vector
  310. template<typename T, typename A>
  311. unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
  312. {
  313. return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
  314. }
  315. template<typename T, typename A>
  316. unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
  317. {
  318. unsigned int nSize = GetSizeOfCompactSize(v.size());
  319. for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
  320. nSize += GetSerializeSize((*vi), nType, nVersion);
  321. return nSize;
  322. }
  323. template<typename T, typename A>
  324. inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
  325. {
  326. return GetSerializeSize_impl(v, nType, nVersion, boost::is_fundamental<T>());
  327. }
  328. template<typename Stream, typename T, typename A>
  329. void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
  330. {
  331. WriteCompactSize(os, v.size());
  332. if (!v.empty())
  333. os.write((char*)&v[0], v.size() * sizeof(T));
  334. }
  335. template<typename Stream, typename T, typename A>
  336. void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
  337. {
  338. WriteCompactSize(os, v.size());
  339. for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
  340. ::Serialize(os, (*vi), nType, nVersion);
  341. }
  342. template<typename Stream, typename T, typename A>
  343. inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
  344. {
  345. Serialize_impl(os, v, nType, nVersion, boost::is_fundamental<T>());
  346. }
  347. template<typename Stream, typename T, typename A>
  348. void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
  349. {
  350. // Limit size per read so bogus size value won't cause out of memory
  351. v.clear();
  352. unsigned int nSize = ReadCompactSize(is);
  353. unsigned int i = 0;
  354. while (i < nSize)
  355. {
  356. unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
  357. v.resize(i + blk);
  358. is.read((char*)&v[i], blk * sizeof(T));
  359. i += blk;
  360. }
  361. }
  362. template<typename Stream, typename T, typename A>
  363. void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
  364. {
  365. v.clear();
  366. unsigned int nSize = ReadCompactSize(is);
  367. unsigned int i = 0;
  368. unsigned int nMid = 0;
  369. while (nMid < nSize)
  370. {
  371. nMid += 5000000 / sizeof(T);
  372. if (nMid > nSize)
  373. nMid = nSize;
  374. v.resize(nMid);
  375. for (; i < nMid; i++)
  376. Unserialize(is, v[i], nType, nVersion);
  377. }
  378. }
  379. template<typename Stream, typename T, typename A>
  380. inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
  381. {
  382. Unserialize_impl(is, v, nType, nVersion, boost::is_fundamental<T>());
  383. }
  384. // others derived from vector
  385. inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
  386. {
  387. return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
  388. }
  389. template<typename Stream>
  390. void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
  391. {
  392. Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
  393. }
  394. template<typename Stream>
  395. void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
  396. {
  397. Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
  398. }
  399. // pair
  400. template<typename K, typename T>
  401. unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
  402. {
  403. return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
  404. }
  405. template<typename Stream, typename K, typename T>
  406. void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
  407. {
  408. Serialize(os, item.first, nType, nVersion);
  409. Serialize(os, item.second, nType, nVersion);
  410. }
  411. template<typename Stream, typename K, typename T>
  412. void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
  413. {
  414. Unserialize(is, item.first, nType, nVersion);
  415. Unserialize(is, item.second, nType, nVersion);
  416. }
  417. // 3 tuple
  418. template<typename T0, typename T1, typename T2>
  419. unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
  420. {
  421. unsigned int nSize = 0;
  422. nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
  423. nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
  424. nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
  425. return nSize;
  426. }
  427. template<typename Stream, typename T0, typename T1, typename T2>
  428. void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
  429. {
  430. Serialize(os, boost::get<0>(item), nType, nVersion);
  431. Serialize(os, boost::get<1>(item), nType, nVersion);
  432. Serialize(os, boost::get<2>(item), nType, nVersion);
  433. }
  434. template<typename Stream, typename T0, typename T1, typename T2>
  435. void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
  436. {
  437. Unserialize(is, boost::get<0>(item), nType, nVersion);
  438. Unserialize(is, boost::get<1>(item), nType, nVersion);
  439. Unserialize(is, boost::get<2>(item), nType, nVersion);
  440. }
  441. // 4 tuple
  442. template<typename T0, typename T1, typename T2, typename T3>
  443. unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
  444. {
  445. unsigned int nSize = 0;
  446. nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
  447. nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
  448. nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
  449. nSize += GetSerializeSize(boost::get<3>(item), nType, nVersion);
  450. return nSize;
  451. }
  452. template<typename Stream, typename T0, typename T1, typename T2, typename T3>
  453. void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
  454. {
  455. Serialize(os, boost::get<0>(item), nType, nVersion);
  456. Serialize(os, boost::get<1>(item), nType, nVersion);
  457. Serialize(os, boost::get<2>(item), nType, nVersion);
  458. Serialize(os, boost::get<3>(item), nType, nVersion);
  459. }
  460. template<typename Stream, typename T0, typename T1, typename T2, typename T3>
  461. void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
  462. {
  463. Unserialize(is, boost::get<0>(item), nType, nVersion);
  464. Unserialize(is, boost::get<1>(item), nType, nVersion);
  465. Unserialize(is, boost::get<2>(item), nType, nVersion);
  466. Unserialize(is, boost::get<3>(item), nType, nVersion);
  467. }
  468. // map
  469. template<typename K, typename T, typename Pred, typename A>
  470. unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
  471. {
  472. unsigned int nSize = GetSizeOfCompactSize(m.size());
  473. for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
  474. nSize += GetSerializeSize((*mi), nType, nVersion);
  475. return nSize;
  476. }
  477. template<typename Stream, typename K, typename T, typename Pred, typename A>
  478. void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
  479. {
  480. WriteCompactSize(os, m.size());
  481. for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
  482. Serialize(os, (*mi), nType, nVersion);
  483. }
  484. template<typename Stream, typename K, typename T, typename Pred, typename A>
  485. void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
  486. {
  487. m.clear();
  488. unsigned int nSize = ReadCompactSize(is);
  489. typename std::map<K, T, Pred, A>::iterator mi = m.begin();
  490. for (unsigned int i = 0; i < nSize; i++)
  491. {
  492. std::pair<K, T> item;
  493. Unserialize(is, item, nType, nVersion);
  494. mi = m.insert(mi, item);
  495. }
  496. }
  497. // set
  498. template<typename K, typename Pred, typename A>
  499. unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
  500. {
  501. unsigned int nSize = GetSizeOfCompactSize(m.size());
  502. for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
  503. nSize += GetSerializeSize((*it), nType, nVersion);
  504. return nSize;
  505. }
  506. template<typename Stream, typename K, typename Pred, typename A>
  507. void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
  508. {
  509. WriteCompactSize(os, m.size());
  510. for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
  511. Serialize(os, (*it), nType, nVersion);
  512. }
  513. template<typename Stream, typename K, typename Pred, typename A>
  514. void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
  515. {
  516. m.clear();
  517. unsigned int nSize = ReadCompactSize(is);
  518. typename std::set<K, Pred, A>::iterator it = m.begin();
  519. for (unsigned int i = 0; i < nSize; i++)
  520. {
  521. K key;
  522. Unserialize(is, key, nType, nVersion);
  523. it = m.insert(it, key);
  524. }
  525. }
  526. // Support for IMPLEMENT_SERIALIZE and READWRITE macro
  527. class CSerActionGetSerializeSize { };
  528. class CSerActionSerialize { };
  529. class CSerActionUnserialize { };
  530. template<typename Stream, typename T>
  531. inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
  532. {
  533. return ::GetSerializeSize(obj, nType, nVersion);
  534. }
  535. template<typename Stream, typename T>
  536. inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
  537. {
  538. ::Serialize(s, obj, nType, nVersion);
  539. return 0;
  540. }
  541. template<typename Stream, typename T>
  542. inline unsigned int SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
  543. {
  544. ::Unserialize(s, obj, nType, nVersion);
  545. return 0;
  546. }
  547. struct ser_streamplaceholder
  548. {
  549. int nType;
  550. int nVersion;
  551. };
  552. class CDataStream
  553. {
  554. protected:
  555. typedef std::vector<char, zero_after_free_allocator<char> > vector_type;
  556. vector_type vch;
  557. unsigned int nReadPos;
  558. short state;
  559. short exceptmask;
  560. public:
  561. int nType;
  562. int nVersion;
  563. typedef vector_type::allocator_type allocator_type;
  564. typedef vector_type::size_type size_type;
  565. typedef vector_type::difference_type difference_type;
  566. typedef vector_type::reference reference;
  567. typedef vector_type::const_reference const_reference;
  568. typedef vector_type::value_type value_type;
  569. typedef vector_type::iterator iterator;
  570. typedef vector_type::const_iterator const_iterator;
  571. typedef vector_type::reverse_iterator reverse_iterator;
  572. explicit CDataStream(int nTypeIn, int nVersionIn)
  573. {
  574. Init(nTypeIn, nVersionIn);
  575. }
  576. CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
  577. {
  578. Init(nTypeIn, nVersionIn);
  579. }
  580. #if !defined(_MSC_VER) || _MSC_VER >= 1300
  581. CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
  582. {
  583. Init(nTypeIn, nVersionIn);
  584. }
  585. #endif
  586. CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
  587. {
  588. Init(nTypeIn, nVersionIn);
  589. }
  590. CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
  591. {
  592. Init(nTypeIn, nVersionIn);
  593. }
  594. CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
  595. {
  596. Init(nTypeIn, nVersionIn);
  597. }
  598. void Init(int nTypeIn, int nVersionIn)
  599. {
  600. nReadPos = 0;
  601. nType = nTypeIn;
  602. nVersion = nVersionIn;
  603. state = 0;
  604. exceptmask = std::ios::badbit | std::ios::failbit;
  605. }
  606. CDataStream& operator+=(const CDataStream& b)
  607. {
  608. vch.insert(vch.end(), b.begin(), b.end());
  609. return *this;
  610. }
  611. friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
  612. {
  613. CDataStream ret = a;
  614. ret += b;
  615. return (ret);
  616. }
  617. std::string str() const
  618. {
  619. return (std::string(begin(), end()));
  620. }
  621. // Vector subset
  622. const_iterator begin() const { return vch.begin() + nReadPos; }
  623. iterator begin() { return vch.begin() + nReadPos; }
  624. const_iterator end() const { return vch.end(); }
  625. iterator end() { return vch.end(); }
  626. size_type size() const { return vch.size() - nReadPos; }
  627. bool empty() const { return vch.size() == nReadPos; }
  628. void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
  629. void reserve(size_type n) { vch.reserve(n + nReadPos); }
  630. const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
  631. reference operator[](size_type pos) { return vch[pos + nReadPos]; }
  632. void clear() { vch.clear(); nReadPos = 0; }
  633. iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
  634. void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
  635. void insert(iterator it, const_iterator first, const_iterator last)
  636. {
  637. assert(last - first >= 0);
  638. if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
  639. {
  640. // special case for inserting at the front when there's room
  641. nReadPos -= (last - first);
  642. memcpy(&vch[nReadPos], &first[0], last - first);
  643. }
  644. else
  645. vch.insert(it, first, last);
  646. }
  647. void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
  648. {
  649. assert(last - first >= 0);
  650. if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
  651. {
  652. // special case for inserting at the front when there's room
  653. nReadPos -= (last - first);
  654. memcpy(&vch[nReadPos], &first[0], last - first);
  655. }
  656. else
  657. vch.insert(it, first, last);
  658. }
  659. #if !defined(_MSC_VER) || _MSC_VER >= 1300
  660. void insert(iterator it, const char* first, const char* last)
  661. {
  662. assert(last - first >= 0);
  663. if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
  664. {
  665. // special case for inserting at the front when there's room
  666. nReadPos -= (last - first);
  667. memcpy(&vch[nReadPos], &first[0], last - first);
  668. }
  669. else
  670. vch.insert(it, first, last);
  671. }
  672. #endif
  673. iterator erase(iterator it)
  674. {
  675. if (it == vch.begin() + nReadPos)
  676. {
  677. // special case for erasing from the front
  678. if (++nReadPos >= vch.size())
  679. {
  680. // whenever we reach the end, we take the opportunity to clear the buffer
  681. nReadPos = 0;
  682. return vch.erase(vch.begin(), vch.end());
  683. }
  684. return vch.begin() + nReadPos;
  685. }
  686. else
  687. return vch.erase(it);
  688. }
  689. iterator erase(iterator first, iterator last)
  690. {
  691. if (first == vch.begin() + nReadPos)
  692. {
  693. // special case for erasing from the front
  694. if (last == vch.end())
  695. {
  696. nReadPos = 0;
  697. return vch.erase(vch.begin(), vch.end());
  698. }
  699. else
  700. {
  701. nReadPos = (last - vch.begin());
  702. return last;
  703. }
  704. }
  705. else
  706. return vch.erase(first, last);
  707. }
  708. inline void Compact()
  709. {
  710. vch.erase(vch.begin(), vch.begin() + nReadPos);
  711. nReadPos = 0;
  712. }
  713. bool Rewind(size_type n)
  714. {
  715. // Rewind by n characters if the buffer hasn't been compacted yet
  716. if (n > nReadPos)
  717. return false;
  718. nReadPos -= n;
  719. return true;
  720. }
  721. // Stream subset
  722. void setstate(short bits, const char* psz)
  723. {
  724. state |= bits;
  725. if (state & exceptmask)
  726. THROW_WITH_STACKTRACE(std::ios_base::failure(psz));
  727. }
  728. bool eof() const { return size() == 0; }
  729. bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
  730. bool good() const { return !eof() && (state == 0); }
  731. void clear(short n) { state = n; } // name conflict with vector clear()
  732. short exceptions() { return exceptmask; }
  733. short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
  734. CDataStream* rdbuf() { return this; }
  735. int in_avail() { return size(); }
  736. void SetType(int n) { nType = n; }
  737. int GetType() { return nType; }
  738. void SetVersion(int n) { nVersion = n; }
  739. int GetVersion() { return nVersion; }
  740. void ReadVersion() { *this >> nVersion; }
  741. void WriteVersion() { *this << nVersion; }
  742. CDataStream& read(char* pch, int nSize)
  743. {
  744. // Read from the beginning of the buffer
  745. assert(nSize >= 0);
  746. unsigned int nReadPosNext = nReadPos + nSize;
  747. if (nReadPosNext >= vch.size())
  748. {
  749. if (nReadPosNext > vch.size())
  750. {
  751. setstate(std::ios::failbit, "CDataStream::read() : end of data");
  752. memset(pch, 0, nSize);
  753. nSize = vch.size() - nReadPos;
  754. }
  755. memcpy(pch, &vch[nReadPos], nSize);
  756. nReadPos = 0;
  757. vch.clear();
  758. return (*this);
  759. }
  760. memcpy(pch, &vch[nReadPos], nSize);
  761. nReadPos = nReadPosNext;
  762. return (*this);
  763. }
  764. CDataStream& ignore(int nSize)
  765. {
  766. // Ignore from the beginning of the buffer
  767. assert(nSize >= 0);
  768. unsigned int nReadPosNext = nReadPos + nSize;
  769. if (nReadPosNext >= vch.size())
  770. {
  771. if (nReadPosNext > vch.size())
  772. {
  773. setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
  774. nSize = vch.size() - nReadPos;
  775. }
  776. nReadPos = 0;
  777. vch.clear();
  778. return (*this);
  779. }
  780. nReadPos = nReadPosNext;
  781. return (*this);
  782. }
  783. CDataStream& write(const char* pch, int nSize)
  784. {
  785. // Write to the end of the buffer
  786. assert(nSize >= 0);
  787. vch.insert(vch.end(), pch, pch + nSize);
  788. return (*this);
  789. }
  790. template<typename Stream>
  791. void Serialize(Stream& s, int nType, int nVersion) const
  792. {
  793. // Special case: stream << stream concatenates like stream += stream
  794. if (!vch.empty())
  795. s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
  796. }
  797. template<typename T>
  798. unsigned int GetSerializeSize(const T& obj)
  799. {
  800. // Tells the size of the object if serialized to this stream
  801. return ::GetSerializeSize(obj, nType, nVersion);
  802. }
  803. template<typename T>
  804. CDataStream& operator<<(const T& obj)
  805. {
  806. // Serialize to this stream
  807. ::Serialize(*this, obj, nType, nVersion);
  808. return (*this);
  809. }
  810. template<typename T>
  811. CDataStream& operator>>(T& obj)
  812. {
  813. // Unserialize from this stream
  814. ::Unserialize(*this, obj, nType, nVersion);
  815. return (*this);
  816. }
  817. };
  818. class CAutoFile
  819. {
  820. protected:
  821. FILE* file;
  822. short state;
  823. short exceptmask;
  824. public:
  825. int nType;
  826. int nVersion;
  827. CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
  828. {
  829. file = filenew;
  830. nType = nTypeIn;
  831. nVersion = nVersionIn;
  832. state = 0;
  833. exceptmask = std::ios::badbit | std::ios::failbit;
  834. }
  835. ~CAutoFile()
  836. {
  837. fclose();
  838. }
  839. void fclose()
  840. {
  841. if (file != NULL && file != stdin && file != stdout && file != stderr)
  842. ::fclose(file);
  843. file = NULL;
  844. }
  845. FILE* release() { FILE* ret = file; file = NULL; return ret; }
  846. operator FILE*() { return file; }
  847. FILE* operator->() { return file; }
  848. FILE& operator*() { return *file; }
  849. FILE** operator&() { return &file; }
  850. FILE* operator=(FILE* pnew) { return file = pnew; }
  851. bool operator!() { return (file == NULL); }
  852. // Stream subset
  853. void setstate(short bits, const char* psz)
  854. {
  855. state |= bits;
  856. if (state & exceptmask)
  857. THROW_WITH_STACKTRACE(std::ios_base::failure(psz));
  858. }
  859. bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
  860. bool good() const { return state == 0; }
  861. void clear(short n = 0) { state = n; }
  862. short exceptions() { return exceptmask; }
  863. short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
  864. void SetType(int n) { nType = n; }
  865. int GetType() { return nType; }
  866. void SetVersion(int n) { nVersion = n; }
  867. int GetVersion() { return nVersion; }
  868. void ReadVersion() { *this >> nVersion; }
  869. void WriteVersion() { *this << nVersion; }
  870. CAutoFile& read(char* pch, size_t nSize)
  871. {
  872. if (!file)
  873. throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
  874. if (fread(pch, 1, nSize, file) != nSize)
  875. setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
  876. return (*this);
  877. }
  878. CAutoFile& write(const char* pch, size_t nSize)
  879. {
  880. if (!file)
  881. throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
  882. if (fwrite(pch, 1, nSize, file) != nSize)
  883. setstate(std::ios::failbit, "CAutoFile::write : write failed");
  884. return (*this);
  885. }
  886. template<typename T>
  887. unsigned int GetSerializeSize(const T& obj)
  888. {
  889. // Tells the size of the object if serialized to this stream
  890. return ::GetSerializeSize(obj, nType, nVersion);
  891. }
  892. template<typename T>
  893. CAutoFile& operator<<(const T& obj)
  894. {
  895. // Serialize to this stream
  896. if (!file)
  897. throw std::ios_base::failure("CAutoFile::operator<< : file handle is NULL");
  898. ::Serialize(*this, obj, nType, nVersion);
  899. return (*this);
  900. }
  901. template<typename T>
  902. CAutoFile& operator>>(T& obj)
  903. {
  904. // Unserialize from this stream
  905. if (!file)
  906. throw std::ios_base::failure("CAutoFile::operator>> : file handle is NULL");
  907. ::Unserialize(*this, obj, nType, nVersion);
  908. return (*this);
  909. }
  910. };
  911. #endif