using System; using System.Runtime.InteropServices; using System.Text; namespace ScreenConnect; public abstract class DeflateCoder : CriticalFinalizerDisposableObject, IMultiCallCoder, ICoder, IDisposable { private unsafe Native.libz.z_stream_s* nativeStream; private DeflateWrapper wrapper; public unsafe int UnusedBitTakenCount => nativeStream->data_type & 0x3F; public bool AreInputOutputCursorsAlwaysSynced => false; public unsafe long TotalInputByteCount => nativeStream->total_in.ToInt64(); public unsafe long TotalOutputByteCount => nativeStream->total_out.ToInt64(); protected DeflateCoder(DeflateWrapper wrapper) { this.wrapper = wrapper; } protected unsafe override void Dispose(bool disposing) { if (nativeStream != null) { try { End((IntPtr)nativeStream); } catch { } Marshal.FreeHGlobal((IntPtr)nativeStream); nativeStream = null; } } protected unsafe void Initialize() { int num = 15; if (wrapper == DeflateWrapper.None) { num *= -1; } else if (wrapper == DeflateWrapper.Gzip) { num += 16; } int num2 = Marshal.SizeOf(typeof(Native.libz.z_stream_s)); nativeStream = (Native.libz.z_stream_s*)(void*)Marshal.AllocHGlobal(num2); Extensions.ZeroMemory(nativeStream, num2); fixed (byte* bytes = Encoding.UTF8.GetBytes("1.0.0")) { Extensions.AssertNativeResultZero(Initialize((IntPtr)nativeStream, bytes, num2, num)); } } public unsafe CoderProcessResult Process(byte* inputData, int inputCount, byte* outputData, int outputCount, CoderFlushType flushType, out int inputTaken, out int outputProduced) { nativeStream->next_in = inputData; nativeStream->avail_in = (uint)inputCount; nativeStream->next_out = outputData; nativeStream->avail_out = (uint)outputCount; int result = Process((IntPtr)nativeStream, flushType); inputTaken = inputCount - (int)nativeStream->avail_in; outputProduced = outputCount - (int)nativeStream->avail_out; return (CoderProcessResult)result; } protected unsafe abstract int Initialize(IntPtr stream, byte* version, int streamSize, int windowBits); protected abstract int Process(IntPtr stream, CoderFlushType flushType); protected abstract int End(IntPtr stream); }