Skip to content

Commit

Permalink
A minor bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
AraHaan committed Jun 26, 2023
1 parent c4577c0 commit e904a3d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 deletions.
29 changes: 5 additions & 24 deletions src/ZlibSharp/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,15 @@
<Import Project="../../Directory.Build.props" />

<PropertyGroup>
<Version>1.2.13.1</Version>
<Version>1.2.13.2</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>
Added gzip support and more control over how compression/decompression is done based on the input Window Bits.

For simplicity reasons, the Window Bits are stored inside of a ZlibWindowBits enum.

Added types:
- ZlibCompressionStrategy
- ZlibWindowBits

Added members in MemoryZlib:
- IsCompressedByGZip
- Note: There is no version of the function for raw deflate streams due to the fact that they lack a header and a footer unline zlib/gzip compressed data.
- GetCompressedSize (works with deflate/zlib/gzip compressed data) No more worrying about over or even under allocating the buffers!!!
- GetDecompressedSize (works with deflate/zlib/gzip compressed data) No more worrying about over or even under allocating the buffers!!!

Changed members in MemoryZlib:
- Compress (both versions) adding optional parameters. Please recompile any existing code using these methods as the original would not be found if not recompiled.
- Decompress (both versions) adding optional parameters. Please recompile any existing code using these methods as the original would not be found if not recompiled.

Internal Changes:
- Migrated from deflateInit_/inflateInit_ to deflateInit2_/inflateInit2_ zlib apis due to needing more control over the compressed data.

The result:
- Performance improvement vs using System.IO.Compression.DeflateStream, System.IO.Compression.ZlibStream, and System.IO.Compression.GZipStream and supports more control over how to compress the data unlike those streams which lack said support.
A minor bug fix the the previous patch release that fixes the incorect default window bits values in the various MemoryZlib members that were changed/introduced in that update.
</PackageReleaseNotes>
<Description>
A high performance .NET zlib wrapper that is span based and offers more functionality over System.IO.Compression by allowing more developer control over how the data is compressed and by avoiding .NET's streams entirely.
</Description>
</PropertyGroup>

</Project>
12 changes: 6 additions & 6 deletions src/ZlibSharp/ZlibSharp/MemoryZlib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static unsafe class MemoryZlib
/// of the compressed/decompressed results.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ZlibResult Compress(string sourcePath, Span<byte> dest, ZlibCompressionLevel compressionLevel = ZlibCompressionLevel.DefaultCompression, ZlibWindowBits windowBits = ZlibWindowBits.Deflate, ZlibCompressionStrategy strategy = ZlibCompressionStrategy.Default)
public static ZlibResult Compress(string sourcePath, Span<byte> dest, ZlibCompressionLevel compressionLevel = ZlibCompressionLevel.DefaultCompression, ZlibWindowBits windowBits = ZlibWindowBits.Zlib, ZlibCompressionStrategy strategy = ZlibCompressionStrategy.Default)
=> Compress(File.ReadAllBytes(sourcePath), dest, compressionLevel, windowBits, strategy);

/// <summary>
Expand All @@ -58,7 +58,7 @@ public static ZlibResult Compress(string sourcePath, Span<byte> dest, ZlibCompre
/// of the compressed/decompressed results.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ZlibResult Compress(ReadOnlySpan<byte> source, Span<byte> dest, ZlibCompressionLevel compressionLevel = ZlibCompressionLevel.DefaultCompression, ZlibWindowBits windowBits = ZlibWindowBits.Deflate, ZlibCompressionStrategy strategy = ZlibCompressionStrategy.Default)
public static ZlibResult Compress(ReadOnlySpan<byte> source, Span<byte> dest, ZlibCompressionLevel compressionLevel = ZlibCompressionLevel.DefaultCompression, ZlibWindowBits windowBits = ZlibWindowBits.Zlib, ZlibCompressionStrategy strategy = ZlibCompressionStrategy.Default)
{
var bytesWritten = ZlibHelper.Compress(source, dest, compressionLevel, windowBits, strategy, out var adler32);
return new(bytesWritten, 0, adler32);
Expand All @@ -79,7 +79,7 @@ public static ZlibResult Compress(ReadOnlySpan<byte> source, Span<byte> dest, Zl
/// of the compressed/decompressed results.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ZlibResult Decompress(string sourcePath, Span<byte> dest, ZlibWindowBits windowBits = ZlibWindowBits.Deflate)
public static ZlibResult Decompress(string sourcePath, Span<byte> dest, ZlibWindowBits windowBits = ZlibWindowBits.Zlib)
=> Decompress(File.ReadAllBytes(sourcePath), dest, windowBits);

/// <summary>
Expand All @@ -97,7 +97,7 @@ public static ZlibResult Decompress(string sourcePath, Span<byte> dest, ZlibWind
/// of the compressed/decompressed results.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ZlibResult Decompress(ReadOnlySpan<byte> source, Span<byte> dest, ZlibWindowBits windowBits = ZlibWindowBits.Deflate)
public static ZlibResult Decompress(ReadOnlySpan<byte> source, Span<byte> dest, ZlibWindowBits windowBits = ZlibWindowBits.Zlib)
{
var bytesRead = ZlibHelper.Decompress(source, dest, out var bytesWritten, out var adler32, windowBits);
return new ZlibResult(bytesWritten, bytesRead, adler32);
Expand Down Expand Up @@ -226,7 +226,7 @@ public static ulong ZlibGetCrc32(ReadOnlySpan<byte> data)
/// </exception>
/// <returns>The size of the data when it is compressed.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint GetCompressedSize(ReadOnlySpan<byte> source, ZlibCompressionLevel compressionLevel = ZlibCompressionLevel.DefaultCompression, ZlibWindowBits windowBits = ZlibWindowBits.Deflate)
public static uint GetCompressedSize(ReadOnlySpan<byte> source, ZlibCompressionLevel compressionLevel = ZlibCompressionLevel.DefaultCompression, ZlibWindowBits windowBits = ZlibWindowBits.Zlib)
{
var discard = new byte[source.Length];
var result = Compress(source, discard, compressionLevel, windowBits);
Expand All @@ -242,7 +242,7 @@ public static uint GetCompressedSize(ReadOnlySpan<byte> source, ZlibCompressionL
/// Thrown when zlib errors internally in any way.
/// </exception>
/// <returns>The size of the data when it is decompressed.</returns>
public static uint GetDecompressedSize(ReadOnlySpan<byte> source, ZlibWindowBits windowBits = ZlibWindowBits.Deflate)
public static uint GetDecompressedSize(ReadOnlySpan<byte> source, ZlibWindowBits windowBits = ZlibWindowBits.Zlib)
{
var discard = new byte[Array.MaxLength];
var result = Decompress(source, discard, windowBits);
Expand Down
Binary file modified tests/CompressedText.txt
Binary file not shown.
8 changes: 2 additions & 6 deletions tests/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void DecompressionToOverAllocatedBufferShouldHaveBytesWrittenEqualToSourc

[Fact]
public void ZlibSharpVersionWorks()
=> _ = MemoryZlib.ZlibSharpVersion().Should().Be("1.2.13.1");
=> _ = MemoryZlib.ZlibSharpVersion().Should().Be("1.2.13.2");

[Fact]
public void ZlibVersionWorks()
Expand All @@ -95,11 +95,7 @@ public void IsCompressedByZlibWorksAndIsFalse()

[Fact]
public void IsCompressedByZlibWorksAndIsTrue()
{
var destBuffer = new byte[MemoryZlib.GetCompressedSize(sourceString)];
_ = MemoryZlib.Compress(sourceString, destBuffer, windowBits: ZlibWindowBits.Zlib);
_ = MemoryZlib.IsCompressedByZlib(destBuffer).Should().BeTrue();
}
=> _ = MemoryZlib.IsCompressedByZlib("CompressedText.txt").Should().BeTrue();

[Fact]
public void IsCompressedByZlibFailure()
Expand Down

0 comments on commit e904a3d

Please sign in to comment.