Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/SharpCompress/Common/Rar/RarCryptoWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public override Task<int> ReadAsync(
int offset,
int count,
CancellationToken cancellationToken
) => ReadAndDecryptAsync(buffer, offset, count, cancellationToken);
) => ReadAndDecryptAsync(buffer, offset, count, cancellationToken).AsTask();

private async Task<int> ReadAndDecryptAsync(
private async ValueTask<int> ReadAndDecryptAsync(
byte[] buffer,
int offset,
int count,
Expand Down
34 changes: 30 additions & 4 deletions src/SharpCompress/Compressors/LZMA/LZ/LzBinTree.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable disable

using System;
using System.Buffers;
using System.IO;

namespace SharpCompress.Compressors.LZMA.LZ;
Expand All @@ -11,8 +12,8 @@ internal sealed class BinTree : InWindow
private uint _cyclicBufferSize;
private uint _matchMaxLen;

private uint[] _son;
private uint[] _hash;
private uint[] _son = [];
private uint[] _hash = [];

private uint _cutValue = 0xFF;
private uint _hashMask;
Expand Down Expand Up @@ -108,7 +109,12 @@ uint keepAddBufferAfter
var cyclicBufferSize = historySize + 1;
if (_cyclicBufferSize != cyclicBufferSize)
{
_son = new uint[(_cyclicBufferSize = cyclicBufferSize) * 2];
if (_son.Length != 0)
{
ArrayPool<uint>.Shared.Return(_son);
}
_cyclicBufferSize = cyclicBufferSize;
_son = ArrayPool<uint>.Shared.Rent(checked((int)(_cyclicBufferSize * 2)));
}

var hs = K_BT2_HASH_SIZE;
Expand All @@ -132,7 +138,27 @@ uint keepAddBufferAfter
}
if (hs != _hashSizeSum)
{
_hash = new uint[_hashSizeSum = hs];
if (_hash.Length != 0)
{
ArrayPool<uint>.Shared.Return(_hash);
}
_hashSizeSum = hs;
_hash = ArrayPool<uint>.Shared.Rent(checked((int)_hashSizeSum));
}
}

public override void Dispose()
{
base.Dispose();
if (_son.Length != 0)
{
ArrayPool<uint>.Shared.Return(_son);
_son = [];
}
if (_hash.Length != 0)
{
ArrayPool<uint>.Shared.Return(_hash);
_hash = [];
}
}

Expand Down
23 changes: 20 additions & 3 deletions src/SharpCompress/Compressors/LZMA/LZ/LzInWindow.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#nullable disable

using System;
using System.Buffers;
using System.IO;

namespace SharpCompress.Compressors.LZMA.LZ;

internal class InWindow
internal class InWindow : IDisposable
{
public byte[] _bufferBase; // pointer to buffer with data
private Stream _stream;
Expand Down Expand Up @@ -78,7 +80,22 @@ public virtual void ReadBlock()
}
}

private void Free() => _bufferBase = null;
private void Free()
{
if (_bufferBase is null)
{
return;
}

ArrayPool<byte>.Shared.Return(_bufferBase);
_bufferBase = null;
}

public virtual void Dispose()
{
ReleaseStream();
Free();
}

public void Create(uint keepSizeBefore, uint keepSizeAfter, uint keepSizeReserv)
{
Expand All @@ -89,7 +106,7 @@ public void Create(uint keepSizeBefore, uint keepSizeAfter, uint keepSizeReserv)
{
Free();
_blockSize = blockSize;
_bufferBase = new byte[_blockSize];
_bufferBase = ArrayPool<byte>.Shared.Rent(checked((int)_blockSize));
}
_pointerToLastSafePosition = _blockSize - keepSizeAfter;
_streamEndWasReached = false;
Expand Down
49 changes: 29 additions & 20 deletions src/SharpCompress/Compressors/LZMA/LZipStream.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,31 +126,40 @@ CancellationToken cancellationToken
)
{
// Read the header
byte[] header = new byte[6];
var n = await stream
.ReadAsync(header, 0, header.Length, cancellationToken)
.ConfigureAwait(false);
var header = ArrayPool<byte>.Shared.Rent(6);
try
{
var n = await stream.ReadAsync(header, 0, 6, cancellationToken).ConfigureAwait(false);

// TODO: Handle reading only part of the header?
// TODO: Handle reading only part of the header?

if (n != 6)
{
return 0;
}
if (n != 6)
{
return 0;
}

if (
header[0] != 'L'
|| header[1] != 'Z'
|| header[2] != 'I'
|| header[3] != 'P'
|| header[4] != 1 /* version 1 */
)
if (
header[0] != 'L'
|| header[1] != 'Z'
|| header[2] != 'I'
|| header[3] != 'P'
|| header[4] != 1 /* version 1 */
)
{
return 0;
}
var basePower = header[5] & 0x1F;
var subtractionNumerator = (header[5] & 0xE0) >> 5;
if (basePower < 4 || basePower > 30)
{
return 0;
}
return (1 << basePower) - (subtractionNumerator * (1 << (basePower - 4)));
Comment thread
adamhathcock marked this conversation as resolved.
}
finally
{
return 0;
ArrayPool<byte>.Shared.Return(header);
}
var basePower = header[5] & 0x1F;
var subtractionNumerator = (header[5] & 0xE0) >> 5;
return (1 << basePower) - (subtractionNumerator * (1 << (basePower - 4)));
}

#if !LEGACY_DOTNET
Expand Down
Loading
Loading