xref: /aosp_15_r20/external/lzma/CPP/7zip/Common/InBuffer.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // InBuffer.h
2 
3 #ifndef ZIP7_INC_IN_BUFFER_H
4 #define ZIP7_INC_IN_BUFFER_H
5 
6 #include "../../Common/MyException.h"
7 #include "../IStream.h"
8 
9 #ifndef Z7_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 Z7_NO_EXCEPTIONS
35   HRESULT ErrorCode;
36   #endif
37   UInt32 NumExtraBytes;
38 
39   CInBufferBase() throw();
40 
41   // the size of portion of data in real stream that was already read from this object
42   // it doesn't include unused data in buffer
43   // it doesn't include virtual Extra bytes after the end of real stream data
GetStreamSize()44   UInt64 GetStreamSize() const { return _processedSize + (size_t)(_buf - _bufBase); }
45 
46   // the size of virtual data that was read from this object
47   // it doesn't include unused data in buffers
48   // it includes any virtual Extra bytes after the end of real data
GetProcessedSize()49   UInt64 GetProcessedSize() const { return _processedSize + NumExtraBytes + (size_t)(_buf - _bufBase); }
50 
WasFinished()51   bool WasFinished() const { return _wasFinished; }
52 
SetStream(ISequentialInStream * stream)53   void SetStream(ISequentialInStream *stream) { _stream = stream; }
ClearStreamPtr()54   void ClearStreamPtr() { _stream = NULL; }
55 
SetBuf(Byte * buf,size_t bufSize,size_t end,size_t pos)56   void SetBuf(Byte *buf, size_t bufSize, size_t end, size_t pos)
57   {
58     _bufBase = buf;
59     _bufSize = bufSize;
60     _processedSize = 0;
61     _buf = buf + pos;
62     _bufLim = buf + end;
63     _wasFinished = false;
64     #ifdef Z7_NO_EXCEPTIONS
65     ErrorCode = S_OK;
66     #endif
67     NumExtraBytes = 0;
68   }
69 
70   void Init() throw();
71 
72   Z7_FORCE_INLINE
ReadByte(Byte & b)73   bool ReadByte(Byte &b)
74   {
75     if (_buf >= _bufLim)
76       return ReadByte_FromNewBlock(b);
77     b = *_buf++;
78     return true;
79   }
80 
81   Z7_FORCE_INLINE
ReadByte_FromBuf(Byte & b)82   bool ReadByte_FromBuf(Byte &b)
83   {
84     if (_buf >= _bufLim)
85       return false;
86     b = *_buf++;
87     return true;
88   }
89 
90   Z7_FORCE_INLINE
ReadByte()91   Byte ReadByte()
92   {
93     if (_buf >= _bufLim)
94       return ReadByte_FromNewBlock();
95     return *_buf++;
96   }
97 
98   size_t ReadBytesPart(Byte *buf, size_t size);
99   size_t ReadBytes(Byte *buf, size_t size);
100   size_t Skip(size_t size);
101 };
102 
103 class CInBuffer: public CInBufferBase
104 {
105 public:
~CInBuffer()106   ~CInBuffer() { Free(); }
107   bool Create(size_t bufSize) throw(); // only up to 32-bits values now are supported!
108   void Free() throw();
109 };
110 
111 #endif
112