-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Made ZlibStatus public. - Replaced MemoryZlib static class with: - Checks inside of a `Checks` static class. - Compression methods inside of a new ZlibEncoder class.* - Decompression methods inside of a new ZlibDecoder class.* - Added Crc32 and Status members to the ZlibResult structure. * The new ZlibEncoder has new TryCompress methods while the new ZlibDecoder has new TryDecompress methods.
Showing
12 changed files
with
425 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright (c) 2021~2022, Els_kom org. | ||
// https://github.com/Elskom/ | ||
// All rights reserved. | ||
// license: MIT, see LICENSE for more details. | ||
|
||
namespace ZlibSharp; | ||
|
||
using Internal; | ||
using Exceptions; | ||
|
||
/// <summary> | ||
/// Zlib Memory Decompression class. | ||
/// </summary> | ||
/// <param name="WindowBits"> Gets or sets the window bits to use to decompress the data. </param> | ||
public record ZlibDecoder(ZlibWindowBits WindowBits) | ||
{ | ||
public ZlibDecoder() : this(ZlibWindowBits.Zlib) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Decompresses a file. | ||
/// </summary> | ||
/// <param name="sourcePath">The path to the file to decompress.</param> | ||
/// <param name="dest">The decompressed data buffer.</param> | ||
/// <param name="result"> | ||
/// The zlib result structure that contains the amount of bytes read, written, | ||
/// and the adler32 hash of the data that can be used to compare the integrity | ||
/// of the compressed/decompressed results. | ||
/// </param> | ||
/// <returns> | ||
/// <see langword="true"/> if the compression was a success, <see langword="false"/> otherwise. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool TryDecompress(string sourcePath, Span<byte> dest, out ZlibResult? result) | ||
=> this.TryDecompress(File.ReadAllBytes(sourcePath), dest, out result); | ||
|
||
/// <summary> | ||
/// Decompresses data. | ||
/// </summary> | ||
/// <param name="source">The compressed input data.</param> | ||
/// <param name="dest">The decompressed data buffer.</param> | ||
/// <param name="result"> | ||
/// The zlib result structure that contains the amount of bytes read, written, | ||
/// and the adler32 hash of the data that can be used to compare the integrity | ||
/// of the compressed/decompressed results. | ||
/// </param> | ||
/// <returns> | ||
/// <see langword="true"/> if the compression was a success, <see langword="false"/> otherwise. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool TryDecompress(ReadOnlySpan<byte> source, Span<byte> dest, out ZlibResult? result) | ||
{ | ||
try | ||
{ | ||
result = this.Decompress(source, dest); | ||
return true; | ||
} | ||
catch (NotUnpackableException) | ||
{ | ||
result = null; | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Decompresses a file. | ||
/// </summary> | ||
/// <param name="sourcePath">The path to the file to decompress.</param> | ||
/// <param name="dest">The decompressed data buffer.</param> | ||
/// <exception cref="NotUnpackableException"> | ||
/// Thrown when zlib errors internally in any way. | ||
/// </exception> | ||
/// <returns> | ||
/// The zlib result structure that contains the amount of bytes read, written, | ||
/// and the adler32 hash of the data that can be used to compare the integrity | ||
/// of the compressed/decompressed results. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public ZlibResult Decompress(string sourcePath, Span<byte> dest) | ||
=> this.Decompress(File.ReadAllBytes(sourcePath), dest); | ||
|
||
/// <summary> | ||
/// Decompresses data. | ||
/// </summary> | ||
/// <param name="source">The compressed input data.</param> | ||
/// <param name="dest">The decompressed data buffer.</param> | ||
/// <exception cref="NotUnpackableException"> | ||
/// Thrown when zlib errors internally in any way. | ||
/// </exception> | ||
/// <returns> | ||
/// The zlib result structure that contains the amount of bytes read, written, | ||
/// and the adler32 hash of the data that can be used to compare the integrity | ||
/// of the compressed/decompressed results. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public ZlibResult Decompress(ReadOnlySpan<byte> source, Span<byte> dest) | ||
{ | ||
var bytesRead = ZlibHelper.Decompress(source, dest, out var bytesWritten, out var adler32, out var crc32, out var status, this.WindowBits); | ||
return new ZlibResult(bytesWritten, bytesRead, adler32, crc32, status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright (c) 2021~2022, Els_kom org. | ||
// https://github.com/Elskom/ | ||
// All rights reserved. | ||
// license: MIT, see LICENSE for more details. | ||
|
||
namespace ZlibSharp; | ||
|
||
using Internal; | ||
using Exceptions; | ||
|
||
/// <summary> | ||
/// Zlib Memory Compression class. | ||
/// </summary> | ||
public record ZlibEncoder | ||
{ | ||
public ZlibEncoder() : this(ZlibCompressionLevel.DefaultCompression, ZlibWindowBits.Zlib, ZlibCompressionStrategy.Default) | ||
{ | ||
} | ||
|
||
public ZlibEncoder(ZlibCompressionLevel compressionLevel, ZlibWindowBits windowBits, ZlibCompressionStrategy strategy) | ||
{ | ||
this.CompressionLevel = compressionLevel; | ||
this.WindowBits = windowBits; | ||
this.Strategy = strategy; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the compression level to use to compress the file. | ||
/// </summary> | ||
public ZlibCompressionLevel CompressionLevel { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the window bits to use to compress the data. | ||
/// </summary> | ||
public ZlibWindowBits WindowBits { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the compression strategy to use to compress the data. | ||
/// </summary> | ||
public ZlibCompressionStrategy Strategy { get; set; } | ||
|
||
/// <summary> | ||
/// Tries to compress a file using the user specified compression level. | ||
/// </summary> | ||
/// <param name="sourcePath">The path to the file to compress.</param> | ||
/// <param name="dest">The compressed data buffer.</param> | ||
/// <param name="result"> | ||
/// The zlib result structure that contains the amount of bytes read, written, | ||
/// and the adler32 hash of the data that can be used to compare the integrity | ||
/// of the compressed/decompressed results. | ||
/// | ||
/// If the compression failed this is set to <see langword="null"/>. | ||
/// </param> | ||
/// <returns> | ||
/// <see langword="true"/> if the compression was a success, <see langword="false"/> otherwise. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool TryCompress(string sourcePath, Span<byte> dest, out ZlibResult? result) | ||
=> this.TryCompress(File.ReadAllBytes(sourcePath), dest, out result); | ||
|
||
/// <summary> | ||
/// Tries to compress the data using the user specified compression level. | ||
/// </summary> | ||
/// <param name="source">The input data buffer.</param> | ||
/// <param name="dest">The compressed data buffer.</param> | ||
/// <param name="result"> | ||
/// The zlib result structure that contains the amount of bytes read, written, | ||
/// and the adler32 hash of the data that can be used to compare the integrity | ||
/// of the compressed/decompressed results. | ||
/// | ||
/// If the compression failed this is set to <see langword="null"/>. | ||
/// </param> | ||
/// <returns> | ||
/// <see langword="true"/> if the compression was a success, <see langword="false"/> otherwise. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool TryCompress(ReadOnlySpan<byte> source, Span<byte> dest, out ZlibResult? result) | ||
{ | ||
try | ||
{ | ||
result = this.Compress(source, dest); | ||
return true; | ||
} | ||
catch (NotPackableException) | ||
{ | ||
result = null; | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Compresses a file using the user specified compression level. | ||
/// </summary> | ||
/// <param name="sourcePath">The path to the file to compress.</param> | ||
/// <param name="dest">The compressed data buffer.</param> | ||
/// <exception cref="NotPackableException"> | ||
/// Thrown when zlib errors internally in any way. | ||
/// </exception> | ||
/// <returns> | ||
/// The zlib result structure that contains the amount of bytes read, written, | ||
/// and the adler32 hash of the data that can be used to compare the integrity | ||
/// of the compressed/decompressed results. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public ZlibResult Compress(string sourcePath, Span<byte> dest) | ||
=> this.Compress(File.ReadAllBytes(sourcePath), dest); | ||
|
||
/// <summary> | ||
/// Compresses data using the user specified compression level. | ||
/// </summary> | ||
/// <param name="source">The input data buffer.</param> | ||
/// <param name="dest">The compressed data buffer.</param> | ||
/// <exception cref="NotPackableException"> | ||
/// Thrown when zlib errors internally in any way. | ||
/// </exception> | ||
/// <returns> | ||
/// The zlib result structure that contains the amount of bytes read, written, | ||
/// and the adler32 hash of the data that can be used to compare the integrity | ||
/// of the compressed/decompressed results. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public ZlibResult Compress(ReadOnlySpan<byte> source, Span<byte> dest) | ||
{ | ||
var bytesWritten = ZlibHelper.Compress(source, dest, this.CompressionLevel, this.WindowBits, this.Strategy, out var adler32, out var crc32, out var status); | ||
return new(bytesWritten, 0, adler32, crc32, status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2021~2022, Els_kom org. | ||
// https://github.com/Elskom/ | ||
// All rights reserved. | ||
// license: MIT, see LICENSE for more details. | ||
|
||
namespace ZlibSharp; | ||
|
||
/// <summary> | ||
/// The possible status codes from zlib. | ||
/// </summary> | ||
public enum ZlibStatus | ||
{ | ||
/// <summary> | ||
/// Version error. | ||
/// </summary> | ||
VersionError = -6, | ||
/// <summary> | ||
/// Buffer Error (usually under-allocated buffers). | ||
/// </summary> | ||
BufError, | ||
/// <summary> | ||
/// A memory error (out of memory). | ||
/// </summary> | ||
MemError, | ||
/// <summary> | ||
/// A data error (the data is possibly corrupted). | ||
/// </summary> | ||
DataError, | ||
/// <summary> | ||
/// A stream error. | ||
/// </summary> | ||
StreamError, | ||
/// <summary> | ||
/// Some other error. | ||
/// </summary> | ||
ErrNo, | ||
/// <summary> | ||
/// Everything is alright. | ||
/// </summary> | ||
Ok, | ||
/// <summary> | ||
/// End of stream. | ||
/// </summary> | ||
StreamEnd, | ||
/// <summary> | ||
/// Need dictionary. | ||
/// </summary> | ||
NeedDict, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters