Bitcoin Core  22.99.0
P2P Digital Currency
streams.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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>
10 #include <span.h>
12 
13 #include <algorithm>
14 #include <assert.h>
15 #include <ios>
16 #include <limits>
17 #include <optional>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 template<typename Stream>
27 {
28  Stream* stream;
29 
30  const int nType;
31  const int nVersion;
32 
33 public:
34  OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
35 
36  template<typename T>
38  {
39  // Serialize to this stream
40  ::Serialize(*this, obj);
41  return (*this);
42  }
43 
44  template<typename T>
46  {
47  // Unserialize from this stream
48  ::Unserialize(*this, obj);
49  return (*this);
50  }
51 
52  void write(const char* pch, size_t nSize)
53  {
54  stream->write(pch, nSize);
55  }
56 
57  void read(char* pch, size_t nSize)
58  {
59  stream->read(pch, nSize);
60  }
61 
62  int GetVersion() const { return nVersion; }
63  int GetType() const { return nType; }
64  size_t size() const { return stream->size(); }
65  void ignore(size_t size) { return stream->ignore(size); }
66 };
67 
68 /* Minimal stream for overwriting and/or appending to an existing byte vector
69  *
70  * The referenced vector will grow as necessary
71  */
73 {
74  public:
75 
76 /*
77  * @param[in] nTypeIn Serialization Type
78  * @param[in] nVersionIn Serialization Version (including any flags)
79  * @param[in] vchDataIn Referenced byte vector to overwrite/append
80  * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
81  * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
82 */
83  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
84  {
85  if(nPos > vchData.size())
86  vchData.resize(nPos);
87  }
88 /*
89  * (other params same as above)
90  * @param[in] args A list of items to serialize starting at nPosIn.
91 */
92  template <typename... Args>
93  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
94  {
95  ::SerializeMany(*this, std::forward<Args>(args)...);
96  }
97  void write(const char* pch, size_t nSize)
98  {
99  assert(nPos <= vchData.size());
100  size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
101  if (nOverwrite) {
102  memcpy(vchData.data() + nPos, reinterpret_cast<const unsigned char*>(pch), nOverwrite);
103  }
104  if (nOverwrite < nSize) {
105  vchData.insert(vchData.end(), reinterpret_cast<const unsigned char*>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
106  }
107  nPos += nSize;
108  }
109  template<typename T>
111  {
112  // Serialize to this stream
113  ::Serialize(*this, obj);
114  return (*this);
115  }
116  int GetVersion() const
117  {
118  return nVersion;
119  }
120  int GetType() const
121  {
122  return nType;
123  }
124 private:
125  const int nType;
126  const int nVersion;
127  std::vector<unsigned char>& vchData;
128  size_t nPos;
129 };
130 
134 {
135 private:
136  const int m_type;
137  const int m_version;
139 
140 public:
141 
147  SpanReader(int type, int version, Span<const unsigned char> data)
148  : m_type(type), m_version(version), m_data(data) {}
149 
150  template<typename T>
152  {
153  // Unserialize from this stream
154  ::Unserialize(*this, obj);
155  return (*this);
156  }
157 
158  int GetVersion() const { return m_version; }
159  int GetType() const { return m_type; }
160 
161  size_t size() const { return m_data.size(); }
162  bool empty() const { return m_data.empty(); }
163 
164  void read(char* dst, size_t n)
165  {
166  if (n == 0) {
167  return;
168  }
169 
170  // Read from the beginning of the buffer
171  if (n > m_data.size()) {
172  throw std::ios_base::failure("SpanReader::read(): end of data");
173  }
174  memcpy(dst, m_data.data(), n);
175  m_data = m_data.subspan(n);
176  }
177 };
178 
185 {
186 protected:
189  unsigned int nReadPos{0};
190 
191  int nType;
192  int nVersion;
193 
194 public:
195  typedef vector_type::allocator_type allocator_type;
196  typedef vector_type::size_type size_type;
197  typedef vector_type::difference_type difference_type;
198  typedef vector_type::reference reference;
199  typedef vector_type::const_reference const_reference;
200  typedef vector_type::value_type value_type;
201  typedef vector_type::iterator iterator;
202  typedef vector_type::const_iterator const_iterator;
203  typedef vector_type::reverse_iterator reverse_iterator;
204 
205  explicit CDataStream(int nTypeIn, int nVersionIn)
206  : nType{nTypeIn},
207  nVersion{nVersionIn} {}
208 
209  explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
210  : vch(sp.data(), sp.data() + sp.size()),
211  nType{nTypeIn},
212  nVersion{nVersionIn} {}
213 
214  template <typename... Args>
215  CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
216  : nType{nTypeIn},
217  nVersion{nVersionIn}
218  {
219  ::SerializeMany(*this, std::forward<Args>(args)...);
220  }
221 
222  std::string str() const
223  {
224  return (std::string(begin(), end()));
225  }
226 
227 
228  //
229  // Vector subset
230  //
231  const_iterator begin() const { return vch.begin() + nReadPos; }
232  iterator begin() { return vch.begin() + nReadPos; }
233  const_iterator end() const { return vch.end(); }
234  iterator end() { return vch.end(); }
235  size_type size() const { return vch.size() - nReadPos; }
236  bool empty() const { return vch.size() == nReadPos; }
237  void resize(size_type n, value_type c = value_type{}) { vch.resize(n + nReadPos, c); }
238  void reserve(size_type n) { vch.reserve(n + nReadPos); }
239  const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
240  reference operator[](size_type pos) { return vch[pos + nReadPos]; }
241  void clear() { vch.clear(); nReadPos = 0; }
242  iterator insert(iterator it, const value_type x) { return vch.insert(it, x); }
243  void insert(iterator it, size_type n, const value_type x) { vch.insert(it, n, x); }
244  value_type* data() { return vch.data() + nReadPos; }
245  const value_type* data() const { return vch.data() + nReadPos; }
246 
247  void insert(iterator it, std::vector<value_type>::const_iterator first, std::vector<value_type>::const_iterator last)
248  {
249  if (last == first) return;
250  assert(last - first > 0);
251  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
252  {
253  // special case for inserting at the front when there's room
254  nReadPos -= (last - first);
255  memcpy(&vch[nReadPos], &first[0], last - first);
256  }
257  else
258  vch.insert(it, first, last);
259  }
260 
261  void insert(iterator it, const value_type* first, const value_type* last)
262  {
263  if (last == first) return;
264  assert(last - first > 0);
265  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
266  {
267  // special case for inserting at the front when there's room
268  nReadPos -= (last - first);
269  memcpy(&vch[nReadPos], &first[0], last - first);
270  }
271  else
272  vch.insert(it, first, last);
273  }
274 
276  {
277  if (it == vch.begin() + nReadPos)
278  {
279  // special case for erasing from the front
280  if (++nReadPos >= vch.size())
281  {
282  // whenever we reach the end, we take the opportunity to clear the buffer
283  nReadPos = 0;
284  return vch.erase(vch.begin(), vch.end());
285  }
286  return vch.begin() + nReadPos;
287  }
288  else
289  return vch.erase(it);
290  }
291 
293  {
294  if (first == vch.begin() + nReadPos)
295  {
296  // special case for erasing from the front
297  if (last == vch.end())
298  {
299  nReadPos = 0;
300  return vch.erase(vch.begin(), vch.end());
301  }
302  else
303  {
304  nReadPos = (last - vch.begin());
305  return last;
306  }
307  }
308  else
309  return vch.erase(first, last);
310  }
311 
312  inline void Compact()
313  {
314  vch.erase(vch.begin(), vch.begin() + nReadPos);
315  nReadPos = 0;
316  }
317 
318  bool Rewind(std::optional<size_type> n = std::nullopt)
319  {
320  // Total rewind if no size is passed
321  if (!n) {
322  nReadPos = 0;
323  return true;
324  }
325  // Rewind by n characters if the buffer hasn't been compacted yet
326  if (*n > nReadPos)
327  return false;
328  nReadPos -= *n;
329  return true;
330  }
331 
332 
333  //
334  // Stream subset
335  //
336  bool eof() const { return size() == 0; }
337  CDataStream* rdbuf() { return this; }
338  int in_avail() const { return size(); }
339 
340  void SetType(int n) { nType = n; }
341  int GetType() const { return nType; }
342  void SetVersion(int n) { nVersion = n; }
343  int GetVersion() const { return nVersion; }
344 
345  void read(char* pch, size_t nSize)
346  {
347  if (nSize == 0) return;
348 
349  // Read from the beginning of the buffer
350  unsigned int nReadPosNext = nReadPos + nSize;
351  if (nReadPosNext > vch.size()) {
352  throw std::ios_base::failure("CDataStream::read(): end of data");
353  }
354  memcpy(pch, &vch[nReadPos], nSize);
355  if (nReadPosNext == vch.size())
356  {
357  nReadPos = 0;
358  vch.clear();
359  return;
360  }
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 
417  void Xor(const std::vector<unsigned char>& key)
418  {
419  if (key.size() == 0) {
420  return;
421  }
422 
423  for (size_type i = 0, j = 0; i != size(); i++) {
424  vch[i] ^= key[j++];
425 
426  // This potentially acts on very many bytes of data, so it's
427  // important that we calculate `j`, i.e. the `key` index in this
428  // way instead of doing a %, which would effectively be a division
429  // for each byte Xor'd -- much slower than need be.
430  if (j == key.size())
431  j = 0;
432  }
433  }
434 };
435 
436 template <typename IStream>
438 {
439 private:
440  IStream& m_istream;
441 
444  uint8_t m_buffer{0};
445 
449  int m_offset{8};
450 
451 public:
452  explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
453 
457  uint64_t Read(int nbits) {
458  if (nbits < 0 || nbits > 64) {
459  throw std::out_of_range("nbits must be between 0 and 64");
460  }
461 
462  uint64_t data = 0;
463  while (nbits > 0) {
464  if (m_offset == 8) {
465  m_istream >> m_buffer;
466  m_offset = 0;
467  }
468 
469  int bits = std::min(8 - m_offset, nbits);
470  data <<= bits;
471  data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
472  m_offset += bits;
473  nbits -= bits;
474  }
475  return data;
476  }
477 };
478 
479 template <typename OStream>
481 {
482 private:
483  OStream& m_ostream;
484 
487  uint8_t m_buffer{0};
488 
492  int m_offset{0};
493 
494 public:
495  explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
496 
498  {
499  Flush();
500  }
501 
505  void Write(uint64_t data, int nbits) {
506  if (nbits < 0 || nbits > 64) {
507  throw std::out_of_range("nbits must be between 0 and 64");
508  }
509 
510  while (nbits > 0) {
511  int bits = std::min(8 - m_offset, nbits);
512  m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
513  m_offset += bits;
514  nbits -= bits;
515 
516  if (m_offset == 8) {
517  Flush();
518  }
519  }
520  }
521 
525  void Flush() {
526  if (m_offset == 0) {
527  return;
528  }
529 
530  m_ostream << m_buffer;
531  m_buffer = 0;
532  m_offset = 0;
533  }
534 };
535 
536 
537 
545 {
546 private:
547  const int nType;
548  const int nVersion;
549 
550  FILE* file;
551 
552 public:
553  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
554  {
555  file = filenew;
556  }
557 
559  {
560  fclose();
561  }
562 
563  // Disallow copies
564  CAutoFile(const CAutoFile&) = delete;
565  CAutoFile& operator=(const CAutoFile&) = delete;
566 
567  void fclose()
568  {
569  if (file) {
570  ::fclose(file);
571  file = nullptr;
572  }
573  }
574 
579  FILE* release() { FILE* ret = file; file = nullptr; return ret; }
580 
585  FILE* Get() const { return file; }
586 
589  bool IsNull() const { return (file == nullptr); }
590 
591  //
592  // Stream subset
593  //
594  int GetType() const { return nType; }
595  int GetVersion() const { return nVersion; }
596 
597  void read(char* pch, size_t nSize)
598  {
599  if (!file)
600  throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
601  if (fread(pch, 1, nSize, file) != nSize)
602  throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
603  }
604 
605  void ignore(size_t nSize)
606  {
607  if (!file)
608  throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
609  unsigned char data[4096];
610  while (nSize > 0) {
611  size_t nNow = std::min<size_t>(nSize, sizeof(data));
612  if (fread(data, 1, nNow, file) != nNow)
613  throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
614  nSize -= nNow;
615  }
616  }
617 
618  void write(const char* pch, size_t nSize)
619  {
620  if (!file)
621  throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
622  if (fwrite(pch, 1, nSize, file) != nSize)
623  throw std::ios_base::failure("CAutoFile::write: write failed");
624  }
625 
626  template<typename T>
627  CAutoFile& operator<<(const T& obj)
628  {
629  // Serialize to this stream
630  if (!file)
631  throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
632  ::Serialize(*this, obj);
633  return (*this);
634  }
635 
636  template<typename T>
638  {
639  // Unserialize from this stream
640  if (!file)
641  throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
642  ::Unserialize(*this, obj);
643  return (*this);
644  }
645 };
646 
654 {
655 private:
656  const int nType;
657  const int nVersion;
658 
659  FILE *src;
660  uint64_t nSrcPos;
661  uint64_t nReadPos;
662  uint64_t nReadLimit;
663  uint64_t nRewind;
664  std::vector<char> vchBuf;
665 
666 protected:
668  bool Fill() {
669  unsigned int pos = nSrcPos % vchBuf.size();
670  unsigned int readNow = vchBuf.size() - pos;
671  unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
672  if (nAvail < readNow)
673  readNow = nAvail;
674  if (readNow == 0)
675  return false;
676  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
677  if (nBytes == 0) {
678  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
679  }
680  nSrcPos += nBytes;
681  return true;
682  }
683 
684 public:
685  CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
686  nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0)
687  {
688  if (nRewindIn >= nBufSize)
689  throw std::ios_base::failure("Rewind limit must be less than buffer size");
690  src = fileIn;
691  }
692 
694  {
695  fclose();
696  }
697 
698  // Disallow copies
699  CBufferedFile(const CBufferedFile&) = delete;
700  CBufferedFile& operator=(const CBufferedFile&) = delete;
701 
702  int GetVersion() const { return nVersion; }
703  int GetType() const { return nType; }
704 
705  void fclose()
706  {
707  if (src) {
708  ::fclose(src);
709  src = nullptr;
710  }
711  }
712 
714  bool eof() const {
715  return nReadPos == nSrcPos && feof(src);
716  }
717 
719  void read(char *pch, size_t nSize) {
720  if (nSize + nReadPos > nReadLimit)
721  throw std::ios_base::failure("Read attempted past buffer limit");
722  while (nSize > 0) {
723  if (nReadPos == nSrcPos)
724  Fill();
725  unsigned int pos = nReadPos % vchBuf.size();
726  size_t nNow = nSize;
727  if (nNow + pos > vchBuf.size())
728  nNow = vchBuf.size() - pos;
729  if (nNow + nReadPos > nSrcPos)
730  nNow = nSrcPos - nReadPos;
731  memcpy(pch, &vchBuf[pos], nNow);
732  nReadPos += nNow;
733  pch += nNow;
734  nSize -= nNow;
735  }
736  }
737 
739  uint64_t GetPos() const {
740  return nReadPos;
741  }
742 
744  bool SetPos(uint64_t nPos) {
745  size_t bufsize = vchBuf.size();
746  if (nPos + bufsize < nSrcPos) {
747  // rewinding too far, rewind as far as possible
748  nReadPos = nSrcPos - bufsize;
749  return false;
750  }
751  if (nPos > nSrcPos) {
752  // can't go this far forward, go as far as possible
753  nReadPos = nSrcPos;
754  return false;
755  }
756  nReadPos = nPos;
757  return true;
758  }
759 
762  bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
763  if (nPos < nReadPos)
764  return false;
765  nReadLimit = nPos;
766  return true;
767  }
768 
769  template<typename T>
771  // Unserialize from this stream
772  ::Unserialize(*this, obj);
773  return (*this);
774  }
775 
777  void FindByte(char ch) {
778  while (true) {
779  if (nReadPos == nSrcPos)
780  Fill();
781  if (vchBuf[nReadPos % vchBuf.size()] == ch)
782  break;
783  nReadPos++;
784  }
785  }
786 };
787 
788 #endif // BITCOIN_STREAMS_H
CAutoFile::operator<<
CAutoFile & operator<<(const T &obj)
Definition: streams.h:627
CDataStream::end
iterator end()
Definition: streams.h:234
BitStreamWriter::m_ostream
OStream & m_ostream
Definition: streams.h:483
CDataStream::value_type
vector_type::value_type value_type
Definition: streams.h:200
CBufferedFile::read
void read(char *pch, size_t nSize)
read a number of bytes
Definition: streams.h:719
CDataStream::in_avail
int in_avail() const
Definition: streams.h:338
CDataStream::size_type
vector_type::size_type size_type
Definition: streams.h:196
CAutoFile::file
FILE * file
Definition: streams.h:550
BitStreamReader
Definition: streams.h:437
assert
assert(!tx.IsCoinBase())
SpanReader::m_type
const int m_type
Definition: streams.h:136
CDataStream::erase
iterator erase(iterator first, iterator last)
Definition: streams.h:292
CAutoFile::ignore
void ignore(size_t nSize)
Definition: streams.h:605
CDataStream::vch
vector_type vch
Definition: streams.h:188
BitStreamWriter::Flush
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:525
CBufferedFile::operator>>
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:770
OverrideStream::read
void read(char *pch, size_t nSize)
Definition: streams.h:57
CVectorWriter
Definition: streams.h:72
CDataStream::begin
const_iterator begin() const
Definition: streams.h:231
CBufferedFile::SetPos
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:744
SerializeData
std::vector< uint8_t, zero_after_free_allocator< uint8_t > > SerializeData
Byte-vector that clears its contents before deletion.
Definition: zeroafterfree.h:44
CDataStream::Xor
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:417
CDataStream::data
value_type * data()
Definition: streams.h:244
BitStreamReader::m_buffer
uint8_t m_buffer
Buffered byte read in from the input stream.
Definition: streams.h:444
string.h
CAutoFile::Get
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:585
OverrideStream::operator>>
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:45
SpanReader::GetVersion
int GetVersion() const
Definition: streams.h:158
CAutoFile::nType
const int nType
Definition: streams.h:547
CAutoFile::read
void read(char *pch, size_t nSize)
Definition: streams.h:597
CDataStream::reference
vector_type::reference reference
Definition: streams.h:198
CDataStream::CDataStream
CDataStream(Span< const value_type > sp, int nTypeIn, int nVersionIn)
Definition: streams.h:209
SpanReader::m_data
Span< const unsigned char > m_data
Definition: streams.h:138
CBufferedFile::fclose
void fclose()
Definition: streams.h:705
CDataStream::insert
void insert(iterator it, const value_type *first, const value_type *last)
Definition: streams.h:261
BitStreamReader::m_offset
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
Definition: streams.h:449
CDataStream::Compact
void Compact()
Definition: streams.h:312
CDataStream::begin
iterator begin()
Definition: streams.h:232
OverrideStream::GetType
int GetType() const
Definition: streams.h:63
OverrideStream::stream
Stream * stream
Definition: streams.h:28
CBufferedFile::FindByte
void FindByte(char ch)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:777
CAutoFile::~CAutoFile
~CAutoFile()
Definition: streams.h:558
CDataStream::read
void read(char *pch, size_t nSize)
Definition: streams.h:345
BitStreamWriter
Definition: streams.h:480
CAutoFile::nVersion
const int nVersion
Definition: streams.h:548
SpanReader::m_version
const int m_version
Definition: streams.h:137
OverrideStream::OverrideStream
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:34
CBufferedFile::src
FILE * src
source file
Definition: streams.h:659
CBufferedFile::nRewind
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:663
CDataStream::write
void write(const char *pch, size_t nSize)
Definition: streams.h:382
CDataStream::GetType
int GetType() const
Definition: streams.h:341
CVectorWriter::nVersion
const int nVersion
Definition: streams.h:126
CAutoFile::GetVersion
int GetVersion() const
Definition: streams.h:595
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:186
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:544
Span< const unsigned char >
CDataStream::const_reference
vector_type::const_reference const_reference
Definition: streams.h:199
CDataStream::nVersion
int nVersion
Definition: streams.h:192
BitStreamReader::Read
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:457
CBufferedFile::~CBufferedFile
~CBufferedFile()
Definition: streams.h:693
CVectorWriter::nPos
size_t nPos
Definition: streams.h:128
CBufferedFile::Fill
bool Fill()
read data from the source to fill the buffer
Definition: streams.h:668
CDataStream::allocator_type
vector_type::allocator_type allocator_type
Definition: streams.h:195
OverrideStream::ignore
void ignore(size_t size)
Definition: streams.h:65
CVectorWriter::GetType
int GetType() const
Definition: streams.h:120
Unserialize
void Unserialize(Stream &s, char &a)
Definition: serialize.h:215
CBufferedFile
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: streams.h:653
CBufferedFile::GetVersion
int GetVersion() const
Definition: streams.h:702
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:505
OverrideStream::nVersion
const int nVersion
Definition: streams.h:31
CBufferedFile::eof
bool eof() const
check whether we're at the end of the source file
Definition: streams.h:714
CDataStream::CDataStream
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:205
CDataStream::operator[]
const_reference operator[](size_type pos) const
Definition: streams.h:239
CBufferedFile::operator=
CBufferedFile & operator=(const CBufferedFile &)=delete
span.h
CBufferedFile::nSrcPos
uint64_t nSrcPos
how many bytes have been read from source
Definition: streams.h:660
CBufferedFile::nReadPos
uint64_t nReadPos
how many bytes have been read from this
Definition: streams.h:661
CDataStream::end
const_iterator end() const
Definition: streams.h:233
CDataStream::nType
int nType
Definition: streams.h:191
CBufferedFile::vchBuf
std::vector< char > vchBuf
the buffer
Definition: streams.h:664
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:492
CAutoFile::CAutoFile
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:553
CDataStream::Rewind
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:318
CBufferedFile::SetLimit
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
prevent reading beyond a certain position no argument removes the limit
Definition: streams.h:762
CDataStream::erase
iterator erase(iterator it)
Definition: streams.h:275
SpanReader
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:133
CDataStream::insert
void insert(iterator it, std::vector< value_type >::const_iterator first, std::vector< value_type >::const_iterator last)
Definition: streams.h:247
CAutoFile::operator>>
CAutoFile & operator>>(T &&obj)
Definition: streams.h:637
CAutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:589
BitStreamWriter::BitStreamWriter
BitStreamWriter(OStream &ostream)
Definition: streams.h:495
SerializeMany
void SerializeMany(Stream &s)
Definition: serialize.h:1024
CDataStream::insert
void insert(iterator it, size_type n, const value_type x)
Definition: streams.h:243
CAutoFile::fclose
void fclose()
Definition: streams.h:567
CDataStream::reverse_iterator
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:203
CDataStream::reserve
void reserve(size_type n)
Definition: streams.h:238
CAutoFile::GetType
int GetType() const
Definition: streams.h:594
SpanReader::read
void read(char *dst, size_t n)
Definition: streams.h:164
CVectorWriter::CVectorWriter
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:93
CDataStream::size
size_type size() const
Definition: streams.h:235
CDataStream::SetVersion
void SetVersion(int n)
Definition: streams.h:342
CDataStream::resize
void resize(size_type n, value_type c=value_type{})
Definition: streams.h:237
CVectorWriter::CVectorWriter
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:83
SpanReader::GetType
int GetType() const
Definition: streams.h:159
CDataStream::ignore
void ignore(int nSize)
Definition: streams.h:364
Span::empty
constexpr bool empty() const noexcept
Definition: span.h:188
CDataStream::data
const value_type * data() const
Definition: streams.h:245
SpanReader::size
size_t size() const
Definition: streams.h:161
OverrideStream
Definition: streams.h:26
CBufferedFile::nType
const int nType
Definition: streams.h:656
CDataStream::operator[]
reference operator[](size_type pos)
Definition: streams.h:240
CBufferedFile::nVersion
const int nVersion
Definition: streams.h:657
CVectorWriter::operator<<
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:110
BitStreamWriter::~BitStreamWriter
~BitStreamWriter()
Definition: streams.h:497
Span::data
constexpr C * data() const noexcept
Definition: span.h:173
CVectorWriter::nType
const int nType
Definition: streams.h:125
CDataStream::GetVersion
int GetVersion() const
Definition: streams.h:343
CDataStream::operator>>
CDataStream & operator>>(T &&obj)
Definition: streams.h:405
CDataStream::insert
iterator insert(iterator it, const value_type x)
Definition: streams.h:242
CDataStream::CDataStream
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:215
SpanReader::operator>>
SpanReader & operator>>(T &&obj)
Definition: streams.h:151
CAutoFile::operator=
CAutoFile & operator=(const CAutoFile &)=delete
CDataStream::clear
void clear()
Definition: streams.h:241
CVectorWriter::vchData
std::vector< unsigned char > & vchData
Definition: streams.h:127
CDataStream::difference_type
vector_type::difference_type difference_type
Definition: streams.h:197
CDataStream::operator<<
CDataStream & operator<<(const T &obj)
Definition: streams.h:397
std
Definition: setup_common.h:33
CDataStream::const_iterator
vector_type::const_iterator const_iterator
Definition: streams.h:202
CBufferedFile::GetType
int GetType() const
Definition: streams.h:703
ByteUnit::T
@ T
CDataStream::vector_type
SerializeData vector_type
Definition: streams.h:187
serialize.h
CAutoFile::write
void write(const char *pch, size_t nSize)
Definition: streams.h:618
CDataStream::nReadPos
unsigned int nReadPos
Definition: streams.h:189
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
OverrideStream::nType
const int nType
Definition: streams.h:30
zeroafterfree.h
OverrideStream::write
void write(const char *pch, size_t nSize)
Definition: streams.h:52
BitStreamWriter::m_buffer
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
Definition: streams.h:487
CVectorWriter::GetVersion
int GetVersion() const
Definition: streams.h:116
SpanReader::SpanReader
SpanReader(int type, int version, Span< const unsigned char > data)
Definition: streams.h:147
CDataStream::empty
bool empty() const
Definition: streams.h:236
CAutoFile::release
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:579
SpanReader::empty
bool empty() const
Definition: streams.h:162
CBufferedFile::nReadLimit
uint64_t nReadLimit
up to which position we're allowed to read
Definition: streams.h:662
CVectorWriter::write
void write(const char *pch, size_t nSize)
Definition: streams.h:97
CDataStream::Serialize
void Serialize(Stream &s) const
Definition: streams.h:389
BitStreamReader::BitStreamReader
BitStreamReader(IStream &istream)
Definition: streams.h:452
CDataStream::str
std::string str() const
Definition: streams.h:222
CBufferedFile::CBufferedFile
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:685
CDataStream::rdbuf
CDataStream * rdbuf()
Definition: streams.h:337
CDataStream::SetType
void SetType(int n)
Definition: streams.h:340
OverrideStream::GetVersion
int GetVersion() const
Definition: streams.h:62
Serialize
void Serialize(Stream &s, char a)
Definition: serialize.h:199
OverrideStream::size
size_t size() const
Definition: streams.h:64
CDataStream::eof
bool eof() const
Definition: streams.h:336
CBufferedFile::GetPos
uint64_t GetPos() const
return the current reading position
Definition: streams.h:739
CDataStream::iterator
vector_type::iterator iterator
Definition: streams.h:201
BitStreamReader::m_istream
IStream & m_istream
Definition: streams.h:440
Span::subspan
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:194
args
ArgsManager args
Definition: notifications.cpp:36
OverrideStream::operator<<
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:37