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