Debian Codegraph @

utils/lzma/SRC/7zip/Compress/LZ/LZInWindow.cpp

raw
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 if (_blockSize != 0)
26 _bufferBase = (Byte *)::BigAlloc(_blockSize);
27 }
28 _pointerToLastSafePosition = _bufferBase + _blockSize - keepSizeAfter;
29 if (_blockSize == 0)
30 return true;
31 return (_bufferBase != 0);
32 }
33
34
35 HRESULT CLZInWindow::Init(ISequentialInStream *stream)
36 {
37 _stream = stream;
38 _buffer = _bufferBase;
39 _pos = 0;
40 _streamPos = 0;
41 _streamEndWasReached = false;
42 return ReadBlock();
43 }
44
45 /*
46 void CLZInWindow::ReleaseStream()
47 {
48 _stream.Release();
49 }
50 */
51
52 ///////////////////////////////////////////
53 // ReadBlock
54
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 //
64
65 HRESULT CLZInWindow::ReadBlock()
66 {
67 if(_streamEndWasReached)
68 return S_OK;
69 while(true)
70 {
71 UInt32 size = UInt32(_bufferBase - _buffer) + _blockSize - _streamPos;
72 if(size == 0)
73 return S_OK;
74 UInt32 numReadBytes;
75 RINOK(_stream->ReadPart(_buffer + _streamPos, size, &numReadBytes));
76 if(numReadBytes == 0)
77 {
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;
84 }
85 _streamPos += numReadBytes;
86 if(_streamPos >= _pos + _keepSizeAfter)
87 {
88 _posLimit = _streamPos - _keepSizeAfter;
89 return S_OK;
90 }
91 }
92 }
93
94 void CLZInWindow::MoveBlock()
95 {
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();
102 }