Fix ZMQ Notification initialization and shutdown
[bitcoinplatinum.git] / src / streams.h
blob8610e4d18e612b71a7ae1501153f356275dd8864
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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 /** Double ended buffer combining vector and stream-like interfaces.
27 * >> and << read and write unformatted data using the above serialization templates.
28 * Fills with data in linear time; some stringstream implementations take N^2 time.
30 class CDataStream
32 protected:
33 typedef CSerializeData vector_type;
34 vector_type vch;
35 unsigned int nReadPos;
36 public:
37 int nType;
38 int nVersion;
40 typedef vector_type::allocator_type allocator_type;
41 typedef vector_type::size_type size_type;
42 typedef vector_type::difference_type difference_type;
43 typedef vector_type::reference reference;
44 typedef vector_type::const_reference const_reference;
45 typedef vector_type::value_type value_type;
46 typedef vector_type::iterator iterator;
47 typedef vector_type::const_iterator const_iterator;
48 typedef vector_type::reverse_iterator reverse_iterator;
50 explicit CDataStream(int nTypeIn, int nVersionIn)
52 Init(nTypeIn, nVersionIn);
55 CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
57 Init(nTypeIn, nVersionIn);
60 #if !defined(_MSC_VER) || _MSC_VER >= 1300
61 CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
63 Init(nTypeIn, nVersionIn);
65 #endif
67 CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
69 Init(nTypeIn, nVersionIn);
72 CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
74 Init(nTypeIn, nVersionIn);
77 CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
79 Init(nTypeIn, nVersionIn);
82 void Init(int nTypeIn, int nVersionIn)
84 nReadPos = 0;
85 nType = nTypeIn;
86 nVersion = nVersionIn;
89 CDataStream& operator+=(const CDataStream& b)
91 vch.insert(vch.end(), b.begin(), b.end());
92 return *this;
95 friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
97 CDataStream ret = a;
98 ret += b;
99 return (ret);
102 std::string str() const
104 return (std::string(begin(), end()));
109 // Vector subset
111 const_iterator begin() const { return vch.begin() + nReadPos; }
112 iterator begin() { return vch.begin() + nReadPos; }
113 const_iterator end() const { return vch.end(); }
114 iterator end() { return vch.end(); }
115 size_type size() const { return vch.size() - nReadPos; }
116 bool empty() const { return vch.size() == nReadPos; }
117 void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
118 void reserve(size_type n) { vch.reserve(n + nReadPos); }
119 const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
120 reference operator[](size_type pos) { return vch[pos + nReadPos]; }
121 void clear() { vch.clear(); nReadPos = 0; }
122 iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
123 void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
125 void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
127 assert(last - first >= 0);
128 if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
130 // special case for inserting at the front when there's room
131 nReadPos -= (last - first);
132 memcpy(&vch[nReadPos], &first[0], last - first);
134 else
135 vch.insert(it, first, last);
138 #if !defined(_MSC_VER) || _MSC_VER >= 1300
139 void insert(iterator it, const char* first, const char* last)
141 assert(last - first >= 0);
142 if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
144 // special case for inserting at the front when there's room
145 nReadPos -= (last - first);
146 memcpy(&vch[nReadPos], &first[0], last - first);
148 else
149 vch.insert(it, first, last);
151 #endif
153 iterator erase(iterator it)
155 if (it == vch.begin() + nReadPos)
157 // special case for erasing from the front
158 if (++nReadPos >= vch.size())
160 // whenever we reach the end, we take the opportunity to clear the buffer
161 nReadPos = 0;
162 return vch.erase(vch.begin(), vch.end());
164 return vch.begin() + nReadPos;
166 else
167 return vch.erase(it);
170 iterator erase(iterator first, iterator last)
172 if (first == vch.begin() + nReadPos)
174 // special case for erasing from the front
175 if (last == vch.end())
177 nReadPos = 0;
178 return vch.erase(vch.begin(), vch.end());
180 else
182 nReadPos = (last - vch.begin());
183 return last;
186 else
187 return vch.erase(first, last);
190 inline void Compact()
192 vch.erase(vch.begin(), vch.begin() + nReadPos);
193 nReadPos = 0;
196 bool Rewind(size_type n)
198 // Rewind by n characters if the buffer hasn't been compacted yet
199 if (n > nReadPos)
200 return false;
201 nReadPos -= n;
202 return true;
207 // Stream subset
209 bool eof() const { return size() == 0; }
210 CDataStream* rdbuf() { return this; }
211 int in_avail() { return size(); }
213 void SetType(int n) { nType = n; }
214 int GetType() { return nType; }
215 void SetVersion(int n) { nVersion = n; }
216 int GetVersion() { return nVersion; }
217 void ReadVersion() { *this >> nVersion; }
218 void WriteVersion() { *this << nVersion; }
220 CDataStream& read(char* pch, size_t nSize)
222 // Read from the beginning of the buffer
223 unsigned int nReadPosNext = nReadPos + nSize;
224 if (nReadPosNext >= vch.size())
226 if (nReadPosNext > vch.size())
228 throw std::ios_base::failure("CDataStream::read(): end of data");
230 memcpy(pch, &vch[nReadPos], nSize);
231 nReadPos = 0;
232 vch.clear();
233 return (*this);
235 memcpy(pch, &vch[nReadPos], nSize);
236 nReadPos = nReadPosNext;
237 return (*this);
240 CDataStream& ignore(int nSize)
242 // Ignore from the beginning of the buffer
243 assert(nSize >= 0);
244 unsigned int nReadPosNext = nReadPos + nSize;
245 if (nReadPosNext >= vch.size())
247 if (nReadPosNext > vch.size())
248 throw std::ios_base::failure("CDataStream::ignore(): end of data");
249 nReadPos = 0;
250 vch.clear();
251 return (*this);
253 nReadPos = nReadPosNext;
254 return (*this);
257 CDataStream& write(const char* pch, size_t nSize)
259 // Write to the end of the buffer
260 vch.insert(vch.end(), pch, pch + nSize);
261 return (*this);
264 template<typename Stream>
265 void Serialize(Stream& s, int nType, int nVersion) const
267 // Special case: stream << stream concatenates like stream += stream
268 if (!vch.empty())
269 s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
272 template<typename T>
273 unsigned int GetSerializeSize(const T& obj)
275 // Tells the size of the object if serialized to this stream
276 return ::GetSerializeSize(obj, nType, nVersion);
279 template<typename T>
280 CDataStream& operator<<(const T& obj)
282 // Serialize to this stream
283 ::Serialize(*this, obj, nType, nVersion);
284 return (*this);
287 template<typename T>
288 CDataStream& operator>>(T& obj)
290 // Unserialize from this stream
291 ::Unserialize(*this, obj, nType, nVersion);
292 return (*this);
295 void GetAndClear(CSerializeData &data) {
296 data.insert(data.end(), begin(), end());
297 clear();
301 * XOR the contents of this stream with a certain key.
303 * @param[in] key The key used to XOR the data in this stream.
305 void Xor(const std::vector<unsigned char>& key)
307 if (key.size() == 0) {
308 return;
311 for (size_type i = 0, j = 0; i != size(); i++) {
312 vch[i] ^= key[j++];
314 // This potentially acts on very many bytes of data, so it's
315 // important that we calculate `j`, i.e. the `key` index in this
316 // way instead of doing a %, which would effectively be a division
317 // for each byte Xor'd -- much slower than need be.
318 if (j == key.size())
319 j = 0;
333 /** Non-refcounted RAII wrapper for FILE*
335 * Will automatically close the file when it goes out of scope if not null.
336 * If you're returning the file pointer, return file.release().
337 * If you need to close the file early, use file.fclose() instead of fclose(file).
339 class CAutoFile
341 private:
342 // Disallow copies
343 CAutoFile(const CAutoFile&);
344 CAutoFile& operator=(const CAutoFile&);
346 int nType;
347 int nVersion;
349 FILE* file;
351 public:
352 CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
354 file = filenew;
355 nType = nTypeIn;
356 nVersion = nVersionIn;
359 ~CAutoFile()
361 fclose();
364 void fclose()
366 if (file) {
367 ::fclose(file);
368 file = NULL;
372 /** Get wrapped FILE* with transfer of ownership.
373 * @note This will invalidate the CAutoFile object, and makes it the responsibility of the caller
374 * of this function to clean up the returned FILE*.
376 FILE* release() { FILE* ret = file; file = NULL; return ret; }
378 /** Get wrapped FILE* without transfer of ownership.
379 * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
380 * CAutoFile outlives use of the passed pointer.
382 FILE* Get() const { return file; }
384 /** Return true if the wrapped FILE* is NULL, false otherwise.
386 bool IsNull() const { return (file == NULL); }
389 // Stream subset
391 void SetType(int n) { nType = n; }
392 int GetType() { return nType; }
393 void SetVersion(int n) { nVersion = n; }
394 int GetVersion() { return nVersion; }
395 void ReadVersion() { *this >> nVersion; }
396 void WriteVersion() { *this << nVersion; }
398 CAutoFile& read(char* pch, size_t nSize)
400 if (!file)
401 throw std::ios_base::failure("CAutoFile::read: file handle is NULL");
402 if (fread(pch, 1, nSize, file) != nSize)
403 throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
404 return (*this);
407 CAutoFile& write(const char* pch, size_t nSize)
409 if (!file)
410 throw std::ios_base::failure("CAutoFile::write: file handle is NULL");
411 if (fwrite(pch, 1, nSize, file) != nSize)
412 throw std::ios_base::failure("CAutoFile::write: write failed");
413 return (*this);
416 template<typename T>
417 unsigned int GetSerializeSize(const T& obj)
419 // Tells the size of the object if serialized to this stream
420 return ::GetSerializeSize(obj, nType, nVersion);
423 template<typename T>
424 CAutoFile& operator<<(const T& obj)
426 // Serialize to this stream
427 if (!file)
428 throw std::ios_base::failure("CAutoFile::operator<<: file handle is NULL");
429 ::Serialize(*this, obj, nType, nVersion);
430 return (*this);
433 template<typename T>
434 CAutoFile& operator>>(T& obj)
436 // Unserialize from this stream
437 if (!file)
438 throw std::ios_base::failure("CAutoFile::operator>>: file handle is NULL");
439 ::Unserialize(*this, obj, nType, nVersion);
440 return (*this);
444 /** Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to
445 * deserialize from. It guarantees the ability to rewind a given number of bytes.
447 * Will automatically close the file when it goes out of scope if not null.
448 * If you need to close the file early, use file.fclose() instead of fclose(file).
450 class CBufferedFile
452 private:
453 // Disallow copies
454 CBufferedFile(const CBufferedFile&);
455 CBufferedFile& operator=(const CBufferedFile&);
457 int nType;
458 int nVersion;
460 FILE *src; // source file
461 uint64_t nSrcPos; // how many bytes have been read from source
462 uint64_t nReadPos; // how many bytes have been read from this
463 uint64_t nReadLimit; // up to which position we're allowed to read
464 uint64_t nRewind; // how many bytes we guarantee to rewind
465 std::vector<char> vchBuf; // the buffer
467 protected:
468 // read data from the source to fill the buffer
469 bool Fill() {
470 unsigned int pos = nSrcPos % vchBuf.size();
471 unsigned int readNow = vchBuf.size() - pos;
472 unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
473 if (nAvail < readNow)
474 readNow = nAvail;
475 if (readNow == 0)
476 return false;
477 size_t read = fread((void*)&vchBuf[pos], 1, readNow, src);
478 if (read == 0) {
479 throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
480 } else {
481 nSrcPos += read;
482 return true;
486 public:
487 CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
488 nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
490 src = fileIn;
491 nType = nTypeIn;
492 nVersion = nVersionIn;
495 ~CBufferedFile()
497 fclose();
500 void fclose()
502 if (src) {
503 ::fclose(src);
504 src = NULL;
508 // check whether we're at the end of the source file
509 bool eof() const {
510 return nReadPos == nSrcPos && feof(src);
513 // read a number of bytes
514 CBufferedFile& read(char *pch, size_t nSize) {
515 if (nSize + nReadPos > nReadLimit)
516 throw std::ios_base::failure("Read attempted past buffer limit");
517 if (nSize + nRewind > vchBuf.size())
518 throw std::ios_base::failure("Read larger than buffer size");
519 while (nSize > 0) {
520 if (nReadPos == nSrcPos)
521 Fill();
522 unsigned int pos = nReadPos % vchBuf.size();
523 size_t nNow = nSize;
524 if (nNow + pos > vchBuf.size())
525 nNow = vchBuf.size() - pos;
526 if (nNow + nReadPos > nSrcPos)
527 nNow = nSrcPos - nReadPos;
528 memcpy(pch, &vchBuf[pos], nNow);
529 nReadPos += nNow;
530 pch += nNow;
531 nSize -= nNow;
533 return (*this);
536 // return the current reading position
537 uint64_t GetPos() {
538 return nReadPos;
541 // rewind to a given reading position
542 bool SetPos(uint64_t nPos) {
543 nReadPos = nPos;
544 if (nReadPos + nRewind < nSrcPos) {
545 nReadPos = nSrcPos - nRewind;
546 return false;
547 } else if (nReadPos > nSrcPos) {
548 nReadPos = nSrcPos;
549 return false;
550 } else {
551 return true;
555 bool Seek(uint64_t nPos) {
556 long nLongPos = nPos;
557 if (nPos != (uint64_t)nLongPos)
558 return false;
559 if (fseek(src, nLongPos, SEEK_SET))
560 return false;
561 nLongPos = ftell(src);
562 nSrcPos = nLongPos;
563 nReadPos = nLongPos;
564 return true;
567 // prevent reading beyond a certain position
568 // no argument removes the limit
569 bool SetLimit(uint64_t nPos = (uint64_t)(-1)) {
570 if (nPos < nReadPos)
571 return false;
572 nReadLimit = nPos;
573 return true;
576 template<typename T>
577 CBufferedFile& operator>>(T& obj) {
578 // Unserialize from this stream
579 ::Unserialize(*this, obj, nType, nVersion);
580 return (*this);
583 // search for a given byte in the stream, and remain positioned on it
584 void FindByte(char ch) {
585 while (true) {
586 if (nReadPos == nSrcPos)
587 Fill();
588 if (vchBuf[nReadPos % vchBuf.size()] == ch)
589 break;
590 nReadPos++;
595 #endif // BITCOIN_STREAMS_H