6#ifndef BITCOIN_STREAMS_H
7#define BITCOIN_STREAMS_H
49 template <
typename... Args>
50 VectorWriter(std::vector<uint8_t> &vchDataIn,
size_t nPosIn, Args &&...args)
60 if (nOverwrite < src.
size()) {
98 if (dst.
size() == 0) {
104 throw std::ios_base::failure(
"SpanReader::read(): end of data");
169 std::vector<value_type>::const_iterator last) {
181 vch.insert(it, first, last);
199 vch.insert(it, first, last);
210 return vch.erase(
vch.begin(),
vch.end());
214 return vch.erase(it);
221 if (last ==
vch.end()) {
223 return vch.erase(
vch.begin(),
vch.end());
229 return vch.erase(first, last);
237 bool Rewind(std::optional<size_type> n = std::nullopt) {
258 if (dst.
size() == 0) {
264 if (!next_read_pos.has_value() || next_read_pos.value() >
vch.size()) {
265 throw std::ios_base::failure(
"DataStream::read(): end of data");
268 if (next_read_pos.value() ==
vch.size()) {
279 if (!next_read_pos.has_value() || next_read_pos.value() >
vch.size()) {
280 throw std::ios_base::failure(
"DataStream::ignore(): end of data");
282 if (next_read_pos.value() ==
vch.size()) {
310 void Xor(
const std::vector<uint8_t> &key) {
311 if (key.size() == 0) {
316 vch[i] ^= std::byte{key[j++]};
322 if (j == key.size()) j = 0;
348 if (nbits < 0 || nbits > 64) {
349 throw std::out_of_range(
"nbits must be between 0 and 64");
359 int bits = std::min(8 -
m_offset, nbits);
361 data |=
static_cast<uint8_t
>(m_buffer << m_offset) >> (8 - bits);
391 void Write(uint64_t data,
int nbits) {
392 if (nbits < 0 || nbits > 64) {
393 throw std::out_of_range(
"nbits must be between 0 and 64");
397 int bits = std::min(8 -
m_offset, nbits);
447 return std::fclose(rel);
481 void ignore(
size_t nSize);
519 unsigned int readNow =
vchBuf.size() - pos;
521 if (nAvail < readNow) {
529 throw std::ios_base::failure{
530 m_src.
feof() ?
"BufferedFile::Fill: end of file"
531 :
"BufferedFile::Fill: fread failed"};
545 throw std::ios_base::failure(
546 "Attempt to position past buffer limit");
554 size_t buffer_available{
555 static_cast<size_t>(
vchBuf.size() - buffer_offset)};
556 size_t bytes_until_source_pos{
559 std::min({length, buffer_available, bytes_until_source_pos})};
561 return std::make_pair(&
vchBuf[buffer_offset], advance);
569 if (nRewindIn >= nBufSize) {
570 throw std::ios_base::failure(
571 "Rewind limit must be less than buffer size");
580 while (dst.
size() > 0) {
582 memcpy(dst.
data(), buffer_pointer, length);
601 size_t bufsize =
vchBuf.size();
602 if (nPos + bufsize <
nSrcPos) {
618 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
642 const size_t len{std::min<size_t>(
vchBuf.size() - buf_offset,
644 const auto it_start{
vchBuf.begin() + buf_offset};
645 const auto it_find{std::find(it_start, it_start + len,
byte)};
646 const size_t inc{size_t(std::distance(it_start, it_find))};
652 if (buf_offset >=
vchBuf.size()) {
Non-refcounted RAII wrapper for FILE*.
std::FILE * release()
Get wrapped FILE* with transfer of ownership.
void ignore(size_t nSize)
AutoFile & operator=(const AutoFile &)=delete
void read(Span< std::byte > dst)
AutoFile & operator<<(const T &obj)
AutoFile(const AutoFile &)=delete
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
std::FILE * Get() const
Get wrapped FILE* without transfer of ownership.
AutoFile & operator>>(T &&obj)
std::size_t detail_fread(Span< std::byte > dst)
Implementation detail, only used internally.
void write(Span< const std::byte > src)
AutoFile(std::FILE *file)
uint8_t m_buffer
Buffered byte read in from the input stream.
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
BitStreamReader(IStream &istream)
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
BitStreamWriter(OStream &ostream)
int m_offset
Number of high order bits in m_buffer already written by previous Write() calls and not yet flushed t...
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
std::vector< std::byte > vchBuf
the buffer
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...
uint64_t nRewind
how many bytes we guarantee to rewind
bool eof() const
check whether we're at the end of the source file
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
Prevent reading beyond a certain position.
uint64_t m_read_pos
how many bytes have been read from this
BufferedFile & operator>>(T &&obj)
uint64_t GetPos() const
return the current reading position
uint64_t nReadLimit
up to which position we're allowed to read
void read(Span< std::byte > dst)
read a number of bytes
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
uint64_t nSrcPos
how many bytes have been read from source
void FindByte(std::byte byte)
search for a given byte in the stream, and remain positioned on it
BufferedFile(AutoFile &file, uint64_t nBufSize, uint64_t nRewindIn)
bool Fill()
read data from the source to fill the buffer
bool SetPos(uint64_t nPos)
rewind to a given reading position
Double ended buffer combining vector and stream-like interfaces.
void write(Span< const value_type > src)
DataStream & operator<<(const T &obj)
vector_type::difference_type difference_type
DataStream & operator>>(T &&obj)
reference operator[](size_type pos)
void Xor(const std::vector< uint8_t > &key)
XOR the contents of this stream with a certain key.
void resize(size_type n, value_type c=value_type{})
void insert(iterator it, const value_type *first, const value_type *last)
const_reference operator[](size_type pos) const
void insert(iterator it, std::vector< value_type >::const_iterator first, std::vector< value_type >::const_iterator last)
DataStream(Span< const value_type > sp)
vector_type::size_type size_type
iterator erase(iterator it)
SerializeData vector_type
const value_type * data() const
vector_type::const_reference const_reference
vector_type::const_iterator const_iterator
DataStream(Span< const uint8_t > sp)
void reserve(size_type n)
vector_type::reverse_iterator reverse_iterator
const_iterator begin() const
void read(Span< value_type > dst)
vector_type::size_type m_read_pos
vector_type::iterator iterator
vector_type::value_type value_type
void ignore(size_t num_ignore)
vector_type::allocator_type allocator_type
const_iterator end() const
bool Rewind(std::optional< size_type > n=std::nullopt)
vector_type::reference reference
iterator erase(iterator first, iterator last)
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
constexpr C * data() const noexcept
constexpr C * begin() const noexcept
constexpr bool empty() const noexcept
constexpr C * end() const noexcept
Minimal stream for reading from an existing byte array by Span.
Span< const uint8_t > m_data
SpanReader(Span< const uint8_t > data)
SpanReader & operator>>(T &obj)
void read(Span< std::byte > dst)
Minimal stream for overwriting and/or appending to an existing byte vector.
VectorWriter(std::vector< uint8_t > &vchDataIn, size_t nPosIn, Args &&...args)
(other params same as above)
VectorWriter & operator<<(const T &obj)
void write(Span< const std::byte > src)
VectorWriter(std::vector< uint8_t > &vchDataIn, size_t nPosIn)
std::vector< uint8_t > & vchData
Implement std::hash so RCUPtr can be used as a key for maps or sets.
std::optional< T > CheckedAdd(const T i, const T j) noexcept
void Serialize(Stream &, V)=delete
void Unserialize(Stream &, V)=delete
void SerializeMany(Stream &s, const Args &...args)
uint8_t * UCharCast(char *c)
Span< const std::byte > AsBytes(Span< T > s) noexcept
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.