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

v1.19 #15

Merged
merged 21 commits into from
Sep 20, 2023
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
2 changes: 1 addition & 1 deletion bench/LibDeflate.Benchmarks/LibDeflate.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.8" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/LibDeflate/Compressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected Compressor(int compressionLevel)
return null;
}

return output.Slice(0, (int)bytesWritten);
return output[..(int)bytesWritten];
}
catch
{
Expand Down
13 changes: 6 additions & 7 deletions src/LibDeflate/Imports/Checksums.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace LibDeflate.Imports;

using size_t = System.UIntPtr;
using size_t = nuint;

internal static class Checksums
{
Expand All @@ -13,15 +12,15 @@ internal static class Checksums
/// required initial value for 'adler' is 1. This value is also returned when
/// 'buffer' is specified as NULL.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern UInt32 libdeflate_adler32(UInt32 adler, in byte buffer, size_t len);
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern uint libdeflate_adler32(uint adler, in byte buffer, size_t len);

///<summary>
/// libdeflate_crc32() updates a running CRC-32 checksum with 'len' bytes of data
/// and returns the updated checksum. When starting a new checksum, the required
/// initial value for 'crc' is 0. This value is also returned when 'buffer' is
/// specified as NULL.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern UInt32 libdeflate_crc32(UInt32 crc, in byte buffer, size_t len);
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern uint libdeflate_crc32(uint crc, in byte buffer, size_t len);
}
27 changes: 16 additions & 11 deletions src/LibDeflate/Imports/Compression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace LibDeflate.Imports;

using libdeflate_compressor = System.IntPtr;
using size_t = System.UIntPtr;
using libdeflate_compressor = nint;
using size_t = nuint;

internal static class Compression
{
Expand All @@ -25,20 +25,25 @@ internal static class Compression
/// A single compressor is not safe to use by multiple threads concurrently.
/// However, different threads may use different compressors concurrently.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_compressor libdeflate_alloc_compressor(int compression_level);

/// <summary>
/// Like <see cref="libdeflate_alloc_compressor"/> but allows specifying advanced options per-compressor.
/// </summary>
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_compressor libdeflate_alloc_compressor_ex(int compression_level, in libdeflate_options options);

///<summary>
/// libdeflate_deflate_compress() performs raw DEFLATE compression on a buffer of
/// data. The function attempts to compress 'in_nbytes' bytes of data located at
/// 'in' and write the results to 'out', which has space for 'out_nbytes_avail'
/// bytes. The return value is the compressed size in bytes, or 0 if the data
/// could not be compressed to 'out_nbytes_avail' bytes or fewer.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_deflate_compress(libdeflate_compressor compressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail);


///<summary>
/// libdeflate_deflate_compress_bound() returns a worst-case upper bound on the
/// number of bytes of compressed data that may be produced by compressing any
Expand All @@ -64,44 +69,44 @@ internal static class Compression
/// libdeflate_deflate_compress() returns 0, indicating that the compressed data
/// did not fit into the provided output buffer.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_deflate_compress_bound(libdeflate_compressor compressor, size_t in_nbytes);

///<summary>
/// Like libdeflate_deflate_compress(), but stores the data in the zlib wrapper
/// format.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_zlib_compress(libdeflate_compressor compressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail);

///<summary>
/// Like libdeflate_deflate_compress_bound(), but assumes the data will be
/// compressed with libdeflate_zlib_compress() rather than with
/// libdeflate_deflate_compress().
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_zlib_compress_bound(libdeflate_compressor compressor, size_t in_nbytes);

///<summary>
/// Like libdeflate_deflate_compress(), but stores the data in the gzip wrapper
/// format.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_gzip_compress(libdeflate_compressor compressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail);

///<summary>
/// Like libdeflate_deflate_compress_bound(), but assumes the data will be
/// compressed with libdeflate_gzip_compress() rather than with
/// libdeflate_deflate_compress().
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern size_t libdeflate_gzip_compress_bound(libdeflate_compressor compressor, size_t in_nbytes);

///<summary>
/// libdeflate_free_compressor() frees a compressor that was allocated with
/// libdeflate_alloc_compressor(). If a NULL pointer is passed in, no action is
/// taken.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern void libdeflate_free_compressor(libdeflate_compressor compressor);
}
2 changes: 2 additions & 0 deletions src/LibDeflate/Imports/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: InternalsVisibleTo($"{nameof(LibDeflate)}.Tests")]
[assembly: InternalsVisibleTo($"{nameof(LibDeflate)}.DangerousTests")]
Expand All @@ -7,4 +8,5 @@ namespace LibDeflate.Imports;
internal static class Constants
{
public const string DllName = "libdeflate";
public const CallingConvention CallConv = CallingConvention.Cdecl;
}
4 changes: 2 additions & 2 deletions src/LibDeflate/Imports/CustomMemoryAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ internal static class CustomMemoryAllocator
/// There must not be any libdeflate_compressor or libdeflate_decompressor
/// structures in existence when calling this function.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern void libdeflate_set_memory_allocator(malloc_func malloc, free_func free);
}
}
24 changes: 15 additions & 9 deletions src/LibDeflate/Imports/Decompression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ public enum libdeflate_result
/// A single decompressor is not safe to use by multiple threads concurrently.
/// However, different threads may use different decompressors concurrently.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_decompressor libdeflate_alloc_decompressor();

/// <summary>
/// Like <see cref="libdeflate_alloc_decompressor"/> but allows specifying advanced options per-decompressor.
/// </summary>
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_decompressor libdeflate_alloc_decompressor_ex(in libdeflate_options options);

///<summary>
/// libdeflate_deflate_decompress() decompresses the DEFLATE-compressed stream
/// from the buffer 'in' with compressed size up to 'in_nbytes' bytes. The
Expand Down Expand Up @@ -83,7 +89,7 @@ public enum libdeflate_result
/// not large enough but no other problems were encountered, or another
/// nonzero result code if decompression failed for another reason.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_deflate_decompress(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -92,7 +98,7 @@ public enum libdeflate_result
/// then the actual compressed size of the DEFLATE stream (aligned to the next
/// byte boundary) is written to *actual_in_nbytes_ret.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_deflate_decompress_ex(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_in_nbytes_ret, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -103,7 +109,7 @@ public enum libdeflate_result
/// than 'in_nbytes'. If you need to know exactly where the zlib stream ended,
/// use libdeflate_zlib_decompress_ex().
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_zlib_decompress(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -114,7 +120,7 @@ public enum libdeflate_result
/// than 'in_nbytes'. If you need to know exactly where the zlib stream ended,
/// use libdeflate_zlib_decompress_ex().
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_zlib_decompress_ex(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_in_nbytes_ret, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -125,7 +131,7 @@ public enum libdeflate_result
/// will be decompressed. Use libdeflate_gzip_decompress_ex() if you need
/// multi-member support.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_gzip_decompress(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_out_nbytes_ret);

///<summary>
Expand All @@ -135,14 +141,14 @@ public enum libdeflate_result
/// buffer was decompressed), then the actual number of input bytes consumed is
/// written to *actual_in_nbytes_ret.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern libdeflate_result libdeflate_gzip_decompress_ex(libdeflate_decompressor decompressor, in byte @in, size_t in_nbytes, ref byte @out, size_t out_nbytes_avail, out size_t actual_in_nbytes_ret, out size_t actual_out_nbytes_ret);

///<summary>
/// libdeflate_free_decompressor() frees a decompressor that was allocated with
/// libdeflate_alloc_decompressor(). If a NULL pointer is passed in, no action
/// is taken.
///</summary>
[DllImport(Constants.DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void libdeflate_free_decompressor(libdeflate_decompressor compressor);
[DllImport(Constants.DllName, CallingConvention = Constants.CallConv, ExactSpelling = true)]
public static extern void libdeflate_free_decompressor(libdeflate_decompressor decompressor);
}
90 changes: 90 additions & 0 deletions src/LibDeflate/Imports/libdeflate_options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace LibDeflate.Imports;

using static LibDeflate.Imports.CustomMemoryAllocator;
using size_t = UIntPtr;

/// <summary>
/// Advanced options. This is the options structure that
/// <see cref="Compression.libdeflate_alloc_compressor_ex"/>
/// and <see cref="Decompression.libdeflate_alloc_decompressor_ex"/>
/// require. Most users won't need this and should just use the non-"_ex"
/// functions instead.
/// </summary>
internal readonly struct libdeflate_options
{
private static readonly size_t Size = (nuint)(nint)Unsafe.SizeOf<libdeflate_options>();

public libdeflate_options(malloc_func malloc, free_func free)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(malloc);
ArgumentNullException.ThrowIfNull(free);
#else
ThrowIfNull(malloc);
ThrowIfNull(free);
#endif

this.sizeof_options = Size;
this.malloc = malloc;
this.free = free;

#if !NET6_0_OR_GREATER
static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if(argument is null)
{
ThrowHelperArgumentNull(paramName!);
}

[DoesNotReturn]
static void ThrowHelperArgumentNull(string paramName) => throw new ArgumentNullException(paramName);
}
#endif
}

/// <summary>
/// This field must be set to the struct size. This field exists for
/// extensibility, so that fields can be appended to this struct in
/// future versions of libdeflate while still supporting old binaries.
/// </summary>
public readonly size_t sizeof_options;

/// <summary>
/// An optional custom memory allocator to use for this (de)compressor.
/// 'malloc_func' must be a function that behaves like malloc().
/// </summary>
/// <remarks>
/// This is useful in cases where a process might have multiple users of
/// libdeflate who want to use different memory allocators. For example,
/// a library might want to use libdeflate with a custom memory allocator
/// without interfering with user code that might use libdeflate too.
///
/// This takes priority over the "global" memory allocator (which by
/// default is malloc() and free(), but can be changed by
/// libdeflate_set_memory_allocator()). Moreover, libdeflate will never
/// call the "global" memory allocator if a per-(de)compressor custom
/// allocator is always given.
/// </remarks>
public readonly malloc_func malloc;

/// <summary>
/// An optional custom memory deallocator to use for this (de)compressor.
/// 'free_func' must be a function that behaves like free().
/// </summary>
/// <remarks>
/// This is useful in cases where a process might have multiple users of
/// libdeflate who want to use different memory allocators. For example,
/// a library might want to use libdeflate with a custom memory allocator
/// without interfering with user code that might use libdeflate too.
///
/// This takes priority over the "global" memory allocator (which by
/// default is malloc() and free(), but can be changed by
/// libdeflate_set_memory_allocator()). Moreover, libdeflate will never
/// call the "global" memory allocator if a per-(de)compressor custom
/// allocator is always given.
/// </remarks>
public readonly free_func free;
}
15 changes: 10 additions & 5 deletions src/LibDeflate/LibDeflate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<AssemblyName>LibDeflate.NET</AssemblyName>
<VersionPrefix>1.18.0</VersionPrefix>
<VersionPrefix>1.19.0</VersionPrefix>
<Authors>jzebedee</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/jzebedee/LibDeflate</PackageProjectUrl>
<PackageTags>libdeflate.net libdeflate deflate zlib gzip gz crc32 crc adler32 adler</PackageTags>
<Description>LibDeflate.NET is a managed wrapper around libdeflate, a native library for fast, whole-buffer DEFLATE-based compression and decompression.</Description>

<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>

<!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand All @@ -21,6 +20,12 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<!-- only enable trimming for TFMs that support it -->
<!-- see: https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/trimming-unsupported-targetframework -->
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<!-- Source Link Support -->
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
Expand All @@ -33,7 +38,7 @@
<Choose>
<When Condition="'$(TargetFramework)' == 'netstandard2.0'">
<ItemGroup>
<PackageReference Include="PolySharp" Version="1.13.1">
<PackageReference Include="PolySharp" Version="1.13.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -45,7 +50,7 @@

<!-- Regular dependencies -->
<ItemGroup>
<PackageReference Include="LibDeflate.Native" Version="1.18.0.1" />
<PackageReference Include="LibDeflate.Native" Version="1.19.0" />
</ItemGroup>

</Project>
Loading
Loading