• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // InBuffer.h
2 
3 #ifndef __IN_BUFFER_H
4 #define __IN_BUFFER_H
5 
6 #include "../../Common/MyException.h"
7 #include "../IStream.h"
8 
9 #ifndef _NO_EXCEPTIONS
10 struct CInBufferException: public CSystemException
11 {
CInBufferExceptionCInBufferException12   CInBufferException(HRESULT errorCode): CSystemException(errorCode) {}
13 };
14 #endif
15 
16 class CInBufferBase
17 {
18 protected:
19   Byte *_buf;
20   Byte *_bufLim;
21   Byte *_bufBase;
22 
23   ISequentialInStream *_stream;
24   UInt64 _processedSize;
25   size_t _bufSize; // actually it's number of Bytes for next read. The buf can be larger
26                    // only up to 32-bits values now are supported!
27   bool _wasFinished;
28 
29   bool ReadBlock();
30   bool ReadByte_FromNewBlock(Byte &b);
31   Byte ReadByte_FromNewBlock();
32 
33 public:
34   #ifdef _NO_EXCEPTIONS
35   HRESULT ErrorCode;
36   #endif
37   UInt32 NumExtraBytes;
38 
39   CInBufferBase() throw();
40 
GetStreamSize()41   UInt64 GetStreamSize() const { return _processedSize + (_buf - _bufBase); }
GetProcessedSize()42   UInt64 GetProcessedSize() const { return _processedSize + NumExtraBytes + (_buf - _bufBase); }
WasFinished()43   bool WasFinished() const { return _wasFinished; }
44 
SetStream(ISequentialInStream * stream)45   void SetStream(ISequentialInStream *stream) { _stream = stream; }
46 
SetBuf(Byte * buf,size_t bufSize,size_t end,size_t pos)47   void SetBuf(Byte *buf, size_t bufSize, size_t end, size_t pos)
48   {
49     _bufBase = buf;
50     _bufSize = bufSize;
51     _processedSize = 0;
52     _buf = buf + pos;
53     _bufLim = buf + end;
54     _wasFinished = false;
55     #ifdef _NO_EXCEPTIONS
56     ErrorCode = S_OK;
57     #endif
58     NumExtraBytes = 0;
59   }
60 
61   void Init() throw();
62 
ReadByte(Byte & b)63   bool ReadByte(Byte &b)
64   {
65     if (_buf >= _bufLim)
66       return ReadByte_FromNewBlock(b);
67     b = *_buf++;
68     return true;
69   }
70 
ReadByte()71   Byte ReadByte()
72   {
73     if (_buf >= _bufLim)
74       return ReadByte_FromNewBlock();
75     return *_buf++;
76   }
77 
78   size_t ReadBytes(Byte *buf, size_t size);
79   size_t Skip(size_t size);
80 };
81 
82 class CInBuffer: public CInBufferBase
83 {
84 public:
~CInBuffer()85   ~CInBuffer() { Free(); }
86   bool Create(size_t bufSize) throw(); // only up to 32-bits values now are supported!
87   void Free() throw();
88 };
89 
90 #endif
91