5 #include "LZInWindow.h"
6 #include "../../../Common/MyCom.h"
7 #include "../../../Common/Alloc.h"
9 void CLZInWindow::Free()
11 ::BigFree(_bufferBase);
15 bool CLZInWindow::Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv)
17 _keepSizeBefore = keepSizeBefore;
18 _keepSizeAfter = keepSizeAfter;
19 UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
20 if (_bufferBase == 0 || _blockSize != blockSize)
23 _blockSize = blockSize;
25 _bufferBase = (Byte *)::BigAlloc(_blockSize);
27 _pointerToLastSafePosition = _bufferBase + _blockSize - keepSizeAfter;
30 return (_bufferBase != 0);
33 void CLZInWindow::SetStream(ISequentialInStream *stream)
38 HRESULT CLZInWindow::Init()
40 _buffer = _bufferBase;
43 _streamEndWasReached = false;
48 void CLZInWindow::ReleaseStream()
54 ///////////////////////////////////////////
58 // (_buffer + _streamPos) <= (_bufferBase + _blockSize)
60 // _posLimit <= _blockSize - _keepSizeAfter;
61 // if(_streamEndWasReached == false):
62 // _streamPos >= _pos + _keepSizeAfter
63 // _posLimit = _streamPos - _keepSizeAfter;
67 HRESULT CLZInWindow::ReadBlock()
69 if(_streamEndWasReached)
73 UInt32 size = (UInt32)(_bufferBase - _buffer) + _blockSize - _streamPos;
77 RINOK(_stream->Read(_buffer + _streamPos, size, &numReadBytes));
80 _posLimit = _streamPos;
81 const Byte *pointerToPostion = _buffer + _posLimit;
82 if(pointerToPostion > _pointerToLastSafePosition)
83 _posLimit = (UInt32)(_pointerToLastSafePosition - _buffer);
84 _streamEndWasReached = true;
87 _streamPos += numReadBytes;
88 if(_streamPos >= _pos + _keepSizeAfter)
90 _posLimit = _streamPos - _keepSizeAfter;
96 void CLZInWindow::MoveBlock()
98 UInt32 offset = (UInt32)(_buffer - _bufferBase) + _pos - _keepSizeBefore;
99 // we need one additional byte, since MovePos moves on 1 byte.
102 UInt32 numBytes = (UInt32)(_buffer - _bufferBase) + _streamPos - offset;
103 memmove(_bufferBase, _bufferBase + offset, numBytes);