 |
Bitcoin Core
23.99.0
P2P Digital Currency
|
Go to the documentation of this file.
6 #ifndef BITCOIN_STREAMS_H
7 #define BITCOIN_STREAMS_H
26 template<
typename Stream>
93 template <
typename... Args>
94 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn, Args&&...
args) :
CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
105 if (nOverwrite < src.
size()) {
167 if (dst.
size() == 0) {
173 throw std::ios_base::failure(
"SpanReader::read(): end of data");
216 template <
typename... Args>
253 bool Rewind(std::optional<size_type> n = std::nullopt)
282 if (dst.
size() == 0)
return;
286 if (!next_read_pos.has_value() || next_read_pos.value() >
vch.size()) {
287 throw std::ios_base::failure(
"CDataStream::read(): end of data");
290 if (next_read_pos.value() ==
vch.size()) {
302 if (!next_read_pos.has_value() || next_read_pos.value() >
vch.size()) {
303 throw std::ios_base::failure(
"CDataStream::ignore(): end of data");
305 if (next_read_pos.value() ==
vch.size()) {
319 template<
typename Stream>
348 void Xor(
const std::vector<unsigned char>& key)
350 if (key.size() == 0) {
355 vch[i] ^= std::byte{key[j++]};
367 template <
typename IStream>
389 if (nbits < 0 || nbits > 64) {
390 throw std::out_of_range(
"nbits must be between 0 and 64");
400 int bits = std::min(8 -
m_offset, nbits);
402 data |=
static_cast<uint8_t
>(m_buffer << m_offset) >> (8 - bits);
410 template <
typename OStream>
436 void Write(uint64_t data,
int nbits) {
437 if (nbits < 0 || nbits > 64) {
438 throw std::out_of_range(
"nbits must be between 0 and 64");
442 int bits = std::min(8 -
m_offset, nbits);
520 if (!
file)
throw std::ios_base::failure(
"AutoFile::read: file handle is nullptr");
522 throw std::ios_base::failure(feof(
file) ?
"AutoFile::read: end of file" :
"AutoFile::read: fread failed");
528 if (!
file)
throw std::ios_base::failure(
"AutoFile::ignore: file handle is nullptr");
529 unsigned char data[4096];
531 size_t nNow = std::min<size_t>(nSize,
sizeof(data));
532 if (fread(data, 1, nNow,
file) != nNow)
533 throw std::ios_base::failure(feof(
file) ?
"AutoFile::ignore: end of file" :
"AutoFile::read: fread failed");
540 if (!
file)
throw std::ios_base::failure(
"AutoFile::write: file handle is nullptr");
542 throw std::ios_base::failure(
"AutoFile::write: write failed");
546 template <
typename T>
549 if (!
file)
throw std::ios_base::failure(
"AutoFile::operator<<: file handle is nullptr");
554 template <
typename T>
557 if (!
file)
throw std::ios_base::failure(
"AutoFile::operator>>: file handle is nullptr");
579 throw std::ios_base::failure(
"CAutoFile::operator<<: file handle is nullptr");
589 throw std::ios_base::failure(
"CAutoFile::operator>>: file handle is nullptr");
618 unsigned int readNow =
vchBuf.size() - pos;
620 if (nAvail < readNow)
624 size_t nBytes = fread((
void*)&
vchBuf[pos], 1, readNow,
src);
626 throw std::ios_base::failure(feof(
src) ?
"CBufferedFile::Fill: end of file" :
"CBufferedFile::Fill: fread failed");
633 CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn)
636 if (nRewindIn >= nBufSize)
637 throw std::ios_base::failure(
"Rewind limit must be less than buffer size");
670 throw std::ios_base::failure(
"Read attempted past buffer limit");
672 while (dst.
size() > 0) {
676 size_t nNow = dst.
size();
677 if (nNow + pos >
vchBuf.size())
678 nNow =
vchBuf.size() - pos;
694 size_t bufsize =
vchBuf.size();
695 if (nPos + bufsize <
nSrcPos) {
711 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
739 #endif // BITCOIN_STREAMS_H
void ignore(size_t num_ignore)
CAutoFile & operator<<(const T &obj)
Span< const std::byte > MakeByteSpan(V &&v) noexcept
vector_type::value_type value_type
vector_type::size_type size_type
Non-refcounted RAII wrapper for FILE*.
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
CBufferedFile & operator>>(T &&obj)
void read(Span< std::byte > dst)
AutoFile & operator=(const AutoFile &)=delete
const_iterator begin() const
bool SetPos(uint64_t nPos)
rewind to a given reading position
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
uint8_t m_buffer
Buffered byte read in from the input stream.
OverrideStream< Stream > & operator>>(T &&obj)
void read(Span< std::byte > dst)
vector_type::reference reference
void write(Span< const std::byte > src)
CDataStream(Span< const value_type > sp, int nTypeIn, int nVersionIn)
Span< const unsigned char > m_data
uint64_t m_read_pos
how many bytes have been read from this
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
std::vector< std::byte > vchBuf
the buffer
void write(Span< const std::byte > src)
void Unserialize(Stream &, char)=delete
void write(Span< const value_type > src)
void Serialize(Stream &, char)=delete
OverrideStream(Stream *stream_, int nType_, int nVersion_)
uint64_t nRewind
how many bytes we guarantee to rewind
void read(Span< std::byte > dst)
constexpr std::size_t size() const noexcept
A Span is an object that can refer to a contiguous sequence of objects.
vector_type::const_reference const_reference
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
bool Fill()
read data from the source to fill the buffer
vector_type::allocator_type allocator_type
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.
FILE * release()
Get wrapped FILE* with transfer of ownership.
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
void read(Span< std::byte > dst)
read a number of bytes
unsigned char * UCharCast(char *c)
bool eof() const
check whether we're at the end of the source file
CDataStream(int nTypeIn, int nVersionIn)
const_reference operator[](size_type pos) const
CBufferedFile & operator=(const CBufferedFile &)=delete
std::optional< T > CheckedAdd(const T i, const T j) noexcept
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
uint64_t nSrcPos
how many bytes have been read from source
const_iterator end() const
int m_offset
Number of high order bits in m_buffer already written by previous Write() calls and not yet flushed t...
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
bool Rewind(std::optional< size_type > n=std::nullopt)
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
prevent reading beyond a certain position no argument removes the limit
constexpr C * begin() const noexcept
void ignore(size_t nSize)
Minimal stream for reading from an existing byte array by Span.
CAutoFile & operator>>(T &&obj)
vector_type::size_type m_read_pos
BitStreamWriter(OStream &ostream)
void SerializeMany(Stream &s)
vector_type::reverse_iterator reverse_iterator
void reserve(size_type n)
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
void resize(size_type n, value_type c=value_type{})
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
AutoFile & operator>>(T &&obj)
constexpr bool empty() const noexcept
const value_type * data() const
Span< const std::byte > AsBytes(Span< T > s) noexcept
reference operator[](size_type pos)
CVectorWriter & operator<<(const T &obj)
constexpr C * data() const noexcept
CDataStream & operator>>(T &&obj)
CDataStream(Span< const uint8_t > sp, int type, int version)
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
SpanReader & operator>>(T &&obj)
std::vector< unsigned char > & vchData
vector_type::difference_type difference_type
CDataStream & operator<<(const T &obj)
vector_type::const_iterator const_iterator
void read(Span< value_type > dst)
SerializeData vector_type
Double ended buffer combining vector and stream-like interfaces.
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
AutoFile & operator<<(const T &obj)
SpanReader(int type, int version, Span< const unsigned char > data)
uint64_t nReadLimit
up to which position we're allowed to read
void Serialize(Stream &s) const
BitStreamReader(IStream &istream)
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
void write(Span< const std::byte > src)
uint64_t GetPos() const
return the current reading position
vector_type::iterator iterator
constexpr C * end() const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
void FindByte(uint8_t ch)
search for a given byte in the stream, and remain positioned on it
OverrideStream< Stream > & operator<<(const T &obj)