Bitcoin Core  22.99.0
P2P Digital Currency
streams.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_STREAMS_H
7 #define BITCOIN_STREAMS_H
8 
9 #include <serialize.h>
10 #include <span.h>
12 
13 #include <algorithm>
14 #include <assert.h>
15 #include <ios>
16 #include <limits>
17 #include <optional>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 template<typename Stream>
27 {
28  Stream* stream;
29 
30  const int nType;
31  const int nVersion;
32 
33 public:
34  OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
35 
36  template<typename T>
38  {
39  // Serialize to this stream
40  ::Serialize(*this, obj);
41  return (*this);
42  }
43 
44  template<typename T>
46  {
47  // Unserialize from this stream
48  ::Unserialize(*this, obj);
49  return (*this);
50  }
51 
53  {
54  stream->write(src);
55  }
56 
58  {
59  stream->read(dst);
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  }
98  {
99  assert(nPos <= vchData.size());
100  size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
101  if (nOverwrite) {
102  memcpy(vchData.data() + nPos, src.data(), nOverwrite);
103  }
104  if (nOverwrite < src.size()) {
105  vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.end()));
106  }
107  nPos += src.size();
108  }
109  template<typename T>
111  {
112  // Serialize to this stream
113  ::Serialize(*this, obj);
114  return (*this);
115  }
116  int GetVersion() const
117  {
118  return nVersion;
119  }
120  int GetType() const
121  {
122  return nType;
123  }
124 private:
125  const int nType;
126  const int nVersion;
127  std::vector<unsigned char>& vchData;
128  size_t nPos;
129 };
130 
134 {
135 private:
136  const int m_type;
137  const int m_version;
139 
140 public:
141 
147  SpanReader(int type, int version, Span<const unsigned char> data)
148  : m_type(type), m_version(version), m_data(data) {}
149 
150  template<typename T>
152  {
153  // Unserialize from this stream
154  ::Unserialize(*this, obj);
155  return (*this);
156  }
157 
158  int GetVersion() const { return m_version; }
159  int GetType() const { return m_type; }
160 
161  size_t size() const { return m_data.size(); }
162  bool empty() const { return m_data.empty(); }
163 
165  {
166  if (dst.size() == 0) {
167  return;
168  }
169 
170  // Read from the beginning of the buffer
171  if (dst.size() > m_data.size()) {
172  throw std::ios_base::failure("SpanReader::read(): end of data");
173  }
174  memcpy(dst.data(), m_data.data(), dst.size());
175  m_data = m_data.subspan(dst.size());
176  }
177 };
178 
185 {
186 protected:
189  unsigned int nReadPos{0};
190 
191  int nType;
192  int nVersion;
193 
194 public:
195  typedef vector_type::allocator_type allocator_type;
196  typedef vector_type::size_type size_type;
197  typedef vector_type::difference_type difference_type;
198  typedef vector_type::reference reference;
199  typedef vector_type::const_reference const_reference;
200  typedef vector_type::value_type value_type;
201  typedef vector_type::iterator iterator;
202  typedef vector_type::const_iterator const_iterator;
203  typedef vector_type::reverse_iterator reverse_iterator;
204 
205  explicit CDataStream(int nTypeIn, int nVersionIn)
206  : nType{nTypeIn},
207  nVersion{nVersionIn} {}
208 
209  explicit CDataStream(Span<const uint8_t> sp, int type, int version) : CDataStream{AsBytes(sp), type, version} {}
210  explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
211  : vch(sp.data(), sp.data() + sp.size()),
212  nType{nTypeIn},
213  nVersion{nVersionIn} {}
214 
215  template <typename... Args>
216  CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
217  : nType{nTypeIn},
218  nVersion{nVersionIn}
219  {
220  ::SerializeMany(*this, std::forward<Args>(args)...);
221  }
222 
223  std::string str() const
224  {
225  return std::string{UCharCast(data()), UCharCast(data() + size())};
226  }
227 
228 
229  //
230  // Vector subset
231  //
232  const_iterator begin() const { return vch.begin() + nReadPos; }
233  iterator begin() { return vch.begin() + nReadPos; }
234  const_iterator end() const { return vch.end(); }
235  iterator end() { return vch.end(); }
236  size_type size() const { return vch.size() - nReadPos; }
237  bool empty() const { return vch.size() == nReadPos; }
238  void resize(size_type n, value_type c = value_type{}) { vch.resize(n + nReadPos, c); }
239  void reserve(size_type n) { vch.reserve(n + nReadPos); }
240  const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
241  reference operator[](size_type pos) { return vch[pos + nReadPos]; }
242  void clear() { vch.clear(); nReadPos = 0; }
243  value_type* data() { return vch.data() + nReadPos; }
244  const value_type* data() const { return vch.data() + nReadPos; }
245 
246  inline void Compact()
247  {
248  vch.erase(vch.begin(), vch.begin() + nReadPos);
249  nReadPos = 0;
250  }
251 
252  bool Rewind(std::optional<size_type> n = std::nullopt)
253  {
254  // Total rewind if no size is passed
255  if (!n) {
256  nReadPos = 0;
257  return true;
258  }
259  // Rewind by n characters if the buffer hasn't been compacted yet
260  if (*n > nReadPos)
261  return false;
262  nReadPos -= *n;
263  return true;
264  }
265 
266 
267  //
268  // Stream subset
269  //
270  bool eof() const { return size() == 0; }
271  CDataStream* rdbuf() { return this; }
272  int in_avail() const { return size(); }
273 
274  void SetType(int n) { nType = n; }
275  int GetType() const { return nType; }
276  void SetVersion(int n) { nVersion = n; }
277  int GetVersion() const { return nVersion; }
278 
280  {
281  if (dst.size() == 0) return;
282 
283  // Read from the beginning of the buffer
284  unsigned int nReadPosNext = nReadPos + dst.size();
285  if (nReadPosNext > vch.size()) {
286  throw std::ios_base::failure("CDataStream::read(): end of data");
287  }
288  memcpy(dst.data(), &vch[nReadPos], dst.size());
289  if (nReadPosNext == vch.size())
290  {
291  nReadPos = 0;
292  vch.clear();
293  return;
294  }
295  nReadPos = nReadPosNext;
296  }
297 
298  void ignore(int nSize)
299  {
300  // Ignore from the beginning of the buffer
301  if (nSize < 0) {
302  throw std::ios_base::failure("CDataStream::ignore(): nSize negative");
303  }
304  unsigned int nReadPosNext = nReadPos + nSize;
305  if (nReadPosNext >= vch.size())
306  {
307  if (nReadPosNext > vch.size())
308  throw std::ios_base::failure("CDataStream::ignore(): end of data");
309  nReadPos = 0;
310  vch.clear();
311  return;
312  }
313  nReadPos = nReadPosNext;
314  }
315 
317  {
318  // Write to the end of the buffer
319  vch.insert(vch.end(), src.begin(), src.end());
320  }
321 
322  template<typename Stream>
323  void Serialize(Stream& s) const
324  {
325  // Special case: stream << stream concatenates like stream += stream
326  if (!vch.empty())
327  s.write(MakeByteSpan(vch));
328  }
329 
330  template<typename T>
331  CDataStream& operator<<(const T& obj)
332  {
333  // Serialize to this stream
334  ::Serialize(*this, obj);
335  return (*this);
336  }
337 
338  template<typename T>
340  {
341  // Unserialize from this stream
342  ::Unserialize(*this, obj);
343  return (*this);
344  }
345 
351  void Xor(const std::vector<unsigned char>& key)
352  {
353  if (key.size() == 0) {
354  return;
355  }
356 
357  for (size_type i = 0, j = 0; i != size(); i++) {
358  vch[i] ^= std::byte{key[j++]};
359 
360  // This potentially acts on very many bytes of data, so it's
361  // important that we calculate `j`, i.e. the `key` index in this
362  // way instead of doing a %, which would effectively be a division
363  // for each byte Xor'd -- much slower than need be.
364  if (j == key.size())
365  j = 0;
366  }
367  }
368 };
369 
370 template <typename IStream>
372 {
373 private:
374  IStream& m_istream;
375 
378  uint8_t m_buffer{0};
379 
383  int m_offset{8};
384 
385 public:
386  explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
387 
391  uint64_t Read(int nbits) {
392  if (nbits < 0 || nbits > 64) {
393  throw std::out_of_range("nbits must be between 0 and 64");
394  }
395 
396  uint64_t data = 0;
397  while (nbits > 0) {
398  if (m_offset == 8) {
399  m_istream >> m_buffer;
400  m_offset = 0;
401  }
402 
403  int bits = std::min(8 - m_offset, nbits);
404  data <<= bits;
405  data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
406  m_offset += bits;
407  nbits -= bits;
408  }
409  return data;
410  }
411 };
412 
413 template <typename OStream>
415 {
416 private:
417  OStream& m_ostream;
418 
421  uint8_t m_buffer{0};
422 
426  int m_offset{0};
427 
428 public:
429  explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
430 
432  {
433  Flush();
434  }
435 
439  void Write(uint64_t data, int nbits) {
440  if (nbits < 0 || nbits > 64) {
441  throw std::out_of_range("nbits must be between 0 and 64");
442  }
443 
444  while (nbits > 0) {
445  int bits = std::min(8 - m_offset, nbits);
446  m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
447  m_offset += bits;
448  nbits -= bits;
449 
450  if (m_offset == 8) {
451  Flush();
452  }
453  }
454  }
455 
459  void Flush() {
460  if (m_offset == 0) {
461  return;
462  }
463 
464  m_ostream << m_buffer;
465  m_buffer = 0;
466  m_offset = 0;
467  }
468 };
469 
470 
471 
479 {
480 private:
481  const int nType;
482  const int nVersion;
483 
484  FILE* file;
485 
486 public:
487  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
488  {
489  file = filenew;
490  }
491 
493  {
494  fclose();
495  }
496 
497  // Disallow copies
498  CAutoFile(const CAutoFile&) = delete;
499  CAutoFile& operator=(const CAutoFile&) = delete;
500 
501  void fclose()
502  {
503  if (file) {
504  ::fclose(file);
505  file = nullptr;
506  }
507  }
508 
513  FILE* release() { FILE* ret = file; file = nullptr; return ret; }
514 
519  FILE* Get() const { return file; }
520 
523  bool IsNull() const { return (file == nullptr); }
524 
525  //
526  // Stream subset
527  //
528  int GetType() const { return nType; }
529  int GetVersion() const { return nVersion; }
530 
532  {
533  if (!file)
534  throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
535  if (fread(dst.data(), 1, dst.size(), file) != dst.size()) {
536  throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
537  }
538  }
539 
540  void ignore(size_t nSize)
541  {
542  if (!file)
543  throw std::ios_base::failure("CAutoFile::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) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
549  nSize -= nNow;
550  }
551  }
552 
554  {
555  if (!file)
556  throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
557  if (fwrite(src.data(), 1, src.size(), file) != src.size()) {
558  throw std::ios_base::failure("CAutoFile::write: write failed");
559  }
560  }
561 
562  template<typename T>
563  CAutoFile& operator<<(const T& obj)
564  {
565  // Serialize to this stream
566  if (!file)
567  throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
568  ::Serialize(*this, obj);
569  return (*this);
570  }
571 
572  template<typename T>
574  {
575  // Unserialize from this stream
576  if (!file)
577  throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
578  ::Unserialize(*this, obj);
579  return (*this);
580  }
581 };
582 
590 {
591 private:
592  const int nType;
593  const int nVersion;
594 
595  FILE *src;
596  uint64_t nSrcPos;
597  uint64_t nReadPos;
598  uint64_t nReadLimit;
599  uint64_t nRewind;
600  std::vector<std::byte> vchBuf;
601 
602 protected:
604  bool Fill() {
605  unsigned int pos = nSrcPos % vchBuf.size();
606  unsigned int readNow = vchBuf.size() - pos;
607  unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
608  if (nAvail < readNow)
609  readNow = nAvail;
610  if (readNow == 0)
611  return false;
612  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
613  if (nBytes == 0) {
614  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
615  }
616  nSrcPos += nBytes;
617  return true;
618  }
619 
620 public:
621  CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
622  : nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, std::byte{0})
623  {
624  if (nRewindIn >= nBufSize)
625  throw std::ios_base::failure("Rewind limit must be less than buffer size");
626  src = fileIn;
627  }
628 
630  {
631  fclose();
632  }
633 
634  // Disallow copies
635  CBufferedFile(const CBufferedFile&) = delete;
636  CBufferedFile& operator=(const CBufferedFile&) = delete;
637 
638  int GetVersion() const { return nVersion; }
639  int GetType() const { return nType; }
640 
641  void fclose()
642  {
643  if (src) {
644  ::fclose(src);
645  src = nullptr;
646  }
647  }
648 
650  bool eof() const {
651  return nReadPos == nSrcPos && feof(src);
652  }
653 
656  {
657  if (dst.size() + nReadPos > nReadLimit) {
658  throw std::ios_base::failure("Read attempted past buffer limit");
659  }
660  while (dst.size() > 0) {
661  if (nReadPos == nSrcPos)
662  Fill();
663  unsigned int pos = nReadPos % vchBuf.size();
664  size_t nNow = dst.size();
665  if (nNow + pos > vchBuf.size())
666  nNow = vchBuf.size() - pos;
667  if (nNow + nReadPos > nSrcPos)
668  nNow = nSrcPos - nReadPos;
669  memcpy(dst.data(), &vchBuf[pos], nNow);
670  nReadPos += nNow;
671  dst = dst.subspan(nNow);
672  }
673  }
674 
676  uint64_t GetPos() const {
677  return nReadPos;
678  }
679 
681  bool SetPos(uint64_t nPos) {
682  size_t bufsize = vchBuf.size();
683  if (nPos + bufsize < nSrcPos) {
684  // rewinding too far, rewind as far as possible
685  nReadPos = nSrcPos - bufsize;
686  return false;
687  }
688  if (nPos > nSrcPos) {
689  // can't go this far forward, go as far as possible
690  nReadPos = nSrcPos;
691  return false;
692  }
693  nReadPos = nPos;
694  return true;
695  }
696 
699  bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
700  if (nPos < nReadPos)
701  return false;
702  nReadLimit = nPos;
703  return true;
704  }
705 
706  template<typename T>
708  // Unserialize from this stream
709  ::Unserialize(*this, obj);
710  return (*this);
711  }
712 
714  void FindByte(uint8_t ch)
715  {
716  while (true) {
717  if (nReadPos == nSrcPos)
718  Fill();
719  if (vchBuf[nReadPos % vchBuf.size()] == std::byte{ch}) {
720  break;
721  }
722  nReadPos++;
723  }
724  }
725 };
726 
727 #endif // BITCOIN_STREAMS_H
CAutoFile::operator<<
CAutoFile & operator<<(const T &obj)
Definition: streams.h:563
MakeByteSpan
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:264
CDataStream::end
iterator end()
Definition: streams.h:235
BitStreamWriter::m_ostream
OStream & m_ostream
Definition: streams.h:417
CDataStream::value_type
vector_type::value_type value_type
Definition: streams.h:200
CDataStream::in_avail
int in_avail() const
Definition: streams.h:272
CDataStream::size_type
vector_type::size_type size_type
Definition: streams.h:196
CAutoFile::file
FILE * file
Definition: streams.h:484
BitStreamReader
Definition: streams.h:371
assert
assert(!tx.IsCoinBase())
SpanReader::m_type
const int m_type
Definition: streams.h:136
CAutoFile::ignore
void ignore(size_t nSize)
Definition: streams.h:540
CDataStream::vch
vector_type vch
Definition: streams.h:188
BitStreamWriter::Flush
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:459
CBufferedFile::operator>>
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:707
SpanReader::read
void read(Span< std::byte > dst)
Definition: streams.h:164
CVectorWriter
Definition: streams.h:72
CDataStream::begin
const_iterator begin() const
Definition: streams.h:232
CBufferedFile::SetPos
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:681
CDataStream::Xor
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:351
CDataStream::data
value_type * data()
Definition: streams.h:243
BitStreamReader::m_buffer
uint8_t m_buffer
Buffered byte read in from the input stream.
Definition: streams.h:378
string.h
CAutoFile::Get
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:519
OverrideStream::operator>>
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:45
SpanReader::GetVersion
int GetVersion() const
Definition: streams.h:158
CAutoFile::nType
const int nType
Definition: streams.h:481
CDataStream::reference
vector_type::reference reference
Definition: streams.h:198
CVectorWriter::write
void write(Span< const std::byte > src)
Definition: streams.h:97
CDataStream::CDataStream
CDataStream(Span< const value_type > sp, int nTypeIn, int nVersionIn)
Definition: streams.h:210
SpanReader::m_data
Span< const unsigned char > m_data
Definition: streams.h:138
CBufferedFile::fclose
void fclose()
Definition: streams.h:641
BitStreamReader::m_offset
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
Definition: streams.h:383
CBufferedFile::vchBuf
std::vector< std::byte > vchBuf
the buffer
Definition: streams.h:600
CDataStream::Compact
void Compact()
Definition: streams.h:246
CDataStream::begin
iterator begin()
Definition: streams.h:233
OverrideStream::GetType
int GetType() const
Definition: streams.h:63
OverrideStream::stream
Stream * stream
Definition: streams.h:28
CAutoFile::~CAutoFile
~CAutoFile()
Definition: streams.h:492
BitStreamWriter
Definition: streams.h:414
Unserialize
void Unserialize(Stream &, char)=delete
CDataStream::write
void write(Span< const value_type > src)
Definition: streams.h:316
Serialize
void Serialize(Stream &, char)=delete
CAutoFile::nVersion
const int nVersion
Definition: streams.h:482
SpanReader::m_version
const int m_version
Definition: streams.h:137
OverrideStream::OverrideStream
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:34
CBufferedFile::src
FILE * src
source file
Definition: streams.h:595
CBufferedFile::nRewind
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:599
CDataStream::GetType
int GetType() const
Definition: streams.h:275
CVectorWriter::nVersion
const int nVersion
Definition: streams.h:126
CAutoFile::GetVersion
int GetVersion() const
Definition: streams.h:529
OverrideStream::read
void read(Span< std::byte > dst)
Definition: streams.h:57
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:186
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:478
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:199
CDataStream::nVersion
int nVersion
Definition: streams.h:192
BitStreamReader::Read
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:391
CBufferedFile::~CBufferedFile
~CBufferedFile()
Definition: streams.h:629
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:604
CDataStream::allocator_type
vector_type::allocator_type allocator_type
Definition: streams.h:195
OverrideStream::ignore
void ignore(size_t size)
Definition: streams.h:65
CVectorWriter::GetType
int GetType() const
Definition: streams.h:120
CBufferedFile
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: streams.h:589
CBufferedFile::GetVersion
int GetVersion() const
Definition: streams.h:638
SerializeData
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.
Definition: zeroafterfree.h:44
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:439
CBufferedFile::read
void read(Span< std::byte > dst)
read a number of bytes
Definition: streams.h:655
UCharCast
unsigned char * UCharCast(char *c)
Definition: span.h:275
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:650
CDataStream::CDataStream
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:205
CDataStream::operator[]
const_reference operator[](size_type pos) const
Definition: streams.h:240
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:596
CBufferedFile::nReadPos
uint64_t nReadPos
how many bytes have been read from this
Definition: streams.h:597
CDataStream::end
const_iterator end() const
Definition: streams.h:234
CDataStream::nType
int nType
Definition: streams.h:191
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:426
CAutoFile::CAutoFile
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:487
CDataStream::Rewind
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:252
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:699
Span::begin
constexpr C * begin() const noexcept
Definition: span.h:174
SpanReader
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:133
CAutoFile::operator>>
CAutoFile & operator>>(T &&obj)
Definition: streams.h:573
CAutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:523
BitStreamWriter::BitStreamWriter
BitStreamWriter(OStream &ostream)
Definition: streams.h:429
SerializeMany
void SerializeMany(Stream &s)
Definition: serialize.h:1017
CAutoFile::fclose
void fclose()
Definition: streams.h:501
CDataStream::reverse_iterator
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:203
CDataStream::reserve
void reserve(size_type n)
Definition: streams.h:239
CAutoFile::GetType
int GetType() const
Definition: streams.h:528
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:236
CDataStream::SetVersion
void SetVersion(int n)
Definition: streams.h:276
CDataStream::resize
void resize(size_type n, value_type c=value_type{})
Definition: streams.h:238
CVectorWriter::CVectorWriter
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:83
SpanReader::GetType
int GetType() const
Definition: streams.h:159
CDataStream::ignore
void ignore(int nSize)
Definition: streams.h:298
Span::empty
constexpr bool empty() const noexcept
Definition: span.h:188
CDataStream::data
const value_type * data() const
Definition: streams.h:244
AsBytes
Span< const std::byte > AsBytes(Span< T > s) noexcept
Definition: span.h:253
SpanReader::size
size_t size() const
Definition: streams.h:161
OverrideStream
Definition: streams.h:26
CBufferedFile::nType
const int nType
Definition: streams.h:592
CDataStream::operator[]
reference operator[](size_type pos)
Definition: streams.h:241
CBufferedFile::nVersion
const int nVersion
Definition: streams.h:593
CVectorWriter::operator<<
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:110
BitStreamWriter::~BitStreamWriter
~BitStreamWriter()
Definition: streams.h:431
Span::data
constexpr C * data() const noexcept
Definition: span.h:173
CVectorWriter::nType
const int nType
Definition: streams.h:125
CDataStream::GetVersion
int GetVersion() const
Definition: streams.h:277
CDataStream::operator>>
CDataStream & operator>>(T &&obj)
Definition: streams.h:339
CDataStream::CDataStream
CDataStream(Span< const uint8_t > sp, int type, int version)
Definition: streams.h:209
CDataStream::CDataStream
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:216
SpanReader::operator>>
SpanReader & operator>>(T &&obj)
Definition: streams.h:151
CAutoFile::operator=
CAutoFile & operator=(const CAutoFile &)=delete
CDataStream::clear
void clear()
Definition: streams.h:242
CVectorWriter::vchData
std::vector< unsigned char > & vchData
Definition: streams.h:127
CDataStream::difference_type
vector_type::difference_type difference_type
Definition: streams.h:197
CDataStream::operator<<
CDataStream & operator<<(const T &obj)
Definition: streams.h:331
std
Definition: setup_common.h:33
CDataStream::const_iterator
vector_type::const_iterator const_iterator
Definition: streams.h:202
CBufferedFile::GetType
int GetType() const
Definition: streams.h:639
ByteUnit::T
@ T
CDataStream::read
void read(Span< value_type > dst)
Definition: streams.h:279
CDataStream::vector_type
SerializeData vector_type
Definition: streams.h:187
serialize.h
CDataStream::nReadPos
unsigned int nReadPos
Definition: streams.h:189
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
OverrideStream::nType
const int nType
Definition: streams.h:30
CAutoFile::read
void read(Span< std::byte > dst)
Definition: streams.h:531
zeroafterfree.h
BitStreamWriter::m_buffer
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
Definition: streams.h:421
CVectorWriter::GetVersion
int GetVersion() const
Definition: streams.h:116
SpanReader::SpanReader
SpanReader(int type, int version, Span< const unsigned char > data)
Definition: streams.h:147
CDataStream::empty
bool empty() const
Definition: streams.h:237
CAutoFile::release
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:513
SpanReader::empty
bool empty() const
Definition: streams.h:162
CBufferedFile::nReadLimit
uint64_t nReadLimit
up to which position we're allowed to read
Definition: streams.h:598
CDataStream::Serialize
void Serialize(Stream &s) const
Definition: streams.h:323
BitStreamReader::BitStreamReader
BitStreamReader(IStream &istream)
Definition: streams.h:386
CDataStream::str
std::string str() const
Definition: streams.h:223
CBufferedFile::CBufferedFile
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:621
CDataStream::rdbuf
CDataStream * rdbuf()
Definition: streams.h:271
CDataStream::SetType
void SetType(int n)
Definition: streams.h:274
OverrideStream::GetVersion
int GetVersion() const
Definition: streams.h:62
OverrideStream::write
void write(Span< const std::byte > src)
Definition: streams.h:52
OverrideStream::size
size_t size() const
Definition: streams.h:64
CDataStream::eof
bool eof() const
Definition: streams.h:270
CAutoFile::write
void write(Span< const std::byte > src)
Definition: streams.h:553
CBufferedFile::GetPos
uint64_t GetPos() const
return the current reading position
Definition: streams.h:676
CDataStream::iterator
vector_type::iterator iterator
Definition: streams.h:201
Span::end
constexpr C * end() const noexcept
Definition: span.h:175
BitStreamReader::m_istream
IStream & m_istream
Definition: streams.h:374
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
CBufferedFile::FindByte
void FindByte(uint8_t ch)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:714
OverrideStream::operator<<
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:37