Project Maintenance

This commit is contained in:
Gustave Monce
2021-08-09 20:21:09 +02:00
parent 9865ef7f79
commit 847ce0506d
411 changed files with 5922 additions and 54941 deletions
+2 -2
View File
@@ -4,7 +4,7 @@ using System;
namespace SevenZip.Compression.LZ
{
interface IInWindowStream
internal interface IInWindowStream
{
void SetStream(System.IO.Stream inStream);
void Init();
@@ -14,7 +14,7 @@ namespace SevenZip.Compression.LZ
UInt32 GetNumAvailableBytes();
}
interface IMatchFinder : IInWindowStream
internal interface IMatchFinder : IInWindowStream
{
void Create(UInt32 historySize, UInt32 keepAddBufferBefore,
UInt32 matchMaxLen, UInt32 keepAddBufferAfter);
+73 -23
View File
@@ -6,30 +6,30 @@ namespace SevenZip.Compression.LZ
{
public class BinTree : InWindow, IMatchFinder
{
UInt32 _cyclicBufferPos;
UInt32 _cyclicBufferSize = 0;
UInt32 _matchMaxLen;
private UInt32 _cyclicBufferPos;
private UInt32 _cyclicBufferSize = 0;
private UInt32 _matchMaxLen;
UInt32[] _son;
UInt32[] _hash;
private UInt32[] _son;
private UInt32[] _hash;
UInt32 _cutValue = 0xFF;
UInt32 _hashMask;
UInt32 _hashSizeSum = 0;
private UInt32 _cutValue = 0xFF;
private UInt32 _hashMask;
private UInt32 _hashSizeSum = 0;
bool HASH_ARRAY = true;
private bool HASH_ARRAY = true;
const UInt32 kHash2Size = 1 << 10;
const UInt32 kHash3Size = 1 << 16;
const UInt32 kBT2HashSize = 1 << 16;
const UInt32 kStartMaxLen = 1;
const UInt32 kHash3Offset = kHash2Size;
const UInt32 kEmptyHashValue = 0;
const UInt32 kMaxValForNormalize = ((UInt32)1 << 31) - 1;
private const UInt32 kHash2Size = 1 << 10;
private const UInt32 kHash3Size = 1 << 16;
private const UInt32 kBT2HashSize = 1 << 16;
private const UInt32 kStartMaxLen = 1;
private const UInt32 kHash3Offset = kHash2Size;
private const UInt32 kEmptyHashValue = 0;
private const UInt32 kMaxValForNormalize = ((UInt32)1 << 31) - 1;
UInt32 kNumHashDirectBytes = 0;
UInt32 kMinMatchCheck = 4;
UInt32 kFixHashSize = kHash2Size + kHash3Size;
private UInt32 kNumHashDirectBytes = 0;
private UInt32 kMinMatchCheck = 4;
private UInt32 kFixHashSize = kHash2Size + kHash3Size;
public void SetType(int numHashBytes)
{
@@ -55,7 +55,10 @@ namespace SevenZip.Compression.LZ
{
base.Init();
for (UInt32 i = 0; i < _hashSizeSum; i++)
{
_hash[i] = kEmptyHashValue;
}
_cyclicBufferPos = 0;
ReduceOffsets(-1);
}
@@ -63,10 +66,15 @@ namespace SevenZip.Compression.LZ
public new void MovePos()
{
if (++_cyclicBufferPos >= _cyclicBufferSize)
{
_cyclicBufferPos = 0;
}
base.MovePos();
if (_pos == kMaxValForNormalize)
{
Normalize();
}
}
public new Byte GetIndexByte(Int32 index) { return base.GetIndexByte(index); }
@@ -80,11 +88,14 @@ namespace SevenZip.Compression.LZ
UInt32 matchMaxLen, UInt32 keepAddBufferAfter)
{
if (historySize > kMaxValForNormalize - 256)
{
throw new Exception();
}
_cutValue = 16 + (matchMaxLen >> 1);
UInt32 windowReservSize = (historySize + keepAddBufferBefore +
matchMaxLen + keepAddBufferAfter) / 2 + 256;
UInt32 windowReservSize = ((historySize + keepAddBufferBefore +
matchMaxLen + keepAddBufferAfter) / 2) + 256;
base.Create(historySize + keepAddBufferBefore, matchMaxLen + keepAddBufferAfter, windowReservSize);
@@ -92,7 +103,9 @@ namespace SevenZip.Compression.LZ
UInt32 cyclicBufferSize = historySize + 1;
if (_cyclicBufferSize != cyclicBufferSize)
{
_son = new UInt32[(_cyclicBufferSize = cyclicBufferSize) * 2];
}
UInt32 hs = kBT2HashSize;
@@ -106,20 +119,27 @@ namespace SevenZip.Compression.LZ
hs >>= 1;
hs |= 0xFFFF;
if (hs > (1 << 24))
{
hs >>= 1;
}
_hashMask = hs;
hs++;
hs += kFixHashSize;
}
if (hs != _hashSizeSum)
{
_hash = new UInt32[_hashSizeSum = hs];
}
}
public UInt32 GetMatches(UInt32[] distances)
{
UInt32 lenLimit;
if (_pos + _matchMaxLen <= _streamPos)
{
lenLimit = _matchMaxLen;
}
else
{
lenLimit = _streamPos - _pos;
@@ -145,7 +165,9 @@ namespace SevenZip.Compression.LZ
hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask;
}
else
{
hashValue = _bufferBase[cur] ^ ((UInt32)(_bufferBase[cur + 1]) << 8);
}
UInt32 curMatch = _hash[kFixHashSize + hashValue];
if (HASH_ARRAY)
@@ -155,20 +177,29 @@ namespace SevenZip.Compression.LZ
_hash[hash2Value] = _pos;
_hash[kHash3Offset + hash3Value] = _pos;
if (curMatch2 > matchMinPos)
{
if (_bufferBase[_bufferOffset + curMatch2] == _bufferBase[cur])
{
distances[offset++] = maxLen = 2;
distances[offset++] = _pos - curMatch2 - 1;
}
}
if (curMatch3 > matchMinPos)
{
if (_bufferBase[_bufferOffset + curMatch3] == _bufferBase[cur])
{
if (curMatch3 == curMatch2)
{
offset -= 2;
}
distances[offset++] = maxLen = 3;
distances[offset++] = _pos - curMatch3 - 1;
curMatch2 = curMatch3;
}
}
if (offset != 0 && curMatch2 == curMatch)
{
offset -= 2;
@@ -216,8 +247,13 @@ namespace SevenZip.Compression.LZ
if (_bufferBase[pby1 + len] == _bufferBase[cur + len])
{
while (++len != lenLimit)
{
if (_bufferBase[pby1 + len] != _bufferBase[cur + len])
{
break;
}
}
if (maxLen < len)
{
distances[offset++] = maxLen = len;
@@ -255,7 +291,9 @@ namespace SevenZip.Compression.LZ
{
UInt32 lenLimit;
if (_pos + _matchMaxLen <= _streamPos)
{
lenLimit = _matchMaxLen;
}
else
{
lenLimit = _streamPos - _pos;
@@ -282,7 +320,9 @@ namespace SevenZip.Compression.LZ
hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask;
}
else
{
hashValue = _bufferBase[cur] ^ ((UInt32)(_bufferBase[cur + 1]) << 8);
}
UInt32 curMatch = _hash[kFixHashSize + hashValue];
_hash[kFixHashSize + hashValue] = _pos;
@@ -312,8 +352,13 @@ namespace SevenZip.Compression.LZ
if (_bufferBase[pby1 + len] == _bufferBase[cur + len])
{
while (++len != lenLimit)
{
if (_bufferBase[pby1 + len] != _bufferBase[cur + len])
{
break;
}
}
if (len == lenLimit)
{
_son[ptr1] = _son[cyclicPos];
@@ -341,20 +386,25 @@ namespace SevenZip.Compression.LZ
while (--num != 0);
}
void NormalizeLinks(UInt32[] items, UInt32 numItems, UInt32 subValue)
private void NormalizeLinks(UInt32[] items, UInt32 numItems, UInt32 subValue)
{
for (UInt32 i = 0; i < numItems; i++)
{
UInt32 value = items[i];
if (value <= subValue)
{
value = kEmptyHashValue;
}
else
{
value -= subValue;
}
items[i] = value;
}
}
void Normalize()
private void Normalize()
{
UInt32 subValue = _pos - _cyclicBufferSize;
NormalizeLinks(_son, _cyclicBufferSize * 2, subValue);
+38 -12
View File
@@ -7,62 +7,77 @@ namespace SevenZip.Compression.LZ
public class InWindow
{
public Byte[] _bufferBase = null; // pointer to buffer with data
System.IO.Stream _stream;
UInt32 _posLimit; // offset (from _buffer) of first byte when new block reading must be done
bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream
private System.IO.Stream _stream;
private UInt32 _posLimit; // offset (from _buffer) of first byte when new block reading must be done
private bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream
UInt32 _pointerToLastSafePosition;
private UInt32 _pointerToLastSafePosition;
public UInt32 _bufferOffset;
public UInt32 _blockSize; // Size of Allocated memory block
public UInt32 _pos; // offset (from _buffer) of curent byte
UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos
UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos
private UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos
private UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos
public UInt32 _streamPos; // offset (from _buffer) of first not read byte from Stream
public void MoveBlock()
{
UInt32 offset = (UInt32)(_bufferOffset) + _pos - _keepSizeBefore;
UInt32 offset = _bufferOffset + _pos - _keepSizeBefore;
// we need one additional byte, since MovePos moves on 1 byte.
if (offset > 0)
{
offset--;
}
UInt32 numBytes = (UInt32)(_bufferOffset) + _streamPos - offset;
UInt32 numBytes = _bufferOffset + _streamPos - offset;
// check negative offset ????
for (UInt32 i = 0; i < numBytes; i++)
{
_bufferBase[i] = _bufferBase[offset + i];
}
_bufferOffset -= offset;
}
public virtual void ReadBlock()
{
if (_streamEndWasReached)
{
return;
}
while (true)
{
int size = (int)((0 - _bufferOffset) + _blockSize - _streamPos);
if (size == 0)
{
return;
}
int numReadBytes = _stream.Read(_bufferBase, (int)(_bufferOffset + _streamPos), size);
if (numReadBytes == 0)
{
_posLimit = _streamPos;
UInt32 pointerToPostion = _bufferOffset + _posLimit;
if (pointerToPostion > _pointerToLastSafePosition)
_posLimit = (UInt32)(_pointerToLastSafePosition - _bufferOffset);
{
_posLimit = _pointerToLastSafePosition - _bufferOffset;
}
_streamEndWasReached = true;
return;
}
_streamPos += (UInt32)numReadBytes;
if (_streamPos >= _pos + _keepSizeAfter)
{
_posLimit = _streamPos - _keepSizeAfter;
}
}
}
void Free() { _bufferBase = null; }
private void Free() { _bufferBase = null; }
public void Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv)
{
@@ -97,7 +112,10 @@ namespace SevenZip.Compression.LZ
{
UInt32 pointerToPostion = _bufferOffset + _pos;
if (pointerToPostion > _pointerToLastSafePosition)
{
MoveBlock();
}
ReadBlock();
}
}
@@ -108,14 +126,22 @@ namespace SevenZip.Compression.LZ
public UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit)
{
if (_streamEndWasReached)
if ((_pos + index) + limit > _streamPos)
{
if (_pos + index + limit > _streamPos)
{
limit = _streamPos - (UInt32)(_pos + index);
}
}
distance++;
// Byte *pby = _buffer + (size_t)_pos + index;
UInt32 pby = _bufferOffset + _pos + (UInt32)index;
UInt32 i;
for (i = 0; i < limit && _bufferBase[pby + i] == _bufferBase[pby + i - distance]; i++) ;
for (i = 0; i < limit && _bufferBase[pby + i] == _bufferBase[pby + i - distance]; i++)
{
}
return i;
}
+32 -5
View File
@@ -4,11 +4,11 @@ namespace SevenZip.Compression.LZ
{
public class OutWindow
{
byte[] _buffer = null;
uint _pos;
uint _windowSize = 0;
uint _streamPos;
System.IO.Stream _stream;
private byte[] _buffer = null;
private uint _pos;
private uint _windowSize = 0;
private uint _streamPos;
private System.IO.Stream _stream;
public uint TrainSize = 0;
@@ -47,15 +47,23 @@ namespace SevenZip.Compression.LZ
{
uint curSize = _windowSize - _pos;
if (size < curSize)
{
curSize = size;
}
int numReadBytes = stream.Read(_buffer, (int)_pos, (int)curSize);
if (numReadBytes == 0)
{
return false;
}
size -= (uint)numReadBytes;
_pos += (uint)numReadBytes;
_streamPos += (uint)numReadBytes;
if (_pos == _windowSize)
{
_streamPos = _pos = 0;
}
}
return true;
}
@@ -70,10 +78,16 @@ namespace SevenZip.Compression.LZ
{
uint size = _pos - _streamPos;
if (size == 0)
{
return;
}
_stream.Write(_buffer, (int)_streamPos, (int)size);
if (_pos >= _windowSize)
{
_pos = 0;
}
_streamPos = _pos;
}
@@ -81,14 +95,22 @@ namespace SevenZip.Compression.LZ
{
uint pos = _pos - distance - 1;
if (pos >= _windowSize)
{
pos += _windowSize;
}
for (; len > 0; len--)
{
if (pos >= _windowSize)
{
pos = 0;
}
_buffer[_pos++] = _buffer[pos++];
if (_pos >= _windowSize)
{
Flush();
}
}
}
@@ -96,14 +118,19 @@ namespace SevenZip.Compression.LZ
{
_buffer[_pos++] = b;
if (_pos >= _windowSize)
{
Flush();
}
}
public byte GetByte(uint distance)
{
uint pos = _pos - distance - 1;
if (pos >= _windowSize)
{
pos += _windowSize;
}
return _buffer[pos];
}
}