Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions SharpCompress.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Config", "Config", "{CDB425
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpCompress.Performance", "tests\SharpCompress.Performance\SharpCompress.Performance.csproj", "{5BDE6DBC-9E5F-4E21-AB71-F138A3E72B17}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpCompress.AotSmoke", "tests\SharpCompress.AotSmoke\SharpCompress.AotSmoke.csproj", "{50C6DA22-2124-4E47-9FB7-96FEC5F59566}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -52,6 +54,10 @@ Global
{5BDE6DBC-9E5F-4E21-AB71-F138A3E72B17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5BDE6DBC-9E5F-4E21-AB71-F138A3E72B17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5BDE6DBC-9E5F-4E21-AB71-F138A3E72B17}.Release|Any CPU.Build.0 = Release|Any CPU
{50C6DA22-2124-4E47-9FB7-96FEC5F59566}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50C6DA22-2124-4E47-9FB7-96FEC5F59566}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50C6DA22-2124-4E47-9FB7-96FEC5F59566}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50C6DA22-2124-4E47-9FB7-96FEC5F59566}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -60,5 +66,6 @@ Global
{FD19DDD8-72B2-4024-8665-0D1F7A2AA998} = {3C5BE746-03E5-4895-9988-0B57F162F86C}
{F2B1A1EB-0FA6-40D0-8908-E13247C7226F} = {0F0901FF-E8D9-426A-B5A2-17C7F47C1529}
{5BDE6DBC-9E5F-4E21-AB71-F138A3E72B17} = {0F0901FF-E8D9-426A-B5A2-17C7F47C1529}
{50C6DA22-2124-4E47-9FB7-96FEC5F59566} = {0F0901FF-E8D9-426A-B5A2-17C7F47C1529}
EndGlobalSection
EndGlobal
30 changes: 28 additions & 2 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 Down Expand Up @@ -108,7 +109,12 @@ uint keepAddBufferAfter
var cyclicBufferSize = historySize + 1;
if (_cyclicBufferSize != cyclicBufferSize)
{
_son = new uint[(_cyclicBufferSize = cyclicBufferSize) * 2];
if (_son is not null)
{
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 is not null)
{
ArrayPool<uint>.Shared.Return(_hash);
}
_hashSizeSum = hs;
_hash = ArrayPool<uint>.Shared.Rent(checked((int)_hashSizeSum));
}
}

public override void Dispose()
{
base.Dispose();
if (_son is not null)
{
ArrayPool<uint>.Shared.Return(_son);
_son = null;
}
if (_hash is not null)
{
ArrayPool<uint>.Shared.Return(_hash);
_hash = null;
}
}

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
45 changes: 25 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,36 @@ 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;
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