mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-15 17:02:03 +00:00
More clean up doesn’t help
This commit is contained in:
@@ -112,31 +112,29 @@ namespace SharpCompress.Common.GZip
|
||||
}
|
||||
if ((header.Memory.Span[3] & 0x02) == 0x02)
|
||||
{
|
||||
using var one = MemoryPool<byte>.Shared.Rent(1);
|
||||
await _stream.ReadAsync(one.Memory.Slice(0,1), cancellationToken); // CRC16, ignore
|
||||
await _stream.ReadByteAsync(cancellationToken); // CRC16, ignore
|
||||
}
|
||||
}
|
||||
|
||||
private async ValueTask<string> ReadZeroTerminatedStringAsync(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
using var buf1 = MemoryPool<byte>.Shared.Rent(1);
|
||||
var list = new List<byte>();
|
||||
bool done = false;
|
||||
do
|
||||
{
|
||||
// workitem 7740
|
||||
int n = await stream.ReadAsync(buf1.Memory.Slice(0, 1), cancellationToken);
|
||||
byte n = await stream.ReadByteAsync(cancellationToken);
|
||||
if (n != 1)
|
||||
{
|
||||
throw new ZlibException("Unexpected EOF reading GZIP header.");
|
||||
}
|
||||
if (buf1.Memory.Span[0] == 0)
|
||||
if (n == 0)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(buf1.Memory.Span[0]);
|
||||
list.Add(n);
|
||||
}
|
||||
}
|
||||
while (!done);
|
||||
|
||||
@@ -90,7 +90,8 @@ namespace SharpCompress.Common.Zip
|
||||
Header.CompressedSize > 0 ? Header.CompressedSize - 4 - props.Length : -1,
|
||||
FlagUtility.HasFlag(Header.Flags, HeaderFlags.Bit1)
|
||||
? -1
|
||||
: (long)Header.UncompressedSize);
|
||||
: (long)Header.UncompressedSize,
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
/* case ZipCompressionMethod.PPMd:
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharpCompress.Compressors.LZMA
|
||||
@@ -61,7 +62,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
/// callback progress reference.
|
||||
/// </param>
|
||||
ValueTask CodeAsync(Stream inStream, Stream outStream,
|
||||
Int64 inSize, Int64 outSize, ICodeProgress progress);
|
||||
Int64 inSize, Int64 outSize, ICodeProgress progress, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Compressors.LZMA.LZ;
|
||||
using SharpCompress.Compressors.LZMA.RangeCoder;
|
||||
@@ -39,21 +40,21 @@ namespace SharpCompress.Compressors.LZMA
|
||||
_highCoder.Init();
|
||||
}
|
||||
|
||||
public async ValueTask<uint> DecodeAsync(RangeCoder.Decoder rangeDecoder, uint posState)
|
||||
public async ValueTask<uint> DecodeAsync(RangeCoder.Decoder rangeDecoder, uint posState, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await _choice.DecodeAsync(rangeDecoder) == 0)
|
||||
if (await _choice.DecodeAsync(rangeDecoder, cancellationToken) == 0)
|
||||
{
|
||||
return await _lowCoder[posState].DecodeAsync(rangeDecoder);
|
||||
return await _lowCoder[posState].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
}
|
||||
uint symbol = Base.K_NUM_LOW_LEN_SYMBOLS;
|
||||
if (await _choice2.DecodeAsync(rangeDecoder) == 0)
|
||||
if (await _choice2.DecodeAsync(rangeDecoder, cancellationToken) == 0)
|
||||
{
|
||||
symbol += await _midCoder[posState].DecodeAsync(rangeDecoder);
|
||||
symbol += await _midCoder[posState].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
symbol += Base.K_NUM_MID_LEN_SYMBOLS;
|
||||
symbol += await _highCoder.DecodeAsync(rangeDecoder);
|
||||
symbol += await _highCoder.DecodeAsync(rangeDecoder, cancellationToken);
|
||||
}
|
||||
return symbol;
|
||||
}
|
||||
@@ -78,31 +79,31 @@ namespace SharpCompress.Compressors.LZMA
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<byte> DecodeNormalAsync(RangeCoder.Decoder rangeDecoder)
|
||||
public async ValueTask<byte> DecodeNormalAsync(RangeCoder.Decoder rangeDecoder, CancellationToken cancellationToken)
|
||||
{
|
||||
uint symbol = 1;
|
||||
do
|
||||
{
|
||||
symbol = (symbol << 1) | await _decoders[symbol].DecodeAsync(rangeDecoder);
|
||||
symbol = (symbol << 1) | await _decoders[symbol].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
}
|
||||
while (symbol < 0x100);
|
||||
return (byte)symbol;
|
||||
}
|
||||
|
||||
public async ValueTask<byte> DecodeWithMatchByteAsync(RangeCoder.Decoder rangeDecoder, byte matchByte)
|
||||
public async ValueTask<byte> DecodeWithMatchByteAsync(RangeCoder.Decoder rangeDecoder, byte matchByte, CancellationToken cancellationToken)
|
||||
{
|
||||
uint symbol = 1;
|
||||
do
|
||||
{
|
||||
uint matchBit = (uint)(matchByte >> 7) & 1;
|
||||
matchByte <<= 1;
|
||||
uint bit = await _decoders[((1 + matchBit) << 8) + symbol].DecodeAsync(rangeDecoder);
|
||||
uint bit = await _decoders[((1 + matchBit) << 8) + symbol].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
symbol = (symbol << 1) | bit;
|
||||
if (matchBit != bit)
|
||||
{
|
||||
while (symbol < 0x100)
|
||||
{
|
||||
symbol = (symbol << 1) | await _decoders[symbol].DecodeAsync(rangeDecoder);
|
||||
symbol = (symbol << 1) | await _decoders[symbol].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -149,14 +150,14 @@ namespace SharpCompress.Compressors.LZMA
|
||||
return ((pos & _posMask) << _numPrevBits) + (uint)(prevByte >> (8 - _numPrevBits));
|
||||
}
|
||||
|
||||
public ValueTask<byte> DecodeNormalAsync(RangeCoder.Decoder rangeDecoder, uint pos, byte prevByte)
|
||||
public ValueTask<byte> DecodeNormalAsync(RangeCoder.Decoder rangeDecoder, uint pos, byte prevByte, CancellationToken cancellationToken)
|
||||
{
|
||||
return _coders[GetState(pos, prevByte)].DecodeNormalAsync(rangeDecoder);
|
||||
return _coders[GetState(pos, prevByte)].DecodeNormalAsync(rangeDecoder, cancellationToken);
|
||||
}
|
||||
|
||||
public ValueTask<byte> DecodeWithMatchByteAsync(RangeCoder.Decoder rangeDecoder, uint pos, byte prevByte, byte matchByte)
|
||||
public ValueTask<byte> DecodeWithMatchByteAsync(RangeCoder.Decoder rangeDecoder, uint pos, byte prevByte, byte matchByte, CancellationToken cancellationToken)
|
||||
{
|
||||
return _coders[GetState(pos, prevByte)].DecodeWithMatchByteAsync(rangeDecoder, matchByte);
|
||||
return _coders[GetState(pos, prevByte)].DecodeWithMatchByteAsync(rangeDecoder, matchByte, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,7 +250,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
_isRepG2Decoders[i].Init();
|
||||
}
|
||||
|
||||
_literalDecoder?.Init();
|
||||
_literalDecoder!.Init();
|
||||
for (i = 0; i < Base.K_NUM_LEN_TO_POS_STATES; i++)
|
||||
{
|
||||
_posSlotDecoder[i].Init();
|
||||
@@ -273,7 +274,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
}
|
||||
|
||||
public async ValueTask CodeAsync(Stream inStream, Stream outStream,
|
||||
Int64 inSize, Int64 outSize, ICodeProgress progress)
|
||||
Int64 inSize, Int64 outSize, ICodeProgress progress, CancellationToken cancellationToken)
|
||||
{
|
||||
if (_outWindow is null)
|
||||
{
|
||||
@@ -290,9 +291,9 @@ namespace SharpCompress.Compressors.LZMA
|
||||
}
|
||||
|
||||
RangeCoder.Decoder rangeDecoder = new RangeCoder.Decoder();
|
||||
await rangeDecoder.InitAsync(inStream);
|
||||
await rangeDecoder.InitAsync(inStream, cancellationToken);
|
||||
|
||||
await CodeAsync(_dictionarySize, _outWindow, rangeDecoder);
|
||||
await CodeAsync(_dictionarySize, _outWindow, rangeDecoder, cancellationToken);
|
||||
|
||||
_outWindow.ReleaseStream();
|
||||
rangeDecoder.ReleaseStream();
|
||||
@@ -308,7 +309,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
_outWindow = null;
|
||||
}
|
||||
|
||||
internal async ValueTask<bool> CodeAsync(int dictionarySize, OutWindow outWindow, RangeCoder.Decoder rangeDecoder)
|
||||
internal async ValueTask<bool> CodeAsync(int dictionarySize, OutWindow outWindow, RangeCoder.Decoder rangeDecoder, CancellationToken cancellationToken)
|
||||
{
|
||||
_literalDecoder ??= _literalDecoder.CheckNotNull(nameof(_literalDecoder));
|
||||
int dictionarySizeCheck = Math.Max(dictionarySize, 1);
|
||||
@@ -318,7 +319,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
while (outWindow.HasSpace)
|
||||
{
|
||||
uint posState = (uint)outWindow._total & _posStateMask;
|
||||
if (await _isMatchDecoders[(_state._index << Base.K_NUM_POS_STATES_BITS_MAX) + posState].DecodeAsync(rangeDecoder) == 0)
|
||||
if (await _isMatchDecoders[(_state._index << Base.K_NUM_POS_STATES_BITS_MAX) + posState].DecodeAsync(rangeDecoder, cancellationToken) == 0)
|
||||
{
|
||||
byte b;
|
||||
byte prevByte = outWindow.GetByte(0);
|
||||
@@ -326,11 +327,11 @@ namespace SharpCompress.Compressors.LZMA
|
||||
{
|
||||
b = await _literalDecoder.DecodeWithMatchByteAsync(rangeDecoder,
|
||||
(uint)outWindow._total, prevByte,
|
||||
outWindow.GetByte((int)_rep0));
|
||||
outWindow.GetByte((int)_rep0), cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
b = await _literalDecoder.DecodeNormalAsync(rangeDecoder, (uint)outWindow._total, prevByte);
|
||||
b = await _literalDecoder.DecodeNormalAsync(rangeDecoder, (uint)outWindow._total, prevByte, cancellationToken);
|
||||
}
|
||||
outWindow.PutByte(b);
|
||||
_state.UpdateChar();
|
||||
@@ -338,13 +339,13 @@ namespace SharpCompress.Compressors.LZMA
|
||||
else
|
||||
{
|
||||
uint len;
|
||||
if (await _isRepDecoders[_state._index].DecodeAsync(rangeDecoder) == 1)
|
||||
if (await _isRepDecoders[_state._index].DecodeAsync(rangeDecoder, cancellationToken) == 1)
|
||||
{
|
||||
if (await _isRepG0Decoders[_state._index].DecodeAsync(rangeDecoder) == 0)
|
||||
if (await _isRepG0Decoders[_state._index].DecodeAsync(rangeDecoder, cancellationToken) == 0)
|
||||
{
|
||||
if (
|
||||
await _isRep0LongDecoders[(_state._index << Base.K_NUM_POS_STATES_BITS_MAX) + posState].DecodeAsync(
|
||||
rangeDecoder) == 0)
|
||||
rangeDecoder, cancellationToken) == 0)
|
||||
{
|
||||
_state.UpdateShortRep();
|
||||
outWindow.PutByte(outWindow.GetByte((int)_rep0));
|
||||
@@ -354,13 +355,13 @@ namespace SharpCompress.Compressors.LZMA
|
||||
else
|
||||
{
|
||||
UInt32 distance;
|
||||
if (await _isRepG1Decoders[_state._index].DecodeAsync(rangeDecoder) == 0)
|
||||
if (await _isRepG1Decoders[_state._index].DecodeAsync(rangeDecoder, cancellationToken) == 0)
|
||||
{
|
||||
distance = _rep1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (await _isRepG2Decoders[_state._index].DecodeAsync(rangeDecoder) == 0)
|
||||
if (await _isRepG2Decoders[_state._index].DecodeAsync(rangeDecoder, cancellationToken) == 0)
|
||||
{
|
||||
distance = _rep2;
|
||||
}
|
||||
@@ -374,7 +375,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
_rep1 = _rep0;
|
||||
_rep0 = distance;
|
||||
}
|
||||
len = await _repLenDecoder.DecodeAsync(rangeDecoder, posState) + Base.K_MATCH_MIN_LEN;
|
||||
len = await _repLenDecoder.DecodeAsync(rangeDecoder, posState, cancellationToken) + Base.K_MATCH_MIN_LEN;
|
||||
_state.UpdateRep();
|
||||
}
|
||||
else
|
||||
@@ -382,9 +383,9 @@ namespace SharpCompress.Compressors.LZMA
|
||||
_rep3 = _rep2;
|
||||
_rep2 = _rep1;
|
||||
_rep1 = _rep0;
|
||||
len = Base.K_MATCH_MIN_LEN + await _lenDecoder.DecodeAsync(rangeDecoder, posState);
|
||||
len = Base.K_MATCH_MIN_LEN + await _lenDecoder.DecodeAsync(rangeDecoder, posState, cancellationToken);
|
||||
_state.UpdateMatch();
|
||||
uint posSlot = await _posSlotDecoder[Base.GetLenToPosState(len)].DecodeAsync(rangeDecoder);
|
||||
uint posSlot = await _posSlotDecoder[Base.GetLenToPosState(len)].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
if (posSlot >= Base.K_START_POS_MODEL_INDEX)
|
||||
{
|
||||
int numDirectBits = (int)((posSlot >> 1) - 1);
|
||||
@@ -392,12 +393,12 @@ namespace SharpCompress.Compressors.LZMA
|
||||
if (posSlot < Base.K_END_POS_MODEL_INDEX)
|
||||
{
|
||||
_rep0 += await BitTreeDecoder.ReverseDecode(_posDecoders,
|
||||
_rep0 - posSlot - 1, rangeDecoder, numDirectBits);
|
||||
_rep0 - posSlot - 1, rangeDecoder, numDirectBits, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
_rep0 += (await rangeDecoder.DecodeDirectBitsAsync(numDirectBits - Base.K_NUM_ALIGN_BITS) << Base.K_NUM_ALIGN_BITS);
|
||||
_rep0 += await _posAlignDecoder.ReverseDecode(rangeDecoder);
|
||||
_rep0 += (await rangeDecoder.DecodeDirectBitsAsync(numDirectBits - Base.K_NUM_ALIGN_BITS, cancellationToken) << Base.K_NUM_ALIGN_BITS);
|
||||
_rep0 += await _posAlignDecoder.ReverseDecode(rangeDecoder, cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Compressors.LZMA.LZ;
|
||||
using SharpCompress.Compressors.LZMA.RangeCoder;
|
||||
@@ -1490,7 +1491,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
}
|
||||
|
||||
public async ValueTask CodeAsync(Stream inStream, Stream outStream,
|
||||
Int64 inSize, Int64 outSize, ICodeProgress progress)
|
||||
Int64 inSize, Int64 outSize, ICodeProgress progress, CancellationToken cancellationToken)
|
||||
{
|
||||
_needReleaseMfStream = false;
|
||||
_processingMode = false;
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
private LzmaStream() {}
|
||||
|
||||
public static async ValueTask<LzmaStream> CreateAsync(byte[] properties, Stream inputStream, long inputSize = -1, long outputSize = -1,
|
||||
Stream presetDictionary = null, bool? isLzma2 = null)
|
||||
Stream presetDictionary = null, bool? isLzma2 = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var ls = new LzmaStream();
|
||||
ls._inputStream = inputStream;
|
||||
@@ -57,7 +57,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
ls._outWindow.Train(presetDictionary);
|
||||
}
|
||||
|
||||
await ls._rangeDecoder.InitAsync(inputStream);
|
||||
await ls._rangeDecoder.InitAsync(inputStream, cancellationToken);
|
||||
|
||||
ls._decoder = new Decoder();
|
||||
ls._decoder.SetDecoderProperties(properties);
|
||||
@@ -152,7 +152,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
{
|
||||
if (_isLzma2)
|
||||
{
|
||||
await DecodeChunkHeader();
|
||||
await DecodeChunkHeader(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -175,7 +175,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
{
|
||||
_inputPosition += _outWindow.CopyStream(_inputStream, toProcess);
|
||||
}
|
||||
else if (await _decoder.CodeAsync(_dictionarySize, _outWindow, _rangeDecoder)
|
||||
else if (await _decoder.CodeAsync(_dictionarySize, _outWindow, _rangeDecoder, cancellationToken)
|
||||
&& _outputSize < 0)
|
||||
{
|
||||
_availableBytes = _outWindow.AvailableBytes;
|
||||
@@ -217,7 +217,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
return total;
|
||||
}
|
||||
|
||||
private async ValueTask DecodeChunkHeader()
|
||||
private async ValueTask DecodeChunkHeader(CancellationToken cancellationToken)
|
||||
{
|
||||
int control = _inputStream.ReadByte();
|
||||
_inputPosition++;
|
||||
@@ -269,7 +269,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
_decoder.SetDecoderProperties(Properties);
|
||||
}
|
||||
|
||||
await _rangeDecoder.InitAsync(_inputStream);
|
||||
await _rangeDecoder.InitAsync(_inputStream, cancellationToken);
|
||||
}
|
||||
else if (control > 0x02)
|
||||
{
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
@@ -144,19 +145,16 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
public Stream _stream;
|
||||
public long _total;
|
||||
|
||||
public async ValueTask InitAsync(Stream stream)
|
||||
public async ValueTask InitAsync(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
// Stream.Init(stream);
|
||||
_stream = stream;
|
||||
|
||||
_code = 0;
|
||||
_range = 0xFFFFFFFF;
|
||||
using var buffer = MemoryPool<byte>.Shared.Rent(1);
|
||||
var b = buffer.Memory.Slice(0,1);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
await _stream.ReadAsync(b);
|
||||
_code = (_code << 8) | b.Span[0];
|
||||
_code = (_code << 8) | await _stream.ReadByteAsync(cancellationToken);
|
||||
}
|
||||
_total = 5;
|
||||
}
|
||||
@@ -172,14 +170,11 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
return _stream.DisposeAsync();
|
||||
}
|
||||
|
||||
public async ValueTask NormalizeAsync()
|
||||
public async ValueTask NormalizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var buffer = MemoryPool<byte>.Shared.Rent(1);
|
||||
var b = buffer.Memory.Slice(0,1);
|
||||
while (_range < K_TOP_VALUE)
|
||||
{
|
||||
await _stream.ReadAsync(b);
|
||||
_code = (_code << 8) | b.Span[0];
|
||||
_code = (_code << 8) | await _stream.ReadByteAsync(cancellationToken);
|
||||
_range <<= 8;
|
||||
_total++;
|
||||
}
|
||||
@@ -190,20 +185,18 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
return _code / (_range /= total);
|
||||
}
|
||||
|
||||
public async ValueTask DecodeAsync(uint start, uint size)
|
||||
public async ValueTask DecodeAsync(uint start, uint size, CancellationToken cancellationToken)
|
||||
{
|
||||
_code -= start * _range;
|
||||
_range *= size;
|
||||
await NormalizeAsync();
|
||||
await NormalizeAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async ValueTask<uint> DecodeDirectBitsAsync(int numTotalBits)
|
||||
public async ValueTask<uint> DecodeDirectBitsAsync(int numTotalBits, CancellationToken cancellationToken)
|
||||
{
|
||||
uint range = _range;
|
||||
uint code = _code;
|
||||
uint result = 0;
|
||||
using var byteBuffer = MemoryPool<byte>.Shared.Rent(1);
|
||||
var bite = byteBuffer.Memory.Slice(0, 1);
|
||||
for (int i = numTotalBits; i > 0; i--)
|
||||
{
|
||||
range >>= 1;
|
||||
@@ -221,8 +214,7 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
|
||||
if (range < K_TOP_VALUE)
|
||||
{
|
||||
await _stream.ReadAsync(bite);
|
||||
code = (code << 8) | bite.Span[0];
|
||||
code = (code << 8) | await _stream.ReadByteAsync(cancellationToken);
|
||||
range <<= 8;
|
||||
_total++;
|
||||
}
|
||||
@@ -232,7 +224,7 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
return result;
|
||||
}
|
||||
|
||||
public async ValueTask<uint> DecodeBitAsync(uint size0, int numTotalBits)
|
||||
public async ValueTask<uint> DecodeBitAsync(uint size0, int numTotalBits, CancellationToken cancellationToken)
|
||||
{
|
||||
uint newBound = (_range >> numTotalBits) * size0;
|
||||
uint symbol;
|
||||
@@ -247,7 +239,7 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
_code -= newBound;
|
||||
_range -= newBound;
|
||||
}
|
||||
await NormalizeAsync();
|
||||
await NormalizeAsync(cancellationToken);
|
||||
return symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
@@ -112,10 +113,8 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
_prob = K_BIT_MODEL_TOTAL >> 1;
|
||||
}
|
||||
|
||||
public async ValueTask<uint> DecodeAsync(Decoder rangeDecoder)
|
||||
public async ValueTask<uint> DecodeAsync(Decoder rangeDecoder, CancellationToken cancellationToken)
|
||||
{
|
||||
using var byteBuffer = MemoryPool<byte>.Shared.Rent(1);
|
||||
var bite = byteBuffer.Memory.Slice(0, 1);
|
||||
uint newBound = (rangeDecoder._range >> K_NUM_BIT_MODEL_TOTAL_BITS) * _prob;
|
||||
if (rangeDecoder._code < newBound)
|
||||
{
|
||||
@@ -123,8 +122,7 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
_prob += (K_BIT_MODEL_TOTAL - _prob) >> K_NUM_MOVE_BITS;
|
||||
if (rangeDecoder._range < Decoder.K_TOP_VALUE)
|
||||
{
|
||||
await rangeDecoder._stream.ReadAsync(bite);
|
||||
rangeDecoder._code = (rangeDecoder._code << 8) | bite.Span[0];
|
||||
rangeDecoder._code = (rangeDecoder._code << 8) | await rangeDecoder._stream.ReadByteAsync(cancellationToken);
|
||||
rangeDecoder._range <<= 8;
|
||||
rangeDecoder._total++;
|
||||
}
|
||||
@@ -135,8 +133,7 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
_prob -= (_prob) >> K_NUM_MOVE_BITS;
|
||||
if (rangeDecoder._range < Decoder.K_TOP_VALUE)
|
||||
{
|
||||
await rangeDecoder._stream.ReadAsync(bite);
|
||||
rangeDecoder._code = (rangeDecoder._code << 8) | bite.Span[0];
|
||||
rangeDecoder._code = (rangeDecoder._code << 8) | await rangeDecoder._stream.ReadByteAsync(cancellationToken);
|
||||
rangeDecoder._range <<= 8;
|
||||
rangeDecoder._total++;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
@@ -122,23 +123,23 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<uint> DecodeAsync(Decoder rangeDecoder)
|
||||
public async ValueTask<uint> DecodeAsync(Decoder rangeDecoder, CancellationToken cancellationToken)
|
||||
{
|
||||
uint m = 1;
|
||||
for (int bitIndex = _numBitLevels; bitIndex > 0; bitIndex--)
|
||||
{
|
||||
m = (m << 1) + await _models[m].DecodeAsync(rangeDecoder);
|
||||
m = (m << 1) + await _models[m].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
}
|
||||
return m - ((uint)1 << _numBitLevels);
|
||||
}
|
||||
|
||||
public async ValueTask<uint> ReverseDecode(Decoder rangeDecoder)
|
||||
public async ValueTask<uint> ReverseDecode(Decoder rangeDecoder, CancellationToken cancellationToken)
|
||||
{
|
||||
uint m = 1;
|
||||
uint symbol = 0;
|
||||
for (int bitIndex = 0; bitIndex < _numBitLevels; bitIndex++)
|
||||
{
|
||||
uint bit = await _models[m].DecodeAsync(rangeDecoder);
|
||||
uint bit = await _models[m].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
m <<= 1;
|
||||
m += bit;
|
||||
symbol |= (bit << bitIndex);
|
||||
@@ -147,13 +148,13 @@ namespace SharpCompress.Compressors.LZMA.RangeCoder
|
||||
}
|
||||
|
||||
public static async ValueTask<uint> ReverseDecode(BitDecoder[] models, UInt32 startIndex,
|
||||
Decoder rangeDecoder, int numBitLevels)
|
||||
Decoder rangeDecoder, int numBitLevels, CancellationToken cancellationToken)
|
||||
{
|
||||
uint m = 1;
|
||||
uint symbol = 0;
|
||||
for (int bitIndex = 0; bitIndex < numBitLevels; bitIndex++)
|
||||
{
|
||||
uint bit = await models[startIndex + m].DecodeAsync(rangeDecoder);
|
||||
uint bit = await models[startIndex + m].DecodeAsync(rangeDecoder, cancellationToken);
|
||||
m <<= 1;
|
||||
m += bit;
|
||||
symbol |= (bit << bitIndex);
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace SharpCompress.Compressors.LZMA
|
||||
return inStreams.Single();
|
||||
case K_LZMA:
|
||||
case K_LZMA2:
|
||||
return await LzmaStream.CreateAsync(info, inStreams.Single(), -1, limit);
|
||||
return await LzmaStream.CreateAsync(info, inStreams.Single(), -1, limit, cancellationToken: cancellationToken);
|
||||
case CMethodId.K_AES_ID:
|
||||
return new AesDecoderStream(inStreams.Single(), info, pass, limit);
|
||||
case K_BCJ:
|
||||
|
||||
Reference in New Issue
Block a user