Bitcoin Core  23.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 #include <util/overflow.h>
13 
14 #include <algorithm>
15 #include <assert.h>
16 #include <ios>
17 #include <limits>
18 #include <optional>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 template<typename Stream>
28 {
29  Stream* stream;
30 
31  const int nType;
32  const int nVersion;
33 
34 public:
35  OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
36 
37  template<typename T>
39  {
40  // Serialize to this stream
41  ::Serialize(*this, obj);
42  return (*this);
43  }
44 
45  template<typename T>
47  {
48  // Unserialize from this stream
49  ::Unserialize(*this, obj);
50  return (*this);
51  }
52 
54  {
55  stream->write(src);
56  }
57 
59  {
60  stream->read(dst);
61  }
62 
63  int GetVersion() const { return nVersion; }
64  int GetType() const { return nType; }
65  size_t size() const { return stream->size(); }
66  void ignore(size_t size) { return stream->ignore(size); }
67 };
68 
69 /* Minimal stream for overwriting and/or appending to an existing byte vector
70  *
71  * The referenced vector will grow as necessary
72  */
74 {
75  public:
76 
77 /*
78  * @param[in] nTypeIn Serialization Type
79  * @param[in] nVersionIn Serialization Version (including any flags)
80  * @param[in] vchDataIn Referenced byte vector to overwrite/append
81  * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
82  * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
83 */
84  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
85  {
86  if(nPos > vchData.size())
87  vchData.resize(nPos);
88  }
89 /*
90  * (other params same as above)
91  * @param[in] args A list of items to serialize starting at nPosIn.
92 */
93  template <typename... Args>
94  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
95  {
96  ::SerializeMany(*this, std::forward<Args>(args)...);
97  }
99  {
100  assert(nPos <= vchData.size());
101  size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
102  if (nOverwrite) {
103  memcpy(vchData.data() + nPos, src.data(), nOverwrite);
104  }
105  if (nOverwrite < src.size()) {
106  vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.end()));
107  }
108  nPos += src.size();
109  }
110  template<typename T>
112  {
113  // Serialize to this stream
114  ::Serialize(*this, obj);
115  return (*this);
116  }
117  int GetVersion() const
118  {
119  return nVersion;
120  }
121  int GetType() const
122  {
123  return nType;
124  }
125 private:
126  const int nType;
127  const int nVersion;
128  std::vector<unsigned char>& vchData;
129  size_t nPos;
130 };
131 
135 {
136 private:
137  const int m_type;
138  const int m_version;
140 
141 public:
142 
148  SpanReader(int type, int version, Span<const unsigned char> data)
149  : m_type(type), m_version(version), m_data(data) {}
150 
151  template<typename T>
153  {
154  // Unserialize from this stream
155  ::Unserialize(*this, obj);
156  return (*this);
157  }
158 
159  int GetVersion() const { return m_version; }
160  int GetType() const { return m_type; }
161 
162  size_t size() const { return m_data.size(); }
163  bool empty() const { return m_data.empty(); }
164 
166  {
167  if (dst.size() == 0) {
168  return;
169  }
170 
171  // Read from the beginning of the buffer
172  if (dst.size() > m_data.size()) {
173  throw std::ios_base::failure("SpanReader::read(): end of data");
174  }
175  memcpy(dst.data(), m_data.data(), dst.size());
176  m_data = m_data.subspan(dst.size());
177  }
178 };
179 
186 {
187 protected:
190  vector_type::size_type m_read_pos{0};
191 
192  int nType;
193  int nVersion;
194 
195 public:
196  typedef vector_type::allocator_type allocator_type;
197  typedef vector_type::size_type size_type;
198  typedef vector_type::difference_type difference_type;
199  typedef vector_type::reference reference;
200  typedef vector_type::const_reference const_reference;
201  typedef vector_type::value_type value_type;
202  typedef vector_type::iterator iterator;
203  typedef vector_type::const_iterator const_iterator;
204  typedef vector_type::reverse_iterator reverse_iterator;
205 
206  explicit CDataStream(int nTypeIn, int nVersionIn)
207  : nType{nTypeIn},
208  nVersion{nVersionIn} {}
209 
210  explicit CDataStream(Span<const uint8_t> sp, int type, int version) : CDataStream{AsBytes(sp), type, version} {}
211  explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
212  : vch(sp.data(), sp.data() + sp.size()),
213  nType{nTypeIn},
214  nVersion{nVersionIn} {}
215 
216  template <typename... Args>
217  CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
218  : nType{nTypeIn},
219  nVersion{nVersionIn}
220  {
221  ::SerializeMany(*this, std::forward<Args>(args)...);
222  }
223 
224  std::string str() const
225  {
226  return std::string{UCharCast(data()), UCharCast(data() + size())};
227  }
228 
229 
230  //
231  // Vector subset
232  //
233  const_iterator begin() const { return vch.begin() + m_read_pos; }
234  iterator begin() { return vch.begin() + m_read_pos; }
235  const_iterator end() const { return vch.end(); }
236  iterator end() { return vch.end(); }
237  size_type size() const { return vch.size() - m_read_pos; }
238  bool empty() const { return vch.size() == m_read_pos; }
239  void resize(size_type n, value_type c = value_type{}) { vch.resize(n + m_read_pos, c); }
240  void reserve(size_type n) { vch.reserve(n + m_read_pos); }
241  const_reference operator[](size_type pos) const { return vch[pos + m_read_pos]; }
242  reference operator[](size_type pos) { return vch[pos + m_read_pos]; }
243  void clear() { vch.clear(); m_read_pos = 0; }
244  value_type* data() { return vch.data() + m_read_pos; }
245  const value_type* data() const { return vch.data() + m_read_pos; }
246 
247  inline void Compact()
248  {
249  vch.erase(vch.begin(), vch.begin() + m_read_pos);
250  m_read_pos = 0;
251  }
252 
253  bool Rewind(std::optional<size_type> n = std::nullopt)
254  {
255  // Total rewind if no size is passed
256  if (!n) {
257  m_read_pos = 0;
258  return true;
259  }
260  // Rewind by n characters if the buffer hasn't been compacted yet
261  if (*n > m_read_pos)
262  return false;
263  m_read_pos -= *n;
264  return true;
265  }
266 
267 
268  //
269  // Stream subset
270  //
271  bool eof() const { return size() == 0; }
272  CDataStream* rdbuf() { return this; }
273  int in_avail() const { return size(); }
274 
275  void SetType(int n) { nType = n; }
276  int GetType() const { return nType; }
277  void SetVersion(int n) { nVersion = n; }
278  int GetVersion() const { return nVersion; }
279 
281  {
282  if (dst.size() == 0) return;
283 
284  // Read from the beginning of the buffer
285  auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
286  if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
287  throw std::ios_base::failure("CDataStream::read(): end of data");
288  }
289  memcpy(dst.data(), &vch[m_read_pos], dst.size());
290  if (next_read_pos.value() == vch.size()) {
291  m_read_pos = 0;
292  vch.clear();
293  return;
294  }
295  m_read_pos = next_read_pos.value();
296  }
297 
298  void ignore(size_t num_ignore)
299  {
300  // Ignore from the beginning of the buffer
301  auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
302  if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
303  throw std::ios_base::failure("CDataStream::ignore(): end of data");
304  }
305  if (next_read_pos.value() == vch.size()) {
306  m_read_pos = 0;
307  vch.clear();
308  return;
309  }
310  m_read_pos = next_read_pos.value();
311  }
312 
314  {
315  // Write to the end of the buffer
316  vch.insert(vch.end(), src.begin(), src.end());
317  }
318 
319  template<typename Stream>
320  void Serialize(Stream& s) const
321  {
322  // Special case: stream << stream concatenates like stream += stream
323  if (!vch.empty())
324  s.write(MakeByteSpan(vch));
325  }
326 
327  template<typename T>
328  CDataStream& operator<<(const T& obj)
329  {
330  // Serialize to this stream
331  ::Serialize(*this, obj);
332  return (*this);
333  }
334 
335  template<typename T>
337  {
338  // Unserialize from this stream
339  ::Unserialize(*this, obj);
340  return (*this);
341  }
342 
348  void Xor(const std::vector<unsigned char>& key)
349  {
350  if (key.size() == 0) {
351  return;
352  }
353 
354  for (size_type i = 0, j = 0; i != size(); i++) {
355  vch[i] ^= std::byte{key[j++]};
356 
357  // This potentially acts on very many bytes of data, so it's
358  // important that we calculate `j`, i.e. the `key` index in this
359  // way instead of doing a %, which would effectively be a division
360  // for each byte Xor'd -- much slower than need be.
361  if (j == key.size())
362  j = 0;
363  }
364  }
365 };
366 
367 template <typename IStream>
369 {
370 private:
371  IStream& m_istream;
372 
375  uint8_t m_buffer{0};
376 
380  int m_offset{8};
381 
382 public:
383  explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
384 
388  uint64_t Read(int nbits) {
389  if (nbits < 0 || nbits > 64) {
390  throw std::out_of_range("nbits must be between 0 and 64");
391  }
392 
393  uint64_t data = 0;
394  while (nbits > 0) {
395  if (m_offset == 8) {
396  m_istream >> m_buffer;
397  m_offset = 0;
398  }
399 
400  int bits = std::min(8 - m_offset, nbits);
401  data <<= bits;
402  data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
403  m_offset += bits;
404  nbits -= bits;
405  }
406  return data;
407  }
408 };
409 
410 template <typename OStream>
412 {
413 private:
414  OStream& m_ostream;
415 
418  uint8_t m_buffer{0};
419 
423  int m_offset{0};
424 
425 public:
426  explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
427 
429  {
430  Flush();
431  }
432 
436  void Write(uint64_t data, int nbits) {
437  if (nbits < 0 || nbits > 64) {
438  throw std::out_of_range("nbits must be between 0 and 64");
439  }
440 
441  while (nbits > 0) {
442  int bits = std::min(8 - m_offset, nbits);
443  m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
444  m_offset += bits;
445  nbits -= bits;
446 
447  if (m_offset == 8) {
448  Flush();
449  }
450  }
451  }
452 
456  void Flush() {
457  if (m_offset == 0) {
458  return;
459  }
460 
461  m_ostream << m_buffer;
462  m_buffer = 0;
463  m_offset = 0;
464  }
465 };
466 
467 
474 class AutoFile
475 {
476 protected:
477  FILE* file;
478 
479 public:
480  explicit AutoFile(FILE* filenew) : file{filenew} {}
481 
483  {
484  fclose();
485  }
486 
487  // Disallow copies
488  AutoFile(const AutoFile&) = delete;
489  AutoFile& operator=(const AutoFile&) = delete;
490 
491  void fclose()
492  {
493  if (file) {
494  ::fclose(file);
495  file = nullptr;
496  }
497  }
498 
503  FILE* release() { FILE* ret = file; file = nullptr; return ret; }
504 
509  FILE* Get() const { return file; }
510 
513  bool IsNull() const { return (file == nullptr); }
514 
515  //
516  // Stream subset
517  //
519  {
520  if (!file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
521  if (fread(dst.data(), 1, dst.size(), file) != dst.size()) {
522  throw std::ios_base::failure(feof(file) ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
523  }
524  }
525 
526  void ignore(size_t nSize)
527  {
528  if (!file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
529  unsigned char data[4096];
530  while (nSize > 0) {
531  size_t nNow = std::min<size_t>(nSize, sizeof(data));
532  if (fread(data, 1, nNow, file) != nNow)
533  throw std::ios_base::failure(feof(file) ? "AutoFile::ignore: end of file" : "AutoFile::read: fread failed");
534  nSize -= nNow;
535  }
536  }
537 
539  {
540  if (!file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
541  if (fwrite(src.data(), 1, src.size(), file) != src.size()) {
542  throw std::ios_base::failure("AutoFile::write: write failed");
543  }
544  }
545 
546  template <typename T>
547  AutoFile& operator<<(const T& obj)
548  {
549  if (!file) throw std::ios_base::failure("AutoFile::operator<<: file handle is nullptr");
550  ::Serialize(*this, obj);
551  return *this;
552  }
553 
554  template <typename T>
556  {
557  if (!file) throw std::ios_base::failure("AutoFile::operator>>: file handle is nullptr");
558  ::Unserialize(*this, obj);
559  return *this;
560  }
561 };
562 
563 class CAutoFile : public AutoFile
564 {
565 private:
566  const int nType;
567  const int nVersion;
568 
569 public:
570  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : AutoFile{filenew}, nType(nTypeIn), nVersion(nVersionIn) {}
571  int GetType() const { return nType; }
572  int GetVersion() const { return nVersion; }
573 
574  template<typename T>
575  CAutoFile& operator<<(const T& obj)
576  {
577  // Serialize to this stream
578  if (!file)
579  throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
580  ::Serialize(*this, obj);
581  return (*this);
582  }
583 
584  template<typename T>
586  {
587  // Unserialize from this stream
588  if (!file)
589  throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
590  ::Unserialize(*this, obj);
591  return (*this);
592  }
593 };
594 
602 {
603 private:
604  const int nType;
605  const int nVersion;
606 
607  FILE *src;
608  uint64_t nSrcPos;
609  uint64_t m_read_pos;
610  uint64_t nReadLimit;
611  uint64_t nRewind;
612  std::vector<std::byte> vchBuf;
613 
614 protected:
616  bool Fill() {
617  unsigned int pos = nSrcPos % vchBuf.size();
618  unsigned int readNow = vchBuf.size() - pos;
619  unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind;
620  if (nAvail < readNow)
621  readNow = nAvail;
622  if (readNow == 0)
623  return false;
624  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
625  if (nBytes == 0) {
626  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
627  }
628  nSrcPos += nBytes;
629  return true;
630  }
631 
632 public:
633  CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
634  : nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), m_read_pos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, std::byte{0})
635  {
636  if (nRewindIn >= nBufSize)
637  throw std::ios_base::failure("Rewind limit must be less than buffer size");
638  src = fileIn;
639  }
640 
642  {
643  fclose();
644  }
645 
646  // Disallow copies
647  CBufferedFile(const CBufferedFile&) = delete;
648  CBufferedFile& operator=(const CBufferedFile&) = delete;
649 
650  int GetVersion() const { return nVersion; }
651  int GetType() const { return nType; }
652 
653  void fclose()
654  {
655  if (src) {
656  ::fclose(src);
657  src = nullptr;
658  }
659  }
660 
662  bool eof() const {
663  return m_read_pos == nSrcPos && feof(src);
664  }
665 
668  {
669  if (dst.size() + m_read_pos > nReadLimit) {
670  throw std::ios_base::failure("Read attempted past buffer limit");
671  }
672  while (dst.size() > 0) {
673  if (m_read_pos == nSrcPos)
674  Fill();
675  unsigned int pos = m_read_pos % vchBuf.size();
676  size_t nNow = dst.size();
677  if (nNow + pos > vchBuf.size())
678  nNow = vchBuf.size() - pos;
679  if (nNow + m_read_pos > nSrcPos)
680  nNow = nSrcPos - m_read_pos;
681  memcpy(dst.data(), &vchBuf[pos], nNow);
682  m_read_pos += nNow;
683  dst = dst.subspan(nNow);
684  }
685  }
686 
688  uint64_t GetPos() const {
689  return m_read_pos;
690  }
691 
693  bool SetPos(uint64_t nPos) {
694  size_t bufsize = vchBuf.size();
695  if (nPos + bufsize < nSrcPos) {
696  // rewinding too far, rewind as far as possible
697  m_read_pos = nSrcPos - bufsize;
698  return false;
699  }
700  if (nPos > nSrcPos) {
701  // can't go this far forward, go as far as possible
703  return false;
704  }
705  m_read_pos = nPos;
706  return true;
707  }
708 
711  bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
712  if (nPos < m_read_pos)
713  return false;
714  nReadLimit = nPos;
715  return true;
716  }
717 
718  template<typename T>
720  // Unserialize from this stream
721  ::Unserialize(*this, obj);
722  return (*this);
723  }
724 
726  void FindByte(uint8_t ch)
727  {
728  while (true) {
729  if (m_read_pos == nSrcPos)
730  Fill();
731  if (vchBuf[m_read_pos % vchBuf.size()] == std::byte{ch}) {
732  break;
733  }
734  m_read_pos++;
735  }
736  }
737 };
738 
739 #endif // BITCOIN_STREAMS_H
AutoFile::fclose
void fclose()
Definition: streams.h:491
CDataStream::ignore
void ignore(size_t num_ignore)
Definition: streams.h:298
AutoFile::file
FILE * file
Definition: streams.h:477
CAutoFile::operator<<
CAutoFile & operator<<(const T &obj)
Definition: streams.h:575
MakeByteSpan
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:264
CDataStream::end
iterator end()
Definition: streams.h:236
BitStreamWriter::m_ostream
OStream & m_ostream
Definition: streams.h:414
CDataStream::value_type
vector_type::value_type value_type
Definition: streams.h:201
CDataStream::in_avail
int in_avail() const
Definition: streams.h:273
CDataStream::size_type
vector_type::size_type size_type
Definition: streams.h:197
BitStreamReader
Definition: streams.h:368
assert
assert(!tx.IsCoinBase())
SpanReader::m_type
const int m_type
Definition: streams.h:137
CDataStream::vch
vector_type vch
Definition: streams.h:189
AutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:474
BitStreamWriter::Flush
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:456
CBufferedFile::operator>>
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:719
SpanReader::read
void read(Span< std::byte > dst)
Definition: streams.h:165
AutoFile::operator=
AutoFile & operator=(const AutoFile &)=delete
CVectorWriter
Definition: streams.h:73
CDataStream::begin
const_iterator begin() const
Definition: streams.h:233
CBufferedFile::SetPos
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:693
CDataStream::Xor
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:348
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:375
string.h
OverrideStream::operator>>
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:46
AutoFile::AutoFile
AutoFile(FILE *filenew)
Definition: streams.h:480
SpanReader::GetVersion
int GetVersion() const
Definition: streams.h:159
CAutoFile::nType
const int nType
Definition: streams.h:566
AutoFile::read
void read(Span< std::byte > dst)
Definition: streams.h:518
CDataStream::reference
vector_type::reference reference
Definition: streams.h:199
CVectorWriter::write
void write(Span< const std::byte > src)
Definition: streams.h:98
CDataStream::CDataStream
CDataStream(Span< const value_type > sp, int nTypeIn, int nVersionIn)
Definition: streams.h:211
SpanReader::m_data
Span< const unsigned char > m_data
Definition: streams.h:139
CBufferedFile::fclose
void fclose()
Definition: streams.h:653
CBufferedFile::m_read_pos
uint64_t m_read_pos
how many bytes have been read from this
Definition: streams.h:609
BitStreamReader::m_offset
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
Definition: streams.h:380
CBufferedFile::vchBuf
std::vector< std::byte > vchBuf
the buffer
Definition: streams.h:612
CDataStream::Compact
void Compact()
Definition: streams.h:247
CDataStream::begin
iterator begin()
Definition: streams.h:234
OverrideStream::GetType
int GetType() const
Definition: streams.h:64
OverrideStream::stream
Stream * stream
Definition: streams.h:29
BitStreamWriter
Definition: streams.h:411
AutoFile::write
void write(Span< const std::byte > src)
Definition: streams.h:538
Unserialize
void Unserialize(Stream &, char)=delete
CDataStream::write
void write(Span< const value_type > src)
Definition: streams.h:313
Serialize
void Serialize(Stream &, char)=delete
CAutoFile::nVersion
const int nVersion
Definition: streams.h:567
SpanReader::m_version
const int m_version
Definition: streams.h:138
OverrideStream::OverrideStream
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:35
CBufferedFile::src
FILE * src
source file
Definition: streams.h:607
CBufferedFile::nRewind
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:611
CDataStream::GetType
int GetType() const
Definition: streams.h:276
CVectorWriter::nVersion
const int nVersion
Definition: streams.h:127
CAutoFile::GetVersion
int GetVersion() const
Definition: streams.h:572
OverrideStream::read
void read(Span< std::byte > dst)
Definition: streams.h:58
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:186
CAutoFile
Definition: streams.h:563
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
CDataStream::const_reference
vector_type::const_reference const_reference
Definition: streams.h:200
CDataStream::nVersion
int nVersion
Definition: streams.h:193
BitStreamReader::Read
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:388
CBufferedFile::~CBufferedFile
~CBufferedFile()
Definition: streams.h:641
CVectorWriter::nPos
size_t nPos
Definition: streams.h:129
CBufferedFile::Fill
bool Fill()
read data from the source to fill the buffer
Definition: streams.h:616
CDataStream::allocator_type
vector_type::allocator_type allocator_type
Definition: streams.h:196
OverrideStream::ignore
void ignore(size_t size)
Definition: streams.h:66
CVectorWriter::GetType
int GetType() const
Definition: streams.h:121
CBufferedFile
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: streams.h:601
CBufferedFile::GetVersion
int GetVersion() const
Definition: streams.h:650
SerializeData
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.
Definition: zeroafterfree.h:44
AutoFile::release
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:503
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:436
CBufferedFile::read
void read(Span< std::byte > dst)
read a number of bytes
Definition: streams.h:667
UCharCast
unsigned char * UCharCast(char *c)
Definition: span.h:275
OverrideStream::nVersion
const int nVersion
Definition: streams.h:32
CBufferedFile::eof
bool eof() const
check whether we're at the end of the source file
Definition: streams.h:662
CDataStream::CDataStream
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:206
CDataStream::operator[]
const_reference operator[](size_type pos) const
Definition: streams.h:241
CBufferedFile::operator=
CBufferedFile & operator=(const CBufferedFile &)=delete
span.h
CheckedAdd
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition: overflow.h:24
AutoFile::Get
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:509
CBufferedFile::nSrcPos
uint64_t nSrcPos
how many bytes have been read from source
Definition: streams.h:608
CDataStream::end
const_iterator end() const
Definition: streams.h:235
CDataStream::nType
int nType
Definition: streams.h:192
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:423
CAutoFile::CAutoFile
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:570
CDataStream::Rewind
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:253
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:711
Span::begin
constexpr C * begin() const noexcept
Definition: span.h:174
AutoFile::ignore
void ignore(size_t nSize)
Definition: streams.h:526
SpanReader
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:134
CAutoFile::operator>>
CAutoFile & operator>>(T &&obj)
Definition: streams.h:585
CDataStream::m_read_pos
vector_type::size_type m_read_pos
Definition: streams.h:190
BitStreamWriter::BitStreamWriter
BitStreamWriter(OStream &ostream)
Definition: streams.h:426
SerializeMany
void SerializeMany(Stream &s)
Definition: serialize.h:1040
CDataStream::reverse_iterator
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:204
CDataStream::reserve
void reserve(size_type n)
Definition: streams.h:240
CAutoFile::GetType
int GetType() const
Definition: streams.h:571
CVectorWriter::CVectorWriter
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:94
CDataStream::size
size_type size() const
Definition: streams.h:237
CDataStream::SetVersion
void SetVersion(int n)
Definition: streams.h:277
AutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:513
CDataStream::resize
void resize(size_type n, value_type c=value_type{})
Definition: streams.h:239
CVectorWriter::CVectorWriter
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:84
AutoFile::operator>>
AutoFile & operator>>(T &&obj)
Definition: streams.h:555
SpanReader::GetType
int GetType() const
Definition: streams.h:160
Span::empty
constexpr bool empty() const noexcept
Definition: span.h:188
CDataStream::data
const value_type * data() const
Definition: streams.h:245
AsBytes
Span< const std::byte > AsBytes(Span< T > s) noexcept
Definition: span.h:253
AutoFile::~AutoFile
~AutoFile()
Definition: streams.h:482
SpanReader::size
size_t size() const
Definition: streams.h:162
OverrideStream
Definition: streams.h:27
CBufferedFile::nType
const int nType
Definition: streams.h:604
CDataStream::operator[]
reference operator[](size_type pos)
Definition: streams.h:242
CBufferedFile::nVersion
const int nVersion
Definition: streams.h:605
CVectorWriter::operator<<
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:111
BitStreamWriter::~BitStreamWriter
~BitStreamWriter()
Definition: streams.h:428
Span::data
constexpr C * data() const noexcept
Definition: span.h:173
CVectorWriter::nType
const int nType
Definition: streams.h:126
CDataStream::GetVersion
int GetVersion() const
Definition: streams.h:278
CDataStream::operator>>
CDataStream & operator>>(T &&obj)
Definition: streams.h:336
CDataStream::CDataStream
CDataStream(Span< const uint8_t > sp, int type, int version)
Definition: streams.h:210
CDataStream::CDataStream
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:217
SpanReader::operator>>
SpanReader & operator>>(T &&obj)
Definition: streams.h:152
CDataStream::clear
void clear()
Definition: streams.h:243
CVectorWriter::vchData
std::vector< unsigned char > & vchData
Definition: streams.h:128
CDataStream::difference_type
vector_type::difference_type difference_type
Definition: streams.h:198
CDataStream::operator<<
CDataStream & operator<<(const T &obj)
Definition: streams.h:328
std
Definition: setup_common.h:33
CDataStream::const_iterator
vector_type::const_iterator const_iterator
Definition: streams.h:203
CBufferedFile::GetType
int GetType() const
Definition: streams.h:651
ByteUnit::T
@ T
CDataStream::read
void read(Span< value_type > dst)
Definition: streams.h:280
CDataStream::vector_type
SerializeData vector_type
Definition: streams.h:188
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:185
OverrideStream::nType
const int nType
Definition: streams.h:31
zeroafterfree.h
BitStreamWriter::m_buffer
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
Definition: streams.h:418
CVectorWriter::GetVersion
int GetVersion() const
Definition: streams.h:117
AutoFile::operator<<
AutoFile & operator<<(const T &obj)
Definition: streams.h:547
SpanReader::SpanReader
SpanReader(int type, int version, Span< const unsigned char > data)
Definition: streams.h:148
CDataStream::empty
bool empty() const
Definition: streams.h:238
SpanReader::empty
bool empty() const
Definition: streams.h:163
CBufferedFile::nReadLimit
uint64_t nReadLimit
up to which position we're allowed to read
Definition: streams.h:610
CDataStream::Serialize
void Serialize(Stream &s) const
Definition: streams.h:320
BitStreamReader::BitStreamReader
BitStreamReader(IStream &istream)
Definition: streams.h:383
CDataStream::str
std::string str() const
Definition: streams.h:224
CBufferedFile::CBufferedFile
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:633
CDataStream::rdbuf
CDataStream * rdbuf()
Definition: streams.h:272
CDataStream::SetType
void SetType(int n)
Definition: streams.h:275
OverrideStream::GetVersion
int GetVersion() const
Definition: streams.h:63
OverrideStream::write
void write(Span< const std::byte > src)
Definition: streams.h:53
overflow.h
OverrideStream::size
size_t size() const
Definition: streams.h:65
CDataStream::eof
bool eof() const
Definition: streams.h:271
CBufferedFile::GetPos
uint64_t GetPos() const
return the current reading position
Definition: streams.h:688
CDataStream::iterator
vector_type::iterator iterator
Definition: streams.h:202
Span::end
constexpr C * end() const noexcept
Definition: span.h:175
BitStreamReader::m_istream
IStream & m_istream
Definition: streams.h:371
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
ret
int ret
Definition: bitcoin-cli.cpp:1242
CBufferedFile::FindByte
void FindByte(uint8_t ch)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:726
OverrideStream::operator<<
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:38