]> Git Repo - VerusCoin.git/blob - src/streams.h
Merge pull request #5360
[VerusCoin.git] / src / streams.h
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.
5
6 #ifndef BITCOIN_STREAMS_H
7 #define BITCOIN_STREAMS_H
8
9 #include "allocators.h"
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 /** Double ended buffer combining vector and stream-like interfaces.
26  *
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.
29  */
30 class CDataStream
31 {
32 protected:
33     typedef CSerializeData vector_type;
34     vector_type vch;
35     unsigned int nReadPos;
36 public:
37     int nType;
38     int nVersion;
39
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;
49
50     explicit CDataStream(int nTypeIn, int nVersionIn)
51     {
52         Init(nTypeIn, nVersionIn);
53     }
54
55     CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
56     {
57         Init(nTypeIn, nVersionIn);
58     }
59
60 #if !defined(_MSC_VER) || _MSC_VER >= 1300
61     CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
62     {
63         Init(nTypeIn, nVersionIn);
64     }
65 #endif
66
67     CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
68     {
69         Init(nTypeIn, nVersionIn);
70     }
71
72     CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
73     {
74         Init(nTypeIn, nVersionIn);
75     }
76
77     CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
78     {
79         Init(nTypeIn, nVersionIn);
80     }
81
82     void Init(int nTypeIn, int nVersionIn)
83     {
84         nReadPos = 0;
85         nType = nTypeIn;
86         nVersion = nVersionIn;
87     }
88
89     CDataStream& operator+=(const CDataStream& b)
90     {
91         vch.insert(vch.end(), b.begin(), b.end());
92         return *this;
93     }
94
95     friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
96     {
97         CDataStream ret = a;
98         ret += b;
99         return (ret);
100     }
101
102     std::string str() const
103     {
104         return (std::string(begin(), end()));
105     }
106
107
108     //
109     // Vector subset
110     //
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); }
124
125     void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
126     {
127         assert(last - first >= 0);
128         if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
129         {
130             // special case for inserting at the front when there's room
131             nReadPos -= (last - first);
132             memcpy(&vch[nReadPos], &first[0], last - first);
133         }
134         else
135             vch.insert(it, first, last);
136     }
137
138 #if !defined(_MSC_VER) || _MSC_VER >= 1300
139     void insert(iterator it, const char* first, const char* last)
140     {
141         assert(last - first >= 0);
142         if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
143         {
144             // special case for inserting at the front when there's room
145             nReadPos -= (last - first);
146             memcpy(&vch[nReadPos], &first[0], last - first);
147         }
148         else
149             vch.insert(it, first, last);
150     }
151 #endif
152
153     iterator erase(iterator it)
154     {
155         if (it == vch.begin() + nReadPos)
156         {
157             // special case for erasing from the front
158             if (++nReadPos >= vch.size())
159             {
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());
163             }
164             return vch.begin() + nReadPos;
165         }
166         else
167             return vch.erase(it);
168     }
169
170     iterator erase(iterator first, iterator last)
171     {
172         if (first == vch.begin() + nReadPos)
173         {
174             // special case for erasing from the front
175             if (last == vch.end())
176             {
177                 nReadPos = 0;
178                 return vch.erase(vch.begin(), vch.end());
179             }
180             else
181             {
182                 nReadPos = (last - vch.begin());
183                 return last;
184             }
185         }
186         else
187             return vch.erase(first, last);
188     }
189
190     inline void Compact()
191     {
192         vch.erase(vch.begin(), vch.begin() + nReadPos);
193         nReadPos = 0;
194     }
195
196     bool Rewind(size_type n)
197     {
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;
203     }
204
205
206     //
207     // Stream subset
208     //
209     bool eof() const             { return size() == 0; }
210     CDataStream* rdbuf()         { return this; }
211     int in_avail()               { return size(); }
212
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; }
219
220     CDataStream& read(char* pch, size_t nSize)
221     {
222         // Read from the beginning of the buffer
223         unsigned int nReadPosNext = nReadPos + nSize;
224         if (nReadPosNext >= vch.size())
225         {
226             if (nReadPosNext > vch.size())
227             {
228                 throw std::ios_base::failure("CDataStream::read(): end of data");
229             }
230             memcpy(pch, &vch[nReadPos], nSize);
231             nReadPos = 0;
232             vch.clear();
233             return (*this);
234         }
235         memcpy(pch, &vch[nReadPos], nSize);
236         nReadPos = nReadPosNext;
237         return (*this);
238     }
239
240     CDataStream& ignore(int nSize)
241     {
242         // Ignore from the beginning of the buffer
243         assert(nSize >= 0);
244         unsigned int nReadPosNext = nReadPos + nSize;
245         if (nReadPosNext >= vch.size())
246         {
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);
252         }
253         nReadPos = nReadPosNext;
254         return (*this);
255     }
256
257     CDataStream& write(const char* pch, size_t nSize)
258     {
259         // Write to the end of the buffer
260         vch.insert(vch.end(), pch, pch + nSize);
261         return (*this);
262     }
263
264     template<typename Stream>
265     void Serialize(Stream& s, int nType, int nVersion) const
266     {
267         // Special case: stream << stream concatenates like stream += stream
268         if (!vch.empty())
269             s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
270     }
271
272     template<typename T>
273     unsigned int GetSerializeSize(const T& obj)
274     {
275         // Tells the size of the object if serialized to this stream
276         return ::GetSerializeSize(obj, nType, nVersion);
277     }
278
279     template<typename T>
280     CDataStream& operator<<(const T& obj)
281     {
282         // Serialize to this stream
283         ::Serialize(*this, obj, nType, nVersion);
284         return (*this);
285     }
286
287     template<typename T>
288     CDataStream& operator>>(T& obj)
289     {
290         // Unserialize from this stream
291         ::Unserialize(*this, obj, nType, nVersion);
292         return (*this);
293     }
294
295     void GetAndClear(CSerializeData &data) {
296         data.insert(data.end(), begin(), end());
297         clear();
298     }
299 };
300
301
302
303
304
305
306
307
308
309
310 /** Non-refcounted RAII wrapper for FILE*
311  *
312  * Will automatically close the file when it goes out of scope if not null.
313  * If you're returning the file pointer, return file.release().
314  * If you need to close the file early, use file.fclose() instead of fclose(file).
315  */
316 class CAutoFile
317 {
318 private:
319     // Disallow copies
320     CAutoFile(const CAutoFile&);
321     CAutoFile& operator=(const CAutoFile&);
322
323     int nType;
324     int nVersion;
325         
326     FILE* file; 
327
328 public:
329     CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
330     {
331         file = filenew;
332         nType = nTypeIn;
333         nVersion = nVersionIn;
334     }
335
336     ~CAutoFile()
337     {
338         fclose();
339     }
340
341     void fclose()
342     {
343         if (file) {
344             ::fclose(file);
345             file = NULL;
346         }
347     }
348
349     /** Get wrapped FILE* with transfer of ownership.
350      * @note This will invalidate the CAutoFile object, and makes it the responsibility of the caller
351      * of this function to clean up the returned FILE*.
352      */
353     FILE* release()             { FILE* ret = file; file = NULL; return ret; }
354
355     /** Get wrapped FILE* without transfer of ownership.
356      * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
357      * CAutoFile outlives use of the passed pointer.
358      */
359     FILE* Get() const           { return file; }
360
361     /** Return true if the wrapped FILE* is NULL, false otherwise.
362      */
363     bool IsNull() const         { return (file == NULL); }
364
365     //
366     // Stream subset
367     //
368     void SetType(int n)          { nType = n; }
369     int GetType()                { return nType; }
370     void SetVersion(int n)       { nVersion = n; }
371     int GetVersion()             { return nVersion; }
372     void ReadVersion()           { *this >> nVersion; }
373     void WriteVersion()          { *this << nVersion; }
374
375     CAutoFile& read(char* pch, size_t nSize)
376     {
377         if (!file)
378             throw std::ios_base::failure("CAutoFile::read: file handle is NULL");
379         if (fread(pch, 1, nSize, file) != nSize)
380             throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
381         return (*this);
382     }
383
384     CAutoFile& write(const char* pch, size_t nSize)
385     {
386         if (!file)
387             throw std::ios_base::failure("CAutoFile::write: file handle is NULL");
388         if (fwrite(pch, 1, nSize, file) != nSize)
389             throw std::ios_base::failure("CAutoFile::write: write failed");
390         return (*this);
391     }
392
393     template<typename T>
394     unsigned int GetSerializeSize(const T& obj)
395     {
396         // Tells the size of the object if serialized to this stream
397         return ::GetSerializeSize(obj, nType, nVersion);
398     }
399
400     template<typename T>
401     CAutoFile& operator<<(const T& obj)
402     {
403         // Serialize to this stream
404         if (!file)
405             throw std::ios_base::failure("CAutoFile::operator<<: file handle is NULL");
406         ::Serialize(*this, obj, nType, nVersion);
407         return (*this);
408     }
409
410     template<typename T>
411     CAutoFile& operator>>(T& obj)
412     {
413         // Unserialize from this stream
414         if (!file)
415             throw std::ios_base::failure("CAutoFile::operator>>: file handle is NULL");
416         ::Unserialize(*this, obj, nType, nVersion);
417         return (*this);
418     }
419 };
420
421 /** Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to
422  *  deserialize from. It guarantees the ability to rewind a given number of bytes.
423  *
424  *  Will automatically close the file when it goes out of scope if not null.
425  *  If you need to close the file early, use file.fclose() instead of fclose(file).
426  */
427 class CBufferedFile
428 {
429 private:
430     // Disallow copies
431     CBufferedFile(const CBufferedFile&);
432     CBufferedFile& operator=(const CBufferedFile&);
433
434     int nType;
435     int nVersion;
436
437     FILE *src;            // source file
438     uint64_t nSrcPos;     // how many bytes have been read from source
439     uint64_t nReadPos;    // how many bytes have been read from this
440     uint64_t nReadLimit;  // up to which position we're allowed to read
441     uint64_t nRewind;     // how many bytes we guarantee to rewind
442     std::vector<char> vchBuf; // the buffer
443
444 protected:
445     // read data from the source to fill the buffer
446     bool Fill() {
447         unsigned int pos = nSrcPos % vchBuf.size();
448         unsigned int readNow = vchBuf.size() - pos;
449         unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
450         if (nAvail < readNow)
451             readNow = nAvail;
452         if (readNow == 0)
453             return false;
454         size_t read = fread((void*)&vchBuf[pos], 1, readNow, src);
455         if (read == 0) {
456             throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
457         } else {
458             nSrcPos += read;
459             return true;
460         }
461     }
462
463 public:
464     CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
465         nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
466     {
467         src = fileIn;
468         nType = nTypeIn;
469         nVersion = nVersionIn;
470     }
471
472     ~CBufferedFile()
473     {
474         fclose();
475     }
476
477     void fclose()
478     {
479         if (src) {
480             ::fclose(src);
481             src = NULL;
482         }
483     }
484
485     // check whether we're at the end of the source file
486     bool eof() const {
487         return nReadPos == nSrcPos && feof(src);
488     }
489
490     // read a number of bytes
491     CBufferedFile& read(char *pch, size_t nSize) {
492         if (nSize + nReadPos > nReadLimit)
493             throw std::ios_base::failure("Read attempted past buffer limit");
494         if (nSize + nRewind > vchBuf.size())
495             throw std::ios_base::failure("Read larger than buffer size");
496         while (nSize > 0) {
497             if (nReadPos == nSrcPos)
498                 Fill();
499             unsigned int pos = nReadPos % vchBuf.size();
500             size_t nNow = nSize;
501             if (nNow + pos > vchBuf.size())
502                 nNow = vchBuf.size() - pos;
503             if (nNow + nReadPos > nSrcPos)
504                 nNow = nSrcPos - nReadPos;
505             memcpy(pch, &vchBuf[pos], nNow);
506             nReadPos += nNow;
507             pch += nNow;
508             nSize -= nNow;
509         }
510         return (*this);
511     }
512
513     // return the current reading position
514     uint64_t GetPos() {
515         return nReadPos;
516     }
517
518     // rewind to a given reading position
519     bool SetPos(uint64_t nPos) {
520         nReadPos = nPos;
521         if (nReadPos + nRewind < nSrcPos) {
522             nReadPos = nSrcPos - nRewind;
523             return false;
524         } else if (nReadPos > nSrcPos) {
525             nReadPos = nSrcPos;
526             return false;
527         } else {
528             return true;
529         }
530     }
531
532     bool Seek(uint64_t nPos) {
533         long nLongPos = nPos;
534         if (nPos != (uint64_t)nLongPos)
535             return false;
536         if (fseek(src, nLongPos, SEEK_SET))
537             return false;
538         nLongPos = ftell(src);
539         nSrcPos = nLongPos;
540         nReadPos = nLongPos;
541         return true;
542     }
543
544     // prevent reading beyond a certain position
545     // no argument removes the limit
546     bool SetLimit(uint64_t nPos = (uint64_t)(-1)) {
547         if (nPos < nReadPos)
548             return false;
549         nReadLimit = nPos;
550         return true;
551     }
552
553     template<typename T>
554     CBufferedFile& operator>>(T& obj) {
555         // Unserialize from this stream
556         ::Unserialize(*this, obj, nType, nVersion);
557         return (*this);
558     }
559
560     // search for a given byte in the stream, and remain positioned on it
561     void FindByte(char ch) {
562         while (true) {
563             if (nReadPos == nSrcPos)
564                 Fill();
565             if (vchBuf[nReadPos % vchBuf.size()] == ch)
566                 break;
567             nReadPos++;
568         }
569     }
570 };
571
572 #endif // BITCOIN_STREAMS_H
This page took 0.053373 seconds and 4 git commands to generate.