6 #ifndef BITCOIN_STREAMS_H 7 #define BITCOIN_STREAMS_H 25 template<
typename Stream>
52 void write(
const char* pch,
size_t nSize)
57 void read(
char* pch,
size_t nSize)
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)
97 void write(
const char* pch,
size_t nSize)
100 size_t nOverwrite = std::min(nSize,
vchData.size() -
nPos);
102 memcpy(
vchData.data() +
nPos,
reinterpret_cast<const unsigned char*
>(pch), nOverwrite);
104 if (nOverwrite < nSize) {
105 vchData.insert(
vchData.end(),
reinterpret_cast<const unsigned char*
>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
138 const std::vector<unsigned char>&
m_data;
149 VectorReader(
int type,
int version,
const std::vector<unsigned char>& data,
size_t pos)
153 throw std::ios_base::failure(
"VectorReader(...): end of data (m_pos > m_data.size())");
161 template <
typename... Args>
162 VectorReader(
int type,
int version,
const std::vector<unsigned char>& data,
size_t pos,
190 size_t pos_next =
m_pos + n;
191 if (pos_next >
m_data.size()) {
192 throw std::ios_base::failure(
"VectorReader::read(): end of data");
234 template <
typename... Args>
244 return (std::string(
begin(),
end()));
267 void insert(
iterator it, std::vector<uint8_t>::const_iterator first, std::vector<uint8_t>::const_iterator last)
269 if (last == first)
return;
278 vch.insert(it, first, last);
283 if (last == first)
return;
292 vch.insert(it, first, last);
304 return vch.erase(
vch.begin(),
vch.end());
309 return vch.erase(it);
317 if (last ==
vch.end())
320 return vch.erase(
vch.begin(),
vch.end());
329 return vch.erase(first, last);
338 bool Rewind(std::optional<size_type> n = std::nullopt)
365 void read(
char* pch,
size_t nSize)
367 if (nSize == 0)
return;
370 unsigned int nReadPosNext =
nReadPos + nSize;
371 if (nReadPosNext >
vch.size()) {
372 throw std::ios_base::failure(
"CDataStream::read(): end of data");
375 if (nReadPosNext ==
vch.size())
388 throw std::ios_base::failure(
"CDataStream::ignore(): nSize negative");
390 unsigned int nReadPosNext =
nReadPos + nSize;
391 if (nReadPosNext >=
vch.size())
393 if (nReadPosNext >
vch.size())
394 throw std::ios_base::failure(
"CDataStream::ignore(): end of data");
402 void write(
const char* pch,
size_t nSize)
405 vch.insert(
vch.end(), pch, pch + nSize);
408 template<
typename Stream>
437 void Xor(
const std::vector<unsigned char>& key)
439 if (key.size() == 0) {
456 template <
typename IStream>
478 if (nbits < 0 || nbits > 64) {
479 throw std::out_of_range(
"nbits must be between 0 and 64");
489 int bits = std::min(8 -
m_offset, nbits);
491 data |=
static_cast<uint8_t
>(m_buffer << m_offset) >> (8 - bits);
499 template <
typename OStream>
525 void Write(uint64_t data,
int nbits) {
526 if (nbits < 0 || nbits > 64) {
527 throw std::out_of_range(
"nbits must be between 0 and 64");
531 int bits = std::min(8 -
m_offset, nbits);
617 void read(
char* pch,
size_t nSize)
620 throw std::ios_base::failure(
"CAutoFile::read: file handle is nullptr");
621 if (fread(pch, 1, nSize,
file) != nSize)
622 throw std::ios_base::failure(feof(
file) ?
"CAutoFile::read: end of file" :
"CAutoFile::read: fread failed");
628 throw std::ios_base::failure(
"CAutoFile::ignore: file handle is nullptr");
629 unsigned char data[4096];
631 size_t nNow = std::min<size_t>(nSize,
sizeof(data));
632 if (fread(data, 1, nNow,
file) != nNow)
633 throw std::ios_base::failure(feof(
file) ?
"CAutoFile::ignore: end of file" :
"CAutoFile::read: fread failed");
638 void write(
const char* pch,
size_t nSize)
641 throw std::ios_base::failure(
"CAutoFile::write: file handle is nullptr");
642 if (fwrite(pch, 1, nSize,
file) != nSize)
643 throw std::ios_base::failure(
"CAutoFile::write: write failed");
651 throw std::ios_base::failure(
"CAutoFile::operator<<: file handle is nullptr");
661 throw std::ios_base::failure(
"CAutoFile::operator>>: file handle is nullptr");
690 unsigned int readNow =
vchBuf.size() - pos;
692 if (nAvail < readNow)
696 size_t nBytes = fread((
void*)&
vchBuf[pos], 1, readNow,
src);
698 throw std::ios_base::failure(feof(
src) ?
"CBufferedFile::Fill: end of file" :
"CBufferedFile::Fill: fread failed");
705 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn) :
708 if (nRewindIn >= nBufSize)
709 throw std::ios_base::failure(
"Rewind limit must be less than buffer size");
739 void read(
char *pch,
size_t nSize) {
741 throw std::ios_base::failure(
"Read attempted past buffer limit");
747 if (nNow + pos >
vchBuf.size())
748 nNow =
vchBuf.size() - pos;
751 memcpy(pch, &
vchBuf[pos], nNow);
765 size_t bufsize =
vchBuf.size();
766 if (nPos + bufsize <
nSrcPos) {
782 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
808 #endif // BITCOIN_STREAMS_H
CVectorWriter & operator<<(const T &obj)
void ignore(size_t nSize)
uint64_t nReadPos
how many bytes have been read from this
vector_type::iterator iterator
vector_type::allocator_type allocator_type
std::vector< uint8_t, zero_after_free_allocator< uint8_t > > SerializeData
Byte-vector that clears its contents before deletion.
std::vector< unsigned char > & vchData
void read(char *dst, size_t n)
void write(const char *pch, size_t nSize)
vector_type::size_type size_type
CDataStream(Span< const uint8_t > sp, int nTypeIn, int nVersionIn)
void resize(size_type n, value_type c=0)
void Serialize(Stream &s) const
vector_type::reference reference
vector_type::value_type value_type
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
void insert(iterator it, std::vector< uint8_t >::const_iterator first, std::vector< uint8_t >::const_iterator last)
OverrideStream< Stream > & operator<<(const T &obj)
Double ended buffer combining vector and stream-like interfaces.
void write(const char *pch, size_t nSize)
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
vector_type::reverse_iterator reverse_iterator
iterator erase(iterator it)
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
void UnserializeMany(Stream &s)
CDataStream(int nTypeIn, int nVersionIn)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
FILE * release()
Get wrapped FILE* with transfer of ownership.
void write(const char *pch, size_t nSize)
void Serialize(Stream &s, char a)
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos, Args &&... args)
(other params same as above)
uint8_t m_buffer
Buffered byte read in from the input stream.
void read(char *pch, size_t nSize)
vector_type::const_reference const_reference
void read(char *pch, size_t nSize)
CAutoFile & operator>>(T &&obj)
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
uint64_t GetPos() const
return the current reading position
BitStreamReader(IStream &istream)
uint64_t nReadLimit
up to which position we're allowed to read
vector_type::const_iterator const_iterator
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Minimal stream for reading from an existing vector by reference.
const value_type * data() const
CDataStream & operator>>(T &&obj)
VectorReader & operator>>(T &&obj)
void write(const char *pch, size_t nSize)
void read(char *pch, size_t nSize)
void read(char *pch, size_t nSize)
read a number of bytes
reference operator[](size_type pos)
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
bool SetPos(uint64_t nPos)
rewind to a given reading position
OverrideStream(Stream *stream_, int nType_, int nVersion_)
void SerializeMany(Stream &s)
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
bool Rewind(std::optional< size_type > n=std::nullopt)
const_reference operator[](size_type pos) const
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos)
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
SerializeData vector_type
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
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...
const_iterator begin() const
CDataStream & operator<<(const T &obj)
void FindByte(char ch)
search for a given byte in the stream, and remain positioned on it
void reserve(size_type n)
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
CBufferedFile & operator>>(T &&obj)
void Unserialize(Stream &s, char &a)
vector_type::difference_type difference_type
CAutoFile & operator=(const CAutoFile &)=delete
const std::vector< unsigned char > & m_data
void insert(iterator it, size_type n, const uint8_t x)
void insert(iterator it, const char *first, const char *last)
uint64_t nSrcPos
how many bytes have been read from source
bool Fill()
read data from the source to fill the buffer
std::vector< char > vchBuf
the buffer
A Span is an object that can refer to a contiguous sequence of objects.
iterator erase(iterator first, iterator last)
uint64_t nRewind
how many bytes we guarantee to rewind
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
prevent reading beyond a certain position no argument removes the limit
iterator insert(iterator it, const uint8_t x)
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from...
OverrideStream< Stream > & operator>>(T &&obj)
CBufferedFile & operator=(const CBufferedFile &)=delete
CAutoFile & operator<<(const T &obj)
Non-refcounted RAII wrapper for FILE*.
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
BitStreamWriter(OStream &ostream)
bool eof() const
check whether we're at the end of the source file