1+ using System ;
2+ using System . Reflection ;
3+ using System . Runtime . InteropServices ;
4+
5+ namespace Oodle . NET
6+ {
7+ #if NET5_0_OR_GREATER
8+ public unsafe class OodleCompressor : IDisposable
9+ #else
10+ public class OodleCompressor : IDisposable
11+ #endif
12+ {
13+ private readonly IntPtr _handle ;
14+
15+ #if NET5_0_OR_GREATER
16+ public readonly delegate * unmanaged[ Cdecl] < OodleLZ_Compressor , byte [ ] , long , byte [ ] , OodleLZ_CompressionLevel , long , long , long , long , long , long > Compress ;
17+ public readonly delegate * unmanaged[ Cdecl] < byte [ ] , long , byte [ ] , long , OodleLZ_FuzzSafe , OodleLZ_CheckCRC , OodleLZ_Verbosity , long , long , long , long , long , long , OodleLZ_Decode_ThreadPhase , long > Decompress ;
18+ public readonly delegate * unmanaged[ Cdecl] < OodleLZ_Compressor , long , long > MemorySizeNeeded ;
19+ #else
20+ public delegate long OodleLZ_Compress ( OodleLZ_Compressor compressor , byte [ ] src , long src_size , byte [ ] dst , OodleLZ_CompressionLevel level , long opts , long context , long unused , long scratch , long scratch_size ) ;
21+ public readonly OodleLZ_Compress Compress ;
22+
23+ public delegate long OodleLZ_Decompress ( byte [ ] src , long src_size , byte [ ] dst , long dst_size , OodleLZ_FuzzSafe fuzz , OodleLZ_CheckCRC crc , OodleLZ_Verbosity verbosity , long context , long e , long callback , long callback_ctx , long scratch , long scratch_size , OodleLZ_Decode_ThreadPhase thread_phase ) ;
24+ public readonly OodleLZ_Decompress Decompress ;
25+
26+ public delegate long OodleLZDecoder_MemorySizeNeeded ( OodleLZ_Compressor compressor , long size ) ;
27+ public readonly OodleLZDecoder_MemorySizeNeeded MemorySizeNeeded ;
28+ #endif
29+
30+ public OodleCompressor ( string libraryPath , Assembly assembly , DllImportSearchPath ? searchPath = null ) : this ( NativeLibrary . Load ( libraryPath , assembly , searchPath ) ) { }
31+ public OodleCompressor ( string libraryPath ) : this ( NativeLibrary . Load ( libraryPath ) ) { }
32+ public OodleCompressor ( IntPtr handle )
33+ {
34+ if ( handle == IntPtr . Zero )
35+ {
36+ throw new ArgumentOutOfRangeException ( nameof ( handle ) , $ "Failed to initialize { nameof ( OodleCompressor ) } ") ;
37+ }
38+
39+ _handle = handle ;
40+ var compressAddress = NativeLibrary . GetExport ( _handle , "OodleLZ_Compress" ) ;
41+ var decompressAddress = NativeLibrary . GetExport ( _handle , "OodleLZ_Decompress" ) ;
42+ var memorySizeNeededAddress = NativeLibrary . GetExport ( _handle , "OodleLZDecoder_MemorySizeNeeded" ) ;
43+
44+ #if NET5_0_OR_GREATER
45+ Compress = ( delegate * unmanaged[ Cdecl] < OodleLZ_Compressor , byte [ ] , long , byte [ ] , OodleLZ_CompressionLevel , long , long , long , long , long , long > ) compressAddress ;
46+ Decompress = ( delegate * unmanaged[ Cdecl] < byte [ ] , long , byte [ ] , long , OodleLZ_FuzzSafe , OodleLZ_CheckCRC , OodleLZ_Verbosity , long , long , long , long , long , long , OodleLZ_Decode_ThreadPhase , long > ) decompressAddress ;
47+ MemorySizeNeeded = ( delegate * unmanaged[ Cdecl] < OodleLZ_Compressor , long , long > ) memorySizeNeededAddress ;
48+ #else
49+ Compress = Marshal . GetDelegateForFunctionPointer < OodleLZ_Compress > ( compressAddress ) ;
50+ Decompress = Marshal . GetDelegateForFunctionPointer < OodleLZ_Decompress > ( decompressAddress ) ;
51+ MemorySizeNeeded = Marshal . GetDelegateForFunctionPointer < OodleLZDecoder_MemorySizeNeeded > ( memorySizeNeededAddress ) ;
52+ #endif
53+ }
54+
55+ public static uint GetCompressionBound ( uint bufferSize )
56+ {
57+ return bufferSize + 274 * ( ( bufferSize + 0x3FFFF ) / 0x40000 ) ;
58+ }
59+
60+ protected virtual void Dispose ( bool disposing )
61+ {
62+ if ( ! disposing )
63+ {
64+ return ;
65+ }
66+
67+ if ( _handle != IntPtr . Zero )
68+ {
69+ NativeLibrary . Free ( _handle ) ;
70+ }
71+ }
72+
73+ public void Dispose ( )
74+ {
75+ Dispose ( true ) ;
76+ GC . SuppressFinalize ( this ) ;
77+ }
78+ }
79+
80+ public enum OodleLZ_FuzzSafe
81+ {
82+ No ,
83+ Yes
84+ }
85+
86+ public enum OodleLZ_CheckCRC
87+ {
88+ No ,
89+ Yes
90+ }
91+
92+ public enum OodleLZ_Verbosity
93+ {
94+ None ,
95+ Max = 3
96+ }
97+
98+ public enum OodleLZ_Decode_ThreadPhase
99+ {
100+ ThreadPhase1 = 0x1 ,
101+ ThreadPhase2 = 0x2 ,
102+
103+ Unthreaded = ThreadPhase1 | ThreadPhase2
104+ }
105+ }
0 commit comments