Bitcoin Core  0.19.99
P2P Digital Currency
streams.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_STREAMS_H
7 #define BITCOIN_STREAMS_H
8 
10 #include <serialize.h>
11 
12 #include <algorithm>
13 #include <assert.h>
14 #include <ios>
15 #include <limits>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <string>
19 #include <string.h>
20 #include <utility>
21 #include <vector>
22 
23 template<typename Stream>
25 {
26  Stream* stream;
27 
28  const int nType;
29  const int nVersion;
30 
31 public:
32  OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
33 
34  template<typename T>
36  {
37  // Serialize to this stream
38  ::Serialize(*this, obj);
39  return (*this);
40  }
41 
42  template<typename T>
44  {
45  // Unserialize from this stream
46  ::Unserialize(*this, obj);
47  return (*this);
48  }
49 
50  void write(const char* pch, size_t nSize)
51  {
52  stream->write(pch, nSize);
53  }
54 
55  void read(char* pch, size_t nSize)
56  {
57  stream->read(pch, nSize);
58  }
59 
60  int GetVersion() const { return nVersion; }
61  int GetType() const { return nType; }
62  size_t size() const { return stream->size(); }
63 };
64 
65 /* Minimal stream for overwriting and/or appending to an existing byte vector
66  *
67  * The referenced vector will grow as necessary
68  */
70 {
71  public:
72 
73 /*
74  * @param[in] nTypeIn Serialization Type
75  * @param[in] nVersionIn Serialization Version (including any flags)
76  * @param[in] vchDataIn Referenced byte vector to overwrite/append
77  * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
78  * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
79 */
80  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
81  {
82  if(nPos > vchData.size())
83  vchData.resize(nPos);
84  }
85 /*
86  * (other params same as above)
87  * @param[in] args A list of items to serialize starting at nPosIn.
88 */
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)
91  {
92  ::SerializeMany(*this, std::forward<Args>(args)...);
93  }
94  void write(const char* pch, size_t nSize)
95  {
96  assert(nPos <= vchData.size());
97  size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
98  if (nOverwrite) {
99  memcpy(vchData.data() + nPos, reinterpret_cast<const unsigned char*>(pch), nOverwrite);
100  }
101  if (nOverwrite < nSize) {
102  vchData.insert(vchData.end(), reinterpret_cast<const unsigned char*>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
103  }
104  nPos += nSize;
105  }
106  template<typename T>
107  CVectorWriter& operator<<(const T& obj)
108  {
109  // Serialize to this stream
110  ::Serialize(*this, obj);
111  return (*this);
112  }
113  int GetVersion() const
114  {
115  return nVersion;
116  }
117  int GetType() const
118  {
119  return nType;
120  }
121 private:
122  const int nType;
123  const int nVersion;
124  std::vector<unsigned char>& vchData;
125  size_t nPos;
126 };
127 
131 {
132 private:
133  const int m_type;
134  const int m_version;
135  const std::vector<unsigned char>& m_data;
136  size_t m_pos = 0;
137 
138 public:
139 
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)
148  {
149  if (m_pos > m_data.size()) {
150  throw std::ios_base::failure("VectorReader(...): end of data (m_pos > m_data.size())");
151  }
152  }
153 
158  template <typename... Args>
159  VectorReader(int type, int version, const std::vector<unsigned char>& data, size_t pos,
160  Args&&... args)
161  : VectorReader(type, version, data, pos)
162  {
163  ::UnserializeMany(*this, std::forward<Args>(args)...);
164  }
165 
166  template<typename T>
168  {
169  // Unserialize from this stream
170  ::Unserialize(*this, obj);
171  return (*this);
172  }
173 
174  int GetVersion() const { return m_version; }
175  int GetType() const { return m_type; }
176 
177  size_t size() const { return m_data.size() - m_pos; }
178  bool empty() const { return m_data.size() == m_pos; }
179 
180  void read(char* dst, size_t n)
181  {
182  if (n == 0) {
183  return;
184  }
185 
186  // Read from the beginning of the buffer
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");
190  }
191  memcpy(dst, m_data.data() + m_pos, n);
192  m_pos = pos_next;
193  }
194 };
195 
202 {
203 protected:
205  vector_type vch;
206  unsigned int nReadPos;
207 
208  int nType;
209  int nVersion;
210 public:
211 
212  typedef vector_type::allocator_type allocator_type;
213  typedef vector_type::size_type size_type;
214  typedef vector_type::difference_type difference_type;
215  typedef vector_type::reference reference;
216  typedef vector_type::const_reference const_reference;
217  typedef vector_type::value_type value_type;
218  typedef vector_type::iterator iterator;
219  typedef vector_type::const_iterator const_iterator;
220  typedef vector_type::reverse_iterator reverse_iterator;
221 
222  explicit CDataStream(int nTypeIn, int nVersionIn)
223  {
224  Init(nTypeIn, nVersionIn);
225  }
226 
227  CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
228  {
229  Init(nTypeIn, nVersionIn);
230  }
231 
232  CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
233  {
234  Init(nTypeIn, nVersionIn);
235  }
236 
237  CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
238  {
239  Init(nTypeIn, nVersionIn);
240  }
241 
242  CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
243  {
244  Init(nTypeIn, nVersionIn);
245  }
246 
247  CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
248  {
249  Init(nTypeIn, nVersionIn);
250  }
251 
252  template <typename... Args>
253  CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
254  {
255  Init(nTypeIn, nVersionIn);
256  ::SerializeMany(*this, std::forward<Args>(args)...);
257  }
258 
259  void Init(int nTypeIn, int nVersionIn)
260  {
261  nReadPos = 0;
262  nType = nTypeIn;
263  nVersion = nVersionIn;
264  }
265 
267  {
268  vch.insert(vch.end(), b.begin(), b.end());
269  return *this;
270  }
271 
272  friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
273  {
274  CDataStream ret = a;
275  ret += b;
276  return (ret);
277  }
278 
279  std::string str() const
280  {
281  return (std::string(begin(), end()));
282  }
283 
284 
285  //
286  // Vector subset
287  //
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; }
303 
304  void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
305  {
306  if (last == first) return;
307  assert(last - first > 0);
308  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
309  {
310  // special case for inserting at the front when there's room
311  nReadPos -= (last - first);
312  memcpy(&vch[nReadPos], &first[0], last - first);
313  }
314  else
315  vch.insert(it, first, last);
316  }
317 
318  void insert(iterator it, const char* first, const char* last)
319  {
320  if (last == first) return;
321  assert(last - first > 0);
322  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
323  {
324  // special case for inserting at the front when there's room
325  nReadPos -= (last - first);
326  memcpy(&vch[nReadPos], &first[0], last - first);
327  }
328  else
329  vch.insert(it, first, last);
330  }
331 
332  iterator erase(iterator it)
333  {
334  if (it == vch.begin() + nReadPos)
335  {
336  // special case for erasing from the front
337  if (++nReadPos >= vch.size())
338  {
339  // whenever we reach the end, we take the opportunity to clear the buffer
340  nReadPos = 0;
341  return vch.erase(vch.begin(), vch.end());
342  }
343  return vch.begin() + nReadPos;
344  }
345  else
346  return vch.erase(it);
347  }
348 
349  iterator erase(iterator first, iterator last)
350  {
351  if (first == vch.begin() + nReadPos)
352  {
353  // special case for erasing from the front
354  if (last == vch.end())
355  {
356  nReadPos = 0;
357  return vch.erase(vch.begin(), vch.end());
358  }
359  else
360  {
361  nReadPos = (last - vch.begin());
362  return last;
363  }
364  }
365  else
366  return vch.erase(first, last);
367  }
368 
369  inline void Compact()
370  {
371  vch.erase(vch.begin(), vch.begin() + nReadPos);
372  nReadPos = 0;
373  }
374 
375  bool Rewind(size_type n)
376  {
377  // Rewind by n characters if the buffer hasn't been compacted yet
378  if (n > nReadPos)
379  return false;
380  nReadPos -= n;
381  return true;
382  }
383 
384 
385  //
386  // Stream subset
387  //
388  bool eof() const { return size() == 0; }
389  CDataStream* rdbuf() { return this; }
390  int in_avail() const { return size(); }
391 
392  void SetType(int n) { nType = n; }
393  int GetType() const { return nType; }
394  void SetVersion(int n) { nVersion = n; }
395  int GetVersion() const { return nVersion; }
396 
397  void read(char* pch, size_t nSize)
398  {
399  if (nSize == 0) return;
400 
401  // Read from the beginning of the buffer
402  unsigned int nReadPosNext = nReadPos + nSize;
403  if (nReadPosNext > vch.size()) {
404  throw std::ios_base::failure("CDataStream::read(): end of data");
405  }
406  memcpy(pch, &vch[nReadPos], nSize);
407  if (nReadPosNext == vch.size())
408  {
409  nReadPos = 0;
410  vch.clear();
411  return;
412  }
413  nReadPos = nReadPosNext;
414  }
415 
416  void ignore(int nSize)
417  {
418  // Ignore from the beginning of the buffer
419  if (nSize < 0) {
420  throw std::ios_base::failure("CDataStream::ignore(): nSize negative");
421  }
422  unsigned int nReadPosNext = nReadPos + nSize;
423  if (nReadPosNext >= vch.size())
424  {
425  if (nReadPosNext > vch.size())
426  throw std::ios_base::failure("CDataStream::ignore(): end of data");
427  nReadPos = 0;
428  vch.clear();
429  return;
430  }
431  nReadPos = nReadPosNext;
432  }
433 
434  void write(const char* pch, size_t nSize)
435  {
436  // Write to the end of the buffer
437  vch.insert(vch.end(), pch, pch + nSize);
438  }
439 
440  template<typename Stream>
441  void Serialize(Stream& s) const
442  {
443  // Special case: stream << stream concatenates like stream += stream
444  if (!vch.empty())
445  s.write((char*)vch.data(), vch.size() * sizeof(value_type));
446  }
447 
448  template<typename T>
449  CDataStream& operator<<(const T& obj)
450  {
451  // Serialize to this stream
452  ::Serialize(*this, obj);
453  return (*this);
454  }
455 
456  template<typename T>
458  {
459  // Unserialize from this stream
460  ::Unserialize(*this, obj);
461  return (*this);
462  }
463 
465  d.insert(d.end(), begin(), end());
466  clear();
467  }
468 
474  void Xor(const std::vector<unsigned char>& key)
475  {
476  if (key.size() == 0) {
477  return;
478  }
479 
480  for (size_type i = 0, j = 0; i != size(); i++) {
481  vch[i] ^= key[j++];
482 
483  // This potentially acts on very many bytes of data, so it's
484  // important that we calculate `j`, i.e. the `key` index in this
485  // way instead of doing a %, which would effectively be a division
486  // for each byte Xor'd -- much slower than need be.
487  if (j == key.size())
488  j = 0;
489  }
490  }
491 };
492 
493 template <typename IStream>
495 {
496 private:
497  IStream& m_istream;
498 
501  uint8_t m_buffer{0};
502 
506  int m_offset{8};
507 
508 public:
509  explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
510 
514  uint64_t Read(int nbits) {
515  if (nbits < 0 || nbits > 64) {
516  throw std::out_of_range("nbits must be between 0 and 64");
517  }
518 
519  uint64_t data = 0;
520  while (nbits > 0) {
521  if (m_offset == 8) {
522  m_istream >> m_buffer;
523  m_offset = 0;
524  }
525 
526  int bits = std::min(8 - m_offset, nbits);
527  data <<= bits;
528  data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
529  m_offset += bits;
530  nbits -= bits;
531  }
532  return data;
533  }
534 };
535 
536 template <typename OStream>
538 {
539 private:
540  OStream& m_ostream;
541 
544  uint8_t m_buffer{0};
545 
549  int m_offset{0};
550 
551 public:
552  explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
553 
555  {
556  Flush();
557  }
558 
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");
565  }
566 
567  while (nbits > 0) {
568  int bits = std::min(8 - m_offset, nbits);
569  m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
570  m_offset += bits;
571  nbits -= bits;
572 
573  if (m_offset == 8) {
574  Flush();
575  }
576  }
577  }
578 
582  void Flush() {
583  if (m_offset == 0) {
584  return;
585  }
586 
587  m_ostream << m_buffer;
588  m_buffer = 0;
589  m_offset = 0;
590  }
591 };
592 
593 
594 
602 {
603 private:
604  const int nType;
605  const int nVersion;
606 
607  FILE* file;
608 
609 public:
610  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
611  {
612  file = filenew;
613  }
614 
616  {
617  fclose();
618  }
619 
620  // Disallow copies
621  CAutoFile(const CAutoFile&) = delete;
622  CAutoFile& operator=(const CAutoFile&) = delete;
623 
624  void fclose()
625  {
626  if (file) {
627  ::fclose(file);
628  file = nullptr;
629  }
630  }
631 
636  FILE* release() { FILE* ret = file; file = nullptr; return ret; }
637 
642  FILE* Get() const { return file; }
643 
646  bool IsNull() const { return (file == nullptr); }
647 
648  //
649  // Stream subset
650  //
651  int GetType() const { return nType; }
652  int GetVersion() const { return nVersion; }
653 
654  void read(char* pch, size_t nSize)
655  {
656  if (!file)
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");
660  }
661 
662  void ignore(size_t nSize)
663  {
664  if (!file)
665  throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
666  unsigned char data[4096];
667  while (nSize > 0) {
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");
671  nSize -= nNow;
672  }
673  }
674 
675  void write(const char* pch, size_t nSize)
676  {
677  if (!file)
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");
681  }
682 
683  template<typename T>
684  CAutoFile& operator<<(const T& obj)
685  {
686  // Serialize to this stream
687  if (!file)
688  throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
689  ::Serialize(*this, obj);
690  return (*this);
691  }
692 
693  template<typename T>
695  {
696  // Unserialize from this stream
697  if (!file)
698  throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
699  ::Unserialize(*this, obj);
700  return (*this);
701  }
702 };
703 
711 {
712 private:
713  const int nType;
714  const int nVersion;
715 
716  FILE *src;
717  uint64_t nSrcPos;
718  uint64_t nReadPos;
719  uint64_t nReadLimit;
720  uint64_t nRewind;
721  std::vector<char> vchBuf;
722 
723 protected:
725  bool Fill() {
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)
730  readNow = nAvail;
731  if (readNow == 0)
732  return false;
733  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
734  if (nBytes == 0) {
735  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
736  }
737  nSrcPos += nBytes;
738  return true;
739  }
740 
741 public:
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)
744  {
745  if (nRewindIn >= nBufSize)
746  throw std::ios_base::failure("Rewind limit must be less than buffer size");
747  src = fileIn;
748  }
749 
751  {
752  fclose();
753  }
754 
755  // Disallow copies
756  CBufferedFile(const CBufferedFile&) = delete;
757  CBufferedFile& operator=(const CBufferedFile&) = delete;
758 
759  int GetVersion() const { return nVersion; }
760  int GetType() const { return nType; }
761 
762  void fclose()
763  {
764  if (src) {
765  ::fclose(src);
766  src = nullptr;
767  }
768  }
769 
771  bool eof() const {
772  return nReadPos == nSrcPos && feof(src);
773  }
774 
776  void read(char *pch, size_t nSize) {
777  if (nSize + nReadPos > nReadLimit)
778  throw std::ios_base::failure("Read attempted past buffer limit");
779  while (nSize > 0) {
780  if (nReadPos == nSrcPos)
781  Fill();
782  unsigned int pos = nReadPos % vchBuf.size();
783  size_t nNow = nSize;
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);
789  nReadPos += nNow;
790  pch += nNow;
791  nSize -= nNow;
792  }
793  }
794 
796  uint64_t GetPos() const {
797  return nReadPos;
798  }
799 
801  bool SetPos(uint64_t nPos) {
802  size_t bufsize = vchBuf.size();
803  if (nPos + bufsize < nSrcPos) {
804  // rewinding too far, rewind as far as possible
805  nReadPos = nSrcPos - bufsize;
806  return false;
807  }
808  if (nPos > nSrcPos) {
809  // can't go this far forward, go as far as possible
810  nReadPos = nSrcPos;
811  return false;
812  }
813  nReadPos = nPos;
814  return true;
815  }
816 
817  bool Seek(uint64_t nPos) {
818  long nLongPos = nPos;
819  if (nPos != (uint64_t)nLongPos)
820  return false;
821  if (fseek(src, nLongPos, SEEK_SET))
822  return false;
823  nLongPos = ftell(src);
824  nSrcPos = nLongPos;
825  nReadPos = nLongPos;
826  return true;
827  }
828 
831  bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
832  if (nPos < nReadPos)
833  return false;
834  nReadLimit = nPos;
835  return true;
836  }
837 
838  template<typename T>
840  // Unserialize from this stream
841  ::Unserialize(*this, obj);
842  return (*this);
843  }
844 
846  void FindByte(char ch) {
847  while (true) {
848  if (nReadPos == nSrcPos)
849  Fill();
850  if (vchBuf[nReadPos % vchBuf.size()] == ch)
851  break;
852  nReadPos++;
853  }
854  }
855 };
856 
857 #endif // BITCOIN_STREAMS_H
const int nType
Definition: streams.h:713
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:107
void Init(int nTypeIn, int nVersionIn)
Definition: streams.h:259
CSerializeData vector_type
Definition: streams.h:204
void ignore(size_t nSize)
Definition: streams.h:662
uint64_t nReadPos
how many bytes have been read from this
Definition: streams.h:718
vector_type::iterator iterator
Definition: streams.h:218
vector_type::allocator_type allocator_type
Definition: streams.h:212
vector_type vch
Definition: streams.h:205
std::vector< unsigned char > & vchData
Definition: streams.h:124
void Compact()
Definition: streams.h:369
int GetVersion() const
Definition: streams.h:395
void read(char *dst, size_t n)
Definition: streams.h:180
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
Definition: streams.h:227
unsigned int nReadPos
Definition: streams.h:206
size_t nPos
Definition: streams.h:125
int GetType() const
Definition: streams.h:393
int GetType() const
Definition: streams.h:117
CDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:247
void write(const char *pch, size_t nSize)
Definition: streams.h:94
const int nVersion
Definition: streams.h:605
vector_type::size_type size_type
Definition: streams.h:213
std::string str() const
Definition: streams.h:279
const int nType
Definition: streams.h:28
void resize(size_type n, value_type c=0)
Definition: streams.h:294
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:237
void Serialize(Stream &s) const
Definition: streams.h:441
vector_type::reference reference
Definition: streams.h:215
int GetType() const
Definition: streams.h:760
vector_type::value_type value_type
Definition: streams.h:217
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:474
value_type * data()
Definition: streams.h:301
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:35
const int m_version
Definition: streams.h:134
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:201
void write(const char *pch, size_t nSize)
Definition: streams.h:434
bool empty() const
Definition: streams.h:293
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
Definition: streams.h:562
int GetVersion() const
Definition: streams.h:60
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:220
size_t size() const
Definition: streams.h:177
int GetVersion() const
Definition: streams.h:652
iterator erase(iterator it)
Definition: streams.h:332
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:610
iterator end()
Definition: streams.h:291
const int nVersion
Definition: streams.h:714
void UnserializeMany(Stream &s)
Definition: serialize.h:969
friend CDataStream operator+(const CDataStream &a, const CDataStream &b)
Definition: streams.h:272
CDataStream * rdbuf()
Definition: streams.h:389
~CAutoFile()
Definition: streams.h:615
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:222
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
Definition: streams.h:304
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:646
iterator insert(iterator it, const char x=char())
Definition: streams.h:299
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:636
void write(const char *pch, size_t nSize)
Definition: streams.h:675
void Serialize(Stream &s, char a)
Definition: serialize.h:203
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:90
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos, Args &&... args)
(other params same as above)
Definition: streams.h:159
disconnectpool queuedTx clear()
void read(char *pch, size_t nSize)
Definition: streams.h:397
vector_type::const_reference const_reference
Definition: streams.h:216
void read(char *pch, size_t nSize)
Definition: streams.h:55
CAutoFile & operator>>(T &&obj)
Definition: streams.h:694
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:253
void fclose()
Definition: streams.h:624
uint64_t GetPos() const
return the current reading position
Definition: streams.h:796
BitStreamReader(IStream &istream)
Definition: streams.h:509
const int nType
Definition: streams.h:122
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:242
uint64_t nReadLimit
up to which position we&#39;re allowed to read
Definition: streams.h:719
size_type size() const
Definition: streams.h:292
vector_type::const_iterator const_iterator
Definition: streams.h:219
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:742
Minimal stream for reading from an existing vector by reference.
Definition: streams.h:130
void ignore(int nSize)
Definition: streams.h:416
const value_type * data() const
Definition: streams.h:302
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
Definition: streams.h:232
CDataStream & operator+=(const CDataStream &b)
Definition: streams.h:266
CDataStream & operator>>(T &&obj)
Definition: streams.h:457
void GetAndClear(CSerializeData &d)
Definition: streams.h:464
IStream & m_istream
Definition: streams.h:497
iterator begin()
Definition: streams.h:289
void write(const char *pch, size_t nSize)
Definition: streams.h:50
int GetVersion() const
Definition: streams.h:174
size_t size() const
Definition: streams.h:62
int GetVersion() const
Definition: streams.h:113
void read(char *pch, size_t nSize)
Definition: streams.h:654
void read(char *pch, size_t nSize)
read a number of bytes
Definition: streams.h:776
reference operator[](size_type pos)
Definition: streams.h:297
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:514
bool SetPos(uint64_t nPos)
rewind to a given reading position
Definition: streams.h:801
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:32
bool Rewind(size_type n)
Definition: streams.h:375
void SerializeMany(Stream &s)
Definition: serialize.h:957
void Flush()
Flush any unwritten bits to the output stream, padding with 0&#39;s to the next byte boundary.
Definition: streams.h:582
const_reference operator[](size_type pos) const
Definition: streams.h:296
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos)
Definition: streams.h:146
int GetType() const
Definition: streams.h:651
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:642
void fclose()
Definition: streams.h:762
const int nVersion
Definition: streams.h:123
const_iterator end() const
Definition: streams.h:290
const_iterator begin() const
Definition: streams.h:288
const int nVersion
Definition: streams.h:29
VectorReader & operator>>(T &obj)
Definition: streams.h:167
CDataStream & operator<<(const T &obj)
Definition: streams.h:449
void FindByte(char ch)
search for a given byte in the stream, and remain positioned on it
Definition: streams.h:846
void reserve(size_type n)
Definition: streams.h:295
void * memcpy(void *a, const void *b, size_t c)
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:839
int nVersion
Definition: streams.h:209
FILE * src
source file
Definition: streams.h:716
void Unserialize(Stream &s, char &a)
Definition: serialize.h:221
vector_type::difference_type difference_type
Definition: streams.h:214
const int m_type
Definition: streams.h:133
bool empty() const
Definition: streams.h:178
const std::vector< unsigned char > & m_data
Definition: streams.h:135
void insert(iterator it, const char *first, const char *last)
Definition: streams.h:318
void clear()
Definition: streams.h:298
uint64_t nSrcPos
how many bytes have been read from source
Definition: streams.h:717
bool Fill()
read data from the source to fill the buffer
Definition: streams.h:725
std::vector< char, zero_after_free_allocator< char > > CSerializeData
Definition: zeroafterfree.h:46
FILE * file
Definition: streams.h:607
std::vector< char > vchBuf
the buffer
Definition: streams.h:721
iterator erase(iterator first, iterator last)
Definition: streams.h:349
uint64_t nRewind
how many bytes we guarantee to rewind
Definition: streams.h:720
int GetVersion() const
Definition: streams.h:759
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
prevent reading beyond a certain position no argument removes the limit
Definition: streams.h:831
OStream & m_ostream
Definition: streams.h:540
const int nType
Definition: streams.h:604
int GetType() const
Definition: streams.h:175
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from...
Definition: streams.h:710
void SetVersion(int n)
Definition: streams.h:394
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:43
auto it
Definition: validation.cpp:362
int nType
Definition: streams.h:208
bool Seek(uint64_t nPos)
Definition: streams.h:817
Stream * stream
Definition: streams.h:26
bool eof() const
Definition: streams.h:388
CAutoFile & operator<<(const T &obj)
Definition: streams.h:684
int GetType() const
Definition: streams.h:61
void insert(iterator it, size_type n, const char x)
Definition: streams.h:300
~CBufferedFile()
Definition: streams.h:750
int in_avail() const
Definition: streams.h:390
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:601
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:80
BitStreamWriter(OStream &ostream)
Definition: streams.h:552
void SetType(int n)
Definition: streams.h:392
bool eof() const
check whether we&#39;re at the end of the source file
Definition: streams.h:771