You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serialize.h 44KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2011 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_SERIALIZE_H
  6. #define BITCOIN_SERIALIZE_H
  7. #include <string>
  8. #include <vector>
  9. #include <map>
  10. #include <set>
  11. #include <cassert>
  12. #include <climits>
  13. #include <cstring>
  14. #include <cstdio>
  15. #include <boost/type_traits/is_fundamental.hpp>
  16. #include <boost/tuple/tuple.hpp>
  17. #include <boost/tuple/tuple_comparison.hpp>
  18. #include <boost/tuple/tuple_io.hpp>
  19. #if defined(_MSC_VER) || defined(__BORLANDC__)
  20. typedef __int64 int64;
  21. typedef unsigned __int64 uint64;
  22. #else
  23. typedef long long int64;
  24. typedef unsigned long long uint64;
  25. #endif
  26. #if defined(_MSC_VER) && _MSC_VER < 1300
  27. #define for if (false) ; else for
  28. #endif
  29. #ifdef __WXMSW__
  30. // This is used to attempt to keep keying material out of swap
  31. // Note that VirtualLock does not provide this as a guarantee on Windows,
  32. // but, in practice, memory that has been VirtualLock'd almost never gets written to
  33. // the pagefile except in rare circumstances where memory is extremely low.
  34. #include <windows.h>
  35. #define mlock(p, n) VirtualLock((p), (n));
  36. #define munlock(p, n) VirtualUnlock((p), (n));
  37. #else
  38. #include <sys/mman.h>
  39. #include <limits.h>
  40. /* This comes from limits.h if it's not defined there set a sane default */
  41. #ifndef PAGESIZE
  42. #include <unistd.h>
  43. #define PAGESIZE sysconf(_SC_PAGESIZE)
  44. #endif
  45. #define mlock(a,b) \
  46. mlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
  47. (((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
  48. #define munlock(a,b) \
  49. munlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
  50. (((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
  51. #endif
  52. class CScript;
  53. class CDataStream;
  54. class CAutoFile;
  55. static const unsigned int MAX_SIZE = 0x02000000;
  56. static const int VERSION = 40000;
  57. static const char* pszSubVer = "";
  58. static const bool VERSION_IS_BETA = true;
  59. // Used to bypass the rule against non-const reference to temporary
  60. // where it makes sense with wrappers such as CFlatData or CTxDB
  61. template<typename T>
  62. inline T& REF(const T& val)
  63. {
  64. return const_cast<T&>(val);
  65. }
  66. /////////////////////////////////////////////////////////////////
  67. //
  68. // Templates for serializing to anything that looks like a stream,
  69. // i.e. anything that supports .read(char*, int) and .write(char*, int)
  70. //
  71. enum
  72. {
  73. // primary actions
  74. SER_NETWORK = (1 << 0),
  75. SER_DISK = (1 << 1),
  76. SER_GETHASH = (1 << 2),
  77. // modifiers
  78. SER_SKIPSIG = (1 << 16),
  79. SER_BLOCKHEADERONLY = (1 << 17),
  80. };
  81. #define IMPLEMENT_SERIALIZE(statements) \
  82. unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const \
  83. { \
  84. CSerActionGetSerializeSize ser_action; \
  85. const bool fGetSize = true; \
  86. const bool fWrite = false; \
  87. const bool fRead = false; \
  88. unsigned int nSerSize = 0; \
  89. ser_streamplaceholder s; \
  90. s.nType = nType; \
  91. s.nVersion = nVersion; \
  92. {statements} \
  93. return nSerSize; \
  94. } \
  95. template<typename Stream> \
  96. void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const \
  97. { \
  98. CSerActionSerialize ser_action; \
  99. const bool fGetSize = false; \
  100. const bool fWrite = true; \
  101. const bool fRead = false; \
  102. unsigned int nSerSize = 0; \
  103. {statements} \
  104. } \
  105. template<typename Stream> \
  106. void Unserialize(Stream& s, int nType=0, int nVersion=VERSION) \
  107. { \
  108. CSerActionUnserialize ser_action; \
  109. const bool fGetSize = false; \
  110. const bool fWrite = false; \
  111. const bool fRead = true; \
  112. unsigned int nSerSize = 0; \
  113. {statements} \
  114. }
  115. #define READWRITE(obj) (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))
  116. //
  117. // Basic types
  118. //
  119. #define WRITEDATA(s, obj) s.write((char*)&(obj), sizeof(obj))
  120. #define READDATA(s, obj) s.read((char*)&(obj), sizeof(obj))
  121. inline unsigned int GetSerializeSize(char a, int, int=0) { return sizeof(a); }
  122. inline unsigned int GetSerializeSize(signed char a, int, int=0) { return sizeof(a); }
  123. inline unsigned int GetSerializeSize(unsigned char a, int, int=0) { return sizeof(a); }
  124. inline unsigned int GetSerializeSize(signed short a, int, int=0) { return sizeof(a); }
  125. inline unsigned int GetSerializeSize(unsigned short a, int, int=0) { return sizeof(a); }
  126. inline unsigned int GetSerializeSize(signed int a, int, int=0) { return sizeof(a); }
  127. inline unsigned int GetSerializeSize(unsigned int a, int, int=0) { return sizeof(a); }
  128. inline unsigned int GetSerializeSize(signed long a, int, int=0) { return sizeof(a); }
  129. inline unsigned int GetSerializeSize(unsigned long a, int, int=0) { return sizeof(a); }
  130. inline unsigned int GetSerializeSize(int64 a, int, int=0) { return sizeof(a); }
  131. inline unsigned int GetSerializeSize(uint64 a, int, int=0) { return sizeof(a); }
  132. inline unsigned int GetSerializeSize(float a, int, int=0) { return sizeof(a); }
  133. inline unsigned int GetSerializeSize(double a, int, int=0) { return sizeof(a); }
  134. template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { WRITEDATA(s, a); }
  135. template<typename Stream> inline void Serialize(Stream& s, signed char a, int, int=0) { WRITEDATA(s, a); }
  136. template<typename Stream> inline void Serialize(Stream& s, unsigned char a, int, int=0) { WRITEDATA(s, a); }
  137. template<typename Stream> inline void Serialize(Stream& s, signed short a, int, int=0) { WRITEDATA(s, a); }
  138. template<typename Stream> inline void Serialize(Stream& s, unsigned short a, int, int=0) { WRITEDATA(s, a); }
  139. template<typename Stream> inline void Serialize(Stream& s, signed int a, int, int=0) { WRITEDATA(s, a); }
  140. template<typename Stream> inline void Serialize(Stream& s, unsigned int a, int, int=0) { WRITEDATA(s, a); }
  141. template<typename Stream> inline void Serialize(Stream& s, signed long a, int, int=0) { WRITEDATA(s, a); }
  142. template<typename Stream> inline void Serialize(Stream& s, unsigned long a, int, int=0) { WRITEDATA(s, a); }
  143. template<typename Stream> inline void Serialize(Stream& s, int64 a, int, int=0) { WRITEDATA(s, a); }
  144. template<typename Stream> inline void Serialize(Stream& s, uint64 a, int, int=0) { WRITEDATA(s, a); }
  145. template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { WRITEDATA(s, a); }
  146. template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { WRITEDATA(s, a); }
  147. template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { READDATA(s, a); }
  148. template<typename Stream> inline void Unserialize(Stream& s, signed char& a, int, int=0) { READDATA(s, a); }
  149. template<typename Stream> inline void Unserialize(Stream& s, unsigned char& a, int, int=0) { READDATA(s, a); }
  150. template<typename Stream> inline void Unserialize(Stream& s, signed short& a, int, int=0) { READDATA(s, a); }
  151. template<typename Stream> inline void Unserialize(Stream& s, unsigned short& a, int, int=0) { READDATA(s, a); }
  152. template<typename Stream> inline void Unserialize(Stream& s, signed int& a, int, int=0) { READDATA(s, a); }
  153. template<typename Stream> inline void Unserialize(Stream& s, unsigned int& a, int, int=0) { READDATA(s, a); }
  154. template<typename Stream> inline void Unserialize(Stream& s, signed long& a, int, int=0) { READDATA(s, a); }
  155. template<typename Stream> inline void Unserialize(Stream& s, unsigned long& a, int, int=0) { READDATA(s, a); }
  156. template<typename Stream> inline void Unserialize(Stream& s, int64& a, int, int=0) { READDATA(s, a); }
  157. template<typename Stream> inline void Unserialize(Stream& s, uint64& a, int, int=0) { READDATA(s, a); }
  158. template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { READDATA(s, a); }
  159. template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { READDATA(s, a); }
  160. inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
  161. template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; WRITEDATA(s, f); }
  162. template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f; READDATA(s, f); a=f; }
  163. //
  164. // Compact size
  165. // size < 253 -- 1 byte
  166. // size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
  167. // size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
  168. // size > UINT_MAX -- 9 bytes (255 + 8 bytes)
  169. //
  170. inline unsigned int GetSizeOfCompactSize(uint64 nSize)
  171. {
  172. if (nSize < 253) return sizeof(unsigned char);
  173. else if (nSize <= USHRT_MAX) return sizeof(unsigned char) + sizeof(unsigned short);
  174. else if (nSize <= UINT_MAX) return sizeof(unsigned char) + sizeof(unsigned int);
  175. else return sizeof(unsigned char) + sizeof(uint64);
  176. }
  177. template<typename Stream>
  178. void WriteCompactSize(Stream& os, uint64 nSize)
  179. {
  180. if (nSize < 253)
  181. {
  182. unsigned char chSize = nSize;
  183. WRITEDATA(os, chSize);
  184. }
  185. else if (nSize <= USHRT_MAX)
  186. {
  187. unsigned char chSize = 253;
  188. unsigned short xSize = nSize;
  189. WRITEDATA(os, chSize);
  190. WRITEDATA(os, xSize);
  191. }
  192. else if (nSize <= UINT_MAX)
  193. {
  194. unsigned char chSize = 254;
  195. unsigned int xSize = nSize;
  196. WRITEDATA(os, chSize);
  197. WRITEDATA(os, xSize);
  198. }
  199. else
  200. {
  201. unsigned char chSize = 255;
  202. uint64 xSize = nSize;
  203. WRITEDATA(os, chSize);
  204. WRITEDATA(os, xSize);
  205. }
  206. return;
  207. }
  208. template<typename Stream>
  209. uint64 ReadCompactSize(Stream& is)
  210. {
  211. unsigned char chSize;
  212. READDATA(is, chSize);
  213. uint64 nSizeRet = 0;
  214. if (chSize < 253)
  215. {
  216. nSizeRet = chSize;
  217. }
  218. else if (chSize == 253)
  219. {
  220. unsigned short xSize;
  221. READDATA(is, xSize);
  222. nSizeRet = xSize;
  223. }
  224. else if (chSize == 254)
  225. {
  226. unsigned int xSize;
  227. READDATA(is, xSize);
  228. nSizeRet = xSize;
  229. }
  230. else
  231. {
  232. uint64 xSize;
  233. READDATA(is, xSize);
  234. nSizeRet = xSize;
  235. }
  236. if (nSizeRet > (uint64)MAX_SIZE)
  237. throw std::ios_base::failure("ReadCompactSize() : size too large");
  238. return nSizeRet;
  239. }
  240. //
  241. // Wrapper for serializing arrays and POD
  242. // There's a clever template way to make arrays serialize normally, but MSVC6 doesn't support it
  243. //
  244. #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
  245. class CFlatData
  246. {
  247. protected:
  248. char* pbegin;
  249. char* pend;
  250. public:
  251. CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
  252. char* begin() { return pbegin; }
  253. const char* begin() const { return pbegin; }
  254. char* end() { return pend; }
  255. const char* end() const { return pend; }
  256. unsigned int GetSerializeSize(int, int=0) const
  257. {
  258. return pend - pbegin;
  259. }
  260. template<typename Stream>
  261. void Serialize(Stream& s, int, int=0) const
  262. {
  263. s.write(pbegin, pend - pbegin);
  264. }
  265. template<typename Stream>
  266. void Unserialize(Stream& s, int, int=0)
  267. {
  268. s.read(pbegin, pend - pbegin);
  269. }
  270. };
  271. //
  272. // string stored as a fixed length field
  273. //
  274. template<std::size_t LEN>
  275. class CFixedFieldString
  276. {
  277. protected:
  278. const std::string* pcstr;
  279. std::string* pstr;
  280. public:
  281. explicit CFixedFieldString(const std::string& str) : pcstr(&str), pstr(NULL) { }
  282. explicit CFixedFieldString(std::string& str) : pcstr(&str), pstr(&str) { }
  283. unsigned int GetSerializeSize(int, int=0) const
  284. {
  285. return LEN;
  286. }
  287. template<typename Stream>
  288. void Serialize(Stream& s, int, int=0) const
  289. {
  290. char pszBuf[LEN];
  291. strncpy(pszBuf, pcstr->c_str(), LEN);
  292. s.write(pszBuf, LEN);
  293. }
  294. template<typename Stream>
  295. void Unserialize(Stream& s, int, int=0)
  296. {
  297. if (pstr == NULL)
  298. throw std::ios_base::failure("CFixedFieldString::Unserialize : trying to unserialize to const string");
  299. char pszBuf[LEN+1];
  300. s.read(pszBuf, LEN);
  301. pszBuf[LEN] = '\0';
  302. *pstr = pszBuf;
  303. }
  304. };
  305. //
  306. // Forward declarations
  307. //
  308. // string
  309. template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
  310. template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
  311. template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
  312. // vector
  313. template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&);
  314. template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&);
  315. template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion=VERSION);
  316. 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&);
  317. 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&);
  318. template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion=VERSION);
  319. 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&);
  320. 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&);
  321. template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion=VERSION);
  322. // others derived from vector
  323. extern inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion=VERSION);
  324. template<typename Stream> void Serialize(Stream& os, const CScript& v, int nType, int nVersion=VERSION);
  325. template<typename Stream> void Unserialize(Stream& is, CScript& v, int nType, int nVersion=VERSION);
  326. // pair
  327. template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion=VERSION);
  328. template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion=VERSION);
  329. template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion=VERSION);
  330. // 3 tuple
  331. template<typename T0, typename T1, typename T2> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
  332. template<typename Stream, typename T0, typename T1, typename T2> void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
  333. template<typename Stream, typename T0, typename T1, typename T2> void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
  334. // 4 tuple
  335. template<typename T0, typename T1, typename T2, typename T3> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=VERSION);
  336. 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=VERSION);
  337. 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=VERSION);
  338. // map
  339. template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion=VERSION);
  340. 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=VERSION);
  341. 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=VERSION);
  342. // set
  343. template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion=VERSION);
  344. template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion=VERSION);
  345. template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion=VERSION);
  346. //
  347. // If none of the specialized versions above matched, default to calling member function.
  348. // "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
  349. // The compiler will only cast int to long if none of the other templates matched.
  350. // Thanks to Boost serialization for this idea.
  351. //
  352. template<typename T>
  353. inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion=VERSION)
  354. {
  355. return a.GetSerializeSize((int)nType, nVersion);
  356. }
  357. template<typename Stream, typename T>
  358. inline void Serialize(Stream& os, const T& a, long nType, int nVersion=VERSION)
  359. {
  360. a.Serialize(os, (int)nType, nVersion);
  361. }
  362. template<typename Stream, typename T>
  363. inline void Unserialize(Stream& is, T& a, long nType, int nVersion=VERSION)
  364. {
  365. a.Unserialize(is, (int)nType, nVersion);
  366. }
  367. //
  368. // string
  369. //
  370. template<typename C>
  371. unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
  372. {
  373. return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
  374. }
  375. template<typename Stream, typename C>
  376. void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
  377. {
  378. WriteCompactSize(os, str.size());
  379. if (!str.empty())
  380. os.write((char*)&str[0], str.size() * sizeof(str[0]));
  381. }
  382. template<typename Stream, typename C>
  383. void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
  384. {
  385. unsigned int nSize = ReadCompactSize(is);
  386. str.resize(nSize);
  387. if (nSize != 0)
  388. is.read((char*)&str[0], nSize * sizeof(str[0]));
  389. }
  390. //
  391. // vector
  392. //
  393. template<typename T, typename A>
  394. unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
  395. {
  396. return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
  397. }
  398. template<typename T, typename A>
  399. unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
  400. {
  401. unsigned int nSize = GetSizeOfCompactSize(v.size());
  402. for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
  403. nSize += GetSerializeSize((*vi), nType, nVersion);
  404. return nSize;
  405. }
  406. template<typename T, typename A>
  407. inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
  408. {
  409. return GetSerializeSize_impl(v, nType, nVersion, boost::is_fundamental<T>());
  410. }
  411. template<typename Stream, typename T, typename A>
  412. void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
  413. {
  414. WriteCompactSize(os, v.size());
  415. if (!v.empty())
  416. os.write((char*)&v[0], v.size() * sizeof(T));
  417. }
  418. template<typename Stream, typename T, typename A>
  419. void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
  420. {
  421. WriteCompactSize(os, v.size());
  422. for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
  423. ::Serialize(os, (*vi), nType, nVersion);
  424. }
  425. template<typename Stream, typename T, typename A>
  426. inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
  427. {
  428. Serialize_impl(os, v, nType, nVersion, boost::is_fundamental<T>());
  429. }
  430. template<typename Stream, typename T, typename A>
  431. void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::true_type&)
  432. {
  433. //unsigned int nSize = ReadCompactSize(is);
  434. //v.resize(nSize);
  435. //is.read((char*)&v[0], nSize * sizeof(T));
  436. // Limit size per read so bogus size value won't cause out of memory
  437. v.clear();
  438. unsigned int nSize = ReadCompactSize(is);
  439. unsigned int i = 0;
  440. while (i < nSize)
  441. {
  442. unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
  443. v.resize(i + blk);
  444. is.read((char*)&v[i], blk * sizeof(T));
  445. i += blk;
  446. }
  447. }
  448. template<typename Stream, typename T, typename A>
  449. void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const boost::false_type&)
  450. {
  451. //unsigned int nSize = ReadCompactSize(is);
  452. //v.resize(nSize);
  453. //for (std::vector<T, A>::iterator vi = v.begin(); vi != v.end(); ++vi)
  454. // Unserialize(is, (*vi), nType, nVersion);
  455. v.clear();
  456. unsigned int nSize = ReadCompactSize(is);
  457. unsigned int i = 0;
  458. unsigned int nMid = 0;
  459. while (nMid < nSize)
  460. {
  461. nMid += 5000000 / sizeof(T);
  462. if (nMid > nSize)
  463. nMid = nSize;
  464. v.resize(nMid);
  465. for (; i < nMid; i++)
  466. Unserialize(is, v[i], nType, nVersion);
  467. }
  468. }
  469. template<typename Stream, typename T, typename A>
  470. inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
  471. {
  472. Unserialize_impl(is, v, nType, nVersion, boost::is_fundamental<T>());
  473. }
  474. //
  475. // others derived from vector
  476. //
  477. inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion)
  478. {
  479. return GetSerializeSize((const std::vector<unsigned char>&)v, nType, nVersion);
  480. }
  481. template<typename Stream>
  482. void Serialize(Stream& os, const CScript& v, int nType, int nVersion)
  483. {
  484. Serialize(os, (const std::vector<unsigned char>&)v, nType, nVersion);
  485. }
  486. template<typename Stream>
  487. void Unserialize(Stream& is, CScript& v, int nType, int nVersion)
  488. {
  489. Unserialize(is, (std::vector<unsigned char>&)v, nType, nVersion);
  490. }
  491. //
  492. // pair
  493. //
  494. template<typename K, typename T>
  495. unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
  496. {
  497. return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
  498. }
  499. template<typename Stream, typename K, typename T>
  500. void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
  501. {
  502. Serialize(os, item.first, nType, nVersion);
  503. Serialize(os, item.second, nType, nVersion);
  504. }
  505. template<typename Stream, typename K, typename T>
  506. void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
  507. {
  508. Unserialize(is, item.first, nType, nVersion);
  509. Unserialize(is, item.second, nType, nVersion);
  510. }
  511. //
  512. // 3 tuple
  513. //
  514. template<typename T0, typename T1, typename T2>
  515. unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
  516. {
  517. unsigned int nSize = 0;
  518. nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
  519. nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
  520. nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
  521. return nSize;
  522. }
  523. template<typename Stream, typename T0, typename T1, typename T2>
  524. void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
  525. {
  526. Serialize(os, boost::get<0>(item), nType, nVersion);
  527. Serialize(os, boost::get<1>(item), nType, nVersion);
  528. Serialize(os, boost::get<2>(item), nType, nVersion);
  529. }
  530. template<typename Stream, typename T0, typename T1, typename T2>
  531. void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
  532. {
  533. Unserialize(is, boost::get<0>(item), nType, nVersion);
  534. Unserialize(is, boost::get<1>(item), nType, nVersion);
  535. Unserialize(is, boost::get<2>(item), nType, nVersion);
  536. }
  537. //
  538. // 4 tuple
  539. //
  540. template<typename T0, typename T1, typename T2, typename T3>
  541. unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
  542. {
  543. unsigned int nSize = 0;
  544. nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion);
  545. nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion);
  546. nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion);
  547. nSize += GetSerializeSize(boost::get<3>(item), nType, nVersion);
  548. return nSize;
  549. }
  550. template<typename Stream, typename T0, typename T1, typename T2, typename T3>
  551. void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
  552. {
  553. Serialize(os, boost::get<0>(item), nType, nVersion);
  554. Serialize(os, boost::get<1>(item), nType, nVersion);
  555. Serialize(os, boost::get<2>(item), nType, nVersion);
  556. Serialize(os, boost::get<3>(item), nType, nVersion);
  557. }
  558. template<typename Stream, typename T0, typename T1, typename T2, typename T3>
  559. void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
  560. {
  561. Unserialize(is, boost::get<0>(item), nType, nVersion);
  562. Unserialize(is, boost::get<1>(item), nType, nVersion);
  563. Unserialize(is, boost::get<2>(item), nType, nVersion);
  564. Unserialize(is, boost::get<3>(item), nType, nVersion);
  565. }
  566. //
  567. // map
  568. //
  569. template<typename K, typename T, typename Pred, typename A>
  570. unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
  571. {
  572. unsigned int nSize = GetSizeOfCompactSize(m.size());
  573. for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
  574. nSize += GetSerializeSize((*mi), nType, nVersion);
  575. return nSize;
  576. }
  577. template<typename Stream, typename K, typename T, typename Pred, typename A>
  578. void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
  579. {
  580. WriteCompactSize(os, m.size());
  581. for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
  582. Serialize(os, (*mi), nType, nVersion);
  583. }
  584. template<typename Stream, typename K, typename T, typename Pred, typename A>
  585. void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
  586. {
  587. m.clear();
  588. unsigned int nSize = ReadCompactSize(is);
  589. typename std::map<K, T, Pred, A>::iterator mi = m.begin();
  590. for (unsigned int i = 0; i < nSize; i++)
  591. {
  592. std::pair<K, T> item;
  593. Unserialize(is, item, nType, nVersion);
  594. mi = m.insert(mi, item);
  595. }
  596. }
  597. //
  598. // set
  599. //
  600. template<typename K, typename Pred, typename A>
  601. unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
  602. {
  603. unsigned int nSize = GetSizeOfCompactSize(m.size());
  604. for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
  605. nSize += GetSerializeSize((*it), nType, nVersion);
  606. return nSize;
  607. }
  608. template<typename Stream, typename K, typename Pred, typename A>
  609. void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
  610. {
  611. WriteCompactSize(os, m.size());
  612. for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
  613. Serialize(os, (*it), nType, nVersion);
  614. }
  615. template<typename Stream, typename K, typename Pred, typename A>
  616. void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
  617. {
  618. m.clear();
  619. unsigned int nSize = ReadCompactSize(is);
  620. typename std::set<K, Pred, A>::iterator it = m.begin();
  621. for (unsigned int i = 0; i < nSize; i++)
  622. {
  623. K key;
  624. Unserialize(is, key, nType, nVersion);
  625. it = m.insert(it, key);
  626. }
  627. }
  628. //
  629. // Support for IMPLEMENT_SERIALIZE and READWRITE macro
  630. //
  631. class CSerActionGetSerializeSize { };
  632. class CSerActionSerialize { };
  633. class CSerActionUnserialize { };
  634. template<typename Stream, typename T>
  635. inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
  636. {
  637. return ::GetSerializeSize(obj, nType, nVersion);
  638. }
  639. template<typename Stream, typename T>
  640. inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
  641. {
  642. ::Serialize(s, obj, nType, nVersion);
  643. return 0;
  644. }
  645. template<typename Stream, typename T>
  646. inline unsigned int SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
  647. {
  648. ::Unserialize(s, obj, nType, nVersion);
  649. return 0;
  650. }
  651. struct ser_streamplaceholder
  652. {
  653. int nType;
  654. int nVersion;
  655. };
  656. //
  657. // Allocator that locks its contents from being paged
  658. // out of memory and clears its contents before deletion.
  659. //
  660. template<typename T>
  661. struct secure_allocator : public std::allocator<T>
  662. {
  663. // MSVC8 default copy constructor is broken
  664. typedef std::allocator<T> base;
  665. typedef typename base::size_type size_type;
  666. typedef typename base::difference_type difference_type;
  667. typedef typename base::pointer pointer;
  668. typedef typename base::const_pointer const_pointer;
  669. typedef typename base::reference reference;
  670. typedef typename base::const_reference const_reference;
  671. typedef typename base::value_type value_type;
  672. secure_allocator() throw() {}
  673. secure_allocator(const secure_allocator& a) throw() : base(a) {}
  674. template <typename U>
  675. secure_allocator(const secure_allocator<U>& a) throw() : base(a) {}
  676. ~secure_allocator() throw() {}
  677. template<typename _Other> struct rebind
  678. { typedef secure_allocator<_Other> other; };
  679. T* allocate(std::size_t n, const void *hint = 0)
  680. {
  681. T *p;
  682. p = std::allocator<T>::allocate(n, hint);
  683. if (p != NULL)
  684. mlock(p, sizeof(T) * n);
  685. return p;
  686. }
  687. void deallocate(T* p, std::size_t n)
  688. {
  689. if (p != NULL)
  690. {
  691. memset(p, 0, sizeof(T) * n);
  692. munlock(p, sizeof(T) * n);
  693. }
  694. std::allocator<T>::deallocate(p, n);
  695. }
  696. };
  697. //
  698. // Double ended buffer combining vector and stream-like interfaces.
  699. // >> and << read and write unformatted data using the above serialization templates.
  700. // Fills with data in linear time; some stringstream implementations take N^2 time.
  701. //
  702. class CDataStream
  703. {
  704. protected:
  705. typedef std::vector<char, secure_allocator<char> > vector_type;
  706. vector_type vch;
  707. unsigned int nReadPos;
  708. short state;
  709. short exceptmask;
  710. public:
  711. int nType;
  712. int nVersion;
  713. typedef vector_type::allocator_type allocator_type;
  714. typedef vector_type::size_type size_type;
  715. typedef vector_type::difference_type difference_type;
  716. typedef vector_type::reference reference;
  717. typedef vector_type::const_reference const_reference;
  718. typedef vector_type::value_type value_type;
  719. typedef vector_type::iterator iterator;
  720. typedef vector_type::const_iterator const_iterator;
  721. typedef vector_type::reverse_iterator reverse_iterator;
  722. explicit CDataStream(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
  723. {
  724. Init(nTypeIn, nVersionIn);
  725. }
  726. CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
  727. {
  728. Init(nTypeIn, nVersionIn);
  729. }
  730. #if !defined(_MSC_VER) || _MSC_VER >= 1300
  731. CDataStream(const char* pbegin, const char* pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
  732. {
  733. Init(nTypeIn, nVersionIn);
  734. }
  735. #endif
  736. CDataStream(const vector_type& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
  737. {
  738. Init(nTypeIn, nVersionIn);
  739. }
  740. CDataStream(const std::vector<char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
  741. {
  742. Init(nTypeIn, nVersionIn);
  743. }
  744. CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
  745. {
  746. Init(nTypeIn, nVersionIn);
  747. }
  748. void Init(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
  749. {
  750. nReadPos = 0;
  751. nType = nTypeIn;
  752. nVersion = nVersionIn;
  753. state = 0;
  754. exceptmask = std::ios::badbit | std::ios::failbit;
  755. }
  756. CDataStream& operator+=(const CDataStream& b)
  757. {
  758. vch.insert(vch.end(), b.begin(), b.end());
  759. return *this;
  760. }
  761. friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
  762. {
  763. CDataStream ret = a;
  764. ret += b;
  765. return (ret);
  766. }
  767. std::string str() const
  768. {
  769. return (std::string(begin(), end()));
  770. }
  771. //
  772. // Vector subset
  773. //
  774. const_iterator begin() const { return vch.begin() + nReadPos; }
  775. iterator begin() { return vch.begin() + nReadPos; }
  776. const_iterator end() const { return vch.end(); }
  777. iterator end() { return vch.end(); }
  778. size_type size() const { return vch.size() - nReadPos; }
  779. bool empty() const { return vch.size() == nReadPos; }
  780. void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
  781. void reserve(size_type n) { vch.reserve(n + nReadPos); }
  782. const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
  783. reference operator[](size_type pos) { return vch[pos + nReadPos]; }
  784. void clear() { vch.clear(); nReadPos = 0; }
  785. iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
  786. void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
  787. void insert(iterator it, const_iterator first, const_iterator last)
  788. {
  789. if (it == vch.begin() + nReadPos && last - first <= nReadPos)
  790. {
  791. // special case for inserting at the front when there's room
  792. nReadPos -= (last - first);
  793. memcpy(&vch[nReadPos], &first[0], last - first);
  794. }
  795. else
  796. vch.insert(it, first, last);
  797. }
  798. void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
  799. {
  800. if (it == vch.begin() + nReadPos && last - first <= nReadPos)
  801. {
  802. // special case for inserting at the front when there's room
  803. nReadPos -= (last - first);
  804. memcpy(&vch[nReadPos], &first[0], last - first);
  805. }
  806. else
  807. vch.insert(it, first, last);
  808. }
  809. #if !defined(_MSC_VER) || _MSC_VER >= 1300
  810. void insert(iterator it, const char* first, const char* last)
  811. {
  812. if (it == vch.begin() + nReadPos && last - first <= nReadPos)
  813. {
  814. // special case for inserting at the front when there's room
  815. nReadPos -= (last - first);
  816. memcpy(&vch[nReadPos], &first[0], last - first);
  817. }
  818. else
  819. vch.insert(it, first, last);
  820. }
  821. #endif
  822. iterator erase(iterator it)
  823. {
  824. if (it == vch.begin() + nReadPos)
  825. {
  826. // special case for erasing from the front
  827. if (++nReadPos >= vch.size())
  828. {
  829. // whenever we reach the end, we take the opportunity to clear the buffer
  830. nReadPos = 0;
  831. return vch.erase(vch.begin(), vch.end());
  832. }
  833. return vch.begin() + nReadPos;
  834. }
  835. else
  836. return vch.erase(it);
  837. }
  838. iterator erase(iterator first, iterator last)
  839. {
  840. if (first == vch.begin() + nReadPos)
  841. {
  842. // special case for erasing from the front
  843. if (last == vch.end())
  844. {
  845. nReadPos = 0;
  846. return vch.erase(vch.begin(), vch.end());
  847. }
  848. else
  849. {
  850. nReadPos = (last - vch.begin());
  851. return last;
  852. }
  853. }
  854. else
  855. return vch.erase(first, last);
  856. }
  857. inline void Compact()
  858. {
  859. vch.erase(vch.begin(), vch.begin() + nReadPos);
  860. nReadPos = 0;
  861. }
  862. bool Rewind(size_type n)
  863. {
  864. // Rewind by n characters if the buffer hasn't been compacted yet
  865. if (n > nReadPos)
  866. return false;
  867. nReadPos -= n;
  868. return true;
  869. }
  870. //
  871. // Stream subset
  872. //
  873. void setstate(short bits, const char* psz)
  874. {
  875. state |= bits;
  876. if (state & exceptmask)
  877. throw std::ios_base::failure(psz);
  878. }
  879. bool eof() const { return size() == 0; }
  880. bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
  881. bool good() const { return !eof() && (state == 0); }
  882. void clear(short n) { state = n; } // name conflict with vector clear()
  883. short exceptions() { return exceptmask; }
  884. short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
  885. CDataStream* rdbuf() { return this; }
  886. int in_avail() { return size(); }
  887. void SetType(int n) { nType = n; }
  888. int GetType() { return nType; }
  889. void SetVersion(int n) { nVersion = n; }
  890. int GetVersion() { return nVersion; }
  891. void ReadVersion() { *this >> nVersion; }
  892. void WriteVersion() { *this << nVersion; }
  893. CDataStream& read(char* pch, int nSize)
  894. {
  895. // Read from the beginning of the buffer
  896. assert(nSize >= 0);
  897. unsigned int nReadPosNext = nReadPos + nSize;
  898. if (nReadPosNext >= vch.size())
  899. {
  900. if (nReadPosNext > vch.size())
  901. {
  902. setstate(std::ios::failbit, "CDataStream::read() : end of data");
  903. memset(pch, 0, nSize);
  904. nSize = vch.size() - nReadPos;
  905. }
  906. memcpy(pch, &vch[nReadPos], nSize);
  907. nReadPos = 0;
  908. vch.clear();
  909. return (*this);
  910. }
  911. memcpy(pch, &vch[nReadPos], nSize);
  912. nReadPos = nReadPosNext;
  913. return (*this);
  914. }
  915. CDataStream& ignore(int nSize)
  916. {
  917. // Ignore from the beginning of the buffer
  918. assert(nSize >= 0);
  919. unsigned int nReadPosNext = nReadPos + nSize;
  920. if (nReadPosNext >= vch.size())
  921. {
  922. if (nReadPosNext > vch.size())
  923. {
  924. setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
  925. nSize = vch.size() - nReadPos;
  926. }
  927. nReadPos = 0;
  928. vch.clear();
  929. return (*this);
  930. }
  931. nReadPos = nReadPosNext;
  932. return (*this);
  933. }
  934. CDataStream& write(const char* pch, int nSize)
  935. {
  936. // Write to the end of the buffer
  937. assert(nSize >= 0);
  938. vch.insert(vch.end(), pch, pch + nSize);
  939. return (*this);
  940. }
  941. template<typename Stream>
  942. void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
  943. {
  944. // Special case: stream << stream concatenates like stream += stream
  945. if (!vch.empty())
  946. s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
  947. }
  948. template<typename T>
  949. unsigned int GetSerializeSize(const T& obj)
  950. {
  951. // Tells the size of the object if serialized to this stream
  952. return ::GetSerializeSize(obj, nType, nVersion);
  953. }
  954. template<typename T>
  955. CDataStream& operator<<(const T& obj)
  956. {
  957. // Serialize to this stream
  958. ::Serialize(*this, obj, nType, nVersion);
  959. return (*this);
  960. }
  961. template<typename T>
  962. CDataStream& operator>>(T& obj)
  963. {
  964. // Unserialize from this stream
  965. ::Unserialize(*this, obj, nType, nVersion);
  966. return (*this);
  967. }
  968. };
  969. #ifdef TESTCDATASTREAM
  970. // VC6sp6
  971. // CDataStream:
  972. // n=1000 0 seconds
  973. // n=2000 0 seconds
  974. // n=4000 0 seconds
  975. // n=8000 0 seconds
  976. // n=16000 0 seconds
  977. // n=32000 0 seconds
  978. // n=64000 1 seconds
  979. // n=128000 1 seconds
  980. // n=256000 2 seconds
  981. // n=512000 4 seconds
  982. // n=1024000 8 seconds
  983. // n=2048000 16 seconds
  984. // n=4096000 32 seconds
  985. // stringstream:
  986. // n=1000 1 seconds
  987. // n=2000 1 seconds
  988. // n=4000 13 seconds
  989. // n=8000 87 seconds
  990. // n=16000 400 seconds
  991. // n=32000 1660 seconds
  992. // n=64000 6749 seconds
  993. // n=128000 27241 seconds
  994. // n=256000 109804 seconds
  995. #include <iostream>
  996. int main(int argc, char *argv[])
  997. {
  998. vector<unsigned char> vch(0xcc, 250);
  999. printf("CDataStream:\n");
  1000. for (int n = 1000; n <= 4500000; n *= 2)
  1001. {
  1002. CDataStream ss;
  1003. time_t nStart = time(NULL);
  1004. for (int i = 0; i < n; i++)
  1005. ss.write((char*)&vch[0], vch.size());
  1006. printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
  1007. }
  1008. printf("stringstream:\n");
  1009. for (int n = 1000; n <= 4500000; n *= 2)
  1010. {
  1011. stringstream ss;
  1012. time_t nStart = time(NULL);
  1013. for (int i = 0; i < n; i++)
  1014. ss.write((char*)&vch[0], vch.size());
  1015. printf("n=%-10d %d seconds\n", n, time(NULL) - nStart);
  1016. }
  1017. }
  1018. #endif
  1019. //
  1020. // Automatic closing wrapper for FILE*
  1021. // - Will automatically close the file when it goes out of scope if not null.
  1022. // - If you're returning the file pointer, return file.release().
  1023. // - If you need to close the file early, use file.fclose() instead of fclose(file).
  1024. //
  1025. class CAutoFile
  1026. {
  1027. protected:
  1028. FILE* file;
  1029. short state;
  1030. short exceptmask;
  1031. public:
  1032. int nType;
  1033. int nVersion;
  1034. typedef FILE element_type;
  1035. CAutoFile(FILE* filenew=NULL, int nTypeIn=SER_DISK, int nVersionIn=VERSION)
  1036. {
  1037. file = filenew;
  1038. nType = nTypeIn;
  1039. nVersion = nVersionIn;
  1040. state = 0;
  1041. exceptmask = std::ios::badbit | std::ios::failbit;
  1042. }
  1043. ~CAutoFile()
  1044. {
  1045. fclose();
  1046. }
  1047. void fclose()
  1048. {
  1049. if (file != NULL && file != stdin && file != stdout && file != stderr)
  1050. ::fclose(file);
  1051. file = NULL;
  1052. }
  1053. FILE* release() { FILE* ret = file; file = NULL; return ret; }
  1054. operator FILE*() { return file; }
  1055. FILE* operator->() { return file; }
  1056. FILE& operator*() { return *file; }
  1057. FILE** operator&() { return &file; }
  1058. FILE* operator=(FILE* pnew) { return file = pnew; }
  1059. bool operator!() { return (file == NULL); }
  1060. //
  1061. // Stream subset
  1062. //
  1063. void setstate(short bits, const char* psz)
  1064. {
  1065. state |= bits;
  1066. if (state & exceptmask)
  1067. throw std::ios_base::failure(psz);
  1068. }
  1069. bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
  1070. bool good() const { return state == 0; }
  1071. void clear(short n = 0) { state = n; }
  1072. short exceptions() { return exceptmask; }
  1073. short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
  1074. void SetType(int n) { nType = n; }
  1075. int GetType() { return nType; }
  1076. void SetVersion(int n) { nVersion = n; }
  1077. int GetVersion() { return nVersion; }
  1078. void ReadVersion() { *this >> nVersion; }
  1079. void WriteVersion() { *this << nVersion; }
  1080. CAutoFile& read(char* pch, int nSize)
  1081. {
  1082. if (!file)
  1083. throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
  1084. if (fread(pch, 1, nSize, file) != nSize)
  1085. setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
  1086. return (*this);
  1087. }
  1088. CAutoFile& write(const char* pch, int nSize)
  1089. {
  1090. if (!file)
  1091. throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
  1092. if (fwrite(pch, 1, nSize, file) != nSize)
  1093. setstate(std::ios::failbit, "CAutoFile::write : write failed");
  1094. return (*this);
  1095. }
  1096. template<typename T>
  1097. unsigned int GetSerializeSize(const T& obj)
  1098. {
  1099. // Tells the size of the object if serialized to this stream
  1100. return ::GetSerializeSize(obj, nType, nVersion);
  1101. }
  1102. template<typename T>
  1103. CAutoFile& operator<<(const T& obj)
  1104. {
  1105. // Serialize to this stream
  1106. if (!file)
  1107. throw std::ios_base::failure("CAutoFile::operator<< : file handle is NULL");
  1108. ::Serialize(*this, obj, nType, nVersion);
  1109. return (*this);
  1110. }
  1111. template<typename T>
  1112. CAutoFile& operator>>(T& obj)
  1113. {
  1114. // Unserialize from this stream
  1115. if (!file)
  1116. throw std::ios_base::failure("CAutoFile::operator>> : file handle is NULL");
  1117. ::Unserialize(*this, obj, nType, nVersion);
  1118. return (*this);
  1119. }
  1120. };
  1121. #endif