diff options
Diffstat (limited to 'lib/Net.Compression')
9 files changed, 187 insertions, 88 deletions
diff --git a/lib/Net.Compression/VNLib.Net.Compression/LibraryWrapper.cs b/lib/Net.Compression/VNLib.Net.Compression/LibraryWrapper.cs index fe8fb5a..78cfd17 100644 --- a/lib/Net.Compression/VNLib.Net.Compression/LibraryWrapper.cs +++ b/lib/Net.Compression/VNLib.Net.Compression/LibraryWrapper.cs @@ -114,21 +114,21 @@ namespace VNLib.Net.Compression //build the method table MethodTable methods = new() { - GetMethods = lib.DangerousGetMethod<GetSupportedMethodsDelegate>(), + GetMethods = lib.DangerousGetFunction<GetSupportedMethodsDelegate>(), - GetBlockSize = lib.DangerousGetMethod<GetBlockSizeDelegate>(), + GetBlockSize = lib.DangerousGetFunction<GetBlockSizeDelegate>(), - GetCompType = lib.DangerousGetMethod<GetCompressorTypeDelegate>(), + GetCompType = lib.DangerousGetFunction<GetCompressorTypeDelegate>(), - GetCompLevel = lib.DangerousGetMethod<GetCompressorLevelDelegate>(), + GetCompLevel = lib.DangerousGetFunction<GetCompressorLevelDelegate>(), - Alloc = lib.DangerousGetMethod<AllocateCompressorDelegate>(), + Alloc = lib.DangerousGetFunction<AllocateCompressorDelegate>(), - Free = lib.DangerousGetMethod<FreeCompressorDelegate>(), + Free = lib.DangerousGetFunction<FreeCompressorDelegate>(), - GetOutputSize = lib.DangerousGetMethod<GetCompressedSizeDelegate>(), + GetOutputSize = lib.DangerousGetFunction<GetCompressedSizeDelegate>(), - Compress = lib.DangerousGetMethod<CompressBlockDelegate>() + Compress = lib.DangerousGetFunction<CompressBlockDelegate>() }; return new (lib, filePath, in methods); diff --git a/lib/Net.Compression/VNLib.Net.CompressionTests/VNLib.Net.CompressionTests.csproj b/lib/Net.Compression/VNLib.Net.CompressionTests/VNLib.Net.CompressionTests.csproj index 97549db..bf1c1a1 100644 --- a/lib/Net.Compression/VNLib.Net.CompressionTests/VNLib.Net.CompressionTests.csproj +++ b/lib/Net.Compression/VNLib.Net.CompressionTests/VNLib.Net.CompressionTests.csproj @@ -8,9 +8,9 @@ </PropertyGroup> <ItemGroup> - <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> - <PackageReference Include="MSTest.TestAdapter" Version="3.1.1" /> - <PackageReference Include="MSTest.TestFramework" Version="3.1.1" /> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> + <PackageReference Include="MSTest.TestAdapter" Version="3.2.0" /> + <PackageReference Include="MSTest.TestFramework" Version="3.2.0" /> <PackageReference Include="coverlet.collector" Version="6.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> diff --git a/lib/Net.Compression/vnlib_compress/CMakeLists.txt b/lib/Net.Compression/vnlib_compress/CMakeLists.txt index 2eb234e..17b01ca 100644 --- a/lib/Net.Compression/vnlib_compress/CMakeLists.txt +++ b/lib/Net.Compression/vnlib_compress/CMakeLists.txt @@ -44,6 +44,9 @@ set(CMAKE_C_STANDARD_REQUIRED ON) #enable position independent code (for shared libraries with exports) set(CMAKE_POSITION_INDEPENDENT_CODE ON) +#since were buildiing in tree, set the export defintiions +add_definitions(-DVNLIB_EXPORTING) + message(STATUS "Build type is '${CMAKE_BUILD_TYPE}'") #include checks for zlib and brotli diff --git a/lib/Net.Compression/vnlib_compress/Taskfile.yaml b/lib/Net.Compression/vnlib_compress/Taskfile.yaml index 74ab24a..5271061 100644 --- a/lib/Net.Compression/vnlib_compress/Taskfile.yaml +++ b/lib/Net.Compression/vnlib_compress/Taskfile.yaml @@ -18,32 +18,19 @@ tasks: cmds: - cmd: echo "Building vnlib_compress" silent: true + + #make third-party dir before cloning libs + - cmd: powershell -Command "mkdir '{{.THIRD_PARTY_DIR}}' -Force" + ignore_error: true - #make dirs on non-win - - cmd: mkdir {{.THIRD_PARTY_DIR}} - platforms: ['linux', 'darwin'] - - #make dirs on windows - - cmd: powershell -Command "mkdir {{.THIRD_PARTY_DIR}} -Force" - platforms: ['windows'] - - #clone libs - - cmd: cd {{.THIRD_PARTY_DIR}} && git clone https://github.com/cloudflare/zlib.git - ignore_error: true - - - cmd: cd {{.THIRD_PARTY_DIR}} && git clone https://github.com/google/brotli.git - ignore_error: true + - task: zlib + - task: brotli #invoke cmake for build - - cmake -B./build -DCMAKE_BUILD_TYPE=RELEASE {{.CMAKE_ARGS}} + - cmake -B./build {{.CMAKE_ARGS}} - #build for Windows - - cmd: cd build && msbuild {{.PROJECT_NAME}}.sln /p:Configuration=release {{.BUILD_FLAGS}} - platforms: ['windows'] - - #using make - - cmd: cd build && make - platforms: ['linux', 'darwin'] + #build for platform + - cmake --build build/ --config Release #when build succeeds, archive the output into a tgz @@ -51,13 +38,28 @@ tasks: cmds: - cmd: powershell mkdir -Force './bin' #copy source code to target - - powershell -Command "tar --exclude build/* --exclude .vs/* --exclude bin/* -czvf bin/src.tgz ." - - postbuild_failed: - cmds: [] + - powershell -Command "tar --exclude build/* --exclude .vs/* --exclude bin/* -czf bin/src.tgz ." #Remove the output dirs on clean clean: ignore_error: true cmds: - cmd: powershell Remove-Item -Recurse './bin' + + #update or install the cloudflare fork of zlib library + zlib: + internal: true + status: + - cd {{.THIRD_PARTY_DIR}} && git clone https://github.com/cloudflare/zlib.git + + cmds: + - cd {{.THIRD_PARTY_DIR}}/zlib && git pull + + #update or install the google brotli library + brotli: + internal: true + status: + - cd {{.THIRD_PARTY_DIR}} && git clone https://github.com/google/brotli.git + cmds: + - cd {{.THIRD_PARTY_DIR}}/brotli && git pull +
\ No newline at end of file diff --git a/lib/Net.Compression/vnlib_compress/compression.c b/lib/Net.Compression/vnlib_compress/compression.c index 56d9157..ffe280b 100644 --- a/lib/Net.Compression/vnlib_compress/compression.c +++ b/lib/Net.Compression/vnlib_compress/compression.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: vnlib_compress @@ -40,26 +40,6 @@ #include "feature_zlib.h" #endif /* VNLIB_COMPRESSOR_GZIP_ENABLED */ - -/* -* Public API functions -*/ -VNLIB_EXPORT CompressorType VNLIB_CC GetSupportedCompressors(void); - -VNLIB_EXPORT int64_t VNLIB_CC GetCompressorBlockSize(_In_ const void* compressor); - -VNLIB_EXPORT CompressorType VNLIB_CC GetCompressorType(_In_ const void* compressor); - -VNLIB_EXPORT CompressionLevel VNLIB_CC GetCompressorLevel(_In_ const void* compressor); - -VNLIB_EXPORT void* VNLIB_CC AllocateCompressor(CompressorType type, CompressionLevel level); - -VNLIB_EXPORT int VNLIB_CC FreeCompressor(_In_ void* compressor); - -VNLIB_EXPORT int64_t VNLIB_CC GetCompressedSize(_In_ const void* compressor, uint64_t inputLength, int32_t flush); - -VNLIB_EXPORT int VNLIB_CC CompressBlock(_In_ const void* compressor, CompressionOperation* operation); - /* Gets the supported compressors, this is defined at compile time and is a convenience method for the user to know what compressors are supported at runtime. diff --git a/lib/Net.Compression/vnlib_compress/compression.h b/lib/Net.Compression/vnlib_compress/compression.h index a4fc65f..ae3bb8f 100644 --- a/lib/Net.Compression/vnlib_compress/compression.h +++ b/lib/Net.Compression/vnlib_compress/compression.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: vnlib_compress @@ -154,4 +154,71 @@ typedef struct CompressionOperationStruct { } CompressionOperation; +/* +* Public API functions +*/ +VNLIB_EXPORT CompressorType VNLIB_CC GetSupportedCompressors(void); + +/* +* Returns the suggested block size for the underlying compressor. +* +* @param compressor A pointer to the desired compressor instance to query. +* @return The suggested block size for the underlying compressor in bytes +*/ +VNLIB_EXPORT int64_t VNLIB_CC GetCompressorBlockSize(_In_ const void* compressor); + +/* +* Gets the compressor type of the specified compressor instance. +* +* @param compressor A pointer to the desired compressor instance to query. +* @return The type of the specified compressor instance. +*/ +VNLIB_EXPORT CompressorType VNLIB_CC GetCompressorType(_In_ const void* compressor); + +/* +* Gets the compression level of the specified compressor instance. +* +* @param compressor A pointer to the desired compressor instance to query. +* @return The compression level of the specified compressor instance. +*/ +VNLIB_EXPORT CompressionLevel VNLIB_CC GetCompressorLevel(_In_ const void* compressor); + +/* +* Allocates a new compressor instance on the native heap of the desired compressor type. +* +* @param type The desired compressor type. +* @param level The desired compression level. +* @return A pointer to the newly allocated compressor instance. NULL if the compressor +could not be allocated. +*/ +VNLIB_EXPORT void* VNLIB_CC AllocateCompressor(CompressorType type, CompressionLevel level); + +/* +* Frees a previously allocated compressor instance. +* +* @param compressor A pointer to the desired compressor instance to free. +* @return The underlying compressor's native return code. +*/ +VNLIB_EXPORT int VNLIB_CC FreeCompressor(_In_ void* compressor); + +/* +* Computes the maximum compressed size of the specified input data. This is not supported + for all compression types. +* +* @param compressor A pointer to the initialized compressor instance to use. +* @param inputLength The length of the input data in bytes. +* @return The maximum compressed size of the specified input data in bytes. +*/ +VNLIB_EXPORT int64_t VNLIB_CC GetCompressedSize(_In_ const void* compressor, uint64_t inputLength, int32_t flush); + + +/* +* Perform compression operation using the specified compressor instance. +* +* @param compressor A pointer to the initialized compressor instance to use. +* @param operation A pointer to the compression operation structure +* @return The underlying compressor's native return code +*/ +VNLIB_EXPORT int VNLIB_CC CompressBlock(_In_ const void* compressor, CompressionOperation* operation); + #endif /* !VNLIB_COMPRESS_MAIN_H_ */
\ No newline at end of file diff --git a/lib/Net.Compression/vnlib_compress/feature_brotli.c b/lib/Net.Compression/vnlib_compress/feature_brotli.c index a300500..3a3e330 100644 --- a/lib/Net.Compression/vnlib_compress/feature_brotli.c +++ b/lib/Net.Compression/vnlib_compress/feature_brotli.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: vnlib_compress @@ -39,7 +39,7 @@ static void _brFreeCallback(void* opaque, void* address) { (void)opaque; - //Brotli may pass a null address to the free callback + /*Brotli may pass a null address to the free callback*/ if (address) { vnfree(address); @@ -51,6 +51,8 @@ int BrAllocCompressor(CompressorState* state) { BrotliEncoderState* comp; + assert(state != NULL); + /* * Never allow no compression, it is not supported by the br encoder */ @@ -121,6 +123,8 @@ int BrAllocCompressor(CompressorState* state) void BrFreeCompressor(CompressorState* state) { + assert(state != NULL); + /* * Free the compressor instance if it exists */ diff --git a/lib/Net.Compression/vnlib_compress/feature_zlib.c b/lib/Net.Compression/vnlib_compress/feature_zlib.c index 5dbd7ad..9993b43 100644 --- a/lib/Net.Compression/vnlib_compress/feature_zlib.c +++ b/lib/Net.Compression/vnlib_compress/feature_zlib.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: vnlib_compress @@ -50,6 +50,8 @@ int DeflateAllocCompressor(CompressorState* state) int result, compLevel; z_stream* stream; + assert(state); + /* * Allocate the z-stream state on the heap so we can * store it in the compressor state @@ -149,6 +151,8 @@ int DeflateFreeCompressor(CompressorState* state) { int result; + assert(state); + /* * Free the z-stream state, only if the compressor is initialized */ diff --git a/lib/Net.Compression/vnlib_compress/util.h b/lib/Net.Compression/vnlib_compress/util.h index 5d87c8f..344a28b 100644 --- a/lib/Net.Compression/vnlib_compress/util.h +++ b/lib/Net.Compression/vnlib_compress/util.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: vnlib_compress @@ -29,29 +29,77 @@ /* * Stub missing types and constants for GCC */ -#if defined(__GNUC__) -#define inline __inline__ -#define VNLIB_EXPORT __attribute__((visibility("default"))) -#define VNLIB_CC -#elif defined(_MSC_VER) -#define VNLIB_EXPORT __declspec(dllexport) -#define VNLIB_CC __cdecl -#endif /* WIN32 */ -#define ERR_INVALID_PTR -1 -#define ERR_OUT_OF_MEMORY -2 - -#define TRUE 1 -#define FALSE 0 +#if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) + #define IS_WINDOWS +#endif +//Set api export calling convention (allow used to override) +#ifndef VNLIB_CC + #ifdef IS_WINDOWS + //STD for importing to other languages such as .NET + #define VNLIB_CC __stdcall + #else + #define VNLIB_CC + #endif +#endif // !VNLIB_CC + +#ifndef VNLIB_EXPORT //Allow users to disable the export/impoty macro if using source code directly + #ifdef VNLIB_EXPORTING + #ifdef IS_WINDOWS + #define VNLIB_EXPORT __declspec(dllexport) + #else + #define VNLIB_EXPORT __attribute__((visibility("default"))) + #endif // IS_WINDOWS + #else + #ifdef IS_WINDOWS + #define VNLIB_EXPORT __declspec(dllimport) + #else + #define VNLIB_EXPORT + #endif // IS_WINDOWS + #endif // !VNLIB_EXPORTING +#endif // !VNLIB_EXPORT + + +/* If not Windows, define inline */ +#ifndef IS_WINDOWS + #ifndef inline + #define inline __inline__ + #endif // !inline +#endif // !IS_WINDOWS + +//NULL #ifndef NULL -#define NULL 0 -#endif /* !NULL */ + #define NULL ((void*)0) +#endif // !NULL + +#ifndef TRUE + #define TRUE 1 +#endif // !TRUE + +#ifndef FALSE + #define FALSE 0 +#endif // !FALSE #ifndef _In_ -#define _In_ + #define _In_ +#endif + +/* +* Add debug runtime assertions +*/ +#ifdef DEBUG + #include <assert.h> +#else + #define assert(x) {} #endif +/* +* ERRORS AND CONSTANTS +*/ +#define ERR_INVALID_PTR -1 +#define ERR_OUT_OF_MEMORY -2 + #if defined(VNLIB_CUSTOM_MALLOC_ENABLE) /* @@ -60,15 +108,6 @@ #include <NativeHeapApi.h> -/* -* Add debug runtime assertions -*/ -#ifdef DEBUG - #define NDEBUG - #include <assert.h> -#else - #define assert(x) -#endif /* * Add overrides for malloc, calloc, and free that use |