Bitcoin Core  24.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-2022 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 <cstdio>
17 #include <ios>
18 #include <limits>
19 #include <optional>
20 #include <stdint.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 public:
193  typedef vector_type::allocator_type allocator_type;
194  typedef vector_type::size_type size_type;
195  typedef vector_type::difference_type difference_type;
196  typedef vector_type::reference reference;
197  typedef vector_type::const_reference const_reference;
198  typedef vector_type::value_type value_type;
199  typedef vector_type::iterator iterator;
200  typedef vector_type::const_iterator const_iterator;
201  typedef vector_type::reverse_iterator reverse_iterator;
202 
203  explicit DataStream() {}
205  explicit DataStream(Span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
206 
207  std::string str() const
208  {
209  return std::string{UCharCast(data()), UCharCast(data() + size())};
210  }
211 
212 
213  //
214  // Vector subset
215  //
216  const_iterator begin() const { return vch.begin() + m_read_pos; }
217  iterator begin() { return vch.begin() + m_read_pos; }
218  const_iterator end() const { return vch.end(); }
219  iterator end() { return vch.end(); }
220  size_type size() const { return vch.size() - m_read_pos; }
221  bool empty() const { return vch.size() == m_read_pos; }
222  void resize(size_type n, value_type c = value_type{}) { vch.resize(n + m_read_pos, c); }
223  void reserve(size_type n) { vch.reserve(n + m_read_pos); }
224  const_reference operator[](size_type pos) const { return vch[pos + m_read_pos]; }
225  reference operator[](size_type pos) { return vch[pos + m_read_pos]; }
226  void clear() { vch.clear(); m_read_pos = 0; }
227  value_type* data() { return vch.data() + m_read_pos; }
228  const value_type* data() const { return vch.data() + m_read_pos; }
229 
230  inline void Compact()
231  {
232  vch.erase(vch.begin(), vch.begin() + m_read_pos);
233  m_read_pos = 0;
234  }
235 
236  bool Rewind(std::optional<size_type> n = std::nullopt)
237  {
238  // Total rewind if no size is passed
239  if (!n) {
240  m_read_pos = 0;
241  return true;
242  }
243  // Rewind by n characters if the buffer hasn't been compacted yet
244  if (*n > m_read_pos)
245  return false;
246  m_read_pos -= *n;
247  return true;
248  }
249 
250 
251  //
252  // Stream subset
253  //
254  bool eof() const { return size() == 0; }
255  int in_avail() const { return size(); }
256 
258  {
259  if (dst.size() == 0) return;
260 
261  // Read from the beginning of the buffer
262  auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
263  if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
264  throw std::ios_base::failure("DataStream::read(): end of data");
265  }
266  memcpy(dst.data(), &vch[m_read_pos], dst.size());
267  if (next_read_pos.value() == vch.size()) {
268  m_read_pos = 0;
269  vch.clear();
270  return;
271  }
272  m_read_pos = next_read_pos.value();
273  }
274 
275  void ignore(size_t num_ignore)
276  {
277  // Ignore from the beginning of the buffer
278  auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
279  if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
280  throw std::ios_base::failure("DataStream::ignore(): end of data");
281  }
282  if (next_read_pos.value() == vch.size()) {
283  m_read_pos = 0;
284  vch.clear();
285  return;
286  }
287  m_read_pos = next_read_pos.value();
288  }
289 
291  {
292  // Write to the end of the buffer
293  vch.insert(vch.end(), src.begin(), src.end());
294  }
295 
296  template<typename Stream>
297  void Serialize(Stream& s) const
298  {
299  // Special case: stream << stream concatenates like stream += stream
300  if (!vch.empty())
301  s.write(MakeByteSpan(vch));
302  }
303 
304  template<typename T>
305  DataStream& operator<<(const T& obj)
306  {
307  // Serialize to this stream
308  ::Serialize(*this, obj);
309  return (*this);
310  }
311 
312  template<typename T>
314  {
315  // Unserialize from this stream
316  ::Unserialize(*this, obj);
317  return (*this);
318  }
319 
325  void Xor(const std::vector<unsigned char>& key)
326  {
327  if (key.size() == 0) {
328  return;
329  }
330 
331  for (size_type i = 0, j = 0; i != size(); i++) {
332  vch[i] ^= std::byte{key[j++]};
333 
334  // This potentially acts on very many bytes of data, so it's
335  // important that we calculate `j`, i.e. the `key` index in this
336  // way instead of doing a %, which would effectively be a division
337  // for each byte Xor'd -- much slower than need be.
338  if (j == key.size())
339  j = 0;
340  }
341  }
342 };
343 
344 class CDataStream : public DataStream
345 {
346 private:
347  int nType;
348  int nVersion;
349 
350 public:
351  explicit CDataStream(int nTypeIn, int nVersionIn)
352  : nType{nTypeIn},
353  nVersion{nVersionIn} {}
354 
355  explicit CDataStream(Span<const uint8_t> sp, int type, int version) : CDataStream{AsBytes(sp), type, version} {}
356  explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
357  : DataStream{sp},
358  nType{nTypeIn},
359  nVersion{nVersionIn} {}
360 
361  int GetType() const { return nType; }
362  void SetVersion(int n) { nVersion = n; }
363  int GetVersion() const { return nVersion; }
364 
365  template <typename T>
366  CDataStream& operator<<(const T& obj)
367  {
368  ::Serialize(*this, obj);
369  return *this;
370  }
371 
372  template <typename T>
374  {
375  ::Unserialize(*this, obj);
376  return *this;
377  }
378 };
379 
380 template <typename IStream>
382 {
383 private:
384  IStream& m_istream;
385 
388  uint8_t m_buffer{0};
389 
393  int m_offset{8};
394 
395 public:
396  explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
397 
401  uint64_t Read(int nbits) {
402  if (nbits < 0 || nbits > 64) {
403  throw std::out_of_range("nbits must be between 0 and 64");
404  }
405 
406  uint64_t data = 0;
407  while (nbits > 0) {
408  if (m_offset == 8) {
409  m_istream >> m_buffer;
410  m_offset = 0;
411  }
412 
413  int bits = std::min(8 - m_offset, nbits);
414  data <<= bits;
415  data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
416  m_offset += bits;
417  nbits -= bits;
418  }
419  return data;
420  }
421 };
422 
423 template <typename OStream>
425 {
426 private:
427  OStream& m_ostream;
428 
431  uint8_t m_buffer{0};
432 
436  int m_offset{0};
437 
438 public:
439  explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
440 
442  {
443  Flush();
444  }
445 
449  void Write(uint64_t data, int nbits) {
450  if (nbits < 0 || nbits > 64) {
451  throw std::out_of_range("nbits must be between 0 and 64");
452  }
453 
454  while (nbits > 0) {
455  int bits = std::min(8 - m_offset, nbits);
456  m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
457  m_offset += bits;
458  nbits -= bits;
459 
460  if (m_offset == 8) {
461  Flush();
462  }
463  }
464  }
465 
469  void Flush() {
470  if (m_offset == 0) {
471  return;
472  }
473 
474  m_ostream << m_buffer;
475  m_buffer = 0;
476  m_offset = 0;
477  }
478 };
479 
480 
487 class AutoFile
488 {
489 protected:
490  FILE* file;
491 
492 public:
493  explicit AutoFile(FILE* filenew) : file{filenew} {}
494 
496  {
497  fclose();
498  }
499 
500  // Disallow copies
501  AutoFile(const AutoFile&) = delete;
502  AutoFile& operator=(const AutoFile&) = delete;
503 
504  int fclose()
505  {
506  int retval{0};
507  if (file) {
508  retval = ::fclose(file);
509  file = nullptr;
510  }
511  return retval;
512  }
513 
518  FILE* release() { FILE* ret = file; file = nullptr; return ret; }
519 
524  FILE* Get() const { return file; }
525 
528  bool IsNull() const { return (file == nullptr); }
529 
530  //
531  // Stream subset
532  //
534  {
535  if (!file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
536  if (fread(dst.data(), 1, dst.size(), file) != dst.size()) {
537  throw std::ios_base::failure(feof(file) ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
538  }
539  }
540 
541  void ignore(size_t nSize)
542  {
543  if (!file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
544  unsigned char data[4096];
545  while (nSize > 0) {
546  size_t nNow = std::min<size_t>(nSize, sizeof(data));
547  if (fread(data, 1, nNow, file) != nNow)
548  throw std::ios_base::failure(feof(file) ? "AutoFile::ignore: end of file" : "AutoFile::read: fread failed");
549  nSize -= nNow;
550  }
551  }
552 
554  {
555  if (!file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
556  if (fwrite(src.data(), 1, src.size(), file) != src.size()) {
557  throw std::ios_base::failure("AutoFile::write: write failed");
558  }
559  }
560 
561  template <typename T>
562  AutoFile& operator<<(const T& obj)
563  {
564  if (!file) throw std::ios_base::failure("AutoFile::operator<<: file handle is nullptr");
565  ::Serialize(*this, obj);
566  return *this;
567  }
568 
569  template <typename T>
571  {
572  if (!file) throw std::ios_base::failure("AutoFile::operator>>: file handle is nullptr");
573  ::Unserialize(*this, obj);
574  return *this;
575  }
576 };
577 
578 class CAutoFile : public AutoFile
579 {
580 private:
581  const int nType;
582  const int nVersion;
583 
584 public:
585  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : AutoFile{filenew}, nType(nTypeIn), nVersion(nVersionIn) {}
586  int GetType() const { return nType; }
587  int GetVersion() const { return nVersion; }
588 
589  template<typename T>
590  CAutoFile& operator<<(const T& obj)
591  {
592  // Serialize to this stream
593  if (!file)
594  throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
595  ::Serialize(*this, obj);
596  return (*this);
597  }
598 
599  template<typename T>
601  {
602  // Unserialize from this stream
603  if (!file)
604  throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
605  ::Unserialize(*this, obj);
606  return (*this);
607  }
608 };
609 
617 {
618 private:
619  const int nType;
620  const int nVersion;
621 
622  FILE *src;
623  uint64_t nSrcPos{0};
624  uint64_t m_read_pos{0};
625  uint64_t nReadLimit;
626  uint64_t nRewind;
627  std::vector<std::byte> vchBuf;
628 
630  bool Fill() {
631  unsigned int pos = nSrcPos % vchBuf.size();
632  unsigned int readNow = vchBuf.size() - pos;
633  unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind;
634  if (nAvail < readNow)
635  readNow = nAvail;
636  if (readNow == 0)
637  return false;
638  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
639  if (nBytes == 0) {
640  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
641  }
642  nSrcPos += nBytes;
643  return true;
644  }
645 
651  std::pair<std::byte*, size_t> AdvanceStream(size_t length)
652  {
654  if (m_read_pos + length > nReadLimit) {
655  throw std::ios_base::failure("Attempt to position past buffer limit");
656  }
657  // If there are no bytes available, read from the file.
658  if (m_read_pos == nSrcPos && length > 0) Fill();
659 
660  size_t buffer_offset{static_cast<size_t>(m_read_pos % vchBuf.size())};
661  size_t buffer_available{static_cast<size_t>(vchBuf.size() - buffer_offset)};
662  size_t bytes_until_source_pos{static_cast<size_t>(nSrcPos - m_read_pos)};
663  size_t advance{std::min({length, buffer_available, bytes_until_source_pos})};
664  m_read_pos += advance;
665  return std::make_pair(&vchBuf[buffer_offset], advance);
666  }
667 
668 public:
669  CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
670  : nType(nTypeIn), nVersion(nVersionIn), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, std::byte{0})
671  {
672  if (nRewindIn >= nBufSize)
673  throw std::ios_base::failure("Rewind limit must be less than buffer size");
674  src = fileIn;
675  }
676 
678  {
679  fclose();
680  }
681 
682  // Disallow copies
683  CBufferedFile(const CBufferedFile&) = delete;
685 
686  int GetVersion() const { return nVersion; }
687  int GetType() const { return nType; }
688 
689  void fclose()
690  {
691  if (src) {
692  ::fclose(src);
693  src = nullptr;
694  }
695  }
696 
698  bool eof() const {
699  return m_read_pos == nSrcPos && feof(src);
700  }
701 
704  {
705  while (dst.size() > 0) {
706  auto [buffer_pointer, length]{AdvanceStream(dst.size())};
707  memcpy(dst.data(), buffer_pointer, length);
708  dst = dst.subspan(length);
709  }
710  }
711 
714  void SkipTo(const uint64_t file_pos)
715  {
716  assert(file_pos >= m_read_pos);
717  while (m_read_pos < file_pos) AdvanceStream(file_pos - m_read_pos);
718  }
719 
721  uint64_t GetPos() const {
722  return m_read_pos;
723  }
724 
726  bool SetPos(uint64_t nPos) {
727  size_t bufsize = vchBuf.size();
728  if (nPos + bufsize < nSrcPos) {
729  // rewinding too far, rewind as far as possible
730  m_read_pos = nSrcPos - bufsize;
731  return false;
732  }
733  if (nPos > nSrcPos) {
734  // can't go this far forward, go as far as possible
736  return false;
737  }
738  m_read_pos = nPos;
739  return true;
740  }
741 
744  bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
745  if (nPos < m_read_pos)
746  return false;
747  nReadLimit = nPos;
748  return true;
749  }
750 
751  template<typename T>
753  // Unserialize from this stream
754  ::Unserialize(*this, obj);
755  return (*this);
756  }
757 
759  void FindByte(uint8_t ch)
760  {
761  while (true) {
762  if (m_read_pos == nSrcPos)
763  Fill();
764  if (vchBuf[m_read_pos % vchBuf.size()] == std::byte{ch}) {
765  break;
766  }
767  m_read_pos++;
768  }
769  }
770 };
771 
772 #endif // BITCOIN_STREAMS_H
int ret
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:488
FILE * file
Definition: streams.h:490
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:518
void ignore(size_t nSize)
Definition: streams.h:541
~AutoFile()
Definition: streams.h:495
void read(Span< std::byte > dst)
Definition: streams.h:533
AutoFile(FILE *filenew)
Definition: streams.h:493
AutoFile(const AutoFile &)=delete
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:528
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:524
AutoFile & operator=(const AutoFile &)=delete
AutoFile & operator<<(const T &obj)
Definition: streams.h:562
AutoFile & operator>>(T &&obj)
Definition: streams.h:570
void write(Span< const std::byte > src)
Definition: streams.h:553
int fclose()
Definition: streams.h:504
uint8_t m_buffer
Buffered byte read in from the input stream.
Definition: streams.h:388
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:401
BitStreamReader(IStream &istream)
Definition: streams.h:396
IStream & m_istream
Definition: streams.h:384
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
Definition: streams.h:393
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:449
OStream & m_ostream
Definition: streams.h:427
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
Definition: streams.h:431
BitStreamWriter(OStream &ostream)
Definition: streams.h:439
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:436
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:469
const int nType
Definition: streams.h:581
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:585
int GetType() const
Definition: streams.h:586
CAutoFile & operator<<(const T &obj)
Definition: streams.h:590
const int nVersion
Definition: streams.h:582
int GetVersion() const
Definition: streams.h:587
CAutoFile & operator>>(T &&obj)
Definition: streams.h:600
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: streams.h:617
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:752
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:744
uint64_t nReadLimit
up to which position we're allowed to read
Definition: streams.h:625
void FindByte(uint8_t ch)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:759
int GetVersion() const
Definition: streams.h:686
bool Fill()
read data from the source to fill the buffer
Definition: streams.h:630
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:669
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:626
void read(Span< std::byte > dst)
read a number of bytes
Definition: streams.h:703
std::vector< std::byte > vchBuf
the buffer
Definition: streams.h:627
uint64_t GetPos() const
return the current reading position
Definition: streams.h:721
FILE * src
source file
Definition: streams.h:622
~CBufferedFile()
Definition: streams.h:677
int GetType() const
Definition: streams.h:687
uint64_t nSrcPos
how many bytes have been read from source
Definition: streams.h:623
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Definition: streams.h:714
std::pair< std::byte *, size_t > AdvanceStream(size_t length)
Advance the stream's read pointer (m_read_pos) by up to 'length' bytes, filling the buffer from the f...
Definition: streams.h:651
uint64_t m_read_pos
how many bytes have been read from this
Definition: streams.h:624
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:726
const int nType
Definition: streams.h:619
CBufferedFile(const CBufferedFile &)=delete
CBufferedFile & operator=(const CBufferedFile &)=delete
const int nVersion
Definition: streams.h:620
void fclose()
Definition: streams.h:689
bool eof() const
check whether we're at the end of the source file
Definition: streams.h:698
CDataStream & operator>>(T &&obj)
Definition: streams.h:373
int nVersion
Definition: streams.h:348
void SetVersion(int n)
Definition: streams.h:362
int nType
Definition: streams.h:347
int GetType() const
Definition: streams.h:361
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:351
CDataStream(Span< const value_type > sp, int nTypeIn, int nVersionIn)
Definition: streams.h:356
int GetVersion() const
Definition: streams.h:363
CDataStream(Span< const uint8_t > sp, int type, int version)
Definition: streams.h:355
CDataStream & operator<<(const T &obj)
Definition: streams.h:366
void write(Span< const std::byte > src)
Definition: streams.h:98
const int nVersion
Definition: streams.h:127
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:111
const int nType
Definition: streams.h:126
int GetType() const
Definition: streams.h:121
size_t nPos
Definition: streams.h:129
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:94
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:84
int GetVersion() const
Definition: streams.h:117
std::vector< unsigned char > & vchData
Definition: streams.h:128
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
void write(Span< const value_type > src)
Definition: streams.h:290
bool empty() const
Definition: streams.h:221
vector_type::difference_type difference_type
Definition: streams.h:195
size_type size() const
Definition: streams.h:220
reference operator[](size_type pos)
Definition: streams.h:225
void Serialize(Stream &s) const
Definition: streams.h:297
void resize(size_type n, value_type c=value_type{})
Definition: streams.h:222
const_reference operator[](size_type pos) const
Definition: streams.h:224
value_type * data()
Definition: streams.h:227
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:325
DataStream(Span< const value_type > sp)
Definition: streams.h:205
vector_type::size_type size_type
Definition: streams.h:194
SerializeData vector_type
Definition: streams.h:188
vector_type vch
Definition: streams.h:189
vector_type::const_reference const_reference
Definition: streams.h:197
const value_type * data() const
Definition: streams.h:228
vector_type::const_iterator const_iterator
Definition: streams.h:200
DataStream(Span< const uint8_t > sp)
Definition: streams.h:204
void reserve(size_type n)
Definition: streams.h:223
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:201
iterator begin()
Definition: streams.h:217
DataStream & operator>>(T &&obj)
Definition: streams.h:313
const_iterator begin() const
Definition: streams.h:216
void read(Span< value_type > dst)
Definition: streams.h:257
DataStream()
Definition: streams.h:203
vector_type::size_type m_read_pos
Definition: streams.h:190
vector_type::iterator iterator
Definition: streams.h:199
vector_type::value_type value_type
Definition: streams.h:198
bool eof() const
Definition: streams.h:254
std::string str() const
Definition: streams.h:207
void ignore(size_t num_ignore)
Definition: streams.h:275
vector_type::allocator_type allocator_type
Definition: streams.h:193
const_iterator end() const
Definition: streams.h:218
void Compact()
Definition: streams.h:230
void clear()
Definition: streams.h:226
iterator end()
Definition: streams.h:219
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:236
vector_type::reference reference
Definition: streams.h:196
int in_avail() const
Definition: streams.h:255
DataStream & operator<<(const T &obj)
Definition: streams.h:305
const int nVersion
Definition: streams.h:32
void ignore(size_t size)
Definition: streams.h:66
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:35
void read(Span< std::byte > dst)
Definition: streams.h:58
void write(Span< const std::byte > src)
Definition: streams.h:53
Stream * stream
Definition: streams.h:29
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:38
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:46
size_t size() const
Definition: streams.h:65
int GetVersion() const
Definition: streams.h:63
const int nType
Definition: streams.h:31
int GetType() const
Definition: streams.h:64
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:97
constexpr std::size_t size() const noexcept
Definition: span.h:186
constexpr C * data() const noexcept
Definition: span.h:173
constexpr C * end() const noexcept
Definition: span.h:175
constexpr C * begin() const noexcept
Definition: span.h:174
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:194
constexpr bool empty() const noexcept
Definition: span.h:188
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:135
bool empty() const
Definition: streams.h:163
const int m_type
Definition: streams.h:137
size_t size() const
Definition: streams.h:162
SpanReader(int type, int version, Span< const unsigned char > data)
Definition: streams.h:148
Span< const unsigned char > m_data
Definition: streams.h:139
const int m_version
Definition: streams.h:138
int GetType() const
Definition: streams.h:160
SpanReader & operator>>(T &&obj)
Definition: streams.h:152
void read(Span< std::byte > dst)
Definition: streams.h:165
int GetVersion() const
Definition: streams.h:159
ArgsManager args
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition: overflow.h:24
void SerializeMany(Stream &s)
Definition: serialize.h:1040
void Unserialize(Stream &, char)=delete
void Serialize(Stream &, char)=delete
unsigned char * UCharCast(char *c)
Definition: span.h:275
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:264
Span< const std::byte > AsBytes(Span< T > s) noexcept
Definition: span.h:253
assert(!tx.IsCoinBase())
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.
Definition: zeroafterfree.h:44