This repository has been archived by the owner on Oct 17, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
886 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// LzOutWindow.cs | ||
|
||
namespace SevenZip.Compression.LZ | ||
{ | ||
public class OutWindow | ||
{ | ||
byte[] _buffer = null; | ||
uint _pos; | ||
uint _windowSize = 0; | ||
uint _streamPos; | ||
System.IO.Stream _stream; | ||
|
||
public uint TrainSize = 0; | ||
|
||
public void Create(uint windowSize) | ||
{ | ||
if (_windowSize != windowSize) | ||
{ | ||
// System.GC.Collect(); | ||
_buffer = new byte[windowSize]; | ||
} | ||
_windowSize = windowSize; | ||
_pos = 0; | ||
_streamPos = 0; | ||
} | ||
|
||
public void Init(System.IO.Stream stream, bool solid) | ||
{ | ||
ReleaseStream(); | ||
_stream = stream; | ||
if (!solid) | ||
{ | ||
_streamPos = 0; | ||
_pos = 0; | ||
TrainSize = 0; | ||
} | ||
} | ||
|
||
public bool Train(System.IO.Stream stream) | ||
{ | ||
long len = stream.Length; | ||
uint size = (len < _windowSize) ? (uint)len : _windowSize; | ||
TrainSize = size; | ||
stream.Position = len - size; | ||
_streamPos = _pos = 0; | ||
while (size > 0) | ||
{ | ||
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; | ||
} | ||
|
||
public void ReleaseStream() | ||
{ | ||
Flush(); | ||
_stream = null; | ||
} | ||
|
||
public void Flush() | ||
{ | ||
uint size = _pos - _streamPos; | ||
if (size == 0) | ||
return; | ||
_stream.Write(_buffer, (int)_streamPos, (int)size); | ||
if (_pos >= _windowSize) | ||
_pos = 0; | ||
_streamPos = _pos; | ||
} | ||
|
||
public void CopyBlock(uint distance, uint len) | ||
{ | ||
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(); | ||
} | ||
} | ||
|
||
public void PutByte(byte b) | ||
{ | ||
_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]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// LzmaBase.cs | ||
|
||
namespace SevenZip.Compression.LZMA | ||
{ | ||
internal abstract class Base | ||
{ | ||
public const uint kNumRepDistances = 4; | ||
public const uint kNumStates = 12; | ||
|
||
// static byte []kLiteralNextStates = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5}; | ||
// static byte []kMatchNextStates = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10}; | ||
// static byte []kRepNextStates = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11}; | ||
// static byte []kShortRepNextStates = {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11}; | ||
|
||
public struct State | ||
{ | ||
public uint Index; | ||
public void Init() { Index = 0; } | ||
public void UpdateChar() | ||
{ | ||
if (Index < 4) Index = 0; | ||
else if (Index < 10) Index -= 3; | ||
else Index -= 6; | ||
} | ||
public void UpdateMatch() { Index = (uint)(Index < 7 ? 7 : 10); } | ||
public void UpdateRep() { Index = (uint)(Index < 7 ? 8 : 11); } | ||
public void UpdateShortRep() { Index = (uint)(Index < 7 ? 9 : 11); } | ||
public bool IsCharState() { return Index < 7; } | ||
} | ||
|
||
public const int kNumPosSlotBits = 6; | ||
public const int kDicLogSizeMin = 0; | ||
// public const int kDicLogSizeMax = 30; | ||
// public const uint kDistTableSizeMax = kDicLogSizeMax * 2; | ||
|
||
public const int kNumLenToPosStatesBits = 2; // it's for speed optimization | ||
public const uint kNumLenToPosStates = 1 << kNumLenToPosStatesBits; | ||
|
||
public const uint kMatchMinLen = 2; | ||
|
||
public static uint GetLenToPosState(uint len) | ||
{ | ||
len -= kMatchMinLen; | ||
if (len < kNumLenToPosStates) | ||
return len; | ||
return (uint)(kNumLenToPosStates - 1); | ||
} | ||
|
||
public const int kNumAlignBits = 4; | ||
public const uint kAlignTableSize = 1 << kNumAlignBits; | ||
public const uint kAlignMask = (kAlignTableSize - 1); | ||
|
||
public const uint kStartPosModelIndex = 4; | ||
public const uint kEndPosModelIndex = 14; | ||
public const uint kNumPosModels = kEndPosModelIndex - kStartPosModelIndex; | ||
|
||
public const uint kNumFullDistances = 1 << ((int)kEndPosModelIndex / 2); | ||
|
||
public const uint kNumLitPosStatesBitsEncodingMax = 4; | ||
public const uint kNumLitContextBitsMax = 8; | ||
|
||
public const int kNumPosStatesBitsMax = 4; | ||
public const uint kNumPosStatesMax = (1 << kNumPosStatesBitsMax); | ||
public const int kNumPosStatesBitsEncodingMax = 4; | ||
public const uint kNumPosStatesEncodingMax = (1 << kNumPosStatesBitsEncodingMax); | ||
|
||
public const int kNumLowLenBits = 3; | ||
public const int kNumMidLenBits = 3; | ||
public const int kNumHighLenBits = 8; | ||
public const uint kNumLowLenSymbols = 1 << kNumLowLenBits; | ||
public const uint kNumMidLenSymbols = 1 << kNumMidLenBits; | ||
public const uint kNumLenSymbols = kNumLowLenSymbols + kNumMidLenSymbols + | ||
(1 << kNumHighLenBits); | ||
public const uint kMatchMaxLen = kMatchMinLen + kNumLenSymbols - 1; | ||
} | ||
} |
Oops, something went wrong.