Bitcoin Core 29.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/overflow.h>
13
14#include <algorithm>
15#include <cassert>
16#include <cstddef>
17#include <cstdint>
18#include <cstdio>
19#include <cstring>
20#include <ios>
21#include <limits>
22#include <optional>
23#include <string>
24#include <utility>
25#include <vector>
26
27namespace util {
28inline void Xor(std::span<std::byte> write, std::span<const std::byte> key, size_t key_offset = 0)
29{
30 if (key.size() == 0) {
31 return;
32 }
33 key_offset %= key.size();
34
35 for (size_t i = 0, j = key_offset; i != write.size(); i++) {
36 write[i] ^= key[j++];
37
38 // This potentially acts on very many bytes of data, so it's
39 // important that we calculate `j`, i.e. the `key` index in this
40 // way instead of doing a %, which would effectively be a division
41 // for each byte Xor'd -- much slower than need be.
42 if (j == key.size())
43 j = 0;
44 }
45}
46} // namespace util
47
48/* Minimal stream for overwriting and/or appending to an existing byte vector
49 *
50 * The referenced vector will grow as necessary
51 */
53{
54public:
55/*
56 * @param[in] vchDataIn Referenced byte vector to overwrite/append
57 * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
58 * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
59*/
60 VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn) : vchData{vchDataIn}, nPos{nPosIn}
61 {
62 if(nPos > vchData.size())
63 vchData.resize(nPos);
64 }
65/*
66 * (other params same as above)
67 * @param[in] args A list of items to serialize starting at nPosIn.
68*/
69 template <typename... Args>
70 VectorWriter(std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : VectorWriter{vchDataIn, nPosIn}
71 {
72 ::SerializeMany(*this, std::forward<Args>(args)...);
73 }
74 void write(std::span<const std::byte> src)
75 {
76 assert(nPos <= vchData.size());
77 size_t nOverwrite = std::min(src.size(), vchData.size() - nPos);
78 if (nOverwrite) {
79 memcpy(vchData.data() + nPos, src.data(), nOverwrite);
80 }
81 if (nOverwrite < src.size()) {
82 vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.data() + src.size()));
83 }
84 nPos += src.size();
85 }
86 template <typename T>
88 {
89 ::Serialize(*this, obj);
90 return (*this);
91 }
92
93private:
94 std::vector<unsigned char>& vchData;
95 size_t nPos;
96};
97
101{
102private:
103 std::span<const std::byte> m_data;
104
105public:
109 explicit SpanReader(std::span<const unsigned char> data) : m_data{std::as_bytes(data)} {}
110 explicit SpanReader(std::span<const std::byte> data) : m_data{data} {}
111
112 template<typename T>
114 {
115 ::Unserialize(*this, obj);
116 return (*this);
117 }
118
119 size_t size() const { return m_data.size(); }
120 bool empty() const { return m_data.empty(); }
121
122 void read(std::span<std::byte> dst)
123 {
124 if (dst.size() == 0) {
125 return;
126 }
127
128 // Read from the beginning of the buffer
129 if (dst.size() > m_data.size()) {
130 throw std::ios_base::failure("SpanReader::read(): end of data");
131 }
132 memcpy(dst.data(), m_data.data(), dst.size());
133 m_data = m_data.subspan(dst.size());
134 }
135
136 void ignore(size_t n)
137 {
138 m_data = m_data.subspan(n);
139 }
140};
141
148{
149protected:
152 vector_type::size_type m_read_pos{0};
153
154public:
155 typedef vector_type::allocator_type allocator_type;
156 typedef vector_type::size_type size_type;
157 typedef vector_type::difference_type difference_type;
158 typedef vector_type::reference reference;
159 typedef vector_type::const_reference const_reference;
160 typedef vector_type::value_type value_type;
161 typedef vector_type::iterator iterator;
162 typedef vector_type::const_iterator const_iterator;
163 typedef vector_type::reverse_iterator reverse_iterator;
164
165 explicit DataStream() = default;
166 explicit DataStream(std::span<const uint8_t> sp) : DataStream{std::as_bytes(sp)} {}
167 explicit DataStream(std::span<const value_type> sp) : vch(sp.data(), sp.data() + sp.size()) {}
168
169 std::string str() const
170 {
171 return std::string{UCharCast(data()), UCharCast(data() + size())};
172 }
173
174
175 //
176 // Vector subset
177 //
178 const_iterator begin() const { return vch.begin() + m_read_pos; }
179 iterator begin() { return vch.begin() + m_read_pos; }
180 const_iterator end() const { return vch.end(); }
181 iterator end() { return vch.end(); }
182 size_type size() const { return vch.size() - m_read_pos; }
183 bool empty() const { return vch.size() == m_read_pos; }
184 void resize(size_type n, value_type c = value_type{}) { vch.resize(n + m_read_pos, c); }
185 void reserve(size_type n) { vch.reserve(n + m_read_pos); }
186 const_reference operator[](size_type pos) const { return vch[pos + m_read_pos]; }
188 void clear() { vch.clear(); m_read_pos = 0; }
189 value_type* data() { return vch.data() + m_read_pos; }
190 const value_type* data() const { return vch.data() + m_read_pos; }
191
192 inline void Compact()
193 {
194 vch.erase(vch.begin(), vch.begin() + m_read_pos);
195 m_read_pos = 0;
196 }
197
198 bool Rewind(std::optional<size_type> n = std::nullopt)
199 {
200 // Total rewind if no size is passed
201 if (!n) {
202 m_read_pos = 0;
203 return true;
204 }
205 // Rewind by n characters if the buffer hasn't been compacted yet
206 if (*n > m_read_pos)
207 return false;
208 m_read_pos -= *n;
209 return true;
210 }
211
212
213 //
214 // Stream subset
215 //
216 bool eof() const { return size() == 0; }
217 int in_avail() const { return size(); }
218
219 void read(std::span<value_type> dst)
220 {
221 if (dst.size() == 0) return;
222
223 // Read from the beginning of the buffer
224 auto next_read_pos{CheckedAdd(m_read_pos, dst.size())};
225 if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
226 throw std::ios_base::failure("DataStream::read(): end of data");
227 }
228 memcpy(dst.data(), &vch[m_read_pos], dst.size());
229 if (next_read_pos.value() == vch.size()) {
230 m_read_pos = 0;
231 vch.clear();
232 return;
233 }
234 m_read_pos = next_read_pos.value();
235 }
236
237 void ignore(size_t num_ignore)
238 {
239 // Ignore from the beginning of the buffer
240 auto next_read_pos{CheckedAdd(m_read_pos, num_ignore)};
241 if (!next_read_pos.has_value() || next_read_pos.value() > vch.size()) {
242 throw std::ios_base::failure("DataStream::ignore(): end of data");
243 }
244 if (next_read_pos.value() == vch.size()) {
245 m_read_pos = 0;
246 vch.clear();
247 return;
248 }
249 m_read_pos = next_read_pos.value();
250 }
251
252 void write(std::span<const value_type> src)
253 {
254 // Write to the end of the buffer
255 vch.insert(vch.end(), src.begin(), src.end());
256 }
257
258 template<typename T>
260 {
261 ::Serialize(*this, obj);
262 return (*this);
263 }
264
265 template<typename T>
267 {
268 ::Unserialize(*this, obj);
269 return (*this);
270 }
271
277 void Xor(const std::vector<unsigned char>& key)
278 {
280 }
281
283 size_t GetMemoryUsage() const noexcept;
284};
285
286template <typename IStream>
288{
289private:
290 IStream& m_istream;
291
294 uint8_t m_buffer{0};
295
299 int m_offset{8};
300
301public:
302 explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
303
307 uint64_t Read(int nbits) {
308 if (nbits < 0 || nbits > 64) {
309 throw std::out_of_range("nbits must be between 0 and 64");
310 }
311
312 uint64_t data = 0;
313 while (nbits > 0) {
314 if (m_offset == 8) {
315 m_istream >> m_buffer;
316 m_offset = 0;
317 }
318
319 int bits = std::min(8 - m_offset, nbits);
320 data <<= bits;
321 data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
322 m_offset += bits;
323 nbits -= bits;
324 }
325 return data;
326 }
327};
328
329template <typename OStream>
331{
332private:
333 OStream& m_ostream;
334
337 uint8_t m_buffer{0};
338
342 int m_offset{0};
343
344public:
345 explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
346
348 {
349 Flush();
350 }
351
355 void Write(uint64_t data, int nbits) {
356 if (nbits < 0 || nbits > 64) {
357 throw std::out_of_range("nbits must be between 0 and 64");
358 }
359
360 while (nbits > 0) {
361 int bits = std::min(8 - m_offset, nbits);
362 m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
363 m_offset += bits;
364 nbits -= bits;
365
366 if (m_offset == 8) {
367 Flush();
368 }
369 }
370 }
371
375 void Flush() {
376 if (m_offset == 0) {
377 return;
378 }
379
380 m_ostream << m_buffer;
381 m_buffer = 0;
382 m_offset = 0;
383 }
384};
385
393{
394protected:
395 std::FILE* m_file;
396 std::vector<std::byte> m_xor;
397 std::optional<int64_t> m_position;
398
399public:
400 explicit AutoFile(std::FILE* file, std::vector<std::byte> data_xor={});
401
402 ~AutoFile() { fclose(); }
403
404 // Disallow copies
405 AutoFile(const AutoFile&) = delete;
406 AutoFile& operator=(const AutoFile&) = delete;
407
408 bool feof() const { return std::feof(m_file); }
409
410 int fclose()
411 {
412 if (auto rel{release()}) return std::fclose(rel);
413 return 0;
414 }
415
420 std::FILE* release()
421 {
422 std::FILE* ret{m_file};
423 m_file = nullptr;
424 return ret;
425 }
426
429 bool IsNull() const { return m_file == nullptr; }
430
432 void SetXor(std::vector<std::byte> data_xor) { m_xor = data_xor; }
433
435 std::size_t detail_fread(std::span<std::byte> dst);
436
438 void seek(int64_t offset, int origin);
439
441 int64_t tell();
442
444 bool Commit();
445
447 bool Truncate(unsigned size);
448
450 void write_buffer(std::span<std::byte> src);
451
452 //
453 // Stream subset
454 //
455 void read(std::span<std::byte> dst);
456 void ignore(size_t nSize);
457 void write(std::span<const std::byte> src);
458
459 template <typename T>
460 AutoFile& operator<<(const T& obj)
461 {
462 ::Serialize(*this, obj);
463 return *this;
464 }
465
466 template <typename T>
468 {
469 ::Unserialize(*this, obj);
470 return *this;
471 }
472};
473
474using DataBuffer = std::vector<std::byte>;
475
483{
484private:
486 uint64_t nSrcPos{0};
487 uint64_t m_read_pos{0};
488 uint64_t nReadLimit;
489 uint64_t nRewind;
491
493 bool Fill() {
494 unsigned int pos = nSrcPos % vchBuf.size();
495 unsigned int readNow = vchBuf.size() - pos;
496 unsigned int nAvail = vchBuf.size() - (nSrcPos - m_read_pos) - nRewind;
497 if (nAvail < readNow)
498 readNow = nAvail;
499 if (readNow == 0)
500 return false;
501 size_t nBytes{m_src.detail_fread(std::span{vchBuf}.subspan(pos, readNow))};
502 if (nBytes == 0) {
503 throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"};
504 }
505 nSrcPos += nBytes;
506 return true;
507 }
508
514 std::pair<std::byte*, size_t> AdvanceStream(size_t length)
515 {
516 assert(m_read_pos <= nSrcPos);
517 if (m_read_pos + length > nReadLimit) {
518 throw std::ios_base::failure("Attempt to position past buffer limit");
519 }
520 // If there are no bytes available, read from the file.
521 if (m_read_pos == nSrcPos && length > 0) Fill();
522
523 size_t buffer_offset{static_cast<size_t>(m_read_pos % vchBuf.size())};
524 size_t buffer_available{static_cast<size_t>(vchBuf.size() - buffer_offset)};
525 size_t bytes_until_source_pos{static_cast<size_t>(nSrcPos - m_read_pos)};
526 size_t advance{std::min({length, buffer_available, bytes_until_source_pos})};
527 m_read_pos += advance;
528 return std::make_pair(&vchBuf[buffer_offset], advance);
529 }
530
531public:
532 BufferedFile(AutoFile& file LIFETIMEBOUND, uint64_t nBufSize, uint64_t nRewindIn)
533 : m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
534 {
535 if (nRewindIn >= nBufSize)
536 throw std::ios_base::failure("Rewind limit must be less than buffer size");
537 }
538
540 bool eof() const {
541 return m_read_pos == nSrcPos && m_src.feof();
542 }
543
545 void read(std::span<std::byte> dst)
546 {
547 while (dst.size() > 0) {
548 auto [buffer_pointer, length]{AdvanceStream(dst.size())};
549 memcpy(dst.data(), buffer_pointer, length);
550 dst = dst.subspan(length);
551 }
552 }
553
556 void SkipTo(const uint64_t file_pos)
557 {
558 assert(file_pos >= m_read_pos);
559 while (m_read_pos < file_pos) AdvanceStream(file_pos - m_read_pos);
560 }
561
563 uint64_t GetPos() const {
564 return m_read_pos;
565 }
566
568 bool SetPos(uint64_t nPos) {
569 size_t bufsize = vchBuf.size();
570 if (nPos + bufsize < nSrcPos) {
571 // rewinding too far, rewind as far as possible
572 m_read_pos = nSrcPos - bufsize;
573 return false;
574 }
575 if (nPos > nSrcPos) {
576 // can't go this far forward, go as far as possible
577 m_read_pos = nSrcPos;
578 return false;
579 }
580 m_read_pos = nPos;
581 return true;
582 }
583
586 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
587 if (nPos < m_read_pos)
588 return false;
589 nReadLimit = nPos;
590 return true;
591 }
592
593 template<typename T>
595 ::Unserialize(*this, obj);
596 return (*this);
597 }
598
600 void FindByte(std::byte byte)
601 {
602 // For best performance, avoid mod operation within the loop.
603 size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))};
604 while (true) {
605 if (m_read_pos == nSrcPos) {
606 // No more bytes available; read from the file into the buffer,
607 // setting nSrcPos to one beyond the end of the new data.
608 // Throws exception if end-of-file reached.
609 Fill();
610 }
611 const size_t len{std::min<size_t>(vchBuf.size() - buf_offset, nSrcPos - m_read_pos)};
612 const auto it_start{vchBuf.begin() + buf_offset};
613 const auto it_find{std::find(it_start, it_start + len, byte)};
614 const size_t inc{size_t(std::distance(it_start, it_find))};
615 m_read_pos += inc;
616 if (inc < len) break;
617 buf_offset += inc;
618 if (buf_offset >= vchBuf.size()) buf_offset = 0;
619 }
620 }
621};
622
628template <typename S>
630{
633 size_t m_buf_pos;
634
635public:
637 explicit BufferedReader(S&& stream LIFETIMEBOUND, size_t size = 1 << 16)
638 requires std::is_rvalue_reference_v<S&&>
639 : m_src{stream}, m_buf(size), m_buf_pos{size} {}
640
641 void read(std::span<std::byte> dst)
642 {
643 if (const auto available{std::min(dst.size(), m_buf.size() - m_buf_pos)}) {
644 std::copy_n(m_buf.begin() + m_buf_pos, available, dst.begin());
645 m_buf_pos += available;
646 dst = dst.subspan(available);
647 }
648 if (dst.size()) {
649 assert(m_buf_pos == m_buf.size());
650 m_src.read(dst);
651
652 m_buf_pos = 0;
653 m_buf.resize(m_src.detail_fread(m_buf));
654 }
655 }
656
657 template <typename T>
659 {
660 Unserialize(*this, obj);
661 return *this;
662 }
663};
664
670template <typename S>
672{
675 size_t m_buf_pos{0};
676
677public:
678 explicit BufferedWriter(S& stream LIFETIMEBOUND, size_t size = 1 << 16) : m_dst{stream}, m_buf(size) {}
679
680 ~BufferedWriter() { flush(); }
681
682 void flush()
683 {
684 if (m_buf_pos) m_dst.write_buffer(std::span{m_buf}.first(m_buf_pos));
685 m_buf_pos = 0;
686 }
687
688 void write(std::span<const std::byte> src)
689 {
690 while (const auto available{std::min(src.size(), m_buf.size() - m_buf_pos)}) {
691 std::copy_n(src.begin(), available, m_buf.begin() + m_buf_pos);
692 m_buf_pos += available;
693 if (m_buf_pos == m_buf.size()) flush();
694 src = src.subspan(available);
695 }
696 }
697
698 template <typename T>
700 {
701 Serialize(*this, obj);
702 return *this;
703 }
704};
705
706#endif // BITCOIN_STREAMS_H
#define LIFETIMEBOUND
Definition: attributes.h:16
int ret
ArgsManager & args
Definition: bitcoind.cpp:277
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:393
bool feof() const
Definition: streams.h:408
std::FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:420
AutoFile & operator=(const AutoFile &)=delete
std::vector< std::byte > m_xor
Definition: streams.h:396
~AutoFile()
Definition: streams.h:402
std::FILE * m_file
Definition: streams.h:395
AutoFile & operator<<(const T &obj)
Definition: streams.h:460
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:429
AutoFile & operator>>(T &&obj)
Definition: streams.h:467
void SetXor(std::vector< std::byte > data_xor)
Continue with a different XOR key.
Definition: streams.h:432
std::optional< int64_t > m_position
Definition: streams.h:397
int fclose()
Definition: streams.h:410
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:307
BitStreamReader(IStream &istream)
Definition: streams.h:302
IStream & m_istream
Definition: streams.h:290
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:355
OStream & m_ostream
Definition: streams.h:333
BitStreamWriter(OStream &ostream)
Definition: streams.h:345
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Definition: streams.h:375
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
Definition: streams.h:483
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:514
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:489
void read(std::span< std::byte > dst)
read a number of bytes
Definition: streams.h:545
BufferedFile(AutoFile &file LIFETIMEBOUND, uint64_t nBufSize, uint64_t nRewindIn)
Definition: streams.h:532
bool eof() const
check whether we're at the end of the source file
Definition: streams.h:540
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:586
DataBuffer vchBuf
Definition: streams.h:490
BufferedFile & operator>>(T &&obj)
Definition: streams.h:594
uint64_t GetPos() const
return the current reading position
Definition: streams.h:563
uint64_t nReadLimit
up to which position we're allowed to read
Definition: streams.h:488
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Definition: streams.h:556
void FindByte(std::byte byte)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:600
bool Fill()
read data from the source to fill the buffer
Definition: streams.h:493
AutoFile & m_src
Definition: streams.h:485
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:568
Wrapper that buffers reads from an underlying stream.
Definition: streams.h:630
size_t m_buf_pos
Definition: streams.h:633
DataBuffer m_buf
Definition: streams.h:632
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:637
BufferedReader & operator>>(T &&obj)
Definition: streams.h:658
void read(std::span< std::byte > dst)
Definition: streams.h:641
Wrapper that buffers writes to an underlying stream.
Definition: streams.h:672
DataBuffer m_buf
Definition: streams.h:674
void write(std::span< const std::byte > src)
Definition: streams.h:688
void flush()
Definition: streams.h:682
BufferedWriter & operator<<(const T &obj)
Definition: streams.h:699
BufferedWriter(S &stream LIFETIMEBOUND, size_t size=1<< 16)
Definition: streams.h:678
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:148
DataStream & operator<<(const T &obj)
Definition: streams.h:259
bool empty() const
Definition: streams.h:183
vector_type::difference_type difference_type
Definition: streams.h:157
void write(std::span< const value_type > src)
Definition: streams.h:252
size_type size() const
Definition: streams.h:182
DataStream & operator>>(T &&obj)
Definition: streams.h:266
reference operator[](size_type pos)
Definition: streams.h:187
void resize(size_type n, value_type c=value_type{})
Definition: streams.h:184
const_reference operator[](size_type pos) const
Definition: streams.h:186
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:277
void read(std::span< value_type > dst)
Definition: streams.h:219
vector_type::size_type size_type
Definition: streams.h:156
SerializeData vector_type
Definition: streams.h:150
vector_type vch
Definition: streams.h:151
const value_type * data() const
Definition: streams.h:190
vector_type::const_reference const_reference
Definition: streams.h:159
value_type * data()
Definition: streams.h:189
vector_type::const_iterator const_iterator
Definition: streams.h:162
void reserve(size_type n)
Definition: streams.h:185
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:163
DataStream(std::span< const value_type > sp)
Definition: streams.h:167
iterator begin()
Definition: streams.h:179
const_iterator begin() const
Definition: streams.h:178
vector_type::size_type m_read_pos
Definition: streams.h:152
vector_type::iterator iterator
Definition: streams.h:161
vector_type::value_type value_type
Definition: streams.h:160
DataStream(std::span< const uint8_t > sp)
Definition: streams.h:166
bool eof() const
Definition: streams.h:216
std::string str() const
Definition: streams.h:169
void ignore(size_t num_ignore)
Definition: streams.h:237
vector_type::allocator_type allocator_type
Definition: streams.h:155
const_iterator end() const
Definition: streams.h:180
void Compact()
Definition: streams.h:192
void clear()
Definition: streams.h:188
size_t GetMemoryUsage() const noexcept
Compute total memory usage of this object (own memory + any dynamic memory).
Definition: streams.cpp:123
iterator end()
Definition: streams.h:181
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:198
DataStream()=default
vector_type::reference reference
Definition: streams.h:158
int in_avail() const
Definition: streams.h:217
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:101
bool empty() const
Definition: streams.h:120
SpanReader & operator>>(T &&obj)
Definition: streams.h:113
std::span< const std::byte > m_data
Definition: streams.h:103
size_t size() const
Definition: streams.h:119
SpanReader(std::span< const std::byte > data)
Definition: streams.h:110
void read(std::span< std::byte > dst)
Definition: streams.h:122
void ignore(size_t n)
Definition: streams.h:136
SpanReader(std::span< const unsigned char > data)
Definition: streams.h:109
std::vector< unsigned char > & vchData
Definition: streams.h:94
VectorWriter & operator<<(const T &obj)
Definition: streams.h:87
size_t nPos
Definition: streams.h:95
void write(std::span< const std::byte > src)
Definition: streams.h:74
VectorWriter(std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:70
VectorWriter(std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:60
void Xor(std::span< std::byte > write, std::span< const std::byte > key, size_t key_offset=0)
Definition: streams.h:28
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition: overflow.h:26
#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:996
void Unserialize(Stream &, V)=delete
auto MakeByteSpan(const V &v) noexcept
Definition: span.h:84
auto MakeWritableByteSpan(V &&v) noexcept
Definition: span.h:89
unsigned char * UCharCast(char *c)
Definition: span.h:95
std::vector< std::byte > DataBuffer
Definition: streams.h:474
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:49