From a19807f7f73ffb023e4ffe93071fe91525fd2c8d Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 7 Sep 2024 15:30:02 -0400 Subject: Squashed commit of the following: commit 97d0c461140e4badf40454471748099266c58c85 Author: vnugent Date: Sat Sep 7 15:18:31 2024 -0400 restsharp vuln update & remove github pull job for now commit f7eb445c9fcf4557f0bf1e1622673242b7da5ced Author: vnugent Date: Sat Sep 7 14:51:44 2024 -0400 update manual plugin loading api commit d3fa866898747c7b7535f2796f8046cdd9766763 Author: vnugent Date: Sat Sep 7 12:56:36 2024 -0400 package updates commit bdb62d04a7c7ea9bcea01b6314ba3306e07099f1 Author: vnugent Date: Sat Sep 7 12:50:58 2024 -0400 minor memory api internal cleanup commit d297b3a958e13a76ea61c8df588ec32ea9a40faf Author: vnugent Date: Mon Aug 26 22:21:56 2024 -0400 refactor: #7 Update compression style, platform and linking --- .../VNLib.Net.Compression/CompressionExtensions.cs | 113 +++++++++++---------- 1 file changed, 61 insertions(+), 52 deletions(-) (limited to 'lib/Net.Compression/VNLib.Net.Compression/CompressionExtensions.cs') 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 /// /// - /// A pointer to the compressor context + /// A pointer to the compressor context /// A buffer to write the result to /// The input block of memory to compress /// A value that indicates if a flush is requested /// The results of the compression operation - public static unsafe CompressionResult CompressBlock(this LibraryWrapper nativeLib, IntPtr comp, Memory output, ReadOnlyMemory input, bool finalBlock) + public static unsafe CompressionResult CompressBlock( + this LibraryWrapper nativeLib, + IntPtr compressorInstance, + Memory output, + ReadOnlyMemory 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 + }; + } } /// /// Compresses a block using the compressor context pointer provided /// /// - /// A pointer to the compressor context + /// A pointer to the compressor context /// A buffer to write the result to /// The input block of memory to compress /// A value that indicates if a flush is requested /// The results of the compression operation - public static unsafe CompressionResult CompressBlock(this LibraryWrapper nativeLib, IntPtr comp, Span output, ReadOnlySpan input, bool finalBlock) + public static unsafe CompressionResult CompressBlock( + this LibraryWrapper nativeLib, + IntPtr compressorInstance, + Span output, + ReadOnlySpan 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 + }; + } } } } -- cgit