BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
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 template<typename S>
68 OverrideStream<S> WithOrVersion(S* s, int nVersionFlag)
69 {
70  return OverrideStream<S>(s, s->GetType(), s->GetVersion() | nVersionFlag);
71 }
72 
73 /* Minimal stream for overwriting and/or appending to an existing byte vector
74  *
75  * The referenced vector will grow as necessary
76  */
78 {
79  public:
80 
81 /*
82  * @param[in] nTypeIn Serialization Type
83  * @param[in] nVersionIn Serialization Version (including any flags)
84  * @param[in] vchDataIn Referenced byte vector to overwrite/append
85  * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
86  * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
87 */
88  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
89  {
90  if(nPos > vchData.size())
91  vchData.resize(nPos);
92  }
93 /*
94  * (other params same as above)
95  * @param[in] args A list of items to serialize starting at nPosIn.
96 */
97  template <typename... Args>
98  CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
99  {
100  ::SerializeMany(*this, std::forward<Args>(args)...);
101  }
102  void write(const char* pch, size_t nSize)
103  {
104  assert(nPos <= vchData.size());
105  size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
106  if (nOverwrite) {
107  memcpy(vchData.data() + nPos, reinterpret_cast<const unsigned char*>(pch), nOverwrite);
108  }
109  if (nOverwrite < nSize) {
110  vchData.insert(vchData.end(), reinterpret_cast<const unsigned char*>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
111  }
112  nPos += nSize;
113  }
114  template<typename T>
115  CVectorWriter& operator<<(const T& obj)
116  {
117  // Serialize to this stream
118  ::Serialize(*this, obj);
119  return (*this);
120  }
121  int GetVersion() const
122  {
123  return nVersion;
124  }
125  int GetType() const
126  {
127  return nType;
128  }
129  void seek(size_t nSize)
130  {
131  nPos += nSize;
132  if(nPos > vchData.size())
133  vchData.resize(nPos);
134  }
135 private:
136  const int nType;
137  const int nVersion;
138  std::vector<unsigned char>& vchData;
139  size_t nPos;
140 };
141 
145 {
146 private:
147  const int m_type;
148  const int m_version;
149  const std::vector<unsigned char>& m_data;
150  size_t m_pos = 0;
151 
152 public:
153 
154  /*
155  * @param[in] type Serialization Type
156  * @param[in] version Serialization Version (including any flags)
157  * @param[in] data Referenced byte vector to overwrite/append
158  * @param[in] pos Starting position. Vector index where reads should start.
159  */
160  VectorReader(int type, int version, const std::vector<unsigned char>& data, size_t pos)
161  : m_type(type), m_version(version), m_data(data)
162  {
163  seek(pos);
164  }
165 
166  /*
167  * (other params same as above)
168  * @param[in] args A list of items to deserialize starting at pos.
169  */
170  template <typename... Args>
171  VectorReader(int type, int version, const std::vector<unsigned char>& data, size_t pos,
172  Args&&... args)
173  : VectorReader(type, version, data, pos)
174  {
175  ::UnserializeMany(*this, std::forward<Args>(args)...);
176  }
177 
178  template<typename T>
180  {
181  // Unserialize from this stream
182  ::Unserialize(*this, obj);
183  return (*this);
184  }
185 
186  int GetVersion() const { return m_version; }
187  int GetType() const { return m_type; }
188 
189  size_t size() const { return m_data.size() - m_pos; }
190  bool empty() const { return m_data.size() == m_pos; }
191 
192  void read(char* dst, size_t n)
193  {
194  if (n == 0) {
195  return;
196  }
197 
198  // Read from the beginning of the buffer
199  size_t pos_next = m_pos + n;
200  if (pos_next > m_data.size()) {
201  throw std::ios_base::failure("VectorReader::read(): end of data");
202  }
203  memcpy(dst, m_data.data() + m_pos, n);
204  m_pos = pos_next;
205  }
206 
207  void seek(size_t n)
208  {
209  m_pos += n;
210  if (m_pos > m_data.size()) {
211  throw std::ios_base::failure("VectorReader::seek(): end of data");
212  }
213  }
214 };
215 
222 {
223 protected:
226  unsigned int nReadPos;
227 
228  int nType;
229  int nVersion;
230 public:
231 
232  typedef vector_type::allocator_type allocator_type;
233  typedef vector_type::size_type size_type;
234  typedef vector_type::difference_type difference_type;
235  typedef vector_type::reference reference;
236  typedef vector_type::const_reference const_reference;
237  typedef vector_type::value_type value_type;
238  typedef vector_type::iterator iterator;
239  typedef vector_type::const_iterator const_iterator;
240  typedef vector_type::reverse_iterator reverse_iterator;
241 
242  explicit CDataStream(int nTypeIn, int nVersionIn)
243  {
244  Init(nTypeIn, nVersionIn);
245  }
246 
247  CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
248  {
249  Init(nTypeIn, nVersionIn);
250  }
251 
252  CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
253  {
254  Init(nTypeIn, nVersionIn);
255  }
256 
257  CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
258  {
259  Init(nTypeIn, nVersionIn);
260  }
261 
262  CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
263  {
264  Init(nTypeIn, nVersionIn);
265  }
266 
267  CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
268  {
269  Init(nTypeIn, nVersionIn);
270  }
271 
272  template <typename... Args>
273  CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
274  {
275  Init(nTypeIn, nVersionIn);
276  ::SerializeMany(*this, std::forward<Args>(args)...);
277  }
278 
279  void Init(int nTypeIn, int nVersionIn)
280  {
281  nReadPos = 0;
282  nType = nTypeIn;
283  nVersion = nVersionIn;
284  }
285 
287  {
288  vch.insert(vch.end(), b.begin(), b.end());
289  return *this;
290  }
291 
292  friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
293  {
294  CDataStream ret = a;
295  ret += b;
296  return (ret);
297  }
298 
299  std::string str() const
300  {
301  return (std::string(begin(), end()));
302  }
303 
304 
305  //
306  // Vector subset
307  //
308  const_iterator begin() const { return vch.begin() + nReadPos; }
309  iterator begin() { return vch.begin() + nReadPos; }
310  const_iterator end() const { return vch.end(); }
311  iterator end() { return vch.end(); }
312  size_type size() const { return vch.size() - nReadPos; }
313  bool empty() const { return vch.size() == nReadPos; }
314  void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
315  void reserve(size_type n) { vch.reserve(n + nReadPos); }
316  const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
317  reference operator[](size_type pos) { return vch[pos + nReadPos]; }
318  void clear() { vch.clear(); nReadPos = 0; }
319  iterator insert(iterator it, const char x=char()) { return vch.insert(it, x); }
320  void insert(iterator it, size_type n, const char x) { vch.insert(it, n, x); }
321  value_type* data() { return vch.data() + nReadPos; }
322  const value_type* data() const { return vch.data() + nReadPos; }
323 
324  void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
325  {
326  if (last == first) return;
327  assert(last - first > 0);
328  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
329  {
330  // special case for inserting at the front when there's room
331  nReadPos -= (last - first);
332  memcpy(&vch[nReadPos], &first[0], last - first);
333  }
334  else
335  vch.insert(it, first, last);
336  }
337 
338  void insert(iterator it, const char* first, const char* last)
339  {
340  if (last == first) return;
341  assert(last - first > 0);
342  if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
343  {
344  // special case for inserting at the front when there's room
345  nReadPos -= (last - first);
346  memcpy(&vch[nReadPos], &first[0], last - first);
347  }
348  else
349  vch.insert(it, first, last);
350  }
351 
353  {
354  if (it == vch.begin() + nReadPos)
355  {
356  // special case for erasing from the front
357  if (++nReadPos >= vch.size())
358  {
359  // whenever we reach the end, we take the opportunity to clear the buffer
360  nReadPos = 0;
361  return vch.erase(vch.begin(), vch.end());
362  }
363  return vch.begin() + nReadPos;
364  }
365  else
366  return vch.erase(it);
367  }
368 
370  {
371  if (first == vch.begin() + nReadPos)
372  {
373  // special case for erasing from the front
374  if (last == vch.end())
375  {
376  nReadPos = 0;
377  return vch.erase(vch.begin(), vch.end());
378  }
379  else
380  {
381  nReadPos = (last - vch.begin());
382  return last;
383  }
384  }
385  else
386  return vch.erase(first, last);
387  }
388 
389  inline void Compact()
390  {
391  vch.erase(vch.begin(), vch.begin() + nReadPos);
392  nReadPos = 0;
393  }
394 
396  {
397  // Rewind by n characters if the buffer hasn't been compacted yet
398  if (n > nReadPos)
399  return false;
400  nReadPos -= n;
401  return true;
402  }
403 
404 
405  //
406  // Stream subset
407  //
408  bool eof() const { return size() == 0; }
409  CDataStream* rdbuf() { return this; }
410  int in_avail() const { return size(); }
411 
412  void SetType(int n) { nType = n; }
413  int GetType() const { return nType; }
414  void SetVersion(int n) { nVersion = n; }
415  int GetVersion() const { return nVersion; }
416 
417  void read(char* pch, size_t nSize)
418  {
419  if (nSize == 0) return;
420 
421  // Read from the beginning of the buffer
422  unsigned int nReadPosNext = nReadPos + nSize;
423  if (nReadPosNext > vch.size()) {
424  throw std::ios_base::failure("CDataStream::read(): end of data");
425  }
426  memcpy(pch, &vch[nReadPos], nSize);
427  if (nReadPosNext == vch.size())
428  {
429  nReadPos = 0;
430  vch.clear();
431  return;
432  }
433  nReadPos = nReadPosNext;
434  }
435 
436  void ignore(int nSize)
437  {
438  // Ignore from the beginning of the buffer
439  if (nSize < 0) {
440  throw std::ios_base::failure("CDataStream::ignore(): nSize negative");
441  }
442  unsigned int nReadPosNext = nReadPos + nSize;
443  if (nReadPosNext >= vch.size())
444  {
445  if (nReadPosNext > vch.size())
446  throw std::ios_base::failure("CDataStream::ignore(): end of data");
447  nReadPos = 0;
448  vch.clear();
449  return;
450  }
451  nReadPos = nReadPosNext;
452  }
453 
454  void write(const char* pch, size_t nSize)
455  {
456  // Write to the end of the buffer
457  vch.insert(vch.end(), pch, pch + nSize);
458  }
459 
460  template<typename Stream>
461  void Serialize(Stream& s) const
462  {
463  // Special case: stream << stream concatenates like stream += stream
464  if (!vch.empty())
465  s.write((char*)vch.data(), vch.size() * sizeof(value_type));
466  }
467 
468  template<typename T>
469  CDataStream& operator<<(const T& obj)
470  {
471  // Serialize to this stream
472  ::Serialize(*this, obj);
473  return (*this);
474  }
475 
476  template<typename T>
478  {
479  // Unserialize from this stream
480  ::Unserialize(*this, obj);
481  return (*this);
482  }
483 
485  d.insert(d.end(), begin(), end());
486  clear();
487  }
488 
494  void Xor(const std::vector<unsigned char>& key)
495  {
496  if (key.size() == 0) {
497  return;
498  }
499 
500  for (size_type i = 0, j = 0; i != size(); i++) {
501  vch[i] ^= key[j++];
502 
503  // This potentially acts on very many bytes of data, so it's
504  // important that we calculate `j`, i.e. the `key` index in this
505  // way instead of doing a %, which would effectively be a division
506  // for each byte Xor'd -- much slower than need be.
507  if (j == key.size())
508  j = 0;
509  }
510  }
511 };
512 
513 template <typename IStream>
515 {
516 private:
517  IStream& m_istream;
518 
521  uint8_t m_buffer{0};
522 
526  int m_offset{8};
527 
528 public:
529  explicit BitStreamReader(IStream& istream) : m_istream(istream) {}
530 
534  uint64_t Read(int nbits) {
535  if (nbits < 0 || nbits > 64) {
536  throw std::out_of_range("nbits must be between 0 and 64");
537  }
538 
539  uint64_t data = 0;
540  while (nbits > 0) {
541  if (m_offset == 8) {
542  m_istream >> m_buffer;
543  m_offset = 0;
544  }
545 
546  int bits = std::min(8 - m_offset, nbits);
547  data <<= bits;
548  data |= static_cast<uint8_t>(m_buffer << m_offset) >> (8 - bits);
549  m_offset += bits;
550  nbits -= bits;
551  }
552  return data;
553  }
554 };
555 
556 template <typename OStream>
558 {
559 private:
560  OStream& m_ostream;
561 
564  uint8_t m_buffer{0};
565 
569  int m_offset{0};
570 
571 public:
572  explicit BitStreamWriter(OStream& ostream) : m_ostream(ostream) {}
573 
575  {
576  Flush();
577  }
578 
582  void Write(uint64_t data, int nbits) {
583  if (nbits < 0 || nbits > 64) {
584  throw std::out_of_range("nbits must be between 0 and 64");
585  }
586 
587  while (nbits > 0) {
588  int bits = std::min(8 - m_offset, nbits);
589  m_buffer |= (data << (64 - nbits)) >> (64 - 8 + m_offset);
590  m_offset += bits;
591  nbits -= bits;
592 
593  if (m_offset == 8) {
594  Flush();
595  }
596  }
597  }
598 
602  void Flush() {
603  if (m_offset == 0) {
604  return;
605  }
606 
607  m_ostream << m_buffer;
608  m_buffer = 0;
609  m_offset = 0;
610  }
611 };
612 
613 
614 
622 {
623 private:
624  const int nType;
625  const int nVersion;
626 
627  FILE* file;
628 
629 public:
630  CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
631  {
632  file = filenew;
633  }
634 
636  {
637  fclose();
638  }
639 
640  // Disallow copies
641  CAutoFile(const CAutoFile&) = delete;
642  CAutoFile& operator=(const CAutoFile&) = delete;
643 
644  void fclose()
645  {
646  if (file) {
647  ::fclose(file);
648  file = nullptr;
649  }
650  }
651 
656  FILE* release() { FILE* ret = file; file = nullptr; return ret; }
657 
662  FILE* Get() const { return file; }
663 
666  bool IsNull() const { return (file == nullptr); }
667 
668  //
669  // Stream subset
670  //
671  int GetType() const { return nType; }
672  int GetVersion() const { return nVersion; }
673 
674  void read(char* pch, size_t nSize)
675  {
676  if (!file)
677  throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
678  if (fread(pch, 1, nSize, file) != nSize)
679  throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
680  }
681 
682  void ignore(size_t nSize)
683  {
684  if (!file)
685  throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
686  unsigned char data[4096];
687  while (nSize > 0) {
688  size_t nNow = std::min<size_t>(nSize, sizeof(data));
689  if (fread(data, 1, nNow, file) != nNow)
690  throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
691  nSize -= nNow;
692  }
693  }
694 
695  void write(const char* pch, size_t nSize)
696  {
697  if (!file)
698  throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
699  if (fwrite(pch, 1, nSize, file) != nSize)
700  throw std::ios_base::failure("CAutoFile::write: write failed");
701  }
702 
703  template<typename T>
704  CAutoFile& operator<<(const T& obj)
705  {
706  // Serialize to this stream
707  if (!file)
708  throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
709  ::Serialize(*this, obj);
710  return (*this);
711  }
712 
713  template<typename T>
715  {
716  // Unserialize from this stream
717  if (!file)
718  throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
719  ::Unserialize(*this, obj);
720  return (*this);
721  }
722 };
723 
731 {
732 private:
733  const int nType;
734  const int nVersion;
735 
736  FILE *src; // source file
737  uint64_t nSrcPos; // how many bytes have been read from source
738  uint64_t nReadPos; // how many bytes have been read from this
739  uint64_t nReadLimit; // up to which position we're allowed to read
740  uint64_t nRewind; // how many bytes we guarantee to rewind
741  std::vector<char> vchBuf; // the buffer
742 
743 protected:
744  // read data from the source to fill the buffer
745  bool Fill() {
746  unsigned int pos = nSrcPos % vchBuf.size();
747  unsigned int readNow = vchBuf.size() - pos;
748  unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
749  if (nAvail < readNow)
750  readNow = nAvail;
751  if (readNow == 0)
752  return false;
753  size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
754  if (nBytes == 0) {
755  throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
756  } else {
757  nSrcPos += nBytes;
758  return true;
759  }
760  }
761 
762 public:
763  CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
764  nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
765  {
766  src = fileIn;
767  }
768 
770  {
771  fclose();
772  }
773 
774  // Disallow copies
775  CBufferedFile(const CBufferedFile&) = delete;
776  CBufferedFile& operator=(const CBufferedFile&) = delete;
777 
778  int GetVersion() const { return nVersion; }
779  int GetType() const { return nType; }
780 
781  void fclose()
782  {
783  if (src) {
784  ::fclose(src);
785  src = nullptr;
786  }
787  }
788 
789  // check whether we're at the end of the source file
790  bool eof() const {
791  return nReadPos == nSrcPos && feof(src);
792  }
793 
794  // read a number of bytes
795  void read(char *pch, size_t nSize) {
796  if (nSize + nReadPos > nReadLimit)
797  throw std::ios_base::failure("Read attempted past buffer limit");
798  if (nSize + nRewind > vchBuf.size())
799  throw std::ios_base::failure("Read larger than buffer size");
800  while (nSize > 0) {
801  if (nReadPos == nSrcPos)
802  Fill();
803  unsigned int pos = nReadPos % vchBuf.size();
804  size_t nNow = nSize;
805  if (nNow + pos > vchBuf.size())
806  nNow = vchBuf.size() - pos;
807  if (nNow + nReadPos > nSrcPos)
808  nNow = nSrcPos - nReadPos;
809  memcpy(pch, &vchBuf[pos], nNow);
810  nReadPos += nNow;
811  pch += nNow;
812  nSize -= nNow;
813  }
814  }
815 
816  // return the current reading position
817  uint64_t GetPos() const {
818  return nReadPos;
819  }
820 
821  // rewind to a given reading position
822  bool SetPos(uint64_t nPos) {
823  nReadPos = nPos;
824  if (nReadPos + nRewind < nSrcPos) {
826  return false;
827  } else if (nReadPos > nSrcPos) {
828  nReadPos = nSrcPos;
829  return false;
830  } else {
831  return true;
832  }
833  }
834 
835  bool Seek(uint64_t nPos) {
836  long nLongPos = nPos;
837  if (nPos != (uint64_t)nLongPos)
838  return false;
839  if (fseek(src, nLongPos, SEEK_SET))
840  return false;
841  nLongPos = ftell(src);
842  nSrcPos = nLongPos;
843  nReadPos = nLongPos;
844  return true;
845  }
846 
847  // prevent reading beyond a certain position
848  // no argument removes the limit
849  bool SetLimit(uint64_t nPos = (uint64_t)(-1)) {
850  if (nPos < nReadPos)
851  return false;
852  nReadLimit = nPos;
853  return true;
854  }
855 
856  template<typename T>
858  // Unserialize from this stream
859  ::Unserialize(*this, obj);
860  return (*this);
861  }
862 
863  // search for a given byte in the stream, and remain positioned on it
864  void FindByte(char ch) {
865  while (true) {
866  if (nReadPos == nSrcPos)
867  Fill();
868  if (vchBuf[nReadPos % vchBuf.size()] == ch)
869  break;
870  nReadPos++;
871  }
872  }
873 };
874 
875 #endif // BITCOIN_STREAMS_H
const int nType
Definition: streams.h:733
CVectorWriter & operator<<(const T &obj)
Definition: streams.h:115
void Init(int nTypeIn, int nVersionIn)
Definition: streams.h:279
CSerializeData vector_type
Definition: streams.h:224
void ignore(size_t nSize)
Definition: streams.h:682
uint64_t nReadPos
Definition: streams.h:738
vector_type::iterator iterator
Definition: streams.h:238
vector_type::allocator_type allocator_type
Definition: streams.h:232
vector_type vch
Definition: streams.h:225
std::vector< unsigned char > & vchData
Definition: streams.h:138
void Compact()
Definition: streams.h:389
int GetVersion() const
Definition: streams.h:415
void read(char *dst, size_t n)
Definition: streams.h:192
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
Definition: streams.h:247
unsigned int nReadPos
Definition: streams.h:226
size_t nPos
Definition: streams.h:139
int GetType() const
Definition: streams.h:413
int GetType() const
Definition: streams.h:125
CDataStream(const std::vector< unsigned char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:267
void write(const char *pch, size_t nSize)
Definition: streams.h:102
const int nVersion
Definition: streams.h:625
vector_type::size_type size_type
Definition: streams.h:233
UniValue ret(UniValue::VARR)
Definition: rpcwallet.cpp:1140
std::string str() const
Definition: streams.h:299
const int nType
Definition: streams.h:30
void resize(size_type n, value_type c=0)
Definition: streams.h:314
CDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:257
void Serialize(Stream &s) const
Definition: streams.h:461
vector_type::reference reference
Definition: streams.h:235
int GetType() const
Definition: streams.h:779
vector_type::value_type value_type
Definition: streams.h:237
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:494
value_type * data()
Definition: streams.h:321
OverrideStream< Stream > & operator<<(const T &obj)
Definition: streams.h:37
const int m_version
Definition: streams.h:148
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:221
void write(const char *pch, size_t nSize)
Definition: streams.h:454
bool empty() const
Definition: streams.h:313
OverrideStream< S > WithOrVersion(S *s, int nVersionFlag)
Definition: streams.h:68
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:582
int GetVersion() const
Definition: streams.h:62
vector_type::reverse_iterator reverse_iterator
Definition: streams.h:240
size_t size() const
Definition: streams.h:189
int GetVersion() const
Definition: streams.h:672
iterator erase(iterator it)
Definition: streams.h:352
CAutoFile(FILE *filenew, int nTypeIn, int nVersionIn)
Definition: streams.h:630
iterator end()
Definition: streams.h:311
const int nVersion
Definition: streams.h:734
void UnserializeMany(Stream &s)
Definition: serialize.h:946
friend CDataStream operator+(const CDataStream &a, const CDataStream &b)
Definition: streams.h:292
CDataStream * rdbuf()
Definition: streams.h:409
~CAutoFile()
Definition: streams.h:635
CDataStream(int nTypeIn, int nVersionIn)
Definition: streams.h:242
void insert(iterator it, std::vector< char >::const_iterator first, std::vector< char >::const_iterator last)
Definition: streams.h:324
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:666
iterator insert(iterator it, const char x=char())
Definition: streams.h:319
FILE * release()
Get wrapped FILE* with transfer of ownership.
Definition: streams.h:656
void write(const char *pch, size_t nSize)
Definition: streams.h:695
void Serialize(Stream &s, char a)
Definition: serialize.h:193
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn, Args &&... args)
Definition: streams.h:98
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos, Args &&... args)
Definition: streams.h:171
uint8_t m_buffer
Buffered byte read in from the input stream.
Definition: streams.h:521
void read(char *pch, size_t nSize)
Definition: streams.h:417
vector_type::const_reference const_reference
Definition: streams.h:236
void read(char *pch, size_t nSize)
Definition: streams.h:57
CAutoFile & operator>>(T &&obj)
Definition: streams.h:714
bool SetLimit(uint64_t nPos=(uint64_t)(-1))
Definition: streams.h:849
CDataStream(int nTypeIn, int nVersionIn, Args &&... args)
Definition: streams.h:273
void fclose()
Definition: streams.h:644
uint64_t GetPos() const
Definition: streams.h:817
BitStreamReader(IStream &istream)
Definition: streams.h:529
const int nType
Definition: streams.h:136
CDataStream(const std::vector< char > &vchIn, int nTypeIn, int nVersionIn)
Definition: streams.h:262
uint64_t nReadLimit
Definition: streams.h:739
size_type size() const
Definition: streams.h:312
vector_type::const_iterator const_iterator
Definition: streams.h:239
void seek(size_t nSize)
Definition: streams.h:129
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
Definition: streams.h:763
Minimal stream for reading from an existing vector by reference.
Definition: streams.h:144
void ignore(int nSize)
Definition: streams.h:436
const value_type * data() const
Definition: streams.h:322
CDataStream(const char *pbegin, const char *pend, int nTypeIn, int nVersionIn)
Definition: streams.h:252
CDataStream & operator+=(const CDataStream &b)
Definition: streams.h:286
CDataStream & operator>>(T &&obj)
Definition: streams.h:477
void GetAndClear(CSerializeData &d)
Definition: streams.h:484
IStream & m_istream
Definition: streams.h:517
iterator begin()
Definition: streams.h:309
void write(const char *pch, size_t nSize)
Definition: streams.h:52
int GetVersion() const
Definition: streams.h:186
size_t size() const
Definition: streams.h:64
int GetVersion() const
Definition: streams.h:121
void read(char *pch, size_t nSize)
Definition: streams.h:674
void read(char *pch, size_t nSize)
Definition: streams.h:795
reference operator[](size_type pos)
Definition: streams.h:317
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
Definition: streams.h:534
bool SetPos(uint64_t nPos)
Definition: streams.h:822
OverrideStream(Stream *stream_, int nType_, int nVersion_)
Definition: streams.h:34
bool Rewind(size_type n)
Definition: streams.h:395
void SerializeMany(Stream &s)
Definition: serialize.h:934
void Flush()
Flush any unwritten bits to the output stream, padding with 0&#39;s to the next byte boundary.
Definition: streams.h:602
const_reference operator[](size_type pos) const
Definition: streams.h:316
VectorReader(int type, int version, const std::vector< unsigned char > &data, size_t pos)
Definition: streams.h:160
int GetType() const
Definition: streams.h:671
uint8_t m_buffer
Buffered byte waiting to be written to the output stream.
Definition: streams.h:564
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:662
size_t m_pos
Definition: streams.h:150
void fclose()
Definition: streams.h:781
const int nVersion
Definition: streams.h:137
const_iterator end() const
Definition: streams.h:310
int m_offset
Number of high order bits in m_buffer already written by previous Write() calls and not yet flushed t...
Definition: streams.h:569
const_iterator begin() const
Definition: streams.h:308
const int nVersion
Definition: streams.h:31
VectorReader & operator>>(T &obj)
Definition: streams.h:179
CDataStream & operator<<(const T &obj)
Definition: streams.h:469
void FindByte(char ch)
Definition: streams.h:864
void reserve(size_type n)
Definition: streams.h:315
void * memcpy(void *a, const void *b, size_t c)
int m_offset
Number of high order bits in m_buffer already returned by previous Read() calls.
Definition: streams.h:526
CBufferedFile & operator>>(T &&obj)
Definition: streams.h:857
int nVersion
Definition: streams.h:229
FILE * src
Definition: streams.h:736
void Unserialize(Stream &s, char &a)
Definition: serialize.h:211
vector_type::difference_type difference_type
Definition: streams.h:234
CAutoFile & operator=(const CAutoFile &)=delete
const int m_type
Definition: streams.h:147
bool empty() const
Definition: streams.h:190
const std::vector< unsigned char > & m_data
Definition: streams.h:149
void insert(iterator it, const char *first, const char *last)
Definition: streams.h:338
void clear()
Definition: streams.h:318
uint64_t nSrcPos
Definition: streams.h:737
bool Fill()
Definition: streams.h:745
std::vector< char, zero_after_free_allocator< char > > CSerializeData
Definition: zeroafterfree.h:46
FILE * file
Definition: streams.h:627
void seek(size_t n)
Definition: streams.h:207
std::vector< char > vchBuf
Definition: streams.h:741
iterator erase(iterator first, iterator last)
Definition: streams.h:369
uint64_t nRewind
Definition: streams.h:740
int GetVersion() const
Definition: streams.h:778
OStream & m_ostream
Definition: streams.h:560
const int nType
Definition: streams.h:624
int GetType() const
Definition: streams.h:187
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from...
Definition: streams.h:730
void SetVersion(int n)
Definition: streams.h:414
OverrideStream< Stream > & operator>>(T &&obj)
Definition: streams.h:45
CBufferedFile & operator=(const CBufferedFile &)=delete
int nType
Definition: streams.h:228
bool Seek(uint64_t nPos)
Definition: streams.h:835
Stream * stream
Definition: streams.h:28
bool eof() const
Definition: streams.h:408
CAutoFile & operator<<(const T &obj)
Definition: streams.h:704
int GetType() const
Definition: streams.h:63
void insert(iterator it, size_type n, const char x)
Definition: streams.h:320
~CBufferedFile()
Definition: streams.h:769
int in_avail() const
Definition: streams.h:410
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:621
CVectorWriter(int nTypeIn, int nVersionIn, std::vector< unsigned char > &vchDataIn, size_t nPosIn)
Definition: streams.h:88
BitStreamWriter(OStream &ostream)
Definition: streams.h:572
void SetType(int n)
Definition: streams.h:412
bool eof() const
Definition: streams.h:790