Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.1.1 / zlib 1.3.1 #1

Merged
merged 12 commits into from
Mar 3, 2024
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
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,5 @@ dotnet_analyzer_diagnostic.severity = warning
dotnet_diagnostic.IDE0010.severity = none
dotnet_diagnostic.IDE0072.severity = none
dotnet_diagnostic.IDE0079.severity = none
dotnet_diagnostic.IDE0057.severity = suggestion
dotnet_diagnostic.IDE0057.severity = suggestion
dotnet_diagnostic.IDE0005.severity = suggestion
15 changes: 6 additions & 9 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Setup the .NET 6 & 7 SDK
- name: Setup the .NET 8 SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x

- name: Get Version From Tag
id: version
Expand All @@ -35,24 +34,22 @@ jobs:
patch=${parts[2]}
echo ::set-output name=version::$major.$minor.$patch

- name: Build for .NET 6 & 7
- name: Build for .NET 8
shell: bash
run: |
version=${{ steps.version.outputs.version }}
dotnet build src/ZLibDotNet/ZLibDotNet.csproj -c Release -p:Version=$version -p:ContinuousIntegrationBuild=true
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net6.0 -p:Version=$version
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net7.0 -p:Version=$version
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net8.0 -p:Version=$version

- name: Build for .NET Framework 4.6.2
if: ${{ matrix.os == 'windows-latest' }}
shell: bash
run: |
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net462 -p:Version=${{ steps.version.outputs.version }}

- name: Test on .NET 6 & 7
- name: Test on .NET 8
run: |
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net6.0 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net7.0 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net8.0 --no-build --no-restore

- name: Test on .NET Framework 4.6.2
if: ${{ matrix.os == 'windows-latest' }}
Expand Down
25 changes: 10 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,33 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Setup the .NET 6 & 7 SDK
- name: Setup the .NET 8 SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x

- name: Build for .NET 6 & 7 on macOS and Ubuntu
- name: Build for .NET 8 on macOS and Ubuntu
if: ${{ matrix.os != 'windows-latest' }}
run: |
dotnet build src/ZLibDotNet/ZLibDotNet.csproj -c Release
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net6.0
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net7.0
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net8.0

- name: Build for .NET Framework 4.6.2, .NET 6 and .NET 7 on Windows (x86 and x64)
- name: Build for .NET Framework 4.6.2 and .NET 8 on Windows (x86 and x64)
if: ${{ matrix.os == 'windows-latest' }}
run: |
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -r win-x86 --no-self-contained
dotnet build tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -r win-x64 --no-self-contained

- name: Test on .NET 6 & 7 on macOS and Ubuntu
- name: Test on .NET 8 on macOS and Ubuntu
if: ${{ matrix.os != 'windows-latest' }}
run: |
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net6.0 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net7.0 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net8.0 --no-build --no-restore

- name: Test on .NET Framework 4.6.2, .NET 6 and .NET 7 on Windows (x86 and x64)
- name: Test on .NET Framework 4.6.2 and .NET 8 on Windows (x86 and x64)
if: ${{ matrix.os == 'windows-latest' }}
run: |
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net462 -r win-x86 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net462 -r win-x64 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net6.0 -r win-x86 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net6.0 -r win-x64 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net7.0 -r win-x86 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net7.0 -r win-x64 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net8.0 -r win-x86 --no-build --no-restore
dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c Release -f net8.0 -r win-x64 --no-build --no-restore
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Follow these steps when contributing code to this repository:

The [.editorconfig](.editorconfig) file defines the code style. Do not edit this one or the [project file](https://github.com/mgnsm/ZLibDotNet/blob/main/src/ZLibDotNet/ZLibDotNet.csproj) unless these files are directly related to your changes.

5. Build and test the code in both debug and release mode using the [.NET 7 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/7.0):
5. Build and test the code in both debug and release mode using the [.NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0):

dotnet test tests/ZLibDotNet.UnitTests/ZLibDotNet.UnitTests.csproj -c release

Expand Down
8 changes: 4 additions & 4 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Copyright for the C#/.NET code in this project is held by Magnus Montin.

The implementation is heavily based on version 1.2.13 of the native zlib data compression library (https://zlib.net/) that was written by and for which copyright is held by Jean-loup Gailly and Mark Adler.
The implementation is heavily based on version 1.3.1 of the native zlib data compression library (https://zlib.net/) that was written by and for which copyright is held by Jean-loup Gailly and Mark Adler.

Copyright (c) 2022-2023 Magnus Montin
Copyright (c) 2022-2024 Magnus Montin

Copyright (c) 1995-2022 Jean-loup Gailly and Mark Adler
Copyright (c) 1995-2024 Jean-loup Gailly and Mark Adler

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
Portions Copyright (c) 2022-2023 Magnus Montin or Copyright (c) 1995-2022 Jean-loup Gailly and Mark Adler
Portions Copyright (c) 2022-2024 Magnus Montin or Copyright (c) 1995-2024 Jean-loup Gailly and Mark Adler.

2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![Build Status](https://github.com/mgnsm/ZLibDotNet/actions/workflows/ci.yml/badge.svg)
[![NuGet Badge](https://img.shields.io/nuget/v/ZLibDotNet.svg)](https://www.nuget.org/packages/ZLibDotNet/)

A fully managed and [performant](tests/ZLibDotNet.Benchmarks) C#/.NET Standard 1.3 compatible implementation of the [zlib compression library](https://www.zlib.net/) which provides in-memory compression, decompression, and integrity checks of uncompressed data in the zlib ([RFC (Request for Comments) 1950](https://datatracker.ietf.org/doc/html/rfc1950)) and raw deflate ([RFC 1951](https://datatracker.ietf.org/doc/html/rfc1951)) data formats (but not the gzip or zip formats).
A fully managed, [performant](tests/ZLibDotNet.Benchmarks) and modern C# (.NET Standard 1.3 compatible) implementation of the [zlib compression library](https://www.zlib.net/) which provides in-memory compression, decompression, and integrity checks of uncompressed data in the zlib ([RFC (Request for Comments) 1950](https://datatracker.ietf.org/doc/html/rfc1950)) and raw deflate ([RFC 1951](https://datatracker.ietf.org/doc/html/rfc1951)) data formats (but not the gzip or zip formats).

## zlib support in .NET
If you simply want to compress or decompress data in the zlib format in a .NET application, there is a `ZLibStream` class for doing this in .NET 6 and later versions:
Expand Down Expand Up @@ -64,7 +64,7 @@ Debug.Assert(MemoryExtensions.SequenceEqual(inputData, uncomressedData));
```
Check out the [unit tests](https://github.com/mgnsm/ZLibDotNet/tree/main/tests/ZLibDotNet.UnitTests) for more examples.
### Implementation
`ZStream` has been named after the equivalent `z_stream` type in the ported library. It breaks the [CA1711](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1711) naming rule as it's not a `System.IO.Stream` but a `ref struct`.
`ZStream` has been named after the equivalent `z_stream` type in the ported library. It intentionally breaks the [CA1711](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1711) naming rule as it's not a `System.IO.Stream` but a `ref struct`.

`Input` is a `ReadOnlySpan<byte>` property that defines the input buffer of the data to be compressed or uncompressed. The optional `int` properties `NextIn` and `AvailableIn` can be used to specify a starting position in the buffer and the maximum number of bytes to consume from it respectively.

Expand Down
2 changes: 1 addition & 1 deletion src/ZLibDotNet/Adler32.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2011, 2016 Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

namespace ZLibDotNet;

Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Compressor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2005, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

using System;
using ZLibDotNet.Deflate;
Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/BlockState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

namespace ZLibDotNet.Deflate;

Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/Config.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

namespace ZLibDotNet.Deflate;

Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly, Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly, Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

namespace ZLibDotNet.Deflate;

Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/DeflateState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2018 Jean-loup Gailly
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

using static ZLibDotNet.Deflate.Constants;

Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/Deflater.DeflateEnd.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

using System.Buffers;

Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/Deflater.DeflateInit.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

using System;
using System.Buffers;
Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/Deflater.DeflateParams.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

using System.Runtime.InteropServices;

Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/Deflater.DeflateReset.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

namespace ZLibDotNet.Deflate;

Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/Deflater.DeflateSetDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

using System;
using System.Runtime.InteropServices;
Expand Down
34 changes: 17 additions & 17 deletions src/ZLibDotNet/Deflate/Deflater.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

using System;
using System.Diagnostics;
Expand Down Expand Up @@ -48,16 +48,16 @@ memory checker errors from longest match routines */

internal static readonly Config[] s_configuration_table = new Config[10]
{
new Config(0, 0, 0, 0, Config.DeflateType.Stored), // 0: store only
new Config(4, 4, 8, 4, Config.DeflateType.Fast), // 1: max speed, no lazy matches
new Config(4, 5, 16, 8, Config.DeflateType.Fast), // 2
new Config(4, 6, 32, 32, Config.DeflateType.Fast), // 3
new Config(4, 4, 16, 16, Config.DeflateType.Slow), // 4: lazy matches
new Config(8, 16, 32, 32, Config.DeflateType.Slow), // 5
new Config(8, 16, 128, 128, Config.DeflateType.Slow), // 6
new Config(8, 32, 128, 256, Config.DeflateType.Slow), // 7
new Config(32, 128, 258, 1024, Config.DeflateType.Slow), // 8
new Config(32, 258, 258, 4096, Config.DeflateType.Slow) // 9: max compression
new(0, 0, 0, 0, Config.DeflateType.Stored), // 0: store only
new(4, 4, 8, 4, Config.DeflateType.Fast), // 1: max speed, no lazy matches
new(4, 5, 16, 8, Config.DeflateType.Fast), // 2
new(4, 6, 32, 32, Config.DeflateType.Fast), // 3
new(4, 4, 16, 16, Config.DeflateType.Slow), // 4: lazy matches
new(8, 16, 32, 32, Config.DeflateType.Slow), // 5
new(8, 16, 128, 128, Config.DeflateType.Slow), // 6
new(8, 32, 128, 256, Config.DeflateType.Slow), // 7
new(32, 128, 258, 1024, Config.DeflateType.Slow), // 8
new(32, 258, 258, 4096, Config.DeflateType.Slow) // 9: max compression
};

private static readonly int[] s_base_dist = new int[DCodes] {
Expand Down Expand Up @@ -402,7 +402,7 @@ private static void LongestMatchInit(ref ZStream strm)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int ReturnWithError(ref ZStream strm, int err)
{
strm.msg = s_z_errmsg[Z_NEED_DICT - err];
strm.msg = s_z_errmsg[err < -6 || err > 2 ? 9 : 2 - err];
return err;
}

Expand Down Expand Up @@ -1544,8 +1544,8 @@ private static uint LongestMatch(DeflateState s, uint cur_match, ref byte window
{
uint chain_length = s.max_chain_length; // max hash chain length
ref byte scan = ref Unsafe.Add(ref window, s.strstart); // current string
uint len; // length of current match
uint best_len = s.prev_length; // best match length so far
int len; // length of current match
int best_len = (int)s.prev_length; // best match length so far
int nice_match = s.nice_match; // stop if match long enough
uint limit = s.strstart > MaxDist(s) ? s.strstart - MaxDist(s) : 0;
/* Stop when cur_match becomes <= limit. To simplify the code,
Expand Down Expand Up @@ -1621,7 +1621,7 @@ private static uint LongestMatch(DeflateState s, uint cur_match, ref byte window

Debug.Assert(scan <= window + (s.window_size - 1), "wild scan");

len = MaxMatch - (uint)netUnsafe.ByteOffset(ref scan, ref strend);
len = MaxMatch - (int)netUnsafe.ByteOffset(ref scan, ref strend);
scan = ref Unsafe.Subtract(ref strend, (uint)MaxMatch);

if (len > best_len)
Expand All @@ -1636,7 +1636,7 @@ private static uint LongestMatch(DeflateState s, uint cur_match, ref byte window
} while ((cur_match = Unsafe.Add(ref prev, cur_match & wmask)) > limit && --chain_length != 0);

if (best_len <= s.lookahead)
return best_len;
return (uint)best_len;

return s.lookahead;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ZLibDotNet/Deflate/StaticTree.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Original code and comments Copyright (C) 1995-2021 Jean-loup Gailly
// Managed C#/.NET code Copyright (C) 2022-2023 Magnus Montin
// Original code and comments Copyright (C) 1995-2024 Jean-loup Gailly
// Managed C#/.NET code Copyright (C) 2022-2024 Magnus Montin

namespace ZLibDotNet.Deflate;

Expand Down
Loading
Loading