Decompiled source of ZstdSharp Port v1.8.6
ZstdSharp.dll
Decompiled 4 hours ago
The result has been truncated due to the large size, download it to view full contents!
using System; using System.Buffers; using System.Buffers.Binary; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using ZstdSharp; using ZstdSharp.Unsafe; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("Oleg Stepanischev")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright Oleg Stepanischev 2025")] [assembly: AssemblyDescription("Port of zstd compression library to c#")] [assembly: AssemblyFileVersion("0.8.6.0")] [assembly: AssemblyInformationalVersion("0.8.6+f475a7d4746d305094fbb09563cb0fcb167bec18")] [assembly: AssemblyProduct("ZstdSharp")] [assembly: AssemblyTitle("ZstdSharp")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/oleg-st/ZstdSharp.git")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("0.8.6.0")] [module: UnverifiableCode] [module: SkipLocalsInit] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsUnmanagedAttribute : Attribute { } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NativeIntegerAttribute : Attribute { public readonly bool[] TransformFlags; public NativeIntegerAttribute() { TransformFlags = new bool[1] { true }; } public NativeIntegerAttribute(bool[] P_0) { TransformFlags = P_0; } } [AttributeUsage(AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Interface, Inherited = false)] internal sealed class SkipLocalsInitAttribute : Attribute { } } namespace System.Numerics { public static class BitOperations { public unsafe static readonly byte* TrailingZeroCountDeBruijn = UnsafeHelper.GetArrayPointer(new byte[32] { 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 }); public unsafe static readonly byte* Log2DeBruijn = UnsafeHelper.GetArrayPointer(new byte[32] { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }); [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static int Log2(uint value) { value |= 1u; value |= value >> 1; value |= value >> 2; value |= value >> 4; value |= value >> 8; value |= value >> 16; return Log2DeBruijn[(int)(value * 130329821 >> 27)]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Log2(ulong value) { value |= 1; uint num = (uint)(value >> 32); if (num == 0) { return Log2((uint)value); } return 32 + Log2(num); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int TrailingZeroCount(int value) { return TrailingZeroCount((uint)value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static int TrailingZeroCount(uint value) { if (value == 0) { return 32; } return TrailingZeroCountDeBruijn[(int)((value & (0 - value)) * 125613361 >> 27)]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int TrailingZeroCount(long value) { return TrailingZeroCount((ulong)value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int TrailingZeroCount(ulong value) { uint num = (uint)value; if (num == 0) { return 32 + TrailingZeroCount((uint)(value >> 32)); } return TrailingZeroCount(num); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint RotateLeft(uint value, int offset) { return (value << offset) | (value >> 32 - offset); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ulong RotateLeft(ulong value, int offset) { return (value << offset) | (value >> 64 - offset); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint RotateRight(uint value, int offset) { return (value >> offset) | (value << 32 - offset); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ulong RotateRight(ulong value, int offset) { return (value >> offset) | (value << 64 - offset); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static int LeadingZeroCount(uint value) { if (value == 0) { return 32; } value |= value >> 1; value |= value >> 2; value |= value >> 4; value |= value >> 8; value |= value >> 16; return 0x1F ^ Log2DeBruijn[(int)(value * 130329821 >> 27)]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LeadingZeroCount(ulong value) { uint num = (uint)(value >> 32); if (num == 0) { return 32 + LeadingZeroCount((uint)value); } return LeadingZeroCount(num); } } } namespace ZstdSharp { public class CompressionStream : Stream { private readonly Stream innerStream; private readonly byte[] outputBuffer; private readonly bool preserveCompressor; private readonly bool leaveOpen; private Compressor compressor; private ZSTD_outBuffer_s output; public override bool CanRead => false; public override bool CanSeek => false; public override bool CanWrite => true; public override long Length { get { throw new NotSupportedException(); } } public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } } public CompressionStream(Stream stream, int level = 3, int bufferSize = 0, bool leaveOpen = true) : this(stream, new Compressor(level), bufferSize, preserveCompressor: false, leaveOpen) { } public CompressionStream(Stream stream, Compressor compressor, int bufferSize = 0, bool preserveCompressor = true, bool leaveOpen = true) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanWrite) { throw new ArgumentException("Stream is not writable", "stream"); } if (bufferSize < 0) { throw new ArgumentOutOfRangeException("bufferSize"); } innerStream = stream; this.compressor = compressor; this.preserveCompressor = preserveCompressor; this.leaveOpen = leaveOpen; int num = ((bufferSize > 0) ? bufferSize : ((int)Methods.ZSTD_CStreamOutSize().EnsureZstdSuccess())); outputBuffer = ArrayPool<byte>.Shared.Rent(num); output = new ZSTD_outBuffer_s { pos = 0u, size = (nuint)num }; } public void SetParameter(ZSTD_cParameter parameter, int value) { EnsureNotDisposed(); compressor.SetParameter(parameter, value); } public int GetParameter(ZSTD_cParameter parameter) { EnsureNotDisposed(); return compressor.GetParameter(parameter); } public void LoadDictionary(byte[] dict) { EnsureNotDisposed(); compressor.LoadDictionary(dict); } ~CompressionStream() { Dispose(disposing: false); } public override async ValueTask DisposeAsync() { if (compressor == null) { return; } try { await FlushInternalAsync(ZSTD_EndDirective.ZSTD_e_end).ConfigureAwait(continueOnCapturedContext: false); } finally { ReleaseUnmanagedResources(); GC.SuppressFinalize(this); } } protected override void Dispose(bool disposing) { if (compressor == null) { return; } try { if (disposing) { FlushInternal(ZSTD_EndDirective.ZSTD_e_end); } } finally { ReleaseUnmanagedResources(); } } private void ReleaseUnmanagedResources() { if (!preserveCompressor) { compressor.Dispose(); } compressor = null; ArrayPool<byte>.Shared.Return(outputBuffer); if (!leaveOpen) { innerStream.Dispose(); } } public override void Flush() { FlushInternal(ZSTD_EndDirective.ZSTD_e_flush); } public override async Task FlushAsync(CancellationToken cancellationToken) { await FlushInternalAsync(ZSTD_EndDirective.ZSTD_e_flush, cancellationToken).ConfigureAwait(continueOnCapturedContext: false); } private void FlushInternal(ZSTD_EndDirective directive) { WriteInternal(null, directive); } private async Task FlushInternalAsync(ZSTD_EndDirective directive, CancellationToken cancellationToken = default(CancellationToken)) { await WriteInternalAsync(null, directive, cancellationToken).ConfigureAwait(continueOnCapturedContext: false); } public override void Write(byte[] buffer, int offset, int count) { Write(new ReadOnlySpan<byte>(buffer, offset, count)); } public override void Write(ReadOnlySpan<byte> buffer) { WriteInternal(buffer, ZSTD_EndDirective.ZSTD_e_continue); } private void WriteInternal(ReadOnlySpan<byte> buffer, ZSTD_EndDirective directive) { EnsureNotDisposed(); ZSTD_inBuffer_s zSTD_inBuffer_s = default(ZSTD_inBuffer_s); zSTD_inBuffer_s.pos = 0u; zSTD_inBuffer_s.size = (nuint)((buffer != null) ? buffer.Length : 0); ZSTD_inBuffer_s input = zSTD_inBuffer_s; nuint num; do { output.pos = 0u; num = CompressStream(ref input, buffer, directive); int num2 = (int)output.pos; if (num2 > 0) { innerStream.Write(outputBuffer, 0, num2); } } while ((directive == ZSTD_EndDirective.ZSTD_e_continue) ? (input.pos < input.size) : (num != 0)); } private async ValueTask WriteInternalAsync(ReadOnlyMemory<byte>? buffer, ZSTD_EndDirective directive, CancellationToken cancellationToken = default(CancellationToken)) { EnsureNotDisposed(); ZSTD_inBuffer_s zSTD_inBuffer_s = new ZSTD_inBuffer_s { pos = 0u }; nint size; ReadOnlyMemory<byte> value; if (!buffer.HasValue) { size = 0; } else { value = buffer.Value; size = value.Length; } zSTD_inBuffer_s.size = (nuint)size; ZSTD_inBuffer_s input = zSTD_inBuffer_s; nuint remaining; do { output.pos = 0u; CompressionStream compressionStream = this; ReadOnlySpan<byte> inputBuffer; if (!buffer.HasValue) { inputBuffer = null; } else { value = buffer.Value; inputBuffer = value.Span; } remaining = compressionStream.CompressStream(ref input, inputBuffer, directive); int num = (int)output.pos; if (num > 0) { await innerStream.WriteAsync(outputBuffer, 0, num, cancellationToken).ConfigureAwait(continueOnCapturedContext: false); } } while ((directive == ZSTD_EndDirective.ZSTD_e_continue) ? (input.pos < input.size) : (remaining != 0)); } public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { return WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken).AsTask(); } public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken)) { await WriteInternalAsync(buffer, ZSTD_EndDirective.ZSTD_e_continue, cancellationToken).ConfigureAwait(continueOnCapturedContext: false); } internal unsafe nuint CompressStream(ref ZSTD_inBuffer_s input, ReadOnlySpan<byte> inputBuffer, ZSTD_EndDirective directive) { fixed (byte* src = inputBuffer) { fixed (byte* dst = outputBuffer) { input.src = src; output.dst = dst; return compressor.CompressStream(ref input, ref output, directive).EnsureZstdSuccess(); } } } public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } public override void SetLength(long value) { throw new NotSupportedException(); } public override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); } private void EnsureNotDisposed() { if (compressor == null) { throw new ObjectDisposedException("CompressionStream"); } } public void SetPledgedSrcSize(ulong pledgedSrcSize) { EnsureNotDisposed(); compressor.SetPledgedSrcSize(pledgedSrcSize); } } public class Compressor : IDisposable { public const int DefaultCompressionLevel = 3; private int level = 3; private readonly SafeCctxHandle handle; public static int MinCompressionLevel => Methods.ZSTD_minCLevel(); public static int MaxCompressionLevel => Methods.ZSTD_maxCLevel(); public int Level { get { return level; } set { if (level != value) { level = value; SetParameter(ZSTD_cParameter.ZSTD_c_compressionLevel, value); } } } public unsafe void SetParameter(ZSTD_cParameter parameter, int value) { SafeHandleHolder<ZSTD_CCtx_s> safeHandleHolder = handle.Acquire(); try { Methods.ZSTD_CCtx_setParameter(safeHandleHolder, parameter, value).EnsureZstdSuccess(); } finally { safeHandleHolder.Dispose(); } } public unsafe int GetParameter(ZSTD_cParameter parameter) { SafeHandleHolder<ZSTD_CCtx_s> safeHandleHolder = handle.Acquire(); try { System.Runtime.CompilerServices.Unsafe.SkipInit(out int result); Methods.ZSTD_CCtx_getParameter(safeHandleHolder, parameter, &result).EnsureZstdSuccess(); return result; } finally { safeHandleHolder.Dispose(); } } public void LoadDictionary(byte[] dict) { ReadOnlySpan<byte> dict2 = new ReadOnlySpan<byte>(dict); LoadDictionary(dict2); } public unsafe void LoadDictionary(ReadOnlySpan<byte> dict) { SafeHandleHolder<ZSTD_CCtx_s> safeHandleHolder = handle.Acquire(); try { fixed (byte* dict2 = dict) { Methods.ZSTD_CCtx_loadDictionary(safeHandleHolder, dict2, (nuint)dict.Length).EnsureZstdSuccess(); } } finally { safeHandleHolder.Dispose(); } } public Compressor(int level = 3) { handle = SafeCctxHandle.Create(); Level = level; } public static int GetCompressBound(int length) { return (int)Methods.ZSTD_compressBound((nuint)length); } public static ulong GetCompressBoundLong(ulong length) { return Methods.ZSTD_compressBound((nuint)length); } public Span<byte> Wrap(ReadOnlySpan<byte> src) { byte[] array = new byte[GetCompressBound(src.Length)]; int length = Wrap(src, array); return new Span<byte>(array, 0, length); } public int Wrap(byte[] src, byte[] dest, int offset) { return Wrap(src, new Span<byte>(dest, offset, dest.Length - offset)); } public unsafe int Wrap(ReadOnlySpan<byte> src, Span<byte> dest) { fixed (byte* src2 = src) { fixed (byte* dst = dest) { SafeHandleHolder<ZSTD_CCtx_s> safeHandleHolder = handle.Acquire(); try { return (int)Methods.ZSTD_compress2(safeHandleHolder, dst, (nuint)dest.Length, src2, (nuint)src.Length).EnsureZstdSuccess(); } finally { safeHandleHolder.Dispose(); } } } } public int Wrap(ArraySegment<byte> src, ArraySegment<byte> dest) { return Wrap((ReadOnlySpan<byte>)src, (Span<byte>)dest); } public int Wrap(byte[] src, int srcOffset, int srcLength, byte[] dst, int dstOffset, int dstLength) { return Wrap(new ReadOnlySpan<byte>(src, srcOffset, srcLength), new Span<byte>(dst, dstOffset, dstLength)); } public bool TryWrap(byte[] src, byte[] dest, int offset, out int written) { return TryWrap(src, new Span<byte>(dest, offset, dest.Length - offset), out written); } public unsafe bool TryWrap(ReadOnlySpan<byte> src, Span<byte> dest, out int written) { fixed (byte* src2 = src) { fixed (byte* dst = dest) { SafeHandleHolder<ZSTD_CCtx_s> safeHandleHolder = handle.Acquire(); nuint num; try { num = Methods.ZSTD_compress2(safeHandleHolder, dst, (nuint)dest.Length, src2, (nuint)src.Length); } finally { safeHandleHolder.Dispose(); } GC.KeepAlive(this); if (num == (nuint)(-70)) { written = 0; return false; } num.EnsureZstdSuccess(); written = (int)num; return true; } } } public bool TryWrap(ArraySegment<byte> src, ArraySegment<byte> dest, out int written) { return TryWrap((ReadOnlySpan<byte>)src, (Span<byte>)dest, out written); } public bool TryWrap(byte[] src, int srcOffset, int srcLength, byte[] dst, int dstOffset, int dstLength, out int written) { return TryWrap(new ReadOnlySpan<byte>(src, srcOffset, srcLength), new Span<byte>(dst, dstOffset, dstLength), out written); } public void Dispose() { handle.Dispose(); GC.SuppressFinalize(this); } internal unsafe nuint CompressStream(ref ZSTD_inBuffer_s input, ref ZSTD_outBuffer_s output, ZSTD_EndDirective directive) { fixed (ZSTD_inBuffer_s* input2 = &input) { fixed (ZSTD_outBuffer_s* output2 = &output) { SafeHandleHolder<ZSTD_CCtx_s> safeHandleHolder = handle.Acquire(); try { return Methods.ZSTD_compressStream2(safeHandleHolder, output2, input2, directive).EnsureZstdSuccess(); } finally { safeHandleHolder.Dispose(); } } } } public unsafe void SetPledgedSrcSize(ulong pledgedSrcSize) { SafeHandleHolder<ZSTD_CCtx_s> safeHandleHolder = handle.Acquire(); try { Methods.ZSTD_CCtx_setPledgedSrcSize(safeHandleHolder, pledgedSrcSize).EnsureZstdSuccess(); } finally { safeHandleHolder.Dispose(); } } } internal class Constants { public const ulong MaxByteArrayLength = 2147483591uL; } public class DecompressionStream : Stream { private readonly Stream innerStream; private readonly byte[] inputBuffer; private readonly int inputBufferSize; private readonly bool preserveDecompressor; private readonly bool leaveOpen; private readonly bool checkEndOfStream; private Decompressor decompressor; private ZSTD_inBuffer_s input; private nuint lastDecompressResult; private bool contextDrained = true; public override bool CanRead => true; public override bool CanSeek => false; public override bool CanWrite => false; public override long Length { get { throw new NotSupportedException(); } } public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } } public DecompressionStream(Stream stream, int bufferSize = 0, bool checkEndOfStream = true, bool leaveOpen = true) : this(stream, new Decompressor(), bufferSize, checkEndOfStream, preserveDecompressor: false, leaveOpen) { } public DecompressionStream(Stream stream, Decompressor decompressor, int bufferSize = 0, bool checkEndOfStream = true, bool preserveDecompressor = true, bool leaveOpen = true) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanRead) { throw new ArgumentException("Stream is not readable", "stream"); } if (bufferSize < 0) { throw new ArgumentOutOfRangeException("bufferSize"); } innerStream = stream; this.decompressor = decompressor; this.preserveDecompressor = preserveDecompressor; this.leaveOpen = leaveOpen; this.checkEndOfStream = checkEndOfStream; inputBufferSize = ((bufferSize > 0) ? bufferSize : ((int)Methods.ZSTD_DStreamInSize().EnsureZstdSuccess())); inputBuffer = ArrayPool<byte>.Shared.Rent(inputBufferSize); input = new ZSTD_inBuffer_s { pos = (nuint)inputBufferSize, size = (nuint)inputBufferSize }; } public void SetParameter(ZSTD_dParameter parameter, int value) { EnsureNotDisposed(); decompressor.SetParameter(parameter, value); } public int GetParameter(ZSTD_dParameter parameter) { EnsureNotDisposed(); return decompressor.GetParameter(parameter); } public void LoadDictionary(byte[] dict) { EnsureNotDisposed(); decompressor.LoadDictionary(dict); } ~DecompressionStream() { Dispose(disposing: false); } protected override void Dispose(bool disposing) { if (decompressor != null) { if (!preserveDecompressor) { decompressor.Dispose(); } decompressor = null; ArrayPool<byte>.Shared.Return(inputBuffer); if (!leaveOpen) { innerStream.Dispose(); } } } public override int Read(byte[] buffer, int offset, int count) { return Read(new Span<byte>(buffer, offset, count)); } public override int Read(Span<byte> buffer) { EnsureNotDisposed(); if (buffer.Length == 0) { return 0; } ZSTD_outBuffer_s zSTD_outBuffer_s = default(ZSTD_outBuffer_s); zSTD_outBuffer_s.pos = 0u; zSTD_outBuffer_s.size = (nuint)buffer.Length; ZSTD_outBuffer_s output = zSTD_outBuffer_s; while (true) { if (input.pos < input.size || !contextDrained) { nuint pos = input.pos; nuint num = DecompressStream(ref output, buffer); if (output.pos != 0 || pos != input.pos) { lastDecompressResult = num; } contextDrained = output.pos < output.size; if (output.pos != 0) { return (int)output.pos; } } else { int num2; if ((num2 = innerStream.Read(inputBuffer, 0, inputBufferSize)) == 0) { break; } input.size = (nuint)num2; input.pos = 0u; } } if (checkEndOfStream && lastDecompressResult != 0) { throw new EndOfStreamException("Premature end of stream"); } return 0; } public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask(); } public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken)) { EnsureNotDisposed(); if (buffer.Length == 0) { return 0; } ZSTD_outBuffer_s output = new ZSTD_outBuffer_s { pos = 0u, size = (nuint)buffer.Length }; while (true) { if (input.pos < input.size || !contextDrained) { nuint pos = input.pos; nuint num = DecompressStream(ref output, buffer.Span); if (output.pos != 0 || pos != input.pos) { lastDecompressResult = num; } contextDrained = output.pos < output.size; if (output.pos != 0) { return (int)output.pos; } } else { int num2; if ((num2 = await innerStream.ReadAsync(inputBuffer, 0, inputBufferSize, cancellationToken).ConfigureAwait(continueOnCapturedContext: false)) == 0) { break; } input.size = (nuint)num2; input.pos = 0u; } } if (checkEndOfStream && lastDecompressResult != 0) { throw new EndOfStreamException("Premature end of stream"); } return 0; } private unsafe nuint DecompressStream(ref ZSTD_outBuffer_s output, Span<byte> outputBuffer) { fixed (byte* src = inputBuffer) { fixed (byte* dst = outputBuffer) { input.src = src; output.dst = dst; return decompressor.DecompressStream(ref input, ref output); } } } public override void Flush() { throw new NotSupportedException(); } public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } public override void SetLength(long value) { throw new NotSupportedException(); } public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); } private void EnsureNotDisposed() { if (decompressor == null) { throw new ObjectDisposedException("DecompressionStream"); } } } public class Decompressor : IDisposable { private readonly SafeDctxHandle handle; public Decompressor() { handle = SafeDctxHandle.Create(); } public unsafe void SetParameter(ZSTD_dParameter parameter, int value) { SafeHandleHolder<ZSTD_DCtx_s> safeHandleHolder = handle.Acquire(); try { Methods.ZSTD_DCtx_setParameter(safeHandleHolder, parameter, value).EnsureZstdSuccess(); } finally { safeHandleHolder.Dispose(); } } public unsafe int GetParameter(ZSTD_dParameter parameter) { SafeHandleHolder<ZSTD_DCtx_s> safeHandleHolder = handle.Acquire(); try { System.Runtime.CompilerServices.Unsafe.SkipInit(out int result); Methods.ZSTD_DCtx_getParameter(safeHandleHolder, parameter, &result).EnsureZstdSuccess(); return result; } finally { safeHandleHolder.Dispose(); } } public void LoadDictionary(byte[] dict) { ReadOnlySpan<byte> dict2 = new ReadOnlySpan<byte>(dict); LoadDictionary(dict2); } public unsafe void LoadDictionary(ReadOnlySpan<byte> dict) { SafeHandleHolder<ZSTD_DCtx_s> safeHandleHolder = handle.Acquire(); try { fixed (byte* dict2 = dict) { Methods.ZSTD_DCtx_loadDictionary(safeHandleHolder, dict2, (nuint)dict.Length).EnsureZstdSuccess(); } } finally { safeHandleHolder.Dispose(); } } public unsafe static ulong GetDecompressedSize(ReadOnlySpan<byte> src) { fixed (byte* src2 = src) { return Methods.ZSTD_decompressBound(src2, (nuint)src.Length).EnsureContentSizeOk(); } } public static ulong GetDecompressedSize(ArraySegment<byte> src) { return GetDecompressedSize((ReadOnlySpan<byte>)src); } public static ulong GetDecompressedSize(byte[] src, int srcOffset, int srcLength) { return GetDecompressedSize(new ReadOnlySpan<byte>(src, srcOffset, srcLength)); } public Span<byte> Unwrap(ReadOnlySpan<byte> src, int maxDecompressedSize = int.MaxValue) { ulong decompressedSize = GetDecompressedSize(src); if (decompressedSize > (ulong)maxDecompressedSize) { throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, string.Format("Decompressed content size {0} is greater than {1} {2}", decompressedSize, "maxDecompressedSize", maxDecompressedSize)); } if (decompressedSize > 2147483591) { throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, $"Decompressed content size {decompressedSize} is greater than max possible byte array size {2147483591uL}"); } byte[] array = new byte[decompressedSize]; int length = Unwrap(src, array); return new Span<byte>(array, 0, length); } public int Unwrap(byte[] src, byte[] dest, int offset) { return Unwrap(src, new Span<byte>(dest, offset, dest.Length - offset)); } public unsafe int Unwrap(ReadOnlySpan<byte> src, Span<byte> dest) { fixed (byte* src2 = src) { fixed (byte* dst = dest) { SafeHandleHolder<ZSTD_DCtx_s> safeHandleHolder = handle.Acquire(); try { return (int)Methods.ZSTD_decompressDCtx(safeHandleHolder, dst, (nuint)dest.Length, src2, (nuint)src.Length).EnsureZstdSuccess(); } finally { safeHandleHolder.Dispose(); } } } } public int Unwrap(byte[] src, int srcOffset, int srcLength, byte[] dst, int dstOffset, int dstLength) { return Unwrap(new ReadOnlySpan<byte>(src, srcOffset, srcLength), new Span<byte>(dst, dstOffset, dstLength)); } public bool TryUnwrap(byte[] src, byte[] dest, int offset, out int written) { return TryUnwrap(src, new Span<byte>(dest, offset, dest.Length - offset), out written); } public unsafe bool TryUnwrap(ReadOnlySpan<byte> src, Span<byte> dest, out int written) { fixed (byte* src2 = src) { fixed (byte* dst = dest) { SafeHandleHolder<ZSTD_DCtx_s> safeHandleHolder = handle.Acquire(); nuint num; try { num = Methods.ZSTD_decompressDCtx(safeHandleHolder, dst, (nuint)dest.Length, src2, (nuint)src.Length); } finally { safeHandleHolder.Dispose(); } GC.KeepAlive(this); if (num == (nuint)(-70)) { written = 0; return false; } num.EnsureZstdSuccess(); written = (int)num; return true; } } } public bool TryUnwrap(byte[] src, int srcOffset, int srcLength, byte[] dst, int dstOffset, int dstLength, out int written) { return TryUnwrap(new ReadOnlySpan<byte>(src, srcOffset, srcLength), new Span<byte>(dst, dstOffset, dstLength), out written); } public void Dispose() { handle.Dispose(); GC.SuppressFinalize(this); } internal unsafe nuint DecompressStream(ref ZSTD_inBuffer_s input, ref ZSTD_outBuffer_s output) { fixed (ZSTD_inBuffer_s* input2 = &input) { fixed (ZSTD_outBuffer_s* output2 = &output) { SafeHandleHolder<ZSTD_DCtx_s> safeHandleHolder = handle.Acquire(); try { return Methods.ZSTD_decompressStream(safeHandleHolder, output2, input2).EnsureZstdSuccess(); } finally { safeHandleHolder.Dispose(); } } } } } public static class DictBuilder { public const int DefaultDictCapacity = 112640; public static byte[] TrainFromBuffer(IEnumerable<byte[]> samples, int dictCapacity = 112640) { return TrainFromBufferFastCover(samples, Methods.ZSTD_defaultCLevel(), dictCapacity).ToArray(); } public static Span<byte> TrainFromBufferFastCover(IEnumerable<byte[]> samples, int level, int dictCapacity = 112640) { return TrainFromBufferFastCover(samples, new ZDICT_fastCover_params_t { d = 8u, steps = 4u, zParams = new ZDICT_params_t { compressionLevel = level } }, dictCapacity); } public unsafe static Span<byte> TrainFromBufferFastCover(IEnumerable<byte[]> samples, ZDICT_fastCover_params_t @params, int dictCapacity = 112640) { MemoryStream ms = new MemoryStream(); UIntPtr[] array = samples.Select(delegate(byte[] sample) { ms.Write(sample, 0, sample.Length); return (nuint)sample.Length; }).ToArray(); byte[] array2 = new byte[dictCapacity]; fixed (byte* dictBuffer = array2) { fixed (byte* samplesBuffer = ms.GetBuffer()) { fixed (UIntPtr* samplesSizes = array) { int length = (int)Methods.ZDICT_optimizeTrainFromBuffer_fastCover(dictBuffer, (nuint)dictCapacity, samplesBuffer, samplesSizes, (uint)array.Length, &@params).EnsureZdictSuccess(); return new Span<byte>(array2, 0, length); } } } } } internal class JobThreadPool : IDisposable { private struct Job { public unsafe void* function; public unsafe void* opaque; } private class JobThread { private Thread Thread { get; } public CancellationTokenSource CancellationTokenSource { get; } public JobThread(Thread thread) { CancellationTokenSource = new CancellationTokenSource(); Thread = thread; } public void Start() { Thread.Start(this); } public void Cancel() { CancellationTokenSource.Cancel(); } public void Join() { Thread.Join(); } } private int numThreads; private readonly List<JobThread> threads; private readonly BlockingCollection<Job> queue; private unsafe void Worker(object obj) { if (!(obj is JobThread jobThread)) { return; } CancellationToken token = jobThread.CancellationTokenSource.Token; while (!queue.IsCompleted && !token.IsCancellationRequested) { try { if (queue.TryTake(out var item, -1, token)) { ((delegate*<void*, void>)item.function)(item.opaque); } } catch (InvalidOperationException) { } catch (OperationCanceledException) { } } } public JobThreadPool(int num, int queueSize) { numThreads = num; queue = new BlockingCollection<Job>(queueSize + 1); threads = new List<JobThread>(num); for (int i = 0; i < numThreads; i++) { CreateThread(); } } private void CreateThread() { JobThread jobThread = new JobThread(new Thread(Worker)); threads.Add(jobThread); jobThread.Start(); } public void Resize(int num) { lock (threads) { if (num < numThreads) { for (int num2 = numThreads - 1; num2 >= num; num2--) { threads[num2].Cancel(); threads.RemoveAt(num2); } } else { for (int i = numThreads; i < num; i++) { CreateThread(); } } } numThreads = num; } public unsafe void Add(void* function, void* opaque) { queue.Add(new Job { function = function, opaque = opaque }); } public unsafe bool TryAdd(void* function, void* opaque) { return queue.TryAdd(new Job { function = function, opaque = opaque }); } public void Join(bool cancel = true) { queue.CompleteAdding(); List<JobThread> list; lock (threads) { list = new List<JobThread>(threads); } if (cancel) { foreach (JobThread item in list) { item.Cancel(); } } foreach (JobThread item2 in list) { item2.Join(); } } public void Dispose() { queue.Dispose(); } public int Size() { return 0; } } internal abstract class SafeZstdHandle : SafeHandle { public sealed override bool IsInvalid => handle == IntPtr.Zero; protected SafeZstdHandle() : base(IntPtr.Zero, ownsHandle: true) { } } internal sealed class SafeCctxHandle : SafeZstdHandle { private SafeCctxHandle() { } public unsafe static SafeCctxHandle Create() { SafeCctxHandle safeCctxHandle = new SafeCctxHandle(); bool flag = false; try { ZSTD_CCtx_s* ptr = Methods.ZSTD_createCCtx(); if (ptr == null) { throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Failed to create cctx"); } safeCctxHandle.SetHandle((IntPtr)ptr); flag = true; } finally { if (!flag) { safeCctxHandle.SetHandleAsInvalid(); } } return safeCctxHandle; } public SafeHandleHolder<ZSTD_CCtx_s> Acquire() { return new SafeHandleHolder<ZSTD_CCtx_s>(this); } protected unsafe override bool ReleaseHandle() { return Methods.ZSTD_freeCCtx((ZSTD_CCtx_s*)(void*)handle) == 0; } } internal sealed class SafeDctxHandle : SafeZstdHandle { private SafeDctxHandle() { } public unsafe static SafeDctxHandle Create() { SafeDctxHandle safeDctxHandle = new SafeDctxHandle(); bool flag = false; try { ZSTD_DCtx_s* ptr = Methods.ZSTD_createDCtx(); if (ptr == null) { throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Failed to create dctx"); } safeDctxHandle.SetHandle((IntPtr)ptr); flag = true; } finally { if (!flag) { safeDctxHandle.SetHandleAsInvalid(); } } return safeDctxHandle; } public SafeHandleHolder<ZSTD_DCtx_s> Acquire() { return new SafeHandleHolder<ZSTD_DCtx_s>(this); } protected unsafe override bool ReleaseHandle() { return Methods.ZSTD_freeDCtx((ZSTD_DCtx_s*)(void*)handle) == 0; } } internal ref struct SafeHandleHolder<T> where T : unmanaged { private readonly SafeHandle _handle; private bool _refAdded; public SafeHandleHolder(SafeHandle safeHandle) { _handle = safeHandle; _refAdded = false; safeHandle.DangerousAddRef(ref _refAdded); } public unsafe static implicit operator T*(SafeHandleHolder<T> holder) { return (T*)(void*)holder._handle.DangerousGetHandle(); } public void Dispose() { if (_refAdded) { _handle.DangerousRelease(); _refAdded = false; } } } internal static class SynchronizationWrapper { private unsafe static object UnwrapObject(void** obj) { return UnmanagedObject.Unwrap<object>(*obj); } public unsafe static void Init(void** obj) { *obj = UnmanagedObject.Wrap(new object()); } public unsafe static void Free(void** obj) { UnmanagedObject.Free(*obj); } public unsafe static void Enter(void** obj) { Monitor.Enter(UnwrapObject(obj)); } public unsafe static void Exit(void** obj) { Monitor.Exit(UnwrapObject(obj)); } public unsafe static void Pulse(void** obj) { Monitor.Pulse(UnwrapObject(obj)); } public unsafe static void PulseAll(void** obj) { Monitor.PulseAll(UnwrapObject(obj)); } public unsafe static void Wait(void** mutex) { Monitor.Wait(UnwrapObject(mutex)); } } public static class ThrowHelper { private const ulong ZSTD_CONTENTSIZE_UNKNOWN = ulong.MaxValue; private const ulong ZSTD_CONTENTSIZE_ERROR = 18446744073709551614uL; public static nuint EnsureZstdSuccess(this nuint returnValue) { if (Methods.ZSTD_isError(returnValue)) { ThrowException(returnValue, Methods.ZSTD_getErrorName(returnValue)); } return returnValue; } public static nuint EnsureZdictSuccess(this nuint returnValue) { if (Methods.ZDICT_isError(returnValue)) { ThrowException(returnValue, Methods.ZDICT_getErrorName(returnValue)); } return returnValue; } public static ulong EnsureContentSizeOk(this ulong returnValue) { return returnValue switch { ulong.MaxValue => throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Decompressed content size is not specified"), 18446744073709551614uL => throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Decompressed content size cannot be determined (e.g. invalid magic number, srcSize too small)"), _ => returnValue, }; } private static void ThrowException(nuint returnValue, string message) { throw new ZstdException((ZSTD_ErrorCode)(0 - returnValue), message); } } internal static class UnmanagedObject { public unsafe static void* Wrap(object obj) { return (void*)GCHandle.ToIntPtr(GCHandle.Alloc(obj)); } private unsafe static GCHandle UnwrapGcHandle(void* value) { return GCHandle.FromIntPtr((IntPtr)value); } public unsafe static T Unwrap<T>(void* value) { return (T)UnwrapGcHandle(value).Target; } public unsafe static void Free(void* value) { UnwrapGcHandle(value).Free(); } } public static class UnsafeHelper { public unsafe static void* PoisonMemory(void* destination, ulong size) { System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(destination, 204, (uint)size); return destination; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void* malloc(ulong size) { return (void*)Marshal.AllocHGlobal((nint)size); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void* calloc(ulong num, ulong size) { ulong num2 = num * size; void* intPtr = (void*)Marshal.AllocHGlobal((nint)num2); System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(intPtr, 0, (uint)num2); return intPtr; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void memcpy(void* destination, void* source, uint size) { System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(destination, source, size); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void memset(void* memPtr, byte val, uint size) { System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(memPtr, val, size); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void free(void* ptr) { Marshal.FreeHGlobal((IntPtr)ptr); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static T* GetArrayPointer<T>(T[] array) where T : unmanaged { uint num = (uint)(sizeof(T) * array.Length); T* ptr = (T*)malloc(num); fixed (T* ptr2 = &array[0]) { void* source = ptr2; System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(ptr, source, num); } return ptr; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [Conditional("DEBUG")] public static void assert(bool condition, string message = null) { if (!condition) { throw new ArgumentException(message ?? "assert failed"); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static void memmove(void* destination, void* source, ulong size) { Buffer.MemoryCopy(source, destination, size, size); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe static int memcmp(void* buf1, void* buf2, ulong size) { int length = (int)size; return new ReadOnlySpan<byte>(buf1, length).SequenceCompareTo(new ReadOnlySpan<byte>(buf2, length)); } } public class ZstdException : Exception { public ZSTD_ErrorCode Code { get; } public ZstdException(ZSTD_ErrorCode code, string message) : base(message) { Code = code; } } } namespace ZstdSharp.Unsafe { public struct algo_time_t { public uint tableTime; public uint decode256Time; public algo_time_t(uint tableTime, uint decode256Time) { this.tableTime = tableTime; this.decode256Time = decode256Time; } } public static class Methods { private unsafe static readonly uint* BIT_mask = UnsafeHelper.GetArrayPointer(new uint[32] { 0u, 1u, 3u, 7u, 15u, 31u, 63u, 127u, 255u, 511u, 1023u, 2047u, 4095u, 8191u, 16383u, 32767u, 65535u, 131071u, 262143u, 524287u, 1048575u, 2097151u, 4194303u, 8388607u, 16777215u, 33554431u, 67108863u, 134217727u, 268435455u, 536870911u, 1073741823u, 2147483647u }); private unsafe static readonly nuint* static_zeroFilled = (nuint*)UnsafeHelper.GetArrayPointer(new byte[8]); private static readonly ZSTD_compressionParameters[][] ZSTD_defaultCParameters = new ZSTD_compressionParameters[4][] { new ZSTD_compressionParameters[23] { new ZSTD_compressionParameters(19u, 12u, 13u, 1u, 6u, 1u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(19u, 13u, 14u, 1u, 7u, 0u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(20u, 15u, 16u, 1u, 6u, 0u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(21u, 16u, 17u, 1u, 5u, 0u, ZSTD_strategy.ZSTD_dfast), new ZSTD_compressionParameters(21u, 18u, 18u, 1u, 5u, 0u, ZSTD_strategy.ZSTD_dfast), new ZSTD_compressionParameters(21u, 18u, 19u, 3u, 5u, 2u, ZSTD_strategy.ZSTD_greedy), new ZSTD_compressionParameters(21u, 18u, 19u, 3u, 5u, 4u, ZSTD_strategy.ZSTD_lazy), new ZSTD_compressionParameters(21u, 19u, 20u, 4u, 5u, 8u, ZSTD_strategy.ZSTD_lazy), new ZSTD_compressionParameters(21u, 19u, 20u, 4u, 5u, 16u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(22u, 20u, 21u, 4u, 5u, 16u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(22u, 21u, 22u, 5u, 5u, 16u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(22u, 21u, 22u, 6u, 5u, 16u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(22u, 22u, 23u, 6u, 5u, 32u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(22u, 22u, 22u, 4u, 5u, 32u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(22u, 22u, 23u, 5u, 5u, 32u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(22u, 23u, 23u, 6u, 5u, 32u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(22u, 22u, 22u, 5u, 5u, 48u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(23u, 23u, 22u, 5u, 4u, 64u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(23u, 23u, 22u, 6u, 3u, 64u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(23u, 24u, 22u, 7u, 3u, 256u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(25u, 25u, 23u, 7u, 3u, 256u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(26u, 26u, 24u, 7u, 3u, 512u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(27u, 27u, 25u, 9u, 3u, 999u, ZSTD_strategy.ZSTD_btultra2) }, new ZSTD_compressionParameters[23] { new ZSTD_compressionParameters(18u, 12u, 13u, 1u, 5u, 1u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(18u, 13u, 14u, 1u, 6u, 0u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(18u, 14u, 14u, 1u, 5u, 0u, ZSTD_strategy.ZSTD_dfast), new ZSTD_compressionParameters(18u, 16u, 16u, 1u, 4u, 0u, ZSTD_strategy.ZSTD_dfast), new ZSTD_compressionParameters(18u, 16u, 17u, 3u, 5u, 2u, ZSTD_strategy.ZSTD_greedy), new ZSTD_compressionParameters(18u, 17u, 18u, 5u, 5u, 2u, ZSTD_strategy.ZSTD_greedy), new ZSTD_compressionParameters(18u, 18u, 19u, 3u, 5u, 4u, ZSTD_strategy.ZSTD_lazy), new ZSTD_compressionParameters(18u, 18u, 19u, 4u, 4u, 4u, ZSTD_strategy.ZSTD_lazy), new ZSTD_compressionParameters(18u, 18u, 19u, 4u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(18u, 18u, 19u, 5u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(18u, 18u, 19u, 6u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(18u, 18u, 19u, 5u, 4u, 12u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(18u, 19u, 19u, 7u, 4u, 12u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(18u, 18u, 19u, 4u, 4u, 16u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(18u, 18u, 19u, 4u, 3u, 32u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(18u, 18u, 19u, 6u, 3u, 128u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(18u, 19u, 19u, 6u, 3u, 128u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(18u, 19u, 19u, 8u, 3u, 256u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(18u, 19u, 19u, 6u, 3u, 128u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(18u, 19u, 19u, 8u, 3u, 256u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(18u, 19u, 19u, 10u, 3u, 512u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(18u, 19u, 19u, 12u, 3u, 512u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(18u, 19u, 19u, 13u, 3u, 999u, ZSTD_strategy.ZSTD_btultra2) }, new ZSTD_compressionParameters[23] { new ZSTD_compressionParameters(17u, 12u, 12u, 1u, 5u, 1u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(17u, 12u, 13u, 1u, 6u, 0u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(17u, 13u, 15u, 1u, 5u, 0u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(17u, 15u, 16u, 2u, 5u, 0u, ZSTD_strategy.ZSTD_dfast), new ZSTD_compressionParameters(17u, 17u, 17u, 2u, 4u, 0u, ZSTD_strategy.ZSTD_dfast), new ZSTD_compressionParameters(17u, 16u, 17u, 3u, 4u, 2u, ZSTD_strategy.ZSTD_greedy), new ZSTD_compressionParameters(17u, 16u, 17u, 3u, 4u, 4u, ZSTD_strategy.ZSTD_lazy), new ZSTD_compressionParameters(17u, 16u, 17u, 3u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(17u, 16u, 17u, 4u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(17u, 16u, 17u, 5u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(17u, 16u, 17u, 6u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(17u, 17u, 17u, 5u, 4u, 8u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(17u, 18u, 17u, 7u, 4u, 12u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(17u, 18u, 17u, 3u, 4u, 12u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(17u, 18u, 17u, 4u, 3u, 32u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(17u, 18u, 17u, 6u, 3u, 256u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(17u, 18u, 17u, 6u, 3u, 128u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(17u, 18u, 17u, 8u, 3u, 256u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(17u, 18u, 17u, 10u, 3u, 512u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(17u, 18u, 17u, 5u, 3u, 256u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(17u, 18u, 17u, 7u, 3u, 512u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(17u, 18u, 17u, 9u, 3u, 512u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(17u, 18u, 17u, 11u, 3u, 999u, ZSTD_strategy.ZSTD_btultra2) }, new ZSTD_compressionParameters[23] { new ZSTD_compressionParameters(14u, 12u, 13u, 1u, 5u, 1u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(14u, 14u, 15u, 1u, 5u, 0u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(14u, 14u, 15u, 1u, 4u, 0u, ZSTD_strategy.ZSTD_fast), new ZSTD_compressionParameters(14u, 14u, 15u, 2u, 4u, 0u, ZSTD_strategy.ZSTD_dfast), new ZSTD_compressionParameters(14u, 14u, 14u, 4u, 4u, 2u, ZSTD_strategy.ZSTD_greedy), new ZSTD_compressionParameters(14u, 14u, 14u, 3u, 4u, 4u, ZSTD_strategy.ZSTD_lazy), new ZSTD_compressionParameters(14u, 14u, 14u, 4u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(14u, 14u, 14u, 6u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(14u, 14u, 14u, 8u, 4u, 8u, ZSTD_strategy.ZSTD_lazy2), new ZSTD_compressionParameters(14u, 15u, 14u, 5u, 4u, 8u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(14u, 15u, 14u, 9u, 4u, 8u, ZSTD_strategy.ZSTD_btlazy2), new ZSTD_compressionParameters(14u, 15u, 14u, 3u, 4u, 12u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(14u, 15u, 14u, 4u, 3u, 24u, ZSTD_strategy.ZSTD_btopt), new ZSTD_compressionParameters(14u, 15u, 14u, 5u, 3u, 32u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(14u, 15u, 15u, 6u, 3u, 64u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(14u, 15u, 15u, 7u, 3u, 256u, ZSTD_strategy.ZSTD_btultra), new ZSTD_compressionParameters(14u, 15u, 15u, 5u, 3u, 48u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(14u, 15u, 15u, 6u, 3u, 128u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(14u, 15u, 15u, 7u, 3u, 256u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(14u, 15u, 15u, 8u, 3u, 256u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(14u, 15u, 15u, 8u, 3u, 512u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(14u, 15u, 15u, 9u, 3u, 512u, ZSTD_strategy.ZSTD_btultra2), new ZSTD_compressionParameters(14u, 15u, 15u, 10u, 3u, 999u, ZSTD_strategy.ZSTD_btultra2) } }; private static int g_displayLevel = 0; private unsafe static readonly FASTCOVER_accel_t* FASTCOVER_defaultAccelParameters = UnsafeHelper.GetArrayPointer(new FASTCOVER_accel_t[11] { new FASTCOVER_accel_t(100u, 0u), new FASTCOVER_accel_t(100u, 0u), new FASTCOVER_accel_t(50u, 1u), new FASTCOVER_accel_t(34u, 2u), new FASTCOVER_accel_t(25u, 3u), new FASTCOVER_accel_t(20u, 4u), new FASTCOVER_accel_t(17u, 5u), new FASTCOVER_accel_t(14u, 6u), new FASTCOVER_accel_t(13u, 7u), new FASTCOVER_accel_t(11u, 8u), new FASTCOVER_accel_t(10u, 9u) }); private unsafe static readonly uint* rtbTable = UnsafeHelper.GetArrayPointer(new uint[8] { 0u, 473195u, 504333u, 520860u, 550000u, 700000u, 750000u, 830000u }); private static readonly algo_time_t[][] algoTime = new algo_time_t[16][] { new algo_time_t[2] { new algo_time_t(0u, 0u), new algo_time_t(1u, 1u) }, new algo_time_t[2] { new algo_time_t(0u, 0u), new algo_time_t(1u, 1u) }, new algo_time_t[2] { new algo_time_t(150u, 216u), new algo_time_t(381u, 119u) }, new algo_time_t[2] { new algo_time_t(170u, 205u), new algo_time_t(514u, 112u) }, new algo_time_t[2] { new algo_time_t(177u, 199u), new algo_time_t(539u, 110u) }, new algo_time_t[2] { new algo_time_t(197u, 194u), new algo_time_t(644u, 107u) }, new algo_time_t[2] { new algo_time_t(221u, 192u), new algo_time_t(735u, 107u) }, new algo_time_t[2] { new algo_time_t(256u, 189u), new algo_time_t(881u, 106u) }, new algo_time_t[2] { new algo_time_t(359u, 188u), new algo_time_t(1167u, 109u) }, new algo_time_t[2] { new algo_time_t(582u, 187u), new algo_time_t(1570u, 114u) }, new algo_time_t[2] { new algo_time_t(688u, 187u), new algo_time_t(1712u, 122u) }, new algo_time_t[2] { new algo_time_t(825u, 186u), new algo_time_t(1965u, 136u) }, new algo_time_t[2] { new algo_time_t(976u, 185u), new algo_time_t(2131u, 150u) }, new algo_time_t[2] { new algo_time_t(1180u, 186u), new algo_time_t(2070u, 175u) }, new algo_time_t[2] { new algo_time_t(1377u, 185u), new algo_time_t(1731u, 202u) }, new algo_time_t[2] { new algo_time_t(1412u, 185u), new algo_time_t(1695u, 202u) } }; private static readonly ZSTD_customMem ZSTD_defaultCMem = new ZSTD_customMem(null, null, null); private unsafe static readonly ulong* srcSizeTiers = UnsafeHelper.GetArrayPointer(new ulong[4] { 16384uL, 131072uL, 262144uL, 18446744073709551615uL }); private unsafe static readonly nuint* attachDictSizeCutoffs = UnsafeHelper.GetArrayPointer(new UIntPtr[10] { (nuint)8192u, (nuint)8192u, (nuint)16384u, (nuint)32768u, (nuint)32768u, (nuint)32768u, (nuint)32768u, (nuint)32768u, (nuint)8192u, (nuint)8192u }); private unsafe static readonly ZSTD_BlockCompressor_f[][] blockCompressor = new ZSTD_BlockCompressor_f[4][] { new ZSTD_BlockCompressor_f[10] { ZSTD_compressBlock_fast, ZSTD_compressBlock_fast, ZSTD_compressBlock_doubleFast, ZSTD_compressBlock_greedy, ZSTD_compressBlock_lazy, ZSTD_compressBlock_lazy2, ZSTD_compressBlock_btlazy2, ZSTD_compressBlock_btopt, ZSTD_compressBlock_btultra, ZSTD_compressBlock_btultra2 }, new ZSTD_BlockCompressor_f[10] { ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_doubleFast_extDict, ZSTD_compressBlock_greedy_extDict, ZSTD_compressBlock_lazy_extDict, ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, ZSTD_compressBlock_btopt_extDict, ZSTD_compressBlock_btultra_extDict, ZSTD_compressBlock_btultra_extDict }, new ZSTD_BlockCompressor_f[10] { ZSTD_compressBlock_fast_dictMatchState, ZSTD_compressBlock_fast_dictMatchState, ZSTD_compressBlock_doubleFast_dictMatchState, ZSTD_compressBlock_greedy_dictMatchState, ZSTD_compressBlock_lazy_dictMatchState, ZSTD_compressBlock_lazy2_dictMatchState, ZSTD_compressBlock_btlazy2_dictMatchState, ZSTD_compressBlock_btopt_dictMatchState, ZSTD_compressBlock_btultra_dictMatchState, ZSTD_compressBlock_btultra_dictMatchState }, new ZSTD_BlockCompressor_f[10] { null, null, null, ZSTD_compressBlock_greedy_dedicatedDictSearch, ZSTD_compressBlock_lazy_dedicatedDictSearch, ZSTD_compressBlock_lazy2_dedicatedDictSearch, null, null, null, null } }; private unsafe static readonly ZSTD_BlockCompressor_f[][] rowBasedBlockCompressors = new ZSTD_BlockCompressor_f[4][] { new ZSTD_BlockCompressor_f[3] { ZSTD_compressBlock_greedy_row, ZSTD_compressBlock_lazy_row, ZSTD_compressBlock_lazy2_row }, new ZSTD_BlockCompressor_f[3] { ZSTD_compressBlock_greedy_extDict_row, ZSTD_compressBlock_lazy_extDict_row, ZSTD_compressBlock_lazy2_extDict_row }, new ZSTD_BlockCompressor_f[3] { ZSTD_compressBlock_greedy_dictMatchState_row, ZSTD_compressBlock_lazy_dictMatchState_row, ZSTD_compressBlock_lazy2_dictMatchState_row }, new ZSTD_BlockCompressor_f[3] { ZSTD_compressBlock_greedy_dedicatedDictSearch_row, ZSTD_compressBlock_lazy_dedicatedDictSearch_row, ZSTD_compressBlock_lazy2_dedicatedDictSearch_row } }; private unsafe static readonly int* splitLevels = UnsafeHelper.GetArrayPointer(new int[10] { 0, 0, 1, 2, 2, 3, 3, 4, 4, 4 }); private static readonly RawSeqStore_t kNullRawSeqStore = new RawSeqStore_t(null, 0u, 0u, 0u, 0u); private unsafe static readonly byte* LL_Code = UnsafeHelper.GetArrayPointer(new byte[64] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24 }); private unsafe static readonly byte* ML_Code = UnsafeHelper.GetArrayPointer(new byte[128] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 }); private const uint prime3bytes = 506832829u; private const uint prime4bytes = 2654435761u; private const ulong prime5bytes = 889523592379uL; private const ulong prime6bytes = 227718039650203uL; private const ulong prime7bytes = 58295818150454627uL; private const ulong prime8bytes = 14923729446382167139uL; private unsafe static readonly byte* stringToByte_20_00 = UnsafeHelper.GetArrayPointer(new byte[2] { 32, 0 }); private unsafe static readonly uint* kInverseProbabilityLog256 = UnsafeHelper.GetArrayPointer(new uint[256] { 0u, 2048u, 1792u, 1642u, 1536u, 1453u, 1386u, 1329u, 1280u, 1236u, 1197u, 1162u, 1130u, 1100u, 1073u, 1047u, 1024u, 1001u, 980u, 960u, 941u, 923u, 906u, 889u, 874u, 859u, 844u, 830u, 817u, 804u, 791u, 779u, 768u, 756u, 745u, 734u, 724u, 714u, 704u, 694u, 685u, 676u, 667u, 658u, 650u, 642u, 633u, 626u, 618u, 610u, 603u, 595u, 588u, 581u, 574u, 567u, 561u, 554u, 548u, 542u, 535u, 529u, 523u, 517u, 512u, 506u, 500u, 495u, 489u, 484u, 478u, 473u, 468u, 463u, 458u, 453u, 448u, 443u, 438u, 434u, 429u, 424u, 420u, 415u, 411u, 407u, 402u, 398u, 394u, 390u, 386u, 382u, 377u, 373u, 370u, 366u, 362u, 358u, 354u, 350u, 347u, 343u, 339u, 336u, 332u, 329u, 325u, 322u, 318u, 315u, 311u, 308u, 305u, 302u, 298u, 295u, 292u, 289u, 286u, 282u, 279u, 276u, 273u, 270u, 267u, 264u, 261u, 258u, 256u, 253u, 250u, 247u, 244u, 241u, 239u, 236u, 233u, 230u, 228u, 225u, 222u, 220u, 217u, 215u, 212u, 209u, 207u, 204u, 202u, 199u, 197u, 194u, 192u, 190u, 187u, 185u, 182u, 180u, 178u, 175u, 173u, 171u, 168u, 166u, 164u, 162u, 159u, 157u, 155u, 153u, 151u, 149u, 146u, 144u, 142u, 140u, 138u, 136u, 134u, 132u, 130u, 128u, 126u, 123u, 121u, 119u, 117u, 115u, 114u, 112u, 110u, 108u, 106u, 104u, 102u, 100u, 98u, 96u, 94u, 93u, 91u, 89u, 87u, 85u, 83u, 82u, 80u, 78u, 76u, 74u, 73u, 71u, 69u, 67u, 66u, 64u, 62u, 61u, 59u, 57u, 55u, 54u, 52u, 50u, 49u, 47u, 46u, 44u, 42u, 41u, 39u, 37u, 36u, 34u, 33u, 31u, 30u, 28u, 26u, 25u, 23u, 22u, 20u, 19u, 17u, 16u, 14u, 13u, 11u, 10u, 8u, 7u, 5u, 4u, 2u, 1u }); private unsafe static readonly ZSTD_seqSymbol* LL_defaultDTable = UnsafeHelper.GetArrayPointer(new ZSTD_seqSymbol[65] { new ZSTD_seqSymbol(1, 1, 1, 6u), new ZSTD_seqSymbol(0, 0, 4, 0u), new ZSTD_seqSymbol(16, 0, 4, 0u), new ZSTD_seqSymbol(32, 0, 5, 1u), new ZSTD_seqSymbol(0, 0, 5, 3u), new ZSTD_seqSymbol(0, 0, 5, 4u), new ZSTD_seqSymbol(0, 0, 5, 6u), new ZSTD_seqSymbol(0, 0, 5, 7u), new ZSTD_seqSymbol(0, 0, 5, 9u), new ZSTD_seqSymbol(0, 0, 5, 10u), new ZSTD_seqSymbol(0, 0, 5, 12u), new ZSTD_seqSymbol(0, 0, 6, 14u), new ZSTD_seqSymbol(0, 1, 5, 16u), new ZSTD_seqSymbol(0, 1, 5, 20u), new ZSTD_seqSymbol(0, 1, 5, 22u), new ZSTD_seqSymbol(0, 2, 5, 28u), new ZSTD_seqSymbol(0, 3, 5, 32u), new ZSTD_seqSymbol(0, 4, 5, 48u), new ZSTD_seqSymbol(32, 6, 5, 64u), new ZSTD_seqSymbol(0, 7, 5, 128u), new ZSTD_seqSymbol(0, 8, 6, 256u), new ZSTD_seqSymbol(0, 10, 6, 1024u), new ZSTD_seqSymbol(0, 12, 6, 4096u), new ZSTD_seqSymbol(32, 0, 4, 0u), new ZSTD_seqSymbol(0, 0, 4, 1u), new ZSTD_seqSymbol(0, 0, 5, 2u), new ZSTD_seqSymbol(32, 0, 5, 4u), new ZSTD_seqSymbol(0, 0, 5, 5u), new ZSTD_seqSymbol(32, 0, 5, 7u), new ZSTD_seqSymbol(0, 0, 5, 8u), new ZSTD_seqSymbol(32, 0, 5, 10u), new ZSTD_seqSymbol(0, 0, 5, 11u), new ZSTD_seqSymbol(0, 0, 6, 13u), new ZSTD_seqSymbol(32, 1, 5, 16u), new ZSTD_seqSymbol(0, 1, 5, 18u), new ZSTD_seqSymbol(32, 1, 5, 22u), new ZSTD_seqSymbol(0, 2, 5, 24u), new ZSTD_seqSymbol(32, 3, 5, 32u), new ZSTD_seqSymbol(0, 3, 5, 40u), new ZSTD_seqSymbol(0, 6, 4, 64u), new ZSTD_seqSymbol(16, 6, 4, 64u), new ZSTD_seqSymbol(32, 7, 5, 128u), new ZSTD_seqSymbol(0, 9, 6, 512u), new ZSTD_seqSymbol(0, 11, 6, 2048u), new ZSTD_seqSymbol(48, 0, 4, 0u), new ZSTD_seqSymbol(16, 0, 4, 1u), new ZSTD_seqSymbol(32, 0, 5, 2u), new ZSTD_seqSymbol(32, 0, 5, 3u), new ZSTD_seqSymbol(32, 0, 5, 5u), new ZSTD_seqSymbol(32, 0, 5, 6u), new ZSTD_seqSymbol(32, 0, 5, 8u), new ZSTD_seqSymbol(32, 0, 5, 9u), new ZSTD_seqSymbol(32, 0, 5, 11u), new ZSTD_seqSymbol(32, 0, 5, 12u), new ZSTD_seqSymbol(0, 0, 6, 15u), new ZSTD_seqSymbol(32, 1, 5, 18u), new ZSTD_seqSymbol(32, 1, 5, 20u), new ZSTD_seqSymbol(32, 2, 5, 24u), new ZSTD_seqSymbol(32, 2, 5, 28u), new ZSTD_seqSymbol(32, 3, 5, 40u), new ZSTD_seqSymbol(32, 4, 5, 48u), new ZSTD_seqSymbol(0, 16, 6, 65536u), new ZSTD_seqSymbol(0, 15, 6, 32768u), new ZSTD_seqSymbol(0, 14, 6, 16384u), new ZSTD_seqSymbol(0, 13, 6, 8192u) }); private unsafe static readonly ZSTD_seqSymbol* OF_defaultDTable = UnsafeHelper.GetArrayPointer(new ZSTD_seqSymbol[33] { new ZSTD_seqSymbol(1, 1, 1, 5u), new ZSTD_seqSymbol(0, 0, 5, 0u), new ZSTD_seqSymbol(0, 6, 4, 61u), new ZSTD_seqSymbol(0, 9, 5, 509u), new ZSTD_seqSymbol(0, 15, 5, 32765u), new ZSTD_seqSymbol(0, 21, 5, 2097149u), new ZSTD_seqSymbol(0, 3, 5, 5u), new ZSTD_seqSymbol(0, 7, 4, 125u), new ZSTD_seqSymbol(0, 12, 5, 4093u), new ZSTD_seqSymbol(0, 18, 5, 262141u), new ZSTD_seqSymbol(0, 23, 5, 8388605u), new ZSTD_seqSymbol(0, 5, 5, 29u), new ZSTD_seqSymbol(0, 8, 4, 253u), new ZSTD_seqSymbol(0, 14, 5, 16381u), new ZSTD_seqSymbol(0, 20, 5, 1048573u), new ZSTD_seqSymbol(0, 2, 5, 1u), new ZSTD_seqSymbol(16, 7, 4, 125u), new ZSTD_seqSymbol(0, 11, 5, 2045u), new ZSTD_seqSymbol(0, 17, 5, 131069u), new ZSTD_seqSymbol(0, 22, 5, 4194301u), new ZSTD_seqSymbol(0, 4, 5, 13u), new ZSTD_seqSymbol(16, 8, 4, 253u), new ZSTD_seqSymbol(0, 13, 5, 8189u), new ZSTD_seqSymbol(0, 19, 5, 524285u), new ZSTD_seqSymbol(0, 1, 5, 1u), new ZSTD_seqSymbol(16, 6, 4, 61u), new ZSTD_seqSymbol(0, 10, 5, 1021u), new ZSTD_seqSymbol(0, 16, 5, 65533u), new ZSTD_seqSymbol(0, 28, 5, 268435453u), new ZSTD_seqSymbol(0, 27, 5, 134217725u), new ZSTD_seqSymbol(0, 26, 5, 67108861u), new ZSTD_seqSymbol(0, 25, 5, 33554429u), new ZSTD_seqSymbol(0, 24, 5, 16777213u) }); private unsafe static readonly ZSTD_seqSymbol* ML_defaultDTable = UnsafeHelper.GetArrayPointer(new ZSTD_seqSymbol[65] { new ZSTD_seqSymbol(1, 1, 1, 6u), new ZSTD_seqSymbol(0, 0, 6, 3u), new ZSTD_seqSymbol(0, 0, 4, 4u), new ZSTD_seqSymbol(32, 0, 5, 5u), new ZSTD_seqSymbol(0, 0, 5, 6u), new ZSTD_seqSymbol(0, 0, 5, 8u), new ZSTD_seqSymbol(0, 0, 5, 9u), new ZSTD_seqSymbol(0, 0, 5, 11u), new ZSTD_seqSymbol(0, 0, 6, 13u), new ZSTD_seqSymbol(0, 0, 6, 16u), new ZSTD_seqSymbol(0, 0, 6, 19u), new ZSTD_seqSymbol(0, 0, 6, 22u), new ZSTD_seqSymbol(0, 0, 6, 25u), new ZSTD_seqSymbol(0, 0, 6, 28u), new ZSTD_seqSymbol(0, 0, 6, 31u), new ZSTD_seqSymbol(0, 0, 6, 34u), new ZSTD_seqSymbol(0, 1, 6, 37u), new ZSTD_seqSymbol(0, 1, 6, 41u), new ZSTD_seqSymbol(0, 2, 6, 47u), new ZSTD_seqSymbol(0, 3, 6, 59u), new ZSTD_seqSymbol(0, 4, 6, 83u), new ZSTD_seqSymbol(0, 7, 6, 131u), new ZSTD_seqSymbol(0, 9, 6, 515u), new ZSTD_seqSymbol(16, 0, 4, 4u), new ZSTD_seqSymbol(0, 0, 4, 5u), new ZSTD_seqSymbol(32, 0, 5, 6u), new ZSTD_seqSymbol(0, 0, 5, 7u), new ZSTD_seqSymbol(32, 0, 5, 9u), new ZSTD_seqSymbol(0, 0, 5, 10u), new ZSTD_seqSymbol(0, 0, 6, 12u), new ZSTD_seqSymbol(0, 0, 6, 15u), new ZSTD_seqSymbol(0, 0, 6, 18u), new ZSTD_seqSymbol(0, 0, 6, 21u), new ZSTD_seqSymbol(0, 0, 6, 24u), new ZSTD_seqSymbol(0, 0, 6, 27u), new ZSTD_seqSymbol(0, 0, 6, 30u), new ZSTD_seqSymbol(0, 0, 6, 33u), new ZSTD_seqSymbol(0, 1, 6, 35u), new ZSTD_seqSymbol(0, 1, 6, 39u), new ZSTD_seqSymbol(0, 2, 6, 43u), new ZSTD_seqSymbol(0, 3, 6, 51u), new ZSTD_seqSymbol(0, 4, 6, 67u), new ZSTD_seqSymbol(0, 5, 6, 99u), new ZSTD_seqSymbol(0, 8, 6, 259u), new ZSTD_seqSymbol(32, 0, 4, 4u), new ZSTD_seqSymbol(48, 0, 4, 4u), new ZSTD_seqSymbol(16, 0, 4, 5u), new ZSTD_seqSymbol(32, 0, 5, 7u), new ZSTD_seqSymbol(32, 0, 5, 8u), new ZSTD_seqSymbol(32, 0, 5, 10u), new ZSTD_seqSymbol(32, 0, 5, 11u), new ZSTD_seqSymbol(0, 0, 6, 14u), new ZSTD_seqSymbol(0, 0, 6, 17u), new ZSTD_seqSymbol(0, 0, 6, 20u), new ZSTD_seqSymbol(0, 0, 6, 23u), new ZSTD_seqSymbol(0, 0, 6, 26u), new ZSTD_seqSymbol(0, 0, 6, 29u), new ZSTD_seqSymbol(0, 0, 6, 32u), new ZSTD_seqSymbol(0, 16, 6, 65539u), new ZSTD_seqSymbol(0, 15, 6, 32771u), new ZSTD_seqSymbol(0, 14, 6, 16387u), new ZSTD_seqSymbol(0, 13, 6, 8195u), new ZSTD_seqSymbol(0, 12, 6, 4099u), new ZSTD_seqSymbol(0, 11, 6, 2051u), new ZSTD_seqSymbol(0, 10, 6, 1027u) }); private unsafe static readonly uint* dec32table = UnsafeHelper.GetArrayPointer(new uint[8] { 0u, 1u, 2u, 1u, 4u, 4u, 4u, 4u }); private unsafe static readonly int* dec64table = UnsafeHelper.GetArrayPointer(new int[8] { 8, 8, 8, 7, 8, 9, 10, 11 }); private unsafe static readonly uint* LL_base = UnsafeHelper.GetArrayPointer(new uint[36] { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u, 16u, 18u, 20u, 22u, 24u, 28u, 32u, 40u, 48u, 64u, 128u, 256u, 512u, 1024u, 2048u, 4096u, 8192u, 16384u, 32768u, 65536u }); private unsafe static readonly uint* OF_base = UnsafeHelper.GetArrayPointer(new uint[32] { 0u, 1u, 1u, 5u, 13u, 29u, 61u, 125u, 253u, 509u, 1021u, 2045u, 4093u, 8189u, 16381u, 32765u, 65533u, 131069u, 262141u, 524285u, 1048573u, 2097149u, 4194301u, 8388605u, 16777213u, 33554429u, 67108861u, 134217725u, 268435453u, 536870909u, 1073741821u, 2147483645u }); private unsafe static readonly byte* OF_bits = UnsafeHelper.GetArrayPointer(new byte[32] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }); private unsafe static readonly uint* ML_base = UnsafeHelper.GetArrayPointer(new uint[53] { 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u, 16u, 17u, 18u, 19u, 20u, 21u, 22u, 23u, 24u, 25u, 26u, 27u, 28u, 29u, 30u, 31u, 32u, 33u, 34u, 35u, 37u, 39u, 41u, 43u, 47u, 51u, 59u, 67u, 83u, 99u, 131u, 259u, 515u, 1027u, 2051u, 4099u, 8195u, 16387u, 32771u, 65539u }); private unsafe static readonly byte* dummy = UnsafeHelper.GetArrayPointer(new byte[10] { 18, 52, 86, 120, 154, 188, 222, 240, 226, 180 }); private unsafe static readonly uint* repStartValue = UnsafeHelper.GetArrayPointer(new uint[3] { 1u, 4u, 8u }); private unsafe static readonly nuint* ZSTD_fcs_fieldSize = UnsafeHelper.GetArrayPointer(new UIntPtr[4] { default(UIntPtr), (nuint)2u, (nuint)4u, (nuint)8u }); private unsafe static readonly nuint* ZSTD_did_fieldSize = UnsafeHelper.GetArrayPointer(new UIntPtr[4] { default(UIntPtr), (nuint)1u, (nuint)2u, (nuint)4u }); private const uint ZSTD_blockHeaderSize = 3u; private unsafe static readonly byte* LL_bits = UnsafeHelper.GetArrayPointer(new byte[36] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }); private unsafe static readonly short* LL_defaultNorm = UnsafeHelper.GetArrayPointer(new short[36] { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1, -1, -1, -1, -1 }); private const uint LL_defaultNormLog = 6u; private unsafe static readonly byte* ML_bits = UnsafeHelper.GetArrayPointer(new byte[53] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }); private unsafe static readonly short* ML_defaultNorm = UnsafeHelper.GetArrayPointer(new short[53] { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1 }); private const uint ML_defaultNormLog = 6u; private unsafe static readonly short* OF_defaultNorm = UnsafeHelper.GetArrayPointer(new short[29] { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1 }); private const uint OF_defaultNormLog = 5u; private unsafe static readonly ulong* ZSTD_ldm_gearTab = UnsafeHelper.GetArrayPointer(new ulong[256] { 17706173704959391580uL, 9553083853199557650uL, 13134657783006956748uL, 12710771454410469854uL, 3771850993348288464uL, 5663242980927105797uL, 4180421565056919044uL, 14901571295287444559uL, 8565414132019635614uL, 11278465880384465610uL, 9679984951781061929uL, 2955002781598341257uL, 12660509806464199654uL, 14854864823388464697uL, 152752410645948062uL, 980088017434152uL, 331732851626658549uL, 9608103326924505408uL, 357946512872856607uL, 9412255165462690336uL, 5763983574984628126uL, 9544216146408746779uL, 5836814799640872626uL, 7626666828866112607uL, 10132336208249967804uL, 14108670375684292611uL, 15018902349139243921uL, 13711462949325910062uL, 3783546168938607943uL, 12449064811086598672uL, 7335087386897913548uL, 10282405655244907485uL, 14378187169578456254uL, 10614961078272045355uL, 6793784568496124290uL, 2174722154453825146uL, 2984138818416736892uL, 11333002331483257390uL, 16165308564874101291uL, 10725110673850565536uL, 14803350382165318147uL, 4998707500772197108uL, 9699827029151170913uL, 4094234931541430821uL, 13003324167594699195uL, 11823133507021527537uL, 8857814660316604090uL, 15046005479562027794uL, 9526922408351028488uL, 12163825929456261054uL, 2432827404989958913uL, 11052631833613140206uL, 12325482593596012379uL, 5984878076832140732uL, 3384046157297573234uL, 13851498048714724614uL, 5291253059634137447uL, 8671685973221512352uL, 2826049182810801527uL, 7403797093788988755uL, 5165094797103512198uL, 3882952073790264921uL, 5144544719191608175uL, 7834774775704163346uL, 15973311502237377217uL, 8174605543888266482uL, 16091365636059134007uL, 9854089133047561481uL, 6401049270349998237uL, 1690663705589720305uL, 18120752767104018391uL, 3162055811205276826uL, 7360403718927882347uL, 4149314189569060432uL, 242631106765699956uL, 4800188873694292702uL, 14206607945176518665uL, 7695287510285100531uL, 17608552626885984827uL, 10020125276345778935uL, 4318197351340892641uL, 1149634524221210890uL, 11331880614614651495uL, 6598639596032476539uL, 11607104452118320194uL, 13978664290031320395uL, 7415306797865260392uL, 10058836985568751709uL, 17188118796833258423uL, 9971713741725809450uL, 13924449053774748311uL, 7907118757495767932uL, 14377196586253082865uL, 15530242928011163547uL, 17733679075010653113uL, 3241772581917522649uL, 9695224596908887329uL, 2730958776012430807uL, 13403885621236429923uL, 5670495071248203466uL, 13693717966825575882uL, 8596907927398544058uL, 5161444109349651405uL, 5785732034315264735uL, 16139739696250318521uL, 5101781436946662964uL, 17487188175106185411uL, 3854890476374695265uL, 2640596163920942051uL, 612531917322106041uL, 2943830675960290284uL, 5689027370104560298uL, 9815517349669543859uL, 13935535009091872214uL, 13552274704568119482uL, 476493605933034202uL, 13562513904193093803uL, 5170495216195818230uL, 5692683294927132859uL, 8144723081244076501uL, 9849224207461918046uL, 17565804824255640984uL, 14017671640842713945uL, 9352397318021489661uL, 9860731225854076747uL, 938274653391260135uL, 16527147869881094957uL, 13761681345554596006uL, 10856826376889166028uL, 13802067315703888081uL, 7411488509093422598uL, 9456257496762226683uL, 13293525432248379492uL, 15813944191857944220uL, 9900494752746575806uL, 14473071140862552588uL, 8549157441122500270uL, 11847229568553546514uL, 12317404726061951340uL, 5601198583872550840uL, 448237196439497921uL, 4429298985553249414uL, 1710925370934130233uL, 649627535685727708uL, 7473286985276266860uL, 11995055668354384625uL, 5916903098106068381uL, 4015692238528745986uL, 9576145023220382545uL, 723755141375996997uL, 585701537146942606uL, 1762095187648923356uL, 10947668810390298908uL, 12912071967546032810uL, 2150778153372540978uL, 11895358708351842773uL, 2851098796233701843uL, 8892087314106251931uL, 8629034505480888229uL, 16390722640229213946uL, 12942376497313048393uL, 10610151180751073064uL, 12647351261234793460uL, 1034405184557968969uL, 6802162732334632298uL, 2989737911373486958uL, 9668576337280186790uL, 16611283449915776681uL, 10960726892268803442uL, 18017126121870916597uL, 3508842189232906005uL, 8135704789603646339uL, 6514020828226693353uL, 14833548559235791599uL, 15476569850396903830uL, 16883495733613697955uL, 6024843996518658034uL, 718621336730405080uL, 11767441106490307461uL, 3328034870986208117uL, 1672684744619306689uL, 13284085454593942393uL, 9476198217633794787uL, 13335035470489323885uL, 8965210182149532773uL, 8555535473824159125uL, 4111651333356154152uL, 14115977012535548530uL, 14207181237838828425uL, 15532545149069149488uL, 3999682325075695998uL, 8341500896009323972uL, 297956889220308695uL, 11347451338840881425uL, 10294383061129258838uL, 6806741856855634849uL, 5498675928299174173uL, 12721473787806600668uL, 8841901128597221836uL, 8452800421686628345uL, 8170466794247151429uL, 3418145686321779422uL, 10817094637055541623uL, 17560763667880801002uL, 17167128933564300955uL, 14801159701709100519uL, 16487363115335837231uL, 11044889568322808004uL, 2726709086420568843uL, 56600323866146812uL, 2432711764059673306uL, 6875692401370490017uL, 15921125343301818124uL, 7150469517113480430uL, 13922559255157977930uL, 6398633964089323354uL, 6824119869706748626uL, 7124491435314367896uL, 13033615390568402455uL, 1648863654502257127uL, 10287225845535084223uL, 7114119705011199411uL, 14698048236964821526uL, 8708228644950752452uL, 17245774881097531892uL, 10193195248059742541uL, 12228736673625442821uL, 4090442791415205777uL, 7208173716382451811uL, 9289858114882807144uL, 4175721917217417792uL, 16850760200582287999uL, 10530818929465860628uL, 13455067593928767297uL, 3502286560157455023uL, 11914541010847425042uL, 4777171913562308143uL, 4711368926514029619uL, 13298464311250560920uL, 15170220567670630564uL, 11116125771979599555uL, 8867750111338937128uL, 5817503254400830125uL, 7843308076737921667uL, 9906686027218562035uL, 2422652698756804679uL, 4252262198383740502uL, 15787297651497342695uL, 10148014497693279666uL, 18064062372941021341uL, 4672201502568865753uL, 12625153173770565918uL, 16973238125497040472uL, 13532142460446213504uL, 934305549654010667uL, 3120327478150945012uL }); private static readonly buffer_s g_nullBuffer = new buffer_s(null, 0u); private static readonly Range kNullRange = new Range(null, 0u); private static readonly RoundBuff_t kNullRoundBuff = new RoundBuff_t(null, 0u, 0u); private unsafe static readonly uint* baseLLfreqs = UnsafeHelper.GetArrayPointer(new uint[36] { 4u, 2u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u }); private unsafe static readonly uint* baseOFCfreqs = UnsafeHelper.GetArrayPointer(new uint[32] { 6u, 2u, 1u, 1u, 2u, 3u, 4u, 4u, 4u, 3u, 2u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u }); private unsafe static readonly ZSTD_getAllMatchesFn[][] getAllMatchesFns = new ZSTD_getAllMatchesFn[3][] { new ZSTD_getAllMatchesFn[4] { ZSTD_btGetAllMatches_noDict_3, ZSTD_btGetAllMatches_noDict_4, ZSTD_btGetAllMatches_noDict_5, ZSTD_btGetAllMatches_noDict_6 }, new ZSTD_getAllMatchesFn[4] { ZSTD_btGetAllMatches_extDict_3, ZSTD_btGetAllMatches_extDict_4, ZSTD_btGetAllMatches_extDict_5, ZSTD_btGetAllMatches_extDict_6 }, new ZSTD_getAllMatchesFn[4] { ZSTD_btGetAllMatches_dictMatchState_3, ZSTD_btGetAllMatches_dictMatchState_4, ZSTD_btGetAllMatches_dictMatchState_5, ZSTD_btGetAllMatches_dictMatchState_6 } }; private unsafe static readonly void*[] records_fs = new void*[4] { (delegate*<Fingerprint*, void*, nuint, void>)(&ZSTD_recordFingerprint_43), (delegate*<Fingerprint*, void*, nuint, void>)(&ZSTD_recordFingerprint_11), (delegate*<Fingerprint*, void*, nuint, void>)(&ZSTD_recordFingerprint_5), (delegate*<Fingerprint*, void*, nuint, void>)(&ZSTD_recordFingerprint_1) }; private unsafe static readonly uint* hashParams = UnsafeHelper.GetArrayPointer(new uint[4] { 8u, 9u, 10u, 10u }); [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static void* ZSTD_customMalloc(nuint size, ZSTD_customMem customMem) { if (customMem.customAlloc != null) { return ((delegate*<void*, UIntPtr, void*>)customMem.customAlloc)(customMem.opaque, size); } return UnsafeHelper.malloc(size); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static void* ZSTD_customCalloc(nuint size, ZSTD_customMem customMem) { if (customMem.customAlloc != null) { void* intPtr = ((delegate*<void*, UIntPtr, void*>)customMem.customAlloc)(customMem.opaque, size); System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(intPtr, 0, (uint)size); return intPtr; } return UnsafeHelper.calloc(1uL, size); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static void ZSTD_customFree(void* ptr, ZSTD_customMem customMem) { if (ptr != null) { if (customMem.customFree != null) { ((delegate*<void*, void*, void>)customMem.customFree)(customMem.opaque, ptr); } else { UnsafeHelper.free(ptr); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ZSTD_countTrailingZeros32(uint val) { return (uint)BitOperations.TrailingZeroCount(val); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ZSTD_countLeadingZeros32(uint val) { return (uint)BitOperations.LeadingZeroCount(val); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ZSTD_countTrailingZeros64(ulong val) { return (uint)BitOperations.TrailingZeroCount(val); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ZSTD_countLeadingZeros64(ulong val) { return (uint)BitOperations.LeadingZeroCount(val); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_initCStream(ref BIT_CStream_t bitC, void* startPtr, nuint dstCapacity) { bitC.bitContainer = 0u; bitC.bitPos = 0u; bitC.startPtr = (sbyte*)startPtr; bitC.ptr = bitC.startPtr; bitC.endPtr = bitC.startPtr + dstCapacity - sizeof(UIntPtr); if (dstCapacity <= (nuint)sizeof(UIntPtr)) { return unchecked((nuint)(-70)); } return 0u; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_getLowerBits(nuint bitContainer, uint nbBits) { return bitContainer & BIT_mask[nbBits]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void BIT_addBits(ref nuint bitC_bitContainer, ref uint bitC_bitPos, nuint value, uint nbBits) { bitC_bitContainer |= BIT_getLowerBits(value, nbBits) << (int)bitC_bitPos; bitC_bitPos += nbBits; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void BIT_addBitsFast(ref nuint bitC_bitContainer, ref uint bitC_bitPos, nuint value, uint nbBits) { bitC_bitContainer |= value << (int)bitC_bitPos; bitC_bitPos += nbBits; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static void BIT_flushBitsFast(ref nuint bitC_bitContainer, ref uint bitC_bitPos, ref sbyte* bitC_ptr, sbyte* bitC_endPtr) { nuint num = bitC_bitPos >> 3; MEM_writeLEST(bitC_ptr, bitC_bitContainer); bitC_ptr += num; bitC_bitPos &= 7u; bitC_bitContainer >>= (int)(num * 8); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static void BIT_flushBits(ref nuint bitC_bitContainer, ref uint bitC_bitPos, ref sbyte* bitC_ptr, sbyte* bitC_endPtr) { nuint num = bitC_bitPos >> 3; MEM_writeLEST(bitC_ptr, bitC_bitContainer); bitC_ptr += num; if (bitC_ptr > bitC_endPtr) { bitC_ptr = bitC_endPtr; } bitC_bitPos &= 7u; bitC_bitContainer >>= (int)(num * 8); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_closeCStream(ref nuint bitC_bitContainer, ref uint bitC_bitPos, sbyte* bitC_ptr, sbyte* bitC_endPtr, sbyte* bitC_startPtr) { BIT_addBitsFast(ref bitC_bitContainer, ref bitC_bitPos, 1u, 1u); BIT_flushBits(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); if (bitC_ptr >= bitC_endPtr) { return 0u; } return (nuint)((nint)(bitC_ptr - bitC_startPtr) + ((bitC_bitPos != 0) ? 1 : 0)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_initDStream(BIT_DStream_t* bitD, void* srcBuffer, nuint srcSize) { if (srcSize < 1) { *bitD = default(BIT_DStream_t); return unchecked((nuint)(-72)); } bitD->start = (sbyte*)srcBuffer; bitD->limitPtr = bitD->start + sizeof(UIntPtr); if (srcSize >= (nuint)sizeof(UIntPtr)) { bitD->ptr = (sbyte*)((byte*)srcBuffer + srcSize - sizeof(UIntPtr)); bitD->bitContainer = MEM_readLEST(bitD->ptr); byte b = ((byte*)srcBuffer)[srcSize - 1]; bitD->bitsConsumed = ((b != 0) ? ((uint)(8 - BitOperations.Log2(b))) : 0u); if (b == 0) { return unchecked((nuint)(-1)); } } else { bitD->ptr = bitD->start; bitD->bitContainer = (uint)(*bitD->start); ulong num = srcSize; ulong num2 = num - 2; if (num2 <= 5) { switch (num2) { case 5uL: bitD->bitContainer += (nuint)((byte*)srcBuffer)[6] << sizeof(UIntPtr) * 8 - 16; goto case 4uL; case 4uL: bitD->bitContainer += (nuint)((byte*)srcBuffer)[5] << sizeof(UIntPtr) * 8 - 24; goto case 3uL; case 3uL: bitD->bitContainer += (nuint)((byte*)srcBuffer)[4] << sizeof(UIntPtr) * 8 - 32; goto case 2uL; case 2uL: bitD->bitContainer += (nuint)((byte*)srcBuffer)[3] << 24; goto case 1uL; case 1uL: bitD->bitContainer += (nuint)((byte*)srcBuffer)[2] << 16; goto case 0uL; case 0uL: bitD->bitContainer += (nuint)((byte*)srcBuffer)[1] << 8; break; } } byte b2 = ((byte*)srcBuffer)[srcSize - 1]; bitD->bitsConsumed = ((b2 != 0) ? ((uint)(8 - BitOperations.Log2(b2))) : 0u); if (b2 == 0) { return unchecked((nuint)(-20)); } bitD->bitsConsumed += (uint)((int)((nint)sizeof(UIntPtr) - (nint)srcSize) * 8); } return srcSize; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint BIT_getUpperBits(nuint bitContainer, uint start) { return bitContainer >> (int)start; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_getMiddleBits(nuint bitContainer, uint start, uint nbBits) { uint num = (uint)(sizeof(UIntPtr) * 8 - 1); return (nuint)((ulong)(bitContainer >> (int)(start & num)) & (ulong)((1L << (int)nbBits) - 1)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_lookBits(BIT_DStream_t* bitD, uint nbBits) { return BIT_getMiddleBits(bitD->bitContainer, (uint)(sizeof(UIntPtr) * 8 - (int)bitD->bitsConsumed) - nbBits, nbBits); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_readBits(BIT_DStream_t* bitD, uint nbBits) { nuint result = BIT_lookBits(bitD, nbBits); bitD->bitsConsumed += nbBits; return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_readBitsFast(BIT_DStream_t* bitD, uint nbBits) { uint num = (uint)(sizeof(UIntPtr) * 8 - 1); nuint result = bitD->bitContainer << (int)(bitD->bitsConsumed & num) >> (int)((num + 1 - nbBits) & num); bitD->bitsConsumed += nbBits; return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static BIT_DStream_status BIT_reloadDStream_internal(BIT_DStream_t* bitD) { bitD->ptr -= bitD->bitsConsumed >> 3; bitD->bitsConsumed &= 7u; bitD->bitContainer = MEM_readLEST(bitD->ptr); return BIT_DStream_status.BIT_DStream_unfinished; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) { if (bitD->bitsConsumed > (uint)(sizeof(UIntPtr) * 8)) { bitD->ptr = (sbyte*)static_zeroFilled; return BIT_DStream_status.BIT_DStream_overflow; } if (bitD->ptr >= bitD->limitPtr) { return BIT_reloadDStream_internal(bitD); } if (bitD->ptr == bitD->start) { if (bitD->bitsConsumed < (uint)(sizeof(UIntPtr) * 8)) { return BIT_DStream_status.BIT_DStream_endOfBuffer; } return BIT_DStream_status.BIT_DStream_completed; } uint num = bitD->bitsConsumed >> 3; BIT_DStream_status result = BIT_DStream_status.BIT_DStream_unfinished; if (bitD->ptr - num < bitD->start) { num = (uint)(bitD->ptr - bitD->start); result = BIT_DStream_status.BIT_DStream_endOfBuffer; } bitD->ptr -= num; bitD->bitsConsumed -= num * 8; bitD->bitContainer = MEM_readLEST(bitD->ptr); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static uint BIT_endOfDStream(BIT_DStream_t* DStream) { if (DStream->ptr != DStream->start || DStream->bitsConsumed != (uint)(sizeof(UIntPtr) * 8)) { return 0u; } return 1u; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_initDStream(ref BIT_DStream_t bitD, void* srcBuffer, nuint srcSize) { if (srcSize < 1) { bitD = default(BIT_DStream_t); return unchecked((nuint)(-72)); } bitD.start = (sbyte*)srcBuffer; bitD.limitPtr = bitD.start + sizeof(UIntPtr); if (srcSize >= (nuint)sizeof(UIntPtr)) { bitD.ptr = (sbyte*)((byte*)srcBuffer + srcSize - sizeof(UIntPtr)); bitD.bitContainer = MEM_readLEST(bitD.ptr); byte b = ((byte*)srcBuffer)[srcSize - 1]; bitD.bitsConsumed = ((b != 0) ? ((uint)(8 - BitOperations.Log2(b))) : 0u); if (b == 0) { return unchecked((nuint)(-1)); } } else { bitD.ptr = bitD.start; bitD.bitContainer = (uint)(*bitD.start); ulong num = srcSize; ulong num2 = num - 2; if (num2 <= 5) { switch (num2) { case 5uL: bitD.bitContainer += (nuint)((byte*)srcBuffer)[6] << sizeof(UIntPtr) * 8 - 16; goto case 4uL; case 4uL: bitD.bitContainer += (nuint)((byte*)srcBuffer)[5] << sizeof(UIntPtr) * 8 - 24; goto case 3uL; case 3uL: bitD.bitContainer += (nuint)((byte*)srcBuffer)[4] << sizeof(UIntPtr) * 8 - 32; goto case 2uL; case 2uL: bitD.bitContainer += (nuint)((byte*)srcBuffer)[3] << 24; goto case 1uL; case 1uL: bitD.bitContainer += (nuint)((byte*)srcBuffer)[2] << 16; goto case 0uL; case 0uL: bitD.bitContainer += (nuint)((byte*)srcBuffer)[1] << 8; break; } } byte b2 = ((byte*)srcBuffer)[srcSize - 1]; bitD.bitsConsumed = ((b2 != 0) ? ((uint)(8 - BitOperations.Log2(b2))) : 0u); if (b2 == 0) { return unchecked((nuint)(-20)); } bitD.bitsConsumed += (uint)((int)((nint)sizeof(UIntPtr) - (nint)srcSize) * 8); } return srcSize; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_lookBits(nuint bitD_bitContainer, uint bitD_bitsConsumed, uint nbBits) { return BIT_getMiddleBits(bitD_bitContainer, (uint)(sizeof(UIntPtr) * 8 - (int)bitD_bitsConsumed) - nbBits, nbBits); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint BIT_readBits(nuint bitD_bitContainer, ref uint bitD_bitsConsumed, uint nbBits) { nuint result = BIT_lookBits(bitD_bitContainer, bitD_bitsConsumed, nbBits); bitD_bitsConsumed += nbBits; return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint BIT_readBitsFast(nuint bitD_bitContainer, ref uint bitD_bitsConsumed, uint nbBits) { uint num = bitD_bitsConsumed; uint num2 = (uint)(sizeof(UIntPtr) * 8 - 1); nuint result = bitD_bitContainer << (int)(num & num2) >> (int)((num2 + 1 - nbBits) & num2); bitD_bitsConsumed += nbBits; return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static BIT_DStream_status BIT_reloadDStream(ref nuint bitD_bitContainer, ref uint bitD_bitsConsumed, ref sbyte* bitD_ptr, sbyte* bitD_start, sbyte* bitD_limitPtr) { if (bitD_bitsConsumed > (uint)(sizeof(UIntPtr) * 8)) { bitD_ptr = (sbyte*)static_zeroFilled; return BIT_DStream_status.BIT_DStream_overflow; } if (bitD_ptr >= bitD_limitPtr) { return BIT_reloadDStream_internal(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start); } if (bitD_ptr == bitD_start) { if (bitD_bitsConsumed < (uint)(sizeof(UIntPtr) * 8)) { return BIT_DStream_status.BIT_DStream_endOfBuffer; } return BIT_DStream_status.BIT_DStream_completed; } uint num = bitD_bitsConsumed >> 3; BIT_DStream_status result = BIT_DStream_status.BIT_DStream_unfinished; if (bitD_ptr - num < bitD_start) { num = (uint)(bitD_ptr - bitD_start); result = BIT_DStream_status.BIT_DStream_endOfBuffer; } bitD_ptr -= num; bitD_bitsConsumed -= num * 8; bitD_bitContainer = MEM_readLEST(bitD_ptr); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static BIT_DStream_status BIT_reloadDStream_internal(ref nuint bitD_bitContainer, ref uint bitD_bitsConsumed, ref sbyte* bitD_ptr, sbyte* bitD_start) { bitD_ptr -= bitD_bitsConsumed >> 3; bitD_bitsConsumed &= 7u; bitD_bitContainer = MEM_readLEST(bitD_ptr); return BIT_DStream_status.BIT_DStream_unfinished; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static uint BIT_endOfDStream(uint DStream_bitsConsumed, sbyte* DStream_ptr, sbyte* DStream_start) { if (DStream_ptr != DStream_start || DStream_bitsConsumed != (uint)(sizeof(UIntPtr) * 8)) { return 0u; } return 1u; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int ZSTD_isPower2(nuint u) { return ((u & (u - 1)) == 0) ? 1 : 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nint ZSTD_wrappedPtrDiff(byte* lhs, byte* rhs) { return (nint)(lhs - rhs); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static byte* ZSTD_wrappedPtrAdd(byte* ptr, nint add) { return ptr + add; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static byte* ZSTD_wrappedPtrSub(byte* ptr, nint sub) { return ptr - sub; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static byte* ZSTD_maybeNullPtrAdd(byte* ptr, nint add) { if (add <= 0) { return ptr; } return ptr + add; } private unsafe static nuint COVER_sum(nuint* samplesSizes, uint nbSamples) { nuint num = 0u; for (uint num2 = 0u; num2 < nbSamples; num2++) { num += samplesSizes[num2]; } return num; } private static void COVER_warnOnSmallCorpus(nuint maxDictSize, nuint nbDmers, int displayLevel) { _ = (double)nbDmers / (double)maxDictSize; _ = 10.0; } private static COVER_epoch_info_t COVER_computeEpochs(uint maxDictSize, uint nbDmers, uint k, uint passes) { uint num = k * 10; System.Runtime.CompilerServices.Unsafe.SkipInit(out COVER_epoch_info_t result); result.num = ((1 > maxDictSize / k / passes) ? 1u : (maxDictSize / k / passes)); result.size = nbDmers / result.num; if (result.size >= num) { return result; } result.size = ((num < nbDmers) ? num : nbDmers); result.num = nbDmers / result.size; return result; } private unsafe static nuint COVER_checkTotalCompressedSize(ZDICT_cover_params_t parameters, nuint* samplesSizes, byte* samples, nuint* offsets, nuint nbTrainSamples, nuint nbSamples, byte* dict, nuint dictBufferCapacity) { nuint num = unchecked((nuint)(-1)); nuint num2 = 0u; for (nuint num3 = ((parameters.splitPoint < 1.0) ? nbTrainSamples : 0); num3 < nbSamples; num3++) { num2 = ((samplesSizes[num3] > num2) ? samplesSizes[num3] : num2); } nuint num4 = ZSTD_compressBound(num2); void* ptr = UnsafeHelper.malloc(num4); ZSTD_CCtx_s* ptr2 = ZSTD_createCCtx(); ZSTD_CDict_s* ptr3 = ZSTD_createCDict(dict, dictBufferCapacity, parameters.zParams.compressionLevel); if (ptr != null && ptr2 != null && ptr3 != null) { num = dictBufferCapacity; for (nuint num3 = ((parameters.splitPoint < 1.0) ? nbTrainSamples : 0); num3 < nbSamples; num3++) { nuint num5 = ZSTD_compress_usingCDict(ptr2, ptr, num4, samples + offsets[num3], samplesSizes[num3], ptr3); if (ERR_isError(num5)) { num = num5; break; } num += num5; } } ZSTD_freeCCtx(ptr2); ZSTD_freeCDict(ptr3); if (ptr != null) { UnsafeHelper.free(ptr); } return num; } private unsafe static void COVER_best_init(COVER_best_s* best) { if (best != null) { SynchronizationWrapper.Init(&best->mutex); best->liveJobs = 0u; best->dict = null; best->dictSize = 0u; best->compressedSize = unchecked((nuint)(-1)); best->parameters = default(ZDICT_cover_params_t); } } private unsafe static void COVER_best_wait(COVER_best_s* best) { if (best != null) { SynchronizationWrapper.Enter(&best->mutex); while (best->liveJobs != 0) { SynchronizationWrapper.Wait(&best->mutex); } SynchronizationWrapper.Exit(&best->mutex); } } private unsafe static void COVER_best_destroy(COVER_best_s* best) { if (best != null) { COVER_best_wait(best); if (best->dict != null) { UnsafeHelper.free(best->dict); } SynchronizationWrapper.Free(&best->mutex); } } private unsafe static void COVER_best_start(COVER_best_s* best) { if (best != null) { SynchronizationWrapper.Enter(&best->mutex); best->liveJobs++; SynchronizationWrapper.Exit(&best->mutex); } } private unsafe static void COVER_best_finish(COVER_best_s* best, ZDICT_cover_params_t parameters, COVER_dictSelection selection) { void* dictContent = selection.dictContent; nuint totalCompressedSize = selection.totalCompressedSize; nuint dictSize = selection.dictSize; if (best == null) { return; } SynchronizationWrapper.Enter(&best->mutex); best->liveJobs--; nuint liveJobs = best->liveJobs; if (totalCompressedSize < best->compressedSize) { if (best->dict == null || best->dictSize < dictSize) { if (best->dict != null) { UnsafeHelper.free(best->dict); } best->dict = UnsafeHelper.malloc(dictSize); if (best->dict == null) { best->compressedSize = unchecked((nuint)(-1)); best->dictSize = 0u; SynchronizationWrapper.Pulse(&best->mutex); SynchronizationWrapper.Exit(&best->mutex); return; } } if (dictContent != null) { System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(best->dict, dictContent, (uint)dictSize); best->dictSize = dictSize; best->parameters = parameters; best->compressedSize = totalCompressedSize; } } if (liveJobs == 0) { SynchronizationWrapper.PulseAll(&best->mutex); } SynchronizationWrapper.Exit(&best->mutex); } private unsafe static COVER_dictSelection setDictSelection(byte* buf, nuint s, nuint csz) { System.Runtime.CompilerServices.Unsafe.SkipInit(out COVER_dictSelection result); result.dictContent = buf; result.dictSize = s; result.totalCompressedSize = csz; return result; } private unsafe static COVER_dictSelection COVER_dictSelectionError(nuint error) { return setDictSelection(null, 0u, error); } private unsafe static uint COVER_dictSelectionIsError(COVER_dictSelection selection) { if (!ERR_isError(selection.totalCompressedSize) && selection.dictContent != null) { return 0u; } return 1u; } private unsafe static void COVER_dictSelectionFree(COVER_dictSelection selection) { UnsafeHelper.free(selection.dictContent); } private unsafe static COVER_dictSelection COVER_selectDict(byte* customDictContent, nuint dictBufferCapacity, nuint dictContentSize, byte* samplesBuffer, nuint* samplesSizes, uint nbFinalizeSamples, nuint nbCheckSamples, nuint nbSamples, ZDICT_cover_params_t @params, nuint* offsets, nuint totalCompressedSize) { nuint num = 0u; nuint num2 = 0u; byte* ptr = customDictContent + dictContentSize; byte* ptr2 = (byte*)UnsafeHelper.malloc(dictBufferCapacity); byte* ptr3 = (byte*)UnsafeHelper.malloc(dictBufferCapacity); double num3 = (double)@params.shrinkDictMaxRegression / 100.0 + 1.0; if (ptr2 == null || ptr3 == null) { UnsafeHelper.free(ptr2); UnsafeHelper.free(ptr3); return COVER_dictSelectionError(dictContentSize); } System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(ptr2, customDictContent, (uint)dictContentSize); dictContentSize = ZDICT_finalizeDictionary(ptr2, dictBufferCapacity, customDictContent, dictContentSize, samplesBuffer, samplesSizes, nbFinalizeSamples, @params.zParams); if (ZDICT_isError(dictContentSize)) { UnsafeHelper.free(ptr2); UnsafeHelper.free(ptr3); return COVER_dictSelectionError(dictContentSize); } totalCompressedSize = COVER_checkTotalCompressedSize(@params, samplesSizes, samplesBuffer, offsets, nbCheckSamples, nbSamples, ptr2, dictContentSize); if (ERR_isError(totalCompressedSize)) { UnsafeHelper.free(ptr2); UnsafeHelper.free(ptr3); return COVER_dictSelectionError(totalCompressedSize); } if (@params.shrinkDict == 0) { UnsafeHelper.free(ptr3); return setDictSelection(ptr2, dictContentSize, totalCompressedSize); } num = dictContentSize; num2 = totalCompressedSize; for (dictContentSize = 256u; dictContentSize < num; dictContentSize *= 2) { System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(ptr3, ptr2, (uint)num); dictContentSize = ZDICT_finalizeDictionary(ptr3, dictBufferCapacity, ptr - dictContentSize, dictContentSize, samplesBuffer, samplesSizes, nbFinalizeSamples, @params.zParams); if (ZDICT_isError(dictContentSize)) { UnsafeHelper.free(ptr2); UnsafeHelper.free(ptr3); return COVER_dictSelectionError(dictContentSize); } totalCompressedSize = COVER_checkTotalCompressedSize(@params, samplesSizes, samplesBuffer, offsets, nbCheckSamples, nbSamples, ptr3, dictContentSize); if (ERR_isError(totalCompressedSize)) { UnsafeHelper.free(ptr2); UnsafeHelper.free(ptr3); return COVER_dictSelectionError(totalCompressedSize); } if ((double)totalCompressedSize <= (double)num2 * num3) { UnsafeHelper.free(ptr2); return setDictSelection(ptr3, dictContentSize, totalCompressedSize); } } dictContentSize = num; totalCompressedSize = num2; UnsafeHelper.free(ptr3); return setDictSelection(ptr2, dictContentSize, totalCompressedSize); } private static uint FSE_versionNumber() { return 900u; } private static bool FSE_isError(nuint code) { return ERR_isError(code); } private static string FSE_getErrorName(nuint code) { return ERR_getErrorName(code); } private static bool HUF_isError(nuint code) { return ERR_isError(code); } private static string HUF_getErrorName(nuint code) { return ERR_getErrorName(code); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private unsafe static nuint FSE_readNCount_body(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize) { byte* ptr = (byte*)headerBuffer + hbSize; byte* ptr2 = (byte*)headerBuffer; uint num = 0u; uint num2 = *maxSVPtr + 1; int num3 = 0; if (hbSize < 8) { sbyte* ptr3 = stackalloc sbyte[8]; System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(ptr3, 0, 8u); System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(ptr3, headerBuffer, (uint)hbSize); nuint num4 = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr, ptr3, 8u); if (FSE_isError(num4)) { return num4; } if (num4 > hbSize) { return unchecked((nuint)(-20)); } return num4; } System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(normalizedCounter, 0, (*maxSVPtr + 1) * 2); uint num5 = MEM_readLE32(ptr2); int num6 = (int)((num5 & 0xF) + 5); if (num6 > 15) { return unchecked((nuint)(-44)); } num5 >>= 4; int num7 = 4; *tableLogPtr = (uint)num6; int num8 = (1 << num6) + 1; int num9 = 1 << num6; num6++; while (true) { if (num3 != 0) { int num10; for (num10 = (int)(ZSTD_countTrailingZeros32(~num5 | 0x80000000u) >> 1); num10 >= 12; num10 = (int)(ZSTD_countTrailingZeros32(~num5 | 0x80000000u) >> 1)) { num += 36; if (ptr2 <= ptr - 7) { ptr2 += 3; } else { num7 -= (int)(8 * (ptr - 7 - ptr2)); num7 &= 0x1F; ptr2 = ptr - 4; } n