Bitcoin ABC  0.22.13
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 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_STREAMS_H
7 #define BITCOIN_STREAMS_H
8 
9 #include <serialize.h>
11 
12 #include <algorithm>
13 #include <cassert>
14 #include <cstdint>
15 #include <cstdio>
16 #include <cstring>
17 #include <ios>
18 #include <limits>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 template <typename Stream> class OverrideStream {
24  Stream *stream;
25 
26  const int nType;
27  const int nVersion;
28 
29 public:
30  OverrideStream(Stream *stream_, int nType_, int nVersion_)
31  : stream(stream_), nType(nType_), nVersion(nVersion_) {}
32 
33  template <typename T> OverrideStream<Stream> &operator<<(const T &obj) {
34  // Serialize to this stream
35  ::Serialize(*this, obj);
36  return (*this);
37  }
38 
39  template <typename T> OverrideStream<Stream> &operator>>(T &&obj) {
40  // Unserialize from this stream
41  ::Unserialize(*this, obj);
42  return (*this);
43  }
44 
45  void write(const char *pch, size_t nSize) { stream->write(pch, nSize); }
46 
47  void read(char *pch, size_t nSize) { stream->read(pch, nSize); }
48 
49  int GetVersion() const { return nVersion; }
50  int GetType() const { return nType; }
51 };
52 
53 template <typename S> OverrideStream<S> WithOrVersion(S *s, int nVersionFlag) {
54  return OverrideStream<S>(s, s->GetType(), s->GetVersion() | nVersionFlag);
55 }
56 
63 public:
72  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<uint8_t> &vchDataIn,
73  size_t nPosIn)
74  : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn),
75  nPos(nPosIn) {
76  if (nPos > vchData.size()) {
77  vchData.resize(nPos);
78  }
79  }
84  template <typename... Args>
85  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<uint8_t> &vchDataIn,
86  size_t nPosIn, Args &&... args)
87  : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn) {
88  ::SerializeMany(*this, std::forward<Args>(args)...);
89  }
90  void write(const char *pch, size_t nSize) {
91  assert(nPos <= vchData.size());
92  size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
93  if (nOverwrite) {
94  memcpy(vchData.data() + nPos,
95  reinterpret_cast<const uint8_t *>(pch), nOverwrite);
96  }
97  if (nOverwrite < nSize) {
98  vchData.insert(vchData.end(),
99  reinterpret_cast<const uint8_t *>(pch) + nOverwrite,
100  reinterpret_cast<const uint8_t *>(pch) + nSize);
101  }
102  nPos += nSize;
103  }
104  template <typename T> CVectorWriter &operator<<(const T &obj) {
105  // Serialize to this stream
106  ::Serialize(*this, obj);
107  return (*this);
108  }
109  int GetVersion() const { return nVersion; }
110  int GetType() const { return nType; }
111  void seek(size_t nSize) {
112  nPos += nSize;
113  if (nPos > vchData.size()) {
114  vchData.resize(nPos);
115  }
116  }
117 
118 private:
119  const int nType;
120  const int nVersion;
121  std::vector<uint8_t> &vchData;
122  size_t nPos;
123 };
124 
129 private:
130  const int m_type;
131  const int m_version;
132  const std::vector<uint8_t> &m_data;
133  size_t m_pos = 0;
134 
135 public:
142  VectorReader(int type, int version, const std::vector<uint8_t> &data,
143  size_t pos)
144  : m_type(type), m_version(version), m_data(data), m_pos(pos) {
145  if (m_pos > m_data.size()) {
146  throw std::ios_base::failure(
147  "VectorReader(...): end of data (m_pos > m_data.size())");
148  }
149  }
150 
155  template <typename... Args>
156  VectorReader(int type, int version, const std::vector<uint8_t> &data,
157  size_t pos, Args &&... args)
158  : VectorReader(type, version, data, pos) {
159  ::UnserializeMany(*this, std::forward<Args>(args)...);
160  }
161 
162  template <typename T> VectorReader &operator>>(T &obj) {
163  // Unserialize from this stream
164  ::Unserialize(*this, obj);
165  return (*this);
166  }
167 
168  int GetVersion() const { return m_version; }
169  int GetType() const { return m_type; }
170 
171  size_t size() const { return m_data.size() - m_pos; }
172  bool empty() const { return m_data.size() == m_pos; }
173 
174  void read(char *dst, size_t n) {
175  if (n == 0) {
176  return;
177  }
178 
179  // Read from the beginning of the buffer
180  size_t pos_next = m_pos + n;
181  if (pos_next > m_data.size()) {
182  throw std::ios_base::failure("VectorReader::read(): end of data");
183  }
184  memcpy(dst, m_data.data() + m_pos, n);
185  m_pos = pos_next;
186  }
187 };
188 
196 class CDataStream {
197 protected:
199  vector_type vch;
200  unsigned int nReadPos;
201 
202  int nType;
203  int nVersion;
204 
205 public:
206  typedef vector_type::allocator_type allocator_type;
207  typedef vector_type::size_type size_type;
208  typedef vector_type::difference_type difference_type;
209  typedef vector_type::reference reference;
210  typedef vector_type::const_reference const_reference;
211  typedef vector_type::value_type value_type;
212  typedef vector_type::iterator iterator;
213  typedef vector_type::const_iterator const_iterator;
214  typedef vector_type::reverse_iterator reverse_iterator;
215 
216  explicit CDataStream(int nTypeIn, int nVersionIn) {
217  Init(nTypeIn, nVersionIn);
218  }
219 
220  CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn,
221  int nVersionIn)
222  : vch(pbegin, pend) {
223  Init(nTypeIn, nVersionIn);
224  }
225 
226  CDataStream(const char *pbegin, const char *pend, int nTypeIn,
227  int nVersionIn)
228  : vch(pbegin, pend) {
229  Init(nTypeIn, nVersionIn);
230  }
231 
232  CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
233  : vch(vchIn.begin(), vchIn.end()) {
234  Init(nTypeIn, nVersionIn);
235  }
236 
237  CDataStream(const std::vector<char> &vchIn, int nTypeIn, int nVersionIn)
238  : vch(vchIn.begin(), vchIn.end()) {
239  Init(nTypeIn, nVersionIn);
240  }
241 
242  CDataStream(const std::vector<uint8_t> &vchIn, int nTypeIn, int nVersionIn)
243  : vch(vchIn.begin(), vchIn.end()) {
244  Init(nTypeIn, nVersionIn);
245  }
246 
247  template <typename... Args>
248  CDataStream(int nTypeIn, int nVersionIn, Args &&... args) {
249  Init(nTypeIn, nVersionIn);
250  ::SerializeMany(*this, std::forward<Args>(args)...);
251  }
252 
253  void Init(int nTypeIn, int nVersionIn) {
254  nReadPos = 0;
255  nType = nTypeIn;
256  nVersion = nVersionIn;
257  }
258 
260  vch.insert(vch.end(), b.begin(), b.end());
261  return *this;
262  }
263 
264  friend CDataStream operator+(const CDataStream &a, const CDataStream &b) {
265  CDataStream ret = a;
266  ret += b;
267  return (ret);
268  }
269 
270  std::string str() const { return (std::string(begin(), end())); }
271 
272  //
273  // Vector subset
274  //
275  const_iterator begin() const { return vch.begin() + nReadPos; }
276  iterator begin() { return vch.begin() + nReadPos; }
277  const_iterator end() const { return vch.end(); }
278  iterator end() { return vch.end(); }
279  size_type size() const { return vch.size() - nReadPos; }
280  bool empty() const { return vch.size() == nReadPos; }
281  void resize(size_type n, value_type c = 0) { vch.resize(n + nReadPos, c); }
282  void reserve(size_type n) { vch.reserve(n + nReadPos); }
283  const_reference operator[](size_type pos) const {
284  return vch[pos + nReadPos];
285  }
286  reference operator[](size_type pos) { return vch[pos + nReadPos]; }
287  void clear() {
288  vch.clear();
289  nReadPos = 0;
290  }
291  iterator insert(iterator it, const char x = char()) {
292  return vch.insert(it, x);
293  }
294  void insert(iterator it, size_type n, const char x) {
295  vch.insert(it, n, x);
296  }
297  value_type *data() { return vch.data() + nReadPos; }
298  const value_type *data() const { return vch.data() + nReadPos; }
299 
300  void insert(iterator it, std::vector<char>::const_iterator first,
301  std::vector<char>::const_iterator last) {
302  if (last == first) {
303  return;
304  }
305 
306  assert(last - first > 0);
307  if (it == vch.begin() + nReadPos &&
308  (unsigned int)(last - first) <= nReadPos) {
309  // special case for inserting at the front when there's room
310  nReadPos -= (last - first);
311  memcpy(&vch[nReadPos], &first[0], last - first);
312  } else {
313  vch.insert(it, first, last);
314  }
315  }
316 
317  void insert(iterator it, const char *first, const char *last) {
318  if (last == first) {
319  return;
320  }
321 
322  assert(last - first > 0);
323  if (it == vch.begin() + nReadPos &&
324  (unsigned int)(last - first) <= nReadPos) {
325  // special case for inserting at the front when there's room
326  nReadPos -= (last - first);
327  memcpy(&vch[nReadPos], &first[0], last - first);
328  } else {
329  vch.insert(it, first, last);
330  }
331  }
332 
333  iterator erase(iterator it) {
334  if (it == vch.begin() + nReadPos) {
335  // special case for erasing from the front
336  if (++nReadPos >= vch.size()) {
337  // whenever we reach the end, we take the opportunity to clear
338  // the buffer
339  nReadPos = 0;
340  return vch.erase(vch.begin(), vch.end());
341  }
342  return vch.begin() + nReadPos;
343  } else {
344  return vch.erase(it);
345  }
346  }
347 
348  iterator erase(iterator first, iterator last) {
349  if (first == vch.begin() + nReadPos) {
350  // special case for erasing from the front
351  if (last == vch.end()) {
352  nReadPos = 0;
353  return vch.erase(vch.begin(), vch.end());
354  } else {
355  nReadPos = (last - vch.begin());
356  return last;
357  }
358  } else
359  return vch.erase(first, last);
360  }
361 
362  inline void Compact() {
363  vch.erase(vch.begin(), vch.begin() + nReadPos);
364  nReadPos = 0;
365  }
366 
367  bool Rewind(size_type n) {
368  // Rewind by n characters if the buffer hasn't been compacted yet
369  if (n > nReadPos) {
370  return false;
371  }
372  nReadPos -= n;
373  return true;
374  }
375 
376  //
377  // Stream subset
378  //
379  bool eof() const { return size() == 0; }
380  CDataStream *rdbuf() { return this; }
381  int in_avail() const { return size(); }
382 
383  void SetType(int n) { nType = n; }
384  int GetType() const { return nType; }
385  void SetVersion(int n) { nVersion = n; }
386  int GetVersion() const { return nVersion; }
387 
388  void read(char *pch, size_t nSize) {
389  if (nSize == 0) {
390  return;
391  }
392 
393  // Read from the beginning of the buffer
394  unsigned int nReadPosNext = nReadPos + nSize;
395  if (nReadPosNext > vch.size()) {
396  throw std::ios_base::failure("CDataStream::read(): end of data");
397  }
398  memcpy(pch, &vch[nReadPos], nSize);
399  if (nReadPosNext == vch.size()) {
400  nReadPos = 0;
401  vch.clear();
402  return;
403  }
404  nReadPos = nReadPosNext;
405  }
406 
407  void ignore(int nSize) {
408  // Ignore from the beginning of the buffer
409  if (nSize < 0) {
410  throw std::ios_base::failure(
411  "CDataStream::ignore(): nSize negative");
412  }
413  unsigned int nReadPosNext = nReadPos + nSize;
414  if (nReadPosNext >= vch.size()) {
415  if (nReadPosNext > vch.size()) {
416  throw std::ios_base::failure(
417  "CDataStream::ignore(): end of data");
418  }
419  nReadPos = 0;
420  vch.clear();
421  return;
422  }
423  nReadPos = nReadPosNext;
424  }
425 
426  void write(const char *pch, size_t nSize) {
427  // Write to the end of the buffer
428  vch.insert(vch.end(), pch, pch + nSize);
429  }
430 
431  template <typename Stream> void Serialize(Stream &s) const {
432  // Special case: stream << stream concatenates like stream += stream
433  if (!vch.empty()) {
434  s.write((char *)vch.data(), vch.size() * sizeof(value_type));
435  }
436  }
437 
438  template <typename T> CDataStream &operator<<(const T &obj) {
439  // Serialize to this stream
440  ::Serialize(*this, obj);
441  return (*this);
442  }
443 
444  template <typename T> CDataStream &operator>>(T &&obj) {
445  // Unserialize from this stream
446  ::Unserialize(*this, obj);
447  return (*this);
448  }
449 
451  d.insert(d.end(), begin(), end());
452  clear();
453  }
454 
460  void Xor(const std::vector<uint8_t> &key) {
461  if (key.size() == 0) {
462  return;
463  }
464 
465  for (size_type i = 0, j = 0; i != size(); i++) {
466  vch[i] ^= key[j++];
467 
468  // This potentially acts on very many bytes of data, so it's
469  // important that we calculate `j`, i.e. the `key` index in this way
470  // instead of doing a %, which would effectively be a division for
471  // each byte Xor'd -- much slower than need be.
472  if (j == key.size()) j = 0;
473  }
474  }
475 };
476 
477 template <typename IStream> class BitStreamReader {
478 private:
479  IStream &m_istream;
480 
483  uint8_t m_buffer{0};
484 
488  int m_offset{8};
489 
490 public:
491  explicit BitStreamReader(IStream &istream) : m_istream(istream) {}
492 
497  uint64_t Read(int nbits) {
498  if (nbits < 0 || nbits > 64) {
499  throw std::out_of_range("nbits must be between 0 and 64");
500  }
501 
502  uint64_t data = 0;
503  while (nbits > 0) {
504  if (m_offset == 8) {
505  m_istream >> m_buffer;
506  m_offset = 0;
507  }
508 
509  int bits = std::min(8 - m_offset, nbits);
510  data <<= bits;
511  data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
512  m_offset += bits;
513  nbits -= bits;
514  }
515  return data;
516  }
517 };
518 
519 template <typename OStream> class BitStreamWriter {
520 private:
521  OStream &m_ostream;
522 
525  uint8_t m_buffer{0};
526 
530  int m_offset{0};
531 
532 public:
533  explicit BitStreamWriter(OStream &ostream) : m_ostream(ostream) {}
534 
535  ~BitStreamWriter() { Flush(); }
536 
541  void Write(uint64_t data, int nbits) {
542  if (nbits < 0 || nbits > 64) {
543  throw std::out_of_range("nbits must be between 0 and 64");
544  }
545 
546  while (nbits > 0) {
547  int bits = std::min(8 - m_offset, nbits);
548  m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
549  m_offset += bits;
550  nbits -= bits;
551 
552  if (m_offset == 8) {
553  Flush();
554  }
555  }
556  }
557 
562  void Flush() {
563  if (m_offset == 0) {
564  return;
565  }
566 
567  m_ostream << m_buffer;
568  m_buffer = 0;
569  m_offset = 0;
570  }
571 };
572 
580 class CAutoFile {
581 private:
582  const int nType;
583  const int nVersion;
584 
585  FILE *file;
586 
587 public:
588  CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
589  : nType(nTypeIn), nVersion(nVersionIn) {
590  file = filenew;
591  }
592 
593  ~CAutoFile() { fclose(); }
594 
595  // Disallow copies
596  CAutoFile(const CAutoFile &) = delete;
597  CAutoFile &operator=(const CAutoFile &) = delete;
598 
599  void fclose() {
600  if (file) {
601  ::fclose(file);
602  file = nullptr;
603  }
604  }
605 
612  FILE *release() {
613  FILE *ret = file;
614  file = nullptr;
615  return ret;
616  }
617 
623  FILE *Get() const { return file; }
624 
626  bool IsNull() const { return (file == nullptr); }
627 
628  //
629  // Stream subset
630  //
631  int GetType() const { return nType; }
632  int GetVersion() const { return nVersion; }
633 
634  void read(char *pch, size_t nSize) {
635  if (!file) {
636  throw std::ios_base::failure(
637  "CAutoFile::read: file handle is nullptr");
638  }
639  if (fread(pch, 1, nSize, file) != nSize) {
640  throw std::ios_base::failure(feof(file)
641  ? "CAutoFile::read: end of file"
642  : "CAutoFile::read: fread failed");
643  }
644  }
645 
646  void ignore(size_t nSize) {
647  if (!file) {
648  throw std::ios_base::failure(
649  "CAutoFile::ignore: file handle is nullptr");
650  }
651  uint8_t data[4096];
652  while (nSize > 0) {
653  size_t nNow = std::min<size_t>(nSize, sizeof(data));
654  if (fread(data, 1, nNow, file) != nNow) {
655  throw std::ios_base::failure(
656  feof(file) ? "CAutoFile::ignore: end of file"
657  : "CAutoFile::read: fread failed");
658  }
659  nSize -= nNow;
660  }
661  }
662 
663  void write(const char *pch, size_t nSize) {
664  if (!file) {
665  throw std::ios_base::failure(
666  "CAutoFile::write: file handle is nullptr");
667  }
668  if (fwrite(pch, 1, nSize, file) != nSize) {
669  throw std::ios_base::failure("CAutoFile::write: write failed");
670  }
671  }
672 
673  template <typename T> CAutoFile &operator<<(const T &obj) {
674  // Serialize to this stream
675  if (!file) {
676  throw std::ios_base::failure(
677  "CAutoFile::operator<<: file handle is nullptr");
678  }
679  ::Serialize(*this, obj);
680  return (*this);
681  }
682 
683  template <typename T> CAutoFile &operator>>(T &&obj) {
684  // Unserialize from this stream
685  if (!file) {
686  throw std::ios_base::failure(
687  "CAutoFile::operator>>: file handle is nullptr");
688  }
689  ::Unserialize(*this, obj);
690  return (*this);
691  }
692 };
693 
703 private:
704  const int nType;
705  const int nVersion;
706 
708  FILE *src;
710  uint64_t nSrcPos;
712  uint64_t nReadPos;
714  uint64_t nReadLimit;
716  uint64_t nRewind;
718  std::vector<char> vchBuf;
719 
720 protected:
722  bool Fill() {
723  unsigned int pos = nSrcPos % vchBuf.size();
724  unsigned int readNow = vchBuf.size() - pos;
725  unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
726  if (nAvail < readNow) {
727  readNow = nAvail;
728  }
729  if (readNow == 0) {
730  return false;
731  }
732  size_t nBytes = fread((void *)&vchBuf[pos], 1, readNow, src);
733  if (nBytes == 0) {
734  throw std::ios_base::failure(
735  feof(src) ? "CBufferedFile::Fill: end of file"
736  : "CBufferedFile::Fill: fread failed");
737  }
738  nSrcPos += nBytes;
739  return true;
740  }
741 
742 public:
743  CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
744  int nTypeIn, int nVersionIn)
745  : nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0),
746  nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn),
747  vchBuf(nBufSize, 0) {
748  if (nRewindIn >= nBufSize) {
749  throw std::ios_base::failure(
750  "Rewind limit must be less than buffer size");
751  }
752  src = fileIn;
753  }
754 
755  ~CBufferedFile() { fclose(); }
756 
757  // Disallow copies
758  CBufferedFile(const CBufferedFile &) = delete;
759  CBufferedFile &operator=(const CBufferedFile &) = delete;
760 
761  int GetVersion() const { return nVersion; }
762  int GetType() const { return nType; }
763 
764  void fclose() {
765  if (src) {
766  ::fclose(src);
767  src = nullptr;
768  }
769  }
770 
772  bool eof() const { return nReadPos == nSrcPos && feof(src); }
773 
775  void read(char *pch, size_t nSize) {
776  if (nSize + nReadPos > nReadLimit) {
777  throw std::ios_base::failure("Read attempted past buffer limit");
778  }
779  while (nSize > 0) {
780  if (nReadPos == nSrcPos) {
781  Fill();
782  }
783  unsigned int pos = nReadPos % vchBuf.size();
784  size_t nNow = nSize;
785  if (nNow + pos > vchBuf.size()) {
786  nNow = vchBuf.size() - pos;
787  }
788  if (nNow + nReadPos > nSrcPos) {
789  nNow = nSrcPos - nReadPos;
790  }
791  memcpy(pch, &vchBuf[pos], nNow);
792  nReadPos += nNow;
793  pch += nNow;
794  nSize -= nNow;
795  }
796  }
797 
799  uint64_t GetPos() const { return nReadPos; }
800 
802  bool SetPos(uint64_t nPos) {
803  size_t bufsize = vchBuf.size();
804  if (nPos + bufsize < nSrcPos) {
805  // rewinding too far, rewind as far as possible
806  nReadPos = nSrcPos - bufsize;
807  return false;
808  }
809  if (nPos > nSrcPos) {
810  // can't go this far forward, go as far as possible
811  nReadPos = nSrcPos;
812  return false;
813  }
814  nReadPos = nPos;
815  return true;
816  }
817 
818  bool Seek(uint64_t nPos) {
819  long nLongPos = nPos;
820  if (nPos != (uint64_t)nLongPos) {
821  return false;
822  }
823  if (fseek(src, nLongPos, SEEK_SET)) {
824  return false;
825  }
826  nLongPos = ftell(src);
827  nSrcPos = nLongPos;
828  nReadPos = nLongPos;
829  return true;
830  }
831 
834  bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
835  if (nPos < nReadPos) {
836  return false;
837  }
838  nReadLimit = nPos;
839  return true;
840  }
841 
842  template <typename T> CBufferedFile &operator>>(T &&obj) {
843  // Unserialize from this stream
844  ::Unserialize(*this, obj);
845  return (*this);
846  }
847 
849  void FindByte(char ch) {
850  while (true) {
851  if (nReadPos == nSrcPos) {
852  Fill();
853  }
854  if (vchBuf[nReadPos % vchBuf.size()] == ch) {
855  break;
856  }
857  nReadPos++;
858  }
859  }
860 };
861 
862 #endif // BITCOIN_STREAMS_H
const int nType
Definition: streams.h:704
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:104
void Init(int nTypeIn, int nVersionIn)
Definition: streams.h:253
CSerializeData vector_type
Definition: streams.h:198
void ignore(size_t nSize)
Definition: streams.h:646
uint64_t nReadPos
how many bytes have been read from this
Definition: streams.h:712
vector_type::iterator iterator
Definition: streams.h:212
vector_type::allocator_type allocator_type
Definition: streams.h:206
vector_type vch
Definition: streams.h:199
void Compact()
Definition: streams.h:362
int GetVersion() const
Definition: streams.h:386
void read(char *dst, size_t n)
Definition: streams.h:174
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
Definition: streams.h:220
unsigned int nReadPos
Definition: streams.h:200
size_t nPos
Definition: streams.h:122
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< uint8_t > &vchDataIn, size_t nPosIn)
Definition: streams.h:72
int GetType() const
Definition: streams.h:384
int GetType() const
Definition: streams.h:110
void write(const char *pch, size_t nSize)
Definition: streams.h:90
const int nVersion
Definition: streams.h:583
vector_type::size_type size_type
Definition: streams.h:207
std::string str() const
Definition: streams.h:270
const int nType
Definition: streams.h:26
void resize(size_type n, value_type c=0)
Definition: streams.h:281
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:232
void Serialize(Stream &s) const
Definition: streams.h:431
vector_type::reference reference
Definition: streams.h:209
int GetType() const
Definition: streams.h:762
vector_type::value_type value_type
Definition: streams.h:211
value_type * data()
Definition: streams.h:297
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:33
const int m_version
Definition: streams.h:131
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:196
void write(const char *pch, size_t nSize)
Definition: streams.h:426
bool empty() const
Definition: streams.h:280
OverrideStream< S > WithOrVersion(S *s, int nVersionFlag)
Definition: streams.h:53
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
Definition: streams.h:541
int GetVersion() const
Definition: streams.h:49
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:214
size_t size() const
Definition: streams.h:171
int GetVersion() const
Definition: streams.h:632
iterator erase(iterator it)
Definition: streams.h:333
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:588
iterator end()
Definition: streams.h:278
const int nVersion
Definition: streams.h:705
void UnserializeMany(Stream &s)
Definition: serialize.h:1165
const std::vector< uint8_t > & m_data
Definition: streams.h:132
friend CDataStream operator+(const CDataStream &a, const CDataStream &b)
Definition: streams.h:264
CDataStream * rdbuf()
Definition: streams.h:380
~CAutoFile()
Definition: streams.h:593
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:216
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
Definition: streams.h:300
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:626
iterator insert(iterator it, const char x=char())
Definition: streams.h:291
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:612
void write(const char *pch, size_t nSize)
Definition: streams.h:663
void Serialize(Stream &s, char a)
Definition: serialize.h:261
void read(char *pch, size_t nSize)
Definition: streams.h:388
vector_type::const_reference const_reference
Definition: streams.h:210
void read(char *pch, size_t nSize)
Definition: streams.h:47
CAutoFile & operator>>(T &&obj)
Definition: streams.h:683
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:248
void fclose()
Definition: streams.h:599
uint64_t GetPos() const
return the current reading position
Definition: streams.h:799
BitStreamReader(IStream &istream)
Definition: streams.h:491
const int nType
Definition: streams.h:119
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:237
uint64_t nReadLimit
up to which position we&#39;re allowed to read
Definition: streams.h:714
size_type size() const
Definition: streams.h:279
vector_type::const_iterator const_iterator
Definition: streams.h:213
void seek(size_t nSize)
Definition: streams.h:111
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:743
Minimal stream for reading from an existing vector by reference.
Definition: streams.h:128
void ignore(int nSize)
Definition: streams.h:407
const value_type * data() const
Definition: streams.h:298
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
Definition: streams.h:226
CDataStream & operator+=(const CDataStream &b)
Definition: streams.h:259
CDataStream & operator>>(T &&obj)
Definition: streams.h:444
void GetAndClear(CSerializeData &d)
Definition: streams.h:450
IStream & m_istream
Definition: streams.h:479
iterator begin()
Definition: streams.h:276
void write(const char *pch, size_t nSize)
Definition: streams.h:45
int GetVersion() const
Definition: streams.h:168
int GetVersion() const
Definition: streams.h:109
CDataStream(const std::vector< uint8_t > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:242
void read(char *pch, size_t nSize)
Definition: streams.h:634
void read(char *pch, size_t nSize)
read a number of bytes
Definition: streams.h:775
reference operator[](size_type pos)
Definition: streams.h:286
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:497
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:802
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:30
VectorReader(int type, int version, const std::vector< uint8_t > &data, size_t pos)
Definition: streams.h:142
bool Rewind(size_type n)
Definition: streams.h:367
void SerializeMany(Stream &s)
Definition: serialize.h:1157
void Flush()
Flush any unwritten bits to the output stream, padding with 0&#39;s to the next byte boundary.
Definition: streams.h:562
const_reference operator[](size_type pos) const
Definition: streams.h:283
int GetType() const
Definition: streams.h:631
void Xor(const std::vector< uint8_t > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:460
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:623
void fclose()
Definition: streams.h:764
const int nVersion
Definition: streams.h:120
const_iterator end() const
Definition: streams.h:277
const_iterator begin() const
Definition: streams.h:275
const int nVersion
Definition: streams.h:27
VectorReader & operator>>(T &obj)
Definition: streams.h:162
CDataStream & operator<<(const T &obj)
Definition: streams.h:438
void FindByte(char ch)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:849
void reserve(size_type n)
Definition: streams.h:282
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:842
int nVersion
Definition: streams.h:203
FILE * src
source file
Definition: streams.h:708
std::vector< uint8_t > & vchData
Definition: streams.h:121
VectorReader(int type, int version, const std::vector< uint8_t > &data, size_t pos, Args &&... args)
(other params same as above)
Definition: streams.h:156
void Unserialize(Stream &s, char &a)
Definition: serialize.h:313
vector_type::difference_type difference_type
Definition: streams.h:208
const int m_type
Definition: streams.h:130
bool empty() const
Definition: streams.h:172
void insert(iterator it, const char *first, const char *last)
Definition: streams.h:317
void clear()
Definition: streams.h:287
uint64_t nSrcPos
how many bytes have been read from source
Definition: streams.h:710
bool Fill()
read data from the source to fill the buffer
Definition: streams.h:722
std::vector< char, zero_after_free_allocator< char > > CSerializeData
Definition: zeroafterfree.h:43
FILE * file
Definition: streams.h:585
Minimal stream for overwriting and/or appending to an existing byte vector.
Definition: streams.h:62
std::vector< char > vchBuf
the buffer
Definition: streams.h:718
iterator erase(iterator first, iterator last)
Definition: streams.h:348
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:716
int GetVersion() const
Definition: streams.h:761
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
Prevent reading beyond a certain position.
Definition: streams.h:834
OStream & m_ostream
Definition: streams.h:521
const int nType
Definition: streams.h:582
int GetType() const
Definition: streams.h:169
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from...
Definition: streams.h:702
void SetVersion(int n)
Definition: streams.h:385
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:39
int nType
Definition: streams.h:202
bool Seek(uint64_t nPos)
Definition: streams.h:818
Stream * stream
Definition: streams.h:24
bool eof() const
Definition: streams.h:379
CAutoFile & operator<<(const T &obj)
Definition: streams.h:673
int GetType() const
Definition: streams.h:50
void insert(iterator it, size_type n, const char x)
Definition: streams.h:294
~CBufferedFile()
Definition: streams.h:755
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< uint8_t > &vchDataIn, size_t nPosIn, Args &&... args)
(other params same as above)
Definition: streams.h:85
int in_avail() const
Definition: streams.h:381
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:580
BitStreamWriter(OStream &ostream)
Definition: streams.h:533
void SetType(int n)
Definition: streams.h:383
bool eof() const
check whether we&#39;re at the end of the source file
Definition: streams.h:772