-// LZInWindow.h\r
-\r
-#ifndef __LZ_IN_WINDOW_H\r
-#define __LZ_IN_WINDOW_H\r
-\r
-#include "../../IStream.h"\r
-\r
-class CLZInWindow\r
-{\r
- Byte *_bufferBase; // pointer to buffer with data\r
- ISequentialInStream *_stream;\r
- UInt32 _posLimit; // offset (from _buffer) when new block reading must be done\r
- bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream\r
- const Byte *_pointerToLastSafePosition;\r
-protected:\r
- Byte *_buffer; // Pointer to virtual Buffer begin\r
- UInt32 _blockSize; // Size of Allocated memory block\r
- UInt32 _pos; // offset (from _buffer) of curent byte\r
- UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos\r
- UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos\r
- UInt32 _streamPos; // offset (from _buffer) of first not read byte from Stream\r
-\r
- void MoveBlock();\r
- HRESULT ReadBlock();\r
- void Free();\r
-public:\r
- CLZInWindow(): _bufferBase(0) {}\r
- virtual ~CLZInWindow() { Free(); }\r
-\r
- // keepSizeBefore + keepSizeAfter + keepSizeReserv < 4G)\r
- bool Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv = (1<<17));\r
-\r
- void SetStream(ISequentialInStream *stream);\r
- HRESULT Init();\r
- // void ReleaseStream();\r
-\r
- Byte *GetBuffer() const { return _buffer; }\r
-\r
- const Byte *GetPointerToCurrentPos() const { return _buffer + _pos; }\r
-\r
- HRESULT MovePos()\r
- {\r
- _pos++;\r
- if (_pos > _posLimit)\r
- {\r
- const Byte *pointerToPostion = _buffer + _pos;\r
- if(pointerToPostion > _pointerToLastSafePosition)\r
- MoveBlock();\r
- return ReadBlock();\r
- }\r
- else\r
- return S_OK;\r
- }\r
- Byte GetIndexByte(Int32 index) const { return _buffer[(size_t)_pos + index]; }\r
-\r
- // index + limit have not to exceed _keepSizeAfter;\r
- // -2G <= index < 2G\r
- UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit) const\r
- { \r
- if(_streamEndWasReached)\r
- if ((_pos + index) + limit > _streamPos)\r
- limit = _streamPos - (_pos + index);\r
- distance++;\r
- const Byte *pby = _buffer + (size_t)_pos + index;\r
- UInt32 i;\r
- for(i = 0; i < limit && pby[i] == pby[(size_t)i - distance]; i++);\r
- return i;\r
- }\r
-\r
- UInt32 GetNumAvailableBytes() const { return _streamPos - _pos; }\r
-\r
- void ReduceOffsets(Int32 subValue)\r
- {\r
- _buffer += subValue;\r
- _posLimit -= subValue;\r
- _pos -= subValue;\r
- _streamPos -= subValue;\r
- }\r
-\r
- bool NeedMove(UInt32 numCheckBytes)\r
- {\r
- UInt32 reserv = _pointerToLastSafePosition - (_buffer + _pos);\r
- return (reserv <= numCheckBytes);\r
- }\r
-};\r
-\r
-#endif\r
+// LZInWindow.h
+
+#ifndef __LZ_IN_WINDOW_H
+#define __LZ_IN_WINDOW_H
+
+#include "../../IStream.h"
+
+class CLZInWindow
+{
+ Byte *_bufferBase; // pointer to buffer with data
+ ISequentialInStream *_stream;
+ UInt32 _posLimit; // offset (from _buffer) when new block reading must be done
+ bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream
+ const Byte *_pointerToLastSafePosition;
+protected:
+ Byte *_buffer; // Pointer to virtual Buffer begin
+ UInt32 _blockSize; // Size of Allocated memory block
+ UInt32 _pos; // offset (from _buffer) of curent byte
+ UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos
+ UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos
+ UInt32 _streamPos; // offset (from _buffer) of first not read byte from Stream
+
+ void MoveBlock();
+ HRESULT ReadBlock();
+ void Free();
+public:
+ CLZInWindow(): _bufferBase(0) {}
+ virtual ~CLZInWindow() { Free(); }
+
+ // keepSizeBefore + keepSizeAfter + keepSizeReserv < 4G)
+ bool Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv = (1<<17));
+
+ void SetStream(ISequentialInStream *stream);
+ HRESULT Init();
+ // void ReleaseStream();
+
+ Byte *GetBuffer() const { return _buffer; }
+
+ const Byte *GetPointerToCurrentPos() const { return _buffer + _pos; }
+
+ HRESULT MovePos()
+ {
+ _pos++;
+ if (_pos > _posLimit)
+ {
+ const Byte *pointerToPostion = _buffer + _pos;
+ if(pointerToPostion > _pointerToLastSafePosition)
+ MoveBlock();
+ return ReadBlock();
+ }
+ else
+ return S_OK;
+ }
+ Byte GetIndexByte(Int32 index) const { return _buffer[(size_t)_pos + index]; }
+
+ // index + limit have not to exceed _keepSizeAfter;
+ // -2G <= index < 2G
+ UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit) const
+ {
+ if(_streamEndWasReached)
+ if ((_pos + index) + limit > _streamPos)
+ limit = _streamPos - (_pos + index);
+ distance++;
+ const Byte *pby = _buffer + (size_t)_pos + index;
+ UInt32 i;
+ for(i = 0; i < limit && pby[i] == pby[(size_t)i - distance]; i++);
+ return i;
+ }
+
+ UInt32 GetNumAvailableBytes() const { return _streamPos - _pos; }
+
+ void ReduceOffsets(Int32 subValue)
+ {
+ _buffer += subValue;
+ _posLimit -= subValue;
+ _pos -= subValue;
+ _streamPos -= subValue;
+ }
+
+ bool NeedMove(UInt32 numCheckBytes)
+ {
+ UInt32 reserv = _pointerToLastSafePosition - (_buffer + _pos);
+ return (reserv <= numCheckBytes);
+ }
+};
+
+#endif