RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / scripts / squashfs / lzma / C / 7zip / Compress / LZ / LZInWindow.cpp
bloba7b7a3872ae5a150dbca159f445e3002dda620a2
1 // LZInWindow.cpp
3 #include "StdAfx.h"
5 #include "LZInWindow.h"
6 #include "../../../Common/MyCom.h"
7 #include "../../../Common/Alloc.h"
9 void CLZInWindow::Free()
11 ::BigFree(_bufferBase);
12 _bufferBase = 0;
15 bool CLZInWindow::Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv)
17 _keepSizeBefore = keepSizeBefore;
18 _keepSizeAfter = keepSizeAfter;
19 _keepSizeReserv = keepSizeReserv;
20 UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
21 if (_bufferBase == 0 || _blockSize != blockSize)
23 Free();
24 _blockSize = blockSize;
25 if (_blockSize != 0)
26 _bufferBase = (Byte *)::BigAlloc(_blockSize);
28 _pointerToLastSafePosition = _bufferBase + _blockSize - keepSizeAfter;
29 if (_blockSize == 0)
30 return true;
31 return (_bufferBase != 0);
35 HRESULT CLZInWindow::Init(ISequentialInStream *stream)
37 _stream = stream;
38 _buffer = _bufferBase;
39 _pos = 0;
40 _streamPos = 0;
41 _streamEndWasReached = false;
42 return ReadBlock();
46 void CLZInWindow::ReleaseStream()
48 _stream.Release();
52 ///////////////////////////////////////////
53 // ReadBlock
55 // In State:
56 // (_buffer + _streamPos) <= (_bufferBase + _blockSize)
57 // Out State:
58 // _posLimit <= _blockSize - _keepSizeAfter;
59 // if(_streamEndWasReached == false):
60 // _streamPos >= _pos + _keepSizeAfter
61 // _posLimit = _streamPos - _keepSizeAfter;
62 // else
63 //
65 HRESULT CLZInWindow::ReadBlock()
67 if(_streamEndWasReached)
68 return S_OK;
69 while(true)
71 UInt32 size = UInt32(_bufferBase - _buffer) + _blockSize - _streamPos;
72 if(size == 0)
73 return S_OK;
74 UInt32 numReadBytes;
75 RINOK(_stream->Read(_buffer + _streamPos, size, &numReadBytes));
76 if(numReadBytes == 0)
78 _posLimit = _streamPos;
79 const Byte *pointerToPostion = _buffer + _posLimit;
80 if(pointerToPostion > _pointerToLastSafePosition)
81 _posLimit = (UInt32)(_pointerToLastSafePosition - _buffer);
82 _streamEndWasReached = true;
83 return S_OK;
85 _streamPos += numReadBytes;
86 if(_streamPos >= _pos + _keepSizeAfter)
88 _posLimit = _streamPos - _keepSizeAfter;
89 return S_OK;
94 void CLZInWindow::MoveBlock()
96 BeforeMoveBlock();
97 UInt32 offset = UInt32(_buffer - _bufferBase) + _pos - _keepSizeBefore;
98 UInt32 numBytes = UInt32(_buffer - _bufferBase) + _streamPos - offset;
99 memmove(_bufferBase, _bufferBase + offset, numBytes);
100 _buffer -= offset;
101 AfterMoveBlock();