5 namespace SevenZip.Compression.LZ
9 public Byte[] _bufferBase = null; // pointer to buffer with data
10 System.IO.Stream _stream;
11 UInt32 _posLimit; // offset (from _buffer) of first byte when new block reading must be done
12 bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream
14 UInt32 _pointerToLastSafePosition;
16 public UInt32 _bufferOffset;
18 public UInt32 _blockSize; // Size of Allocated memory block
19 public UInt32 _pos; // offset (from _buffer) of curent byte
20 UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos
21 UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos
22 public UInt32 _streamPos; // offset (from _buffer) of first not read byte from Stream
24 public void MoveBlock()
26 UInt32 offset = (UInt32)(_bufferOffset) + _pos - _keepSizeBefore;
27 // we need one additional byte, since MovePos moves on 1 byte.
31 UInt32 numBytes = (UInt32)(_bufferOffset) + _streamPos - offset;
33 // check negative offset ????
34 for (UInt32 i = 0; i < numBytes; i++)
35 _bufferBase[i] = _bufferBase[offset + i];
36 _bufferOffset -= offset;
39 public virtual void ReadBlock()
41 if (_streamEndWasReached)
45 int size = (int)((0 - _bufferOffset) + _blockSize - _streamPos);
48 int numReadBytes = _stream.Read(_bufferBase, (int)(_bufferOffset + _streamPos), size);
49 if (numReadBytes == 0)
51 _posLimit = _streamPos;
52 UInt32 pointerToPostion = _bufferOffset + _posLimit;
53 if (pointerToPostion > _pointerToLastSafePosition)
54 _posLimit = (UInt32)(_pointerToLastSafePosition - _bufferOffset);
56 _streamEndWasReached = true;
59 _streamPos += (UInt32)numReadBytes;
60 if (_streamPos >= _pos + _keepSizeAfter)
61 _posLimit = _streamPos - _keepSizeAfter;
65 void Free() { _bufferBase = null; }
67 public void Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv)
69 _keepSizeBefore = keepSizeBefore;
70 _keepSizeAfter = keepSizeAfter;
71 UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv;
72 if (_bufferBase == null || _blockSize != blockSize)
75 _blockSize = blockSize;
76 _bufferBase = new Byte[_blockSize];
78 _pointerToLastSafePosition = _blockSize - keepSizeAfter;
81 public void SetStream(System.IO.Stream stream) { _stream = stream; }
82 public void ReleaseStream() { _stream = null; }
89 _streamEndWasReached = false;
98 UInt32 pointerToPostion = _bufferOffset + _pos;
99 if (pointerToPostion > _pointerToLastSafePosition)
105 public Byte GetIndexByte(Int32 index) { return _bufferBase[_bufferOffset + _pos + index]; }
107 // index + limit have not to exceed _keepSizeAfter;
108 public UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit)
110 if (_streamEndWasReached)
111 if ((_pos + index) + limit > _streamPos)
112 limit = _streamPos - (UInt32)(_pos + index);
114 // Byte *pby = _buffer + (size_t)_pos + index;
115 UInt32 pby = _bufferOffset + _pos + (UInt32)index;
118 for (i = 0; i < limit && _bufferBase[pby + i] == _bufferBase[pby + i - distance]; i++);
122 public UInt32 GetNumAvailableBytes() { return _streamPos - _pos; }
124 public void ReduceOffsets(Int32 subValue)
126 _bufferOffset += (UInt32)subValue;
127 _posLimit -= (UInt32)subValue;
128 _pos -= (UInt32)subValue;
129 _streamPos -= (UInt32)subValue;