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