Raven Core  3.0.0
P2P Digital Currency
streams.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_STREAMS_H
8 #define RAVEN_STREAMS_H
9 
11 #include "serialize.h"
12 
13 #include <algorithm>
14 #include <assert.h>
15 #include <ios>
16 #include <limits>
17 #include <map>
18 #include <set>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string>
22 #include <string.h>
23 #include <utility>
24 #include <vector>
25 
26 template<typename Stream>
28 {
29  Stream* stream;
30 
31  const int nType;
32  const int nVersion;
33 
34 public:
35  OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
36 
37  template<typename T>
39  {
40  // Serialize to this stream
41  ::Serialize(*this, obj);
42  return (*this);
43  }
44 
45  template<typename T>
47  {
48  // Unserialize from this stream
49  ::Unserialize(*this, obj);
50  return (*this);
51  }
52 
53  void write(const char* pch, size_t nSize)
54  {
55  stream->write(pch, nSize);
56  }
57 
58  void read(char* pch, size_t nSize)
59  {
60  stream->read(pch, nSize);
61  }
62 
63  int GetVersion() const { return nVersion; }
64  int GetType() const { return nType; }
65 };
66 
67 template<typename S>
68 OverrideStream<S> WithOrVersion(S* s, int nVersionFlag)
69 {
70  return OverrideStream<S>(s, s->GetType(), s->GetVersion() | nVersionFlag);
71 }
72 
73 /* Minimal stream for overwriting and/or appending to an existing byte vector
74  *
75  * The referenced vector will grow as necessary
76  */
78 {
79  public:
80 
81 /*
82  * @param[in] nTypeIn Serialization Type
83  * @param[in] nVersionIn Serialization Version (including any flags)
84  * @param[in] vchDataIn Referenced byte vector to overwrite/append
85  * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
86  * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
87 */
88  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
89  {
90  if(nPos > vchData.size())
91  vchData.resize(nPos);
92  }
93 /*
94  * (other params same as above)
95  * @param[in] args A list of items to serialize starting at nPosIn.
96 */
97  template <typename... Args>
98  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
99  {
100  ::SerializeMany(*this, std::forward<Args>(args)...);
101  }
102  void write(const char* pch, size_t nSize)
103  {
104  assert(nPos <= vchData.size());
105  size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
106  if (nOverwrite) {
107  memcpy(vchData.data() + nPos, reinterpret_cast<const unsigned char*>(pch), nOverwrite);
108  }
109  if (nOverwrite < nSize) {
110  vchData.insert(vchData.end(), reinterpret_cast<const unsigned char*>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
111  }
112  nPos += nSize;
113  }
114  template<typename T>
115  CVectorWriter& operator<<(const T& obj)
116  {
117  // Serialize to this stream
118  ::Serialize(*this, obj);
119  return (*this);
120  }
121  int GetVersion() const
122  {
123  return nVersion;
124  }
125  int GetType() const
126  {
127  return nType;
128  }
129  void seek(size_t nSize)
130  {
131  nPos += nSize;
132  if(nPos > vchData.size())
133  vchData.resize(nPos);
134  }
135 private:
136  const int nType;
137  const int nVersion;
138  std::vector<unsigned char>& vchData;
139  size_t nPos;
140 };
141 
148 {
149 protected:
151  vector_type vch;
152  unsigned int nReadPos;
153 
154  int nType;
155  int nVersion;
156 public:
157 
158  typedef vector_type::allocator_type allocator_type;
159  typedef vector_type::size_type size_type;
160  typedef vector_type::difference_type difference_type;
161  typedef vector_type::reference reference;
162  typedef vector_type::const_reference const_reference;
163  typedef vector_type::value_type value_type;
164  typedef vector_type::iterator iterator;
165  typedef vector_type::const_iterator const_iterator;
166  typedef vector_type::reverse_iterator reverse_iterator;
167 
168  explicit CDataStream(int nTypeIn, int nVersionIn)
169  {
170  Init(nTypeIn, nVersionIn);
171  }
172 
173  CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
174  {
175  Init(nTypeIn, nVersionIn);
176  }
177 
178  CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
179  {
180  Init(nTypeIn, nVersionIn);
181  }
182 
183  CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
184  {
185  Init(nTypeIn, nVersionIn);
186  }
187 
188  CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
189  {
190  Init(nTypeIn, nVersionIn);
191  }
192 
193  CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
194  {
195  Init(nTypeIn, nVersionIn);
196  }
197 
198  template <typename... Args>
199  CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
200  {
201  Init(nTypeIn, nVersionIn);
202  ::SerializeMany(*this, std::forward<Args>(args)...);
203  }
204 
205  void Init(int nTypeIn, int nVersionIn)
206  {
207  nReadPos = 0;
208  nType = nTypeIn;
209  nVersion = nVersionIn;
210  }
211 
213  {
214  vch.insert(vch.end(), b.begin(), b.end());
215  return *this;
216  }
217 
218  friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
219  {
220  CDataStream ret = a;
221  ret += b;
222  return (ret);
223  }
224 
225  std::string str() const
226  {
227  return (std::string(begin(), end()));
228  }
229 
230 
231  //
232  // Vector subset
233  //
234  const_iterator begin() const { return vch.begin() + nReadPos; }
235  iterator begin() { return vch.begin() + nReadPos; }
236  const_iterator end() const { return vch.end(); }
237  iterator end() { return vch.end(); }
238  size_type size() const { return vch.size() - nReadPos; }
239  bool empty() const { return vch.size() == nReadPos; }
240  void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
241  void reserve(size_type n) { vch.reserve(n + nReadPos); }
242  const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
243  reference operator[](size_type pos) { return vch[pos + nReadPos]; }
244  void clear() { vch.clear(); nReadPos = 0; }
245  iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
246  void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
247  value_type* data() { return vch.data() + nReadPos; }
248  const value_type* data() const { return vch.data() + nReadPos; }
249 
250  void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
251  {
252  if (last == first) return;
253  assert(last - first > 0);
254  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
255  {
256  // special case for inserting at the front when there's room
257  nReadPos -= (last - first);
258  memcpy(&vch[nReadPos], &first[0], last - first);
259  }
260  else
261  vch.insert(it, first, last);
262  }
263 
264  void insert(iterator it, const char* first, const char* last)
265  {
266  if (last == first) return;
267  assert(last - first > 0);
268  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
269  {
270  // special case for inserting at the front when there's room
271  nReadPos -= (last - first);
272  memcpy(&vch[nReadPos], &first[0], last - first);
273  }
274  else
275  vch.insert(it, first, last);
276  }
277 
278  iterator erase(iterator it)
279  {
280  if (it == vch.begin() + nReadPos)
281  {
282  // special case for erasing from the front
283  if (++nReadPos >= vch.size())
284  {
285  // whenever we reach the end, we take the opportunity to clear the buffer
286  nReadPos = 0;
287  return vch.erase(vch.begin(), vch.end());
288  }
289  return vch.begin() + nReadPos;
290  }
291  else
292  return vch.erase(it);
293  }
294 
295  iterator erase(iterator first, iterator last)
296  {
297  if (first == vch.begin() + nReadPos)
298  {
299  // special case for erasing from the front
300  if (last == vch.end())
301  {
302  nReadPos = 0;
303  return vch.erase(vch.begin(), vch.end());
304  }
305  else
306  {
307  nReadPos = (last - vch.begin());
308  return last;
309  }
310  }
311  else
312  return vch.erase(first, last);
313  }
314 
315  inline void Compact()
316  {
317  vch.erase(vch.begin(), vch.begin() + nReadPos);
318  nReadPos = 0;
319  }
320 
321  bool Rewind(size_type n)
322  {
323  // Rewind by n characters if the buffer hasn't been compacted yet
324  if (n > nReadPos)
325  return false;
326  nReadPos -= n;
327  return true;
328  }
329 
330 
331  //
332  // Stream subset
333  //
334  bool eof() const { return size() == 0; }
335  CDataStream* rdbuf() { return this; }
336  int in_avail() const { return size(); }
337 
338  void SetType(int n) { nType = n; }
339  int GetType() const { return nType; }
340  void SetVersion(int n) { nVersion = n; }
341  int GetVersion() const { return nVersion; }
342 
343  void read(char* pch, size_t nSize)
344  {
345  if (nSize == 0) return;
346 
347  // Read from the beginning of the buffer
348  unsigned int nReadPosNext = nReadPos + nSize;
349  if (nReadPosNext >= vch.size())
350  {
351  if (nReadPosNext > vch.size())
352  {
353  throw std::ios_base::failure("CDataStream::read(): end of data");
354  }
355  memcpy(pch, &vch[nReadPos], nSize);
356  nReadPos = 0;
357  vch.clear();
358  return;
359  }
360  memcpy(pch, &vch[nReadPos], nSize);
361  nReadPos = nReadPosNext;
362  }
363 
364  void ignore(int nSize)
365  {
366  // Ignore from the beginning of the buffer
367  if (nSize < 0) {
368  throw std::ios_base::failure("CDataStream::ignore(): nSize negative");
369  }
370  unsigned int nReadPosNext = nReadPos + nSize;
371  if (nReadPosNext >= vch.size())
372  {
373  if (nReadPosNext > vch.size())
374  throw std::ios_base::failure("CDataStream::ignore(): end of data");
375  nReadPos = 0;
376  vch.clear();
377  return;
378  }
379  nReadPos = nReadPosNext;
380  }
381 
382  void write(const char* pch, size_t nSize)
383  {
384  // Write to the end of the buffer
385  vch.insert(vch.end(), pch, pch + nSize);
386  }
387 
388  template<typename Stream>
389  void Serialize(Stream& s) const
390  {
391  // Special case: stream << stream concatenates like stream += stream
392  if (!vch.empty())
393  s.write((char*)vch.data(), vch.size() * sizeof(value_type));
394  }
395 
396  template<typename T>
397  CDataStream& operator<<(const T& obj)
398  {
399  // Serialize to this stream
400  ::Serialize(*this, obj);
401  return (*this);
402  }
403 
404  template<typename T>
406  {
407  // Unserialize from this stream
408  ::Unserialize(*this, obj);
409  return (*this);
410  }
411 
413  d.insert(d.end(), begin(), end());
414  clear();
415  }
416 
422  void Xor(const std::vector<unsigned char>& key)
423  {
424  if (key.size() == 0) {
425  return;
426  }
427 
428  for (size_type i = 0, j = 0; i != size(); i++) {
429  vch[i] ^= key[j++];
430 
431  // This potentially acts on very many bytes of data, so it's
432  // important that we calculate `j`, i.e. the `key` index in this
433  // way instead of doing a %, which would effectively be a division
434  // for each byte Xor'd -- much slower than need be.
435  if (j == key.size())
436  j = 0;
437  }
438  }
439 };
440 
441 
442 
443 
444 
445 
446 
447 
448 
449 
457 {
458 private:
459  const int nType;
460  const int nVersion;
461 
462  FILE* file;
463 
464 public:
465  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
466  {
467  file = filenew;
468  }
469 
471  {
472  fclose();
473  }
474 
475  // Disallow copies
476  CAutoFile(const CAutoFile&) = delete;
477  CAutoFile& operator=(const CAutoFile&) = delete;
478 
479  void fclose()
480  {
481  if (file) {
482  ::fclose(file);
483  file = nullptr;
484  }
485  }
486 
491  FILE* release() { FILE* ret = file; file = nullptr; return ret; }
492 
497  FILE* Get() const { return file; }
498 
501  bool IsNull() const { return (file == nullptr); }
502 
503  //
504  // Stream subset
505  //
506  int GetType() const { return nType; }
507  int GetVersion() const { return nVersion; }
508 
509  void read(char* pch, size_t nSize)
510  {
511  if (!file)
512  throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
513  if (fread(pch, 1, nSize, file) != nSize)
514  throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
515  }
516 
517  void ignore(size_t nSize)
518  {
519  if (!file)
520  throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
521  unsigned char data[4096];
522  while (nSize > 0) {
523  size_t nNow = std::min<size_t>(nSize, sizeof(data));
524  if (fread(data, 1, nNow, file) != nNow)
525  throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
526  nSize -= nNow;
527  }
528  }
529 
530  void write(const char* pch, size_t nSize)
531  {
532  if (!file)
533  throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
534  if (fwrite(pch, 1, nSize, file) != nSize)
535  throw std::ios_base::failure("CAutoFile::write: write failed");
536  }
537 
538  template<typename T>
539  CAutoFile& operator<<(const T& obj)
540  {
541  // Serialize to this stream
542  if (!file)
543  throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
544  ::Serialize(*this, obj);
545  return (*this);
546  }
547 
548  template<typename T>
550  {
551  // Unserialize from this stream
552  if (!file)
553  throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
554  ::Unserialize(*this, obj);
555  return (*this);
556  }
557 };
558 
566 {
567 private:
568  const int nType;
569  const int nVersion;
570 
571  FILE *src; // source file
572  uint64_t nSrcPos; // how many bytes have been read from source
573  uint64_t nReadPos; // how many bytes have been read from this
574  uint64_t nReadLimit; // up to which position we're allowed to read
575  uint64_t nRewind; // how many bytes we guarantee to rewind
576  std::vector<char> vchBuf; // the buffer
577 
578 protected:
579  // read data from the source to fill the buffer
580  bool Fill() {
581  unsigned int pos = nSrcPos % vchBuf.size();
582  unsigned int readNow = vchBuf.size() - pos;
583  unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
584  if (nAvail < readNow)
585  readNow = nAvail;
586  if (readNow == 0)
587  return false;
588  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
589  if (nBytes == 0) {
590  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
591  } else {
592  nSrcPos += nBytes;
593  return true;
594  }
595  }
596 
597 public:
598  CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
599  nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
600  {
601  src = fileIn;
602  }
603 
605  {
606  fclose();
607  }
608 
609  // Disallow copies
610  CBufferedFile(const CBufferedFile&) = delete;
611  CBufferedFile& operator=(const CBufferedFile&) = delete;
612 
613  int GetVersion() const { return nVersion; }
614  int GetType() const { return nType; }
615 
616  void fclose()
617  {
618  if (src) {
619  ::fclose(src);
620  src = nullptr;
621  }
622  }
623 
624  // check whether we're at the end of the source file
625  bool eof() const {
626  return nReadPos == nSrcPos && feof(src);
627  }
628 
629  // read a number of bytes
630  void read(char *pch, size_t nSize) {
631  if (nSize + nReadPos > nReadLimit)
632  throw std::ios_base::failure("Read attempted past buffer limit");
633  if (nSize + nRewind > vchBuf.size())
634  throw std::ios_base::failure("Read larger than buffer size");
635  while (nSize > 0) {
636  if (nReadPos == nSrcPos)
637  Fill();
638  unsigned int pos = nReadPos % vchBuf.size();
639  size_t nNow = nSize;
640  if (nNow + pos > vchBuf.size())
641  nNow = vchBuf.size() - pos;
642  if (nNow + nReadPos > nSrcPos)
643  nNow = nSrcPos - nReadPos;
644  memcpy(pch, &vchBuf[pos], nNow);
645  nReadPos += nNow;
646  pch += nNow;
647  nSize -= nNow;
648  }
649  }
650 
651  // return the current reading position
652  uint64_t GetPos() const {
653  return nReadPos;
654  }
655 
656  // rewind to a given reading position
657  bool SetPos(uint64_t nPos) {
658  nReadPos = nPos;
659  if (nReadPos + nRewind < nSrcPos) {
660  nReadPos = nSrcPos - nRewind;
661  return false;
662  } else if (nReadPos > nSrcPos) {
663  nReadPos = nSrcPos;
664  return false;
665  } else {
666  return true;
667  }
668  }
669 
670  bool Seek(uint64_t nPos) {
671  long nLongPos = nPos;
672  if (nPos != (uint64_t)nLongPos)
673  return false;
674  if (fseek(src, nLongPos, SEEK_SET))
675  return false;
676  nLongPos = ftell(src);
677  nSrcPos = nLongPos;
678  nReadPos = nLongPos;
679  return true;
680  }
681 
682  // prevent reading beyond a certain position
683  // no argument removes the limit
684  bool SetLimit(uint64_t nPos = (uint64_t)(-1)) {
685  if (nPos < nReadPos)
686  return false;
687  nReadLimit = nPos;
688  return true;
689  }
690 
691  template<typename T>
693  // Unserialize from this stream
694  ::Unserialize(*this, obj);
695  return (*this);
696  }
697 
698  // search for a given byte in the stream, and remain positioned on it
699  void FindByte(char ch) {
700  while (true) {
701  if (nReadPos == nSrcPos)
702  Fill();
703  if (vchBuf[nReadPos % vchBuf.size()] == ch)
704  break;
705  nReadPos++;
706  }
707  }
708 };
709 
710 #endif // RAVEN_STREAMS_H
const int nType
Definition: streams.h:568
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:115
void Init(int nTypeIn, int nVersionIn)
Definition: streams.h:205
CSerializeData vector_type
Definition: streams.h:150
void ignore(size_t nSize)
Definition: streams.h:517
uint64_t nReadPos
Definition: streams.h:573
vector_type::iterator iterator
Definition: streams.h:164
vector_type::allocator_type allocator_type
Definition: streams.h:158
vector_type vch
Definition: streams.h:151
std::vector< unsigned char > & vchData
Definition: streams.h:138
void Compact()
Definition: streams.h:315
int GetVersion() const
Definition: streams.h:341
CAutoFile & operator>>(T &obj)
Definition: streams.h:549
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
Definition: streams.h:173
unsigned int nReadPos
Definition: streams.h:152
size_t nPos
Definition: streams.h:139
int GetType() const
Definition: streams.h:339
int GetType() const
Definition: streams.h:125
CDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:193
void write(const char *pch, size_t nSize)
Definition: streams.h:102
const int nVersion
Definition: streams.h:460
vector_type::size_type size_type
Definition: streams.h:159
std::string str() const
Definition: streams.h:225
const int nType
Definition: streams.h:31
void resize(size_type n, value_type c=0)
Definition: streams.h:240
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:183
void Serialize(Stream &s) const
Definition: streams.h:389
vector_type::reference reference
Definition: streams.h:161
int GetType() const
Definition: streams.h:614
vector_type::value_type value_type
Definition: streams.h:163
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:422
value_type * data()
Definition: streams.h:247
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:38
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
void write(const char *pch, size_t nSize)
Definition: streams.h:382
bool empty() const
Definition: streams.h:239
OverrideStream< S > WithOrVersion(S *s, int nVersionFlag)
Definition: streams.h:68
int GetVersion() const
Definition: streams.h:63
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:166
int GetVersion() const
Definition: streams.h:507
iterator erase(iterator it)
Definition: streams.h:278
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:465
iterator end()
Definition: streams.h:237
const int nVersion
Definition: streams.h:569
friend CDataStream operator+(const CDataStream &a, const CDataStream &b)
Definition: streams.h:218
CDataStream * rdbuf()
Definition: streams.h:335
~CAutoFile()
Definition: streams.h:470
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:168
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
Definition: streams.h:250
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:501
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:491
void write(const char *pch, size_t nSize)
Definition: streams.h:530
void Serialize(Stream &s, char a)
Definition: serialize.h:182
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:98
void read(char *pch, size_t nSize)
Definition: streams.h:343
vector_type::const_reference const_reference
Definition: streams.h:162
void read(char *pch, size_t nSize)
Definition: streams.h:58
bool SetLimit(uint64_t nPos=(uint64_t)(-1))
Definition: streams.h:684
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:199
void fclose()
Definition: streams.h:479
uint64_t GetPos() const
Definition: streams.h:652
const int nType
Definition: streams.h:136
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:188
uint64_t nReadLimit
Definition: streams.h:574
size_type size() const
Definition: streams.h:238
vector_type::const_iterator const_iterator
Definition: streams.h:165
void seek(size_t nSize)
Definition: streams.h:129
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:598
void ignore(int nSize)
Definition: streams.h:364
const value_type * data() const
Definition: streams.h:248
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
Definition: streams.h:178
CDataStream & operator+=(const CDataStream &b)
Definition: streams.h:212
CDataStream & operator>>(T &obj)
Definition: streams.h:405
void GetAndClear(CSerializeData &d)
Definition: streams.h:412
#define S(x0, x1, x2, x3, cb, r)
Definition: jh.c:494
iterator begin()
Definition: streams.h:235
void write(const char *pch, size_t nSize)
Definition: streams.h:53
int GetVersion() const
Definition: streams.h:121
void read(char *pch, size_t nSize)
Definition: streams.h:509
void read(char *pch, size_t nSize)
Definition: streams.h:630
reference operator[](size_type pos)
Definition: streams.h:243
bool SetPos(uint64_t nPos)
Definition: streams.h:657
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:35
bool Rewind(size_type n)
Definition: streams.h:321
void SerializeMany(Stream &s)
Definition: serialize.h:908
const_reference operator[](size_type pos) const
Definition: streams.h:242
int GetType() const
Definition: streams.h:506
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:497
void fclose()
Definition: streams.h:616
const int nVersion
Definition: streams.h:137
const_iterator end() const
Definition: streams.h:236
const_iterator begin() const
Definition: streams.h:234
const int nVersion
Definition: streams.h:32
iterator insert(iterator it, const char &x=char())
Definition: streams.h:245
CDataStream & operator<<(const T &obj)
Definition: streams.h:397
void FindByte(char ch)
Definition: streams.h:699
void reserve(size_type n)
Definition: streams.h:241
void * memcpy(void *a, const void *b, size_t c)
void insert(iterator it, size_type n, const char &x)
Definition: streams.h:246
int nVersion
Definition: streams.h:155
FILE * src
Definition: streams.h:571
void Unserialize(Stream &s, char &a)
Definition: serialize.h:194
vector_type::difference_type difference_type
Definition: streams.h:160
void insert(iterator it, const char *first, const char *last)
Definition: streams.h:264
void clear()
Definition: streams.h:244
uint64_t nSrcPos
Definition: streams.h:572
bool Fill()
Definition: streams.h:580
std::vector< char, zero_after_free_allocator< char > > CSerializeData
Definition: zeroafterfree.h:47
FILE * file
Definition: streams.h:462
std::vector< char > vchBuf
Definition: streams.h:576
iterator erase(iterator first, iterator last)
Definition: streams.h:295
uint64_t nRewind
Definition: streams.h:575
int GetVersion() const
Definition: streams.h:613
const int nType
Definition: streams.h:459
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from...
Definition: streams.h:565
void SetVersion(int n)
Definition: streams.h:340
CBufferedFile & operator>>(T &obj)
Definition: streams.h:692
int nType
Definition: streams.h:154
bool Seek(uint64_t nPos)
Definition: streams.h:670
Stream * stream
Definition: streams.h:29
bool eof() const
Definition: streams.h:334
CAutoFile & operator<<(const T &obj)
Definition: streams.h:539
int GetType() const
Definition: streams.h:64
~CBufferedFile()
Definition: streams.h:604
int in_avail() const
Definition: streams.h:336
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:456
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:88
OverrideStream< Stream > & operator>>(T &obj)
Definition: streams.h:46
void SetType(int n)
Definition: streams.h:338
bool eof() const
Definition: streams.h:625