Bitcoin Core  21.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-2020 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>
110  CVectorWriter& operator<<(const T& obj)
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;
138  const std::vector<unsigned char>& m_data;
139  size_t m_pos = 0;
140 
141 public:
142 
149  VectorReader(int type, int version, const std::vector<unsigned char>& data, size_t pos)
150  : m_type(type), m_version(version), m_data(data), m_pos(pos)
151  {
152  if (m_pos > m_data.size()) {
153  throw std::ios_base::failure("VectorReader(...): end of data (m_pos > m_data.size())");
154  }
155  }
156 
161  template <typename... Args>
162  VectorReader(int type, int version, const std::vector<unsigned char>& data, size_t pos,
163  Args&&... args)
164  : VectorReader(type, version, data, pos)
165  {
166  ::UnserializeMany(*this, std::forward<Args>(args)...);
167  }
168 
169  template<typename T>
171  {
172  // Unserialize from this stream
173  ::Unserialize(*this, obj);
174  return (*this);
175  }
176 
177  int GetVersion() const { return m_version; }
178  int GetType() const { return m_type; }
179 
180  size_t size() const { return m_data.size() - m_pos; }
181  bool empty() const { return m_data.size() == m_pos; }
182 
183  void read(char* dst, size_t n)
184  {
185  if (n == 0) {
186  return;
187  }
188 
189  // Read from the beginning of the buffer
190  size_t pos_next = m_pos + n;
191  if (pos_next > m_data.size()) {
192  throw std::ios_base::failure("VectorReader::read(): end of data");
193  }
194  memcpy(dst, m_data.data() + m_pos, n);
195  m_pos = pos_next;
196  }
197 };
198 
205 {
206 protected:
209  unsigned int nReadPos{0};
210 
211  int nType;
212  int nVersion;
213 
214 public:
215  typedef vector_type::allocator_type allocator_type;
216  typedef vector_type::size_type size_type;
217  typedef vector_type::difference_type difference_type;
218  typedef vector_type::reference reference;
219  typedef vector_type::const_reference const_reference;
220  typedef vector_type::value_type value_type;
221  typedef vector_type::iterator iterator;
222  typedef vector_type::const_iterator const_iterator;
223  typedef vector_type::reverse_iterator reverse_iterator;
224 
225  explicit CDataStream(int nTypeIn, int nVersionIn)
226  : nType{nTypeIn},
227  nVersion{nVersionIn} {}
228 
229  explicit CDataStream(Span<const uint8_t> sp, int nTypeIn, int nVersionIn)
230  : vch(sp.data(), sp.data() + sp.size()),
231  nType{nTypeIn},
232  nVersion{nVersionIn} {}
233 
234  template <typename... Args>
235  CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
236  : nType{nTypeIn},
237  nVersion{nVersionIn}
238  {
239  ::SerializeMany(*this, std::forward<Args>(args)...);
240  }
241 
242  std::string str() const
243  {
244  return (std::string(begin(), end()));
245  }
246 
247 
248  //
249  // Vector subset
250  //
251  const_iterator begin() const { return vch.begin() + nReadPos; }
252  iterator begin() { return vch.begin() + nReadPos; }
253  const_iterator end() const { return vch.end(); }
254  iterator end() { return vch.end(); }
255  size_type size() const { return vch.size() - nReadPos; }
256  bool empty() const { return vch.size() == nReadPos; }
257  void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
258  void reserve(size_type n) { vch.reserve(n + nReadPos); }
259  const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
260  reference operator[](size_type pos) { return vch[pos + nReadPos]; }
261  void clear() { vch.clear(); nReadPos = 0; }
262  iterator insert(iterator it, const uint8_t x) { return vch.insert(it, x); }
263  void insert(iterator it, size_type n, const uint8_t x) { vch.insert(it, n, x); }
264  value_type* data() { return vch.data() + nReadPos; }
265  const value_type* data() const { return vch.data() + nReadPos; }
266 
267  void insert(iterator it, std::vector<uint8_t>::const_iterator first, std::vector<uint8_t>::const_iterator last)
268  {
269  if (last == first) return;
270  assert(last - first > 0);
271  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
272  {
273  // special case for inserting at the front when there's room
274  nReadPos -= (last - first);
275  memcpy(&vch[nReadPos], &first[0], last - first);
276  }
277  else
278  vch.insert(it, first, last);
279  }
280 
281  void insert(iterator it, const char* first, const char* last)
282  {
283  if (last == first) return;
284  assert(last - first > 0);
285  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
286  {
287  // special case for inserting at the front when there's room
288  nReadPos -= (last - first);
289  memcpy(&vch[nReadPos], &first[0], last - first);
290  }
291  else
292  vch.insert(it, first, last);
293  }
294 
296  {
297  if (it == vch.begin() + nReadPos)
298  {
299  // special case for erasing from the front
300  if (++nReadPos >= vch.size())
301  {
302  // whenever we reach the end, we take the opportunity to clear the buffer
303  nReadPos = 0;
304  return vch.erase(vch.begin(), vch.end());
305  }
306  return vch.begin() + nReadPos;
307  }
308  else
309  return vch.erase(it);
310  }
311 
313  {
314  if (first == vch.begin() + nReadPos)
315  {
316  // special case for erasing from the front
317  if (last == vch.end())
318  {
319  nReadPos = 0;
320  return vch.erase(vch.begin(), vch.end());
321  }
322  else
323  {
324  nReadPos = (last - vch.begin());
325  return last;
326  }
327  }
328  else
329  return vch.erase(first, last);
330  }
331 
332  inline void Compact()
333  {
334  vch.erase(vch.begin(), vch.begin() + nReadPos);
335  nReadPos = 0;
336  }
337 
338  bool Rewind(std::optional<size_type> n = std::nullopt)
339  {
340  // Total rewind if no size is passed
341  if (!n) {
342  nReadPos = 0;
343  return true;
344  }
345  // Rewind by n characters if the buffer hasn't been compacted yet
346  if (*n > nReadPos)
347  return false;
348  nReadPos -= *n;
349  return true;
350  }
351 
352 
353  //
354  // Stream subset
355  //
356  bool eof() const { return size() == 0; }
357  CDataStream* rdbuf() { return this; }
358  int in_avail() const { return size(); }
359 
360  void SetType(int n) { nType = n; }
361  int GetType() const { return nType; }
362  void SetVersion(int n) { nVersion = n; }
363  int GetVersion() const { return nVersion; }
364 
365  void read(char* pch, size_t nSize)
366  {
367  if (nSize == 0) return;
368 
369  // Read from the beginning of the buffer
370  unsigned int nReadPosNext = nReadPos + nSize;
371  if (nReadPosNext > vch.size()) {
372  throw std::ios_base::failure("CDataStream::read(): end of data");
373  }
374  memcpy(pch, &vch[nReadPos], nSize);
375  if (nReadPosNext == vch.size())
376  {
377  nReadPos = 0;
378  vch.clear();
379  return;
380  }
381  nReadPos = nReadPosNext;
382  }
383 
384  void ignore(int nSize)
385  {
386  // Ignore from the beginning of the buffer
387  if (nSize < 0) {
388  throw std::ios_base::failure("CDataStream::ignore(): nSize negative");
389  }
390  unsigned int nReadPosNext = nReadPos + nSize;
391  if (nReadPosNext >= vch.size())
392  {
393  if (nReadPosNext > vch.size())
394  throw std::ios_base::failure("CDataStream::ignore(): end of data");
395  nReadPos = 0;
396  vch.clear();
397  return;
398  }
399  nReadPos = nReadPosNext;
400  }
401 
402  void write(const char* pch, size_t nSize)
403  {
404  // Write to the end of the buffer
405  vch.insert(vch.end(), pch, pch + nSize);
406  }
407 
408  template<typename Stream>
409  void Serialize(Stream& s) const
410  {
411  // Special case: stream << stream concatenates like stream += stream
412  if (!vch.empty())
413  s.write((char*)vch.data(), vch.size() * sizeof(value_type));
414  }
415 
416  template<typename T>
417  CDataStream& operator<<(const T& obj)
418  {
419  // Serialize to this stream
420  ::Serialize(*this, obj);
421  return (*this);
422  }
423 
424  template<typename T>
426  {
427  // Unserialize from this stream
428  ::Unserialize(*this, obj);
429  return (*this);
430  }
431 
437  void Xor(const std::vector<unsigned char>& key)
438  {
439  if (key.size() == 0) {
440  return;
441  }
442 
443  for (size_type i = 0, j = 0; i != size(); i++) {
444  vch[i] ^= key[j++];
445 
446  // This potentially acts on very many bytes of data, so it's
447  // important that we calculate `j`, i.e. the `key` index in this
448  // way instead of doing a %, which would effectively be a division
449  // for each byte Xor'd -- much slower than need be.
450  if (j == key.size())
451  j = 0;
452  }
453  }
454 };
455 
456 template <typename IStream>
458 {
459 private:
460  IStream& m_istream;
461 
464  uint8_t m_buffer{0};
465 
469  int m_offset{8};
470 
471 public:
472  explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
473 
477  uint64_t Read(int nbits) {
478  if (nbits < 0 || nbits > 64) {
479  throw std::out_of_range("nbits must be between 0 and 64");
480  }
481 
482  uint64_t data = 0;
483  while (nbits > 0) {
484  if (m_offset == 8) {
485  m_istream >> m_buffer;
486  m_offset = 0;
487  }
488 
489  int bits = std::min(8 - m_offset, nbits);
490  data <<= bits;
491  data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
492  m_offset += bits;
493  nbits -= bits;
494  }
495  return data;
496  }
497 };
498 
499 template <typename OStream>
501 {
502 private:
503  OStream& m_ostream;
504 
507  uint8_t m_buffer{0};
508 
512  int m_offset{0};
513 
514 public:
515  explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
516 
518  {
519  Flush();
520  }
521 
525  void Write(uint64_t data, int nbits) {
526  if (nbits < 0 || nbits > 64) {
527  throw std::out_of_range("nbits must be between 0 and 64");
528  }
529 
530  while (nbits > 0) {
531  int bits = std::min(8 - m_offset, nbits);
532  m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
533  m_offset += bits;
534  nbits -= bits;
535 
536  if (m_offset == 8) {
537  Flush();
538  }
539  }
540  }
541 
545  void Flush() {
546  if (m_offset == 0) {
547  return;
548  }
549 
550  m_ostream << m_buffer;
551  m_buffer = 0;
552  m_offset = 0;
553  }
554 };
555 
556 
557 
565 {
566 private:
567  const int nType;
568  const int nVersion;
569 
570  FILE* file;
571 
572 public:
573  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
574  {
575  file = filenew;
576  }
577 
579  {
580  fclose();
581  }
582 
583  // Disallow copies
584  CAutoFile(const CAutoFile&) = delete;
585  CAutoFile& operator=(const CAutoFile&) = delete;
586 
587  void fclose()
588  {
589  if (file) {
590  ::fclose(file);
591  file = nullptr;
592  }
593  }
594 
599  FILE* release() { FILE* ret = file; file = nullptr; return ret; }
600 
605  FILE* Get() const { return file; }
606 
609  bool IsNull() const { return (file == nullptr); }
610 
611  //
612  // Stream subset
613  //
614  int GetType() const { return nType; }
615  int GetVersion() const { return nVersion; }
616 
617  void read(char* pch, size_t nSize)
618  {
619  if (!file)
620  throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
621  if (fread(pch, 1, nSize, file) != nSize)
622  throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
623  }
624 
625  void ignore(size_t nSize)
626  {
627  if (!file)
628  throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
629  unsigned char data[4096];
630  while (nSize > 0) {
631  size_t nNow = std::min<size_t>(nSize, sizeof(data));
632  if (fread(data, 1, nNow, file) != nNow)
633  throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
634  nSize -= nNow;
635  }
636  }
637 
638  void write(const char* pch, size_t nSize)
639  {
640  if (!file)
641  throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
642  if (fwrite(pch, 1, nSize, file) != nSize)
643  throw std::ios_base::failure("CAutoFile::write: write failed");
644  }
645 
646  template<typename T>
647  CAutoFile& operator<<(const T& obj)
648  {
649  // Serialize to this stream
650  if (!file)
651  throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
652  ::Serialize(*this, obj);
653  return (*this);
654  }
655 
656  template<typename T>
658  {
659  // Unserialize from this stream
660  if (!file)
661  throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
662  ::Unserialize(*this, obj);
663  return (*this);
664  }
665 };
666 
674 {
675 private:
676  const int nType;
677  const int nVersion;
678 
679  FILE *src;
680  uint64_t nSrcPos;
681  uint64_t nReadPos;
682  uint64_t nReadLimit;
683  uint64_t nRewind;
684  std::vector<char> vchBuf;
685 
686 protected:
688  bool Fill() {
689  unsigned int pos = nSrcPos % vchBuf.size();
690  unsigned int readNow = vchBuf.size() - pos;
691  unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
692  if (nAvail < readNow)
693  readNow = nAvail;
694  if (readNow == 0)
695  return false;
696  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
697  if (nBytes == 0) {
698  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
699  }
700  nSrcPos += nBytes;
701  return true;
702  }
703 
704 public:
705  CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
706  nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0)
707  {
708  if (nRewindIn >= nBufSize)
709  throw std::ios_base::failure("Rewind limit must be less than buffer size");
710  src = fileIn;
711  }
712 
714  {
715  fclose();
716  }
717 
718  // Disallow copies
719  CBufferedFile(const CBufferedFile&) = delete;
720  CBufferedFile& operator=(const CBufferedFile&) = delete;
721 
722  int GetVersion() const { return nVersion; }
723  int GetType() const { return nType; }
724 
725  void fclose()
726  {
727  if (src) {
728  ::fclose(src);
729  src = nullptr;
730  }
731  }
732 
734  bool eof() const {
735  return nReadPos == nSrcPos && feof(src);
736  }
737 
739  void read(char *pch, size_t nSize) {
740  if (nSize + nReadPos > nReadLimit)
741  throw std::ios_base::failure("Read attempted past buffer limit");
742  while (nSize > 0) {
743  if (nReadPos == nSrcPos)
744  Fill();
745  unsigned int pos = nReadPos % vchBuf.size();
746  size_t nNow = nSize;
747  if (nNow + pos > vchBuf.size())
748  nNow = vchBuf.size() - pos;
749  if (nNow + nReadPos > nSrcPos)
750  nNow = nSrcPos - nReadPos;
751  memcpy(pch, &vchBuf[pos], nNow);
752  nReadPos += nNow;
753  pch += nNow;
754  nSize -= nNow;
755  }
756  }
757 
759  uint64_t GetPos() const {
760  return nReadPos;
761  }
762 
764  bool SetPos(uint64_t nPos) {
765  size_t bufsize = vchBuf.size();
766  if (nPos + bufsize < nSrcPos) {
767  // rewinding too far, rewind as far as possible
768  nReadPos = nSrcPos - bufsize;
769  return false;
770  }
771  if (nPos > nSrcPos) {
772  // can't go this far forward, go as far as possible
773  nReadPos = nSrcPos;
774  return false;
775  }
776  nReadPos = nPos;
777  return true;
778  }
779 
782  bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
783  if (nPos < nReadPos)
784  return false;
785  nReadLimit = nPos;
786  return true;
787  }
788 
789  template<typename T>
791  // Unserialize from this stream
792  ::Unserialize(*this, obj);
793  return (*this);
794  }
795 
797  void FindByte(char ch) {
798  while (true) {
799  if (nReadPos == nSrcPos)
800  Fill();
801  if (vchBuf[nReadPos % vchBuf.size()] == ch)
802  break;
803  nReadPos++;
804  }
805  }
806 };
807 
808 #endif // BITCOIN_STREAMS_H
CAutoFile::operator<<
CAutoFile & operator<<(const T &obj)
Definition: streams.h:647
CDataStream::end
iterator end()
Definition: streams.h:254
BitStreamWriter::m_ostream
OStream & m_ostream
Definition: streams.h:503
CDataStream::value_type
vector_type::value_type value_type
Definition: streams.h:220
CBufferedFile::read
void read(char *pch, size_t nSize)
read a number of bytes
Definition: streams.h:739
CDataStream::in_avail
int in_avail() const
Definition: streams.h:358
VectorReader::VectorReader
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos, Args &&... args)
(other params same as above)
Definition: streams.h:162
CDataStream::size_type
vector_type::size_type size_type
Definition: streams.h:216
CAutoFile::file
FILE * file
Definition: streams.h:570
CDataStream::insert
iterator insert(iterator it, const uint8_t x)
Definition: streams.h:262
BitStreamReader
Definition: streams.h:457
CDataStream::erase
iterator erase(iterator first, iterator last)
Definition: streams.h:312
CAutoFile::ignore
void ignore(size_t nSize)
Definition: streams.h:625
CDataStream::vch
vector_type vch
Definition: streams.h:208
BitStreamWriter::Flush
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:545
CBufferedFile::operator>>
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:790
OverrideStream::read
void read(char *pch, size_t nSize)
Definition: streams.h:57
CVectorWriter
Definition: streams.h:72
CDataStream::insert
void insert(iterator it, std::vector< uint8_t >::const_iterator first, std::vector< uint8_t >::const_iterator last)
Definition: streams.h:267
CDataStream::begin
const_iterator begin() const
Definition: streams.h:251
CBufferedFile::SetPos
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:764
SerializeData
std::vector< uint8_t, zero_after_free_allocator< uint8_t > > SerializeData
Byte-vector that clears its contents before deletion.
Definition: zeroafterfree.h:46
CDataStream::Xor
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:437
VectorReader::m_pos
size_t m_pos
Definition: streams.h:139
VectorReader::read
void read(char *dst, size_t n)
Definition: streams.h:183
CDataStream::data
value_type * data()
Definition: streams.h:264
BitStreamReader::m_buffer
uint8_t m_buffer
Buffered byte read in from the input stream.
Definition: streams.h:464
string.h
CAutoFile::Get
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:605
OverrideStream::operator>>
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:45
CAutoFile::nType
const int nType
Definition: streams.h:567
VectorReader::operator>>
VectorReader & operator>>(T &obj)
Definition: streams.h:170
CAutoFile::read
void read(char *pch, size_t nSize)
Definition: streams.h:617
VectorReader::VectorReader
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos)
Definition: streams.h:149
CDataStream::reference
vector_type::reference reference
Definition: streams.h:218
CBufferedFile::fclose
void fclose()
Definition: streams.h:725
BitStreamReader::m_offset
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
Definition: streams.h:469
CDataStream::Compact
void Compact()
Definition: streams.h:332
CDataStream::begin
iterator begin()
Definition: streams.h:252
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:797
VectorReader::GetType
int GetType() const
Definition: streams.h:178
CAutoFile::~CAutoFile
~CAutoFile()
Definition: streams.h:578
VectorReader
Minimal stream for reading from an existing vector by reference.
Definition: streams.h:133
CDataStream::read
void read(char *pch, size_t nSize)
Definition: streams.h:365
BitStreamWriter
Definition: streams.h:500
CAutoFile::nVersion
const int nVersion
Definition: streams.h:568
OverrideStream::OverrideStream
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:34
CBufferedFile::src
FILE * src
source file
Definition: streams.h:679
CBufferedFile::nRewind
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:683
CDataStream::write
void write(const char *pch, size_t nSize)
Definition: streams.h:402
CDataStream::GetType
int GetType() const
Definition: streams.h:361
CVectorWriter::nVersion
const int nVersion
Definition: streams.h:126
CAutoFile::GetVersion
int GetVersion() const
Definition: streams.h:615
CDataStream::CDataStream
CDataStream(Span< const uint8_t > sp, int nTypeIn, int nVersionIn)
Definition: streams.h:229
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:564
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
CDataStream::const_reference
vector_type::const_reference const_reference
Definition: streams.h:219
CDataStream::nVersion
int nVersion
Definition: streams.h:212
VectorReader::empty
bool empty() const
Definition: streams.h:181
BitStreamReader::Read
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:477
CBufferedFile::~CBufferedFile
~CBufferedFile()
Definition: streams.h:713
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:688
CDataStream::allocator_type
vector_type::allocator_type allocator_type
Definition: streams.h:215
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:245
CBufferedFile
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: streams.h:673
CBufferedFile::GetVersion
int GetVersion() const
Definition: streams.h:722
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:525
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:734
CDataStream::CDataStream
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:225
CDataStream::operator[]
const_reference operator[](size_type pos) const
Definition: streams.h:259
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:680
CBufferedFile::nReadPos
uint64_t nReadPos
how many bytes have been read from this
Definition: streams.h:681
CDataStream::end
const_iterator end() const
Definition: streams.h:253
VectorReader::m_data
const std::vector< unsigned char > & m_data
Definition: streams.h:138
CDataStream::nType
int nType
Definition: streams.h:211
CBufferedFile::vchBuf
std::vector< char > vchBuf
the buffer
Definition: streams.h:684
VectorReader::m_version
const int m_version
Definition: streams.h:137
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:512
CAutoFile::CAutoFile
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:573
CDataStream::Rewind
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:338
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:782
CDataStream::erase
iterator erase(iterator it)
Definition: streams.h:295
CAutoFile::operator>>
CAutoFile & operator>>(T &&obj)
Definition: streams.h:657
CAutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:609
BitStreamWriter::BitStreamWriter
BitStreamWriter(OStream &ostream)
Definition: streams.h:515
UnserializeMany
void UnserializeMany(Stream &s)
Definition: serialize.h:1059
SerializeMany
void SerializeMany(Stream &s)
Definition: serialize.h:1047
CAutoFile::fclose
void fclose()
Definition: streams.h:587
CDataStream::reverse_iterator
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:223
CDataStream::reserve
void reserve(size_type n)
Definition: streams.h:258
CAutoFile::GetType
int GetType() const
Definition: streams.h:614
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:255
CDataStream::SetVersion
void SetVersion(int n)
Definition: streams.h:362
VectorReader::size
size_t size() const
Definition: streams.h:180
CVectorWriter::CVectorWriter
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:83
CDataStream::ignore
void ignore(int nSize)
Definition: streams.h:384
CDataStream::data
const value_type * data() const
Definition: streams.h:265
OverrideStream
Definition: streams.h:26
CBufferedFile::nType
const int nType
Definition: streams.h:676
CDataStream::operator[]
reference operator[](size_type pos)
Definition: streams.h:260
CBufferedFile::nVersion
const int nVersion
Definition: streams.h:677
CVectorWriter::operator<<
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:110
BitStreamWriter::~BitStreamWriter
~BitStreamWriter()
Definition: streams.h:517
CVectorWriter::nType
const int nType
Definition: streams.h:125
CDataStream::GetVersion
int GetVersion() const
Definition: streams.h:363
CDataStream::operator>>
CDataStream & operator>>(T &&obj)
Definition: streams.h:425
CDataStream::CDataStream
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:235
CDataStream::insert
void insert(iterator it, size_type n, const uint8_t x)
Definition: streams.h:263
CAutoFile::operator=
CAutoFile & operator=(const CAutoFile &)=delete
CDataStream::clear
void clear()
Definition: streams.h:261
CVectorWriter::vchData
std::vector< unsigned char > & vchData
Definition: streams.h:127
CDataStream::difference_type
vector_type::difference_type difference_type
Definition: streams.h:217
CDataStream::operator<<
CDataStream & operator<<(const T &obj)
Definition: streams.h:417
CDataStream::const_iterator
vector_type::const_iterator const_iterator
Definition: streams.h:222
CBufferedFile::GetType
int GetType() const
Definition: streams.h:723
CDataStream::resize
void resize(size_type n, value_type c=0)
Definition: streams.h:257
CDataStream::vector_type
SerializeData vector_type
Definition: streams.h:207
serialize.h
CAutoFile::write
void write(const char *pch, size_t nSize)
Definition: streams.h:638
CDataStream::nReadPos
unsigned int nReadPos
Definition: streams.h:209
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
OverrideStream::nType
const int nType
Definition: streams.h:30
CDataStream::insert
void insert(iterator it, const char *first, const char *last)
Definition: streams.h:281
VectorReader::GetVersion
int GetVersion() const
Definition: streams.h:177
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:507
CVectorWriter::GetVersion
int GetVersion() const
Definition: streams.h:116
CDataStream::empty
bool empty() const
Definition: streams.h:256
CAutoFile::release
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:599
CBufferedFile::nReadLimit
uint64_t nReadLimit
up to which position we're allowed to read
Definition: streams.h:682
CVectorWriter::write
void write(const char *pch, size_t nSize)
Definition: streams.h:97
CDataStream::Serialize
void Serialize(Stream &s) const
Definition: streams.h:409
BitStreamReader::BitStreamReader
BitStreamReader(IStream &istream)
Definition: streams.h:472
CDataStream::str
std::string str() const
Definition: streams.h:242
CBufferedFile::CBufferedFile
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:705
CDataStream::rdbuf
CDataStream * rdbuf()
Definition: streams.h:357
CDataStream::SetType
void SetType(int n)
Definition: streams.h:360
OverrideStream::GetVersion
int GetVersion() const
Definition: streams.h:62
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
Serialize
void Serialize(Stream &s, char a)
Definition: serialize.h:227
OverrideStream::size
size_t size() const
Definition: streams.h:64
CDataStream::eof
bool eof() const
Definition: streams.h:356
CBufferedFile::GetPos
uint64_t GetPos() const
return the current reading position
Definition: streams.h:759
CDataStream::iterator
vector_type::iterator iterator
Definition: streams.h:221
BitStreamReader::m_istream
IStream & m_istream
Definition: streams.h:460
it
auto it
Definition: validation.cpp:399
VectorReader::m_type
const int m_type
Definition: streams.h:136
OverrideStream::operator<<
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:37