aboutsummaryrefslogtreecommitdiff
path: root/lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-09-07 15:30:02 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-09-07 15:30:02 -0400
commita19807f7f73ffb023e4ffe93071fe91525fd2c8d (patch)
treeddd829ec6d4bd9fc99b9446bd49b7b051fa405ab /lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs
parent0419f315e5689e043f311203ab8e61f69f1ee1d6 (diff)
Squashed commit of the following:
commit 97d0c461140e4badf40454471748099266c58c85 Author: vnugent <public@vaughnnugent.com> Date: Sat Sep 7 15:18:31 2024 -0400 restsharp vuln update & remove github pull job for now commit f7eb445c9fcf4557f0bf1e1622673242b7da5ced Author: vnugent <public@vaughnnugent.com> Date: Sat Sep 7 14:51:44 2024 -0400 update manual plugin loading api commit d3fa866898747c7b7535f2796f8046cdd9766763 Author: vnugent <public@vaughnnugent.com> Date: Sat Sep 7 12:56:36 2024 -0400 package updates commit bdb62d04a7c7ea9bcea01b6314ba3306e07099f1 Author: vnugent <public@vaughnnugent.com> Date: Sat Sep 7 12:50:58 2024 -0400 minor memory api internal cleanup commit d297b3a958e13a76ea61c8df588ec32ea9a40faf Author: vnugent <public@vaughnnugent.com> Date: Mon Aug 26 22:21:56 2024 -0400 refactor: #7 Update compression style, platform and linking
Diffstat (limited to 'lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs')
-rw-r--r--lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs113
1 files changed, 61 insertions, 52 deletions
diff --git a/lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs b/lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs
index 4509b67..0d1fc73 100644
--- a/lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs
+++ b/lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Net.Compression
@@ -36,12 +36,18 @@ namespace VNLib.Net.Compression
/// Compresses a block using the compressor context pointer provided
/// </summary>
/// <param name="nativeLib"></param>
- /// <param name="comp">A pointer to the compressor context</param>
+ /// <param name="compressorInstance">A pointer to the compressor context</param>
/// <param name="output">A buffer to write the result to</param>
/// <param name="input">The input block of memory to compress</param>
/// <param name="finalBlock">A value that indicates if a flush is requested</param>
/// <returns>The results of the compression operation</returns>
- public static unsafe CompressionResult CompressBlock(this LibraryWrapper nativeLib, IntPtr comp, Memory<byte> output, ReadOnlyMemory<byte> input, bool finalBlock)
+ public static unsafe CompressionResult CompressBlock(
+ this LibraryWrapper nativeLib,
+ IntPtr compressorInstance,
+ Memory<byte> output,
+ ReadOnlyMemory<byte> input,
+ bool finalBlock
+ )
{
/*
* Since .NET only supports int32 size memory blocks
@@ -51,46 +57,51 @@ namespace VNLib.Net.Compression
* sizes (read/written)
*/
- //get pointers to the input and output buffers
- using MemoryHandle inPtr = input.Pin();
- using MemoryHandle outPtr = output.Pin();
-
//Create the operation struct
- CompressionOperation operation;
- CompressionOperation* op = &operation;
+ CompressionOperation operation = default;
- op->flush = finalBlock ? 1 : 0;
- op->bytesRead = 0;
- op->bytesWritten = 0;
+ operation.flush = finalBlock ? 1 : 0;
- //Configure the input and output buffers
- op->inputBuffer = inPtr.Pointer;
- op->inputSize = (uint)input.Length;
+ checked
+ {
+ //get pointers to the input and output buffers
+ using MemoryHandle inPtr = input.Pin();
+ using MemoryHandle outPtr = output.Pin();
- op->outputBuffer = outPtr.Pointer;
- op->outputSize = (uint)output.Length;
+ //Configure the input and output buffers
+ operation.inputBuffer = inPtr.Pointer;
+ operation.inputSize = (uint)input.Length;
- //Call the native compress function
- nativeLib!.CompressBlock(comp, &operation);
+ operation.outputBuffer = outPtr.Pointer;
+ operation.outputSize = (uint)output.Length;
- //Return the number of bytes written
- return new()
- {
- BytesRead = (int)op->bytesRead,
- BytesWritten = (int)op->bytesWritten
- };
+ //Call the native compress function
+ nativeLib!.CompressBlock(compressorInstance, &operation);
+
+ return new()
+ {
+ BytesRead = (int)operation.bytesRead,
+ BytesWritten = (int)operation.bytesWritten
+ };
+ }
}
/// <summary>
/// Compresses a block using the compressor context pointer provided
/// </summary>
/// <param name="nativeLib"></param>
- /// <param name="comp">A pointer to the compressor context</param>
+ /// <param name="compressorInstance">A pointer to the compressor context</param>
/// <param name="output">A buffer to write the result to</param>
/// <param name="input">The input block of memory to compress</param>
/// <param name="finalBlock">A value that indicates if a flush is requested</param>
/// <returns>The results of the compression operation</returns>
- public static unsafe CompressionResult CompressBlock(this LibraryWrapper nativeLib, IntPtr comp, Span<byte> output, ReadOnlySpan<byte> input, bool finalBlock)
+ public static unsafe CompressionResult CompressBlock(
+ this LibraryWrapper nativeLib,
+ IntPtr compressorInstance,
+ Span<byte> output,
+ ReadOnlySpan<byte> input,
+ bool finalBlock
+ )
{
/*
* Since .NET only supports int32 size memory blocks
@@ -100,33 +111,31 @@ namespace VNLib.Net.Compression
* sizes (read/written)
*/
- fixed(byte* inputPtr = &MemoryMarshal.GetReference(input),
- outPtr = &MemoryMarshal.GetReference(output))
- {
- //Create the operation struct
- CompressionOperation operation;
- CompressionOperation* op = &operation;
-
- op->flush = finalBlock ? 1 : 0;
- op->bytesRead = 0;
- op->bytesWritten = 0;
-
- //Configure the input and output buffers
- op->inputBuffer = inputPtr;
- op->inputSize = (uint)input.Length;
-
- op->outputBuffer = outPtr;
- op->outputSize = (uint)output.Length;
-
- //Call the native compress function
- nativeLib!.CompressBlock(comp, &operation);
+ //Create the operation struct
+ CompressionOperation operation = default;
+ operation.flush = finalBlock ? 1 : 0;
- //Return the number of bytes written
- return new()
+ checked
+ {
+ fixed (byte* inputPtr = &MemoryMarshal.GetReference(input),
+ outPtr = &MemoryMarshal.GetReference(output))
{
- BytesRead = (int)op->bytesRead,
- BytesWritten = (int)op->bytesWritten
- };
+ //Configure the input and output buffers
+ operation.inputBuffer = inputPtr;
+ operation.inputSize = (uint)input.Length;
+
+ operation.outputBuffer = outPtr;
+ operation.outputSize = (uint)output.Length;
+
+ //Call the native compress function
+ nativeLib!.CompressBlock(compressorInstance, &operation);
+
+ return new()
+ {
+ BytesRead = (int)operation.bytesRead,
+ BytesWritten = (int)operation.bytesWritten
+ };
+ }
}
}
}