Annotation of elwix/tools/oldlzma/SRC/7zip/Compress/LZ/LZInWindow.cpp, revision 1.1.1.1

1.1       misho       1: // LZInWindow.cpp
                      2: 
                      3: #include "StdAfx.h"
                      4: 
                      5: #include "LZInWindow.h"
                      6: #include "../../../Common/MyCom.h"
                      7: #include "../../../Common/Alloc.h"
                      8: 
                      9: void CLZInWindow::Free()
                     10: {
                     11:   ::BigFree(_bufferBase);
                     12:   _bufferBase = 0;
                     13: }
                     14: 
                     15: bool CLZInWindow::Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv)
                     16: {
                     17:   _keepSizeBefore = keepSizeBefore;
                     18:   _keepSizeAfter = keepSizeAfter;
                     19:   _keepSizeReserv = keepSizeReserv;
                     20:   UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
                     21:   if (_bufferBase == 0 || _blockSize != blockSize)
                     22:   {
                     23:     Free();
                     24:     _blockSize = blockSize;
                     25:     _bufferBase = (Byte *)::BigAlloc(_blockSize);
                     26:   }
                     27:   _pointerToLastSafePosition = _bufferBase + _blockSize - keepSizeAfter;
                     28:   return (_bufferBase != 0);
                     29: }
                     30: 
                     31: 
                     32: HRESULT CLZInWindow::Init(ISequentialInStream *stream)
                     33: {
                     34:   _stream = stream;
                     35:   _buffer = _bufferBase;
                     36:   _pos = 0;
                     37:   _streamPos = 0;
                     38:   _streamEndWasReached = false;
                     39:   return ReadBlock();
                     40: }
                     41: 
                     42: /*
                     43: void CLZInWindow::ReleaseStream()
                     44: {
                     45:   _stream.Release();
                     46: }
                     47: */
                     48: 
                     49: ///////////////////////////////////////////
                     50: // ReadBlock
                     51: 
                     52: // In State:
                     53: //   (_buffer + _streamPos) <= (_bufferBase + _blockSize)
                     54: // Out State:
                     55: //   _posLimit <= _blockSize - _keepSizeAfter;
                     56: //   if(_streamEndWasReached == false):
                     57: //     _streamPos >= _pos + _keepSizeAfter
                     58: //     _posLimit = _streamPos - _keepSizeAfter;
                     59: //   else
                     60: //          
                     61:   
                     62: HRESULT CLZInWindow::ReadBlock()
                     63: {
                     64:   if(_streamEndWasReached)
                     65:     return S_OK;
                     66:   while(true)
                     67:   {
                     68:     UInt32 size = UInt32(_bufferBase - _buffer) + _blockSize - _streamPos;
                     69:     if(size == 0)
                     70:       return S_OK;
                     71:     UInt32 numReadBytes;
                     72:     RINOK(_stream->ReadPart(_buffer + _streamPos, size, &numReadBytes));
                     73:     if(numReadBytes == 0)
                     74:     {
                     75:       _posLimit = _streamPos;
                     76:       const Byte *pointerToPostion = _buffer + _posLimit;
                     77:       if(pointerToPostion > _pointerToLastSafePosition)
                     78:         _posLimit = (UInt32)(_pointerToLastSafePosition - _buffer);
                     79:       _streamEndWasReached = true;
                     80:       return S_OK;
                     81:     }
                     82:     _streamPos += numReadBytes;
                     83:     if(_streamPos >= _pos + _keepSizeAfter)
                     84:     {
                     85:       _posLimit = _streamPos - _keepSizeAfter;
                     86:       return S_OK;
                     87:     }
                     88:   }
                     89: }
                     90: 
                     91: void CLZInWindow::MoveBlock()
                     92: {
                     93:   BeforeMoveBlock();
                     94:   UInt32 offset = UInt32(_buffer - _bufferBase) + _pos - _keepSizeBefore;
                     95:   UInt32 numBytes = UInt32(_buffer - _bufferBase) + _streamPos -  offset;
                     96:   memmove(_bufferBase, _bufferBase + offset, numBytes);
                     97:   _buffer -= offset;
                     98:   AfterMoveBlock();
                     99: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>