Bitcoin ABC 0.32.5
P2P Digital Currency
streams.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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 <cassert>
16#include <cstdint>
17#include <cstdio>
18#include <cstring>
19#include <ios>
20#include <limits>
21#include <optional>
22#include <string>
23#include <utility>
24#include <vector>
25
26template <typename Stream> class OverrideStream {
27 Stream *stream;
28
29 const int nType;
30 const int nVersion;
31
32public:
33 OverrideStream(Stream *stream_, int nType_, int nVersion_)
34 : stream(stream_), nType(nType_), nVersion(nVersion_) {}
35
36 template <typename T> OverrideStream<Stream> &operator<<(const T &obj) {
37 ::Serialize(*this, obj);
38 return (*this);
39 }
40
41 template <typename T> OverrideStream<Stream> &operator>>(T &&obj) {
42 ::Unserialize(*this, obj);
43 return (*this);
44 }
45
46 void write(Span<const std::byte> src) { stream->write(src); }
47
48 void read(Span<std::byte> dst) { stream->read(dst); }
49
50 int GetVersion() const { return nVersion; }
51 int GetType() const { return nType; }
52 void ignore(size_t size) { return stream->ignore(size); }
53};
54
55template <typename S> OverrideStream<S> WithOrVersion(S *s, int nVersionFlag) {
56 return OverrideStream<S>(s, s->GetType(), s->GetVersion() | nVersionFlag);
57}
58
65public:
74 CVectorWriter(int nTypeIn, int nVersionIn, std::vector<uint8_t> &vchDataIn,
75 size_t nPosIn)
76 : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn),
77 nPos(nPosIn) {
78 if (nPos > vchData.size()) {
79 vchData.resize(nPos);
80 }
81 }
86 template <typename... Args>
87 CVectorWriter(int nTypeIn, int nVersionIn, std::vector<uint8_t> &vchDataIn,
88 size_t nPosIn, Args &&...args)
89 : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn) {
90 ::SerializeMany(*this, std::forward<Args>(args)...);
91 }
93 assert(nPos <= vchData.size());
94 size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
95 if (nOverwrite) {
96 memcpy(vchData.data() + nPos, src.data(), nOverwrite);
97 }
98 if (nOverwrite < src.size()) {
99 vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite,
100 UCharCast(src.end()));
101 }
102 nPos += src.size();
103 }
104 template <typename T> CVectorWriter &operator<<(const T &obj) {
105 ::Serialize(*this, obj);
106 return (*this);
107 }
108 int GetVersion() const { return nVersion; }
109 int GetType() const { return nType; }
110 void seek(size_t nSize) {
111 nPos += nSize;
112 if (nPos > vchData.size()) {
113 vchData.resize(nPos);
114 }
115 }
116
117private:
118 const int nType;
119 const int nVersion;
120 std::vector<uint8_t> &vchData;
121 size_t nPos;
122};
123
128private:
129 const int m_type;
130 const int m_version;
132
133public:
139 SpanReader(int type, int version, Span<const uint8_t> data)
140 : m_type(type), m_version(version), m_data(data) {}
141
142 template <typename T> SpanReader &operator>>(T &obj) {
143 ::Unserialize(*this, obj);
144 return (*this);
145 }
146
147 int GetVersion() const { return m_version; }
148 int GetType() const { return m_type; }
149
150 size_t size() const { return m_data.size(); }
151 bool empty() const { return m_data.empty(); }
152
154 if (dst.size() == 0) {
155 return;
156 }
157
158 // Read from the beginning of the buffer
159 if (dst.size() > m_data.size()) {
160 throw std::ios_base::failure("SpanReader::read(): end of data");
161 }
162 memcpy(dst.data(), m_data.data(), dst.size());
163 m_data = m_data.subspan(dst.size());
164 }
165};
166
175protected:
178 vector_type::size_type m_read_pos{0};
179
180public:
181 typedef vector_type::allocator_type allocator_type;
182 typedef vector_type::size_type size_type;
183 typedef vector_type::difference_type difference_type;
184 typedef vector_type::reference reference;
185 typedef vector_type::const_reference const_reference;
186 typedef vector_type::value_type value_type;
187 typedef vector_type::iterator iterator;
188 typedef vector_type::const_iterator const_iterator;
189 typedef vector_type::reverse_iterator reverse_iterator;
190
191 explicit DataStream() {}
194 : vch(sp.data(), sp.data() + sp.size()) {}
195
196 std::string str() const {
197 return std::string{UCharCast(data()), UCharCast(data() + size())};
198 }
199
200 //
201 // Vector subset
202 //
203 const_iterator begin() const { return vch.begin() + m_read_pos; }
204 iterator begin() { return vch.begin() + m_read_pos; }
205 const_iterator end() const { return vch.end(); }
206 iterator end() { return vch.end(); }
207 size_type size() const { return vch.size() - m_read_pos; }
208 bool empty() const { return vch.size() == m_read_pos; }
210 vch.resize(n + m_read_pos, c);
211 }
212 void reserve(size_type n) { vch.reserve(n + m_read_pos); }
214 return vch[pos + m_read_pos];
215 }
217 void clear() {
218 vch.clear();
219 m_read_pos = 0;
220 }
221 value_type *data() { return vch.data() + m_read_pos; }
222 const value_type *data() const { return vch.data() + m_read_pos; }
223
224 void insert(iterator it, std::vector<value_type>::const_iterator first,
225 std::vector<value_type>::const_iterator last) {
226 if (last == first) {
227 return;
228 }
229
230 assert(last - first > 0);
231 if (it == vch.begin() + m_read_pos &&
232 (unsigned int)(last - first) <= m_read_pos) {
233 // special case for inserting at the front when there's room
234 m_read_pos -= (last - first);
235 memcpy(&vch[m_read_pos], &first[0], last - first);
236 } else {
237 vch.insert(it, first, last);
238 }
239 }
240
241 // This was added to have full compat with the std::vector interface but is
242 // unused (except in a Bitcoin ABC specific test in stream_tests)
243 void insert(iterator it, const value_type *first, const value_type *last) {
244 if (last == first) {
245 return;
246 }
247
248 assert(last - first > 0);
249 if (it == vch.begin() + m_read_pos &&
250 (unsigned int)(last - first) <= m_read_pos) {
251 // special case for inserting at the front when there's room
252 m_read_pos -= (last - first);
253 memcpy(&vch[m_read_pos], &first[0], last - first);
254 } else {
255 vch.insert(it, first, last);
256 }
257 }
258
260 if (it == vch.begin() + m_read_pos) {
261 // special case for erasing from the front
262 if (++m_read_pos >= vch.size()) {
263 // whenever we reach the end, we take the opportunity to clear
264 // the buffer
265 m_read_pos = 0;
266 return vch.erase(vch.begin(), vch.end());
267 }
268 return vch.begin() + m_read_pos;
269 } else {
270 return vch.erase(it);
271 }
272 }
273
275 if (first == vch.begin() + m_read_pos) {
276 // special case for erasing from the front
277 if (last == vch.end()) {
278 m_read_pos = 0;
279 return vch.erase(vch.begin(), vch.end());
280 } else {
281 m_read_pos = (last - vch.begin());
282 return last;
283 }
284 } else
285 return vch.erase(first, last);
286 }
287
288 inline void Compact() {
289 vch.erase(vch.begin(), vch.begin() + m_read_pos);
290 m_read_pos = 0;
291 }
292
293 bool Rewind(std::optional<size_type> n = std::nullopt) {
294 // Total rewind if no size is passed
295 if (!n) {
296 m_read_pos = 0;
297 return true;
298 }
299 // Rewind by n characters if the buffer hasn't been compacted yet
300 if (*n > m_read_pos) {
301 return false;
302 }
303 m_read_pos -= *n;
304 return true;
305 }
306
307 //
308 // Stream subset
309 //
310 bool eof() const { return size() == 0; }
311 int in_avail() const { return size(); }
312
314 if (dst.size() == 0) {
315 return;
316 }
317
318 // Read from the beginning of the buffer
319 auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
320 if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
321 throw std::ios_base::failure("DataStream::read(): end of data");
322 }
323 memcpy(dst.data(), &vch[m_read_pos], dst.size());
324 if (next_read_pos.value() == vch.size()) {
325 m_read_pos = 0;
326 vch.clear();
327 return;
328 }
329 m_read_pos = next_read_pos.value();
330 }
331
332 void ignore(size_t num_ignore) {
333 // Ignore from the beginning of the buffer
334 auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
335 if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
336 throw std::ios_base::failure("DataStream::ignore(): end of data");
337 }
338 if (next_read_pos.value() == vch.size()) {
339 m_read_pos = 0;
340 vch.clear();
341 return;
342 }
343 m_read_pos = next_read_pos.value();
344 }
345
347 // Write to the end of the buffer
348 vch.insert(vch.end(), src.begin(), src.end());
349 }
350
351 template <typename T> DataStream &operator<<(const T &obj) {
352 ::Serialize(*this, obj);
353 return (*this);
354 }
355
356 template <typename T> DataStream &operator>>(T &&obj) {
357 ::Unserialize(*this, obj);
358 return (*this);
359 }
360
366 void Xor(const std::vector<uint8_t> &key) {
367 if (key.size() == 0) {
368 return;
369 }
370
371 for (size_type i = 0, j = 0; i != size(); i++) {
372 vch[i] ^= std::byte{key[j++]};
373
374 // This potentially acts on very many bytes of data, so it's
375 // important that we calculate `j`, i.e. the `key` index in this way
376 // instead of doing a %, which would effectively be a division for
377 // each byte Xor'd -- much slower than need be.
378 if (j == key.size()) j = 0;
379 }
380 }
381};
382
383class CDataStream : public DataStream {
384private:
385 int nType;
387
388public:
389 explicit CDataStream(int nTypeIn, int nVersionIn)
390 : nType{nTypeIn}, nVersion{nVersionIn} {}
391
392 explicit CDataStream(Span<const uint8_t> sp, int type, int version)
393 : CDataStream{AsBytes(sp), type, version} {}
394 explicit CDataStream(Span<const value_type> sp, int nTypeIn, int nVersionIn)
395 : DataStream{sp}, nType{nTypeIn}, nVersion{nVersionIn} {}
396
397 int GetType() const { return nType; }
398 void SetVersion(int n) { nVersion = n; }
399 int GetVersion() const { return nVersion; }
400
401 template <typename T> CDataStream &operator<<(const T &obj) {
402 ::Serialize(*this, obj);
403 return *this;
404 }
405
406 template <typename T> CDataStream &operator>>(T &&obj) {
407 ::Unserialize(*this, obj);
408 return *this;
409 }
410};
411
412template <typename IStream> class BitStreamReader {
413private:
414 IStream &m_istream;
415
418 uint8_t m_buffer{0};
419
423 int m_offset{8};
424
425public:
426 explicit BitStreamReader(IStream &istream) : m_istream(istream) {}
427
432 uint64_t Read(int nbits) {
433 if (nbits < 0 || nbits > 64) {
434 throw std::out_of_range("nbits must be between 0 and 64");
435 }
436
437 uint64_t data = 0;
438 while (nbits > 0) {
439 if (m_offset == 8) {
441 m_offset = 0;
442 }
443
444 int bits = std::min(8 - m_offset, nbits);
445 data <<= bits;
446 data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
447 m_offset += bits;
448 nbits -= bits;
449 }
450 return data;
451 }
452};
453
454template <typename OStream> class BitStreamWriter {
455private:
456 OStream &m_ostream;
457
460 uint8_t m_buffer{0};
461
465 int m_offset{0};
466
467public:
468 explicit BitStreamWriter(OStream &ostream) : m_ostream(ostream) {}
469
471
476 void Write(uint64_t data, int nbits) {
477 if (nbits < 0 || nbits > 64) {
478 throw std::out_of_range("nbits must be between 0 and 64");
479 }
480
481 while (nbits > 0) {
482 int bits = std::min(8 - m_offset, nbits);
483 m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
484 m_offset += bits;
485 nbits -= bits;
486
487 if (m_offset == 8) {
488 Flush();
489 }
490 }
491 }
492
497 void Flush() {
498 if (m_offset == 0) {
499 return;
500 }
501
503 m_buffer = 0;
504 m_offset = 0;
505 }
506};
507
515class AutoFile {
516protected:
517 std::FILE *m_file;
518
519public:
520 explicit AutoFile(std::FILE *file) : m_file{file} {}
521
523
524 // Disallow copies
525 AutoFile(const AutoFile &) = delete;
526 AutoFile &operator=(const AutoFile &) = delete;
527
528 bool feof() const { return std::feof(m_file); }
529
530 int fclose() {
531 if (auto rel{release()}) {
532 return std::fclose(rel);
533 }
534 return 0;
535 }
536
543 std::FILE *release() {
544 std::FILE *ret{m_file};
545 m_file = nullptr;
546 return ret;
547 }
548
554 std::FILE *Get() const { return m_file; }
555
557 bool IsNull() const { return m_file == nullptr; }
558
560 std::size_t detail_fread(Span<std::byte> dst);
561
562 //
563 // Stream subset
564 //
565 void read(Span<std::byte> dst);
566 void ignore(size_t nSize);
568
569 template <typename T> AutoFile &operator<<(const T &obj) {
570 ::Serialize(*this, obj);
571 return *this;
572 }
573
574 template <typename T> AutoFile &operator>>(T &&obj) {
575 ::Unserialize(*this, obj);
576 return *this;
577 }
578};
579
580class CAutoFile : public AutoFile {
581private:
582 const int nType;
583 const int nVersion;
584
585public:
586 CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
587 : AutoFile{filenew}, nType(nTypeIn), nVersion(nVersionIn) {}
588 int GetType() const { return nType; }
589 int GetVersion() const { return nVersion; }
590
591 template <typename T> CAutoFile &operator<<(const T &obj) {
592 ::Serialize(*this, obj);
593 return (*this);
594 }
595
596 template <typename T> CAutoFile &operator>>(T &&obj) {
597 ::Unserialize(*this, obj);
598 return (*this);
599 }
600};
601
611private:
612 const int nType;
613 const int nVersion;
614
616 FILE *src;
618 uint64_t nSrcPos;
620 uint64_t m_read_pos;
622 uint64_t nReadLimit;
624 uint64_t nRewind;
626 std::vector<std::byte> vchBuf;
627
629 bool Fill() {
630 unsigned int pos = nSrcPos % vchBuf.size();
631 unsigned int readNow = vchBuf.size() - pos;
632 unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind;
633 if (nAvail < readNow) {
634 readNow = nAvail;
635 }
636 if (readNow == 0) {
637 return false;
638 }
639 size_t nBytes = fread((void *)&vchBuf[pos], 1, readNow, src);
640 if (nBytes == 0) {
641 throw std::ios_base::failure(
642 feof(src) ? "CBufferedFile::Fill: end of file"
643 : "CBufferedFile::Fill: fread failed");
644 }
645 nSrcPos += nBytes;
646 return true;
647 }
648
654 std::pair<std::byte *, size_t> AdvanceStream(size_t length) {
656 if (m_read_pos + length > nReadLimit) {
657 throw std::ios_base::failure(
658 "Attempt to position past buffer limit");
659 }
660 // If there are no bytes available, read from the file.
661 if (m_read_pos == nSrcPos && length > 0) {
662 Fill();
663 }
664
665 size_t buffer_offset{static_cast<size_t>(m_read_pos % vchBuf.size())};
666 size_t buffer_available{
667 static_cast<size_t>(vchBuf.size() - buffer_offset)};
668 size_t bytes_until_source_pos{
669 static_cast<size_t>(nSrcPos - m_read_pos)};
670 size_t advance{
671 std::min({length, buffer_available, bytes_until_source_pos})};
672 m_read_pos += advance;
673 return std::make_pair(&vchBuf[buffer_offset], advance);
674 }
675
676public:
677 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
678 int nTypeIn, int nVersionIn)
679 : nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), m_read_pos(0),
680 nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn),
681 vchBuf(nBufSize, std::byte{0}) {
682 if (nRewindIn >= nBufSize) {
683 throw std::ios_base::failure(
684 "Rewind limit must be less than buffer size");
685 }
686 src = fileIn;
687 }
688
690
691 // Disallow copies
692 CBufferedFile(const CBufferedFile &) = delete;
694
695 int GetVersion() const { return nVersion; }
696 int GetType() const { return nType; }
697
698 void fclose() {
699 if (src) {
700 ::fclose(src);
701 src = nullptr;
702 }
703 }
704
706 bool eof() const { return m_read_pos == nSrcPos && feof(src); }
707
710 while (dst.size() > 0) {
711 auto [buffer_pointer, length]{AdvanceStream(dst.size())};
712 memcpy(dst.data(), buffer_pointer, length);
713 dst = dst.subspan(length);
714 }
715 }
716
719 void SkipTo(const uint64_t file_pos) {
720 assert(file_pos >= m_read_pos);
721 while (m_read_pos < file_pos) {
722 AdvanceStream(file_pos - m_read_pos);
723 }
724 }
725
727 uint64_t GetPos() const { return m_read_pos; }
728
730 bool SetPos(uint64_t nPos) {
731 size_t bufsize = vchBuf.size();
732 if (nPos + bufsize < nSrcPos) {
733 // rewinding too far, rewind as far as possible
734 m_read_pos = nSrcPos - bufsize;
735 return false;
736 }
737 if (nPos > nSrcPos) {
738 // can't go this far forward, go as far as possible
740 return false;
741 }
742 m_read_pos = nPos;
743 return true;
744 }
745
748 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
749 if (nPos < m_read_pos) {
750 return false;
751 }
752 nReadLimit = nPos;
753 return true;
754 }
755
756 template <typename T> CBufferedFile &operator>>(T &&obj) {
757 ::Unserialize(*this, obj);
758 return (*this);
759 }
760
762 void FindByte(std::byte byte) {
763 // For best performance, avoid mod operation within the loop.
764 size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))};
765 while (true) {
766 if (m_read_pos == nSrcPos) {
767 // No more bytes available; read from the file into the buffer,
768 // setting nSrcPos to one beyond the end of the new data.
769 // Throws exception if end-of-file reached.
770 Fill();
771 }
772 const size_t len{std::min<size_t>(vchBuf.size() - buf_offset,
774 const auto it_start{vchBuf.begin() + buf_offset};
775 const auto it_find{std::find(it_start, it_start + len, byte)};
776 const size_t inc{size_t(std::distance(it_start, it_find))};
777 m_read_pos += inc;
778 if (inc < len) {
779 break;
780 }
781 buf_offset += inc;
782 if (buf_offset >= vchBuf.size()) {
783 buf_offset = 0;
784 }
785 }
786 }
787};
788
789#endif // BITCOIN_STREAMS_H
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:515
bool feof() const
Definition: streams.h:528
std::FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:543
void ignore(size_t nSize)
Definition: streams.cpp:22
AutoFile & operator=(const AutoFile &)=delete
~AutoFile()
Definition: streams.h:522
std::FILE * m_file
Definition: streams.h:517
void read(Span< std::byte > dst)
Definition: streams.cpp:15
AutoFile & operator<<(const T &obj)
Definition: streams.h:569
AutoFile(const AutoFile &)=delete
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:557
std::FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:554
AutoFile & operator>>(T &&obj)
Definition: streams.h:574
std::size_t detail_fread(Span< std::byte > dst)
Implementation detail, only used internally.
Definition: streams.cpp:8
void write(Span< const std::byte > src)
Definition: streams.cpp:39
AutoFile(std::FILE *file)
Definition: streams.h:520
int fclose()
Definition: streams.h:530
uint8_t m_buffer
Buffered byte read in from the input stream.
Definition: streams.h:418
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:432
BitStreamReader(IStream &istream)
Definition: streams.h:426
IStream & m_istream
Definition: streams.h:414
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
Definition: streams.h:423
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:476
OStream & m_ostream
Definition: streams.h:456
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
Definition: streams.h:460
BitStreamWriter(OStream &ostream)
Definition: streams.h:468
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:465
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:497
CAutoFile & operator>>(T &&obj)
Definition: streams.h:596
const int nType
Definition: streams.h:582
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:586
int GetType() const
Definition: streams.h:588
const int nVersion
Definition: streams.h:583
CAutoFile & operator<<(const T &obj)
Definition: streams.h:591
int GetVersion() const
Definition: streams.h:589
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
Definition: streams.h:610
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
Prevent reading beyond a certain position.
Definition: streams.h:748
uint64_t nReadLimit
up to which position we're allowed to read
Definition: streams.h:622
int GetVersion() const
Definition: streams.h:695
bool Fill()
read data from the source to fill the buffer
Definition: streams.h:629
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:677
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:624
void read(Span< std::byte > dst)
read a number of bytes
Definition: streams.h:709
std::vector< std::byte > vchBuf
the buffer
Definition: streams.h:626
uint64_t GetPos() const
return the current reading position
Definition: streams.h:727
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:756
void FindByte(std::byte byte)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:762
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:654
FILE * src
source file
Definition: streams.h:616
~CBufferedFile()
Definition: streams.h:689
int GetType() const
Definition: streams.h:696
uint64_t nSrcPos
how many bytes have been read from source
Definition: streams.h:618
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Definition: streams.h:719
uint64_t m_read_pos
how many bytes have been read from this
Definition: streams.h:620
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:730
const int nType
Definition: streams.h:612
CBufferedFile & operator=(const CBufferedFile &)=delete
CBufferedFile(const CBufferedFile &)=delete
const int nVersion
Definition: streams.h:613
void fclose()
Definition: streams.h:698
bool eof() const
check whether we're at the end of the source file
Definition: streams.h:706
int nVersion
Definition: streams.h:386
void SetVersion(int n)
Definition: streams.h:398
int nType
Definition: streams.h:385
CDataStream & operator<<(const T &obj)
Definition: streams.h:401
int GetType() const
Definition: streams.h:397
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:389
CDataStream(Span< const value_type > sp, int nTypeIn, int nVersionIn)
Definition: streams.h:394
int GetVersion() const
Definition: streams.h:399
CDataStream & operator>>(T &&obj)
Definition: streams.h:406
CDataStream(Span< const uint8_t > sp, int type, int version)
Definition: streams.h:392
Minimal stream for overwriting and/or appending to an existing byte vector.
Definition: streams.h:64
void write(Span< const std::byte > src)
Definition: streams.h:92
const int nVersion
Definition: streams.h:119
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< uint8_t > &vchDataIn, size_t nPosIn, Args &&...args)
(other params same as above)
Definition: streams.h:87
std::vector< uint8_t > & vchData
Definition: streams.h:120
const int nType
Definition: streams.h:118
void seek(size_t nSize)
Definition: streams.h:110
int GetType() const
Definition: streams.h:109
size_t nPos
Definition: streams.h:121
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:104
int GetVersion() const
Definition: streams.h:108
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< uint8_t > &vchDataIn, size_t nPosIn)
Definition: streams.h:74
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:174
void write(Span< const value_type > src)
Definition: streams.h:346
DataStream & operator<<(const T &obj)
Definition: streams.h:351
bool empty() const
Definition: streams.h:208
vector_type::difference_type difference_type
Definition: streams.h:183
size_type size() const
Definition: streams.h:207
DataStream & operator>>(T &&obj)
Definition: streams.h:356
reference operator[](size_type pos)
Definition: streams.h:216
void Xor(const std::vector< uint8_t > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:366
void resize(size_type n, value_type c=value_type{})
Definition: streams.h:209
void insert(iterator it, const value_type *first, const value_type *last)
Definition: streams.h:243
const_reference operator[](size_type pos) const
Definition: streams.h:213
void insert(iterator it, std::vector< value_type >::const_iterator first, std::vector< value_type >::const_iterator last)
Definition: streams.h:224
DataStream(Span< const value_type > sp)
Definition: streams.h:193
vector_type::size_type size_type
Definition: streams.h:182
iterator erase(iterator it)
Definition: streams.h:259
SerializeData vector_type
Definition: streams.h:176
vector_type vch
Definition: streams.h:177
const value_type * data() const
Definition: streams.h:222
vector_type::const_reference const_reference
Definition: streams.h:185
value_type * data()
Definition: streams.h:221
vector_type::const_iterator const_iterator
Definition: streams.h:188
DataStream(Span< const uint8_t > sp)
Definition: streams.h:192
void reserve(size_type n)
Definition: streams.h:212
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:189
iterator begin()
Definition: streams.h:204
const_iterator begin() const
Definition: streams.h:203
void read(Span< value_type > dst)
Definition: streams.h:313
DataStream()
Definition: streams.h:191
vector_type::size_type m_read_pos
Definition: streams.h:178
vector_type::iterator iterator
Definition: streams.h:187
vector_type::value_type value_type
Definition: streams.h:186
bool eof() const
Definition: streams.h:310
std::string str() const
Definition: streams.h:196
void ignore(size_t num_ignore)
Definition: streams.h:332
vector_type::allocator_type allocator_type
Definition: streams.h:181
const_iterator end() const
Definition: streams.h:205
void Compact()
Definition: streams.h:288
void clear()
Definition: streams.h:217
iterator end()
Definition: streams.h:206
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:293
vector_type::reference reference
Definition: streams.h:184
int in_avail() const
Definition: streams.h:311
iterator erase(iterator first, iterator last)
Definition: streams.h:274
const int nVersion
Definition: streams.h:30
void ignore(size_t size)
Definition: streams.h:52
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:33
void read(Span< std::byte > dst)
Definition: streams.h:48
void write(Span< const std::byte > src)
Definition: streams.h:46
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:41
Stream * stream
Definition: streams.h:27
int GetVersion() const
Definition: streams.h:50
const int nType
Definition: streams.h:29
int GetType() const
Definition: streams.h:51
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:36
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:94
constexpr std::size_t size() const noexcept
Definition: span.h:210
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:219
constexpr C * data() const noexcept
Definition: span.h:199
constexpr C * begin() const noexcept
Definition: span.h:200
constexpr bool empty() const noexcept
Definition: span.h:214
constexpr C * end() const noexcept
Definition: span.h:201
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:127
bool empty() const
Definition: streams.h:151
const int m_type
Definition: streams.h:129
size_t size() const
Definition: streams.h:150
Span< const uint8_t > m_data
Definition: streams.h:131
SpanReader & operator>>(T &obj)
Definition: streams.h:142
const int m_version
Definition: streams.h:130
int GetType() const
Definition: streams.h:148
void read(Span< std::byte > dst)
Definition: streams.h:153
SpanReader(int type, int version, Span< const uint8_t > data)
Definition: streams.h:139
int GetVersion() const
Definition: streams.h:147
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition: overflow.h:24
void Serialize(Stream &, V)=delete
void SerializeMany(Stream &s)
Definition: serialize.h:1226
void Unserialize(Stream &, V)=delete
uint8_t * UCharCast(char *c)
Definition: span.h:310
Span< const std::byte > AsBytes(Span< T > s) noexcept
Definition: span.h:295
OverrideStream< S > WithOrVersion(S *s, int nVersionFlag)
Definition: streams.h:55
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:42