Skip to content

Commit 096647c

Browse files
committed
Changes in 1.2.13.5.
- Made ZlibStatus internal. - Replaced Checks static class with: - ZlibEncoder and ZlibDecoder extension classes. - Made ZlibEncoder and ZlibDecoder constructors internal. - Made ZlibEncoder and ZlibDecoder into classes. - The ZlibEncoder and ZlibDecoder classes now contain a ComputeHash method. - The properties of ZlibEncoder and ZlibDecoder are now simplified to provide an Options property that contains all the options for the encoder/decoder. - A new ZlibOptions class which contains all the options for the encoder/decoder. - The ZlibResult class's Status property is now of type OperationStatus. - The ZlibResult class's Crc32 and Adler32 properties have been combined into a single hash property.
1 parent 2c453fd commit 096647c

19 files changed

+460
-497
lines changed

.github/workflows/dotnet-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ jobs:
2525
- name: Install latest .NET SDK
2626
uses: Elskom/setup-latest-dotnet@main
2727
with:
28-
SDK_VERSION: '7.0.100'
29-
RUNTIME_VERSIONS: '6.0.x'
28+
SDK_VERSION: '9.0.100'
29+
RUNTIME_VERSIONS: '8.0.x'
3030

3131
- name: Restore, Build, Test, Pack, and Push.
3232
uses: Elskom/build-dotnet@main

.github/workflows/dotnet.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ jobs:
2525
- name: Install latest .NET SDK
2626
uses: Elskom/setup-latest-dotnet@main
2727
with:
28-
SDK_VERSION: '7.0.100'
29-
RUNTIME_VERSIONS: '6.0.x'
28+
SDK_VERSION: '9.0.100'
29+
RUNTIME_VERSIONS: '8.0.x'
3030

3131
- name: Restore, Build, Test, and Pack.
3232
uses: Elskom/build-dotnet@main

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<DebugType>embedded</DebugType>

src/ZlibSharp/Directory.Build.props

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
<Import Project="../../Directory.Build.props" />
44

55
<PropertyGroup>
6-
<Version>1.2.13.4</Version>
6+
<Version>1.2.13.5</Version>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<PackageReleaseNotes>
10-
- Made ZlibStatus public.
11-
- Replaced MemoryZlib static class with:
12-
- Checks inside of a `Checks` static class.
13-
- Compression methods inside of a new ZlibEncoder class.*
14-
- Decompression methods inside of a new ZlibDecoder class.*
15-
- Added Crc32 and Status members to the ZlibResult structure.
16-
* The new ZlibEncoder has new TryCompress methods while the new ZlibDecoder has new TryDecompress methods.
10+
- Made ZlibStatus internal.
11+
- Replaced Checks static class with:
12+
- ZlibEncoder and ZlibDecoder extension classes.
13+
- Made ZlibEncoder and ZlibDecoder constructors internal.
14+
- Made ZlibEncoder and ZlibDecoder into classes.
15+
- The ZlibEncoder and ZlibDecoder classes now contain a ComputeHash method.
16+
- The properties of ZlibEncoder and ZlibDecoder are now simplified to provide an Options property that contains all the options for the encoder/decoder.
17+
- A new ZlibOptions class which contains all the options for the encoder/decoder.
18+
- The ZlibResult class's Status property is now of type OperationStatus.
19+
- The ZlibResult class's Crc32 and Adler32 properties have been combined into a single hash property.
1720
</PackageReleaseNotes>
1821
<Description>
1922
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.

src/ZlibSharp/ZlibSharp/Checks.cs

Lines changed: 0 additions & 173 deletions
This file was deleted.

src/ZlibSharp/ZlibSharp/Exceptions/NotPackableException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public NotPackableException(string s, Exception ex)
3737
/// </summary>
3838
/// <param name="info">The data for serializing or deserializing the object.</param>
3939
/// <param name="context">The source and destination for the object.</param>
40+
[Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
4041
private NotPackableException(SerializationInfo info, StreamingContext context)
4142
: base(info, context)
4243
{

src/ZlibSharp/ZlibSharp/Exceptions/NotUnpackableException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public NotUnpackableException(string s, Exception ex)
3838
/// </summary>
3939
/// <param name="info">The data for serializing or deserializing the object.</param>
4040
/// <param name="context">The source and destination for the object.</param>
41+
[Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId = "SYSLIB0051", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
4142
private NotUnpackableException(SerializationInfo info, StreamingContext context)
4243
: base(info, context)
4344
{
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
namespace ZlibSharp.Extensions;
2+
3+
using ZlibSharp.Exceptions;
4+
5+
/// <summary>
6+
/// Extension methods for the <see cref="ZlibDecoder" /> class.
7+
/// </summary>
8+
public static class ZlibDecoderExtensions
9+
{
10+
/// <summary>
11+
/// Gets the size of the data when it is decompressed. Useful for when the data actually gets decompressed.
12+
/// </summary>
13+
/// <param name="decoder">The <see cref="ZlibDecoder" /> instance to use.</param>
14+
/// <param name="source">The compressed input data.</param>
15+
/// <exception cref="NotUnpackableException">
16+
/// Thrown when zlib errors internally in any way.
17+
/// </exception>
18+
/// <returns>The size of the data when it is decompressed.</returns>
19+
public static uint GetDecompressedSize(this ZlibDecoder decoder, ReadOnlySpan<byte> source)
20+
{
21+
var discard = new byte[Array.MaxLength];
22+
_ = decoder.TryDecompress(source, discard, out var result);
23+
return result.BytesWritten;
24+
}
25+
26+
/// <summary>
27+
/// Check data for compression by gzip.
28+
/// </summary>
29+
/// <param name="_">The <see cref="ZlibDecoder" /> instance to use.</param>
30+
/// <param name="source">Input data.</param>
31+
/// <returns>Returns <see langword="true" /> if data is compressed by gzip, else <see langword="false" />.</returns>
32+
/// <exception cref="ArgumentNullException">When <paramref name="source"/> is <see langword="null" />.</exception>
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
public static bool IsCompressedByGZip(this ZlibDecoder _, ReadOnlySpan<byte> source)
35+
{
36+
if (source.Length >= 2)
37+
{
38+
ref var sourceRef = ref MemoryMarshal.GetReference(source);
39+
var byte1 = sourceRef;
40+
var byte2 = Unsafe.Add(ref sourceRef, 1);
41+
return byte1 is 0x1F && byte2 is 0x8B;
42+
}
43+
44+
throw new ArgumentNullException(nameof(source));
45+
}
46+
47+
/// <summary>
48+
/// Check data for compression by gzip.
49+
/// </summary>
50+
/// <param name="decoder">The <see cref="ZlibDecoder" /> instance to use.</param>
51+
/// <param name="path">The file to check on if it is compressed by gzip.</param>
52+
/// <returns>Returns <see langword="true" /> if data is compressed by gzip, else <see langword="false" />.</returns>
53+
/// <exception cref="ArgumentNullException">When <paramref name="path"/> is <see langword="null" /> or <see cref="string.Empty"/>.</exception>
54+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
55+
public static bool IsCompressedByGZip(this ZlibDecoder decoder, string path)
56+
=> IsCompressedByGZip(decoder, File.ReadAllBytes(path));
57+
58+
/// <summary>
59+
/// Check data for compression by zlib.
60+
/// </summary>
61+
/// <param name="_">The <see cref="ZlibDecoder" /> instance to use.</param>
62+
/// <param name="source">Input data.</param>
63+
/// <returns>Returns <see langword="true" /> if data is compressed by zlib, else <see langword="false" />.</returns>
64+
/// <exception cref="ArgumentNullException">When <paramref name="source"/> is <see langword="null" />.</exception>
65+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
66+
public static bool IsCompressedByZlib(this ZlibDecoder _, ReadOnlySpan<byte> source)
67+
{
68+
if (source.Length >= 2)
69+
{
70+
ref var sourceRef = ref MemoryMarshal.GetReference(source);
71+
var byte1 = sourceRef;
72+
var byte2 = Unsafe.Add(ref sourceRef, 1);
73+
return IsZlibHeader(byte1, byte2);
74+
}
75+
76+
throw new ArgumentNullException(nameof(source));
77+
}
78+
79+
/// <summary>
80+
/// Check data for compression by zlib.
81+
/// </summary>
82+
/// <param name="decoder">The <see cref="ZlibDecoder" /> instance to use.</param>
83+
/// <param name="path">The file to check on if it is compressed by zlib.</param>
84+
/// <returns>Returns <see langword="true" /> if data is compressed by zlib, else <see langword="false" />.</returns>
85+
/// <exception cref="ArgumentNullException">When <paramref name="path"/> is <see langword="null" /> or <see cref="string.Empty"/>.</exception>
86+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
87+
public static bool IsCompressedByZlib(this ZlibDecoder decoder, string path)
88+
=> IsCompressedByZlib(decoder, File.ReadAllBytes(path));
89+
90+
[ExcludeFromCodeCoverage]
91+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92+
private static bool IsZlibHeader(byte byte1, byte byte2)
93+
=> byte1 is 0x78 && byte2 is 0x01 or 0x5E or 0x9C or 0xDA;
94+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace ZlibSharp.Extensions;
2+
3+
using ZlibSharp.Exceptions;
4+
5+
/// <summary>
6+
/// Extension methods for the <see cref="ZlibEncoder" /> class.
7+
/// </summary>
8+
public static class ZlibEncoderExtensions
9+
{
10+
/// <summary>
11+
/// Gets the size of the data when it is compressed. Useful for when the data actually gets compressed.
12+
/// </summary>
13+
/// <param name="encoder">The <see cref="ZlibEncoder" /> instance to use.</param>
14+
/// <param name="source">The input data buffer.</param>
15+
/// <exception cref="NotPackableException">
16+
/// Thrown when zlib errors internally in any way.
17+
/// </exception>
18+
/// <returns>The size of the data when it is compressed.</returns>
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20+
public static uint GetCompressedSize(this ZlibEncoder encoder, ReadOnlySpan<byte> source)
21+
{
22+
var discard = new byte[source.Length];
23+
_ = encoder.TryCompress(source, discard, out var result);
24+
return result.BytesWritten;
25+
}
26+
}

0 commit comments

Comments
 (0)