[qa] TestNode: Add wait_until_stopped helper method
[bitcoinplatinum.git] / src / streams.h
blob159847279d548d0b12be5b4f70e2b9d5fc9624e8
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #ifndef BITCOIN_STREAMS_H
7 #define BITCOIN_STREAMS_H
9 #include "support/allocators/zeroafterfree.h"
10 #include "serialize.h"
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>
25 template<typename Stream>
26 class OverrideStream
28 Stream* stream;
30 const int nType;
31 const int nVersion;
33 public:
34 OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
36 template<typename T>
37 OverrideStream<Stream>& operator<<(const T& obj)
39 // Serialize to this stream
40 ::Serialize(*this, obj);
41 return (*this);
44 template<typename T>
45 OverrideStream<Stream>& operator>>(T& obj)
47 // Unserialize from this stream
48 ::Unserialize(*this, obj);
49 return (*this);
52 void write(const char* pch, size_t nSize)
54 stream->write(pch, nSize);
57 void read(char* pch, size_t nSize)
59 stream->read(pch, nSize);
62 int GetVersion() const { return nVersion; }
63 int GetType() const { return nType; }
66 template<typename S>
67 OverrideStream<S> WithOrVersion(S* s, int nVersionFlag)
69 return OverrideStream<S>(s, s->GetType(), s->GetVersion() | nVersionFlag);
72 /* Minimal stream for overwriting and/or appending to an existing byte vector
74 * The referenced vector will grow as necessary
76 class CVectorWriter
78 public:
81 * @param[in] nTypeIn Serialization Type
82 * @param[in] nVersionIn Serialization Version (including any flags)
83 * @param[in] vchDataIn Referenced byte vector to overwrite/append
84 * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
85 * grow as necessary to max(nPosIn, vec.size()). So to append, use vec.size().
87 CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
89 if(nPos > vchData.size())
90 vchData.resize(nPos);
93 * (other params same as above)
94 * @param[in] args A list of items to serialize starting at nPosIn.
96 template <typename... Args>
97 CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
99 ::SerializeMany(*this, std::forward<Args>(args)...);
101 void write(const char* pch, size_t nSize)
103 assert(nPos <= vchData.size());
104 size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
105 if (nOverwrite) {
106 memcpy(vchData.data() + nPos, reinterpret_cast<const unsigned char*>(pch), nOverwrite);
108 if (nOverwrite < nSize) {
109 vchData.insert(vchData.end(), reinterpret_cast<const unsigned char*>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
111 nPos += nSize;
113 template<typename T>
114 CVectorWriter& operator<<(const T& obj)
116 // Serialize to this stream
117 ::Serialize(*this, obj);
118 return (*this);
120 int GetVersion() const
122 return nVersion;
124 int GetType() const
126 return nType;
128 void seek(size_t nSize)
130 nPos += nSize;
131 if(nPos > vchData.size())
132 vchData.resize(nPos);
134 private:
135 const int nType;
136 const int nVersion;
137 std::vector<unsigned char>& vchData;
138 size_t nPos;
141 /** Double ended buffer combining vector and stream-like interfaces.
143 * >> and << read and write unformatted data using the above serialization templates.
144 * Fills with data in linear time; some stringstream implementations take N^2 time.
146 class CDataStream
148 protected:
149 typedef CSerializeData vector_type;
150 vector_type vch;
151 unsigned int nReadPos;
153 int nType;
154 int nVersion;
155 public:
157 typedef vector_type::allocator_type allocator_type;
158 typedef vector_type::size_type size_type;
159 typedef vector_type::difference_type difference_type;
160 typedef vector_type::reference reference;
161 typedef vector_type::const_reference const_reference;
162 typedef vector_type::value_type value_type;
163 typedef vector_type::iterator iterator;
164 typedef vector_type::const_iterator const_iterator;
165 typedef vector_type::reverse_iterator reverse_iterator;
167 explicit CDataStream(int nTypeIn, int nVersionIn)
169 Init(nTypeIn, nVersionIn);
172 CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
174 Init(nTypeIn, nVersionIn);
177 CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
179 Init(nTypeIn, nVersionIn);
182 CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
184 Init(nTypeIn, nVersionIn);
187 CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
189 Init(nTypeIn, nVersionIn);
192 CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
194 Init(nTypeIn, nVersionIn);
197 template <typename... Args>
198 CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
200 Init(nTypeIn, nVersionIn);
201 ::SerializeMany(*this, std::forward<Args>(args)...);
204 void Init(int nTypeIn, int nVersionIn)
206 nReadPos = 0;
207 nType = nTypeIn;
208 nVersion = nVersionIn;
211 CDataStream& operator+=(const CDataStream& b)
213 vch.insert(vch.end(), b.begin(), b.end());
214 return *this;
217 friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
219 CDataStream ret = a;
220 ret += b;
221 return (ret);
224 std::string str() const
226 return (std::string(begin(), end()));
231 // Vector subset
233 const_iterator begin() const { return vch.begin() + nReadPos; }
234 iterator begin() { return vch.begin() + nReadPos; }
235 const_iterator end() const { return vch.end(); }
236 iterator end() { return vch.end(); }
237 size_type size() const { return vch.size() - nReadPos; }
238 bool empty() const { return vch.size() == nReadPos; }
239 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
240 void reserve(size_type n) { vch.reserve(n + nReadPos); }
241 const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
242 reference operator[](size_type pos) { return vch[pos + nReadPos]; }
243 void clear() { vch.clear(); nReadPos = 0; }
244 iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
245 void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
246 value_type* data() { return vch.data() + nReadPos; }
247 const value_type* data() const { return vch.data() + nReadPos; }
249 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
251 if (last == first) return;
252 assert(last - first > 0);
253 if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
255 // special case for inserting at the front when there's room
256 nReadPos -= (last - first);
257 memcpy(&vch[nReadPos], &first[0], last - first);
259 else
260 vch.insert(it, first, last);
263 void insert(iterator it, const char* first, const char* last)
265 if (last == first) return;
266 assert(last - first > 0);
267 if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
269 // special case for inserting at the front when there's room
270 nReadPos -= (last - first);
271 memcpy(&vch[nReadPos], &first[0], last - first);
273 else
274 vch.insert(it, first, last);
277 iterator erase(iterator it)
279 if (it == vch.begin() + nReadPos)
281 // special case for erasing from the front
282 if (++nReadPos >= vch.size())
284 // whenever we reach the end, we take the opportunity to clear the buffer
285 nReadPos = 0;
286 return vch.erase(vch.begin(), vch.end());
288 return vch.begin() + nReadPos;
290 else
291 return vch.erase(it);
294 iterator erase(iterator first, iterator last)
296 if (first == vch.begin() + nReadPos)
298 // special case for erasing from the front
299 if (last == vch.end())
301 nReadPos = 0;
302 return vch.erase(vch.begin(), vch.end());
304 else
306 nReadPos = (last - vch.begin());
307 return last;
310 else
311 return vch.erase(first, last);
314 inline void Compact()
316 vch.erase(vch.begin(), vch.begin() + nReadPos);
317 nReadPos = 0;
320 bool Rewind(size_type n)
322 // Rewind by n characters if the buffer hasn't been compacted yet
323 if (n > nReadPos)
324 return false;
325 nReadPos -= n;
326 return true;
331 // Stream subset
333 bool eof() const { return size() == 0; }
334 CDataStream* rdbuf() { return this; }
335 int in_avail() const { return size(); }
337 void SetType(int n) { nType = n; }
338 int GetType() const { return nType; }
339 void SetVersion(int n) { nVersion = n; }
340 int GetVersion() const { return nVersion; }
342 void read(char* pch, size_t nSize)
344 if (nSize == 0) return;
346 // Read from the beginning of the buffer
347 unsigned int nReadPosNext = nReadPos + nSize;
348 if (nReadPosNext >= vch.size())
350 if (nReadPosNext > vch.size())
352 throw std::ios_base::failure("CDataStream::read(): end of data");
354 memcpy(pch, &vch[nReadPos], nSize);
355 nReadPos = 0;
356 vch.clear();
357 return;
359 memcpy(pch, &vch[nReadPos], nSize);
360 nReadPos = nReadPosNext;
363 void ignore(int nSize)
365 // Ignore from the beginning of the buffer
366 if (nSize < 0) {
367 throw std::ios_base::failure("CDataStream::ignore(): nSize negative");
369 unsigned int nReadPosNext = nReadPos + nSize;
370 if (nReadPosNext >= vch.size())
372 if (nReadPosNext > vch.size())
373 throw std::ios_base::failure("CDataStream::ignore(): end of data");
374 nReadPos = 0;
375 vch.clear();
376 return;
378 nReadPos = nReadPosNext;
381 void write(const char* pch, size_t nSize)
383 // Write to the end of the buffer
384 vch.insert(vch.end(), pch, pch + nSize);
387 template<typename Stream>
388 void Serialize(Stream& s) const
390 // Special case: stream << stream concatenates like stream += stream
391 if (!vch.empty())
392 s.write((char*)vch.data(), vch.size() * sizeof(value_type));
395 template<typename T>
396 CDataStream& operator<<(const T& obj)
398 // Serialize to this stream
399 ::Serialize(*this, obj);
400 return (*this);
403 template<typename T>
404 CDataStream& operator>>(T& obj)
406 // Unserialize from this stream
407 ::Unserialize(*this, obj);
408 return (*this);
411 void GetAndClear(CSerializeData &d) {
412 d.insert(d.end(), begin(), end());
413 clear();
417 * XOR the contents of this stream with a certain key.
419 * @param[in] key The key used to XOR the data in this stream.
421 void Xor(const std::vector<unsigned char>& key)
423 if (key.size() == 0) {
424 return;
427 for (size_type i = 0, j = 0; i != size(); i++) {
428 vch[i] ^= key[j++];
430 // This potentially acts on very many bytes of data, so it's
431 // important that we calculate `j`, i.e. the `key` index in this
432 // way instead of doing a %, which would effectively be a division
433 // for each byte Xor'd -- much slower than need be.
434 if (j == key.size())
435 j = 0;
449 /** Non-refcounted RAII wrapper for FILE*
451 * Will automatically close the file when it goes out of scope if not null.
452 * If you're returning the file pointer, return file.release().
453 * If you need to close the file early, use file.fclose() instead of fclose(file).
455 class CAutoFile
457 private:
458 // Disallow copies
459 CAutoFile(const CAutoFile&);
460 CAutoFile& operator=(const CAutoFile&);
462 const int nType;
463 const int nVersion;
465 FILE* file;
467 public:
468 CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
470 file = filenew;
473 ~CAutoFile()
475 fclose();
478 void fclose()
480 if (file) {
481 ::fclose(file);
482 file = nullptr;
486 /** Get wrapped FILE* with transfer of ownership.
487 * @note This will invalidate the CAutoFile object, and makes it the responsibility of the caller
488 * of this function to clean up the returned FILE*.
490 FILE* release() { FILE* ret = file; file = nullptr; return ret; }
492 /** Get wrapped FILE* without transfer of ownership.
493 * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
494 * CAutoFile outlives use of the passed pointer.
496 FILE* Get() const { return file; }
498 /** Return true if the wrapped FILE* is nullptr, false otherwise.
500 bool IsNull() const { return (file == nullptr); }
503 // Stream subset
505 int GetType() const { return nType; }
506 int GetVersion() const { return nVersion; }
508 void read(char* pch, size_t nSize)
510 if (!file)
511 throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
512 if (fread(pch, 1, nSize, file) != nSize)
513 throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
516 void ignore(size_t nSize)
518 if (!file)
519 throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
520 unsigned char data[4096];
521 while (nSize > 0) {
522 size_t nNow = std::min<size_t>(nSize, sizeof(data));
523 if (fread(data, 1, nNow, file) != nNow)
524 throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
525 nSize -= nNow;
529 void write(const char* pch, size_t nSize)
531 if (!file)
532 throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
533 if (fwrite(pch, 1, nSize, file) != nSize)
534 throw std::ios_base::failure("CAutoFile::write: write failed");
537 template<typename T>
538 CAutoFile& operator<<(const T& obj)
540 // Serialize to this stream
541 if (!file)
542 throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
543 ::Serialize(*this, obj);
544 return (*this);
547 template<typename T>
548 CAutoFile& operator>>(T& obj)
550 // Unserialize from this stream
551 if (!file)
552 throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
553 ::Unserialize(*this, obj);
554 return (*this);
558 /** Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to
559 * deserialize from. It guarantees the ability to rewind a given number of bytes.
561 * Will automatically close the file when it goes out of scope if not null.
562 * If you need to close the file early, use file.fclose() instead of fclose(file).
564 class CBufferedFile
566 private:
567 // Disallow copies
568 CBufferedFile(const CBufferedFile&);
569 CBufferedFile& operator=(const CBufferedFile&);
571 const int nType;
572 const int nVersion;
574 FILE *src; // source file
575 uint64_t nSrcPos; // how many bytes have been read from source
576 uint64_t nReadPos; // how many bytes have been read from this
577 uint64_t nReadLimit; // up to which position we're allowed to read
578 uint64_t nRewind; // how many bytes we guarantee to rewind
579 std::vector<char> vchBuf; // the buffer
581 protected:
582 // read data from the source to fill the buffer
583 bool Fill() {
584 unsigned int pos = nSrcPos % vchBuf.size();
585 unsigned int readNow = vchBuf.size() - pos;
586 unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
587 if (nAvail < readNow)
588 readNow = nAvail;
589 if (readNow == 0)
590 return false;
591 size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
592 if (nBytes == 0) {
593 throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
594 } else {
595 nSrcPos += nBytes;
596 return true;
600 public:
601 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
602 nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
604 src = fileIn;
607 ~CBufferedFile()
609 fclose();
612 int GetVersion() const { return nVersion; }
613 int GetType() const { return nType; }
615 void fclose()
617 if (src) {
618 ::fclose(src);
619 src = nullptr;
623 // check whether we're at the end of the source file
624 bool eof() const {
625 return nReadPos == nSrcPos && feof(src);
628 // read a number of bytes
629 void read(char *pch, size_t nSize) {
630 if (nSize + nReadPos > nReadLimit)
631 throw std::ios_base::failure("Read attempted past buffer limit");
632 if (nSize + nRewind > vchBuf.size())
633 throw std::ios_base::failure("Read larger than buffer size");
634 while (nSize > 0) {
635 if (nReadPos == nSrcPos)
636 Fill();
637 unsigned int pos = nReadPos % vchBuf.size();
638 size_t nNow = nSize;
639 if (nNow + pos > vchBuf.size())
640 nNow = vchBuf.size() - pos;
641 if (nNow + nReadPos > nSrcPos)
642 nNow = nSrcPos - nReadPos;
643 memcpy(pch, &vchBuf[pos], nNow);
644 nReadPos += nNow;
645 pch += nNow;
646 nSize -= nNow;
650 // return the current reading position
651 uint64_t GetPos() const {
652 return nReadPos;
655 // rewind to a given reading position
656 bool SetPos(uint64_t nPos) {
657 nReadPos = nPos;
658 if (nReadPos + nRewind < nSrcPos) {
659 nReadPos = nSrcPos - nRewind;
660 return false;
661 } else if (nReadPos > nSrcPos) {
662 nReadPos = nSrcPos;
663 return false;
664 } else {
665 return true;
669 bool Seek(uint64_t nPos) {
670 long nLongPos = nPos;
671 if (nPos != (uint64_t)nLongPos)
672 return false;
673 if (fseek(src, nLongPos, SEEK_SET))
674 return false;
675 nLongPos = ftell(src);
676 nSrcPos = nLongPos;
677 nReadPos = nLongPos;
678 return true;
681 // prevent reading beyond a certain position
682 // no argument removes the limit
683 bool SetLimit(uint64_t nPos = (uint64_t)(-1)) {
684 if (nPos < nReadPos)
685 return false;
686 nReadLimit = nPos;
687 return true;
690 template<typename T>
691 CBufferedFile& operator>>(T& obj) {
692 // Unserialize from this stream
693 ::Unserialize(*this, obj);
694 return (*this);
697 // search for a given byte in the stream, and remain positioned on it
698 void FindByte(char ch) {
699 while (true) {
700 if (nReadPos == nSrcPos)
701 Fill();
702 if (vchBuf[nReadPos % vchBuf.size()] == ch)
703 break;
704 nReadPos++;
709 #endif // BITCOIN_STREAMS_H