Bitcoin Core 31.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-present 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/check.h>
13#include <util/log.h>
14#include <util/obfuscation.h>
15#include <util/overflow.h>
16#include <util/syserror.h>
17
18#include <algorithm>
19#include <cassert>
20#include <cstddef>
21#include <cstdint>
22#include <cstdio>
23#include <cstring>
24#include <ios>
25#include <limits>
26#include <optional>
27#include <string>
28#include <vector>
29
30/* Minimal stream for overwriting and/or appending to an existing byte vector
31 *
32 * The referenced vector will grow as necessary
33 */
35{
36public:
37/*
38 * @param[in] vchDataIn Referenced byte vector to overwrite/append
39 * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
40 * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
41*/
42 VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn) : vchData{vchDataIn}, nPos{nPosIn}
43 {
44 if(nPos > vchData.size())
45 vchData.resize(nPos);
46 }
47/*
48 * (other params same as above)
49 * @param[in] args A list of items to serialize starting at nPosIn.
50*/
51 template <typename... Args>
52 VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : VectorWriter{vchDataIn, nPosIn}
53 {
54 ::SerializeMany(*this, std::forward<Args>(args)...);
55 }
56 void write(std::span<const std::byte> src)
57 {
58 assert(nPos <= vchData.size());
59 size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
60 if (nOverwrite) {
61 memcpy(vchData.data() + nPos, src.data(), nOverwrite);
62 }
63 if (nOverwrite < src.size()) {
64 vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.data() + src.size()));
65 }
66 nPos += src.size();
67 }
68 template <typename T>
70 {
71 ::Serialize(*this, obj);
72 return (*this);
73 }
74
75private:
76 std::vector<unsigned char>& vchData;
77 size_t nPos;
78};
79
83{
84private:
85 std::span<const std::byte> m_data;
86
87public:
91 explicit SpanReader(std::span<const unsigned char> data) : m_data{std::as_bytes(data)} {}
92 explicit SpanReader(std::span<const std::byte> data) : m_data{data} {}
93
94 template<typename T>
96 {
97 ::Unserialize(*this, obj);
98 return (*this);
99 }
100
101 size_t size() const { return m_data.size(); }
102 bool empty() const { return m_data.empty(); }
103
104 void read(std::span<std::byte> dst)
105 {
106 if (dst.size() == 0) {
107 return;
108 }
109
110 // Read from the beginning of the buffer
111 if (dst.size() > m_data.size()) {
112 throw std::ios_base::failure("SpanReader::read(): end of data");
113 }
114 memcpy(dst.data(), m_data.data(), dst.size());
115 m_data = m_data.subspan(dst.size());
116 }
117
118 void ignore(size_t n)
119 {
120 if (n > m_data.size()) {
121 throw std::ios_base::failure("SpanReader::ignore(): end of data");
122 }
123 m_data = m_data.subspan(n);
124 }
125};
126
130{
131private:
132 std::span<std::byte> m_dest;
133
134public:
135 explicit SpanWriter(std::span<std::byte> dest) : m_dest{dest} {}
136 template <typename... Args>
137 SpanWriter(std::span<std::byte> dest, Args&&... args) : SpanWriter{dest}
138 {
139 ::SerializeMany(*this, std::forward<Args>(args)...);
140 }
141
142 void write(std::span<const std::byte> src)
143 {
144 if (src.size() > m_dest.size()) {
145 throw std::ios_base::failure("SpanWriter::write(): exceeded buffer size");
146 }
147 memcpy(m_dest.data(), src.data(), src.size());
148 m_dest = m_dest.subspan(src.size());
149 }
150
151 template<typename T>
153 {
154 ::Serialize(*this, obj);
155 return *this;
156 }
157};
158
165{
166protected:
169 vector_type::size_type m_read_pos{0};
170
171public:
172 typedef vector_type::allocator_type allocator_type;
173 typedef vector_type::size_type size_type;
174 typedef vector_type::difference_type difference_type;
175 typedef vector_type::reference reference;
176 typedef vector_type::const_reference const_reference;
177 typedef vector_type::value_type value_type;
178 typedef vector_type::iterator iterator;
179 typedef vector_type::const_iterator const_iterator;
180 typedef vector_type::reverse_iterator reverse_iterator;
181
182 explicit DataStream() = default;
183 explicit DataStream(std::span<const uint8_t> sp) : DataStream{std::as_bytes(sp)} {}
184 explicit DataStream(std::span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
185
186 std::string str() const
187 {
188 return std::string{UCharCast(data()), UCharCast(data() + size())};
189 }
190
191
192 //
193 // Vector subset
194 //
195 const_iterator begin() const { return vch.begin() + m_read_pos; }
196 iterator begin() { return vch.begin() + m_read_pos; }
197 const_iterator end() const { return vch.end(); }
198 iterator end() { return vch.end(); }
199 size_type size() const { return vch.size() - m_read_pos; }
200 bool empty() const { return vch.size() == m_read_pos; }
201 void resize(size_type n, value_type c = value_type{}) { vch.resize(n + m_read_pos, c); }
202 void reserve(size_type n) { vch.reserve(n + m_read_pos); }
203 const_reference operator[](size_type pos) const { return vch[pos + m_read_pos]; }
205 void clear() { vch.clear(); m_read_pos = 0; }
206 value_type* data() { return vch.data() + m_read_pos; }
207 const value_type* data() const { return vch.data() + m_read_pos; }
208
209 //
210 // Stream subset
211 //
212 int in_avail() const { return size(); }
213
214 void read(std::span<value_type> dst)
215 {
216 if (dst.size() == 0) return;
217
218 // Read from the beginning of the buffer
219 auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
220 if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
221 throw std::ios_base::failure("DataStream::read(): end of data");
222 }
223 memcpy(dst.data(), &vch[m_read_pos], dst.size());
224 if (next_read_pos.value() == vch.size()) {
225 // If fully consumed, reset to empty state.
226 clear();
227 return;
228 }
229 m_read_pos = next_read_pos.value();
230 }
231
232 void ignore(size_t num_ignore)
233 {
234 // Ignore from the beginning of the buffer
235 auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
236 if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
237 throw std::ios_base::failure("DataStream::ignore(): end of data");
238 }
239 if (next_read_pos.value() == vch.size()) {
240 // If all bytes are ignored, reset to empty state.
241 clear();
242 return;
243 }
244 m_read_pos = next_read_pos.value();
245 }
246
247 void write(std::span<const value_type> src)
248 {
249 // Write to the end of the buffer
250 vch.insert(vch.end(), src.begin(), src.end());
251 }
252
253 template<typename T>
255 {
256 ::Serialize(*this, obj);
257 return (*this);
258 }
259
260 template <typename T>
262 {
263 ::Unserialize(*this, obj);
264 return (*this);
265 }
266
268 size_t GetMemoryUsage() const noexcept;
269};
270
271template <typename IStream>
273{
274private:
275 IStream& m_istream;
276
279 uint8_t m_buffer{0};
280
284 int m_offset{8};
285
286public:
287 explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
288
292 uint64_t Read(int nbits) {
293 if (nbits < 0 || nbits > 64) {
294 throw std::out_of_range("nbits must be between 0 and 64");
295 }
296
297 uint64_t data = 0;
298 while (nbits > 0) {
299 if (m_offset == 8) {
300 m_istream >> m_buffer;
301 m_offset = 0;
302 }
303
304 int bits = std::min(8 - m_offset, nbits);
305 data <<= bits;
306 data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
307 m_offset += bits;
308 nbits -= bits;
309 }
310 return data;
311 }
312};
313
314template <typename OStream>
316{
317private:
318 OStream& m_ostream;
319
322 uint8_t m_buffer{0};
323
327 int m_offset{0};
328
329public:
330 explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
331
333 {
334 Flush();
335 }
336
340 void Write(uint64_t data, int nbits) {
341 if (nbits < 0 || nbits > 64) {
342 throw std::out_of_range("nbits must be between 0 and 64");
343 }
344
345 while (nbits > 0) {
346 int bits = std::min(8 - m_offset, nbits);
347 m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
348 m_offset += bits;
349 nbits -= bits;
350
351 if (m_offset == 8) {
352 Flush();
353 }
354 }
355 }
356
360 void Flush() {
361 if (m_offset == 0) {
362 return;
363 }
364
365 m_ostream << m_buffer;
366 m_buffer = 0;
367 m_offset = 0;
368 }
369};
370
384{
385protected:
386 std::FILE* m_file;
388 std::optional<int64_t> m_position;
389 bool m_was_written{false};
390
391public:
392 explicit AutoFile(std::FILE* file, const Obfuscation& obfuscation = {});
393
395 {
396 if (m_was_written) {
397 // Callers that wrote to the file must have closed it explicitly
398 // with the fclose() method and checked that the close succeeded.
399 // This is because here in the destructor we have no way to signal
400 // errors from fclose() which, after write, could mean the file is
401 // corrupted and must be handled properly at the call site.
402 // Destructors in C++ cannot signal an error to the callers because
403 // they do not return a value and are not allowed to throw exceptions.
404 Assume(IsNull());
405 }
406
407 if (fclose() != 0) {
408 LogError("Failed to close file: %s", SysErrorString(errno));
409 }
410 }
411
412 // Disallow copies
413 AutoFile(const AutoFile&) = delete;
414 AutoFile& operator=(const AutoFile&) = delete;
415
416 bool feof() const { return std::feof(m_file); }
417
418 [[nodiscard]] int fclose()
419 {
420 if (auto rel{release()}) return std::fclose(rel);
421 return 0;
422 }
423
428 std::FILE* release()
429 {
430 std::FILE* ret{m_file};
431 m_file = nullptr;
432 return ret;
433 }
434
437 bool IsNull() const { return m_file == nullptr; }
438
440 void SetObfuscation(const Obfuscation& obfuscation) { m_obfuscation = obfuscation; }
441
443 std::size_t detail_fread(std::span<std::byte> dst);
444
446 void seek(int64_t offset, int origin);
447
449 int64_t tell();
450
452 int64_t size();
453
455 bool Commit();
456
458 bool Truncate(unsigned size);
459
461 void write_buffer(std::span<std::byte> src);
462
463 //
464 // Stream subset
465 //
466 void read(std::span<std::byte> dst);
467 void ignore(size_t nSize);
468 void write(std::span<const std::byte> src);
469
470 template <typename T>
471 AutoFile& operator<<(const T& obj)
472 {
473 ::Serialize(*this, obj);
474 return *this;
475 }
476
477 template <typename T>
479 {
480 ::Unserialize(*this, obj);
481 return *this;
482 }
483};
484
485using DataBuffer = std::vector<std::byte>;
486
494{
495private:
497 uint64_t nSrcPos{0};
498 uint64_t m_read_pos{0};
499 uint64_t nReadLimit;
500 uint64_t nRewind;
502
504 bool Fill() {
505 unsigned int pos = nSrcPos % vchBuf.size();
506 unsigned int readNow = vchBuf.size() - pos;
507 unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind;
508 if (nAvail < readNow)
509 readNow = nAvail;
510 if (readNow == 0)
511 return false;
512 size_t nBytes{m_src.detail_fread(std::span{vchBuf}.subspan(pos, readNow))};
513 if (nBytes == 0) {
514 throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"};
515 }
516 nSrcPos += nBytes;
517 return true;
518 }
519
525 std::pair<std::byte*, size_t> AdvanceStream(size_t length)
526 {
527 assert(m_read_pos <= nSrcPos);
528 if (m_read_pos + length > nReadLimit) {
529 throw std::ios_base::failure("Attempt to position past buffer limit");
530 }
531 // If there are no bytes available, read from the file.
532 if (m_read_pos == nSrcPos && length > 0) Fill();
533
534 size_t buffer_offset{static_cast<size_t>(m_read_pos % vchBuf.size())};
535 size_t buffer_available{static_cast<size_t>(vchBuf.size() - buffer_offset)};
536 size_t bytes_until_source_pos{static_cast<size_t>(nSrcPos - m_read_pos)};
537 size_t advance{std::min({length, buffer_available, bytes_until_source_pos})};
538 m_read_pos += advance;
539 return std::make_pair(&vchBuf[buffer_offset], advance);
540 }
541
542public:
543 BufferedFile(AutoFile& file LIFETIMEBOUND, uint64_t nBufSize, uint64_t nRewindIn)
544 : m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
545 {
546 if (nRewindIn >= nBufSize)
547 throw std::ios_base::failure("Rewind limit must be less than buffer size");
548 }
549
551 bool eof() const {
552 return m_read_pos == nSrcPos && m_src.feof();
553 }
554
556 void read(std::span<std::byte> dst)
557 {
558 while (dst.size() > 0) {
559 auto [buffer_pointer, length]{AdvanceStream(dst.size())};
560 memcpy(dst.data(), buffer_pointer, length);
561 dst = dst.subspan(length);
562 }
563 }
564
567 void SkipTo(const uint64_t file_pos)
568 {
569 assert(file_pos >= m_read_pos);
570 while (m_read_pos < file_pos) AdvanceStream(file_pos - m_read_pos);
571 }
572
574 uint64_t GetPos() const {
575 return m_read_pos;
576 }
577
579 bool SetPos(uint64_t nPos) {
580 size_t bufsize = vchBuf.size();
581 if (nPos + bufsize < nSrcPos) {
582 // rewinding too far, rewind as far as possible
583 m_read_pos = nSrcPos - bufsize;
584 return false;
585 }
586 if (nPos > nSrcPos) {
587 // can't go this far forward, go as far as possible
588 m_read_pos = nSrcPos;
589 return false;
590 }
591 m_read_pos = nPos;
592 return true;
593 }
594
597 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
598 if (nPos < m_read_pos)
599 return false;
600 nReadLimit = nPos;
601 return true;
602 }
603
604 template<typename T>
606 ::Unserialize(*this, obj);
607 return (*this);
608 }
609
611 void FindByte(std::byte byte)
612 {
613 // For best performance, avoid mod operation within the loop.
614 size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))};
615 while (true) {
616 if (m_read_pos == nSrcPos) {
617 // No more bytes available; read from the file into the buffer,
618 // setting nSrcPos to one beyond the end of the new data.
619 // Throws exception if end-of-file reached.
620 Fill();
621 }
622 const size_t len{std::min<size_t>(vchBuf.size() - buf_offset, nSrcPos - m_read_pos)};
623 const auto it_start{vchBuf.begin() + buf_offset};
624 const auto it_find{std::find(it_start, it_start + len, byte)};
625 const size_t inc{size_t(std::distance(it_start, it_find))};
626 m_read_pos += inc;
627 if (inc < len) break;
628 buf_offset += inc;
629 if (buf_offset >= vchBuf.size()) buf_offset = 0;
630 }
631 }
632};
633
639template <typename S>
641{
644 size_t m_buf_pos;
645
646public:
648 explicit BufferedReader(S&& stream LIFETIMEBOUND, size_t size = 1 << 16)
649 requires std::is_rvalue_reference_v<S&&>
650 : m_src{stream}, m_buf(size), m_buf_pos{size} {}
651
652 void read(std::span<std::byte> dst)
653 {
654 if (const auto available{std::min(dst.size(), m_buf.size() - m_buf_pos)}) {
655 std::copy_n(m_buf.begin() + m_buf_pos, available, dst.begin());
656 m_buf_pos += available;
657 dst = dst.subspan(available);
658 }
659 if (dst.size()) {
660 assert(m_buf_pos == m_buf.size());
661 m_src.read(dst);
662
663 m_buf_pos = 0;
664 m_buf.resize(m_src.detail_fread(m_buf));
665 }
666 }
667
668 template <typename T>
670 {
671 Unserialize(*this, obj);
672 return *this;
673 }
674};
675
681template <typename S>
683{
686 size_t m_buf_pos{0};
687
688public:
689 explicit BufferedWriter(S& stream LIFETIMEBOUND, size_t size = 1 << 16) : m_dst{stream}, m_buf(size) {}
690
691 ~BufferedWriter() { flush(); }
692
693 void flush()
694 {
695 if (m_buf_pos) m_dst.write_buffer(std::span{m_buf}.first(m_buf_pos));
696 m_buf_pos = 0;
697 }
698
699 void write(std::span<const std::byte> src)
700 {
701 while (const auto available{std::min(src.size(), m_buf.size() - m_buf_pos)}) {
702 std::copy_n(src.begin(), available, m_buf.begin() + m_buf_pos);
703 m_buf_pos += available;
704 if (m_buf_pos == m_buf.size()) flush();
705 src = src.subspan(available);
706 }
707 }
708
709 template <typename T>
711 {
712 Serialize(*this, obj);
713 return *this;
714 }
715};
716
717#endif // BITCOIN_STREAMS_H
#define LIFETIMEBOUND
Definition: attributes.h:16
int ret
ArgsManager & args
Definition: bitcoind.cpp:278
#define Assume(val)
Assume is the identity function.
Definition: check.h:128
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:384
bool feof() const
Definition: streams.h:416
std::FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:428
AutoFile & operator=(const AutoFile &)=delete
~AutoFile()
Definition: streams.h:394
std::FILE * m_file
Definition: streams.h:386
void SetObfuscation(const Obfuscation &obfuscation)
Continue with a different XOR key.
Definition: streams.h:440
AutoFile & operator<<(const T &obj)
Definition: streams.h:471
std::size_t detail_fread(std::span< std::byte > dst)
Implementation detail, only used internally.
Definition: streams.cpp:21
AutoFile(const AutoFile &)=delete
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:437
AutoFile & operator>>(T &&obj)
Definition: streams.h:478
std::optional< int64_t > m_position
Definition: streams.h:388
Obfuscation m_obfuscation
Definition: streams.h:387
int fclose()
Definition: streams.h:418
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:292
BitStreamReader(IStream &istream)
Definition: streams.h:287
IStream & m_istream
Definition: streams.h:275
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:340
OStream & m_ostream
Definition: streams.h:318
BitStreamWriter(OStream &ostream)
Definition: streams.h:330
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:360
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
Definition: streams.h:494
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:525
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:500
void read(std::span< std::byte > dst)
read a number of bytes
Definition: streams.h:556
BufferedFile(AutoFile &file LIFETIMEBOUND, uint64_t nBufSize, uint64_t nRewindIn)
Definition: streams.h:543
bool eof() const
check whether we're at the end of the source file
Definition: streams.h:551
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:597
DataBuffer vchBuf
Definition: streams.h:501
BufferedFile & operator>>(T &&obj)
Definition: streams.h:605
uint64_t GetPos() const
return the current reading position
Definition: streams.h:574
uint64_t nReadLimit
up to which position we're allowed to read
Definition: streams.h:499
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Definition: streams.h:567
void FindByte(std::byte byte)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:611
bool Fill()
read data from the source to fill the buffer
Definition: streams.h:504
AutoFile & m_src
Definition: streams.h:496
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:579
Wrapper that buffers reads from an underlying stream.
Definition: streams.h:641
size_t m_buf_pos
Definition: streams.h:644
DataBuffer m_buf
Definition: streams.h:643
BufferedReader(S &&stream LIFETIMEBOUND, size_t size=1<< 16)
Requires stream ownership to prevent leaving the stream at an unexpected position after buffered read...
Definition: streams.h:648
BufferedReader & operator>>(T &&obj)
Definition: streams.h:669
void read(std::span< std::byte > dst)
Definition: streams.h:652
Wrapper that buffers writes to an underlying stream.
Definition: streams.h:683
DataBuffer m_buf
Definition: streams.h:685
void write(std::span< const std::byte > src)
Definition: streams.h:699
void flush()
Definition: streams.h:693
BufferedWriter & operator<<(const T &obj)
Definition: streams.h:710
BufferedWriter(S &stream LIFETIMEBOUND, size_t size=1<< 16)
Definition: streams.h:689
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:165
DataStream & operator<<(const T &obj)
Definition: streams.h:254
bool empty() const
Definition: streams.h:200
vector_type::difference_type difference_type
Definition: streams.h:174
void write(std::span< const value_type > src)
Definition: streams.h:247
size_type size() const
Definition: streams.h:199
DataStream & operator>>(T &&obj)
Definition: streams.h:261
reference operator[](size_type pos)
Definition: streams.h:204
void resize(size_type n, value_type c=value_type{})
Definition: streams.h:201
const_reference operator[](size_type pos) const
Definition: streams.h:203
void read(std::span< value_type > dst)
Definition: streams.h:214
vector_type::size_type size_type
Definition: streams.h:173
SerializeData vector_type
Definition: streams.h:167
vector_type vch
Definition: streams.h:168
const value_type * data() const
Definition: streams.h:207
vector_type::const_reference const_reference
Definition: streams.h:176
value_type * data()
Definition: streams.h:206
vector_type::const_iterator const_iterator
Definition: streams.h:179
void reserve(size_type n)
Definition: streams.h:202
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:180
DataStream(std::span< const value_type > sp)
Definition: streams.h:184
iterator begin()
Definition: streams.h:196
const_iterator begin() const
Definition: streams.h:195
vector_type::size_type m_read_pos
Definition: streams.h:169
vector_type::iterator iterator
Definition: streams.h:178
vector_type::value_type value_type
Definition: streams.h:177
DataStream(std::span< const uint8_t > sp)
Definition: streams.h:183
std::string str() const
Definition: streams.h:186
void ignore(size_t num_ignore)
Definition: streams.h:232
vector_type::allocator_type allocator_type
Definition: streams.h:172
const_iterator end() const
Definition: streams.h:197
void clear()
Definition: streams.h:205
size_t GetMemoryUsage() const noexcept
Compute total memory usage of this object (own memory + any dynamic memory).
Definition: streams.cpp:140
iterator end()
Definition: streams.h:198
DataStream()=default
vector_type::reference reference
Definition: streams.h:175
int in_avail() const
Definition: streams.h:212
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:83
bool empty() const
Definition: streams.h:102
SpanReader & operator>>(T &&obj)
Definition: streams.h:95
std::span< const std::byte > m_data
Definition: streams.h:85
size_t size() const
Definition: streams.h:101
SpanReader(std::span< const std::byte > data)
Definition: streams.h:92
void read(std::span< std::byte > dst)
Definition: streams.h:104
void ignore(size_t n)
Definition: streams.h:118
SpanReader(std::span< const unsigned char > data)
Definition: streams.h:91
Minimal stream for writing to an existing span of bytes.
Definition: streams.h:130
void write(std::span< const std::byte > src)
Definition: streams.h:142
SpanWriter & operator<<(const T &obj)
Definition: streams.h:152
SpanWriter(std::span< std::byte > dest)
Definition: streams.h:135
SpanWriter(std::span< std::byte > dest, Args &&... args)
Definition: streams.h:137
std::span< std::byte > m_dest
Definition: streams.h:132
std::vector< unsigned char > & vchData
Definition: streams.h:76
VectorWriter & operator<<(const T &obj)
Definition: streams.h:69
size_t nPos
Definition: streams.h:77
void write(std::span< const std::byte > src)
Definition: streams.h:56
VectorWriter(std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:52
VectorWriter(std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:42
#define LogError(...)
Definition: log.h:99
Definition: common.h:30
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition: overflow.h:28
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
void Serialize(Stream &, V)=delete
void SerializeMany(Stream &s, const Args &... args)
Support for (un)serializing many things at once.
Definition: serialize.h:987
void Unserialize(Stream &, V)=delete
unsigned char * UCharCast(char *c)
Definition: span.h:95
std::vector< std::byte > DataBuffer
Definition: streams.h:485
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:18
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