6 #ifndef BITCOIN_STREAMS_H 7 #define BITCOIN_STREAMS_H 23 template<
typename Stream>
32 OverrideStream(Stream* stream_,
int nType_,
int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
50 void write(
const char* pch,
size_t nSize)
52 stream->write(pch, nSize);
55 void read(
char* pch,
size_t nSize)
57 stream->read(pch, nSize);
62 size_t size()
const {
return stream->size(); }
63 void ignore(
size_t size) {
return stream->ignore(size); }
81 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn) :
nType(nTypeIn),
nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
83 if(nPos > vchData.size())
90 template <
typename... Args>
91 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn, Args&&... args) :
CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
95 void write(
const char* pch,
size_t nSize)
97 assert(nPos <= vchData.size());
98 size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
100 memcpy(vchData.data() + nPos,
reinterpret_cast<const unsigned char*
>(pch), nOverwrite);
102 if (nOverwrite < nSize) {
103 vchData.insert(vchData.end(),
reinterpret_cast<const unsigned char*
>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
136 const std::vector<unsigned char>&
m_data;
147 VectorReader(
int type,
int version,
const std::vector<unsigned char>& data,
size_t pos)
148 : m_type(type), m_version(version), m_data(data), m_pos(pos)
150 if (m_pos > m_data.size()) {
151 throw std::ios_base::failure(
"VectorReader(...): end of data (m_pos > m_data.size())");
159 template <
typename... Args>
160 VectorReader(
int type,
int version,
const std::vector<unsigned char>& data,
size_t pos,
178 size_t size()
const {
return m_data.size() - m_pos; }
179 bool empty()
const {
return m_data.size() == m_pos; }
188 size_t pos_next = m_pos + n;
189 if (pos_next > m_data.size()) {
190 throw std::ios_base::failure(
"VectorReader::read(): end of data");
192 memcpy(dst, m_data.data() + m_pos, n);
225 Init(nTypeIn, nVersionIn);
228 CDataStream(const_iterator pbegin, const_iterator pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
230 Init(nTypeIn, nVersionIn);
233 CDataStream(
const char* pbegin,
const char* pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
235 Init(nTypeIn, nVersionIn);
238 CDataStream(
const vector_type& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
240 Init(nTypeIn, nVersionIn);
243 CDataStream(
const std::vector<char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
245 Init(nTypeIn, nVersionIn);
248 CDataStream(
const std::vector<unsigned char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
250 Init(nTypeIn, nVersionIn);
253 template <
typename... Args>
256 Init(nTypeIn, nVersionIn);
260 void Init(
int nTypeIn,
int nVersionIn)
264 nVersion = nVersionIn;
282 return (std::string(begin(), end()));
289 const_iterator
begin()
const {
return vch.begin() + nReadPos; }
290 iterator
begin() {
return vch.begin() + nReadPos; }
291 const_iterator
end()
const {
return vch.end(); }
292 iterator
end() {
return vch.end(); }
293 size_type
size()
const {
return vch.size() - nReadPos; }
294 bool empty()
const {
return vch.size() == nReadPos; }
295 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
296 void reserve(size_type n) { vch.reserve(n + nReadPos); }
297 const_reference
operator[](size_type pos)
const {
return vch[pos + nReadPos]; }
298 reference
operator[](size_type pos) {
return vch[pos + nReadPos]; }
299 void clear() { vch.clear(); nReadPos = 0; }
300 iterator
insert(iterator
it,
const char x=
char()) {
return vch.insert(it, x); }
301 void insert(iterator it, size_type n,
const char x) { vch.insert(it, n, x); }
302 value_type*
data() {
return vch.data() + nReadPos; }
303 const value_type*
data()
const {
return vch.data() + nReadPos; }
305 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
307 if (last == first)
return;
309 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
312 nReadPos -= (last - first);
313 memcpy(&vch[nReadPos], &first[0], last - first);
316 vch.insert(it, first, last);
319 void insert(iterator it,
const char* first,
const char* last)
321 if (last == first)
return;
323 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
326 nReadPos -= (last - first);
327 memcpy(&vch[nReadPos], &first[0], last - first);
330 vch.insert(it, first, last);
335 if (it == vch.begin() + nReadPos)
338 if (++nReadPos >= vch.size())
342 return vch.erase(vch.begin(), vch.end());
344 return vch.begin() + nReadPos;
347 return vch.erase(it);
350 iterator
erase(iterator first, iterator last)
352 if (first == vch.begin() + nReadPos)
355 if (last == vch.end())
358 return vch.erase(vch.begin(), vch.end());
362 nReadPos = (last - vch.begin());
367 return vch.erase(first, last);
372 vch.erase(vch.begin(), vch.begin() + nReadPos);
398 void read(
char* pch,
size_t nSize)
400 if (nSize == 0)
return;
403 unsigned int nReadPosNext = nReadPos + nSize;
404 if (nReadPosNext > vch.size()) {
405 throw std::ios_base::failure(
"CDataStream::read(): end of data");
407 memcpy(pch, &vch[nReadPos], nSize);
408 if (nReadPosNext == vch.size())
414 nReadPos = nReadPosNext;
421 throw std::ios_base::failure(
"CDataStream::ignore(): nSize negative");
423 unsigned int nReadPosNext = nReadPos + nSize;
424 if (nReadPosNext >= vch.size())
426 if (nReadPosNext > vch.size())
427 throw std::ios_base::failure(
"CDataStream::ignore(): end of data");
432 nReadPos = nReadPosNext;
435 void write(
const char* pch,
size_t nSize)
438 vch.insert(vch.end(), pch, pch + nSize);
441 template<
typename Stream>
446 s.write((
char*)vch.data(), vch.size() *
sizeof(value_type));
466 d.insert(d.end(), begin(), end());
475 void Xor(
const std::vector<unsigned char>& key)
477 if (key.size() == 0) {
481 for (size_type i = 0, j = 0; i !=
size(); i++) {
494 template <
typename IStream>
516 if (nbits < 0 || nbits > 64) {
517 throw std::out_of_range(
"nbits must be between 0 and 64");
523 m_istream >> m_buffer;
527 int bits = std::min(8 - m_offset, nbits);
529 data |=
static_cast<uint8_t
>(m_buffer << m_offset) >> (8 - bits);
537 template <
typename OStream>
563 void Write(uint64_t data,
int nbits) {
564 if (nbits < 0 || nbits > 64) {
565 throw std::out_of_range(
"nbits must be between 0 and 64");
569 int bits = std::min(8 - m_offset, nbits);
570 m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
588 m_ostream << m_buffer;
611 CAutoFile(FILE* filenew,
int nTypeIn,
int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
637 FILE*
release() { FILE* ret = file; file =
nullptr;
return ret; }
643 FILE*
Get()
const {
return file; }
647 bool IsNull()
const {
return (file ==
nullptr); }
655 void read(
char* pch,
size_t nSize)
658 throw std::ios_base::failure(
"CAutoFile::read: file handle is nullptr");
659 if (fread(pch, 1, nSize, file) != nSize)
660 throw std::ios_base::failure(feof(file) ?
"CAutoFile::read: end of file" :
"CAutoFile::read: fread failed");
666 throw std::ios_base::failure(
"CAutoFile::ignore: file handle is nullptr");
667 unsigned char data[4096];
669 size_t nNow = std::min<size_t>(nSize,
sizeof(data));
670 if (fread(data, 1, nNow, file) != nNow)
671 throw std::ios_base::failure(feof(file) ?
"CAutoFile::ignore: end of file" :
"CAutoFile::read: fread failed");
676 void write(
const char* pch,
size_t nSize)
679 throw std::ios_base::failure(
"CAutoFile::write: file handle is nullptr");
680 if (fwrite(pch, 1, nSize, file) != nSize)
681 throw std::ios_base::failure(
"CAutoFile::write: write failed");
689 throw std::ios_base::failure(
"CAutoFile::operator<<: file handle is nullptr");
699 throw std::ios_base::failure(
"CAutoFile::operator>>: file handle is nullptr");
727 unsigned int pos = nSrcPos % vchBuf.size();
728 unsigned int readNow = vchBuf.size() - pos;
729 unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
730 if (nAvail < readNow)
734 size_t nBytes = fread((
void*)&vchBuf[pos], 1, readNow, src);
736 throw std::ios_base::failure(feof(src) ?
"CBufferedFile::Fill: end of file" :
"CBufferedFile::Fill: fread failed");
743 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn) :
744 nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(
std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0)
746 if (nRewindIn >= nBufSize)
747 throw std::ios_base::failure(
"Rewind limit must be less than buffer size");
773 return nReadPos == nSrcPos && feof(src);
777 void read(
char *pch,
size_t nSize) {
778 if (nSize + nReadPos > nReadLimit)
779 throw std::ios_base::failure(
"Read attempted past buffer limit");
781 if (nReadPos == nSrcPos)
783 unsigned int pos = nReadPos % vchBuf.size();
785 if (nNow + pos > vchBuf.size())
786 nNow = vchBuf.size() - pos;
787 if (nNow + nReadPos > nSrcPos)
788 nNow = nSrcPos - nReadPos;
789 memcpy(pch, &vchBuf[pos], nNow);
803 size_t bufsize = vchBuf.size();
804 if (nPos + bufsize < nSrcPos) {
806 nReadPos = nSrcPos - bufsize;
809 if (nPos > nSrcPos) {
820 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
837 if (nReadPos == nSrcPos)
839 if (vchBuf[nReadPos % vchBuf.size()] == ch)
846 #endif // BITCOIN_STREAMS_H
CVectorWriter & operator<<(const T &obj)
void Init(int nTypeIn, int nVersionIn)
CSerializeData vector_type
void ignore(size_t nSize)
uint64_t nReadPos
how many bytes have been read from this
std::deque< CInv >::iterator it
vector_type::iterator iterator
vector_type::allocator_type allocator_type
std::vector< unsigned char > & vchData
void read(char *dst, size_t n)
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
CDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
void write(const char *pch, size_t nSize)
vector_type::size_type size_type
void resize(size_type n, value_type c=0)
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
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.
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)
friend CDataStream operator+(const CDataStream &a, const CDataStream &b)
CDataStream(int nTypeIn, int nVersionIn)
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
iterator insert(iterator it, const char x=char())
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)
disconnectpool queuedTx clear()
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)
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
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(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
CDataStream & operator+=(const CDataStream &b)
CDataStream & operator>>(T &&obj)
void GetAndClear(CSerializeData &d)
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.
const_reference operator[](size_type pos) const
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos)
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
const_iterator end() const
const_iterator begin() const
VectorReader & operator>>(T &obj)
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)
void * memcpy(void *a, const void *b, size_t c)
CBufferedFile & operator>>(T &&obj)
void Unserialize(Stream &s, char &a)
vector_type::difference_type difference_type
const std::vector< unsigned char > & m_data
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, zero_after_free_allocator< char > > CSerializeData
std::vector< char > vchBuf
the buffer
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
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from...
OverrideStream< Stream > & operator>>(T &&obj)
CAutoFile & operator<<(const T &obj)
void insert(iterator it, size_type n, const char x)
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