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 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef STARWELS_SERIALIZE_H
  6. #define STARWELS_SERIALIZE_H
  7. #include "compat/endian.h"
  8. #include <algorithm>
  9. #include <assert.h>
  10. #include <ios>
  11. #include <limits>
  12. #include <map>
  13. #include <memory>
  14. #include <set>
  15. #include <stdint.h>
  16. #include <string>
  17. #include <string.h>
  18. #include <utility>
  19. #include <vector>
  20. #include "prevector.h"
  21. static const unsigned int MAX_SIZE = 0x02000000;
  22. /**
  23. * Dummy data type to identify deserializing constructors.
  24. *
  25. * By convention, a constructor of a type T with signature
  26. *
  27. * template <typename Stream> T::T(deserialize_type, Stream& s)
  28. *
  29. * is a deserializing constructor, which builds the type by
  30. * deserializing it from s. If T contains const fields, this
  31. * is likely the only way to do so.
  32. */
  33. struct deserialize_type {};
  34. constexpr deserialize_type deserialize {};
  35. /**
  36. * Used to bypass the rule against non-const reference to temporary
  37. * where it makes sense with wrappers such as CFlatData or CTxDB
  38. */
  39. template<typename T>
  40. inline T& REF(const T& val)
  41. {
  42. return const_cast<T&>(val);
  43. }
  44. /**
  45. * Used to acquire a non-const pointer "this" to generate bodies
  46. * of const serialization operations from a template
  47. */
  48. template<typename T>
  49. inline T* NCONST_PTR(const T* val)
  50. {
  51. return const_cast<T*>(val);
  52. }
  53. /*
  54. * Lowest-level serialization and conversion.
  55. * @note Sizes of these types are verified in the tests
  56. */
  57. template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
  58. {
  59. s.write((char*)&obj, 1);
  60. }
  61. template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
  62. {
  63. obj = htole16(obj);
  64. s.write((char*)&obj, 2);
  65. }
  66. template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
  67. {
  68. obj = htole32(obj);
  69. s.write((char*)&obj, 4);
  70. }
  71. template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
  72. {
  73. obj = htole64(obj);
  74. s.write((char*)&obj, 8);
  75. }
  76. template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
  77. {
  78. uint8_t obj;
  79. s.read((char*)&obj, 1);
  80. return obj;
  81. }
  82. template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
  83. {
  84. uint16_t obj;
  85. s.read((char*)&obj, 2);
  86. return le16toh(obj);
  87. }
  88. template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
  89. {
  90. uint32_t obj;
  91. s.read((char*)&obj, 4);
  92. return le32toh(obj);
  93. }
  94. template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
  95. {
  96. uint64_t obj;
  97. s.read((char*)&obj, 8);
  98. return le64toh(obj);
  99. }
  100. inline uint64_t ser_double_to_uint64(double x)
  101. {
  102. union { double x; uint64_t y; } tmp;
  103. tmp.x = x;
  104. return tmp.y;
  105. }
  106. inline uint32_t ser_float_to_uint32(float x)
  107. {
  108. union { float x; uint32_t y; } tmp;
  109. tmp.x = x;
  110. return tmp.y;
  111. }
  112. inline double ser_uint64_to_double(uint64_t y)
  113. {
  114. union { double x; uint64_t y; } tmp;
  115. tmp.y = y;
  116. return tmp.x;
  117. }
  118. inline float ser_uint32_to_float(uint32_t y)
  119. {
  120. union { float x; uint32_t y; } tmp;
  121. tmp.y = y;
  122. return tmp.x;
  123. }
  124. /////////////////////////////////////////////////////////////////
  125. //
  126. // Templates for serializing to anything that looks like a stream,
  127. // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
  128. //
  129. class CSizeComputer;
  130. enum
  131. {
  132. // primary actions
  133. SER_NETWORK = (1 << 0),
  134. SER_DISK = (1 << 1),
  135. SER_GETHASH = (1 << 2),
  136. };
  137. #define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
  138. #define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
  139. /**
  140. * Implement three methods for serializable objects. These are actually wrappers over
  141. * "SerializationOp" template, which implements the body of each class' serialization
  142. * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
  143. * added as members.
  144. */
  145. #define ADD_SERIALIZE_METHODS \
  146. template<typename Stream> \
  147. void Serialize(Stream& s) const { \
  148. NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
  149. } \
  150. template<typename Stream> \
  151. void Unserialize(Stream& s) { \
  152. SerializationOp(s, CSerActionUnserialize()); \
  153. }
  154. template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
  155. template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
  156. template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
  157. template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
  158. template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
  159. template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
  160. template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
  161. template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
  162. template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
  163. template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); }
  164. template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
  165. template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
  166. template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
  167. template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
  168. template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
  169. template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
  170. template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); }
  171. template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
  172. template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
  173. template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
  174. template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); }
  175. template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
  176. template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
  177. template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
  178. /**
  179. * Compact Size
  180. * size < 253 -- 1 byte
  181. * size <= USHRT_MAX -- 3 bytes (253 + 2 bytes)
  182. * size <= UINT_MAX -- 5 bytes (254 + 4 bytes)
  183. * size > UINT_MAX -- 9 bytes (255 + 8 bytes)
  184. */
  185. inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
  186. {
  187. if (nSize < 253) return sizeof(unsigned char);
  188. else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
  189. else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
  190. else return sizeof(unsigned char) + sizeof(uint64_t);
  191. }
  192. inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
  193. template<typename Stream>
  194. void WriteCompactSize(Stream& os, uint64_t nSize)
  195. {
  196. if (nSize < 253)
  197. {
  198. ser_writedata8(os, nSize);
  199. }
  200. else if (nSize <= std::numeric_limits<unsigned short>::max())
  201. {
  202. ser_writedata8(os, 253);
  203. ser_writedata16(os, nSize);
  204. }
  205. else if (nSize <= std::numeric_limits<unsigned int>::max())
  206. {
  207. ser_writedata8(os, 254);
  208. ser_writedata32(os, nSize);
  209. }
  210. else
  211. {
  212. ser_writedata8(os, 255);
  213. ser_writedata64(os, nSize);
  214. }
  215. return;
  216. }
  217. template<typename Stream>
  218. uint64_t ReadCompactSize(Stream& is)
  219. {
  220. uint8_t chSize = ser_readdata8(is);
  221. uint64_t nSizeRet = 0;
  222. if (chSize < 253)
  223. {
  224. nSizeRet = chSize;
  225. }
  226. else if (chSize == 253)
  227. {
  228. nSizeRet = ser_readdata16(is);
  229. if (nSizeRet < 253)
  230. throw std::ios_base::failure("non-canonical ReadCompactSize()");
  231. }
  232. else if (chSize == 254)
  233. {
  234. nSizeRet = ser_readdata32(is);
  235. if (nSizeRet < 0x10000u)
  236. throw std::ios_base::failure("non-canonical ReadCompactSize()");
  237. }
  238. else
  239. {
  240. nSizeRet = ser_readdata64(is);
  241. if (nSizeRet < 0x100000000ULL)
  242. throw std::ios_base::failure("non-canonical ReadCompactSize()");
  243. }
  244. if (nSizeRet > (uint64_t)MAX_SIZE)
  245. throw std::ios_base::failure("ReadCompactSize(): size too large");
  246. return nSizeRet;
  247. }
  248. /**
  249. * Variable-length integers: bytes are a MSB base-128 encoding of the number.
  250. * The high bit in each byte signifies whether another digit follows. To make
  251. * sure the encoding is one-to-one, one is subtracted from all but the last digit.
  252. * Thus, the byte sequence a[] with length len, where all but the last byte
  253. * has bit 128 set, encodes the number:
  254. *
  255. * (a[len-1] & 0x7F) + sum(i=1..len-1, 128^i*((a[len-i-1] & 0x7F)+1))
  256. *
  257. * Properties:
  258. * * Very small (0-127: 1 byte, 128-16511: 2 bytes, 16512-2113663: 3 bytes)
  259. * * Every integer has exactly one encoding
  260. * * Encoding does not depend on size of original integer type
  261. * * No redundancy: every (infinite) byte sequence corresponds to a list
  262. * of encoded integers.
  263. *
  264. * 0: [0x00] 256: [0x81 0x00]
  265. * 1: [0x01] 16383: [0xFE 0x7F]
  266. * 127: [0x7F] 16384: [0xFF 0x00]
  267. * 128: [0x80 0x00] 16511: [0xFF 0x7F]
  268. * 255: [0x80 0x7F] 65535: [0x82 0xFE 0x7F]
  269. * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00]
  270. */
  271. template<typename I>
  272. inline unsigned int GetSizeOfVarInt(I n)
  273. {
  274. int nRet = 0;
  275. while(true) {
  276. nRet++;
  277. if (n <= 0x7F)
  278. break;
  279. n = (n >> 7) - 1;
  280. }
  281. return nRet;
  282. }
  283. template<typename I>
  284. inline void WriteVarInt(CSizeComputer& os, I n);
  285. template<typename Stream, typename I>
  286. void WriteVarInt(Stream& os, I n)
  287. {
  288. unsigned char tmp[(sizeof(n)*8+6)/7];
  289. int len=0;
  290. while(true) {
  291. tmp[len] = (n & 0x7F) | (len ? 0x80 : 0x00);
  292. if (n <= 0x7F)
  293. break;
  294. n = (n >> 7) - 1;
  295. len++;
  296. }
  297. do {
  298. ser_writedata8(os, tmp[len]);
  299. } while(len--);
  300. }
  301. template<typename Stream, typename I>
  302. I ReadVarInt(Stream& is)
  303. {
  304. I n = 0;
  305. while(true) {
  306. unsigned char chData = ser_readdata8(is);
  307. if (n > (std::numeric_limits<I>::max() >> 7)) {
  308. throw std::ios_base::failure("ReadVarInt(): size too large");
  309. }
  310. n = (n << 7) | (chData & 0x7F);
  311. if (chData & 0x80) {
  312. if (n == std::numeric_limits<I>::max()) {
  313. throw std::ios_base::failure("ReadVarInt(): size too large");
  314. }
  315. n++;
  316. } else {
  317. return n;
  318. }
  319. }
  320. }
  321. #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
  322. #define VARINT(obj) REF(WrapVarInt(REF(obj)))
  323. #define COMPACTSIZE(obj) REF(CCompactSize(REF(obj)))
  324. #define LIMITED_STRING(obj,n) REF(LimitedString< n >(REF(obj)))
  325. /**
  326. * Wrapper for serializing arrays and POD.
  327. */
  328. class CFlatData
  329. {
  330. protected:
  331. char* pbegin;
  332. char* pend;
  333. public:
  334. CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
  335. template <class T, class TAl>
  336. explicit CFlatData(std::vector<T,TAl> &v)
  337. {
  338. pbegin = (char*)v.data();
  339. pend = (char*)(v.data() + v.size());
  340. }
  341. template <unsigned int N, typename T, typename S, typename D>
  342. explicit CFlatData(prevector<N, T, S, D> &v)
  343. {
  344. pbegin = (char*)v.data();
  345. pend = (char*)(v.data() + v.size());
  346. }
  347. char* begin() { return pbegin; }
  348. const char* begin() const { return pbegin; }
  349. char* end() { return pend; }
  350. const char* end() const { return pend; }
  351. template<typename Stream>
  352. void Serialize(Stream& s) const
  353. {
  354. s.write(pbegin, pend - pbegin);
  355. }
  356. template<typename Stream>
  357. void Unserialize(Stream& s)
  358. {
  359. s.read(pbegin, pend - pbegin);
  360. }
  361. };
  362. template<typename I>
  363. class CVarInt
  364. {
  365. protected:
  366. I &n;
  367. public:
  368. CVarInt(I& nIn) : n(nIn) { }
  369. template<typename Stream>
  370. void Serialize(Stream &s) const {
  371. WriteVarInt<Stream,I>(s, n);
  372. }
  373. template<typename Stream>
  374. void Unserialize(Stream& s) {
  375. n = ReadVarInt<Stream,I>(s);
  376. }
  377. };
  378. class CCompactSize
  379. {
  380. protected:
  381. uint64_t &n;
  382. public:
  383. CCompactSize(uint64_t& nIn) : n(nIn) { }
  384. template<typename Stream>
  385. void Serialize(Stream &s) const {
  386. WriteCompactSize<Stream>(s, n);
  387. }
  388. template<typename Stream>
  389. void Unserialize(Stream& s) {
  390. n = ReadCompactSize<Stream>(s);
  391. }
  392. };
  393. template<size_t Limit>
  394. class LimitedString
  395. {
  396. protected:
  397. std::string& string;
  398. public:
  399. LimitedString(std::string& _string) : string(_string) {}
  400. template<typename Stream>
  401. void Unserialize(Stream& s)
  402. {
  403. size_t size = ReadCompactSize(s);
  404. if (size > Limit) {
  405. throw std::ios_base::failure("String length limit exceeded");
  406. }
  407. string.resize(size);
  408. if (size != 0)
  409. s.read((char*)string.data(), size);
  410. }
  411. template<typename Stream>
  412. void Serialize(Stream& s) const
  413. {
  414. WriteCompactSize(s, string.size());
  415. if (!string.empty())
  416. s.write((char*)string.data(), string.size());
  417. }
  418. };
  419. template<typename I>
  420. CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
  421. /**
  422. * Forward declarations
  423. */
  424. /**
  425. * string
  426. */
  427. template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
  428. template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
  429. /**
  430. * prevector
  431. * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
  432. */
  433. template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
  434. template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
  435. template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
  436. template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
  437. template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
  438. template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
  439. /**
  440. * vector
  441. * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
  442. */
  443. template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
  444. template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
  445. template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
  446. template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
  447. template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
  448. template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
  449. /**
  450. * pair
  451. */
  452. template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
  453. template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
  454. /**
  455. * map
  456. */
  457. template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
  458. template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
  459. /**
  460. * set
  461. */
  462. template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
  463. template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
  464. /**
  465. * shared_ptr
  466. */
  467. template<typename Stream, typename T> void Serialize(Stream& os, const std::shared_ptr<const T>& p);
  468. template<typename Stream, typename T> void Unserialize(Stream& os, std::shared_ptr<const T>& p);
  469. /**
  470. * unique_ptr
  471. */
  472. template<typename Stream, typename T> void Serialize(Stream& os, const std::unique_ptr<const T>& p);
  473. template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
  474. /**
  475. * If none of the specialized versions above matched, default to calling member function.
  476. */
  477. template<typename Stream, typename T>
  478. inline void Serialize(Stream& os, const T& a)
  479. {
  480. a.Serialize(os);
  481. }
  482. template<typename Stream, typename T>
  483. inline void Unserialize(Stream& is, T& a)
  484. {
  485. a.Unserialize(is);
  486. }
  487. /**
  488. * string
  489. */
  490. template<typename Stream, typename C>
  491. void Serialize(Stream& os, const std::basic_string<C>& str)
  492. {
  493. WriteCompactSize(os, str.size());
  494. if (!str.empty())
  495. os.write((char*)str.data(), str.size() * sizeof(C));
  496. }
  497. template<typename Stream, typename C>
  498. void Unserialize(Stream& is, std::basic_string<C>& str)
  499. {
  500. unsigned int nSize = ReadCompactSize(is);
  501. str.resize(nSize);
  502. if (nSize != 0)
  503. is.read((char*)str.data(), nSize * sizeof(C));
  504. }
  505. /**
  506. * prevector
  507. */
  508. template<typename Stream, unsigned int N, typename T>
  509. void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
  510. {
  511. WriteCompactSize(os, v.size());
  512. if (!v.empty())
  513. os.write((char*)v.data(), v.size() * sizeof(T));
  514. }
  515. template<typename Stream, unsigned int N, typename T, typename V>
  516. void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
  517. {
  518. WriteCompactSize(os, v.size());
  519. for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
  520. ::Serialize(os, (*vi));
  521. }
  522. template<typename Stream, unsigned int N, typename T>
  523. inline void Serialize(Stream& os, const prevector<N, T>& v)
  524. {
  525. Serialize_impl(os, v, T());
  526. }
  527. template<typename Stream, unsigned int N, typename T>
  528. void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
  529. {
  530. // Limit size per read so bogus size value won't cause out of memory
  531. v.clear();
  532. unsigned int nSize = ReadCompactSize(is);
  533. unsigned int i = 0;
  534. while (i < nSize)
  535. {
  536. unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
  537. v.resize(i + blk);
  538. is.read((char*)&v[i], blk * sizeof(T));
  539. i += blk;
  540. }
  541. }
  542. template<typename Stream, unsigned int N, typename T, typename V>
  543. void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
  544. {
  545. v.clear();
  546. unsigned int nSize = ReadCompactSize(is);
  547. unsigned int i = 0;
  548. unsigned int nMid = 0;
  549. while (nMid < nSize)
  550. {
  551. nMid += 5000000 / sizeof(T);
  552. if (nMid > nSize)
  553. nMid = nSize;
  554. v.resize(nMid);
  555. for (; i < nMid; i++)
  556. Unserialize(is, v[i]);
  557. }
  558. }
  559. template<typename Stream, unsigned int N, typename T>
  560. inline void Unserialize(Stream& is, prevector<N, T>& v)
  561. {
  562. Unserialize_impl(is, v, T());
  563. }
  564. /**
  565. * vector
  566. */
  567. template<typename Stream, typename T, typename A>
  568. void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
  569. {
  570. WriteCompactSize(os, v.size());
  571. if (!v.empty())
  572. os.write((char*)v.data(), v.size() * sizeof(T));
  573. }
  574. template<typename Stream, typename T, typename A, typename V>
  575. void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
  576. {
  577. WriteCompactSize(os, v.size());
  578. for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
  579. ::Serialize(os, (*vi));
  580. }
  581. template<typename Stream, typename T, typename A>
  582. inline void Serialize(Stream& os, const std::vector<T, A>& v)
  583. {
  584. Serialize_impl(os, v, T());
  585. }
  586. template<typename Stream, typename T, typename A>
  587. void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
  588. {
  589. // Limit size per read so bogus size value won't cause out of memory
  590. v.clear();
  591. unsigned int nSize = ReadCompactSize(is);
  592. unsigned int i = 0;
  593. while (i < nSize)
  594. {
  595. unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
  596. v.resize(i + blk);
  597. is.read((char*)&v[i], blk * sizeof(T));
  598. i += blk;
  599. }
  600. }
  601. template<typename Stream, typename T, typename A, typename V>
  602. void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
  603. {
  604. v.clear();
  605. unsigned int nSize = ReadCompactSize(is);
  606. unsigned int i = 0;
  607. unsigned int nMid = 0;
  608. while (nMid < nSize)
  609. {
  610. nMid += 5000000 / sizeof(T);
  611. if (nMid > nSize)
  612. nMid = nSize;
  613. v.resize(nMid);
  614. for (; i < nMid; i++)
  615. Unserialize(is, v[i]);
  616. }
  617. }
  618. template<typename Stream, typename T, typename A>
  619. inline void Unserialize(Stream& is, std::vector<T, A>& v)
  620. {
  621. Unserialize_impl(is, v, T());
  622. }
  623. /**
  624. * pair
  625. */
  626. template<typename Stream, typename K, typename T>
  627. void Serialize(Stream& os, const std::pair<K, T>& item)
  628. {
  629. Serialize(os, item.first);
  630. Serialize(os, item.second);
  631. }
  632. template<typename Stream, typename K, typename T>
  633. void Unserialize(Stream& is, std::pair<K, T>& item)
  634. {
  635. Unserialize(is, item.first);
  636. Unserialize(is, item.second);
  637. }
  638. /**
  639. * map
  640. */
  641. template<typename Stream, typename K, typename T, typename Pred, typename A>
  642. void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
  643. {
  644. WriteCompactSize(os, m.size());
  645. for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
  646. Serialize(os, (*mi));
  647. }
  648. template<typename Stream, typename K, typename T, typename Pred, typename A>
  649. void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
  650. {
  651. m.clear();
  652. unsigned int nSize = ReadCompactSize(is);
  653. typename std::map<K, T, Pred, A>::iterator mi = m.begin();
  654. for (unsigned int i = 0; i < nSize; i++)
  655. {
  656. std::pair<K, T> item;
  657. Unserialize(is, item);
  658. mi = m.insert(mi, item);
  659. }
  660. }
  661. /**
  662. * set
  663. */
  664. template<typename Stream, typename K, typename Pred, typename A>
  665. void Serialize(Stream& os, const std::set<K, Pred, A>& m)
  666. {
  667. WriteCompactSize(os, m.size());
  668. for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
  669. Serialize(os, (*it));
  670. }
  671. template<typename Stream, typename K, typename Pred, typename A>
  672. void Unserialize(Stream& is, std::set<K, Pred, A>& m)
  673. {
  674. m.clear();
  675. unsigned int nSize = ReadCompactSize(is);
  676. typename std::set<K, Pred, A>::iterator it = m.begin();
  677. for (unsigned int i = 0; i < nSize; i++)
  678. {
  679. K key;
  680. Unserialize(is, key);
  681. it = m.insert(it, key);
  682. }
  683. }
  684. /**
  685. * unique_ptr
  686. */
  687. template<typename Stream, typename T> void
  688. Serialize(Stream& os, const std::unique_ptr<const T>& p)
  689. {
  690. Serialize(os, *p);
  691. }
  692. template<typename Stream, typename T>
  693. void Unserialize(Stream& is, std::unique_ptr<const T>& p)
  694. {
  695. p.reset(new T(deserialize, is));
  696. }
  697. /**
  698. * shared_ptr
  699. */
  700. template<typename Stream, typename T> void
  701. Serialize(Stream& os, const std::shared_ptr<const T>& p)
  702. {
  703. Serialize(os, *p);
  704. }
  705. template<typename Stream, typename T>
  706. void Unserialize(Stream& is, std::shared_ptr<const T>& p)
  707. {
  708. p = std::make_shared<const T>(deserialize, is);
  709. }
  710. /**
  711. * Support for ADD_SERIALIZE_METHODS and READWRITE macro
  712. */
  713. struct CSerActionSerialize
  714. {
  715. constexpr bool ForRead() const { return false; }
  716. };
  717. struct CSerActionUnserialize
  718. {
  719. constexpr bool ForRead() const { return true; }
  720. };
  721. template<typename Stream, typename T>
  722. inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action)
  723. {
  724. ::Serialize(s, obj);
  725. }
  726. template<typename Stream, typename T>
  727. inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action)
  728. {
  729. ::Unserialize(s, obj);
  730. }
  731. /* ::GetSerializeSize implementations
  732. *
  733. * Computing the serialized size of objects is done through a special stream
  734. * object of type CSizeComputer, which only records the number of bytes written
  735. * to it.
  736. *
  737. * If your Serialize or SerializationOp method has non-trivial overhead for
  738. * serialization, it may be worthwhile to implement a specialized version for
  739. * CSizeComputer, which uses the s.seek() method to record bytes that would
  740. * be written instead.
  741. */
  742. class CSizeComputer
  743. {
  744. protected:
  745. size_t nSize;
  746. const int nType;
  747. const int nVersion;
  748. public:
  749. CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
  750. void write(const char *psz, size_t _nSize)
  751. {
  752. this->nSize += _nSize;
  753. }
  754. /** Pretend _nSize bytes are written, without specifying them. */
  755. void seek(size_t _nSize)
  756. {
  757. this->nSize += _nSize;
  758. }
  759. template<typename T>
  760. CSizeComputer& operator<<(const T& obj)
  761. {
  762. ::Serialize(*this, obj);
  763. return (*this);
  764. }
  765. size_t size() const {
  766. return nSize;
  767. }
  768. int GetVersion() const { return nVersion; }
  769. int GetType() const { return nType; }
  770. };
  771. template<typename Stream>
  772. void SerializeMany(Stream& s)
  773. {
  774. }
  775. template<typename Stream, typename Arg>
  776. void SerializeMany(Stream& s, Arg&& arg)
  777. {
  778. ::Serialize(s, std::forward<Arg>(arg));
  779. }
  780. template<typename Stream, typename Arg, typename... Args>
  781. void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
  782. {
  783. ::Serialize(s, std::forward<Arg>(arg));
  784. ::SerializeMany(s, std::forward<Args>(args)...);
  785. }
  786. template<typename Stream>
  787. inline void UnserializeMany(Stream& s)
  788. {
  789. }
  790. template<typename Stream, typename Arg>
  791. inline void UnserializeMany(Stream& s, Arg& arg)
  792. {
  793. ::Unserialize(s, arg);
  794. }
  795. template<typename Stream, typename Arg, typename... Args>
  796. inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
  797. {
  798. ::Unserialize(s, arg);
  799. ::UnserializeMany(s, args...);
  800. }
  801. template<typename Stream, typename... Args>
  802. inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
  803. {
  804. ::SerializeMany(s, std::forward<Args>(args)...);
  805. }
  806. template<typename Stream, typename... Args>
  807. inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
  808. {
  809. ::UnserializeMany(s, args...);
  810. }
  811. template<typename I>
  812. inline void WriteVarInt(CSizeComputer &s, I n)
  813. {
  814. s.seek(GetSizeOfVarInt<I>(n));
  815. }
  816. inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
  817. {
  818. s.seek(GetSizeOfCompactSize(nSize));
  819. }
  820. template <typename T>
  821. size_t GetSerializeSize(const T& t, int nType, int nVersion = 0)
  822. {
  823. return (CSizeComputer(nType, nVersion) << t).size();
  824. }
  825. template <typename S, typename T>
  826. size_t GetSerializeSize(const S& s, const T& t)
  827. {
  828. return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
  829. }
  830. #endif // STARWELS_SERIALIZE_H