 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
6 #ifndef BITCOIN_STREAMS_H
7 #define BITCOIN_STREAMS_H
25 template<
typename Stream>
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)
104 if (nOverwrite < src.
size()) {
166 if (dst.
size() == 0) {
172 throw std::ios_base::failure(
"SpanReader::read(): end of data");
215 template <
typename... Args>
252 bool Rewind(std::optional<size_type> n = std::nullopt)
281 if (dst.
size() == 0)
return;
285 if (nReadPosNext >
vch.size()) {
286 throw std::ios_base::failure(
"CDataStream::read(): end of data");
289 if (nReadPosNext ==
vch.size())
302 throw std::ios_base::failure(
"CDataStream::ignore(): nSize negative");
304 unsigned int nReadPosNext =
nReadPos + nSize;
305 if (nReadPosNext >=
vch.size())
307 if (nReadPosNext >
vch.size())
308 throw std::ios_base::failure(
"CDataStream::ignore(): end of data");
322 template<
typename Stream>
351 void Xor(
const std::vector<unsigned char>& key)
353 if (key.size() == 0) {
358 vch[i] ^= std::byte{key[j++]};
370 template <
typename IStream>
392 if (nbits < 0 || nbits > 64) {
393 throw std::out_of_range(
"nbits must be between 0 and 64");
403 int bits = std::min(8 -
m_offset, nbits);
405 data |=
static_cast<uint8_t
>(m_buffer << m_offset) >> (8 - bits);
413 template <
typename OStream>
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");
445 int bits = std::min(8 -
m_offset, nbits);
534 throw std::ios_base::failure(
"CAutoFile::read: file handle is nullptr");
536 throw std::ios_base::failure(feof(
file) ?
"CAutoFile::read: end of file" :
"CAutoFile::read: fread failed");
543 throw std::ios_base::failure(
"CAutoFile::ignore: file handle is nullptr");
544 unsigned char data[4096];
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");
556 throw std::ios_base::failure(
"CAutoFile::write: file handle is nullptr");
558 throw std::ios_base::failure(
"CAutoFile::write: write failed");
567 throw std::ios_base::failure(
"CAutoFile::operator<<: file handle is nullptr");
577 throw std::ios_base::failure(
"CAutoFile::operator>>: file handle is nullptr");
606 unsigned int readNow =
vchBuf.size() - pos;
608 if (nAvail < readNow)
612 size_t nBytes = fread((
void*)&
vchBuf[pos], 1, readNow,
src);
614 throw std::ios_base::failure(feof(
src) ?
"CBufferedFile::Fill: end of file" :
"CBufferedFile::Fill: fread failed");
621 CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn)
624 if (nRewindIn >= nBufSize)
625 throw std::ios_base::failure(
"Rewind limit must be less than buffer size");
658 throw std::ios_base::failure(
"Read attempted past buffer limit");
660 while (dst.
size() > 0) {
664 size_t nNow = dst.
size();
665 if (nNow + pos >
vchBuf.size())
666 nNow =
vchBuf.size() - pos;
682 size_t bufsize =
vchBuf.size();
683 if (nPos + bufsize <
nSrcPos) {
699 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
727 #endif // BITCOIN_STREAMS_H
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
void ignore(size_t nSize)
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)
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.
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
OverrideStream< Stream > & operator>>(T &&obj)
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
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 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
Non-refcounted RAII wrapper for FILE*.
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.
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
uint64_t nSrcPos
how many bytes have been read from source
uint64_t nReadPos
how many bytes have been read from this
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
Minimal stream for reading from an existing byte array by Span.
CAutoFile & operator>>(T &&obj)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
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)
void resize(size_type n, value_type c=value_type{})
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
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)
CAutoFile & operator=(const CAutoFile &)=delete
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.
void read(Span< std::byte > dst)
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
SpanReader(int type, int version, Span< const unsigned char > data)
FILE * release()
Get wrapped FILE* with transfer of ownership.
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)
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)