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(); }
80 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn) :
nType(nTypeIn),
nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
82 if(nPos > vchData.size())
89 template <
typename... Args>
90 CVectorWriter(
int nTypeIn,
int nVersionIn, std::vector<unsigned char>& vchDataIn,
size_t nPosIn, Args&&... args) :
CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
94 void write(
const char* pch,
size_t nSize)
96 assert(nPos <= vchData.size());
97 size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
99 memcpy(vchData.data() + nPos,
reinterpret_cast<const unsigned char*
>(pch), nOverwrite);
101 if (nOverwrite < nSize) {
102 vchData.insert(vchData.end(),
reinterpret_cast<const unsigned char*
>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
135 const std::vector<unsigned char>&
m_data;
146 VectorReader(
int type,
int version,
const std::vector<unsigned char>& data,
size_t pos)
147 : m_type(type), m_version(version), m_data(data), m_pos(pos)
149 if (m_pos > m_data.size()) {
150 throw std::ios_base::failure(
"VectorReader(...): end of data (m_pos > m_data.size())");
158 template <
typename... Args>
159 VectorReader(
int type,
int version,
const std::vector<unsigned char>& data,
size_t pos,
177 size_t size()
const {
return m_data.size() - m_pos; }
178 bool empty()
const {
return m_data.size() == m_pos; }
187 size_t pos_next = m_pos + n;
188 if (pos_next > m_data.size()) {
189 throw std::ios_base::failure(
"VectorReader::read(): end of data");
191 memcpy(dst, m_data.data() + m_pos, n);
224 Init(nTypeIn, nVersionIn);
227 CDataStream(const_iterator pbegin, const_iterator pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
229 Init(nTypeIn, nVersionIn);
232 CDataStream(
const char* pbegin,
const char* pend,
int nTypeIn,
int nVersionIn) : vch(pbegin, pend)
234 Init(nTypeIn, nVersionIn);
237 CDataStream(
const vector_type& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
239 Init(nTypeIn, nVersionIn);
242 CDataStream(
const std::vector<char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
244 Init(nTypeIn, nVersionIn);
247 CDataStream(
const std::vector<unsigned char>& vchIn,
int nTypeIn,
int nVersionIn) : vch(vchIn.begin(), vchIn.end())
249 Init(nTypeIn, nVersionIn);
252 template <
typename... Args>
255 Init(nTypeIn, nVersionIn);
259 void Init(
int nTypeIn,
int nVersionIn)
263 nVersion = nVersionIn;
281 return (std::string(begin(), end()));
288 const_iterator
begin()
const {
return vch.begin() + nReadPos; }
289 iterator
begin() {
return vch.begin() + nReadPos; }
290 const_iterator
end()
const {
return vch.end(); }
291 iterator
end() {
return vch.end(); }
292 size_type
size()
const {
return vch.size() - nReadPos; }
293 bool empty()
const {
return vch.size() == nReadPos; }
294 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
295 void reserve(size_type n) { vch.reserve(n + nReadPos); }
296 const_reference
operator[](size_type pos)
const {
return vch[pos + nReadPos]; }
297 reference
operator[](size_type pos) {
return vch[pos + nReadPos]; }
298 void clear() { vch.clear(); nReadPos = 0; }
299 iterator
insert(iterator
it,
const char x=
char()) {
return vch.insert(it, x); }
300 void insert(iterator it, size_type n,
const char x) { vch.insert(it, n, x); }
301 value_type*
data() {
return vch.data() + nReadPos; }
302 const value_type*
data()
const {
return vch.data() + nReadPos; }
304 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
306 if (last == first)
return;
307 assert(last - first > 0);
308 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
311 nReadPos -= (last - first);
312 memcpy(&vch[nReadPos], &first[0], last - first);
315 vch.insert(it, first, last);
318 void insert(iterator it,
const char* first,
const char* last)
320 if (last == first)
return;
321 assert(last - first > 0);
322 if (it == vch.begin() + nReadPos && (
unsigned int)(last - first) <= nReadPos)
325 nReadPos -= (last - first);
326 memcpy(&vch[nReadPos], &first[0], last - first);
329 vch.insert(it, first, last);
334 if (it == vch.begin() + nReadPos)
337 if (++nReadPos >= vch.size())
341 return vch.erase(vch.begin(), vch.end());
343 return vch.begin() + nReadPos;
346 return vch.erase(it);
349 iterator
erase(iterator first, iterator last)
351 if (first == vch.begin() + nReadPos)
354 if (last == vch.end())
357 return vch.erase(vch.begin(), vch.end());
361 nReadPos = (last - vch.begin());
366 return vch.erase(first, last);
371 vch.erase(vch.begin(), vch.begin() + nReadPos);
397 void read(
char* pch,
size_t nSize)
399 if (nSize == 0)
return;
402 unsigned int nReadPosNext = nReadPos + nSize;
403 if (nReadPosNext > vch.size()) {
404 throw std::ios_base::failure(
"CDataStream::read(): end of data");
406 memcpy(pch, &vch[nReadPos], nSize);
407 if (nReadPosNext == vch.size())
413 nReadPos = nReadPosNext;
420 throw std::ios_base::failure(
"CDataStream::ignore(): nSize negative");
422 unsigned int nReadPosNext = nReadPos + nSize;
423 if (nReadPosNext >= vch.size())
425 if (nReadPosNext > vch.size())
426 throw std::ios_base::failure(
"CDataStream::ignore(): end of data");
431 nReadPos = nReadPosNext;
434 void write(
const char* pch,
size_t nSize)
437 vch.insert(vch.end(), pch, pch + nSize);
440 template<
typename Stream>
445 s.write((
char*)vch.data(), vch.size() *
sizeof(value_type));
465 d.insert(d.end(), begin(), end());
474 void Xor(
const std::vector<unsigned char>& key)
476 if (key.size() == 0) {
480 for (size_type i = 0, j = 0; i !=
size(); i++) {
493 template <
typename IStream>
515 if (nbits < 0 || nbits > 64) {
516 throw std::out_of_range(
"nbits must be between 0 and 64");
522 m_istream >> m_buffer;
526 int bits = std::min(8 - m_offset, nbits);
528 data |=
static_cast<uint8_t
>(m_buffer << m_offset) >> (8 - bits);
536 template <
typename OStream>
562 void Write(uint64_t data,
int nbits) {
563 if (nbits < 0 || nbits > 64) {
564 throw std::out_of_range(
"nbits must be between 0 and 64");
568 int bits = std::min(8 - m_offset, nbits);
569 m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
587 m_ostream << m_buffer;
610 CAutoFile(FILE* filenew,
int nTypeIn,
int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
636 FILE*
release() { FILE* ret = file; file =
nullptr;
return ret; }
642 FILE*
Get()
const {
return file; }
646 bool IsNull()
const {
return (file ==
nullptr); }
654 void read(
char* pch,
size_t nSize)
657 throw std::ios_base::failure(
"CAutoFile::read: file handle is nullptr");
658 if (fread(pch, 1, nSize, file) != nSize)
659 throw std::ios_base::failure(feof(file) ?
"CAutoFile::read: end of file" :
"CAutoFile::read: fread failed");
665 throw std::ios_base::failure(
"CAutoFile::ignore: file handle is nullptr");
666 unsigned char data[4096];
668 size_t nNow = std::min<size_t>(nSize,
sizeof(data));
669 if (fread(data, 1, nNow, file) != nNow)
670 throw std::ios_base::failure(feof(file) ?
"CAutoFile::ignore: end of file" :
"CAutoFile::read: fread failed");
675 void write(
const char* pch,
size_t nSize)
678 throw std::ios_base::failure(
"CAutoFile::write: file handle is nullptr");
679 if (fwrite(pch, 1, nSize, file) != nSize)
680 throw std::ios_base::failure(
"CAutoFile::write: write failed");
688 throw std::ios_base::failure(
"CAutoFile::operator<<: file handle is nullptr");
698 throw std::ios_base::failure(
"CAutoFile::operator>>: file handle is nullptr");
726 unsigned int pos = nSrcPos % vchBuf.size();
727 unsigned int readNow = vchBuf.size() - pos;
728 unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
729 if (nAvail < readNow)
733 size_t nBytes = fread((
void*)&vchBuf[pos], 1, readNow, src);
735 throw std::ios_base::failure(feof(src) ?
"CBufferedFile::Fill: end of file" :
"CBufferedFile::Fill: fread failed");
742 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn,
int nTypeIn,
int nVersionIn) :
743 nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(
std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0)
745 if (nRewindIn >= nBufSize)
746 throw std::ios_base::failure(
"Rewind limit must be less than buffer size");
772 return nReadPos == nSrcPos && feof(src);
776 void read(
char *pch,
size_t nSize) {
777 if (nSize + nReadPos > nReadLimit)
778 throw std::ios_base::failure(
"Read attempted past buffer limit");
780 if (nReadPos == nSrcPos)
782 unsigned int pos = nReadPos % vchBuf.size();
784 if (nNow + pos > vchBuf.size())
785 nNow = vchBuf.size() - pos;
786 if (nNow + nReadPos > nSrcPos)
787 nNow = nSrcPos - nReadPos;
788 memcpy(pch, &vchBuf[pos], nNow);
802 size_t bufsize = vchBuf.size();
803 if (nPos + bufsize < nSrcPos) {
805 nReadPos = nSrcPos - bufsize;
808 if (nPos > nSrcPos) {
818 long nLongPos = nPos;
819 if (nPos != (uint64_t)nLongPos)
821 if (fseek(src, nLongPos, SEEK_SET))
823 nLongPos = ftell(src);
831 bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
848 if (nReadPos == nSrcPos)
850 if (vchBuf[nReadPos % vchBuf.size()] == ch)
857 #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
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