diff --git a/src/SharpCompress/AssemblyInfo.cs b/src/SharpCompress/AssemblyInfo.cs index 086b9adc..d942a05d 100644 --- a/src/SharpCompress/AssemblyInfo.cs +++ b/src/SharpCompress/AssemblyInfo.cs @@ -5,7 +5,7 @@ using System.Runtime.CompilerServices; [assembly: AssemblyTitle("SharpCompress")] [assembly: AssemblyProduct("SharpCompress")] [assembly: InternalsVisibleTo("SharpCompress.Test" + SharpCompress.AssemblyInfo.PublicKeySuffix)] -[assembly: CLSCompliant(true)] +[assembly: CLSCompliant(false)] namespace SharpCompress { diff --git a/src/SharpCompress/Compressors/Zstd/CompressionStream.cs b/src/SharpCompress/Compressors/Zstd/CompressionStream.cs new file mode 100644 index 00000000..cb4243c6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/CompressionStream.cs @@ -0,0 +1,201 @@ +using System; +using System.Buffers; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using ZstdSharp.Unsafe; + +namespace ZstdSharp +{ + public class CompressionStream : Stream + { + private readonly Stream innerStream; + private readonly byte[] outputBuffer; + private Compressor compressor; + private ZSTD_outBuffer_s output; + + public CompressionStream(Stream stream, int level = Compressor.DefaultCompressionLevel, + int bufferSize = 0) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (!stream.CanWrite) + throw new ArgumentException("Stream is not writable", nameof(stream)); + + if (bufferSize < 0) + throw new ArgumentOutOfRangeException(nameof(bufferSize)); + + innerStream = stream; + compressor = new Compressor(level); + + var outputBufferSize = + bufferSize > 0 ? bufferSize : (int) Methods.ZSTD_CStreamOutSize().EnsureZstdSuccess(); + outputBuffer = ArrayPool.Shared.Rent(outputBufferSize); + output = new ZSTD_outBuffer_s {pos = 0, size = (nuint) outputBufferSize}; + } + + 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(false); + +#if !NETSTANDARD2_0 && !NET461 + public override async ValueTask DisposeAsync() +#else + public async ValueTask DisposeAsync() +#endif + { + if (compressor == null) + return; + + try + { + await FlushAsync().ConfigureAwait(false); + } + finally + { + ReleaseUnmanagedResources(); + GC.SuppressFinalize(this); + } + } + + protected override void Dispose(bool disposing) + { + if (compressor == null) + return; + + try + { + if (disposing) + Flush(); + } + finally + { + ReleaseUnmanagedResources(); + } + } + + private void ReleaseUnmanagedResources() + { + compressor.Dispose(); + ArrayPool.Shared.Return(outputBuffer); + } + + public override void Flush() + => WriteInternal(null, true); + + public override async Task FlushAsync(CancellationToken cancellationToken) + => await WriteInternalAsync(null, true, cancellationToken).ConfigureAwait(false); + + public override void Write(byte[] buffer, int offset, int count) + => Write(new ReadOnlySpan(buffer, offset, count)); + +#if !NETSTANDARD2_0 && !NET461 + public override void Write(ReadOnlySpan buffer) + => WriteInternal(buffer, false); +#else + public void Write(ReadOnlySpan buffer) + => WriteInternal(buffer, false); +#endif + + private void WriteInternal(ReadOnlySpan buffer, bool lastChunk) + { + EnsureNotDisposed(); + + var input = new ZSTD_inBuffer_s {pos = 0, size = buffer != null ? (nuint) buffer.Length : 0}; + nuint remaining; + do + { + output.pos = 0; + remaining = CompressStream(ref input, buffer, + lastChunk ? ZSTD_EndDirective.ZSTD_e_end : ZSTD_EndDirective.ZSTD_e_continue); + + var written = (int) output.pos; + if (written > 0) + innerStream.Write(outputBuffer, 0, written); + } while (lastChunk ? remaining > 0 : input.pos < input.size); + } + + private async ValueTask WriteInternalAsync(ReadOnlyMemory? buffer, bool lastChunk, + CancellationToken cancellationToken = default) + { + EnsureNotDisposed(); + + var input = new ZSTD_inBuffer_s { pos = 0, size = buffer.HasValue ? (nuint)buffer.Value.Length : 0 }; + nuint remaining; + do + { + output.pos = 0; + remaining = CompressStream(ref input, buffer.HasValue ? buffer.Value.Span : null, + lastChunk ? ZSTD_EndDirective.ZSTD_e_end : ZSTD_EndDirective.ZSTD_e_continue); + + var written = (int) output.pos; + if (written > 0) + await innerStream.WriteAsync(outputBuffer, 0, written, cancellationToken).ConfigureAwait(false); + } while (lastChunk ? remaining > 0 : input.pos < input.size); + } + + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + => WriteAsync(new ReadOnlyMemory(buffer, offset, count), cancellationToken).AsTask(); + +#if !NETSTANDARD2_0 && !NET461 + public override async ValueTask WriteAsync(ReadOnlyMemory buffer, + CancellationToken cancellationToken = default) + => await WriteInternalAsync(buffer, false, cancellationToken).ConfigureAwait(false); +#else + public async ValueTask WriteAsync(ReadOnlyMemory buffer, + CancellationToken cancellationToken = default) + => await WriteInternalAsync(buffer, false, cancellationToken).ConfigureAwait(false); +#endif + + internal unsafe nuint CompressStream(ref ZSTD_inBuffer_s input, ReadOnlySpan inputBuffer, + ZSTD_EndDirective directive) + { + fixed (byte* inputBufferPtr = inputBuffer) + fixed (byte* outputBufferPtr = outputBuffer) + { + input.src = inputBufferPtr; + output.dst = outputBufferPtr; + return compressor.CompressStream(ref input, ref output, directive).EnsureZstdSuccess(); + } + } + + public override bool CanRead => false; + public override bool CanSeek => false; + public override bool CanWrite => true; + + public override long Length => throw new NotSupportedException(); + + public override long Position + { + get => throw new NotSupportedException(); + set => 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 int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); + + private void EnsureNotDisposed() + { + if (compressor == null) + throw new ObjectDisposedException(nameof(CompressionStream)); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Compressor.cs b/src/SharpCompress/Compressors/Zstd/Compressor.cs new file mode 100644 index 00000000..dcf93193 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Compressor.cs @@ -0,0 +1,164 @@ +using System; +using ZstdSharp.Unsafe; + +namespace ZstdSharp +{ + public unsafe class Compressor : IDisposable + { + public static int MinCompressionLevel => Methods.ZSTD_minCLevel(); + public static int MaxCompressionLevel => Methods.ZSTD_maxCLevel(); + public const int DefaultCompressionLevel = 0; + + private int level = DefaultCompressionLevel; + + private ZSTD_CCtx_s* cctx; + + public int Level + { + get => level; + set + { + if (level != value) + { + level = value; + SetParameter(ZSTD_cParameter.ZSTD_c_compressionLevel, value); + } + } + } + + public void SetParameter(ZSTD_cParameter parameter, int value) + { + EnsureNotDisposed(); + Methods.ZSTD_CCtx_setParameter(cctx, parameter, value).EnsureZstdSuccess(); + } + + public int GetParameter(ZSTD_cParameter parameter) + { + EnsureNotDisposed(); + int value; + Methods.ZSTD_CCtx_getParameter(cctx, parameter, &value).EnsureZstdSuccess(); + return value; + } + + public void LoadDictionary(byte[] dict) + { + EnsureNotDisposed(); + if (dict == null) + { + Methods.ZSTD_CCtx_loadDictionary(cctx, null, 0).EnsureZstdSuccess(); + } + else + { + + fixed (byte* dictPtr = dict) + Methods.ZSTD_CCtx_loadDictionary(cctx, dictPtr, (nuint) dict.Length).EnsureZstdSuccess(); + } + } + + public Compressor(int level = DefaultCompressionLevel) + { + cctx = Methods.ZSTD_createCCtx(); + if (cctx == null) + throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Failed to create cctx"); + + Level = level; + } + + ~Compressor() + { + ReleaseUnmanagedResources(); + } + + public static int GetCompressBound(int length) + => (int) Methods.ZSTD_compressBound((nuint) length); + + public static ulong GetCompressBoundLong(ulong length) + => Methods.ZSTD_compressBound((nuint) length); + + public Span Wrap(ReadOnlySpan src) + { + var dest = new byte[GetCompressBound(src.Length)]; + var length = Wrap(src, dest); + return new Span(dest, 0, length); + } + + public int Wrap(byte[] src, byte[] dest, int offset) + => Wrap(src, new Span(dest, offset, dest.Length - offset)); + + public int Wrap(ReadOnlySpan src, Span dest) + { + EnsureNotDisposed(); + fixed (byte* srcPtr = src) + fixed (byte* destPtr = dest) + return (int) Methods + .ZSTD_compress2(cctx, destPtr, (nuint) dest.Length, srcPtr, (nuint) src.Length) + .EnsureZstdSuccess(); + } + + public int Wrap(ArraySegment src, ArraySegment dest) + => Wrap((ReadOnlySpan) src, dest); + + public int Wrap(byte[] src, int srcOffset, int srcLength, byte[] dst, int dstOffset, int dstLength) + => Wrap(new ReadOnlySpan(src, srcOffset, srcLength), new Span(dst, dstOffset, dstLength)); + + public bool TryWrap(byte[] src, byte[] dest, int offset, out int written) + => TryWrap(src, new Span(dest, offset, dest.Length - offset), out written); + + public bool TryWrap(ReadOnlySpan src, Span dest, out int written) + { + EnsureNotDisposed(); + fixed (byte* srcPtr = src) + fixed (byte* destPtr = dest) + { + var returnValue = + Methods.ZSTD_compress2(cctx, destPtr, (nuint) dest.Length, srcPtr, (nuint) src.Length); + + if (returnValue == unchecked(0 - (nuint)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)) + { + written = default; + return false; + } + + returnValue.EnsureZstdSuccess(); + written = (int) returnValue; + return true; + } + } + + public bool TryWrap(ArraySegment src, ArraySegment dest, out int written) + => TryWrap((ReadOnlySpan)src, dest, out written); + + public bool TryWrap(byte[] src, int srcOffset, int srcLength, byte[] dst, int dstOffset, int dstLength, out int written) + => TryWrap(new ReadOnlySpan(src, srcOffset, srcLength), new Span(dst, dstOffset, dstLength), out written); + + private void ReleaseUnmanagedResources() + { + if (cctx != null) + { + Methods.ZSTD_freeCCtx(cctx); + cctx = null; + } + } + + public void Dispose() + { + ReleaseUnmanagedResources(); + GC.SuppressFinalize(this); + } + + private void EnsureNotDisposed() + { + if (cctx == null) + throw new ObjectDisposedException(nameof(Compressor)); + } + + internal nuint CompressStream(ref ZSTD_inBuffer_s input, ref ZSTD_outBuffer_s output, ZSTD_EndDirective directive) + { + fixed (ZSTD_inBuffer_s* inputPtr = &input) + fixed (ZSTD_outBuffer_s* outputPtr = &output) + { + return Methods.ZSTD_compressStream2(cctx, outputPtr, inputPtr, directive).EnsureZstdSuccess(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Constants.cs b/src/SharpCompress/Compressors/Zstd/Constants.cs new file mode 100644 index 00000000..c7af1231 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Constants.cs @@ -0,0 +1,9 @@ +namespace ZstdSharp +{ + internal class Constants + { + //NOTE: https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element#remarks + //NOTE: https://github.com/dotnet/runtime/blob/v5.0.0-rtm.20519.4/src/libraries/System.Private.CoreLib/src/System/Array.cs#L27 + public const ulong MaxByteArrayLength = 0x7FFFFFC7; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/DecompressionStream.cs b/src/SharpCompress/Compressors/Zstd/DecompressionStream.cs new file mode 100644 index 00000000..b3f9e490 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/DecompressionStream.cs @@ -0,0 +1,190 @@ +using System; +using System.Buffers; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using ZstdSharp.Unsafe; + +namespace ZstdSharp +{ + public class DecompressionStream : Stream + { + private readonly Stream innerStream; + private readonly byte[] inputBuffer; + private readonly int inputBufferSize; + private Decompressor decompressor; + private ZSTD_inBuffer_s input; + private nuint lastDecompressResult = 0; + + public DecompressionStream(Stream stream, int bufferSize = 0) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + if (!stream.CanRead) + throw new ArgumentException("Stream is not readable", nameof(stream)); + + if (bufferSize < 0) + throw new ArgumentOutOfRangeException(nameof(bufferSize)); + + innerStream = stream; + decompressor = new Decompressor(); + + inputBufferSize = bufferSize > 0 ? bufferSize : (int) Methods.ZSTD_CStreamInSize().EnsureZstdSuccess(); + inputBuffer = ArrayPool.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(false); + + protected override void Dispose(bool disposing) + { + if (decompressor == null) + return; + + decompressor.Dispose(); + ArrayPool.Shared.Return(inputBuffer); + } + + public override int Read(byte[] buffer, int offset, int count) + => Read(new Span(buffer, offset, count)); + +#if !NETSTANDARD2_0 && !NETFRAMEWORK + public override int Read(Span buffer) +#else + public int Read(Span buffer) +#endif + { + EnsureNotDisposed(); + + var output = new ZSTD_outBuffer_s {pos = 0, size = (nuint) buffer.Length}; + while (output.pos < output.size) + { + if (input.pos >= input.size) + { + int bytesRead; + if ((bytesRead = innerStream.Read(inputBuffer, 0, inputBufferSize)) == 0) + { + if (lastDecompressResult != 0) + throw new EndOfStreamException("Premature end of stream"); + + break; + } + + input.size = (nuint) bytesRead; + input.pos = 0; + } + + lastDecompressResult = DecompressStream(ref output, buffer); + } + + return (int) output.pos; + } + + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + => ReadAsync(new Memory(buffer, offset, count), cancellationToken).AsTask(); + +#if !NETSTANDARD2_0 && !NET461 + public override async ValueTask ReadAsync(Memory buffer, + CancellationToken cancellationToken = default) +#else + public async ValueTask ReadAsync(Memory buffer, + CancellationToken cancellationToken = default) +#endif + { + EnsureNotDisposed(); + + var output = new ZSTD_outBuffer_s { pos = 0, size = (nuint)buffer.Length}; + while (output.pos < output.size) + { + if (input.pos >= input.size) + { + int bytesRead; + if ((bytesRead = await innerStream.ReadAsync(inputBuffer, 0, inputBufferSize, cancellationToken) + .ConfigureAwait(false)) == 0) + { + if (lastDecompressResult != 0) + throw new EndOfStreamException("Premature end of stream"); + + break; + } + + input.size = (nuint) bytesRead; + input.pos = 0; + } + + lastDecompressResult = DecompressStream(ref output, buffer.Span); + } + + return (int) output.pos; + } + + private unsafe nuint DecompressStream(ref ZSTD_outBuffer_s output, Span outputBuffer) + { + fixed (byte* inputBufferPtr = inputBuffer) + fixed (byte* outputBufferPtr = outputBuffer) + { + input.src = inputBufferPtr; + output.dst = outputBufferPtr; + return decompressor.DecompressStream(ref input, ref output); + } + } + + public override bool CanRead => true; + public override bool CanSeek => false; + public override bool CanWrite => false; + + public override long Length => throw new NotSupportedException(); + + public override long Position + { + get => throw new NotSupportedException(); + set => throw new NotSupportedException(); + } + + 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(nameof(DecompressionStream)); + } + +#if NETSTANDARD2_0 || NET461 + public virtual ValueTask DisposeAsync() + { + try + { + Dispose(); + return default; + } + catch (Exception exc) + { + return new ValueTask(Task.FromException(exc)); + } + } +#endif + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Decompressor.cs b/src/SharpCompress/Compressors/Zstd/Decompressor.cs new file mode 100644 index 00000000..6e73dc7c --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Decompressor.cs @@ -0,0 +1,149 @@ +using System; +using ZstdSharp.Unsafe; + +namespace ZstdSharp +{ + public unsafe class Decompressor : IDisposable + { + private ZSTD_DCtx_s* dctx; + + public Decompressor() + { + dctx = Methods.ZSTD_createDCtx(); + if (dctx == null) + throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Failed to create dctx"); + } + + ~Decompressor() + { + ReleaseUnmanagedResources(); + } + + public void SetParameter(ZSTD_dParameter parameter, int value) + { + EnsureNotDisposed(); + Methods.ZSTD_DCtx_setParameter(dctx, parameter, value).EnsureZstdSuccess(); + } + + public int GetParameter(ZSTD_dParameter parameter) + { + EnsureNotDisposed(); + int value; + Methods.ZSTD_DCtx_getParameter(dctx, parameter, &value).EnsureZstdSuccess(); + return value; + } + + public void LoadDictionary(byte[] dict) + { + EnsureNotDisposed(); + if (dict == null) + { + Methods.ZSTD_DCtx_loadDictionary(dctx, null, 0).EnsureZstdSuccess(); + } + else + { + fixed (byte* dictPtr = dict) + Methods.ZSTD_DCtx_loadDictionary(dctx, dictPtr, (nuint) dict.Length).EnsureZstdSuccess(); + } + } + + public static ulong GetDecompressedSize(ReadOnlySpan src) + { + fixed (byte* srcPtr = src) + return Methods.ZSTD_decompressBound(srcPtr, (nuint) src.Length).EnsureContentSizeOk(); + } + + public static ulong GetDecompressedSize(ArraySegment src) + => GetDecompressedSize((ReadOnlySpan) src); + + public static ulong GetDecompressedSize(byte[] src, int srcOffset, int srcLength) + => GetDecompressedSize(new ReadOnlySpan(src, srcOffset, srcLength)); + + public Span Unwrap(ReadOnlySpan src, int maxDecompressedSize = int.MaxValue) + { + var expectedDstSize = GetDecompressedSize(src); + if (expectedDstSize > (ulong) maxDecompressedSize) + throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, + $"Decompressed content size {expectedDstSize} is greater than {nameof(maxDecompressedSize)} {maxDecompressedSize}"); + if (expectedDstSize > Constants.MaxByteArrayLength) + throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, + $"Decompressed content size {expectedDstSize} is greater than max possible byte array size {Constants.MaxByteArrayLength}"); + + var dest = new byte[expectedDstSize]; + var length = Unwrap(src, dest); + return new Span(dest, 0, length); + } + + public int Unwrap(byte[] src, byte[] dest, int offset) + => Unwrap(src, new Span(dest, offset, dest.Length - offset)); + + public int Unwrap(ReadOnlySpan src, Span dest) + { + EnsureNotDisposed(); + fixed (byte* srcPtr = src) + fixed (byte* destPtr = dest) + return (int) Methods + .ZSTD_decompressDCtx(dctx, destPtr, (nuint) dest.Length, srcPtr, (nuint) src.Length) + .EnsureZstdSuccess(); + } + + public int Unwrap(byte[] src, int srcOffset, int srcLength, byte[] dst, int dstOffset, int dstLength) + => Unwrap(new ReadOnlySpan(src, srcOffset, srcLength), new Span(dst, dstOffset, dstLength)); + + public bool TryUnwrap(byte[] src, byte[] dest, int offset, out int written) + => TryUnwrap(src, new Span(dest, offset, dest.Length - offset), out written); + + public bool TryUnwrap(ReadOnlySpan src, Span dest, out int written) + { + EnsureNotDisposed(); + fixed (byte* srcPtr = src) + fixed (byte* destPtr = dest) + { + var returnValue = + Methods.ZSTD_decompressDCtx(dctx, destPtr, (nuint) dest.Length, srcPtr, (nuint) src.Length); + + if (returnValue == unchecked(0 - (nuint)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)) + { + written = default; + return false; + } + + returnValue.EnsureZstdSuccess(); + written = (int) returnValue; + return true; + } + } + + public bool TryUnwrap(byte[] src, int srcOffset, int srcLength, byte[] dst, int dstOffset, int dstLength, out int written) + => TryUnwrap(new ReadOnlySpan(src, srcOffset, srcLength), new Span(dst, dstOffset, dstLength), out written); + + private void ReleaseUnmanagedResources() + { + if (dctx != null) + { + Methods.ZSTD_freeDCtx(dctx); + dctx = null; + } + } + + public void Dispose() + { + ReleaseUnmanagedResources(); + GC.SuppressFinalize(this); + } + private void EnsureNotDisposed() + { + if (dctx == null) + throw new ObjectDisposedException(nameof(Decompressor)); + } + + internal nuint DecompressStream(ref ZSTD_inBuffer_s input, ref ZSTD_outBuffer_s output) + { + fixed (ZSTD_inBuffer_s* inputPtr = &input) + fixed (ZSTD_outBuffer_s* outputPtr = &output) + { + return Methods.ZSTD_decompressStream(dctx, outputPtr, inputPtr).EnsureZstdSuccess(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/DictBuilder.cs b/src/SharpCompress/Compressors/Zstd/DictBuilder.cs new file mode 100644 index 00000000..9b6fe73f --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/DictBuilder.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using ZstdSharp.Unsafe; + +namespace ZstdSharp +{ + public static unsafe class DictBuilder + { + public static byte[] TrainFromBuffer(IEnumerable samples, int dictCapacity = DefaultDictCapacity) + { + var ms = new MemoryStream(); + var samplesSizes = samples.Select(sample => + { + ms.Write(sample, 0, sample.Length); + return (nuint) sample.Length; + }).ToArray(); + + var dictBuffer = new byte[dictCapacity]; + fixed (byte* dictBufferPtr = dictBuffer) + fixed (byte* samplesBufferPtr = ms.GetBuffer()) + fixed (nuint* samplesSizesPtr = samplesSizes) + { + var dictSize = (int) Methods + .ZDICT_trainFromBuffer(dictBufferPtr, (nuint) dictCapacity, samplesBufferPtr, samplesSizesPtr, + (uint) samplesSizes.Length) + .EnsureZdictSuccess(); + + if (dictCapacity != dictSize) + Array.Resize(ref dictBuffer, dictSize); + + return dictBuffer; + } + } + + public const int DefaultDictCapacity = 112640; // Used by zstd utility by default + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Polyfills/BitOperations.cs b/src/SharpCompress/Compressors/Zstd/Polyfills/BitOperations.cs new file mode 100644 index 00000000..f5bb209d --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Polyfills/BitOperations.cs @@ -0,0 +1,201 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if !NETCOREAPP3_0_OR_GREATER + +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +// Some routines inspired by the Stanford Bit Twiddling Hacks by Sean Eron Anderson: +// http://graphics.stanford.edu/~seander/bithacks.html + +namespace System.Numerics +{ + /// + /// Utility methods for intrinsic bit-twiddling operations. + /// The methods use hardware intrinsics when available on the underlying platform, + /// otherwise they use optimized software fallbacks. + /// + public static unsafe class BitOperations + { + // hack: should be public because of inline + public static readonly byte* TrailingZeroCountDeBruijn = GetArrayPointer(new byte[] + { + 00, 01, 28, 02, 29, 14, 24, 03, + 30, 22, 20, 15, 25, 17, 04, 08, + 31, 27, 13, 23, 21, 19, 16, 07, + 26, 12, 18, 06, 11, 05, 10, 09 + }); + + // hack: should be public because of inline + public static readonly byte* Log2DeBruijn = GetArrayPointer(new byte[] + { + 00, 09, 01, 10, 13, 21, 02, 29, + 11, 14, 16, 18, 22, 25, 03, 30, + 08, 12, 20, 28, 15, 17, 24, 07, + 19, 27, 23, 06, 26, 05, 04, 31 + }); + + /// + /// Returns the integer (floor) log of the specified value, base 2. + /// Note that by convention, input value 0 returns 0 since log(0) is undefined. + /// + /// The value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int Log2(uint value) + { + // The 0->0 contract is fulfilled by setting the LSB to 1. + // Log(1) is 0, and setting the LSB for values > 1 does not change the log2 result. + value |= 1; + + // value lzcnt actual expected + // ..0001 31 31-31 0 + // ..0010 30 31-30 1 + // 0010.. 2 31-2 29 + // 0100.. 1 31-1 30 + // 1000.. 0 31-0 31 + + // Fallback contract is 0->0 + // No AggressiveInlining due to large method size + // Has conventional contract 0->0 (Log(0) is undefined) + + // Fill trailing zeros with ones, eg 00010010 becomes 00011111 + value |= value >> 01; + value |= value >> 02; + value |= value >> 04; + value |= value >> 08; + value |= value >> 16; + + // uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check + return Log2DeBruijn[ + // Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_1100_0100_1010_1100_1101_1101u + (int)((value * 0x07C4ACDDu) >> 27)]; + } + + /// + /// Returns the integer (floor) log of the specified value, base 2. + /// Note that by convention, input value 0 returns 0 since log(0) is undefined. + /// + /// The value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int Log2(ulong value) + { + value |= 1; + + uint hi = (uint)(value >> 32); + + if (hi == 0) + { + return Log2((uint)value); + } + + return 32 + Log2(hi); + } + + /// + /// Count the number of trailing zero bits in an integer value. + /// Similar in behavior to the x86 instruction TZCNT. + /// + /// The value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int TrailingZeroCount(int value) + => TrailingZeroCount((uint)value); + + /// + /// Count the number of trailing zero bits in an integer value. + /// Similar in behavior to the x86 instruction TZCNT. + /// + /// The value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int TrailingZeroCount(uint value) + { + // Unguarded fallback contract is 0->0, BSF contract is 0->undefined + if (value == 0) + { + return 32; + } + + // uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check + return TrailingZeroCountDeBruijn[ + // Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_0111_1100_1011_0101_0011_0001u + (int)(((value & (uint)-(int)value) * 0x077CB531u) >> 27)]; // Multi-cast mitigates redundant conv.u8 + } + + /// + /// Count the number of trailing zero bits in a mask. + /// Similar in behavior to the x86 instruction TZCNT. + /// + /// The value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int TrailingZeroCount(long value) + => TrailingZeroCount((ulong)value); + + /// + /// Count the number of trailing zero bits in a mask. + /// Similar in behavior to the x86 instruction TZCNT. + /// + /// The value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int TrailingZeroCount(ulong value) + { + uint lo = (uint)value; + + if (lo == 0) + { + return 32 + TrailingZeroCount((uint)(value >> 32)); + } + + return TrailingZeroCount(lo); + } + + /// + /// Rotates the specified value left by the specified number of bits. + /// Similar in behavior to the x86 instruction ROL. + /// + /// The value to rotate. + /// The number of bits to rotate by. + /// Any value outside the range [0..31] is treated as congruent mod 32. + /// The rotated value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint RotateLeft(uint value, int offset) + => (value << offset) | (value >> (32 - offset)); + + /// + /// Rotates the specified value left by the specified number of bits. + /// Similar in behavior to the x86 instruction ROL. + /// + /// The value to rotate. + /// The number of bits to rotate by. + /// Any value outside the range [0..63] is treated as congruent mod 64. + /// The rotated value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong RotateLeft(ulong value, int offset) + => (value << offset) | (value >> (64 - offset)); + + /// + /// Rotates the specified value right by the specified number of bits. + /// Similar in behavior to the x86 instruction ROR. + /// + /// The value to rotate. + /// The number of bits to rotate by. + /// Any value outside the range [0..31] is treated as congruent mod 32. + /// The rotated value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static uint RotateRight(uint value, int offset) + => (value >> offset) | (value << (32 - offset)); + + /// + /// Rotates the specified value right by the specified number of bits. + /// Similar in behavior to the x86 instruction ROR. + /// + /// The value to rotate. + /// The number of bits to rotate by. + /// Any value outside the range [0..63] is treated as congruent mod 64. + /// The rotated value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ulong RotateRight(ulong value, int offset) + => (value >> offset) | (value << (64 - offset)); + } +} + +#endif diff --git a/src/SharpCompress/Compressors/Zstd/Polyfills/SkipLocalsInitAttribute.cs b/src/SharpCompress/Compressors/Zstd/Polyfills/SkipLocalsInitAttribute.cs new file mode 100644 index 00000000..cb043f65 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Polyfills/SkipLocalsInitAttribute.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if !NET5_0_OR_GREATER +namespace System.Runtime.CompilerServices +{ + /// + /// Used to indicate to the compiler that the .locals init flag should not be set in method headers. + /// + /// Internal copy of the .NET 5 attribute. + [AttributeUsage( + AttributeTargets.Module | + AttributeTargets.Class | + AttributeTargets.Struct | + AttributeTargets.Interface | + AttributeTargets.Constructor | + AttributeTargets.Method | + AttributeTargets.Property | + AttributeTargets.Event, + Inherited = false)] + internal sealed class SkipLocalsInitAttribute : Attribute + { + } +} +#endif diff --git a/src/SharpCompress/Compressors/Zstd/Polyfills/Vector128.cs b/src/SharpCompress/Compressors/Zstd/Polyfills/Vector128.cs new file mode 100644 index 00000000..3985df57 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Polyfills/Vector128.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if !NETCOREAPP3_0_OR_GREATER + +using System.Runtime.CompilerServices; + +namespace System.Runtime.Intrinsics +{ + public static class Vector128 + { + internal const int Size = 16; + + public static unsafe Vector128 Create(byte value) + { + byte* pResult = stackalloc byte[16] + { + value, + value, + value, + value, + value, + value, + value, + value, + value, + value, + value, + value, + value, + value, + value, + value, + }; + + return Unsafe.AsRef>(pResult); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector128 As(this Vector128 vector) + where T : struct + where U : struct => + Unsafe.As, Vector128>(ref vector); + + public static T GetElement(this Vector128 vector, int index) + where T : struct + { + ref T e0 = ref Unsafe.As, T>(ref vector); + return Unsafe.Add(ref e0, index); + } + } +} + +#endif diff --git a/src/SharpCompress/Compressors/Zstd/Polyfills/Vector128_1.cs b/src/SharpCompress/Compressors/Zstd/Polyfills/Vector128_1.cs new file mode 100644 index 00000000..659fdb9d --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Polyfills/Vector128_1.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if !NETCOREAPP3_0_OR_GREATER + +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace System.Runtime.Intrinsics +{ + [StructLayout(LayoutKind.Sequential, Size = Vector128.Size)] + public readonly struct Vector128 : IEquatable> + where T : struct + { + private readonly ulong _00; + private readonly ulong _01; + + public static int Count => Vector128.Size / Unsafe.SizeOf(); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Vector128 other) + { + for (int i = 0; i < Count; i++) + { + if (!((IEquatable)(this.GetElement(i))).Equals(other.GetElement(i))) + { + return false; + } + } + + return true; + } + } +} + +#endif diff --git a/src/SharpCompress/Compressors/Zstd/ThrowHelper.cs b/src/SharpCompress/Compressors/Zstd/ThrowHelper.cs new file mode 100644 index 00000000..7769a7fd --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/ThrowHelper.cs @@ -0,0 +1,43 @@ +using ZstdSharp.Unsafe; + +namespace ZstdSharp +{ + public static unsafe class ThrowHelper + { + private const ulong ZSTD_CONTENTSIZE_UNKNOWN = unchecked(0UL - 1); + private const ulong ZSTD_CONTENTSIZE_ERROR = unchecked(0UL - 2); + + public static nuint EnsureZstdSuccess(this nuint returnValue) + { + if (Methods.ZSTD_isError(returnValue) != 0) + ThrowException(returnValue, Methods.ZSTD_getErrorName(returnValue)); + + return returnValue; + } + + public static nuint EnsureZdictSuccess(this nuint returnValue) + { + if (Methods.ZDICT_isError(returnValue) != 0) + ThrowException(returnValue, Methods.ZDICT_getErrorName(returnValue)); + + return returnValue; + } + + public static ulong EnsureContentSizeOk(this ulong returnValue) + { + if (returnValue == ZSTD_CONTENTSIZE_UNKNOWN) + throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Decompressed content size is not specified"); + + if (returnValue == ZSTD_CONTENTSIZE_ERROR) + throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Decompressed content size cannot be determined (e.g. invalid magic number, srcSize too small)"); + + return returnValue; + } + + private static void ThrowException(nuint returnValue, string message) + { + var code = 0 - returnValue; + throw new ZstdException((ZSTD_ErrorCode) code, message); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Arrays.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Arrays.cs new file mode 100644 index 00000000..0a015fc3 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Arrays.cs @@ -0,0 +1,368 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + static uint* rtbTable = GetArrayPointer(new uint[8] { + 0, + 473195, + 504333, + 520860, + 550000, + 700000, + 750000, + 830000, + }); + static byte* LL_Code = 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, + }); + static byte* ML_Code = 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, + }); + static ulong* srcSizeTiers = GetArrayPointer(new ulong[4] + { + 16 * (1 << 10), + 128 * (1 << 10), + 256 * (1 << 10), + (unchecked(0UL - 1)), + }); + static ZSTD_blockCompressor?[][] blockCompressor = new ZSTD_blockCompressor?[4][] + { + new ZSTD_blockCompressor[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[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[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?[10] + { + null, + null, + null, + ZSTD_compressBlock_greedy_dedicatedDictSearch, + ZSTD_compressBlock_lazy_dedicatedDictSearch, + ZSTD_compressBlock_lazy2_dedicatedDictSearch, + null, + null, + null, + null, + }, + }; + static ZSTD_blockCompressor[][] rowBasedBlockCompressors = new ZSTD_blockCompressor[4][] + { + new ZSTD_blockCompressor[3] + { + ZSTD_compressBlock_greedy_row, + ZSTD_compressBlock_lazy_row, + ZSTD_compressBlock_lazy2_row, + }, + new ZSTD_blockCompressor[3] + { + ZSTD_compressBlock_greedy_extDict_row, + ZSTD_compressBlock_lazy_extDict_row, + ZSTD_compressBlock_lazy2_extDict_row, + }, + new ZSTD_blockCompressor[3] + { + ZSTD_compressBlock_greedy_dictMatchState_row, + ZSTD_compressBlock_lazy_dictMatchState_row, + ZSTD_compressBlock_lazy2_dictMatchState_row, + }, + new ZSTD_blockCompressor[3] + { + ZSTD_compressBlock_greedy_dedicatedDictSearch_row, + ZSTD_compressBlock_lazy_dedicatedDictSearch_row, + ZSTD_compressBlock_lazy2_dedicatedDictSearch_row, + }, + }; + static searchMax_f?[][] searchFuncs = new searchMax_f?[4][] + { + new searchMax_f[3] + { + ZSTD_HcFindBestMatch_selectMLS, + ZSTD_BtFindBestMatch_selectMLS, + ZSTD_RowFindBestMatch_selectRowLog, + }, + new searchMax_f?[3] + { + null, + null, + null, + }, + new searchMax_f[3] + { + ZSTD_HcFindBestMatch_dictMatchState_selectMLS, + ZSTD_BtFindBestMatch_dictMatchState_selectMLS, + ZSTD_RowFindBestMatch_dictMatchState_selectRowLog, + }, + new searchMax_f?[3] + { + ZSTD_HcFindBestMatch_dedicatedDictSearch_selectMLS, + null, + ZSTD_RowFindBestMatch_dedicatedDictSearch_selectRowLog, + }, + }; + static searchMax_f[] searchFuncsExtGeneric = new searchMax_f[3] + { + ZSTD_HcFindBestMatch_extDict_selectMLS, + ZSTD_BtFindBestMatch_extDict_selectMLS, + ZSTD_RowFindBestMatch_extDict_selectRowLog, + }; + static decompressionAlgo[] decompress = new decompressionAlgo[2] + { + HUF_decompress4X1, + HUF_decompress4X2, + }; + static uint* dec32table = GetArrayPointer(new uint[8] + { + 0, + 1, + 2, + 1, + 4, + 4, + 4, + 4, + }); + static int* dec64table = GetArrayPointer(new int[8] + { + 8, + 8, + 8, + 7, + 8, + 9, + 10, + 11, + }); + + private static byte* emptyString = GetArrayPointer(new byte[] {0}); + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_CStream_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_CStream_t.cs new file mode 100644 index 00000000..92eff1ef --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_CStream_t.cs @@ -0,0 +1,24 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*-****************************************** + * bitStream encoding API (write forward) + ********************************************/ + /* bitStream can mix input from multiple sources. + * A critical property of these streams is that they encode and decode in **reverse** direction. + * So the first bit sequence you add will be the last to be read, like a LIFO stack. + */ + public unsafe partial struct BIT_CStream_t + { + public nuint bitContainer; + + public uint bitPos; + + public sbyte* startPtr; + + public sbyte* ptr; + + public sbyte* endPtr; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_DStream_status.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_DStream_status.cs new file mode 100644 index 00000000..e295b902 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_DStream_status.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum BIT_DStream_status + { + BIT_DStream_unfinished = 0, + BIT_DStream_endOfBuffer = 1, + BIT_DStream_completed = 2, + BIT_DStream_overflow = 3, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_DStream_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_DStream_t.cs new file mode 100644 index 00000000..7959c22e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/BIT_DStream_t.cs @@ -0,0 +1,20 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*-******************************************** + * bitStream decoding API (read backward) + **********************************************/ + public unsafe partial struct BIT_DStream_t + { + public nuint bitContainer; + + public uint bitsConsumed; + + public sbyte* ptr; + + public sbyte* start; + + public sbyte* limitPtr; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Bitstream.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Bitstream.cs new file mode 100644 index 00000000..9dfa3399 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Bitstream.cs @@ -0,0 +1,420 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*-************************************************************** + * Internal functions + ****************************************************************/ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint BIT_highbit32(uint val) + { + assert(val != 0); + + { + return (uint) BitOperations.Log2(val); + } + } + + public static uint* BIT_mask = GetArrayPointer(new uint[32] + { + 0, + 1, + 3, + 7, + 0xF, + 0x1F, + 0x3F, + 0x7F, + 0xFF, + 0x1FF, + 0x3FF, + 0x7FF, + 0xFFF, + 0x1FFF, + 0x3FFF, + 0x7FFF, + 0xFFFF, + 0x1FFFF, + 0x3FFFF, + 0x7FFFF, + 0xFFFFF, + 0x1FFFFF, + 0x3FFFFF, + 0x7FFFFF, + 0xFFFFFF, + 0x1FFFFFF, + 0x3FFFFFF, + 0x7FFFFFF, + 0xFFFFFFF, + 0x1FFFFFFF, + 0x3FFFFFFF, + 0x7FFFFFFF, + }); + + /*-************************************************************** + * bitStream encoding + ****************************************************************/ + /*! BIT_initCStream() : + * `dstCapacity` must be > sizeof(size_t) + * @return : 0 if success, + * otherwise an error code (can be tested using ERR_isError()) */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, nuint dstCapacity) + { + bitC->bitContainer = 0; + bitC->bitPos = 0; + bitC->startPtr = (sbyte*)(startPtr); + bitC->ptr = bitC->startPtr; + bitC->endPtr = bitC->startPtr + dstCapacity - (nuint)(sizeof(nuint)); + if (dstCapacity <= (nuint)(sizeof(nuint))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + return 0; + } + + /*! BIT_addBits() : + * can add up to 31 bits into `bitC`. + * Note : does not check for register overflow ! */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void BIT_addBits(BIT_CStream_t* bitC, nuint value, uint nbBits) + { + assert(nbBits < ((nuint)(sizeof(uint) * 32) / (nuint)(sizeof(uint)))); + assert(nbBits + bitC->bitPos < (nuint)(sizeof(nuint)) * 8); + bitC->bitContainer |= (value & BIT_mask[nbBits]) << (int)bitC->bitPos; + bitC->bitPos += nbBits; + } + + /*! BIT_addBitsFast() : + * works only if `value` is _clean_, + * meaning all high bits above nbBits are 0 */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void BIT_addBitsFast(BIT_CStream_t* bitC, nuint value, uint nbBits) + { + assert((value >> (int)nbBits) == 0); + assert(nbBits + bitC->bitPos < (nuint)(sizeof(nuint)) * 8); + bitC->bitContainer |= value << (int)bitC->bitPos; + bitC->bitPos += nbBits; + } + + /*! BIT_flushBitsFast() : + * assumption : bitContainer has not overflowed + * unsafe version; does not check buffer overflow */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void BIT_flushBitsFast(BIT_CStream_t* bitC) + { + nuint nbBytes = bitC->bitPos >> 3; + + assert(bitC->bitPos < (nuint)(sizeof(nuint)) * 8); + assert(bitC->ptr <= bitC->endPtr); + MEM_writeLEST((void*)bitC->ptr, bitC->bitContainer); + bitC->ptr += nbBytes; + bitC->bitPos &= 7; + bitC->bitContainer >>= (int)(nbBytes * 8); + } + + /*! BIT_flushBits() : + * assumption : bitContainer has not overflowed + * safe version; check for buffer overflow, and prevents it. + * note : does not signal buffer overflow. + * overflow will be revealed later on using BIT_closeCStream() */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void BIT_flushBits(BIT_CStream_t* bitC) + { + nuint nbBytes = bitC->bitPos >> 3; + + assert(bitC->bitPos < (nuint)(sizeof(nuint)) * 8); + assert(bitC->ptr <= bitC->endPtr); + MEM_writeLEST((void*)bitC->ptr, bitC->bitContainer); + bitC->ptr += nbBytes; + if (bitC->ptr > bitC->endPtr) + { + bitC->ptr = bitC->endPtr; + } + + bitC->bitPos &= 7; + bitC->bitContainer >>= (int)(nbBytes * 8); + } + + /*! BIT_closeCStream() : + * @return : size of CStream, in bytes, + * or 0 if it could not fit into dstBuffer */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_closeCStream(BIT_CStream_t* bitC) + { + BIT_addBitsFast(bitC, 1, 1); + BIT_flushBits(bitC); + if (bitC->ptr >= bitC->endPtr) + { + return 0; + } + + return (nuint)((bitC->ptr - bitC->startPtr) + (((bitC->bitPos > 0)) ? 1 : 0)); + } + + /*-******************************************************** + * bitStream decoding + **********************************************************/ + /*! BIT_initDStream() : + * Initialize a BIT_DStream_t. + * `bitD` : a pointer to an already allocated BIT_DStream_t structure. + * `srcSize` must be the *exact* size of the bitStream, in bytes. + * @return : size of stream (== srcSize), or an errorCode if a problem is detected + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_initDStream(BIT_DStream_t* bitD, void* srcBuffer, nuint srcSize) + { + if (srcSize < 1) + { + memset((void*)(bitD), (0), ((nuint)(sizeof(BIT_DStream_t)))); + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + bitD->start = (sbyte*)(srcBuffer); + bitD->limitPtr = bitD->start + (nuint)(sizeof(nuint)); + if (srcSize >= (nuint)(sizeof(nuint))) + { + bitD->ptr = (sbyte*)(srcBuffer) + srcSize - (nuint)(sizeof(nuint)); + bitD->bitContainer = MEM_readLEST((void*)bitD->ptr); + + { + byte lastByte = ((byte*)(srcBuffer))[srcSize - 1]; + + bitD->bitsConsumed = lastByte != 0 ? 8 - BIT_highbit32(lastByte) : 0; + if (lastByte == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + } + } + else + { + bitD->ptr = bitD->start; + bitD->bitContainer = *(byte*)(bitD->start); + switch (srcSize) + { + case 7: + { + bitD->bitContainer += (nuint)(((byte*)(srcBuffer))[6]) << (int)((nuint)(sizeof(nuint)) * 8 - 16); + } + + + goto case 6; + case 6: + { + bitD->bitContainer += (nuint)(((byte*)(srcBuffer))[5]) << (int)((nuint)(sizeof(nuint)) * 8 - 24); + } + + + goto case 5; + case 5: + { + bitD->bitContainer += (nuint)(((byte*)(srcBuffer))[4]) << (int)((nuint)(sizeof(nuint)) * 8 - 32); + } + + + goto case 4; + case 4: + { + bitD->bitContainer += (nuint)(((byte*)(srcBuffer))[3]) << 24; + } + + + goto case 3; + case 3: + { + bitD->bitContainer += (nuint)(((byte*)(srcBuffer))[2]) << 16; + } + + + goto case 2; + case 2: + { + bitD->bitContainer += (nuint)(((byte*)(srcBuffer))[1]) << 8; + } + + + goto default; + default: + { + break; + } + } + + + { + byte lastByte = ((byte*)(srcBuffer))[srcSize - 1]; + + bitD->bitsConsumed = lastByte != 0 ? 8 - BIT_highbit32(lastByte) : 0; + if (lastByte == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + } + + bitD->bitsConsumed += (uint)((nuint)(sizeof(nuint)) - srcSize) * 8; + } + + return srcSize; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_getUpperBits(nuint bitContainer, uint start) + { + return bitContainer >> (int)start; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_getMiddleBits(nuint bitContainer, uint start, uint nbBits) + { + uint regMask = (uint)((nuint)(sizeof(nuint)) * 8 - 1); + + assert(nbBits < ((nuint)(sizeof(uint) * 32) / (nuint)(sizeof(uint)))); + return (bitContainer >> (int)(start & regMask)) & BIT_mask[nbBits]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_getLowerBits(nuint bitContainer, uint nbBits) + { + assert(nbBits < ((nuint)(sizeof(uint) * 32) / (nuint)(sizeof(uint)))); + return bitContainer & BIT_mask[nbBits]; + } + + /*! BIT_lookBits() : + * Provides next n bits from local register. + * local register is not modified. + * On 32-bits, maxNbBits==24. + * On 64-bits, maxNbBits==56. + * @return : value extracted */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_lookBits(BIT_DStream_t* bitD, uint nbBits) + { + return BIT_getMiddleBits(bitD->bitContainer, (uint)(((nuint)(sizeof(nuint)) * 8) - bitD->bitsConsumed - nbBits), nbBits); + } + + /*! BIT_lookBitsFast() : + * unsafe version; only works if nbBits >= 1 */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_lookBitsFast(BIT_DStream_t* bitD, uint nbBits) + { + uint regMask = (uint)((nuint)(sizeof(nuint)) * 8 - 1); + + assert(nbBits >= 1); + return (bitD->bitContainer << (int)(bitD->bitsConsumed & regMask)) >> (int)(((regMask + 1) - nbBits) & regMask); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void BIT_skipBits(BIT_DStream_t* bitD, uint nbBits) + { + bitD->bitsConsumed += nbBits; + } + + /*! BIT_readBits() : + * Read (consume) next n bits from local register and update. + * Pay attention to not read more than nbBits contained into local register. + * @return : extracted value. */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_readBits(BIT_DStream_t* bitD, uint nbBits) + { + nuint value = BIT_lookBits(bitD, nbBits); + + BIT_skipBits(bitD, nbBits); + return value; + } + + /*! BIT_readBitsFast() : + * unsafe version; only works only if nbBits >= 1 */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint BIT_readBitsFast(BIT_DStream_t* bitD, uint nbBits) + { + nuint value = BIT_lookBitsFast(bitD, nbBits); + + assert(nbBits >= 1); + BIT_skipBits(bitD, nbBits); + return value; + } + + /*! BIT_reloadDStreamFast() : + * Similar to BIT_reloadDStream(), but with two differences: + * 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold! + * 2. Returns BIT_DStream_overflow when bitD->ptr < bitD->limitPtr, at this + * point you must use BIT_reloadDStream() to reload. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD) + { + if ((bitD->ptr < bitD->limitPtr)) + { + return BIT_DStream_status.BIT_DStream_overflow; + } + + assert(bitD->bitsConsumed <= (nuint)(sizeof(nuint)) * 8); + bitD->ptr -= bitD->bitsConsumed >> 3; + bitD->bitsConsumed &= 7; + bitD->bitContainer = MEM_readLEST((void*)bitD->ptr); + return BIT_DStream_status.BIT_DStream_unfinished; + } + + /*! BIT_reloadDStream() : + * Refill `bitD` from buffer previously set in BIT_initDStream() . + * This function is safe, it guarantees it will not read beyond src buffer. + * @return : status of `BIT_DStream_t` internal register. + * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) + { + if (bitD->bitsConsumed > ((nuint)(sizeof(nuint)) * 8)) + { + return BIT_DStream_status.BIT_DStream_overflow; + } + + if (bitD->ptr >= bitD->limitPtr) + { + return BIT_reloadDStreamFast(bitD); + } + + if (bitD->ptr == bitD->start) + { + if (bitD->bitsConsumed < (nuint)(sizeof(nuint)) * 8) + { + return BIT_DStream_status.BIT_DStream_endOfBuffer; + } + + return BIT_DStream_status.BIT_DStream_completed; + } + + + { + uint nbBytes = bitD->bitsConsumed >> 3; + BIT_DStream_status result = BIT_DStream_status.BIT_DStream_unfinished; + + if (bitD->ptr - nbBytes < bitD->start) + { + nbBytes = (uint)(bitD->ptr - bitD->start); + result = BIT_DStream_status.BIT_DStream_endOfBuffer; + } + + bitD->ptr -= nbBytes; + bitD->bitsConsumed -= nbBytes * 8; + bitD->bitContainer = MEM_readLEST((void*)bitD->ptr); + return result; + } + } + + /*! BIT_endOfDStream() : + * @return : 1 if DStream has _exactly_ reached its end (all bits consumed). + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint BIT_endOfDStream(BIT_DStream_t* DStream) + { + return ((((DStream->ptr == DStream->start) && (DStream->bitsConsumed == (nuint)(sizeof(nuint)) * 8))) ? 1U : 0U); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_best_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_best_s.cs new file mode 100644 index 00000000..dbf10688 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_best_s.cs @@ -0,0 +1,29 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /** + * COVER_best_t is used for two purposes: + * 1. Synchronizing threads. + * 2. Saving the best parameters and dictionary. + * + * All of the methods except COVER_best_init() are thread safe if zstd is + * compiled with multithreaded support. + */ + public unsafe partial struct COVER_best_s + { + public int mutex; + + public int cond; + + public nuint liveJobs; + + public void* dict; + + public nuint dictSize; + + public ZDICT_cover_params_t parameters; + + public nuint compressedSize; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_dictSelection.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_dictSelection.cs new file mode 100644 index 00000000..307a69a8 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_dictSelection.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /** + * Struct used for the dictionary selection function. + */ + public unsafe partial struct COVER_dictSelection + { + public byte* dictContent; + + public nuint dictSize; + + public nuint totalCompressedSize; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_epoch_info_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_epoch_info_t.cs new file mode 100644 index 00000000..620a534a --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_epoch_info_t.cs @@ -0,0 +1,14 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /** + *Number of epochs and size of each epoch. + */ + public partial struct COVER_epoch_info_t + { + public uint num; + + public uint size; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_segment_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_segment_t.cs new file mode 100644 index 00000000..06c7db5e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/COVER_segment_t.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /** + * A segment is a range in the source as well as the score of the segment. + */ + public partial struct COVER_segment_t + { + public uint begin; + + public uint end; + + public uint score; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Cover.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Cover.cs new file mode 100644 index 00000000..e561f988 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Cover.cs @@ -0,0 +1,411 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static int g_displayLevel = 2; + + /** + * Returns the sum of the sample sizes. + */ + public static nuint COVER_sum(nuint* samplesSizes, uint nbSamples) + { + nuint sum = 0; + uint i; + + for (i = 0; i < nbSamples; ++i) + { + sum += samplesSizes[i]; + } + + return sum; + } + + /** + * Warns the user when their corpus is too small. + */ + public static void COVER_warnOnSmallCorpus(nuint maxDictSize, nuint nbDmers, int displayLevel) + { + double ratio = (double)(nbDmers) / maxDictSize; + + if (ratio >= 10) + { + return; + } + + if (displayLevel >= 1) + { + ; + } + + } + + /** + * Computes the number of epochs and the size of each epoch. + * We will make sure that each epoch gets at least 10 * k bytes. + * + * The COVER algorithms divide the data up into epochs of equal size and + * select one segment from each epoch. + * + * @param maxDictSize The maximum allowed dictionary size. + * @param nbDmers The number of dmers we are training on. + * @param k The parameter k (segment size). + * @param passes The target number of passes over the dmer corpus. + * More passes means a better dictionary. + */ + public static COVER_epoch_info_t COVER_computeEpochs(uint maxDictSize, uint nbDmers, uint k, uint passes) + { + uint minEpochSize = k * 10; + COVER_epoch_info_t epochs; + + epochs.num = (uint)((1) > (maxDictSize / k / passes) ? (1) : (maxDictSize / k / passes)); + epochs.size = nbDmers / epochs.num; + if (epochs.size >= minEpochSize) + { + assert(epochs.size * epochs.num <= nbDmers); + return epochs; + } + + epochs.size = ((minEpochSize) < (nbDmers) ? (minEpochSize) : (nbDmers)); + epochs.num = nbDmers / epochs.size; + assert(epochs.size * epochs.num <= nbDmers); + return epochs; + } + + /** + * Checks total compressed size of a dictionary + */ + public static nuint COVER_checkTotalCompressedSize(ZDICT_cover_params_t parameters, nuint* samplesSizes, byte* samples, nuint* offsets, nuint nbTrainSamples, nuint nbSamples, byte* dict, nuint dictBufferCapacity) + { + nuint totalCompressedSize = (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + ZSTD_CCtx_s* cctx; + ZSTD_CDict_s* cdict; + void* dst; + nuint dstCapacity; + nuint i; + + + { + nuint maxSampleSize = 0; + + i = parameters.splitPoint < 1.0 ? nbTrainSamples : 0; + for (; i < nbSamples; ++i) + { + maxSampleSize = ((samplesSizes[i]) > (maxSampleSize) ? (samplesSizes[i]) : (maxSampleSize)); + } + + dstCapacity = ZSTD_compressBound(maxSampleSize); + dst = malloc(dstCapacity); + } + + cctx = ZSTD_createCCtx(); + cdict = ZSTD_createCDict((void*)dict, dictBufferCapacity, parameters.zParams.compressionLevel); + if (dst == null || cctx == null || cdict == null) + { + goto _compressCleanup; + } + + totalCompressedSize = dictBufferCapacity; + i = parameters.splitPoint < 1.0 ? nbTrainSamples : 0; + for (; i < nbSamples; ++i) + { + nuint size = ZSTD_compress_usingCDict(cctx, dst, dstCapacity, (void*)(samples + offsets[i]), samplesSizes[i], cdict); + + if ((ERR_isError(size)) != 0) + { + totalCompressedSize = size; + goto _compressCleanup; + } + + totalCompressedSize += size; + } + + _compressCleanup: + ZSTD_freeCCtx(cctx); + ZSTD_freeCDict(cdict); + if (dst != null) + { + free(dst); + } + + return totalCompressedSize; + } + + /** + * Initialize the `COVER_best_t`. + */ + public static void COVER_best_init(COVER_best_s* best) + { + if (best == null) + { + return; + } + + + + best->liveJobs = 0; + best->dict = null; + best->dictSize = 0; + best->compressedSize = unchecked((nuint)(-1)); + memset((void*)&best->parameters, 0, (nuint)(sizeof(ZDICT_cover_params_t))); + } + + /** + * Wait until liveJobs == 0. + */ + public static void COVER_best_wait(COVER_best_s* best) + { + if (best == null) + { + return; + } + + + while (best->liveJobs != 0) + { + + } + + + } + + /** + * Call COVER_best_wait() and then destroy the COVER_best_t. + */ + public static void COVER_best_destroy(COVER_best_s* best) + { + if (best == null) + { + return; + } + + COVER_best_wait(best); + if (best->dict != null) + { + free(best->dict); + } + + + + } + + /** + * Called when a thread is about to be launched. + * Increments liveJobs. + */ + public static void COVER_best_start(COVER_best_s* best) + { + if (best == null) + { + return; + } + + + ++best->liveJobs; + + } + + /** + * Called when a thread finishes executing, both on error or success. + * Decrements liveJobs and signals any waiting threads if liveJobs == 0. + * If this dictionary is the best so far save it and its parameters. + */ + public static void COVER_best_finish(COVER_best_s* best, ZDICT_cover_params_t parameters, COVER_dictSelection selection) + { + void* dict = (void*)selection.dictContent; + nuint compressedSize = selection.totalCompressedSize; + nuint dictSize = selection.dictSize; + + if (best == null) + { + return; + } + + + { + nuint liveJobs; + + + --best->liveJobs; + liveJobs = best->liveJobs; + if (compressedSize < best->compressedSize) + { + if (best->dict == null || best->dictSize < dictSize) + { + if (best->dict != null) + { + free(best->dict); + } + + best->dict = malloc(dictSize); + if (best->dict == null) + { + best->compressedSize = (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + best->dictSize = 0; + + + return; + } + } + + if (dict != null) + { + memcpy(best->dict, dict, dictSize); + best->dictSize = dictSize; + best->parameters = parameters; + best->compressedSize = compressedSize; + } + } + + if (liveJobs == 0) + { + + } + + + } + } + + /** + * Error function for COVER_selectDict function. Returns a struct where + * return.totalCompressedSize is a ZSTD error. + */ + public static COVER_dictSelection COVER_dictSelectionError(nuint error) + { + COVER_dictSelection selection = new COVER_dictSelection + { + dictContent = null, + dictSize = 0, + totalCompressedSize = error, + }; + + return selection; + } + + /** + * Error function for COVER_selectDict function. Checks if the return + * value is an error. + */ + public static uint COVER_dictSelectionIsError(COVER_dictSelection selection) + { + return ((((ERR_isError(selection.totalCompressedSize)) != 0 || selection.dictContent == null)) ? 1U : 0U); + } + + /** + * Always call after selectDict is called to free up used memory from + * newly created dictionary. + */ + public static void COVER_dictSelectionFree(COVER_dictSelection selection) + { + free((void*)selection.dictContent); + } + + /** + * Called to finalize the dictionary and select one based on whether or not + * the shrink-dict flag was enabled. If enabled the dictionary used is the + * smallest dictionary within a specified regression of the compressed size + * from the largest dictionary. + */ + public 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 largestDict = 0; + nuint largestCompressed = 0; + byte* customDictContentEnd = customDictContent + dictContentSize; + byte* largestDictbuffer = (byte*)(malloc(dictBufferCapacity)); + byte* candidateDictBuffer = (byte*)(malloc(dictBufferCapacity)); + double regressionTolerance = ((double)(@params.shrinkDictMaxRegression) / 100.0) + 1.00; + + if (largestDictbuffer == null || candidateDictBuffer == null) + { + free((void*)largestDictbuffer); + free((void*)candidateDictBuffer); + return COVER_dictSelectionError(dictContentSize); + } + + memcpy((void*)largestDictbuffer, (void*)customDictContent, dictContentSize); + dictContentSize = ZDICT_finalizeDictionary((void*)largestDictbuffer, dictBufferCapacity, (void*)customDictContent, dictContentSize, (void*)samplesBuffer, samplesSizes, nbFinalizeSamples, @params.zParams); + if ((ZDICT_isError(dictContentSize)) != 0) + { + free((void*)largestDictbuffer); + free((void*)candidateDictBuffer); + return COVER_dictSelectionError(dictContentSize); + } + + totalCompressedSize = COVER_checkTotalCompressedSize(@params, samplesSizes, samplesBuffer, offsets, nbCheckSamples, nbSamples, largestDictbuffer, dictContentSize); + if ((ERR_isError(totalCompressedSize)) != 0) + { + free((void*)largestDictbuffer); + free((void*)candidateDictBuffer); + return COVER_dictSelectionError(totalCompressedSize); + } + + if (@params.shrinkDict == 0) + { + COVER_dictSelection selection = new COVER_dictSelection + { + dictContent = largestDictbuffer, + dictSize = dictContentSize, + totalCompressedSize = totalCompressedSize, + }; + + free((void*)candidateDictBuffer); + return selection; + } + + largestDict = dictContentSize; + largestCompressed = totalCompressedSize; + dictContentSize = 256; + while (dictContentSize < largestDict) + { + memcpy((void*)candidateDictBuffer, (void*)largestDictbuffer, largestDict); + dictContentSize = ZDICT_finalizeDictionary((void*)candidateDictBuffer, dictBufferCapacity, (void*)(customDictContentEnd - dictContentSize), dictContentSize, (void*)samplesBuffer, samplesSizes, nbFinalizeSamples, @params.zParams); + if ((ZDICT_isError(dictContentSize)) != 0) + { + free((void*)largestDictbuffer); + free((void*)candidateDictBuffer); + return COVER_dictSelectionError(dictContentSize); + } + + totalCompressedSize = COVER_checkTotalCompressedSize(@params, samplesSizes, samplesBuffer, offsets, nbCheckSamples, nbSamples, candidateDictBuffer, dictContentSize); + if ((ERR_isError(totalCompressedSize)) != 0) + { + free((void*)largestDictbuffer); + free((void*)candidateDictBuffer); + return COVER_dictSelectionError(totalCompressedSize); + } + + if (totalCompressedSize <= largestCompressed * regressionTolerance) + { + COVER_dictSelection selection = new COVER_dictSelection + { + dictContent = candidateDictBuffer, + dictSize = dictContentSize, + totalCompressedSize = totalCompressedSize, + }; + + free((void*)largestDictbuffer); + return selection; + } + + dictContentSize *= 2; + } + + dictContentSize = largestDict; + totalCompressedSize = largestCompressed; + + { + COVER_dictSelection selection = new COVER_dictSelection + { + dictContent = largestDictbuffer, + dictSize = dictContentSize, + totalCompressedSize = totalCompressedSize, + }; + + free((void*)candidateDictBuffer); + return selection; + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/DTableDesc.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/DTableDesc.cs new file mode 100644 index 00000000..3e8bb63c --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/DTableDesc.cs @@ -0,0 +1,18 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*-***************************/ + /* generic DTableDesc */ + /*-***************************/ + public partial struct DTableDesc + { + public byte maxTableLog; + + public byte tableType; + + public byte tableLog; + + public byte reserved; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/EStats_ress_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/EStats_ress_t.cs new file mode 100644 index 00000000..1b9f54b3 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/EStats_ress_t.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct EStats_ress_t + { + /* dictionary */ + public ZSTD_CDict_s* dict; + + /* working context */ + public ZSTD_CCtx_s* zc; + + /* must be ZSTD_BLOCKSIZE_MAX allocated */ + public void* workPlace; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/EntropyCommon.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/EntropyCommon.cs new file mode 100644 index 00000000..b02733ef --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/EntropyCommon.cs @@ -0,0 +1,425 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*=== Version ===*/ + public static uint FSE_versionNumber() + { + return (uint)((0 * 100 * 100 + 9 * 100 + 0)); + } + + /*=== Error Management ===*/ + public static uint FSE_isError(nuint code) + { + return ERR_isError(code); + } + + public static string FSE_getErrorName(nuint code) + { + return ERR_getErrorName(code); + } + + /* Error Management */ + public static uint HUF_isError(nuint code) + { + return ERR_isError(code); + } + + public static string HUF_getErrorName(nuint code) + { + return ERR_getErrorName(code); + } + + /*-************************************************************** + * FSE NCount encoding-decoding + ****************************************************************/ + [InlineMethod.Inline] + private static uint FSE_ctz(uint val) + { + assert(val != 0); + + { + return (uint) BitOperations.TrailingZeroCount(val); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint FSE_readNCount_body(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize) + { + byte* istart = (byte*)(headerBuffer); + byte* iend = istart + hbSize; + byte* ip = istart; + int nbBits; + int remaining; + int threshold; + uint bitStream; + int bitCount; + uint charnum = 0; + uint maxSV1 = *maxSVPtr + 1; + int previous0 = 0; + + if (hbSize < 8) + { + sbyte* buffer = stackalloc sbyte[8]; + memset(buffer, 0, sizeof(sbyte) * 8); + + memcpy((void*)(buffer), (headerBuffer), (hbSize)); + + { + nuint countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr, (void*)buffer, (nuint)(8)); + + if ((FSE_isError(countSize)) != 0) + { + return countSize; + } + + if (countSize > hbSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + return countSize; + } + } + + assert(hbSize >= 8); + memset((void*)(normalizedCounter), (0), ((*maxSVPtr + 1) * (nuint)(sizeof(short)))); + bitStream = MEM_readLE32((void*)ip); + nbBits = (int)((bitStream & 0xF) + 5); + if (nbBits > 15) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + bitStream >>= 4; + bitCount = 4; + *tableLogPtr = (uint)nbBits; + remaining = (1 << nbBits) + 1; + threshold = 1 << nbBits; + nbBits++; + for (;;) + { + if (previous0 != 0) + { + int repeats = (int)(FSE_ctz(~bitStream | 0x80000000) >> 1); + + while (repeats >= 12) + { + charnum += (uint)(3 * 12); + if ((ip <= iend - 7)) + { + ip += 3; + } + else + { + bitCount -= (int)(8 * (iend - 7 - ip)); + bitCount &= 31; + ip = iend - 4; + } + + bitStream = MEM_readLE32((void*)ip) >> bitCount; + repeats = (int)(FSE_ctz(~bitStream | 0x80000000) >> 1); + } + + charnum += (uint)(3 * repeats); + bitStream >>= 2 * repeats; + bitCount += 2 * repeats; + assert((bitStream & 3) < 3); + charnum += bitStream & 3; + bitCount += 2; + if (charnum >= maxSV1) + { + break; + } + + if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) + { + assert((bitCount >> 3) <= 3); + ip += bitCount >> 3; + bitCount &= 7; + } + else + { + bitCount -= (int)(8 * (iend - 4 - ip)); + bitCount &= 31; + ip = iend - 4; + } + + bitStream = MEM_readLE32((void*)ip) >> bitCount; + } + + + { + int max = (2 * threshold - 1) - remaining; + int count; + + if ((bitStream & (uint)((threshold - 1))) < (uint)(max)) + { + count = (int)(bitStream & (uint)((threshold - 1))); + bitCount += nbBits - 1; + } + else + { + count = (int)(bitStream & (uint)((2 * threshold - 1))); + if (count >= threshold) + { + count -= max; + } + + bitCount += nbBits; + } + + count--; + if (count >= 0) + { + remaining -= count; + } + else + { + assert(count == -1); + remaining += count; + } + + normalizedCounter[charnum++] = (short)(count); + previous0 = (count == 0 ? 1 : 0); + assert(threshold > 1); + if (remaining < threshold) + { + if (remaining <= 1) + { + break; + } + + nbBits = (int)(BIT_highbit32((uint)remaining) + 1); + threshold = 1 << (nbBits - 1); + } + + if (charnum >= maxSV1) + { + break; + } + + if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) + { + ip += bitCount >> 3; + bitCount &= 7; + } + else + { + bitCount -= (int)(8 * (iend - 4 - ip)); + bitCount &= 31; + ip = iend - 4; + } + + bitStream = MEM_readLE32((void*)ip) >> bitCount; + } + } + + if (remaining != 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (charnum > maxSV1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooSmall))); + } + + if (bitCount > 32) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + *maxSVPtr = charnum - 1; + ip += (bitCount + 7) >> 3; + return (nuint)(ip - istart); + } + + /* Avoids the FORCE_INLINE of the _body() function. */ + private static nuint FSE_readNCount_body_default(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize) + { + return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); + } + + private static nuint FSE_readNCount_body_bmi2(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize) + { + return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); + } + + /*! FSE_readNCount_bmi2(): + * Same as FSE_readNCount() but pass bmi2=1 when your CPU supports BMI2 and 0 otherwise. + */ + public static nuint FSE_readNCount_bmi2(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize, int bmi2) + { + if (bmi2 != 0) + { + return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); + } + + return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); + } + + /*! FSE_readNCount(): + Read compactly saved 'normalizedCounter' from 'rBuffer'. + @return : size read from 'rBuffer', + or an errorCode, which can be tested using FSE_isError(). + maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */ + public static nuint FSE_readNCount(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize) + { + return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, 0); + } + + /*! HUF_readStats() : + Read compact Huffman tree, saved by HUF_writeCTable(). + `huffWeight` is destination buffer. + `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32. + @return : size read from `src` , or an error Code . + Note : Needed by HUF_readCTable() and HUF_readDTableX?() . + */ + public static nuint HUF_readStats(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize) + { + uint* wksp = stackalloc uint[218]; + + return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, (void*)wksp, (nuint)(sizeof(uint) * 218), 0); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint HUF_readStats_body(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize, void* workSpace, nuint wkspSize, int bmi2) + { + uint weightTotal; + byte* ip = (byte*)(src); + nuint iSize; + nuint oSize; + + if (srcSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + iSize = ip[0]; + if (iSize >= 128) + { + oSize = iSize - 127; + iSize = ((oSize + 1) / 2); + if (iSize + 1 > srcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (oSize >= hwSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + ip += 1; + + { + uint n; + + for (n = 0; n < oSize; n += 2) + { + huffWeight[n] = (byte)(ip[n / 2] >> 4); + huffWeight[n + 1] = (byte)(ip[n / 2] & 15); + } + } + } + else + { + if (iSize + 1 > srcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + oSize = FSE_decompress_wksp_bmi2((void*)huffWeight, hwSize - 1, (void*)(ip + 1), iSize, 6, workSpace, wkspSize, bmi2); + if ((FSE_isError(oSize)) != 0) + { + return oSize; + } + } + + memset((void*)(rankStats), (0), ((uint)((12 + 1)) * (nuint)(sizeof(uint)))); + weightTotal = 0; + + { + uint n; + + for (n = 0; n < oSize; n++) + { + if (huffWeight[n] >= 12) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + rankStats[huffWeight[n]]++; + weightTotal += (uint)((1 << (int)(huffWeight[n])) >> 1); + } + } + + if (weightTotal == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + uint tableLog = BIT_highbit32(weightTotal) + 1; + + if (tableLog > 12) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + *tableLogPtr = tableLog; + + { + uint total = (uint)(1 << (int)tableLog); + uint rest = total - weightTotal; + uint verif = (uint)(1 << (int)(BIT_highbit32(rest))); + uint lastWeight = BIT_highbit32(rest) + 1; + + if (verif != rest) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + huffWeight[oSize] = (byte)(lastWeight); + rankStats[lastWeight]++; + } + } + + if ((rankStats[1] < 2) || (rankStats[1] & 1) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + *nbSymbolsPtr = (uint)(oSize + 1); + return iSize + 1; + } + + /* Avoids the FORCE_INLINE of the _body() function. */ + private static nuint HUF_readStats_body_default(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize, void* workSpace, nuint wkspSize) + { + return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0); + } + + private static nuint HUF_readStats_body_bmi2(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize, void* workSpace, nuint wkspSize) + { + return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1); + } + + public static nuint HUF_readStats_wksp(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize, void* workSpace, nuint wkspSize, int bmi2) + { + if (bmi2 != 0) + { + return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize); + } + + return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ErrorPrivate.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ErrorPrivate.cs new file mode 100644 index 00000000..9ca7b291 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ErrorPrivate.cs @@ -0,0 +1,184 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ERR_isError(nuint code) + { + return (((code > (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxCode))))) ? 1U : 0U); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ZSTD_ErrorCode ERR_getErrorCode(nuint code) + { + if ((ERR_isError(code)) == 0) + { + return (ZSTD_ErrorCode)(0); + } + + return (ZSTD_ErrorCode)(0 - code); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static string ERR_getErrorName(nuint code) + { + return ERR_getErrorString(ERR_getErrorCode(code)); + } + + /*-**************************************** + * Error Strings + ******************************************/ + public static string ERR_getErrorString(ZSTD_ErrorCode code) + { + var notErrorCode = "Unspecified error code"; + + switch (code) + { + case ZSTD_ErrorCode.ZSTD_error_no_error: + { + return "No error detected"; + } + + case ZSTD_ErrorCode.ZSTD_error_GENERIC: + { + return "Error (generic)"; + } + + case ZSTD_ErrorCode.ZSTD_error_prefix_unknown: + { + return "Unknown frame descriptor"; + } + + case ZSTD_ErrorCode.ZSTD_error_version_unsupported: + { + return "Version not supported"; + } + + case ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported: + { + return "Unsupported frame parameter"; + } + + case ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge: + { + return "Frame requires too much memory for decoding"; + } + + case ZSTD_ErrorCode.ZSTD_error_corruption_detected: + { + return "Corrupted block detected"; + } + + case ZSTD_ErrorCode.ZSTD_error_checksum_wrong: + { + return "Restored data doesn't match checksum"; + } + + case ZSTD_ErrorCode.ZSTD_error_parameter_unsupported: + { + return "Unsupported parameter"; + } + + case ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound: + { + return "Parameter is out of bound"; + } + + case ZSTD_ErrorCode.ZSTD_error_init_missing: + { + return "Context should be init first"; + } + + case ZSTD_ErrorCode.ZSTD_error_memory_allocation: + { + return "Allocation error : not enough memory"; + } + + case ZSTD_ErrorCode.ZSTD_error_workSpace_tooSmall: + { + return "workSpace buffer is not large enough"; + } + + case ZSTD_ErrorCode.ZSTD_error_stage_wrong: + { + return "Operation not authorized at current processing stage"; + } + + case ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge: + { + return "tableLog requires too much memory : unsupported"; + } + + case ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooLarge: + { + return "Unsupported max Symbol Value : too large"; + } + + case ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooSmall: + { + return "Specified maxSymbolValue is too small"; + } + + case ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted: + { + return "Dictionary is corrupted"; + } + + case ZSTD_ErrorCode.ZSTD_error_dictionary_wrong: + { + return "Dictionary mismatch"; + } + + case ZSTD_ErrorCode.ZSTD_error_dictionaryCreation_failed: + { + return "Cannot create Dictionary from provided samples"; + } + + case ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall: + { + return "Destination buffer is too small"; + } + + case ZSTD_ErrorCode.ZSTD_error_srcSize_wrong: + { + return "Src size is incorrect"; + } + + case ZSTD_ErrorCode.ZSTD_error_dstBuffer_null: + { + return "Operation on NULL destination buffer"; + } + + case ZSTD_ErrorCode.ZSTD_error_frameIndex_tooLarge: + { + return "Frame index is too large"; + } + + case ZSTD_ErrorCode.ZSTD_error_seekableIO: + { + return "An I/O error occurred when reading/seeking"; + } + + case ZSTD_ErrorCode.ZSTD_error_dstBuffer_wrong: + { + return "Destination buffer is wrong"; + } + + case ZSTD_ErrorCode.ZSTD_error_srcBuffer_wrong: + { + return "Source buffer is wrong"; + } + + case ZSTD_ErrorCode.ZSTD_error_maxCode: + default: + { + return notErrorCode; + } + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_accel_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_accel_t.cs new file mode 100644 index 00000000..0cfb16c6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_accel_t.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*-************************************* + * Acceleration + ***************************************/ + public partial struct FASTCOVER_accel_t + { + /* Percentage of training samples used for ZDICT_finalizeDictionary */ + public uint finalize; + + /* Number of dmer skipped between each dmer counted in computeFrequency */ + public uint skip; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_ctx_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_ctx_t.cs new file mode 100644 index 00000000..3941706d --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_ctx_t.cs @@ -0,0 +1,32 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*-************************************* + * Context + ***************************************/ + public unsafe partial struct FASTCOVER_ctx_t + { + public byte* samples; + + public nuint* offsets; + + public nuint* samplesSizes; + + public nuint nbSamples; + + public nuint nbTrainSamples; + + public nuint nbTestSamples; + + public nuint nbDmers; + + public uint* freqs; + + public uint d; + + public uint f; + + public FASTCOVER_accel_t accelParams; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_tryParameters_data_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_tryParameters_data_s.cs new file mode 100644 index 00000000..fd472944 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FASTCOVER_tryParameters_data_s.cs @@ -0,0 +1,18 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /** + * Parameters for FASTCOVER_tryParameters(). + */ + public unsafe partial struct FASTCOVER_tryParameters_data_s + { + public FASTCOVER_ctx_t* ctx; + + public COVER_best_s* best; + + public nuint dictBufferCapacity; + + public ZDICT_cover_params_t parameters; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_CState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_CState_t.cs new file mode 100644 index 00000000..8461e8d3 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_CState_t.cs @@ -0,0 +1,22 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* ***************************************** + * FSE symbol compression API + *******************************************/ + /*! + This API consists of small unitary functions, which highly benefit from being inlined. + Hence their body are included in next section. + */ + public unsafe partial struct FSE_CState_t + { + public nint value; + + public void* stateTable; + + public void* symbolTT; + + public uint stateLog; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DState_t.cs new file mode 100644 index 00000000..f031b693 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DState_t.cs @@ -0,0 +1,15 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* ***************************************** + * FSE symbol decompression API + *******************************************/ + public unsafe partial struct FSE_DState_t + { + public nuint state; + + /* precise table may vary, depending on U16 */ + public void* table; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DTableHeader.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DTableHeader.cs new file mode 100644 index 00000000..271e76db --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DTableHeader.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* ====== Decompression ====== */ + public partial struct FSE_DTableHeader + { + public ushort tableLog; + + public ushort fastMode; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DecompressWksp.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DecompressWksp.cs new file mode 100644 index 00000000..24ac57d8 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_DecompressWksp.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct FSE_DecompressWksp + { + public fixed short ncount[256]; + + /* Dynamically sized */ + public fixed uint dtable[1]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_decode_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_decode_t.cs new file mode 100644 index 00000000..219d77f8 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_decode_t.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct FSE_decode_t + { + public ushort newState; + + public byte symbol; + + public byte nbBits; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_repeat.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_repeat.cs new file mode 100644 index 00000000..99fa81a0 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_repeat.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum FSE_repeat + { + FSE_repeat_none, + FSE_repeat_check, + FSE_repeat_valid, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_symbolCompressionTransform.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_symbolCompressionTransform.cs new file mode 100644 index 00000000..b95766e6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FSE_symbolCompressionTransform.cs @@ -0,0 +1,14 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* ***************************************** + * Implementation of inlined functions + *******************************************/ + public partial struct FSE_symbolCompressionTransform + { + public int deltaFindState; + + public uint deltaNbBits; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Fastcover.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Fastcover.cs new file mode 100644 index 00000000..58c7046e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Fastcover.cs @@ -0,0 +1,715 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*-************************************* + * Hash Functions + ***************************************/ + /** + * Hash the d-byte value pointed to by p and mod 2^f into the frequency vector + */ + private static nuint FASTCOVER_hashPtrToIndex(void* p, uint f, uint d) + { + if (d == 6) + { + return ZSTD_hash6Ptr(p, f); + } + + return ZSTD_hash8Ptr(p, f); + } + + public static FASTCOVER_accel_t* FASTCOVER_defaultAccelParameters = GetArrayPointer(new FASTCOVER_accel_t[11] + { + new FASTCOVER_accel_t + { + finalize = 100, + skip = 0, + }, + new FASTCOVER_accel_t + { + finalize = 100, + skip = 0, + }, + new FASTCOVER_accel_t + { + finalize = 50, + skip = 1, + }, + new FASTCOVER_accel_t + { + finalize = 34, + skip = 2, + }, + new FASTCOVER_accel_t + { + finalize = 25, + skip = 3, + }, + new FASTCOVER_accel_t + { + finalize = 20, + skip = 4, + }, + new FASTCOVER_accel_t + { + finalize = 17, + skip = 5, + }, + new FASTCOVER_accel_t + { + finalize = 14, + skip = 6, + }, + new FASTCOVER_accel_t + { + finalize = 13, + skip = 7, + }, + new FASTCOVER_accel_t + { + finalize = 11, + skip = 8, + }, + new FASTCOVER_accel_t + { + finalize = 10, + skip = 9, + }, + }); + + /*-************************************* + * Helper functions + ***************************************/ + /** + * Selects the best segment in an epoch. + * Segments of are scored according to the function: + * + * Let F(d) be the frequency of all dmers with hash value d. + * Let S_i be hash value of the dmer at position i of segment S which has length k. + * + * Score(S) = F(S_1) + F(S_2) + ... + F(S_{k-d+1}) + * + * Once the dmer with hash value d is in the dictionary we set F(d) = 0. + */ + private static COVER_segment_t FASTCOVER_selectSegment(FASTCOVER_ctx_t* ctx, uint* freqs, uint begin, uint end, ZDICT_cover_params_t parameters, ushort* segmentFreqs) + { + uint k = parameters.k; + uint d = parameters.d; + uint f = ctx->f; + uint dmersInK = k - d + 1; + COVER_segment_t bestSegment = new COVER_segment_t + { + begin = 0, + end = 0, + score = 0, + }; + COVER_segment_t activeSegment; + + activeSegment.begin = begin; + activeSegment.end = begin; + activeSegment.score = 0; + while (activeSegment.end < end) + { + nuint idx = FASTCOVER_hashPtrToIndex((void*)(ctx->samples + activeSegment.end), f, d); + + if (segmentFreqs[idx] == 0) + { + activeSegment.score += freqs[idx]; + } + + activeSegment.end += 1; + segmentFreqs[idx] += 1; + if (activeSegment.end - activeSegment.begin == dmersInK + 1) + { + nuint delIndex = FASTCOVER_hashPtrToIndex((void*)(ctx->samples + activeSegment.begin), f, d); + + segmentFreqs[delIndex] -= 1; + if (segmentFreqs[delIndex] == 0) + { + activeSegment.score -= freqs[delIndex]; + } + + activeSegment.begin += 1; + } + + if (activeSegment.score > bestSegment.score) + { + bestSegment = activeSegment; + } + } + + while (activeSegment.begin < end) + { + nuint delIndex = FASTCOVER_hashPtrToIndex((void*)(ctx->samples + activeSegment.begin), f, d); + + segmentFreqs[delIndex] -= 1; + activeSegment.begin += 1; + } + + + { + uint pos; + + for (pos = bestSegment.begin; pos != bestSegment.end; ++pos) + { + nuint i = FASTCOVER_hashPtrToIndex((void*)(ctx->samples + pos), f, d); + + freqs[i] = 0; + } + } + + return bestSegment; + } + + private static int FASTCOVER_checkParameters(ZDICT_cover_params_t parameters, nuint maxDictSize, uint f, uint accel) + { + if (parameters.d == 0 || parameters.k == 0) + { + return 0; + } + + if (parameters.d != 6 && parameters.d != 8) + { + return 0; + } + + if (parameters.k > maxDictSize) + { + return 0; + } + + if (parameters.d > parameters.k) + { + return 0; + } + + if (f > 31 || f == 0) + { + return 0; + } + + if (parameters.splitPoint <= 0 || parameters.splitPoint > 1) + { + return 0; + } + + if (accel > 10 || accel == 0) + { + return 0; + } + + return 1; + } + + /** + * Clean up a context initialized with `FASTCOVER_ctx_init()`. + */ + private static void FASTCOVER_ctx_destroy(FASTCOVER_ctx_t* ctx) + { + if (ctx == null) + { + return; + } + + free((void*)ctx->freqs); + ctx->freqs = null; + free((void*)ctx->offsets); + ctx->offsets = null; + } + + /** + * Calculate for frequency of hash value of each dmer in ctx->samples + */ + private static void FASTCOVER_computeFrequency(uint* freqs, FASTCOVER_ctx_t* ctx) + { + uint f = ctx->f; + uint d = ctx->d; + uint skip = ctx->accelParams.skip; + uint readLength = ((d) > (8) ? (d) : (8)); + nuint i; + + assert(ctx->nbTrainSamples >= 5); + assert(ctx->nbTrainSamples <= ctx->nbSamples); + for (i = 0; i < ctx->nbTrainSamples; i++) + { + nuint start = ctx->offsets[i]; + nuint currSampleEnd = ctx->offsets[i + 1]; + + while (start + readLength <= currSampleEnd) + { + nuint dmerIndex = FASTCOVER_hashPtrToIndex((void*)(ctx->samples + start), f, d); + + freqs[dmerIndex]++; + start = start + skip + 1; + } + } + } + + /** + * Prepare a context for dictionary building. + * The context is only dependent on the parameter `d` and can used multiple + * times. + * Returns 0 on success or error code on error. + * The context must be destroyed with `FASTCOVER_ctx_destroy()`. + */ + private static nuint FASTCOVER_ctx_init(FASTCOVER_ctx_t* ctx, void* samplesBuffer, nuint* samplesSizes, uint nbSamples, uint d, double splitPoint, uint f, FASTCOVER_accel_t accelParams) + { + byte* samples = (byte*)(samplesBuffer); + nuint totalSamplesSize = COVER_sum(samplesSizes, nbSamples); + uint nbTrainSamples = splitPoint < 1.0 ? (uint)((double)(nbSamples) * splitPoint) : nbSamples; + uint nbTestSamples = splitPoint < 1.0 ? nbSamples - nbTrainSamples : nbSamples; + nuint trainingSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes, nbTrainSamples) : totalSamplesSize; + nuint testSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes + nbTrainSamples, nbTestSamples) : totalSamplesSize; + + if (totalSamplesSize < ((d) > ((nuint)(sizeof(ulong))) ? (d) : ((nuint)(sizeof(ulong)))) || totalSamplesSize >= (nuint)((nuint)(sizeof(nuint)) == 8 ? (unchecked((uint)(-1))) : ((uint)(1) * (1U << 30)))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (nbTrainSamples < 5) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (nbTestSamples < 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + memset((void*)ctx, 0, (nuint)(sizeof(FASTCOVER_ctx_t))); + ctx->samples = samples; + ctx->samplesSizes = samplesSizes; + ctx->nbSamples = nbSamples; + ctx->nbTrainSamples = nbTrainSamples; + ctx->nbTestSamples = nbTestSamples; + ctx->nbDmers = trainingSamplesSize - ((d) > ((nuint)(sizeof(ulong))) ? (d) : ((nuint)(sizeof(ulong)))) + 1; + ctx->d = d; + ctx->f = f; + ctx->accelParams = accelParams; + ctx->offsets = (nuint*)(calloc((nbSamples + 1), (nuint)(sizeof(nuint)))); + if (ctx->offsets == null) + { + FASTCOVER_ctx_destroy(ctx); + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + + { + uint i; + + ctx->offsets[0] = 0; + assert(nbSamples >= 5); + for (i = 1; i <= nbSamples; ++i) + { + ctx->offsets[i] = ctx->offsets[i - 1] + samplesSizes[i - 1]; + } + } + + ctx->freqs = (uint*)(calloc((nuint)((ulong)(1) << (int)f), (nuint)(sizeof(uint)))); + if (ctx->freqs == null) + { + FASTCOVER_ctx_destroy(ctx); + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + FASTCOVER_computeFrequency(ctx->freqs, ctx); + return 0; + } + + /** + * Given the prepared context build the dictionary. + */ + private static nuint FASTCOVER_buildDictionary(FASTCOVER_ctx_t* ctx, uint* freqs, void* dictBuffer, nuint dictBufferCapacity, ZDICT_cover_params_t parameters, ushort* segmentFreqs) + { + byte* dict = (byte*)(dictBuffer); + nuint tail = dictBufferCapacity; + COVER_epoch_info_t epochs = COVER_computeEpochs((uint)(dictBufferCapacity), (uint)(ctx->nbDmers), parameters.k, 1); + nuint maxZeroScoreRun = 10; + nuint zeroScoreRun = 0; + nuint epoch; + + for (epoch = 0; tail > 0; epoch = (nuint)((epoch + 1) % epochs.num)) + { + uint epochBegin = (uint)(epoch * epochs.size); + uint epochEnd = epochBegin + epochs.size; + nuint segmentSize; + COVER_segment_t segment = FASTCOVER_selectSegment(ctx, freqs, epochBegin, epochEnd, parameters, segmentFreqs); + + if (segment.score == 0) + { + if (++zeroScoreRun >= maxZeroScoreRun) + { + break; + } + + continue; + } + + zeroScoreRun = 0; + segmentSize = ((segment.end - segment.begin + parameters.d - 1) < (tail) ? (segment.end - segment.begin + parameters.d - 1) : (tail)); + if (segmentSize < parameters.d) + { + break; + } + + tail -= segmentSize; + memcpy((void*)(dict + tail), (void*)(ctx->samples + segment.begin), segmentSize); + } + + return tail; + } + + /** + * Tries a set of parameters and updates the COVER_best_t with the results. + * This function is thread safe if zstd is compiled with multithreaded support. + * It takes its parameters as an *OWNING* opaque pointer to support threading. + */ + private static void FASTCOVER_tryParameters(void* opaque) + { + FASTCOVER_tryParameters_data_s* data = (FASTCOVER_tryParameters_data_s*)(opaque); + FASTCOVER_ctx_t* ctx = data->ctx; + ZDICT_cover_params_t parameters = data->parameters; + nuint dictBufferCapacity = data->dictBufferCapacity; + nuint totalCompressedSize = (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + ushort* segmentFreqs = (ushort*)(calloc((nuint)((ulong)(1) << (int)ctx->f), (nuint)(2))); + byte* dict = (byte*)(malloc(dictBufferCapacity)); + COVER_dictSelection selection = COVER_dictSelectionError((unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC)))); + uint* freqs = (uint*)(malloc((nuint)(((ulong)(1) << (int)ctx->f) * (nuint)(4)))); + + if (segmentFreqs == null || dict == null || freqs == null) + { + goto _cleanup; + } + + memcpy((void*)freqs, (void*)ctx->freqs, (nuint)(((ulong)(1) << (int)ctx->f) * (nuint)(sizeof(uint)))); + + { + nuint tail = FASTCOVER_buildDictionary(ctx, freqs, (void*)dict, dictBufferCapacity, parameters, segmentFreqs); + uint nbFinalizeSamples = (uint)(ctx->nbTrainSamples * ctx->accelParams.finalize / 100); + + selection = COVER_selectDict(dict + tail, dictBufferCapacity, dictBufferCapacity - tail, ctx->samples, ctx->samplesSizes, nbFinalizeSamples, ctx->nbTrainSamples, ctx->nbSamples, parameters, ctx->offsets, totalCompressedSize); + if ((COVER_dictSelectionIsError(selection)) != 0) + { + goto _cleanup; + } + } + + _cleanup: + free((void*)dict); + COVER_best_finish(data->best, parameters, selection); + free((void*)data); + free((void*)segmentFreqs); + COVER_dictSelectionFree(selection); + free((void*)freqs); + } + + private static void FASTCOVER_convertToCoverParams(ZDICT_fastCover_params_t fastCoverParams, ZDICT_cover_params_t* coverParams) + { + coverParams->k = fastCoverParams.k; + coverParams->d = fastCoverParams.d; + coverParams->steps = fastCoverParams.steps; + coverParams->nbThreads = fastCoverParams.nbThreads; + coverParams->splitPoint = fastCoverParams.splitPoint; + coverParams->zParams = fastCoverParams.zParams; + coverParams->shrinkDict = fastCoverParams.shrinkDict; + } + + private static void FASTCOVER_convertToFastCoverParams(ZDICT_cover_params_t coverParams, ZDICT_fastCover_params_t* fastCoverParams, uint f, uint accel) + { + fastCoverParams->k = coverParams.k; + fastCoverParams->d = coverParams.d; + fastCoverParams->steps = coverParams.steps; + fastCoverParams->nbThreads = coverParams.nbThreads; + fastCoverParams->splitPoint = coverParams.splitPoint; + fastCoverParams->f = f; + fastCoverParams->accel = accel; + fastCoverParams->zParams = coverParams.zParams; + fastCoverParams->shrinkDict = coverParams.shrinkDict; + } + + /*! ZDICT_trainFromBuffer_fastCover(): + * Train a dictionary from an array of samples using a modified version of COVER algorithm. + * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, + * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. + * d and k are required. + * All other parameters are optional, will use default values if not provided + * The resulting dictionary will be saved into `dictBuffer`. + * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) + * or an error code, which can be tested with ZDICT_isError(). + * See ZDICT_trainFromBuffer() for details on failure modes. + * Note: ZDICT_trainFromBuffer_fastCover() requires 6 * 2^f bytes of memory. + * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. + * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. + * In general, it's recommended to provide a few thousands samples, though this can vary a lot. + * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. + */ + public static nuint ZDICT_trainFromBuffer_fastCover(void* dictBuffer, nuint dictBufferCapacity, void* samplesBuffer, nuint* samplesSizes, uint nbSamples, ZDICT_fastCover_params_t parameters) + { + byte* dict = (byte*)(dictBuffer); + FASTCOVER_ctx_t ctx; + ZDICT_cover_params_t coverParams; + FASTCOVER_accel_t accelParams; + + g_displayLevel = (int)parameters.zParams.notificationLevel; + parameters.splitPoint = 1.0; + parameters.f = (uint)(parameters.f == 0 ? 20 : parameters.f); + parameters.accel = (uint)(parameters.accel == 0 ? 1 : parameters.accel); + memset((void*)&coverParams, 0, (nuint)(sizeof(ZDICT_cover_params_t))); + FASTCOVER_convertToCoverParams(parameters, &coverParams); + if ((FASTCOVER_checkParameters(coverParams, dictBufferCapacity, parameters.f, parameters.accel)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + if (nbSamples == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (dictBufferCapacity < 256) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + accelParams = FASTCOVER_defaultAccelParameters[parameters.accel]; + + { + nuint initVal = FASTCOVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, coverParams.d, parameters.splitPoint, parameters.f, accelParams); + + if ((ERR_isError(initVal)) != 0) + { + return initVal; + } + } + + COVER_warnOnSmallCorpus(dictBufferCapacity, ctx.nbDmers, g_displayLevel); + + { + ushort* segmentFreqs = (ushort*)(calloc((nuint)((ulong)(1) << (int)parameters.f), (nuint)(2))); + nuint tail = FASTCOVER_buildDictionary(&ctx, ctx.freqs, dictBuffer, dictBufferCapacity, coverParams, segmentFreqs); + uint nbFinalizeSamples = (uint)(ctx.nbTrainSamples * ctx.accelParams.finalize / 100); + nuint dictionarySize = ZDICT_finalizeDictionary((void*)dict, dictBufferCapacity, (void*)(dict + tail), dictBufferCapacity - tail, samplesBuffer, samplesSizes, nbFinalizeSamples, coverParams.zParams); + + if ((ERR_isError(dictionarySize)) == 0) + { + ; + } + + FASTCOVER_ctx_destroy(&ctx); + free((void*)segmentFreqs); + return dictionarySize; + } + } + + /*! ZDICT_optimizeTrainFromBuffer_fastCover(): + * The same requirements as above hold for all the parameters except `parameters`. + * This function tries many parameter combinations (specifically, k and d combinations) + * and picks the best parameters. `*parameters` is filled with the best parameters found, + * dictionary constructed with those parameters is stored in `dictBuffer`. + * All of the parameters d, k, steps, f, and accel are optional. + * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}. + * if steps is zero it defaults to its default value. + * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000]. + * If f is zero, default value of 20 is used. + * If accel is zero, default value of 1 is used. + * + * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) + * or an error code, which can be tested with ZDICT_isError(). + * On success `*parameters` contains the parameters selected. + * See ZDICT_trainFromBuffer() for details on failure modes. + * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 6 * 2^f bytes of memory for each thread. + */ + public static nuint ZDICT_optimizeTrainFromBuffer_fastCover(void* dictBuffer, nuint dictBufferCapacity, void* samplesBuffer, nuint* samplesSizes, uint nbSamples, ZDICT_fastCover_params_t* parameters) + { + ZDICT_cover_params_t coverParams; + FASTCOVER_accel_t accelParams; + uint nbThreads = parameters->nbThreads; + double splitPoint = parameters->splitPoint <= 0.0 ? 0.75 : parameters->splitPoint; + uint kMinD = (uint)(parameters->d == 0 ? 6 : parameters->d); + uint kMaxD = (uint)(parameters->d == 0 ? 8 : parameters->d); + uint kMinK = (uint)(parameters->k == 0 ? 50 : parameters->k); + uint kMaxK = (uint)(parameters->k == 0 ? 2000 : parameters->k); + uint kSteps = (uint)(parameters->steps == 0 ? 40 : parameters->steps); + uint kStepSize = (((kMaxK - kMinK) / kSteps) > (1) ? ((kMaxK - kMinK) / kSteps) : (1)); + uint kIterations = (1 + (kMaxD - kMinD) / 2) * (1 + (kMaxK - kMinK) / kStepSize); + uint f = (uint)(parameters->f == 0 ? 20 : parameters->f); + uint accel = (uint)(parameters->accel == 0 ? 1 : parameters->accel); + uint shrinkDict = 0; + int displayLevel = (int)parameters->zParams.notificationLevel; + uint iteration = 1; + uint d; + uint k; + COVER_best_s best; + int warned = 0; + + if (splitPoint <= 0 || splitPoint > 1) + { + if (displayLevel >= 1) + { + ; + } + + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + if (accel == 0 || accel > 10) + { + if (displayLevel >= 1) + { + ; + } + + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + if (kMinK < kMaxD || kMaxK < kMinK) + { + if (displayLevel >= 1) + { + ; + } + + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + if (nbSamples == 0) + { + if (displayLevel >= 1) + { + ; + } + + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (dictBufferCapacity < 256) + { + if (displayLevel >= 1) + { + ; + } + + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (nbThreads > 1) + { + throw new NotImplementedException("Multiple threads are not supported"); + } + + COVER_best_init(&best); + memset((void*)&coverParams, 0, (nuint)(sizeof(ZDICT_cover_params_t))); + FASTCOVER_convertToCoverParams(*parameters, &coverParams); + accelParams = FASTCOVER_defaultAccelParameters[accel]; + g_displayLevel = displayLevel == 0 ? 0 : displayLevel - 1; + if (displayLevel >= 2) + { + ; + } + + for (d = kMinD; d <= kMaxD; d += 2) + { + FASTCOVER_ctx_t ctx; + + if (displayLevel >= 3) + { + ; + } + + + { + nuint initVal = FASTCOVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, d, splitPoint, f, accelParams); + + if ((ERR_isError(initVal)) != 0) + { + if (displayLevel >= 1) + { + ; + } + + COVER_best_destroy(&best); + return initVal; + } + } + + if (warned == 0) + { + COVER_warnOnSmallCorpus(dictBufferCapacity, ctx.nbDmers, displayLevel); + warned = 1; + } + + for (k = kMinK; k <= kMaxK; k += kStepSize) + { + FASTCOVER_tryParameters_data_s* data = (FASTCOVER_tryParameters_data_s*)(malloc((nuint)(sizeof(FASTCOVER_tryParameters_data_s)))); + + if (displayLevel >= 3) + { + ; + } + + if (data == null) + { + if (displayLevel >= 1) + { + ; + } + + COVER_best_destroy(&best); + FASTCOVER_ctx_destroy(&ctx); + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + data->ctx = &ctx; + data->best = &best; + data->dictBufferCapacity = dictBufferCapacity; + data->parameters = coverParams; + data->parameters.k = k; + data->parameters.d = d; + data->parameters.splitPoint = splitPoint; + data->parameters.steps = kSteps; + data->parameters.shrinkDict = shrinkDict; + data->parameters.zParams.notificationLevel = (uint)g_displayLevel; + if ((FASTCOVER_checkParameters(data->parameters, dictBufferCapacity, data->ctx->f, accel)) == 0) + { + free((void*)data); + continue; + } + + COVER_best_start(&best); + FASTCOVER_tryParameters((void*)data); + + ++iteration; + } + + COVER_best_wait(&best); + FASTCOVER_ctx_destroy(&ctx); + } + + if (displayLevel >= 2) + { + ; + } + + + { + nuint dictSize = best.dictSize; + + if ((ERR_isError(best.compressedSize)) != 0) + { + nuint compressedSize = best.compressedSize; + + COVER_best_destroy(&best); + return compressedSize; + } + + FASTCOVER_convertToFastCoverParams(best.parameters, parameters, f, accel); + memcpy(dictBuffer, best.dict, dictSize); + COVER_best_destroy(&best); + return dictSize; + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Fse.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Fse.cs new file mode 100644 index 00000000..565c5e62 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Fse.cs @@ -0,0 +1,158 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void FSE_initCState(FSE_CState_t* statePtr, uint* ct) + { + void* ptr = (void*)ct; + ushort* u16ptr = (ushort*)(ptr); + uint tableLog = MEM_read16(ptr); + + statePtr->value = (nint)(1) << (int)tableLog; + statePtr->stateTable = u16ptr + 2; + statePtr->symbolTT = ct + 1 + (tableLog != 0 ? (1 << (int)(tableLog - 1)) : 1); + statePtr->stateLog = tableLog; + } + + /*! FSE_initCState2() : + * Same as FSE_initCState(), but the first symbol to include (which will be the last to be read) + * uses the smallest state value possible, saving the cost of this symbol */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void FSE_initCState2(FSE_CState_t* statePtr, uint* ct, uint symbol) + { + FSE_initCState(statePtr, ct); + + { + FSE_symbolCompressionTransform symbolTT = ((FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol]; + ushort* stateTable = (ushort*)(statePtr->stateTable); + uint nbBitsOut = (uint)((symbolTT.deltaNbBits + (uint)((1 << 15))) >> 16); + + statePtr->value = (nint)((nbBitsOut << 16) - symbolTT.deltaNbBits); + statePtr->value = (nint)(stateTable[(statePtr->value >> (int)nbBitsOut) + symbolTT.deltaFindState]); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, uint symbol) + { + FSE_symbolCompressionTransform symbolTT = ((FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol]; + ushort* stateTable = (ushort*)(statePtr->stateTable); + uint nbBitsOut = (uint)(((nuint)statePtr->value + symbolTT.deltaNbBits) >> 16); + + BIT_addBits(bitC, (nuint)statePtr->value, nbBitsOut); + statePtr->value = (nint)(stateTable[(statePtr->value >> (int)nbBitsOut) + symbolTT.deltaFindState]); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void FSE_flushCState(BIT_CStream_t* bitC, FSE_CState_t* statePtr) + { + BIT_addBits(bitC, (nuint)statePtr->value, statePtr->stateLog); + BIT_flushBits(bitC); + } + + /* FSE_getMaxNbBits() : + * Approximate maximum cost of a symbol, in bits. + * Fractional get rounded up (i.e : a symbol with a normalized frequency of 3 gives the same result as a frequency of 2) + * note 1 : assume symbolValue is valid (<= maxSymbolValue) + * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint FSE_getMaxNbBits(void* symbolTTPtr, uint symbolValue) + { + FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*)(symbolTTPtr); + + return (symbolTT[symbolValue].deltaNbBits + (uint)(((1 << 16) - 1))) >> 16; + } + + /* FSE_bitCost() : + * Approximate symbol cost, as fractional value, using fixed-point format (accuracyLog fractional bits) + * note 1 : assume symbolValue is valid (<= maxSymbolValue) + * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint FSE_bitCost(void* symbolTTPtr, uint tableLog, uint symbolValue, uint accuracyLog) + { + FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*)(symbolTTPtr); + uint minNbBits = symbolTT[symbolValue].deltaNbBits >> 16; + uint threshold = (minNbBits + 1) << 16; + + assert(tableLog < 16); + assert(accuracyLog < 31 - tableLog); + + { + uint tableSize = (uint)(1 << (int)tableLog); + uint deltaFromThreshold = threshold - (symbolTT[symbolValue].deltaNbBits + tableSize); + uint normalizedDeltaFromThreshold = (deltaFromThreshold << (int)accuracyLog) >> (int)tableLog; + uint bitMultiplier = (uint)(1 << (int)accuracyLog); + + assert(symbolTT[symbolValue].deltaNbBits + tableSize <= threshold); + assert(normalizedDeltaFromThreshold <= bitMultiplier); + return (minNbBits + 1) * bitMultiplier - normalizedDeltaFromThreshold; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, uint* dt) + { + void* ptr = (void*)dt; + FSE_DTableHeader* DTableH = (FSE_DTableHeader*)(ptr); + + DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog); + BIT_reloadDStream(bitD); + DStatePtr->table = dt + 1; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static byte FSE_peekSymbol(FSE_DState_t* DStatePtr) + { + FSE_decode_t DInfo = ((FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; + + return DInfo.symbol; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void FSE_updateState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) + { + FSE_decode_t DInfo = ((FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; + uint nbBits = DInfo.nbBits; + nuint lowBits = BIT_readBits(bitD, nbBits); + + DStatePtr->state = DInfo.newState + lowBits; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static byte FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) + { + FSE_decode_t DInfo = ((FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; + uint nbBits = DInfo.nbBits; + byte symbol = DInfo.symbol; + nuint lowBits = BIT_readBits(bitD, nbBits); + + DStatePtr->state = DInfo.newState + lowBits; + return symbol; + } + + /*! FSE_decodeSymbolFast() : + unsafe, only works if no symbol has a probability > 50% */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static byte FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) + { + FSE_decode_t DInfo = ((FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; + uint nbBits = DInfo.nbBits; + byte symbol = DInfo.symbol; + nuint lowBits = BIT_readBitsFast(bitD, nbBits); + + DStatePtr->state = DInfo.newState + lowBits; + return symbol; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint FSE_endOfDState(FSE_DState_t* DStatePtr) + { + return ((DStatePtr->state == 0) ? 1U : 0U); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FseCompress.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FseCompress.cs new file mode 100644 index 00000000..f87fd237 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FseCompress.cs @@ -0,0 +1,960 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /* FSE_buildCTable_wksp() : + * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`). + * wkspSize should be sized to handle worst case situation, which is `1<> 1 : 1)); + FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*)(FSCT); + uint step = (((tableSize) >> 1) + ((tableSize) >> 3) + 3); + uint* cumul = (uint*)(workSpace); + byte* tableSymbol = (byte*)(cumul + (maxSymbolValue + 2)); + uint highThreshold = tableSize - 1; + + if (((nuint)(workSpace) & 3) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (((nuint)(sizeof(uint)) * (maxSymbolValue + 2 + (1UL << (int)(tableLog - 2)))) > wkspSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + tableU16[-2] = (ushort)(tableLog); + tableU16[-1] = (ushort)(maxSymbolValue); + assert(tableLog < 16); + + { + uint u; + + cumul[0] = 0; + for (u = 1; u <= maxSymbolValue + 1; u++) + { + if (normalizedCounter[u - 1] == -1) + { + cumul[u] = cumul[u - 1] + 1; + tableSymbol[highThreshold--] = (byte)(u - 1); + } + else + { + cumul[u] = cumul[u - 1] + (ushort)(normalizedCounter[u - 1]); + } + } + + cumul[maxSymbolValue + 1] = tableSize + 1; + } + + + { + uint position = 0; + uint symbol; + + for (symbol = 0; symbol <= maxSymbolValue; symbol++) + { + int nbOccurrences; + int freq = normalizedCounter[symbol]; + + for (nbOccurrences = 0; nbOccurrences < freq; nbOccurrences++) + { + tableSymbol[position] = (byte)(symbol); + position = (position + step) & tableMask; + while (position > highThreshold) + { + position = (position + step) & tableMask; + } + } + } + + assert(position == 0); + } + + + { + uint u; + + for (u = 0; u < tableSize; u++) + { + byte s = tableSymbol[u]; + + tableU16[cumul[s]++] = (ushort)(tableSize + u); + } + } + + + { + uint total = 0; + uint s; + + for (s = 0; s <= maxSymbolValue; s++) + { + switch (normalizedCounter[s]) + { + case 0: + { + symbolTT[s].deltaNbBits = ((tableLog + 1) << 16) - (uint)((1 << (int)tableLog)); + } + + break; + case -1: + case 1: + { + symbolTT[s].deltaNbBits = (tableLog << 16) - (uint)((1 << (int)tableLog)); + } + + symbolTT[s].deltaFindState = (int)(total - 1); + total++; + break; + default: + { + uint maxBitsOut = tableLog - BIT_highbit32((uint)(normalizedCounter[s] - 1)); + uint minStatePlus = (uint)(normalizedCounter[s] << (int)maxBitsOut); + + symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus; + symbolTT[s].deltaFindState = (int)(total - (ushort)(normalizedCounter[s])); + total += (uint)(normalizedCounter[s]); + } + break; + } + } + } + + return 0; + } + + /*! FSE_buildCTable(): + Builds `ct`, which must be already allocated, using FSE_createCTable(). + @return : 0, or an errorCode, which can be tested using FSE_isError() */ + public static nuint FSE_buildCTable(uint* ct, short* normalizedCounter, uint maxSymbolValue, uint tableLog) + { + byte* tableSymbol = stackalloc byte[4096]; + + return FSE_buildCTable_wksp(ct, normalizedCounter, maxSymbolValue, tableLog, (void*)tableSymbol, (nuint)(sizeof(byte) * 4096)); + } + + /*-************************************************************** + * FSE NCount encoding + ****************************************************************/ + public static nuint FSE_NCountWriteBound(uint maxSymbolValue, uint tableLog) + { + nuint maxHeaderSize = (((maxSymbolValue + 1) * tableLog) >> 3) + 3; + + return maxSymbolValue != 0 ? maxHeaderSize : 512; + } + + private static nuint FSE_writeNCount_generic(void* header, nuint headerBufferSize, short* normalizedCounter, uint maxSymbolValue, uint tableLog, uint writeIsSafe) + { + byte* ostart = (byte*)(header); + byte* @out = ostart; + byte* oend = ostart + headerBufferSize; + int nbBits; + int tableSize = 1 << (int)tableLog; + int remaining; + int threshold; + uint bitStream = 0; + int bitCount = 0; + uint symbol = 0; + uint alphabetSize = maxSymbolValue + 1; + int previousIs0 = 0; + + bitStream += (tableLog - 5) << bitCount; + bitCount += 4; + remaining = tableSize + 1; + threshold = tableSize; + nbBits = (int)(tableLog + 1); + while ((symbol < alphabetSize) && (remaining > 1)) + { + if (previousIs0 != 0) + { + uint start = symbol; + + while ((symbol < alphabetSize) && (normalizedCounter[symbol]) == 0) + { + symbol++; + } + + if (symbol == alphabetSize) + { + break; + } + + while (symbol >= start + 24) + { + start += 24; + bitStream += 0xFFFFU << bitCount; + if (writeIsSafe == 0 && (@out > oend - 2)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + @out[0] = (byte)(bitStream); + @out[1] = (byte)(bitStream >> 8); + @out += 2; + bitStream >>= 16; + } + + while (symbol >= start + 3) + { + start += 3; + bitStream += (uint)(3 << bitCount); + bitCount += 2; + } + + bitStream += (symbol - start) << bitCount; + bitCount += 2; + if (bitCount > 16) + { + if (writeIsSafe == 0 && (@out > oend - 2)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + @out[0] = (byte)(bitStream); + @out[1] = (byte)(bitStream >> 8); + @out += 2; + bitStream >>= 16; + bitCount -= 16; + } + } + + + { + int count = normalizedCounter[symbol++]; + int max = (2 * threshold - 1) - remaining; + + remaining -= count < 0 ? -count : count; + count++; + if (count >= threshold) + { + count += max; + } + + bitStream += (uint)(count << bitCount); + bitCount += nbBits; + bitCount -= ((count < max) ? 1 : 0); + previousIs0 = ((count == 1) ? 1 : 0); + if (remaining < 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + while (remaining < threshold) + { + nbBits--; + threshold >>= 1; + } + } + + if (bitCount > 16) + { + if (writeIsSafe == 0 && (@out > oend - 2)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + @out[0] = (byte)(bitStream); + @out[1] = (byte)(bitStream >> 8); + @out += 2; + bitStream >>= 16; + bitCount -= 16; + } + } + + if (remaining != 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + assert(symbol <= alphabetSize); + if (writeIsSafe == 0 && (@out > oend - 2)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + @out[0] = (byte)(bitStream); + @out[1] = (byte)(bitStream >> 8); + @out += (bitCount + 7) / 8; + return (nuint)((@out - ostart)); + } + + /*! FSE_writeNCount(): + Compactly save 'normalizedCounter' into 'buffer'. + @return : size of the compressed table, + or an errorCode, which can be tested using FSE_isError(). */ + public static nuint FSE_writeNCount(void* buffer, nuint bufferSize, short* normalizedCounter, uint maxSymbolValue, uint tableLog) + { + if (tableLog > (uint)((14 - 2))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + if (tableLog < 5) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog)) + { + return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0); + } + + return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1); + } + + /*-************************************************************** + * FSE Compression Code + ****************************************************************/ + public static uint* FSE_createCTable(uint maxSymbolValue, uint tableLog) + { + nuint size; + + if (tableLog > 15) + { + tableLog = 15; + } + + size = ((uint)(1 + (1 << (int)((tableLog) - 1))) + (((maxSymbolValue) + 1) * 2)) * (nuint)(sizeof(uint)); + return (uint*)(malloc(size)); + } + + public static void FSE_freeCTable(uint* ct) + { + free((void*)(ct)); + } + + /* provides the minimum logSize to safely represent a distribution */ + [InlineMethod.Inline] + private static uint FSE_minTableLog(nuint srcSize, uint maxSymbolValue) + { + uint minBitsSrc = BIT_highbit32((uint)(srcSize)) + 1; + uint minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2; + uint minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols; + + assert(srcSize > 1); + return minBits; + } + + /* ***************************************** + * FSE advanced API + ***************************************** */ + public static uint FSE_optimalTableLog_internal(uint maxTableLog, nuint srcSize, uint maxSymbolValue, uint minus) + { + uint maxBitsSrc = BIT_highbit32((uint)(srcSize - 1)) - minus; + uint tableLog = maxTableLog; + uint minBits = FSE_minTableLog(srcSize, maxSymbolValue); + + assert(srcSize > 1); + if (tableLog == 0) + { + tableLog = (uint)((13 - 2)); + } + + if (maxBitsSrc < tableLog) + { + tableLog = maxBitsSrc; + } + + if (minBits > tableLog) + { + tableLog = minBits; + } + + if (tableLog < 5) + { + tableLog = 5; + } + + if (tableLog > (uint)((14 - 2))) + { + tableLog = (uint)((14 - 2)); + } + + return tableLog; + } + + /*! FSE_optimalTableLog(): + dynamically downsize 'tableLog' when conditions are met. + It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. + @return : recommended tableLog (necessarily <= 'maxTableLog') */ + public static uint FSE_optimalTableLog(uint maxTableLog, nuint srcSize, uint maxSymbolValue) + { + return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2); + } + + /* Secondary normalization method. + To be used when primary method fails. */ + private static nuint FSE_normalizeM2(short* norm, uint tableLog, uint* count, nuint total, uint maxSymbolValue, short lowProbCount) + { + short NOT_YET_ASSIGNED = (short)-2; + uint s; + uint distributed = 0; + uint ToDistribute; + uint lowThreshold = (uint)(total >> (int)tableLog); + uint lowOne = (uint)((total * 3) >> (int)(tableLog + 1)); + + for (s = 0; s <= maxSymbolValue; s++) + { + if (count[s] == 0) + { + norm[s] = 0; + continue; + } + + if (count[s] <= lowThreshold) + { + norm[s] = lowProbCount; + distributed++; + total -= count[s]; + continue; + } + + if (count[s] <= lowOne) + { + norm[s] = 1; + distributed++; + total -= count[s]; + continue; + } + + norm[s] = NOT_YET_ASSIGNED; + } + + ToDistribute = (uint)((1 << (int)tableLog)) - distributed; + if (ToDistribute == 0) + { + return 0; + } + + if ((total / ToDistribute) > lowOne) + { + lowOne = (uint)((total * 3) / (ToDistribute * 2)); + for (s = 0; s <= maxSymbolValue; s++) + { + if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) + { + norm[s] = 1; + distributed++; + total -= count[s]; + continue; + } + } + + ToDistribute = (uint)((1 << (int)tableLog)) - distributed; + } + + if (distributed == maxSymbolValue + 1) + { + uint maxV = 0, maxC = 0; + + for (s = 0; s <= maxSymbolValue; s++) + { + if (count[s] > maxC) + { + maxV = s; + maxC = count[s]; + } + } + + norm[maxV] += (short)(short)(ToDistribute); + return 0; + } + + if (total == 0) + { + for (s = 0; ToDistribute > 0; s = (s + 1) % (maxSymbolValue + 1)) + { + if (norm[s] > 0) + { + ToDistribute--; + norm[s]++; + } + } + + return 0; + } + + + { + ulong vStepLog = 62 - tableLog; + ulong mid = (1UL << (int)(vStepLog - 1)) - 1; + ulong rStep = (((((ulong)(1) << (int)vStepLog) * ToDistribute) + mid) / ((uint)(total))); + ulong tmpTotal = mid; + + for (s = 0; s <= maxSymbolValue; s++) + { + if (norm[s] == NOT_YET_ASSIGNED) + { + ulong end = tmpTotal + (count[s] * rStep); + uint sStart = (uint)(tmpTotal >> (int)vStepLog); + uint sEnd = (uint)(end >> (int)vStepLog); + uint weight = sEnd - sStart; + + if (weight < 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + norm[s] = (short)(weight); + tmpTotal = end; + } + } + } + + return 0; + } + + /*! FSE_normalizeCount(): + normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) + 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). + useLowProbCount is a boolean parameter which trades off compressed size for + faster header decoding. When it is set to 1, the compressed data will be slightly + smaller. And when it is set to 0, FSE_readNCount() and FSE_buildDTable() will be + faster. If you are compressing a small amount of data (< 2 KB) then useLowProbCount=0 + is a good default, since header deserialization makes a big speed difference. + Otherwise, useLowProbCount=1 is a good default, since the speed difference is small. + @return : tableLog, + or an errorCode, which can be tested using FSE_isError() */ + public static nuint FSE_normalizeCount(short* normalizedCounter, uint tableLog, uint* count, nuint total, uint maxSymbolValue, uint useLowProbCount) + { + if (tableLog == 0) + { + tableLog = (uint)((13 - 2)); + } + + if (tableLog < 5) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (tableLog > (uint)((14 - 2))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + if (tableLog < FSE_minTableLog(total, maxSymbolValue)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + + { + + short lowProbCount = (short)(useLowProbCount != 0 ? -1 : 1); + ulong scale = 62 - tableLog; + ulong step = (((ulong)(1) << 62) / ((uint)(total))); + ulong vStep = 1UL << (int)(scale - 20); + int stillToDistribute = 1 << (int)tableLog; + uint s; + uint largest = 0; + short largestP = 0; + uint lowThreshold = (uint)(total >> (int)tableLog); + + for (s = 0; s <= maxSymbolValue; s++) + { + if (count[s] == total) + { + return 0; + } + + if (count[s] == 0) + { + normalizedCounter[s] = 0; + continue; + } + + if (count[s] <= lowThreshold) + { + normalizedCounter[s] = lowProbCount; + stillToDistribute--; + } + else + { + short proba = (short)((count[s] * step) >> (int)scale); + + if (proba < 8) + { + ulong restToBeat = vStep * rtbTable[proba]; + + proba += (short)((((count[s] * step) - ((ulong)(proba) << (int)scale) > restToBeat) ? 1 : 0)); + } + + if (proba > largestP) + { + largestP = proba; + largest = s; + } + + normalizedCounter[s] = proba; + stillToDistribute -= proba; + } + } + + if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) + { + nuint errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue, lowProbCount); + + if ((ERR_isError(errorCode)) != 0) + { + return errorCode; + } + } + else + { + normalizedCounter[largest] += (short)(short)(stillToDistribute); + } + } + + return tableLog; + } + + /* fake FSE_CTable, for raw (uncompressed) input */ + public static nuint FSE_buildCTable_raw(uint* ct, uint nbBits) + { + uint tableSize = (uint)(1 << (int)nbBits); + uint tableMask = tableSize - 1; + uint maxSymbolValue = tableMask; + void* ptr = (void*)ct; + ushort* tableU16 = ((ushort*)(ptr)) + 2; + void* FSCT = (void*)(((uint*)(ptr)) + 1 + (tableSize >> 1)); + FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*)(FSCT); + uint s; + + if (nbBits < 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + tableU16[-2] = (ushort)(nbBits); + tableU16[-1] = (ushort)(maxSymbolValue); + for (s = 0; s < tableSize; s++) + { + tableU16[s] = (ushort)(tableSize + s); + } + + + { + uint deltaNbBits = (nbBits << 16) - (uint)((1 << (int)nbBits)); + + for (s = 0; s <= maxSymbolValue; s++) + { + symbolTT[s].deltaNbBits = deltaNbBits; + symbolTT[s].deltaFindState = (int)(s - 1); + } + } + + return 0; + } + + /* fake FSE_CTable, for rle input (always same symbol) */ + public static nuint FSE_buildCTable_rle(uint* ct, byte symbolValue) + { + void* ptr = (void*)ct; + ushort* tableU16 = ((ushort*)(ptr)) + 2; + void* FSCTptr = (void*)((uint*)(ptr) + 2); + FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*)(FSCTptr); + + tableU16[-2] = (ushort)(0); + tableU16[-1] = (ushort)(symbolValue); + tableU16[0] = 0; + tableU16[1] = 0; + symbolTT[symbolValue].deltaNbBits = 0; + symbolTT[symbolValue].deltaFindState = 0; + return 0; + } + + private static nuint FSE_compress_usingCTable_generic(void* dst, nuint dstSize, void* src, nuint srcSize, uint* ct, uint fast) + { + byte* istart = (byte*)(src); + byte* iend = istart + srcSize; + byte* ip = iend; + BIT_CStream_t bitC; + FSE_CState_t CState1, CState2; + + if (srcSize <= 2) + { + return 0; + } + + + { + nuint initError = BIT_initCStream(&bitC, dst, dstSize); + + if ((ERR_isError(initError)) != 0) + { + return 0; + } + } + + if ((srcSize & 1) != 0) + { + FSE_initCState2(&CState1, ct, *--ip); + FSE_initCState2(&CState2, ct, *--ip); + FSE_encodeSymbol(&bitC, &CState1, *--ip); + if (fast != 0) + { + BIT_flushBitsFast(&bitC); + } + else + { + BIT_flushBits(&bitC); + } + + } + else + { + FSE_initCState2(&CState2, ct, *--ip); + FSE_initCState2(&CState1, ct, *--ip); + } + + srcSize -= 2; + if (((nuint)(sizeof(nuint)) * 8 > (uint)((14 - 2) * 4 + 7)) && (srcSize & 2) != 0) + { + FSE_encodeSymbol(&bitC, &CState2, *--ip); + FSE_encodeSymbol(&bitC, &CState1, *--ip); + if (fast != 0) + { + BIT_flushBitsFast(&bitC); + } + else + { + BIT_flushBits(&bitC); + } + + } + + while (ip > istart) + { + FSE_encodeSymbol(&bitC, &CState2, *--ip); + if ((nuint)(sizeof(nuint)) * 8 < (uint)((14 - 2) * 2 + 7)) + { + if (fast != 0) + { + BIT_flushBitsFast(&bitC); + } + else + { + BIT_flushBits(&bitC); + } + } + + FSE_encodeSymbol(&bitC, &CState1, *--ip); + if ((nuint)(sizeof(nuint)) * 8 > (uint)((14 - 2) * 4 + 7)) + { + FSE_encodeSymbol(&bitC, &CState2, *--ip); + FSE_encodeSymbol(&bitC, &CState1, *--ip); + } + + if (fast != 0) + { + BIT_flushBitsFast(&bitC); + } + else + { + BIT_flushBits(&bitC); + } + + } + + FSE_flushCState(&bitC, &CState2); + FSE_flushCState(&bitC, &CState1); + return BIT_closeCStream(&bitC); + } + + /*! FSE_compress_usingCTable(): + Compress `src` using `ct` into `dst` which must be already allocated. + @return : size of compressed data (<= `dstCapacity`), + or 0 if compressed data could not fit into `dst`, + or an errorCode, which can be tested using FSE_isError() */ + public static nuint FSE_compress_usingCTable(void* dst, nuint dstSize, void* src, nuint srcSize, uint* ct) + { + uint fast = (((dstSize >= ((srcSize) + ((srcSize) >> 7) + 4 + (nuint)(sizeof(nuint))))) ? 1U : 0U); + + if (fast != 0) + { + return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1); + } + else + { + return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0); + } + } + + /*-***************************************** + * Tool functions + ******************************************/ + public static nuint FSE_compressBound(nuint size) + { + return (512 + ((size) + ((size) >> 7) + 4 + (nuint)(sizeof(nuint)))); + } + + /* FSE_compress_wksp() : + * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`). + * `wkspSize` size must be `(1< 12) ? (1 << (int)(tableLog - 2)) : 1024)))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + if (srcSize <= 1) + { + return 0; + } + + if (maxSymbolValue == 0) + { + maxSymbolValue = 255; + } + + if (tableLog == 0) + { + tableLog = (uint)((13 - 2)); + } + + + { + nuint maxCount = HIST_count_wksp((uint*)count, &maxSymbolValue, src, srcSize, scratchBuffer, scratchBufferSize); + + if ((ERR_isError(maxCount)) != 0) + { + return maxCount; + } + + if (maxCount == srcSize) + { + return 1; + } + + if (maxCount == 1) + { + return 0; + } + + if (maxCount < (srcSize >> 7)) + { + return 0; + } + } + + tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue); + + { + nuint _var_err__ = FSE_normalizeCount((short*)norm, tableLog, (uint*)count, srcSize, maxSymbolValue, ((srcSize >= 2048) ? 1U : 0U)); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint nc_err = FSE_writeNCount((void*)op, (nuint)(oend - op), (short*)norm, maxSymbolValue, tableLog); + + if ((ERR_isError(nc_err)) != 0) + { + return nc_err; + } + + op += nc_err; + } + + + { + nuint _var_err__ = FSE_buildCTable_wksp(CTable, (short*)norm, maxSymbolValue, tableLog, scratchBuffer, scratchBufferSize); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint cSize = FSE_compress_usingCTable((void*)op, (nuint)(oend - op), src, srcSize, CTable); + + if ((ERR_isError(cSize)) != 0) + { + return cSize; + } + + if (cSize == 0) + { + return 0; + } + + op += cSize; + } + + if ((nuint)(op - ostart) >= srcSize - 1) + { + return 0; + } + + return (nuint)(op - ostart); + } + + /*-***************************************** + * FSE advanced functions + ******************************************/ + /*! FSE_compress2() : + Same as FSE_compress(), but allows the selection of 'maxSymbolValue' and 'tableLog' + Both parameters can be defined as '0' to mean : use default value + @return : size of compressed data + Special values : if return == 0, srcData is not compressible => Nothing is stored within cSrc !!! + if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression. + if FSE_isError(return), it's an error code. + */ + public static nuint FSE_compress2(void* dst, nuint dstCapacity, void* src, nuint srcSize, uint maxSymbolValue, uint tableLog) + { + fseWkspMax_t scratchBuffer; + + if (tableLog > (uint)((14 - 2))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + return FSE_compress_wksp(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog, (void*)&scratchBuffer, (nuint)(sizeof(fseWkspMax_t))); + } + + /*-**************************************** + * FSE simple functions + ******************************************/ + /*! FSE_compress() : + Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'. + 'dst' buffer must be already allocated. Compression runs faster is dstCapacity >= FSE_compressBound(srcSize). + @return : size of compressed data (<= dstCapacity). + Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! + if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead. + if FSE_isError(return), compression failed (more details using FSE_getErrorName()) + */ + public static nuint FSE_compress(void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + return FSE_compress2(dst, dstCapacity, src, srcSize, 255, (uint)((13 - 2))); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/FseDecompress.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/FseDecompress.cs new file mode 100644 index 00000000..4eb3f471 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/FseDecompress.cs @@ -0,0 +1,446 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /* Function templates */ + public static uint* FSE_createDTable(uint tableLog) + { + if (tableLog > 15) + { + tableLog = 15; + } + + return (uint*)(malloc((uint)((1 + (1 << (int)(tableLog)))) * (nuint)(sizeof(uint)))); + } + + public static void FSE_freeDTable(uint* dt) + { + free((void*)(dt)); + } + + private static nuint FSE_buildDTable_internal(uint* dt, short* normalizedCounter, uint maxSymbolValue, uint tableLog, void* workSpace, nuint wkspSize) + { + void* tdPtr = (void*)(dt + 1); + FSE_decode_t* tableDecode = (FSE_decode_t*)(tdPtr); + ushort* symbolNext = (ushort*)(workSpace); + byte* spread = (byte*)(symbolNext + maxSymbolValue + 1); + uint maxSV1 = maxSymbolValue + 1; + uint tableSize = (uint)(1 << (int)tableLog); + uint highThreshold = tableSize - 1; + + if (((nuint)(sizeof(short)) * (maxSymbolValue + 1) + (1UL << (int)tableLog) + 8) > wkspSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooLarge))); + } + + if (maxSymbolValue > 255) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooLarge))); + } + + if (tableLog > (uint)((14 - 2))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + + { + FSE_DTableHeader DTableH; + + DTableH.tableLog = (ushort)(tableLog); + DTableH.fastMode = 1; + + { + short largeLimit = (short)(1 << (int)(tableLog - 1)); + uint s; + + for (s = 0; s < maxSV1; s++) + { + if (normalizedCounter[s] == -1) + { + tableDecode[highThreshold--].symbol = (byte)(s); + symbolNext[s] = 1; + } + else + { + if (normalizedCounter[s] >= largeLimit) + { + DTableH.fastMode = 0; + } + + symbolNext[s] = (ushort)(normalizedCounter[s]); + } + } + } + + memcpy((void*)(dt), (void*)(&DTableH), ((nuint)(sizeof(FSE_DTableHeader)))); + } + + if (highThreshold == tableSize - 1) + { + nuint tableMask = tableSize - 1; + nuint step = (((tableSize) >> 1) + ((tableSize) >> 3) + 3); + + + { + ulong add = 0x0101010101010101UL; + nuint pos = 0; + ulong sv = 0; + uint s; + + for (s = 0; s < maxSV1; ++s , sv += add) + { + int i; + int n = normalizedCounter[s]; + + MEM_write64((void*)(spread + pos), sv); + for (i = 8; i < n; i += 8) + { + MEM_write64((void*)(spread + pos + i), sv); + } + + pos += (nuint)n; + } + } + + + { + nuint position = 0; + nuint s; + nuint unroll = 2; + + assert(tableSize % unroll == 0); + for (s = 0; s < (nuint)(tableSize); s += unroll) + { + nuint u; + + for (u = 0; u < unroll; ++u) + { + nuint uPosition = (position + (u * step)) & tableMask; + + tableDecode[uPosition].symbol = spread[s + u]; + } + + position = (position + (unroll * step)) & tableMask; + } + + assert(position == 0); + } + } + else + { + uint tableMask = tableSize - 1; + uint step = (((tableSize) >> 1) + ((tableSize) >> 3) + 3); + uint s, position = 0; + + for (s = 0; s < maxSV1; s++) + { + int i; + + for (i = 0; i < normalizedCounter[s]; i++) + { + tableDecode[position].symbol = (byte)(s); + position = (position + step) & tableMask; + while (position > highThreshold) + { + position = (position + step) & tableMask; + } + } + } + + if (position != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + } + + + { + uint u; + + for (u = 0; u < tableSize; u++) + { + byte symbol = (byte)(tableDecode[u].symbol); + uint nextState = symbolNext[symbol]++; + + tableDecode[u].nbBits = (byte)(tableLog - BIT_highbit32(nextState)); + tableDecode[u].newState = (ushort)((nextState << (int)(tableDecode[u].nbBits)) - tableSize); + } + } + + return 0; + } + + public static nuint FSE_buildDTable_wksp(uint* dt, short* normalizedCounter, uint maxSymbolValue, uint tableLog, void* workSpace, nuint wkspSize) + { + return FSE_buildDTable_internal(dt, normalizedCounter, maxSymbolValue, tableLog, workSpace, wkspSize); + } + + /*-******************************************************* + * Decompression (Byte symbols) + *********************************************************/ + public static nuint FSE_buildDTable_rle(uint* dt, byte symbolValue) + { + void* ptr = (void*)dt; + FSE_DTableHeader* DTableH = (FSE_DTableHeader*)(ptr); + void* dPtr = (void*)(dt + 1); + FSE_decode_t* cell = (FSE_decode_t*)(dPtr); + + DTableH->tableLog = 0; + DTableH->fastMode = 0; + cell->newState = 0; + cell->symbol = symbolValue; + cell->nbBits = 0; + return 0; + } + + public static nuint FSE_buildDTable_raw(uint* dt, uint nbBits) + { + void* ptr = (void*)dt; + FSE_DTableHeader* DTableH = (FSE_DTableHeader*)(ptr); + void* dPtr = (void*)(dt + 1); + FSE_decode_t* dinfo = (FSE_decode_t*)(dPtr); + uint tableSize = (uint)(1 << (int)nbBits); + uint tableMask = tableSize - 1; + uint maxSV1 = tableMask + 1; + uint s; + + if (nbBits < 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + DTableH->tableLog = (ushort)(nbBits); + DTableH->fastMode = 1; + for (s = 0; s < maxSV1; s++) + { + dinfo[s].newState = 0; + dinfo[s].symbol = (byte)(s); + dinfo[s].nbBits = (byte)(nbBits); + } + + return 0; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint FSE_decompress_usingDTable_generic(void* dst, nuint maxDstSize, void* cSrc, nuint cSrcSize, uint* dt, uint fast) + { + byte* ostart = (byte*)(dst); + byte* op = ostart; + byte* omax = op + maxDstSize; + byte* olimit = omax - 3; + BIT_DStream_t bitD; + FSE_DState_t state1; + FSE_DState_t state2; + + + { + nuint _var_err__ = BIT_initDStream(&bitD, cSrc, cSrcSize); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + FSE_initDState(&state1, &bitD, dt); + FSE_initDState(&state2, &bitD, dt); + for (; ((BIT_reloadDStream(&bitD) == BIT_DStream_status.BIT_DStream_unfinished) && (op < olimit)); op += 4) + { + op[0] = fast != 0 ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD); + if ((uint)((14 - 2) * 2 + 7) > (nuint)(sizeof(nuint)) * 8) + { + BIT_reloadDStream(&bitD); + } + + op[1] = fast != 0 ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD); + if ((uint)((14 - 2) * 4 + 7) > (nuint)(sizeof(nuint)) * 8) + { + if (BIT_reloadDStream(&bitD) > BIT_DStream_status.BIT_DStream_unfinished) + { + op += 2; + break; + } + } + + op[2] = fast != 0 ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD); + if ((uint)((14 - 2) * 2 + 7) > (nuint)(sizeof(nuint)) * 8) + { + BIT_reloadDStream(&bitD); + } + + op[3] = fast != 0 ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD); + } + + while (1 != 0) + { + if (op > (omax - 2)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + *op++ = fast != 0 ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD); + if (BIT_reloadDStream(&bitD) == BIT_DStream_status.BIT_DStream_overflow) + { + *op++ = fast != 0 ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD); + break; + } + + if (op > (omax - 2)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + *op++ = fast != 0 ? FSE_decodeSymbolFast(&state2, &bitD) : FSE_decodeSymbol(&state2, &bitD); + if (BIT_reloadDStream(&bitD) == BIT_DStream_status.BIT_DStream_overflow) + { + *op++ = fast != 0 ? FSE_decodeSymbolFast(&state1, &bitD) : FSE_decodeSymbol(&state1, &bitD); + break; + } + } + + return (nuint)(op - ostart); + } + + /*! FSE_decompress_usingDTable(): + Decompress compressed source `cSrc` of size `cSrcSize` using `dt` + into `dst` which must be already allocated. + @return : size of regenerated data (necessarily <= `dstCapacity`), + or an errorCode, which can be tested using FSE_isError() */ + public static nuint FSE_decompress_usingDTable(void* dst, nuint originalSize, void* cSrc, nuint cSrcSize, uint* dt) + { + void* ptr = (void*)dt; + FSE_DTableHeader* DTableH = (FSE_DTableHeader*)(ptr); + uint fastMode = DTableH->fastMode; + + if (fastMode != 0) + { + return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1); + } + + return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0); + } + + public static nuint FSE_decompress_wksp(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize, uint maxLog, void* workSpace, nuint wkspSize) + { + return FSE_decompress_wksp_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint FSE_decompress_wksp_body(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize, uint maxLog, void* workSpace, nuint wkspSize, int bmi2) + { + byte* istart = (byte*)(cSrc); + byte* ip = istart; + uint tableLog; + uint maxSymbolValue = 255; + FSE_DecompressWksp* wksp = (FSE_DecompressWksp*)(workSpace); + + if (wkspSize < (nuint)(sizeof(FSE_DecompressWksp))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + + { + nuint NCountLength = FSE_readNCount_bmi2((short*)wksp->ncount, &maxSymbolValue, &tableLog, (void*)istart, cSrcSize, bmi2); + + if ((ERR_isError(NCountLength)) != 0) + { + return NCountLength; + } + + if (tableLog > maxLog) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + assert(NCountLength <= cSrcSize); + ip += NCountLength; + cSrcSize -= NCountLength; + } + + if ((((uint)((1 + (1 << (int)(tableLog)))) + ((((nuint)(sizeof(short)) * (maxSymbolValue + 1) + (1UL << (int)tableLog) + 8) + (nuint)(sizeof(uint)) - 1) / (nuint)(sizeof(uint))) + (uint)((255 + 1) / 2) + 1) * (nuint)(sizeof(uint))) > wkspSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + workSpace = wksp->dtable + (1 + (1 << (int)(tableLog))); + wkspSize -= (nuint)(sizeof(FSE_DecompressWksp)) + ((uint)((1 + (1 << (int)(tableLog)))) * (nuint)(sizeof(uint))); + + { + nuint _var_err__ = FSE_buildDTable_internal((uint*)wksp->dtable, (short*)wksp->ncount, maxSymbolValue, tableLog, workSpace, wkspSize); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + void* ptr = (void*)wksp->dtable; + FSE_DTableHeader* DTableH = (FSE_DTableHeader*)(ptr); + uint fastMode = DTableH->fastMode; + + if (fastMode != 0) + { + return FSE_decompress_usingDTable_generic(dst, dstCapacity, (void*)ip, cSrcSize, (uint*)wksp->dtable, 1); + } + + return FSE_decompress_usingDTable_generic(dst, dstCapacity, (void*)ip, cSrcSize, (uint*)wksp->dtable, 0); + } + } + + /* Avoids the FORCE_INLINE of the _body() function. */ + private static nuint FSE_decompress_wksp_body_default(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize, uint maxLog, void* workSpace, nuint wkspSize) + { + return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0); + } + + private static nuint FSE_decompress_wksp_body_bmi2(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize, uint maxLog, void* workSpace, nuint wkspSize) + { + return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1); + } + + public static nuint FSE_decompress_wksp_bmi2(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize, uint maxLog, void* workSpace, nuint wkspSize, int bmi2) + { + if (bmi2 != 0) + { + return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize); + } + + return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize); + } + + /*! FSE_buildDTable(): + Builds 'dt', which must be already allocated, using FSE_createDTable(). + return : 0, or an errorCode, which can be tested using FSE_isError() */ + public static nuint FSE_buildDTable(uint* dt, short* normalizedCounter, uint maxSymbolValue, uint tableLog) + { + uint* wksp = stackalloc uint[8322]; + + return FSE_buildDTable_wksp(dt, normalizedCounter, maxSymbolValue, tableLog, (void*)wksp, (nuint)(sizeof(uint) * 8322)); + } + + /*! FSE_decompress(): + Decompress FSE data from buffer 'cSrc', of size 'cSrcSize', + into already allocated destination buffer 'dst', of size 'dstCapacity'. + @return : size of regenerated data (<= maxDstSize), + or an error code, which can be tested using FSE_isError() . + + ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!! + Why ? : making this distinction requires a header. + Header management is intentionally delegated to the user layer, which can better manage special cases. + */ + public static nuint FSE_decompress(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize) + { + uint* wksp = stackalloc uint[5380]; + + return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, (uint)((14 - 2)), (void*)wksp, (nuint)(sizeof(uint) * 5380)); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HIST_checkInput_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HIST_checkInput_e.cs new file mode 100644 index 00000000..c8a5ce96 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HIST_checkInput_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum HIST_checkInput_e + { + trustInput, + checkMaxSymbolValue, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_CElt_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_CElt_s.cs new file mode 100644 index 00000000..21e62160 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_CElt_s.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* static allocation of HUF's Compression Table */ + /* this is a private definition, just exposed for allocation and strict aliasing purpose. never EVER access its members directly */ + public partial struct HUF_CElt_s + { + public ushort val; + + public byte nbBits; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_CompressWeightsWksp.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_CompressWeightsWksp.cs new file mode 100644 index 00000000..15ff08f5 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_CompressWeightsWksp.cs @@ -0,0 +1,15 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct HUF_CompressWeightsWksp + { + public fixed uint CTable[59]; + + public fixed uint scratchBuffer[30]; + + public fixed uint count[13]; + + public fixed short norm[13]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_DEltX1.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_DEltX1.cs new file mode 100644 index 00000000..e0c47bc4 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_DEltX1.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*-***************************/ + /* single-symbol decoding */ + /*-***************************/ + public partial struct HUF_DEltX1 + { + /* single-symbol decoding */ + public byte @byte; + + /* single-symbol decoding */ + public byte nbBits; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_DEltX2.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_DEltX2.cs new file mode 100644 index 00000000..a4d5d471 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_DEltX2.cs @@ -0,0 +1,19 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* *************************/ + /* double-symbols decoding */ + /* *************************/ + public partial struct HUF_DEltX2 + { + /* double-symbols decoding */ + public ushort sequence; + + /* double-symbols decoding */ + public byte nbBits; + + /* double-symbols decoding */ + public byte length; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_ReadDTableX1_Workspace.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_ReadDTableX1_Workspace.cs new file mode 100644 index 00000000..74028de0 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_ReadDTableX1_Workspace.cs @@ -0,0 +1,17 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct HUF_ReadDTableX1_Workspace + { + public fixed uint rankVal[16]; + + public fixed uint rankStart[16]; + + public fixed uint statsWksp[218]; + + public fixed byte symbols[256]; + + public fixed byte huffWeight[256]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_ReadDTableX2_Workspace.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_ReadDTableX2_Workspace.cs new file mode 100644 index 00000000..99247774 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_ReadDTableX2_Workspace.cs @@ -0,0 +1,389 @@ +using InlineIL; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static InlineIL.IL.Emit; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct HUF_ReadDTableX2_Workspace + { + public _rankVal_e__FixedBuffer rankVal; + + public fixed uint rankStats[13]; + + public fixed uint rankStart0[14]; + + public _sortedSymbol_e__FixedBuffer sortedSymbol; + + public fixed byte weightList[256]; + + public fixed uint calleeWksp[218]; + + public unsafe partial struct _rankVal_e__FixedBuffer + { + public rankValCol_t e0; + public rankValCol_t e1; + public rankValCol_t e2; + public rankValCol_t e3; + public rankValCol_t e4; + public rankValCol_t e5; + public rankValCol_t e6; + public rankValCol_t e7; + public rankValCol_t e8; + public rankValCol_t e9; + public rankValCol_t e10; + public rankValCol_t e11; + + public ref rankValCol_t this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref rankValCol_t this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref rankValCol_t this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator rankValCol_t*(in _rankVal_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_rankVal_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static rankValCol_t* operator +(in _rankVal_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_rankVal_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + + public unsafe partial struct _sortedSymbol_e__FixedBuffer + { + public sortedSymbol_t e0; + public sortedSymbol_t e1; + public sortedSymbol_t e2; + public sortedSymbol_t e3; + public sortedSymbol_t e4; + public sortedSymbol_t e5; + public sortedSymbol_t e6; + public sortedSymbol_t e7; + public sortedSymbol_t e8; + public sortedSymbol_t e9; + public sortedSymbol_t e10; + public sortedSymbol_t e11; + public sortedSymbol_t e12; + public sortedSymbol_t e13; + public sortedSymbol_t e14; + public sortedSymbol_t e15; + public sortedSymbol_t e16; + public sortedSymbol_t e17; + public sortedSymbol_t e18; + public sortedSymbol_t e19; + public sortedSymbol_t e20; + public sortedSymbol_t e21; + public sortedSymbol_t e22; + public sortedSymbol_t e23; + public sortedSymbol_t e24; + public sortedSymbol_t e25; + public sortedSymbol_t e26; + public sortedSymbol_t e27; + public sortedSymbol_t e28; + public sortedSymbol_t e29; + public sortedSymbol_t e30; + public sortedSymbol_t e31; + public sortedSymbol_t e32; + public sortedSymbol_t e33; + public sortedSymbol_t e34; + public sortedSymbol_t e35; + public sortedSymbol_t e36; + public sortedSymbol_t e37; + public sortedSymbol_t e38; + public sortedSymbol_t e39; + public sortedSymbol_t e40; + public sortedSymbol_t e41; + public sortedSymbol_t e42; + public sortedSymbol_t e43; + public sortedSymbol_t e44; + public sortedSymbol_t e45; + public sortedSymbol_t e46; + public sortedSymbol_t e47; + public sortedSymbol_t e48; + public sortedSymbol_t e49; + public sortedSymbol_t e50; + public sortedSymbol_t e51; + public sortedSymbol_t e52; + public sortedSymbol_t e53; + public sortedSymbol_t e54; + public sortedSymbol_t e55; + public sortedSymbol_t e56; + public sortedSymbol_t e57; + public sortedSymbol_t e58; + public sortedSymbol_t e59; + public sortedSymbol_t e60; + public sortedSymbol_t e61; + public sortedSymbol_t e62; + public sortedSymbol_t e63; + public sortedSymbol_t e64; + public sortedSymbol_t e65; + public sortedSymbol_t e66; + public sortedSymbol_t e67; + public sortedSymbol_t e68; + public sortedSymbol_t e69; + public sortedSymbol_t e70; + public sortedSymbol_t e71; + public sortedSymbol_t e72; + public sortedSymbol_t e73; + public sortedSymbol_t e74; + public sortedSymbol_t e75; + public sortedSymbol_t e76; + public sortedSymbol_t e77; + public sortedSymbol_t e78; + public sortedSymbol_t e79; + public sortedSymbol_t e80; + public sortedSymbol_t e81; + public sortedSymbol_t e82; + public sortedSymbol_t e83; + public sortedSymbol_t e84; + public sortedSymbol_t e85; + public sortedSymbol_t e86; + public sortedSymbol_t e87; + public sortedSymbol_t e88; + public sortedSymbol_t e89; + public sortedSymbol_t e90; + public sortedSymbol_t e91; + public sortedSymbol_t e92; + public sortedSymbol_t e93; + public sortedSymbol_t e94; + public sortedSymbol_t e95; + public sortedSymbol_t e96; + public sortedSymbol_t e97; + public sortedSymbol_t e98; + public sortedSymbol_t e99; + public sortedSymbol_t e100; + public sortedSymbol_t e101; + public sortedSymbol_t e102; + public sortedSymbol_t e103; + public sortedSymbol_t e104; + public sortedSymbol_t e105; + public sortedSymbol_t e106; + public sortedSymbol_t e107; + public sortedSymbol_t e108; + public sortedSymbol_t e109; + public sortedSymbol_t e110; + public sortedSymbol_t e111; + public sortedSymbol_t e112; + public sortedSymbol_t e113; + public sortedSymbol_t e114; + public sortedSymbol_t e115; + public sortedSymbol_t e116; + public sortedSymbol_t e117; + public sortedSymbol_t e118; + public sortedSymbol_t e119; + public sortedSymbol_t e120; + public sortedSymbol_t e121; + public sortedSymbol_t e122; + public sortedSymbol_t e123; + public sortedSymbol_t e124; + public sortedSymbol_t e125; + public sortedSymbol_t e126; + public sortedSymbol_t e127; + public sortedSymbol_t e128; + public sortedSymbol_t e129; + public sortedSymbol_t e130; + public sortedSymbol_t e131; + public sortedSymbol_t e132; + public sortedSymbol_t e133; + public sortedSymbol_t e134; + public sortedSymbol_t e135; + public sortedSymbol_t e136; + public sortedSymbol_t e137; + public sortedSymbol_t e138; + public sortedSymbol_t e139; + public sortedSymbol_t e140; + public sortedSymbol_t e141; + public sortedSymbol_t e142; + public sortedSymbol_t e143; + public sortedSymbol_t e144; + public sortedSymbol_t e145; + public sortedSymbol_t e146; + public sortedSymbol_t e147; + public sortedSymbol_t e148; + public sortedSymbol_t e149; + public sortedSymbol_t e150; + public sortedSymbol_t e151; + public sortedSymbol_t e152; + public sortedSymbol_t e153; + public sortedSymbol_t e154; + public sortedSymbol_t e155; + public sortedSymbol_t e156; + public sortedSymbol_t e157; + public sortedSymbol_t e158; + public sortedSymbol_t e159; + public sortedSymbol_t e160; + public sortedSymbol_t e161; + public sortedSymbol_t e162; + public sortedSymbol_t e163; + public sortedSymbol_t e164; + public sortedSymbol_t e165; + public sortedSymbol_t e166; + public sortedSymbol_t e167; + public sortedSymbol_t e168; + public sortedSymbol_t e169; + public sortedSymbol_t e170; + public sortedSymbol_t e171; + public sortedSymbol_t e172; + public sortedSymbol_t e173; + public sortedSymbol_t e174; + public sortedSymbol_t e175; + public sortedSymbol_t e176; + public sortedSymbol_t e177; + public sortedSymbol_t e178; + public sortedSymbol_t e179; + public sortedSymbol_t e180; + public sortedSymbol_t e181; + public sortedSymbol_t e182; + public sortedSymbol_t e183; + public sortedSymbol_t e184; + public sortedSymbol_t e185; + public sortedSymbol_t e186; + public sortedSymbol_t e187; + public sortedSymbol_t e188; + public sortedSymbol_t e189; + public sortedSymbol_t e190; + public sortedSymbol_t e191; + public sortedSymbol_t e192; + public sortedSymbol_t e193; + public sortedSymbol_t e194; + public sortedSymbol_t e195; + public sortedSymbol_t e196; + public sortedSymbol_t e197; + public sortedSymbol_t e198; + public sortedSymbol_t e199; + public sortedSymbol_t e200; + public sortedSymbol_t e201; + public sortedSymbol_t e202; + public sortedSymbol_t e203; + public sortedSymbol_t e204; + public sortedSymbol_t e205; + public sortedSymbol_t e206; + public sortedSymbol_t e207; + public sortedSymbol_t e208; + public sortedSymbol_t e209; + public sortedSymbol_t e210; + public sortedSymbol_t e211; + public sortedSymbol_t e212; + public sortedSymbol_t e213; + public sortedSymbol_t e214; + public sortedSymbol_t e215; + public sortedSymbol_t e216; + public sortedSymbol_t e217; + public sortedSymbol_t e218; + public sortedSymbol_t e219; + public sortedSymbol_t e220; + public sortedSymbol_t e221; + public sortedSymbol_t e222; + public sortedSymbol_t e223; + public sortedSymbol_t e224; + public sortedSymbol_t e225; + public sortedSymbol_t e226; + public sortedSymbol_t e227; + public sortedSymbol_t e228; + public sortedSymbol_t e229; + public sortedSymbol_t e230; + public sortedSymbol_t e231; + public sortedSymbol_t e232; + public sortedSymbol_t e233; + public sortedSymbol_t e234; + public sortedSymbol_t e235; + public sortedSymbol_t e236; + public sortedSymbol_t e237; + public sortedSymbol_t e238; + public sortedSymbol_t e239; + public sortedSymbol_t e240; + public sortedSymbol_t e241; + public sortedSymbol_t e242; + public sortedSymbol_t e243; + public sortedSymbol_t e244; + public sortedSymbol_t e245; + public sortedSymbol_t e246; + public sortedSymbol_t e247; + public sortedSymbol_t e248; + public sortedSymbol_t e249; + public sortedSymbol_t e250; + public sortedSymbol_t e251; + public sortedSymbol_t e252; + public sortedSymbol_t e253; + public sortedSymbol_t e254; + public sortedSymbol_t e255; + + public ref sortedSymbol_t this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref sortedSymbol_t this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref sortedSymbol_t this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator sortedSymbol_t*(in _sortedSymbol_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_sortedSymbol_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static sortedSymbol_t* operator +(in _sortedSymbol_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_sortedSymbol_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_WriteCTableWksp.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_WriteCTableWksp.cs new file mode 100644 index 00000000..c2174355 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_WriteCTableWksp.cs @@ -0,0 +1,14 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct HUF_WriteCTableWksp + { + public HUF_CompressWeightsWksp wksp; + + /* precomputed conversion table */ + public fixed byte bitsToWeight[13]; + + public fixed byte huffWeight[255]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_buildCTable_wksp_tables.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_buildCTable_wksp_tables.cs new file mode 100644 index 00000000..c86bd150 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_buildCTable_wksp_tables.cs @@ -0,0 +1,657 @@ +using InlineIL; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static InlineIL.IL.Emit; + +namespace ZstdSharp.Unsafe +{ + public partial struct HUF_buildCTable_wksp_tables + { + public _huffNodeTbl_e__FixedBuffer huffNodeTbl; + + public _rankPosition_e__FixedBuffer rankPosition; + + public unsafe partial struct _huffNodeTbl_e__FixedBuffer + { + public nodeElt_s e0; + public nodeElt_s e1; + public nodeElt_s e2; + public nodeElt_s e3; + public nodeElt_s e4; + public nodeElt_s e5; + public nodeElt_s e6; + public nodeElt_s e7; + public nodeElt_s e8; + public nodeElt_s e9; + public nodeElt_s e10; + public nodeElt_s e11; + public nodeElt_s e12; + public nodeElt_s e13; + public nodeElt_s e14; + public nodeElt_s e15; + public nodeElt_s e16; + public nodeElt_s e17; + public nodeElt_s e18; + public nodeElt_s e19; + public nodeElt_s e20; + public nodeElt_s e21; + public nodeElt_s e22; + public nodeElt_s e23; + public nodeElt_s e24; + public nodeElt_s e25; + public nodeElt_s e26; + public nodeElt_s e27; + public nodeElt_s e28; + public nodeElt_s e29; + public nodeElt_s e30; + public nodeElt_s e31; + public nodeElt_s e32; + public nodeElt_s e33; + public nodeElt_s e34; + public nodeElt_s e35; + public nodeElt_s e36; + public nodeElt_s e37; + public nodeElt_s e38; + public nodeElt_s e39; + public nodeElt_s e40; + public nodeElt_s e41; + public nodeElt_s e42; + public nodeElt_s e43; + public nodeElt_s e44; + public nodeElt_s e45; + public nodeElt_s e46; + public nodeElt_s e47; + public nodeElt_s e48; + public nodeElt_s e49; + public nodeElt_s e50; + public nodeElt_s e51; + public nodeElt_s e52; + public nodeElt_s e53; + public nodeElt_s e54; + public nodeElt_s e55; + public nodeElt_s e56; + public nodeElt_s e57; + public nodeElt_s e58; + public nodeElt_s e59; + public nodeElt_s e60; + public nodeElt_s e61; + public nodeElt_s e62; + public nodeElt_s e63; + public nodeElt_s e64; + public nodeElt_s e65; + public nodeElt_s e66; + public nodeElt_s e67; + public nodeElt_s e68; + public nodeElt_s e69; + public nodeElt_s e70; + public nodeElt_s e71; + public nodeElt_s e72; + public nodeElt_s e73; + public nodeElt_s e74; + public nodeElt_s e75; + public nodeElt_s e76; + public nodeElt_s e77; + public nodeElt_s e78; + public nodeElt_s e79; + public nodeElt_s e80; + public nodeElt_s e81; + public nodeElt_s e82; + public nodeElt_s e83; + public nodeElt_s e84; + public nodeElt_s e85; + public nodeElt_s e86; + public nodeElt_s e87; + public nodeElt_s e88; + public nodeElt_s e89; + public nodeElt_s e90; + public nodeElt_s e91; + public nodeElt_s e92; + public nodeElt_s e93; + public nodeElt_s e94; + public nodeElt_s e95; + public nodeElt_s e96; + public nodeElt_s e97; + public nodeElt_s e98; + public nodeElt_s e99; + public nodeElt_s e100; + public nodeElt_s e101; + public nodeElt_s e102; + public nodeElt_s e103; + public nodeElt_s e104; + public nodeElt_s e105; + public nodeElt_s e106; + public nodeElt_s e107; + public nodeElt_s e108; + public nodeElt_s e109; + public nodeElt_s e110; + public nodeElt_s e111; + public nodeElt_s e112; + public nodeElt_s e113; + public nodeElt_s e114; + public nodeElt_s e115; + public nodeElt_s e116; + public nodeElt_s e117; + public nodeElt_s e118; + public nodeElt_s e119; + public nodeElt_s e120; + public nodeElt_s e121; + public nodeElt_s e122; + public nodeElt_s e123; + public nodeElt_s e124; + public nodeElt_s e125; + public nodeElt_s e126; + public nodeElt_s e127; + public nodeElt_s e128; + public nodeElt_s e129; + public nodeElt_s e130; + public nodeElt_s e131; + public nodeElt_s e132; + public nodeElt_s e133; + public nodeElt_s e134; + public nodeElt_s e135; + public nodeElt_s e136; + public nodeElt_s e137; + public nodeElt_s e138; + public nodeElt_s e139; + public nodeElt_s e140; + public nodeElt_s e141; + public nodeElt_s e142; + public nodeElt_s e143; + public nodeElt_s e144; + public nodeElt_s e145; + public nodeElt_s e146; + public nodeElt_s e147; + public nodeElt_s e148; + public nodeElt_s e149; + public nodeElt_s e150; + public nodeElt_s e151; + public nodeElt_s e152; + public nodeElt_s e153; + public nodeElt_s e154; + public nodeElt_s e155; + public nodeElt_s e156; + public nodeElt_s e157; + public nodeElt_s e158; + public nodeElt_s e159; + public nodeElt_s e160; + public nodeElt_s e161; + public nodeElt_s e162; + public nodeElt_s e163; + public nodeElt_s e164; + public nodeElt_s e165; + public nodeElt_s e166; + public nodeElt_s e167; + public nodeElt_s e168; + public nodeElt_s e169; + public nodeElt_s e170; + public nodeElt_s e171; + public nodeElt_s e172; + public nodeElt_s e173; + public nodeElt_s e174; + public nodeElt_s e175; + public nodeElt_s e176; + public nodeElt_s e177; + public nodeElt_s e178; + public nodeElt_s e179; + public nodeElt_s e180; + public nodeElt_s e181; + public nodeElt_s e182; + public nodeElt_s e183; + public nodeElt_s e184; + public nodeElt_s e185; + public nodeElt_s e186; + public nodeElt_s e187; + public nodeElt_s e188; + public nodeElt_s e189; + public nodeElt_s e190; + public nodeElt_s e191; + public nodeElt_s e192; + public nodeElt_s e193; + public nodeElt_s e194; + public nodeElt_s e195; + public nodeElt_s e196; + public nodeElt_s e197; + public nodeElt_s e198; + public nodeElt_s e199; + public nodeElt_s e200; + public nodeElt_s e201; + public nodeElt_s e202; + public nodeElt_s e203; + public nodeElt_s e204; + public nodeElt_s e205; + public nodeElt_s e206; + public nodeElt_s e207; + public nodeElt_s e208; + public nodeElt_s e209; + public nodeElt_s e210; + public nodeElt_s e211; + public nodeElt_s e212; + public nodeElt_s e213; + public nodeElt_s e214; + public nodeElt_s e215; + public nodeElt_s e216; + public nodeElt_s e217; + public nodeElt_s e218; + public nodeElt_s e219; + public nodeElt_s e220; + public nodeElt_s e221; + public nodeElt_s e222; + public nodeElt_s e223; + public nodeElt_s e224; + public nodeElt_s e225; + public nodeElt_s e226; + public nodeElt_s e227; + public nodeElt_s e228; + public nodeElt_s e229; + public nodeElt_s e230; + public nodeElt_s e231; + public nodeElt_s e232; + public nodeElt_s e233; + public nodeElt_s e234; + public nodeElt_s e235; + public nodeElt_s e236; + public nodeElt_s e237; + public nodeElt_s e238; + public nodeElt_s e239; + public nodeElt_s e240; + public nodeElt_s e241; + public nodeElt_s e242; + public nodeElt_s e243; + public nodeElt_s e244; + public nodeElt_s e245; + public nodeElt_s e246; + public nodeElt_s e247; + public nodeElt_s e248; + public nodeElt_s e249; + public nodeElt_s e250; + public nodeElt_s e251; + public nodeElt_s e252; + public nodeElt_s e253; + public nodeElt_s e254; + public nodeElt_s e255; + public nodeElt_s e256; + public nodeElt_s e257; + public nodeElt_s e258; + public nodeElt_s e259; + public nodeElt_s e260; + public nodeElt_s e261; + public nodeElt_s e262; + public nodeElt_s e263; + public nodeElt_s e264; + public nodeElt_s e265; + public nodeElt_s e266; + public nodeElt_s e267; + public nodeElt_s e268; + public nodeElt_s e269; + public nodeElt_s e270; + public nodeElt_s e271; + public nodeElt_s e272; + public nodeElt_s e273; + public nodeElt_s e274; + public nodeElt_s e275; + public nodeElt_s e276; + public nodeElt_s e277; + public nodeElt_s e278; + public nodeElt_s e279; + public nodeElt_s e280; + public nodeElt_s e281; + public nodeElt_s e282; + public nodeElt_s e283; + public nodeElt_s e284; + public nodeElt_s e285; + public nodeElt_s e286; + public nodeElt_s e287; + public nodeElt_s e288; + public nodeElt_s e289; + public nodeElt_s e290; + public nodeElt_s e291; + public nodeElt_s e292; + public nodeElt_s e293; + public nodeElt_s e294; + public nodeElt_s e295; + public nodeElt_s e296; + public nodeElt_s e297; + public nodeElt_s e298; + public nodeElt_s e299; + public nodeElt_s e300; + public nodeElt_s e301; + public nodeElt_s e302; + public nodeElt_s e303; + public nodeElt_s e304; + public nodeElt_s e305; + public nodeElt_s e306; + public nodeElt_s e307; + public nodeElt_s e308; + public nodeElt_s e309; + public nodeElt_s e310; + public nodeElt_s e311; + public nodeElt_s e312; + public nodeElt_s e313; + public nodeElt_s e314; + public nodeElt_s e315; + public nodeElt_s e316; + public nodeElt_s e317; + public nodeElt_s e318; + public nodeElt_s e319; + public nodeElt_s e320; + public nodeElt_s e321; + public nodeElt_s e322; + public nodeElt_s e323; + public nodeElt_s e324; + public nodeElt_s e325; + public nodeElt_s e326; + public nodeElt_s e327; + public nodeElt_s e328; + public nodeElt_s e329; + public nodeElt_s e330; + public nodeElt_s e331; + public nodeElt_s e332; + public nodeElt_s e333; + public nodeElt_s e334; + public nodeElt_s e335; + public nodeElt_s e336; + public nodeElt_s e337; + public nodeElt_s e338; + public nodeElt_s e339; + public nodeElt_s e340; + public nodeElt_s e341; + public nodeElt_s e342; + public nodeElt_s e343; + public nodeElt_s e344; + public nodeElt_s e345; + public nodeElt_s e346; + public nodeElt_s e347; + public nodeElt_s e348; + public nodeElt_s e349; + public nodeElt_s e350; + public nodeElt_s e351; + public nodeElt_s e352; + public nodeElt_s e353; + public nodeElt_s e354; + public nodeElt_s e355; + public nodeElt_s e356; + public nodeElt_s e357; + public nodeElt_s e358; + public nodeElt_s e359; + public nodeElt_s e360; + public nodeElt_s e361; + public nodeElt_s e362; + public nodeElt_s e363; + public nodeElt_s e364; + public nodeElt_s e365; + public nodeElt_s e366; + public nodeElt_s e367; + public nodeElt_s e368; + public nodeElt_s e369; + public nodeElt_s e370; + public nodeElt_s e371; + public nodeElt_s e372; + public nodeElt_s e373; + public nodeElt_s e374; + public nodeElt_s e375; + public nodeElt_s e376; + public nodeElt_s e377; + public nodeElt_s e378; + public nodeElt_s e379; + public nodeElt_s e380; + public nodeElt_s e381; + public nodeElt_s e382; + public nodeElt_s e383; + public nodeElt_s e384; + public nodeElt_s e385; + public nodeElt_s e386; + public nodeElt_s e387; + public nodeElt_s e388; + public nodeElt_s e389; + public nodeElt_s e390; + public nodeElt_s e391; + public nodeElt_s e392; + public nodeElt_s e393; + public nodeElt_s e394; + public nodeElt_s e395; + public nodeElt_s e396; + public nodeElt_s e397; + public nodeElt_s e398; + public nodeElt_s e399; + public nodeElt_s e400; + public nodeElt_s e401; + public nodeElt_s e402; + public nodeElt_s e403; + public nodeElt_s e404; + public nodeElt_s e405; + public nodeElt_s e406; + public nodeElt_s e407; + public nodeElt_s e408; + public nodeElt_s e409; + public nodeElt_s e410; + public nodeElt_s e411; + public nodeElt_s e412; + public nodeElt_s e413; + public nodeElt_s e414; + public nodeElt_s e415; + public nodeElt_s e416; + public nodeElt_s e417; + public nodeElt_s e418; + public nodeElt_s e419; + public nodeElt_s e420; + public nodeElt_s e421; + public nodeElt_s e422; + public nodeElt_s e423; + public nodeElt_s e424; + public nodeElt_s e425; + public nodeElt_s e426; + public nodeElt_s e427; + public nodeElt_s e428; + public nodeElt_s e429; + public nodeElt_s e430; + public nodeElt_s e431; + public nodeElt_s e432; + public nodeElt_s e433; + public nodeElt_s e434; + public nodeElt_s e435; + public nodeElt_s e436; + public nodeElt_s e437; + public nodeElt_s e438; + public nodeElt_s e439; + public nodeElt_s e440; + public nodeElt_s e441; + public nodeElt_s e442; + public nodeElt_s e443; + public nodeElt_s e444; + public nodeElt_s e445; + public nodeElt_s e446; + public nodeElt_s e447; + public nodeElt_s e448; + public nodeElt_s e449; + public nodeElt_s e450; + public nodeElt_s e451; + public nodeElt_s e452; + public nodeElt_s e453; + public nodeElt_s e454; + public nodeElt_s e455; + public nodeElt_s e456; + public nodeElt_s e457; + public nodeElt_s e458; + public nodeElt_s e459; + public nodeElt_s e460; + public nodeElt_s e461; + public nodeElt_s e462; + public nodeElt_s e463; + public nodeElt_s e464; + public nodeElt_s e465; + public nodeElt_s e466; + public nodeElt_s e467; + public nodeElt_s e468; + public nodeElt_s e469; + public nodeElt_s e470; + public nodeElt_s e471; + public nodeElt_s e472; + public nodeElt_s e473; + public nodeElt_s e474; + public nodeElt_s e475; + public nodeElt_s e476; + public nodeElt_s e477; + public nodeElt_s e478; + public nodeElt_s e479; + public nodeElt_s e480; + public nodeElt_s e481; + public nodeElt_s e482; + public nodeElt_s e483; + public nodeElt_s e484; + public nodeElt_s e485; + public nodeElt_s e486; + public nodeElt_s e487; + public nodeElt_s e488; + public nodeElt_s e489; + public nodeElt_s e490; + public nodeElt_s e491; + public nodeElt_s e492; + public nodeElt_s e493; + public nodeElt_s e494; + public nodeElt_s e495; + public nodeElt_s e496; + public nodeElt_s e497; + public nodeElt_s e498; + public nodeElt_s e499; + public nodeElt_s e500; + public nodeElt_s e501; + public nodeElt_s e502; + public nodeElt_s e503; + public nodeElt_s e504; + public nodeElt_s e505; + public nodeElt_s e506; + public nodeElt_s e507; + public nodeElt_s e508; + public nodeElt_s e509; + public nodeElt_s e510; + public nodeElt_s e511; + + public ref nodeElt_s this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref nodeElt_s this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref nodeElt_s this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator nodeElt_s*(in _huffNodeTbl_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_huffNodeTbl_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static nodeElt_s* operator +(in _huffNodeTbl_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_huffNodeTbl_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + + public unsafe partial struct _rankPosition_e__FixedBuffer + { + public rankPos e0; + public rankPos e1; + public rankPos e2; + public rankPos e3; + public rankPos e4; + public rankPos e5; + public rankPos e6; + public rankPos e7; + public rankPos e8; + public rankPos e9; + public rankPos e10; + public rankPos e11; + public rankPos e12; + public rankPos e13; + public rankPos e14; + public rankPos e15; + public rankPos e16; + public rankPos e17; + public rankPos e18; + public rankPos e19; + public rankPos e20; + public rankPos e21; + public rankPos e22; + public rankPos e23; + public rankPos e24; + public rankPos e25; + public rankPos e26; + public rankPos e27; + public rankPos e28; + public rankPos e29; + public rankPos e30; + public rankPos e31; + + public ref rankPos this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref rankPos this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref rankPos this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator rankPos*(in _rankPosition_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_rankPosition_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static rankPos* operator +(in _rankPosition_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_rankPosition_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_compress_tables_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_compress_tables_t.cs new file mode 100644 index 00000000..fc68c091 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_compress_tables_t.cs @@ -0,0 +1,322 @@ +using InlineIL; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static InlineIL.IL.Emit; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct HUF_compress_tables_t + { + public fixed uint count[256]; + + public _CTable_e__FixedBuffer CTable; + + public _wksps_e__Union wksps; + + public unsafe partial struct _CTable_e__FixedBuffer + { + public HUF_CElt_s e0; + public HUF_CElt_s e1; + public HUF_CElt_s e2; + public HUF_CElt_s e3; + public HUF_CElt_s e4; + public HUF_CElt_s e5; + public HUF_CElt_s e6; + public HUF_CElt_s e7; + public HUF_CElt_s e8; + public HUF_CElt_s e9; + public HUF_CElt_s e10; + public HUF_CElt_s e11; + public HUF_CElt_s e12; + public HUF_CElt_s e13; + public HUF_CElt_s e14; + public HUF_CElt_s e15; + public HUF_CElt_s e16; + public HUF_CElt_s e17; + public HUF_CElt_s e18; + public HUF_CElt_s e19; + public HUF_CElt_s e20; + public HUF_CElt_s e21; + public HUF_CElt_s e22; + public HUF_CElt_s e23; + public HUF_CElt_s e24; + public HUF_CElt_s e25; + public HUF_CElt_s e26; + public HUF_CElt_s e27; + public HUF_CElt_s e28; + public HUF_CElt_s e29; + public HUF_CElt_s e30; + public HUF_CElt_s e31; + public HUF_CElt_s e32; + public HUF_CElt_s e33; + public HUF_CElt_s e34; + public HUF_CElt_s e35; + public HUF_CElt_s e36; + public HUF_CElt_s e37; + public HUF_CElt_s e38; + public HUF_CElt_s e39; + public HUF_CElt_s e40; + public HUF_CElt_s e41; + public HUF_CElt_s e42; + public HUF_CElt_s e43; + public HUF_CElt_s e44; + public HUF_CElt_s e45; + public HUF_CElt_s e46; + public HUF_CElt_s e47; + public HUF_CElt_s e48; + public HUF_CElt_s e49; + public HUF_CElt_s e50; + public HUF_CElt_s e51; + public HUF_CElt_s e52; + public HUF_CElt_s e53; + public HUF_CElt_s e54; + public HUF_CElt_s e55; + public HUF_CElt_s e56; + public HUF_CElt_s e57; + public HUF_CElt_s e58; + public HUF_CElt_s e59; + public HUF_CElt_s e60; + public HUF_CElt_s e61; + public HUF_CElt_s e62; + public HUF_CElt_s e63; + public HUF_CElt_s e64; + public HUF_CElt_s e65; + public HUF_CElt_s e66; + public HUF_CElt_s e67; + public HUF_CElt_s e68; + public HUF_CElt_s e69; + public HUF_CElt_s e70; + public HUF_CElt_s e71; + public HUF_CElt_s e72; + public HUF_CElt_s e73; + public HUF_CElt_s e74; + public HUF_CElt_s e75; + public HUF_CElt_s e76; + public HUF_CElt_s e77; + public HUF_CElt_s e78; + public HUF_CElt_s e79; + public HUF_CElt_s e80; + public HUF_CElt_s e81; + public HUF_CElt_s e82; + public HUF_CElt_s e83; + public HUF_CElt_s e84; + public HUF_CElt_s e85; + public HUF_CElt_s e86; + public HUF_CElt_s e87; + public HUF_CElt_s e88; + public HUF_CElt_s e89; + public HUF_CElt_s e90; + public HUF_CElt_s e91; + public HUF_CElt_s e92; + public HUF_CElt_s e93; + public HUF_CElt_s e94; + public HUF_CElt_s e95; + public HUF_CElt_s e96; + public HUF_CElt_s e97; + public HUF_CElt_s e98; + public HUF_CElt_s e99; + public HUF_CElt_s e100; + public HUF_CElt_s e101; + public HUF_CElt_s e102; + public HUF_CElt_s e103; + public HUF_CElt_s e104; + public HUF_CElt_s e105; + public HUF_CElt_s e106; + public HUF_CElt_s e107; + public HUF_CElt_s e108; + public HUF_CElt_s e109; + public HUF_CElt_s e110; + public HUF_CElt_s e111; + public HUF_CElt_s e112; + public HUF_CElt_s e113; + public HUF_CElt_s e114; + public HUF_CElt_s e115; + public HUF_CElt_s e116; + public HUF_CElt_s e117; + public HUF_CElt_s e118; + public HUF_CElt_s e119; + public HUF_CElt_s e120; + public HUF_CElt_s e121; + public HUF_CElt_s e122; + public HUF_CElt_s e123; + public HUF_CElt_s e124; + public HUF_CElt_s e125; + public HUF_CElt_s e126; + public HUF_CElt_s e127; + public HUF_CElt_s e128; + public HUF_CElt_s e129; + public HUF_CElt_s e130; + public HUF_CElt_s e131; + public HUF_CElt_s e132; + public HUF_CElt_s e133; + public HUF_CElt_s e134; + public HUF_CElt_s e135; + public HUF_CElt_s e136; + public HUF_CElt_s e137; + public HUF_CElt_s e138; + public HUF_CElt_s e139; + public HUF_CElt_s e140; + public HUF_CElt_s e141; + public HUF_CElt_s e142; + public HUF_CElt_s e143; + public HUF_CElt_s e144; + public HUF_CElt_s e145; + public HUF_CElt_s e146; + public HUF_CElt_s e147; + public HUF_CElt_s e148; + public HUF_CElt_s e149; + public HUF_CElt_s e150; + public HUF_CElt_s e151; + public HUF_CElt_s e152; + public HUF_CElt_s e153; + public HUF_CElt_s e154; + public HUF_CElt_s e155; + public HUF_CElt_s e156; + public HUF_CElt_s e157; + public HUF_CElt_s e158; + public HUF_CElt_s e159; + public HUF_CElt_s e160; + public HUF_CElt_s e161; + public HUF_CElt_s e162; + public HUF_CElt_s e163; + public HUF_CElt_s e164; + public HUF_CElt_s e165; + public HUF_CElt_s e166; + public HUF_CElt_s e167; + public HUF_CElt_s e168; + public HUF_CElt_s e169; + public HUF_CElt_s e170; + public HUF_CElt_s e171; + public HUF_CElt_s e172; + public HUF_CElt_s e173; + public HUF_CElt_s e174; + public HUF_CElt_s e175; + public HUF_CElt_s e176; + public HUF_CElt_s e177; + public HUF_CElt_s e178; + public HUF_CElt_s e179; + public HUF_CElt_s e180; + public HUF_CElt_s e181; + public HUF_CElt_s e182; + public HUF_CElt_s e183; + public HUF_CElt_s e184; + public HUF_CElt_s e185; + public HUF_CElt_s e186; + public HUF_CElt_s e187; + public HUF_CElt_s e188; + public HUF_CElt_s e189; + public HUF_CElt_s e190; + public HUF_CElt_s e191; + public HUF_CElt_s e192; + public HUF_CElt_s e193; + public HUF_CElt_s e194; + public HUF_CElt_s e195; + public HUF_CElt_s e196; + public HUF_CElt_s e197; + public HUF_CElt_s e198; + public HUF_CElt_s e199; + public HUF_CElt_s e200; + public HUF_CElt_s e201; + public HUF_CElt_s e202; + public HUF_CElt_s e203; + public HUF_CElt_s e204; + public HUF_CElt_s e205; + public HUF_CElt_s e206; + public HUF_CElt_s e207; + public HUF_CElt_s e208; + public HUF_CElt_s e209; + public HUF_CElt_s e210; + public HUF_CElt_s e211; + public HUF_CElt_s e212; + public HUF_CElt_s e213; + public HUF_CElt_s e214; + public HUF_CElt_s e215; + public HUF_CElt_s e216; + public HUF_CElt_s e217; + public HUF_CElt_s e218; + public HUF_CElt_s e219; + public HUF_CElt_s e220; + public HUF_CElt_s e221; + public HUF_CElt_s e222; + public HUF_CElt_s e223; + public HUF_CElt_s e224; + public HUF_CElt_s e225; + public HUF_CElt_s e226; + public HUF_CElt_s e227; + public HUF_CElt_s e228; + public HUF_CElt_s e229; + public HUF_CElt_s e230; + public HUF_CElt_s e231; + public HUF_CElt_s e232; + public HUF_CElt_s e233; + public HUF_CElt_s e234; + public HUF_CElt_s e235; + public HUF_CElt_s e236; + public HUF_CElt_s e237; + public HUF_CElt_s e238; + public HUF_CElt_s e239; + public HUF_CElt_s e240; + public HUF_CElt_s e241; + public HUF_CElt_s e242; + public HUF_CElt_s e243; + public HUF_CElt_s e244; + public HUF_CElt_s e245; + public HUF_CElt_s e246; + public HUF_CElt_s e247; + public HUF_CElt_s e248; + public HUF_CElt_s e249; + public HUF_CElt_s e250; + public HUF_CElt_s e251; + public HUF_CElt_s e252; + public HUF_CElt_s e253; + public HUF_CElt_s e254; + public HUF_CElt_s e255; + + public ref HUF_CElt_s this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref HUF_CElt_s this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref HUF_CElt_s this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator HUF_CElt_s*(in _CTable_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_CTable_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static HUF_CElt_s* operator +(in _CTable_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_CTable_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_decompress_usingDTable_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_decompress_usingDTable_t.cs new file mode 100644 index 00000000..78478e2f --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_decompress_usingDTable_t.cs @@ -0,0 +1,8 @@ +using System; +using System.Runtime.InteropServices; + +namespace ZstdSharp.Unsafe +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public unsafe delegate nuint HUF_decompress_usingDTable_t(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable); +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_nbStreams_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_nbStreams_e.cs new file mode 100644 index 00000000..260541be --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_nbStreams_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum HUF_nbStreams_e + { + HUF_singleStream, + HUF_fourStreams, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_repeat.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_repeat.cs new file mode 100644 index 00000000..59e880f6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HUF_repeat.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum HUF_repeat + { + HUF_repeat_none, + HUF_repeat_check, + HUF_repeat_valid, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Hist.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Hist.cs new file mode 100644 index 00000000..09d3ccbd --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Hist.cs @@ -0,0 +1,240 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /* --- Error management --- */ + public static uint HIST_isError(nuint code) + { + return ERR_isError(code); + } + + /*-************************************************************** + * Histogram functions + ****************************************************************/ + public static uint HIST_count_simple(uint* count, uint* maxSymbolValuePtr, void* src, nuint srcSize) + { + byte* ip = (byte*)(src); + byte* end = ip + srcSize; + uint maxSymbolValue = *maxSymbolValuePtr; + uint largestCount = 0; + + memset((void*)(count), (0), ((maxSymbolValue + 1) * (nuint)(sizeof(uint)))); + if (srcSize == 0) + { + *maxSymbolValuePtr = 0; + return 0; + } + + while (ip < end) + { + assert(*ip <= maxSymbolValue); + count[*ip++]++; + } + + while ((count[maxSymbolValue]) == 0) + { + maxSymbolValue--; + } + + *maxSymbolValuePtr = maxSymbolValue; + + { + uint s; + + for (s = 0; s <= maxSymbolValue; s++) + { + if (count[s] > largestCount) + { + largestCount = count[s]; + } + } + } + + return largestCount; + } + + /* HIST_count_parallel_wksp() : + * store histogram into 4 intermediate tables, recombined at the end. + * this design makes better use of OoO cpus, + * and is noticeably faster when some values are heavily repeated. + * But it needs some additional workspace for intermediate tables. + * `workSpace` must be a U32 table of size >= HIST_WKSP_SIZE_U32. + * @return : largest histogram frequency, + * or an error code (notably when histogram's alphabet is larger than *maxSymbolValuePtr) */ + private static nuint HIST_count_parallel_wksp(uint* count, uint* maxSymbolValuePtr, void* source, nuint sourceSize, HIST_checkInput_e check, uint* workSpace) + { + byte* ip = (byte*)(source); + byte* iend = ip + sourceSize; + nuint countSize = (*maxSymbolValuePtr + 1) * (nuint)(4); + uint max = 0; + uint* Counting1 = workSpace; + uint* Counting2 = Counting1 + 256; + uint* Counting3 = Counting2 + 256; + uint* Counting4 = Counting3 + 256; + + assert(*maxSymbolValuePtr <= 255); + if (sourceSize == 0) + { + memset((void*)(count), (0), (countSize)); + *maxSymbolValuePtr = 0; + return 0; + } + + memset((void*)(workSpace), (0), ((uint)(4 * 256) * (nuint)(sizeof(uint)))); + + { + uint cached = MEM_read32((void*)ip); + + ip += 4; + while (ip < iend - 15) + { + uint c = cached; + + cached = MEM_read32((void*)ip); + ip += 4; + Counting1[(byte)(c)]++; + Counting2[(byte)(c >> 8)]++; + Counting3[(byte)(c >> 16)]++; + Counting4[c >> 24]++; + c = cached; + cached = MEM_read32((void*)ip); + ip += 4; + Counting1[(byte)(c)]++; + Counting2[(byte)(c >> 8)]++; + Counting3[(byte)(c >> 16)]++; + Counting4[c >> 24]++; + c = cached; + cached = MEM_read32((void*)ip); + ip += 4; + Counting1[(byte)(c)]++; + Counting2[(byte)(c >> 8)]++; + Counting3[(byte)(c >> 16)]++; + Counting4[c >> 24]++; + c = cached; + cached = MEM_read32((void*)ip); + ip += 4; + Counting1[(byte)(c)]++; + Counting2[(byte)(c >> 8)]++; + Counting3[(byte)(c >> 16)]++; + Counting4[c >> 24]++; + } + + ip -= 4; + } + + while (ip < iend) + { + Counting1[*ip++]++; + } + + + { + uint s; + + for (s = 0; s < 256; s++) + { + Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s]; + if (Counting1[s] > max) + { + max = Counting1[s]; + } + } + } + + + { + uint maxSymbolValue = 255; + + while ((Counting1[maxSymbolValue]) == 0) + { + maxSymbolValue--; + } + + if (check != default && maxSymbolValue > *maxSymbolValuePtr) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooSmall))); + } + + *maxSymbolValuePtr = maxSymbolValue; + memmove((void*)(count), (void*)(Counting1), (countSize)); + } + + return (nuint)(max); + } + + /* HIST_countFast_wksp() : + * Same as HIST_countFast(), but using an externally provided scratch buffer. + * `workSpace` is a writable buffer which must be 4-bytes aligned, + * `workSpaceSize` must be >= HIST_WKSP_SIZE + */ + public static nuint HIST_countFast_wksp(uint* count, uint* maxSymbolValuePtr, void* source, nuint sourceSize, void* workSpace, nuint workSpaceSize) + { + if (sourceSize < 1500) + { + return HIST_count_simple(count, maxSymbolValuePtr, source, sourceSize); + } + + if (((nuint)(workSpace) & 3) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (workSpaceSize < (1024 * (nuint)(sizeof(uint)))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_workSpace_tooSmall))); + } + + return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, HIST_checkInput_e.trustInput, (uint*)(workSpace)); + } + + /* HIST_count_wksp() : + * Same as HIST_count(), but using an externally provided scratch buffer. + * `workSpace` size must be table of >= HIST_WKSP_SIZE_U32 unsigned */ + public static nuint HIST_count_wksp(uint* count, uint* maxSymbolValuePtr, void* source, nuint sourceSize, void* workSpace, nuint workSpaceSize) + { + if (((nuint)(workSpace) & 3) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (workSpaceSize < (1024 * (nuint)(sizeof(uint)))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_workSpace_tooSmall))); + } + + if (*maxSymbolValuePtr < 255) + { + return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, HIST_checkInput_e.checkMaxSymbolValue, (uint*)(workSpace)); + } + + *maxSymbolValuePtr = 255; + return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace, workSpaceSize); + } + + /* fast variant (unsafe : won't check if src contains values beyond count[] limit) */ + public static nuint HIST_countFast(uint* count, uint* maxSymbolValuePtr, void* source, nuint sourceSize) + { + uint* tmpCounters = stackalloc uint[1024]; + + return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, (void*)tmpCounters, (nuint)(sizeof(uint) * 1024)); + } + + /*! HIST_count(): + * Provides the precise count of each byte within a table 'count'. + * 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). + * Updates *maxSymbolValuePtr with actual largest symbol value detected. + * @return : count of the most frequent symbol (which isn't identified). + * or an error code, which can be tested using HIST_isError(). + * note : if return == srcSize, there is only one symbol. + */ + public static nuint HIST_count(uint* count, uint* maxSymbolValuePtr, void* src, nuint srcSize) + { + uint* tmpCounters = stackalloc uint[1024]; + + return HIST_count_wksp(count, maxSymbolValuePtr, src, srcSize, (void*)tmpCounters, (nuint)(sizeof(uint) * 1024)); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HufCompress.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HufCompress.cs new file mode 100644 index 00000000..2f37a5c6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HufCompress.cs @@ -0,0 +1,1150 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /* ************************************************************** + * Utils + ****************************************************************/ + public static uint HUF_optimalTableLog(uint maxTableLog, nuint srcSize, uint maxSymbolValue) + { + return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 1); + } + + private static nuint HUF_compressWeights(void* dst, nuint dstSize, void* weightTable, nuint wtSize, void* workspace, nuint workspaceSize) + { + byte* ostart = (byte*)(dst); + byte* op = ostart; + byte* oend = ostart + dstSize; + uint maxSymbolValue = 12; + uint tableLog = 6; + HUF_CompressWeightsWksp* wksp = (HUF_CompressWeightsWksp*)(workspace); + + if (workspaceSize < (nuint)(sizeof(HUF_CompressWeightsWksp))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (wtSize <= 1) + { + return 0; + } + + + { + uint maxCount = HIST_count_simple((uint*)wksp->count, &maxSymbolValue, weightTable, wtSize); + + if (maxCount == wtSize) + { + return 1; + } + + if (maxCount == 1) + { + return 0; + } + } + + tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue); + + { + nuint _var_err__ = FSE_normalizeCount((short*)wksp->norm, tableLog, (uint*)wksp->count, wtSize, maxSymbolValue, 0); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint hSize = FSE_writeNCount((void*)op, (nuint)(oend - op), (short*)wksp->norm, maxSymbolValue, tableLog); + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + op += hSize; + } + + + { + nuint _var_err__ = FSE_buildCTable_wksp((uint*)wksp->CTable, (short*)wksp->norm, maxSymbolValue, tableLog, (void*)wksp->scratchBuffer, (nuint)(120)); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint cSize = FSE_compress_usingCTable((void*)op, (nuint)(oend - op), weightTable, wtSize, (uint*)wksp->CTable); + + if ((ERR_isError(cSize)) != 0) + { + return cSize; + } + + if (cSize == 0) + { + return 0; + } + + op += cSize; + } + + return (nuint)(op - ostart); + } + + public static nuint HUF_writeCTable_wksp(void* dst, nuint maxDstSize, HUF_CElt_s* CTable, uint maxSymbolValue, uint huffLog, void* workspace, nuint workspaceSize) + { + byte* op = (byte*)(dst); + uint n; + HUF_WriteCTableWksp* wksp = (HUF_WriteCTableWksp*)(workspace); + + if (workspaceSize < (nuint)(sizeof(HUF_WriteCTableWksp))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (maxSymbolValue > 255) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooLarge))); + } + + wksp->bitsToWeight[0] = 0; + for (n = 1; n < huffLog + 1; n++) + { + wksp->bitsToWeight[n] = (byte)(huffLog + 1 - n); + } + + for (n = 0; n < maxSymbolValue; n++) + { + wksp->huffWeight[n] = wksp->bitsToWeight[CTable[n].nbBits]; + } + + + { + nuint hSize = HUF_compressWeights((void*)(op + 1), maxDstSize - 1, (void*)wksp->huffWeight, maxSymbolValue, (void*)&wksp->wksp, (nuint)(436)); + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + if (((hSize > 1) && (hSize < maxSymbolValue / 2))) + { + op[0] = (byte)(hSize); + return hSize + 1; + } + } + + if (maxSymbolValue > (uint)((256 - 128))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (((maxSymbolValue + 1) / 2) + 1 > maxDstSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + op[0] = (byte)(128 + (maxSymbolValue - 1)); + wksp->huffWeight[maxSymbolValue] = 0; + for (n = 0; n < maxSymbolValue; n += 2) + { + op[(n / 2) + 1] = (byte)((wksp->huffWeight[n] << 4) + wksp->huffWeight[n + 1]); + } + + return ((maxSymbolValue + 1) / 2) + 1; + } + + /*! HUF_writeCTable() : + `CTable` : Huffman tree to save, using huf representation. + @return : size of saved CTable */ + public static nuint HUF_writeCTable(void* dst, nuint maxDstSize, HUF_CElt_s* CTable, uint maxSymbolValue, uint huffLog) + { + HUF_WriteCTableWksp wksp; + + return HUF_writeCTable_wksp(dst, maxDstSize, CTable, maxSymbolValue, huffLog, (void*)&wksp, (nuint)(sizeof(HUF_WriteCTableWksp))); + } + + /** HUF_readCTable() : + * Loading a CTable saved with HUF_writeCTable() */ + public static nuint HUF_readCTable(HUF_CElt_s* CTable, uint* maxSymbolValuePtr, void* src, nuint srcSize, uint* hasZeroWeights) + { + byte* huffWeight = stackalloc byte[256]; + uint* rankVal = stackalloc uint[16]; + uint tableLog = 0; + uint nbSymbols = 0; + nuint readSize = HUF_readStats((byte*)huffWeight, (nuint)(255 + 1), (uint*)rankVal, &nbSymbols, &tableLog, src, srcSize); + + if ((ERR_isError(readSize)) != 0) + { + return readSize; + } + + *hasZeroWeights = (((rankVal[0] > 0)) ? 1U : 0U); + if (tableLog > 12) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + if (nbSymbols > *maxSymbolValuePtr + 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooSmall))); + } + + + { + uint n, nextRankStart = 0; + + for (n = 1; n <= tableLog; n++) + { + uint curr = nextRankStart; + + nextRankStart += (rankVal[n] << (int)(n - 1)); + rankVal[n] = curr; + } + } + + + { + uint n; + + for (n = 0; n < nbSymbols; n++) + { + uint w = huffWeight[n]; + + CTable[n].nbBits = (byte)(unchecked((byte)(tableLog + 1 - w) & -((w != 0) ? 1 : 0))); + } + } + + + { + ushort* nbPerRank = stackalloc ushort[14]; + memset(nbPerRank, 0, sizeof(ushort) * 14); + ushort* valPerRank = stackalloc ushort[14]; + memset(valPerRank, 0, sizeof(ushort) * 14); + + + { + uint n; + + for (n = 0; n < nbSymbols; n++) + { + nbPerRank[CTable[n].nbBits]++; + } + } + + valPerRank[tableLog + 1] = 0; + + { + ushort min = 0; + uint n; + + for (n = tableLog; n > 0; n--) + { + valPerRank[n] = min; + min += (ushort)(nbPerRank[n]); + min >>= 1; + } + } + + + { + uint n; + + for (n = 0; n < nbSymbols; n++) + { + CTable[n].val = valPerRank[CTable[n].nbBits]++; + } + } + } + + *maxSymbolValuePtr = nbSymbols - 1; + return readSize; + } + + /** HUF_getNbBits() : + * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX + * Note 1 : is not inlined, as HUF_CElt definition is private + * Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */ + public static uint HUF_getNbBits(void* symbolTable, uint symbolValue) + { + HUF_CElt_s* table = (HUF_CElt_s*)(symbolTable); + + assert(symbolValue <= 255); + return table[symbolValue].nbBits; + } + + /** + * HUF_setMaxHeight(): + * Enforces maxNbBits on the Huffman tree described in huffNode. + * + * It sets all nodes with nbBits > maxNbBits to be maxNbBits. Then it adjusts + * the tree to so that it is a valid canonical Huffman tree. + * + * @pre The sum of the ranks of each symbol == 2^largestBits, + * where largestBits == huffNode[lastNonNull].nbBits. + * @post The sum of the ranks of each symbol == 2^largestBits, + * where largestBits is the return value <= maxNbBits. + * + * @param huffNode The Huffman tree modified in place to enforce maxNbBits. + * @param lastNonNull The symbol with the lowest count in the Huffman tree. + * @param maxNbBits The maximum allowed number of bits, which the Huffman tree + * may not respect. After this function the Huffman tree will + * respect maxNbBits. + * @return The maximum number of bits of the Huffman tree after adjustment, + * necessarily no more than maxNbBits. + */ + private static uint HUF_setMaxHeight(nodeElt_s* huffNode, uint lastNonNull, uint maxNbBits) + { + uint largestBits = huffNode[lastNonNull].nbBits; + + if (largestBits <= maxNbBits) + { + return largestBits; + } + + + { + int totalCost = 0; + uint baseCost = (uint)(1 << (int)(largestBits - maxNbBits)); + int n = (int)(lastNonNull); + + while (huffNode[n].nbBits > maxNbBits) + { + totalCost += (int)((int)(baseCost - (uint)((1 << (int)(largestBits - huffNode[n].nbBits))))); + huffNode[n].nbBits = (byte)(maxNbBits); + n--; + } + + assert(huffNode[n].nbBits <= maxNbBits); + while (huffNode[n].nbBits == maxNbBits) + { + --n; + } + + assert(((uint)totalCost & (baseCost - 1)) == 0); + totalCost >>= (int)(largestBits - maxNbBits); + assert(totalCost > 0); + + { + uint noSymbol = 0xF0F0F0F0; + uint* rankLast = stackalloc uint[14]; + + memset((void*)(rankLast), (0xF0), ((nuint)(sizeof(uint) * 14))); + + { + uint currentNbBits = maxNbBits; + int pos; + + for (pos = n; pos >= 0; pos--) + { + if (huffNode[pos].nbBits >= currentNbBits) + { + continue; + } + + currentNbBits = huffNode[pos].nbBits; + rankLast[maxNbBits - currentNbBits] = (uint)(pos); + } + } + + while (totalCost > 0) + { + uint nBitsToDecrease = BIT_highbit32((uint)(totalCost)) + 1; + + for (; nBitsToDecrease > 1; nBitsToDecrease--) + { + uint highPos = rankLast[nBitsToDecrease]; + uint lowPos = rankLast[nBitsToDecrease - 1]; + + if (highPos == noSymbol) + { + continue; + } + + if (lowPos == noSymbol) + { + break; + } + + + { + uint highTotal = huffNode[highPos].count; + uint lowTotal = 2 * huffNode[lowPos].count; + + if (highTotal <= lowTotal) + { + break; + } + } + } + + assert(rankLast[nBitsToDecrease] != noSymbol || nBitsToDecrease == 1); + while ((nBitsToDecrease <= 12) && (rankLast[nBitsToDecrease] == noSymbol)) + { + nBitsToDecrease++; + } + + assert(rankLast[nBitsToDecrease] != noSymbol); + totalCost -= 1 << (int)(nBitsToDecrease - 1); + huffNode[rankLast[nBitsToDecrease]].nbBits++; + if (rankLast[nBitsToDecrease - 1] == noSymbol) + { + rankLast[nBitsToDecrease - 1] = rankLast[nBitsToDecrease]; + } + + if (rankLast[nBitsToDecrease] == 0) + { + rankLast[nBitsToDecrease] = noSymbol; + } + else + { + rankLast[nBitsToDecrease]--; + if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits - nBitsToDecrease) + { + rankLast[nBitsToDecrease] = noSymbol; + } + } + } + + while (totalCost < 0) + { + if (rankLast[1] == noSymbol) + { + while (huffNode[n].nbBits == maxNbBits) + { + n--; + } + + huffNode[n + 1].nbBits--; + assert(n >= 0); + rankLast[1] = (uint)(n + 1); + totalCost++; + continue; + } + + huffNode[rankLast[1] + 1].nbBits--; + rankLast[1]++; + totalCost++; + } + } + } + + return maxNbBits; + } + + /** + * HUF_sort(): + * Sorts the symbols [0, maxSymbolValue] by count[symbol] in decreasing order. + * + * @param[out] huffNode Sorted symbols by decreasing count. Only members `.count` and `.byte` are filled. + * Must have (maxSymbolValue + 1) entries. + * @param[in] count Histogram of the symbols. + * @param[in] maxSymbolValue Maximum symbol value. + * @param rankPosition This is a scratch workspace. Must have RANK_POSITION_TABLE_SIZE entries. + */ + private static void HUF_sort(nodeElt_s* huffNode, uint* count, uint maxSymbolValue, rankPos* rankPosition) + { + int n; + int maxSymbolValue1 = (int)(maxSymbolValue) + 1; + + memset((void*)(rankPosition), (0), ((nuint)(sizeof(rankPos)) * 32)); + for (n = 0; n < maxSymbolValue1; ++n) + { + uint lowerRank = BIT_highbit32(count[n] + 1); + + rankPosition[lowerRank].@base++; + } + + assert(rankPosition[32 - 1].@base == 0); + for (n = 32 - 1; n > 0; --n) + { + rankPosition[n - 1].@base += rankPosition[n].@base; + rankPosition[n - 1].curr = rankPosition[n - 1].@base; + } + + for (n = 0; n < maxSymbolValue1; ++n) + { + uint c = count[n]; + uint r = BIT_highbit32(c + 1) + 1; + uint pos = rankPosition[r].curr++; + + while ((pos > rankPosition[r].@base) && (c > huffNode[pos - 1].count)) + { + huffNode[pos] = huffNode[pos - 1]; + pos--; + } + + huffNode[pos].count = c; + huffNode[pos].@byte = (byte)(n); + } + } + + /* HUF_buildTree(): + * Takes the huffNode array sorted by HUF_sort() and builds an unlimited-depth Huffman tree. + * + * @param huffNode The array sorted by HUF_sort(). Builds the Huffman tree in this array. + * @param maxSymbolValue The maximum symbol value. + * @return The smallest node in the Huffman tree (by count). + */ + private static int HUF_buildTree(nodeElt_s* huffNode, uint maxSymbolValue) + { + nodeElt_s* huffNode0 = huffNode - 1; + int nonNullRank; + int lowS, lowN; + int nodeNb = (255 + 1); + int n, nodeRoot; + + nonNullRank = (int)(maxSymbolValue); + while (huffNode[nonNullRank].count == 0) + { + nonNullRank--; + } + + lowS = nonNullRank; + nodeRoot = nodeNb + lowS - 1; + lowN = nodeNb; + huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS - 1].count; + huffNode[lowS].parent = huffNode[lowS - 1].parent = (ushort)(nodeNb); + nodeNb++; + lowS -= 2; + for (n = nodeNb; n <= nodeRoot; n++) + { + huffNode[n].count = (uint)(1U << 30); + } + + huffNode0[0].count = (uint)(1U << 31); + while (nodeNb <= nodeRoot) + { + int n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++; + int n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++; + + huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count; + huffNode[n1].parent = huffNode[n2].parent = (ushort)(nodeNb); + nodeNb++; + } + + huffNode[nodeRoot].nbBits = 0; + for (n = nodeRoot - 1; n >= (255 + 1); n--) + { + huffNode[n].nbBits = (byte)(huffNode[huffNode[n].parent].nbBits + 1); + } + + for (n = 0; n <= nonNullRank; n++) + { + huffNode[n].nbBits = (byte)(huffNode[huffNode[n].parent].nbBits + 1); + } + + return nonNullRank; + } + + /** + * HUF_buildCTableFromTree(): + * Build the CTable given the Huffman tree in huffNode. + * + * @param[out] CTable The output Huffman CTable. + * @param huffNode The Huffman tree. + * @param nonNullRank The last and smallest node in the Huffman tree. + * @param maxSymbolValue The maximum symbol value. + * @param maxNbBits The exact maximum number of bits used in the Huffman tree. + */ + private static void HUF_buildCTableFromTree(HUF_CElt_s* CTable, nodeElt_s* huffNode, int nonNullRank, uint maxSymbolValue, uint maxNbBits) + { + int n; + ushort* nbPerRank = stackalloc ushort[13]; + memset(nbPerRank, 0, sizeof(ushort) * 13); + ushort* valPerRank = stackalloc ushort[13]; + memset(valPerRank, 0, sizeof(ushort) * 13); + int alphabetSize = (int)(maxSymbolValue + 1); + + for (n = 0; n <= nonNullRank; n++) + { + nbPerRank[huffNode[n].nbBits]++; + } + + + { + ushort min = 0; + + for (n = (int)(maxNbBits); n > 0; n--) + { + valPerRank[n] = min; + min += (ushort)(nbPerRank[n]); + min >>= 1; + } + } + + for (n = 0; n < alphabetSize; n++) + { + CTable[huffNode[n].@byte].nbBits = huffNode[n].nbBits; + } + + for (n = 0; n < alphabetSize; n++) + { + CTable[n].val = valPerRank[CTable[n].nbBits]++; + } + } + + public static nuint HUF_buildCTable_wksp(HUF_CElt_s* tree, uint* count, uint maxSymbolValue, uint maxNbBits, void* workSpace, nuint wkspSize) + { + HUF_buildCTable_wksp_tables* wksp_tables = (HUF_buildCTable_wksp_tables*)(workSpace); + nodeElt_s* huffNode0 = (nodeElt_s*)wksp_tables->huffNodeTbl; + nodeElt_s* huffNode = huffNode0 + 1; + int nonNullRank; + + if (((nuint)(workSpace) & 3) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if (wkspSize < (nuint)(sizeof(HUF_buildCTable_wksp_tables))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_workSpace_tooSmall))); + } + + if (maxNbBits == 0) + { + maxNbBits = 11; + } + + if (maxSymbolValue > 255) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooLarge))); + } + + memset((void*)(huffNode0), (0), ((nuint)(sizeof(nodeElt_s) * 512))); + HUF_sort(huffNode, count, maxSymbolValue, (rankPos*)wksp_tables->rankPosition); + nonNullRank = HUF_buildTree(huffNode, maxSymbolValue); + maxNbBits = HUF_setMaxHeight(huffNode, (uint)(nonNullRank), maxNbBits); + if (maxNbBits > 12) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + HUF_buildCTableFromTree(tree, huffNode, nonNullRank, maxSymbolValue, maxNbBits); + return maxNbBits; + } + + public static nuint HUF_estimateCompressedSize(HUF_CElt_s* CTable, uint* count, uint maxSymbolValue) + { + nuint nbBits = 0; + int s; + + for (s = 0; s <= (int)(maxSymbolValue); ++s) + { + nbBits += CTable[s].nbBits * count[s]; + } + + return nbBits >> 3; + } + + public static int HUF_validateCTable(HUF_CElt_s* CTable, uint* count, uint maxSymbolValue) + { + int bad = 0; + int s; + + for (s = 0; s <= (int)(maxSymbolValue); ++s) + { + bad |= ((((count[s] != 0) && (CTable[s].nbBits == 0))) ? 1 : 0); + } + + return (bad == 0 ? 1 : 0); + } + + public static nuint HUF_compressBound(nuint size) + { + return (129 + (size + (size >> 8) + 8)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void HUF_encodeSymbol(BIT_CStream_t* bitCPtr, uint symbol, HUF_CElt_s* CTable) + { + BIT_addBitsFast(bitCPtr, CTable[symbol].val, CTable[symbol].nbBits); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint HUF_compress1X_usingCTable_internal_body(void* dst, nuint dstSize, void* src, nuint srcSize, HUF_CElt_s* CTable) + { + byte* ip = (byte*)(src); + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstSize; + byte* op = ostart; + nuint n; + BIT_CStream_t bitC; + + if (dstSize < 8) + { + return 0; + } + + + { + nuint initErr = BIT_initCStream(&bitC, (void*)op, (nuint)(oend - op)); + + if ((ERR_isError(initErr)) != 0) + { + return 0; + } + } + + n = srcSize & unchecked((nuint)unchecked(~3)); + switch (srcSize & 3) + { + case 3: + { + HUF_encodeSymbol(&bitC, ip[n + 2], CTable); + } + + if ((nuint)(sizeof(nuint)) * 8 < (uint)(12 * 4 + 7)) + { + BIT_flushBits(&bitC); + } + + + goto case 2; + case 2: + { + HUF_encodeSymbol(&bitC, ip[n + 1], CTable); + } + + if ((nuint)(sizeof(nuint)) * 8 < (uint)(12 * 2 + 7)) + { + BIT_flushBits(&bitC); + } + + + goto case 1; + case 1: + { + HUF_encodeSymbol(&bitC, ip[n + 0], CTable); + } + + BIT_flushBits(&bitC); + + goto case 0; + case 0: + default: + { + break; + } + } + + for (; n > 0; n -= 4) + { + HUF_encodeSymbol(&bitC, ip[n - 1], CTable); + if ((nuint)(sizeof(nuint)) * 8 < (uint)(12 * 2 + 7)) + { + BIT_flushBits(&bitC); + } + + HUF_encodeSymbol(&bitC, ip[n - 2], CTable); + if ((nuint)(sizeof(nuint)) * 8 < (uint)(12 * 4 + 7)) + { + BIT_flushBits(&bitC); + } + + HUF_encodeSymbol(&bitC, ip[n - 3], CTable); + if ((nuint)(sizeof(nuint)) * 8 < (uint)(12 * 2 + 7)) + { + BIT_flushBits(&bitC); + } + + HUF_encodeSymbol(&bitC, ip[n - 4], CTable); + BIT_flushBits(&bitC); + } + + return BIT_closeCStream(&bitC); + } + + private static nuint HUF_compress1X_usingCTable_internal_bmi2(void* dst, nuint dstSize, void* src, nuint srcSize, HUF_CElt_s* CTable) + { + return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable); + } + + private static nuint HUF_compress1X_usingCTable_internal_default(void* dst, nuint dstSize, void* src, nuint srcSize, HUF_CElt_s* CTable) + { + return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable); + } + + private static nuint HUF_compress1X_usingCTable_internal(void* dst, nuint dstSize, void* src, nuint srcSize, HUF_CElt_s* CTable, int bmi2) + { + if (bmi2 != 0) + { + return HUF_compress1X_usingCTable_internal_bmi2(dst, dstSize, src, srcSize, CTable); + } + + return HUF_compress1X_usingCTable_internal_default(dst, dstSize, src, srcSize, CTable); + } + + public static nuint HUF_compress1X_usingCTable(void* dst, nuint dstSize, void* src, nuint srcSize, HUF_CElt_s* CTable) + { + return HUF_compress1X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, 0); + } + + private static nuint HUF_compress4X_usingCTable_internal(void* dst, nuint dstSize, void* src, nuint srcSize, HUF_CElt_s* CTable, int bmi2) + { + nuint segmentSize = (srcSize + 3) / 4; + byte* ip = (byte*)(src); + byte* iend = ip + srcSize; + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstSize; + byte* op = ostart; + + if (dstSize < (uint)(6 + 1 + 1 + 1 + 8)) + { + return 0; + } + + if (srcSize < 12) + { + return 0; + } + + op += 6; + assert(op <= oend); + + { + nuint cSize = HUF_compress1X_usingCTable_internal((void*)op, (nuint)(oend - op), (void*)ip, segmentSize, CTable, bmi2); + + if ((ERR_isError(cSize)) != 0) + { + return cSize; + } + + if (cSize == 0) + { + return 0; + } + + assert(cSize <= 65535); + MEM_writeLE16((void*)ostart, (ushort)(cSize)); + op += cSize; + } + + ip += segmentSize; + assert(op <= oend); + + { + nuint cSize = HUF_compress1X_usingCTable_internal((void*)op, (nuint)(oend - op), (void*)ip, segmentSize, CTable, bmi2); + + if ((ERR_isError(cSize)) != 0) + { + return cSize; + } + + if (cSize == 0) + { + return 0; + } + + assert(cSize <= 65535); + MEM_writeLE16((void*)(ostart + 2), (ushort)(cSize)); + op += cSize; + } + + ip += segmentSize; + assert(op <= oend); + + { + nuint cSize = HUF_compress1X_usingCTable_internal((void*)op, (nuint)(oend - op), (void*)ip, segmentSize, CTable, bmi2); + + if ((ERR_isError(cSize)) != 0) + { + return cSize; + } + + if (cSize == 0) + { + return 0; + } + + assert(cSize <= 65535); + MEM_writeLE16((void*)(ostart + 4), (ushort)(cSize)); + op += cSize; + } + + ip += segmentSize; + assert(op <= oend); + assert(ip <= iend); + + { + nuint cSize = HUF_compress1X_usingCTable_internal((void*)op, (nuint)(oend - op), (void*)ip, (nuint)(iend - ip), CTable, bmi2); + + if ((ERR_isError(cSize)) != 0) + { + return cSize; + } + + if (cSize == 0) + { + return 0; + } + + op += cSize; + } + + return (nuint)(op - ostart); + } + + public static nuint HUF_compress4X_usingCTable(void* dst, nuint dstSize, void* src, nuint srcSize, HUF_CElt_s* CTable) + { + return HUF_compress4X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, 0); + } + + private static nuint HUF_compressCTable_internal(byte* ostart, byte* op, byte* oend, void* src, nuint srcSize, HUF_nbStreams_e nbStreams, HUF_CElt_s* CTable, int bmi2) + { + nuint cSize = (nbStreams == HUF_nbStreams_e.HUF_singleStream) ? HUF_compress1X_usingCTable_internal((void*)op, (nuint)(oend - op), src, srcSize, CTable, bmi2) : HUF_compress4X_usingCTable_internal((void*)op, (nuint)(oend - op), src, srcSize, CTable, bmi2); + + if ((ERR_isError(cSize)) != 0) + { + return cSize; + } + + if (cSize == 0) + { + return 0; + } + + op += cSize; + assert(op >= ostart); + if ((nuint)(op - ostart) >= srcSize - 1) + { + return 0; + } + + return (nuint)(op - ostart); + } + + /* HUF_compress_internal() : + * `workSpace_align4` must be aligned on 4-bytes boundaries, + * and occupies the same space as a table of HUF_WORKSPACE_SIZE_U32 unsigned */ + private static nuint HUF_compress_internal(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog, HUF_nbStreams_e nbStreams, void* workSpace_align4, nuint wkspSize, HUF_CElt_s* oldHufTable, HUF_repeat* repeat, int preferRepeat, int bmi2) + { + HUF_compress_tables_t* table = (HUF_compress_tables_t*)(workSpace_align4); + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstSize; + byte* op = ostart; + + assert(((nuint)(workSpace_align4) & 3) == 0); + if (wkspSize < (uint)(((6 << 10) + 256))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_workSpace_tooSmall))); + } + + if (srcSize == 0) + { + return 0; + } + + if (dstSize == 0) + { + return 0; + } + + if (srcSize > (uint)((128 * 1024))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (huffLog > 12) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + if (maxSymbolValue > 255) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooLarge))); + } + + if (maxSymbolValue == 0) + { + maxSymbolValue = 255; + } + + if (huffLog == 0) + { + huffLog = 11; + } + + if (preferRepeat != 0 && repeat != null && *repeat == HUF_repeat.HUF_repeat_valid) + { + return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, oldHufTable, bmi2); + } + + + { + nuint largest = HIST_count_wksp((uint*)table->count, &maxSymbolValue, (void*)(byte*)(src), srcSize, workSpace_align4, wkspSize); + + if ((ERR_isError(largest)) != 0) + { + return largest; + } + + if (largest == srcSize) + { + *ostart = ((byte*)(src))[0]; + return 1; + } + + if (largest <= (srcSize >> 7) + 4) + { + return 0; + } + } + + if (repeat != null && *repeat == HUF_repeat.HUF_repeat_check && (HUF_validateCTable(oldHufTable, (uint*)table->count, maxSymbolValue)) == 0) + { + *repeat = HUF_repeat.HUF_repeat_none; + } + + if (preferRepeat != 0 && repeat != null && *repeat != HUF_repeat.HUF_repeat_none) + { + return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, oldHufTable, bmi2); + } + + huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue); + + { + nuint maxBits = HUF_buildCTable_wksp((HUF_CElt_s*)table->CTable, (uint*)table->count, maxSymbolValue, huffLog, (void*)&table->wksps.buildCTable_wksp, (nuint)(4352)); + + + { + nuint _var_err__ = maxBits; + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + huffLog = (uint)(maxBits); + memset((void*)((table->CTable + (maxSymbolValue + 1))), (0), ((nuint)(sizeof(HUF_CElt_s) * 256) - ((maxSymbolValue + 1) * (nuint)(sizeof(HUF_CElt_s))))); + } + + + { + nuint hSize = HUF_writeCTable_wksp((void*)op, dstSize, (HUF_CElt_s*)table->CTable, maxSymbolValue, huffLog, (void*)&table->wksps.writeCTable_wksp, (nuint)(704)); + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + if (repeat != null && *repeat != HUF_repeat.HUF_repeat_none) + { + nuint oldSize = HUF_estimateCompressedSize(oldHufTable, (uint*)table->count, maxSymbolValue); + nuint newSize = HUF_estimateCompressedSize((HUF_CElt_s*)table->CTable, (uint*)table->count, maxSymbolValue); + + if (oldSize <= hSize + newSize || hSize + 12 >= srcSize) + { + return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, oldHufTable, bmi2); + } + } + + if (hSize + 12U >= srcSize) + { + return 0; + } + + op += hSize; + if (repeat != null) + { + *repeat = HUF_repeat.HUF_repeat_none; + } + + if (oldHufTable != null) + { + memcpy((void*)(oldHufTable), (void*)(table->CTable), ((nuint)(sizeof(HUF_CElt_s) * 256))); + } + } + + return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, (HUF_CElt_s*)table->CTable, bmi2); + } + + public static nuint HUF_compress1X_wksp(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog, void* workSpace, nuint wkspSize) + { + return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, HUF_nbStreams_e.HUF_singleStream, workSpace, wkspSize, (HUF_CElt_s*)null, (HUF_repeat*)null, 0, 0); + } + + /** HUF_compress1X_repeat() : + * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none. + * If it uses hufTable it does not modify hufTable or repeat. + * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used. + * If preferRepeat then the old table will always be used if valid. */ + public static nuint HUF_compress1X_repeat(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog, void* workSpace, nuint wkspSize, HUF_CElt_s* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2) + { + return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, HUF_nbStreams_e.HUF_singleStream, workSpace, wkspSize, hufTable, repeat, preferRepeat, bmi2); + } + + /* HUF_compress4X_repeat(): + * compress input using 4 streams. + * provide workspace to generate compression tables */ + public static nuint HUF_compress4X_wksp(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog, void* workSpace, nuint wkspSize) + { + return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, HUF_nbStreams_e.HUF_fourStreams, workSpace, wkspSize, (HUF_CElt_s*)null, (HUF_repeat*)null, 0, 0); + } + + /* HUF_compress4X_repeat(): + * compress input using 4 streams. + * re-use an existing huffman compression table */ + public static nuint HUF_compress4X_repeat(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog, void* workSpace, nuint wkspSize, HUF_CElt_s* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2) + { + return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, HUF_nbStreams_e.HUF_fourStreams, workSpace, wkspSize, hufTable, repeat, preferRepeat, bmi2); + } + + /** HUF_buildCTable() : + * @return : maxNbBits + * Note : count is used before tree is written, so they can safely overlap + */ + public static nuint HUF_buildCTable(HUF_CElt_s* tree, uint* count, uint maxSymbolValue, uint maxNbBits) + { + HUF_buildCTable_wksp_tables workspace; + + return HUF_buildCTable_wksp(tree, count, maxSymbolValue, maxNbBits, (void*)&workspace, (nuint)(sizeof(HUF_buildCTable_wksp_tables))); + } + + /* ====================== */ + /* single stream variants */ + /* ====================== */ + public static nuint HUF_compress1X(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog) + { + uint* workSpace = stackalloc uint[1600]; + + return HUF_compress1X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, (void*)workSpace, (nuint)(sizeof(uint) * 1600)); + } + + /** HUF_compress2() : + * Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`. + * `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX . + * `tableLog` must be `<= HUF_TABLELOG_MAX` . */ + public static nuint HUF_compress2(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog) + { + uint* workSpace = stackalloc uint[1600]; + + return HUF_compress4X_wksp(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, (void*)workSpace, (nuint)(sizeof(uint) * 1600)); + } + + /** HUF_compress() : + * Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'. + * 'dst' buffer must be already allocated. + * Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize). + * `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB. + * @return : size of compressed data (<= `dstCapacity`). + * Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! + * if HUF_isError(return), compression failed (more details using HUF_getErrorName()) + */ + public static nuint HUF_compress(void* dst, nuint maxDstSize, void* src, nuint srcSize) + { + return HUF_compress2(dst, maxDstSize, src, srcSize, 255, 11); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/HufDecompress.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/HufDecompress.cs new file mode 100644 index 00000000..8b32644f --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/HufDecompress.cs @@ -0,0 +1,1836 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + private static DTableDesc HUF_getDTableDesc(uint* table) + { + DTableDesc dtd; + + memcpy((void*)(&dtd), (void*)(table), ((nuint)(sizeof(DTableDesc)))); + return dtd; + } + + /** + * Packs 4 HUF_DEltX1 structs into a U64. This is used to lay down 4 entries at + * a time. + */ + [InlineMethod.Inline] + private static ulong HUF_DEltX1_set4(byte symbol, byte nbBits) + { + ulong D4; + + if (BitConverter.IsLittleEndian) + { + D4 = (ulong)(symbol + (nbBits << 8)); + } + else + { + D4 = (ulong)((symbol << 8) + nbBits); + } + + D4 *= 0x0001000100010001UL; + return D4; + } + + public static nuint HUF_readDTableX1_wksp(uint* DTable, void* src, nuint srcSize, void* workSpace, nuint wkspSize) + { + return HUF_readDTableX1_wksp_bmi2(DTable, src, srcSize, workSpace, wkspSize, 0); + } + + public static nuint HUF_readDTableX1_wksp_bmi2(uint* DTable, void* src, nuint srcSize, void* workSpace, nuint wkspSize, int bmi2) + { + uint tableLog = 0; + uint nbSymbols = 0; + nuint iSize; + void* dtPtr = (void*)(DTable + 1); + HUF_DEltX1* dt = (HUF_DEltX1*)(dtPtr); + HUF_ReadDTableX1_Workspace* wksp = (HUF_ReadDTableX1_Workspace*)(workSpace); + + if ((nuint)(sizeof(HUF_ReadDTableX1_Workspace)) > wkspSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + iSize = HUF_readStats_wksp((byte*)wksp->huffWeight, (nuint)(255 + 1), (uint*)wksp->rankVal, &nbSymbols, &tableLog, src, srcSize, (void*)wksp->statsWksp, (nuint)(sizeof(uint) * 218), bmi2); + if ((ERR_isError(iSize)) != 0) + { + return iSize; + } + + + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + if (tableLog > (uint)(dtd.maxTableLog + 1)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + dtd.tableType = 0; + dtd.tableLog = (byte)(tableLog); + memcpy((void*)(DTable), (void*)(&dtd), ((nuint)(sizeof(DTableDesc)))); + } + + + { + int n; + int nextRankStart = 0; + int unroll = 4; + int nLimit = (int)(nbSymbols) - unroll + 1; + + for (n = 0; n < (int)(tableLog) + 1; n++) + { + uint curr = (uint)nextRankStart; + + nextRankStart += (int)((int)(wksp->rankVal[n])); + wksp->rankStart[n] = curr; + } + + for (n = 0; n < nLimit; n += unroll) + { + int u; + + for (u = 0; u < unroll; ++u) + { + nuint w = wksp->huffWeight[n + u]; + + wksp->symbols[wksp->rankStart[w]++] = (byte)(n + u); + } + } + + for (; n < (int)(nbSymbols); ++n) + { + nuint w = wksp->huffWeight[n]; + + wksp->symbols[wksp->rankStart[w]++] = (byte)(n); + } + } + + + { + uint w; + int symbol = (int)(wksp->rankVal[0]); + int rankStart = 0; + + for (w = 1; w < tableLog + 1; ++w) + { + int symbolCount = (int)(wksp->rankVal[w]); + int length = (1 << (int)w) >> 1; + int uStart = rankStart; + byte nbBits = (byte)(tableLog + 1 - w); + int s; + int u; + + switch (length) + { + case 1: + { + for (s = 0; s < symbolCount; ++s) + { + HUF_DEltX1 D; + + D.@byte = wksp->symbols[symbol + s]; + D.nbBits = nbBits; + dt[uStart] = D; + uStart += 1; + } + } + + break; + case 2: + { + for (s = 0; s < symbolCount; ++s) + { + HUF_DEltX1 D; + + D.@byte = wksp->symbols[symbol + s]; + D.nbBits = nbBits; + dt[uStart + 0] = D; + dt[uStart + 1] = D; + uStart += 2; + } + } + + break; + case 4: + { + for (s = 0; s < symbolCount; ++s) + { + ulong D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits); + + MEM_write64((void*)(dt + uStart), D4); + uStart += 4; + } + } + + break; + case 8: + { + for (s = 0; s < symbolCount; ++s) + { + ulong D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits); + + MEM_write64((void*)(dt + uStart), D4); + MEM_write64((void*)(dt + uStart + 4), D4); + uStart += 8; + } + } + + break; + default: + { + for (s = 0; s < symbolCount; ++s) + { + ulong D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits); + + for (u = 0; u < length; u += 16) + { + MEM_write64((void*)(dt + uStart + u + 0), D4); + MEM_write64((void*)(dt + uStart + u + 4), D4); + MEM_write64((void*)(dt + uStart + u + 8), D4); + MEM_write64((void*)(dt + uStart + u + 12), D4); + } + + assert(u == length); + uStart += length; + } + } + + break; + } + + symbol += symbolCount; + rankStart += symbolCount * length; + } + } + + return iSize; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static byte HUF_decodeSymbolX1(BIT_DStream_t* Dstream, HUF_DEltX1* dt, uint dtLog) + { + nuint val = BIT_lookBitsFast(Dstream, dtLog); + byte c = dt[val].@byte; + + BIT_skipBits(Dstream, dt[val].nbBits); + return c; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint HUF_decodeStreamX1(byte* p, BIT_DStream_t* bitDPtr, byte* pEnd, HUF_DEltX1* dt, uint dtLog) + { + byte* pStart = p; + + while (((BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished) && (p < pEnd - 3))) + { + if (MEM_64bits) + { + *p++ = HUF_decodeSymbolX1(bitDPtr, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + *p++ = HUF_decodeSymbolX1(bitDPtr, dt, dtLog); + } + + if (MEM_64bits) + { + *p++ = HUF_decodeSymbolX1(bitDPtr, dt, dtLog); + } + + *p++ = HUF_decodeSymbolX1(bitDPtr, dt, dtLog); + } + + if (MEM_32bits) + { + while (((BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished) && (p < pEnd))) + { + *p++ = HUF_decodeSymbolX1(bitDPtr, dt, dtLog); + } + } + + while (p < pEnd) + { + *p++ = HUF_decodeSymbolX1(bitDPtr, dt, dtLog); + } + + return (nuint)(pEnd - pStart); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint HUF_decompress1X1_usingDTable_internal_body(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + byte* op = (byte*)(dst); + byte* oend = op + dstSize; + void* dtPtr = (void*)(DTable + 1); + HUF_DEltX1* dt = (HUF_DEltX1*)(dtPtr); + BIT_DStream_t bitD; + DTableDesc dtd = HUF_getDTableDesc(DTable); + uint dtLog = dtd.tableLog; + + + { + nuint _var_err__ = BIT_initDStream(&bitD, cSrc, cSrcSize); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + HUF_decodeStreamX1(op, &bitD, oend, dt, dtLog); + if ((BIT_endOfDStream(&bitD)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + return dstSize; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint HUF_decompress4X1_usingDTable_internal_body(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + if (cSrcSize < 10) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + byte* istart = (byte*)(cSrc); + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstSize; + byte* olimit = oend - 3; + void* dtPtr = (void*)(DTable + 1); + HUF_DEltX1* dt = (HUF_DEltX1*)(dtPtr); + BIT_DStream_t bitD1; + BIT_DStream_t bitD2; + BIT_DStream_t bitD3; + BIT_DStream_t bitD4; + nuint length1 = MEM_readLE16((void*)istart); + nuint length2 = MEM_readLE16((void*)(istart + 2)); + nuint length3 = MEM_readLE16((void*)(istart + 4)); + nuint length4 = cSrcSize - (length1 + length2 + length3 + 6); + byte* istart1 = istart + 6; + byte* istart2 = istart1 + length1; + byte* istart3 = istart2 + length2; + byte* istart4 = istart3 + length3; + nuint segmentSize = (dstSize + 3) / 4; + byte* opStart2 = ostart + segmentSize; + byte* opStart3 = opStart2 + segmentSize; + byte* opStart4 = opStart3 + segmentSize; + byte* op1 = ostart; + byte* op2 = opStart2; + byte* op3 = opStart3; + byte* op4 = opStart4; + DTableDesc dtd = HUF_getDTableDesc(DTable); + uint dtLog = dtd.tableLog; + uint endSignal = 1; + + if (length4 > cSrcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + nuint _var_err__ = BIT_initDStream(&bitD1, (void*)istart1, length1); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint _var_err__ = BIT_initDStream(&bitD2, (void*)istart2, length2); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint _var_err__ = BIT_initDStream(&bitD3, (void*)istart3, length3); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint _var_err__ = BIT_initDStream(&bitD4, (void*)istart4, length4); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + for (; ((endSignal) & (uint)((((op4 < olimit)) ? 1 : 0))) != 0;) + { + if (MEM_64bits) + { + *op1++ = HUF_decodeSymbolX1(&bitD1, dt, dtLog); + } + + if (MEM_64bits) + { + *op2++ = HUF_decodeSymbolX1(&bitD2, dt, dtLog); + } + + if (MEM_64bits) + { + *op3++ = HUF_decodeSymbolX1(&bitD3, dt, dtLog); + } + + if (MEM_64bits) + { + *op4++ = HUF_decodeSymbolX1(&bitD4, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + *op1++ = HUF_decodeSymbolX1(&bitD1, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + *op2++ = HUF_decodeSymbolX1(&bitD2, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + *op3++ = HUF_decodeSymbolX1(&bitD3, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + *op4++ = HUF_decodeSymbolX1(&bitD4, dt, dtLog); + } + + if (MEM_64bits) + { + *op1++ = HUF_decodeSymbolX1(&bitD1, dt, dtLog); + } + + if (MEM_64bits) + { + *op2++ = HUF_decodeSymbolX1(&bitD2, dt, dtLog); + } + + if (MEM_64bits) + { + *op3++ = HUF_decodeSymbolX1(&bitD3, dt, dtLog); + } + + if (MEM_64bits) + { + *op4++ = HUF_decodeSymbolX1(&bitD4, dt, dtLog); + } + + *op1++ = HUF_decodeSymbolX1(&bitD1, dt, dtLog); + *op2++ = HUF_decodeSymbolX1(&bitD2, dt, dtLog); + *op3++ = HUF_decodeSymbolX1(&bitD3, dt, dtLog); + *op4++ = HUF_decodeSymbolX1(&bitD4, dt, dtLog); + endSignal &= ((BIT_reloadDStreamFast(&bitD1) == BIT_DStream_status.BIT_DStream_unfinished) ? 1U : 0U); + endSignal &= ((BIT_reloadDStreamFast(&bitD2) == BIT_DStream_status.BIT_DStream_unfinished) ? 1U : 0U); + endSignal &= ((BIT_reloadDStreamFast(&bitD3) == BIT_DStream_status.BIT_DStream_unfinished) ? 1U : 0U); + endSignal &= ((BIT_reloadDStreamFast(&bitD4) == BIT_DStream_status.BIT_DStream_unfinished) ? 1U : 0U); + } + + if (op1 > opStart2) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (op2 > opStart3) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (op3 > opStart4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + HUF_decodeStreamX1(op1, &bitD1, opStart2, dt, dtLog); + HUF_decodeStreamX1(op2, &bitD2, opStart3, dt, dtLog); + HUF_decodeStreamX1(op3, &bitD3, opStart4, dt, dtLog); + HUF_decodeStreamX1(op4, &bitD4, oend, dt, dtLog); + + { + uint endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); + + if (endCheck == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + } + + return dstSize; + } + } + + private static nuint HUF_decompress1X1_usingDTable_internal_default(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + return HUF_decompress1X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress1X1_usingDTable_internal_bmi2(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + return HUF_decompress1X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress1X1_usingDTable_internal(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, int bmi2) + { + if (bmi2 != 0) + { + return HUF_decompress1X1_usingDTable_internal_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); + } + + return HUF_decompress1X1_usingDTable_internal_default(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress4X1_usingDTable_internal_default(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + return HUF_decompress4X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress4X1_usingDTable_internal_bmi2(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + return HUF_decompress4X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress4X1_usingDTable_internal(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, int bmi2) + { + if (bmi2 != 0) + { + return HUF_decompress4X1_usingDTable_internal_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); + } + + return HUF_decompress4X1_usingDTable_internal_default(dst, dstSize, cSrc, cSrcSize, DTable); + } + + public static nuint HUF_decompress1X1_usingDTable(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + if (dtd.tableType != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + return HUF_decompress1X1_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, 0); + } + + public static nuint HUF_decompress1X1_DCtx_wksp(uint* DCtx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize) + { + byte* ip = (byte*)(cSrc); + nuint hSize = HUF_readDTableX1_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize); + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + if (hSize >= cSrcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + ip += hSize; + cSrcSize -= hSize; + return HUF_decompress1X1_usingDTable_internal(dst, dstSize, (void*)ip, cSrcSize, DCtx, 0); + } + + public static nuint HUF_decompress4X1_usingDTable(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + if (dtd.tableType != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + return HUF_decompress4X1_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, 0); + } + + private static nuint HUF_decompress4X1_DCtx_wksp_bmi2(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int bmi2) + { + byte* ip = (byte*)(cSrc); + nuint hSize = HUF_readDTableX1_wksp_bmi2(dctx, cSrc, cSrcSize, workSpace, wkspSize, bmi2); + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + if (hSize >= cSrcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + ip += hSize; + cSrcSize -= hSize; + return HUF_decompress4X1_usingDTable_internal(dst, dstSize, (void*)ip, cSrcSize, dctx, bmi2); + } + + public static nuint HUF_decompress4X1_DCtx_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize) + { + return HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, 0); + } + + /* HUF_fillDTableX2Level2() : + * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */ + [InlineMethod.Inline] + private static void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, uint sizeLog, uint consumed, uint* rankValOrigin, int minWeight, sortedSymbol_t* sortedSymbols, uint sortedListSize, uint nbBitsBaseline, ushort baseSeq, uint* wksp, nuint wkspSize) + { + HUF_DEltX2 DElt; + uint* rankVal = wksp; + + assert(wkspSize >= (uint)(12 + 1)); + memcpy((void*)(rankVal), (void*)(rankValOrigin), ((nuint)(sizeof(uint)) * (uint)((12 + 1)))); + if (minWeight > 1) + { + uint i, skipSize = rankVal[minWeight]; + + MEM_writeLE16((void*)&(DElt.sequence), baseSeq); + DElt.nbBits = (byte)(consumed); + DElt.length = 1; + for (i = 0; i < skipSize; i++) + { + DTable[i] = DElt; + } + } + + + { + uint s; + + for (s = 0; s < sortedListSize; s++) + { + uint symbol = sortedSymbols[s].symbol; + uint weight = sortedSymbols[s].weight; + uint nbBits = nbBitsBaseline - weight; + uint length = (uint)(1 << (int)(sizeLog - nbBits)); + uint start = rankVal[weight]; + uint i = start; + uint end = start + length; + + MEM_writeLE16((void*)&(DElt.sequence), (ushort)(baseSeq + (symbol << 8))); + DElt.nbBits = (byte)(nbBits + consumed); + DElt.length = 2; + do + { + DTable[i++] = DElt; + } + while (i < end); + + rankVal[weight] += length; + } + } + } + + private static void HUF_fillDTableX2(HUF_DEltX2* DTable, uint targetLog, sortedSymbol_t* sortedList, uint sortedListSize, uint* rankStart, rankValCol_t* rankValOrigin, uint maxWeight, uint nbBitsBaseline, uint* wksp, nuint wkspSize) + { + uint* rankVal = wksp; + int scaleLog = (int)(nbBitsBaseline - targetLog); + uint minBits = nbBitsBaseline - maxWeight; + uint s; + + assert(wkspSize >= (uint)(12 + 1)); + wksp += 12 + 1; + wkspSize -= (nuint)(12 + 1); + memcpy((void*)(rankVal), (void*)(rankValOrigin), ((nuint)(sizeof(uint)) * (uint)((12 + 1)))); + for (s = 0; s < sortedListSize; s++) + { + ushort symbol = sortedList[s].symbol; + uint weight = sortedList[s].weight; + uint nbBits = nbBitsBaseline - weight; + uint start = rankVal[weight]; + uint length = (uint)(1 << (int)(targetLog - nbBits)); + + if (targetLog - nbBits >= minBits) + { + uint sortedRank; + int minWeight = (int)(nbBits + (uint)scaleLog); + + if (minWeight < 1) + { + minWeight = 1; + } + + sortedRank = rankStart[minWeight]; + HUF_fillDTableX2Level2(DTable + start, targetLog - nbBits, nbBits, (uint*)(rankValOrigin[nbBits]), minWeight, sortedList + sortedRank, sortedListSize - sortedRank, nbBitsBaseline, symbol, wksp, wkspSize); + } + else + { + HUF_DEltX2 DElt; + + MEM_writeLE16((void*)&(DElt.sequence), symbol); + DElt.nbBits = (byte)(nbBits); + DElt.length = 1; + + { + uint end = start + length; + uint u; + + for (u = start; u < end; u++) + { + DTable[u] = DElt; + } + } + } + + rankVal[weight] += length; + } + } + + public static nuint HUF_readDTableX2_wksp(uint* DTable, void* src, nuint srcSize, void* workSpace, nuint wkspSize) + { + uint tableLog, maxW, sizeOfSort, nbSymbols; + DTableDesc dtd = HUF_getDTableDesc(DTable); + uint maxTableLog = dtd.maxTableLog; + nuint iSize; + void* dtPtr = (void*)(DTable + 1); + HUF_DEltX2* dt = (HUF_DEltX2*)(dtPtr); + uint* rankStart; + HUF_ReadDTableX2_Workspace* wksp = (HUF_ReadDTableX2_Workspace*)(workSpace); + + if ((nuint)(sizeof(HUF_ReadDTableX2_Workspace)) > wkspSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + rankStart = wksp->rankStart0 + 1; + memset((void*)(wksp->rankStats), (0), ((nuint)(sizeof(uint) * 13))); + memset((void*)(wksp->rankStart0), (0), ((nuint)(sizeof(uint) * 14))); + if (maxTableLog > 12) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + iSize = HUF_readStats_wksp((byte*)wksp->weightList, (nuint)(255 + 1), (uint*)wksp->rankStats, &nbSymbols, &tableLog, src, srcSize, (void*)wksp->calleeWksp, (nuint)(sizeof(uint) * 218), 0); + if ((ERR_isError(iSize)) != 0) + { + return iSize; + } + + if (tableLog > maxTableLog) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge))); + } + + for (maxW = tableLog; wksp->rankStats[maxW] == 0; maxW--) + { + } + + + { + uint w, nextRankStart = 0; + + for (w = 1; w < maxW + 1; w++) + { + uint curr = nextRankStart; + + nextRankStart += wksp->rankStats[w]; + rankStart[w] = curr; + } + + rankStart[0] = nextRankStart; + sizeOfSort = nextRankStart; + } + + + { + uint s; + + for (s = 0; s < nbSymbols; s++) + { + uint w = wksp->weightList[s]; + uint r = rankStart[w]++; + + wksp->sortedSymbol[r].symbol = (byte)(s); + wksp->sortedSymbol[r].weight = (byte)(w); + } + + rankStart[0] = 0; + } + + + { + uint* rankVal0 = (uint*)(wksp->rankVal[0]); + + + { + int rescale = (int)((maxTableLog - tableLog) - 1); + uint nextRankVal = 0; + uint w; + + for (w = 1; w < maxW + 1; w++) + { + uint curr = nextRankVal; + + nextRankVal += wksp->rankStats[w] << (int)(w + (uint)rescale); + rankVal0[w] = curr; + } + } + + + { + uint minBits = tableLog + 1 - maxW; + uint consumed; + + for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) + { + uint* rankValPtr = (uint*)(wksp->rankVal[consumed]); + uint w; + + for (w = 1; w < maxW + 1; w++) + { + rankValPtr[w] = rankVal0[w] >> (int)consumed; + } + } + } + } + + HUF_fillDTableX2(dt, maxTableLog, (sortedSymbol_t*)wksp->sortedSymbol, sizeOfSort, (uint*)wksp->rankStart0, wksp->rankVal, maxW, tableLog + 1, (uint*)wksp->calleeWksp, (nuint)(sizeof(uint) * 218) / (nuint)(sizeof(uint))); + dtd.tableLog = (byte)(maxTableLog); + dtd.tableType = 1; + memcpy((void*)(DTable), (void*)(&dtd), ((nuint)(sizeof(DTableDesc)))); + return iSize; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint HUF_decodeSymbolX2(void* op, BIT_DStream_t* DStream, HUF_DEltX2* dt, uint dtLog) + { + nuint val = BIT_lookBitsFast(DStream, dtLog); + + memcpy((op), (void*)((dt + val)), (2)); + BIT_skipBits(DStream, dt[val].nbBits); + return dt[val].length; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint HUF_decodeLastSymbolX2(void* op, BIT_DStream_t* DStream, HUF_DEltX2* dt, uint dtLog) + { + nuint val = BIT_lookBitsFast(DStream, dtLog); + + memcpy((op), (void*)((dt + val)), (1)); + if (dt[val].length == 1) + { + BIT_skipBits(DStream, dt[val].nbBits); + } + else + { + if (DStream->bitsConsumed < ((nuint)(sizeof(nuint)) * 8)) + { + BIT_skipBits(DStream, dt[val].nbBits); + if (DStream->bitsConsumed > ((nuint)(sizeof(nuint)) * 8)) + { + DStream->bitsConsumed = (uint)(((nuint)(sizeof(nuint)) * 8)); + } + } + } + + return 1; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint HUF_decodeStreamX2(byte* p, BIT_DStream_t* bitDPtr, byte* pEnd, HUF_DEltX2* dt, uint dtLog) + { + byte* pStart = p; + + while (((BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished) && (p < pEnd - ((nuint)(sizeof(nuint)) - 1)))) + { + if (MEM_64bits) + { + p += HUF_decodeSymbolX2((void*)p, bitDPtr, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + p += HUF_decodeSymbolX2((void*)p, bitDPtr, dt, dtLog); + } + + if (MEM_64bits) + { + p += HUF_decodeSymbolX2((void*)p, bitDPtr, dt, dtLog); + } + + p += HUF_decodeSymbolX2((void*)p, bitDPtr, dt, dtLog); + } + + while (((BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished) && (p <= pEnd - 2))) + { + p += HUF_decodeSymbolX2((void*)p, bitDPtr, dt, dtLog); + } + + while (p <= pEnd - 2) + { + p += HUF_decodeSymbolX2((void*)p, bitDPtr, dt, dtLog); + } + + if (p < pEnd) + { + p += HUF_decodeLastSymbolX2((void*)p, bitDPtr, dt, dtLog); + } + + return (nuint)(p - pStart); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint HUF_decompress1X2_usingDTable_internal_body(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + BIT_DStream_t bitD; + + + { + nuint _var_err__ = BIT_initDStream(&bitD, cSrc, cSrcSize); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstSize; + void* dtPtr = (void*)(DTable + 1); + HUF_DEltX2* dt = (HUF_DEltX2*)(dtPtr); + DTableDesc dtd = HUF_getDTableDesc(DTable); + + HUF_decodeStreamX2(ostart, &bitD, oend, dt, dtd.tableLog); + } + + if ((BIT_endOfDStream(&bitD)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + return dstSize; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint HUF_decompress4X2_usingDTable_internal_body(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + if (cSrcSize < 10) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + byte* istart = (byte*)(cSrc); + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstSize; + byte* olimit = oend - ((nuint)(sizeof(nuint)) - 1); + void* dtPtr = (void*)(DTable + 1); + HUF_DEltX2* dt = (HUF_DEltX2*)(dtPtr); + BIT_DStream_t bitD1; + BIT_DStream_t bitD2; + BIT_DStream_t bitD3; + BIT_DStream_t bitD4; + nuint length1 = MEM_readLE16((void*)istart); + nuint length2 = MEM_readLE16((void*)(istart + 2)); + nuint length3 = MEM_readLE16((void*)(istart + 4)); + nuint length4 = cSrcSize - (length1 + length2 + length3 + 6); + byte* istart1 = istart + 6; + byte* istart2 = istart1 + length1; + byte* istart3 = istart2 + length2; + byte* istart4 = istart3 + length3; + nuint segmentSize = (dstSize + 3) / 4; + byte* opStart2 = ostart + segmentSize; + byte* opStart3 = opStart2 + segmentSize; + byte* opStart4 = opStart3 + segmentSize; + byte* op1 = ostart; + byte* op2 = opStart2; + byte* op3 = opStart3; + byte* op4 = opStart4; + uint endSignal = 1; + DTableDesc dtd = HUF_getDTableDesc(DTable); + uint dtLog = dtd.tableLog; + + if (length4 > cSrcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + nuint _var_err__ = BIT_initDStream(&bitD1, (void*)istart1, length1); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint _var_err__ = BIT_initDStream(&bitD2, (void*)istart2, length2); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint _var_err__ = BIT_initDStream(&bitD3, (void*)istart3, length3); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + + { + nuint _var_err__ = BIT_initDStream(&bitD4, (void*)istart4, length4); + + if ((ERR_isError(_var_err__)) != 0) + { + return _var_err__; + } + } + + for (; ((endSignal) & (uint)((((op4 < olimit)) ? 1 : 0))) != 0;) + { + if (MEM_64bits) + { + op1 += HUF_decodeSymbolX2((void*)op1, &bitD1, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + op1 += HUF_decodeSymbolX2((void*)op1, &bitD1, dt, dtLog); + } + + if (MEM_64bits) + { + op1 += HUF_decodeSymbolX2((void*)op1, &bitD1, dt, dtLog); + } + + op1 += HUF_decodeSymbolX2((void*)op1, &bitD1, dt, dtLog); + if (MEM_64bits) + { + op2 += HUF_decodeSymbolX2((void*)op2, &bitD2, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + op2 += HUF_decodeSymbolX2((void*)op2, &bitD2, dt, dtLog); + } + + if (MEM_64bits) + { + op2 += HUF_decodeSymbolX2((void*)op2, &bitD2, dt, dtLog); + } + + op2 += HUF_decodeSymbolX2((void*)op2, &bitD2, dt, dtLog); + endSignal &= ((BIT_reloadDStreamFast(&bitD1) == BIT_DStream_status.BIT_DStream_unfinished) ? 1U : 0U); + endSignal &= ((BIT_reloadDStreamFast(&bitD2) == BIT_DStream_status.BIT_DStream_unfinished) ? 1U : 0U); + if (MEM_64bits) + { + op3 += HUF_decodeSymbolX2((void*)op3, &bitD3, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + op3 += HUF_decodeSymbolX2((void*)op3, &bitD3, dt, dtLog); + } + + if (MEM_64bits) + { + op3 += HUF_decodeSymbolX2((void*)op3, &bitD3, dt, dtLog); + } + + op3 += HUF_decodeSymbolX2((void*)op3, &bitD3, dt, dtLog); + if (MEM_64bits) + { + op4 += HUF_decodeSymbolX2((void*)op4, &bitD4, dt, dtLog); + } + + if (MEM_64bits || (12 <= 12)) + { + op4 += HUF_decodeSymbolX2((void*)op4, &bitD4, dt, dtLog); + } + + if (MEM_64bits) + { + op4 += HUF_decodeSymbolX2((void*)op4, &bitD4, dt, dtLog); + } + + op4 += HUF_decodeSymbolX2((void*)op4, &bitD4, dt, dtLog); + endSignal &= ((BIT_reloadDStreamFast(&bitD3) == BIT_DStream_status.BIT_DStream_unfinished) ? 1U : 0U); + endSignal &= ((BIT_reloadDStreamFast(&bitD4) == BIT_DStream_status.BIT_DStream_unfinished) ? 1U : 0U); + } + + if (op1 > opStart2) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (op2 > opStart3) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (op3 > opStart4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog); + HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog); + HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog); + HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog); + + { + uint endCheck = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4); + + if (endCheck == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + } + + return dstSize; + } + } + + private static nuint HUF_decompress1X2_usingDTable_internal_default(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + return HUF_decompress1X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress1X2_usingDTable_internal_bmi2(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + return HUF_decompress1X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress1X2_usingDTable_internal(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, int bmi2) + { + if (bmi2 != 0) + { + return HUF_decompress1X2_usingDTable_internal_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); + } + + return HUF_decompress1X2_usingDTable_internal_default(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress4X2_usingDTable_internal_default(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + return HUF_decompress4X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress4X2_usingDTable_internal_bmi2(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + return HUF_decompress4X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + } + + private static nuint HUF_decompress4X2_usingDTable_internal(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, int bmi2) + { + if (bmi2 != 0) + { + return HUF_decompress4X2_usingDTable_internal_bmi2(dst, dstSize, cSrc, cSrcSize, DTable); + } + + return HUF_decompress4X2_usingDTable_internal_default(dst, dstSize, cSrc, cSrcSize, DTable); + } + + public static nuint HUF_decompress1X2_usingDTable(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + if (dtd.tableType != 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + return HUF_decompress1X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, 0); + } + + public static nuint HUF_decompress1X2_DCtx_wksp(uint* DCtx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize) + { + byte* ip = (byte*)(cSrc); + nuint hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize); + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + if (hSize >= cSrcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + ip += hSize; + cSrcSize -= hSize; + return HUF_decompress1X2_usingDTable_internal(dst, dstSize, (void*)ip, cSrcSize, DCtx, 0); + } + + public static nuint HUF_decompress4X2_usingDTable(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + if (dtd.tableType != 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + return HUF_decompress4X2_usingDTable_internal(dst, dstSize, cSrc, cSrcSize, DTable, 0); + } + + private static nuint HUF_decompress4X2_DCtx_wksp_bmi2(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int bmi2) + { + byte* ip = (byte*)(cSrc); + nuint hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize); + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + if (hSize >= cSrcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + ip += hSize; + cSrcSize -= hSize; + return HUF_decompress4X2_usingDTable_internal(dst, dstSize, (void*)ip, cSrcSize, dctx, bmi2); + } + + public static nuint HUF_decompress4X2_DCtx_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize) + { + return HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, 0); + } + + /* ***********************************/ + /* Universal decompression selectors */ + /* ***********************************/ + public static nuint HUF_decompress1X_usingDTable(void* dst, nuint maxDstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + return dtd.tableType != 0 ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, 0) : HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, 0); + } + + public static nuint HUF_decompress4X_usingDTable(void* dst, nuint maxDstSize, void* cSrc, nuint cSrcSize, uint* DTable) + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + return dtd.tableType != 0 ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, 0) : HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, 0); + } + + public static algo_time_t[][] algoTime = new algo_time_t[16][] + { + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 0, + decode256Time = 0, + }, + new algo_time_t + { + tableTime = 1, + decode256Time = 1, + }, + new algo_time_t + { + tableTime = 2, + decode256Time = 2, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 0, + decode256Time = 0, + }, + new algo_time_t + { + tableTime = 1, + decode256Time = 1, + }, + new algo_time_t + { + tableTime = 2, + decode256Time = 2, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 38, + decode256Time = 130, + }, + new algo_time_t + { + tableTime = 1313, + decode256Time = 74, + }, + new algo_time_t + { + tableTime = 2151, + decode256Time = 38, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 448, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 1353, + decode256Time = 74, + }, + new algo_time_t + { + tableTime = 2238, + decode256Time = 41, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 556, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 1353, + decode256Time = 74, + }, + new algo_time_t + { + tableTime = 2238, + decode256Time = 47, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 714, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 1418, + decode256Time = 74, + }, + new algo_time_t + { + tableTime = 2436, + decode256Time = 53, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 883, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 1437, + decode256Time = 74, + }, + new algo_time_t + { + tableTime = 2464, + decode256Time = 61, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 897, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 1515, + decode256Time = 75, + }, + new algo_time_t + { + tableTime = 2622, + decode256Time = 68, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 926, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 1613, + decode256Time = 75, + }, + new algo_time_t + { + tableTime = 2730, + decode256Time = 75, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 947, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 1729, + decode256Time = 77, + }, + new algo_time_t + { + tableTime = 3359, + decode256Time = 77, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 1107, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 2083, + decode256Time = 81, + }, + new algo_time_t + { + tableTime = 4006, + decode256Time = 84, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 1177, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 2379, + decode256Time = 87, + }, + new algo_time_t + { + tableTime = 4785, + decode256Time = 88, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 1242, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 2415, + decode256Time = 93, + }, + new algo_time_t + { + tableTime = 5155, + decode256Time = 84, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 1349, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 2644, + decode256Time = 106, + }, + new algo_time_t + { + tableTime = 5260, + decode256Time = 106, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 1455, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 2422, + decode256Time = 124, + }, + new algo_time_t + { + tableTime = 4174, + decode256Time = 124, + }, + }, + new algo_time_t[3] + { + new algo_time_t + { + tableTime = 722, + decode256Time = 128, + }, + new algo_time_t + { + tableTime = 1891, + decode256Time = 145, + }, + new algo_time_t + { + tableTime = 1936, + decode256Time = 146, + }, + }, + }; + + /** HUF_selectDecoder() : + * Tells which decoder is likely to decode faster, + * based on a set of pre-computed metrics. + * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 . + * Assumption : 0 < dstSize <= 128 KB */ + public static uint HUF_selectDecoder(nuint dstSize, nuint cSrcSize) + { + assert(dstSize > 0); + assert(dstSize <= (uint)(128 * 1024)); + + { + uint Q = (uint)((cSrcSize >= dstSize) ? 15 : (uint)(cSrcSize * 16 / dstSize)); + uint D256 = (uint)(dstSize >> 8); + uint DTime0 = algoTime[Q][0].tableTime + (algoTime[Q][0].decode256Time * D256); + uint DTime1 = algoTime[Q][1].tableTime + (algoTime[Q][1].decode256Time * D256); + + DTime1 += DTime1 >> 3; + return ((DTime1 < DTime0) ? 1U : 0U); + } + } + + public static nuint HUF_decompress4X_hufOnly_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize) + { + if (dstSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (cSrcSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + uint algoNb = HUF_selectDecoder(dstSize, cSrcSize); + + return algoNb != 0 ? HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize) : HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize); + } + } + + public static nuint HUF_decompress1X_DCtx_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize) + { + if (dstSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (cSrcSize > dstSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (cSrcSize == dstSize) + { + memcpy((dst), (cSrc), (dstSize)); + return dstSize; + } + + if (cSrcSize == 1) + { + memset((dst), (int)(*(byte*)(cSrc)), (dstSize)); + return dstSize; + } + + + { + uint algoNb = HUF_selectDecoder(dstSize, cSrcSize); + + return algoNb != 0 ? HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize) : HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize); + } + } + + /* BMI2 variants. + * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0. + */ + public static nuint HUF_decompress1X_usingDTable_bmi2(void* dst, nuint maxDstSize, void* cSrc, nuint cSrcSize, uint* DTable, int bmi2) + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + return dtd.tableType != 0 ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2) : HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2); + } + + public static nuint HUF_decompress1X1_DCtx_wksp_bmi2(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int bmi2) + { + byte* ip = (byte*)(cSrc); + nuint hSize = HUF_readDTableX1_wksp_bmi2(dctx, cSrc, cSrcSize, workSpace, wkspSize, bmi2); + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + if (hSize >= cSrcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + ip += hSize; + cSrcSize -= hSize; + return HUF_decompress1X1_usingDTable_internal(dst, dstSize, (void*)ip, cSrcSize, dctx, bmi2); + } + + public static nuint HUF_decompress4X_usingDTable_bmi2(void* dst, nuint maxDstSize, void* cSrc, nuint cSrcSize, uint* DTable, int bmi2) + { + DTableDesc dtd = HUF_getDTableDesc(DTable); + + return dtd.tableType != 0 ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2) : HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, bmi2); + } + + public static nuint HUF_decompress4X_hufOnly_wksp_bmi2(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int bmi2) + { + if (dstSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (cSrcSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + uint algoNb = HUF_selectDecoder(dstSize, cSrcSize); + + return algoNb != 0 ? HUF_decompress4X2_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2) : HUF_decompress4X1_DCtx_wksp_bmi2(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, bmi2); + } + } + + public static nuint HUF_readDTableX1(uint* DTable, void* src, nuint srcSize) + { + uint* workSpace = stackalloc uint[640]; + + return HUF_readDTableX1_wksp(DTable, src, srcSize, (void*)workSpace, (nuint)(sizeof(uint) * 640)); + } + + public static nuint HUF_decompress1X1_DCtx(uint* DCtx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* workSpace = stackalloc uint[640]; + + return HUF_decompress1X1_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize, (void*)workSpace, (nuint)(sizeof(uint) * 640)); + } + + public static nuint HUF_decompress1X1(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* DTable = stackalloc uint[2049]; + DTable[0] = ((uint)((12) - 1) * 0x01000001); + memset(DTable + 1, 0, sizeof(uint) * 2048); + + return HUF_decompress1X1_DCtx((uint*)DTable, dst, dstSize, cSrc, cSrcSize); + } + + public static nuint HUF_readDTableX2(uint* DTable, void* src, nuint srcSize) + { + uint* workSpace = stackalloc uint[640]; + + return HUF_readDTableX2_wksp(DTable, src, srcSize, (void*)workSpace, (nuint)(sizeof(uint) * 640)); + } + + public static nuint HUF_decompress1X2_DCtx(uint* DCtx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* workSpace = stackalloc uint[640]; + + return HUF_decompress1X2_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize, (void*)workSpace, (nuint)(sizeof(uint) * 640)); + } + + public static nuint HUF_decompress1X2(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* DTable = stackalloc uint[4097]; + DTable[0] = ((uint)(12) * 0x01000001); + memset(DTable + 1, 0, sizeof(uint) * 4096); + + return HUF_decompress1X2_DCtx((uint*)DTable, dst, dstSize, cSrc, cSrcSize); + } + + public static nuint HUF_decompress4X1_DCtx(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* workSpace = stackalloc uint[640]; + + return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, (void*)workSpace, (nuint)(sizeof(uint) * 640)); + } + + /* **************************************** + * Advanced decompression functions + ******************************************/ + public static nuint HUF_decompress4X1(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* DTable = stackalloc uint[2049]; + DTable[0] = ((uint)((12) - 1) * 0x01000001); + memset(DTable + 1, 0, sizeof(uint) * 2048); + + return HUF_decompress4X1_DCtx((uint*)DTable, dst, dstSize, cSrc, cSrcSize); + } + + public static nuint HUF_decompress4X2_DCtx(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* workSpace = stackalloc uint[640]; + + return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, (void*)workSpace, (nuint)(sizeof(uint) * 640)); + } + + public static nuint HUF_decompress4X2(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* DTable = stackalloc uint[4097]; + DTable[0] = ((uint)(12) * 0x01000001); + memset(DTable + 1, 0, sizeof(uint) * 4096); + + return HUF_decompress4X2_DCtx((uint*)DTable, dst, dstSize, cSrc, cSrcSize); + } + + /** HUF_decompress() : + * Decompress HUF data from buffer 'cSrc', of size 'cSrcSize', + * into already allocated buffer 'dst', of minimum size 'dstSize'. + * `originalSize` : **must** be the ***exact*** size of original (uncompressed) data. + * Note : in contrast with FSE, HUF_decompress can regenerate + * RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data, + * because it knows size to regenerate (originalSize). + * @return : size of regenerated data (== originalSize), + * or an error code, which can be tested using HUF_isError() + */ + public static nuint HUF_decompress(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + + + if (dstSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (cSrcSize > dstSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (cSrcSize == dstSize) + { + memcpy((dst), (cSrc), (dstSize)); + return dstSize; + } + + if (cSrcSize == 1) + { + memset((dst), (int)(*(byte*)(cSrc)), (dstSize)); + return dstSize; + } + + + { + uint algoNb = HUF_selectDecoder(dstSize, cSrcSize); + + return decompress[algoNb](dst, dstSize, cSrc, cSrcSize); + } + } + + public static nuint HUF_decompress4X_DCtx(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + if (dstSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (cSrcSize > dstSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (cSrcSize == dstSize) + { + memcpy((dst), (cSrc), (dstSize)); + return dstSize; + } + + if (cSrcSize == 1) + { + memset((dst), (int)(*(byte*)(cSrc)), (dstSize)); + return dstSize; + } + + + { + uint algoNb = HUF_selectDecoder(dstSize, cSrcSize); + + return algoNb != 0 ? HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) : HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize); + } + } + + public static nuint HUF_decompress4X_hufOnly(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* workSpace = stackalloc uint[640]; + + return HUF_decompress4X_hufOnly_wksp(dctx, dst, dstSize, cSrc, cSrcSize, (void*)workSpace, (nuint)(sizeof(uint) * 640)); + } + + public static nuint HUF_decompress1X_DCtx(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize) + { + uint* workSpace = stackalloc uint[640]; + + return HUF_decompress1X_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, (void*)workSpace, (nuint)(sizeof(uint) * 640)); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Mem.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Mem.cs new file mode 100644 index 00000000..2ead3bbc --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Mem.cs @@ -0,0 +1,225 @@ +using System; +using System.Buffers.Binary; +using System.Runtime.CompilerServices; +using InlineIL; +using static InlineIL.IL.Emit; + +// ReSharper disable InconsistentNaming +// ReSharper disable IdentifierTypo + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*-************************************************************** + * Memory I/O API + *****************************************************************/ + /*=== Static platform detection ===*/ + public static bool MEM_32bits => sizeof(nint) == 4; + + public static bool MEM_64bits => sizeof(nint) == 8; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + /* default method, safe and standard. + can sometimes prove slower */ + [InlineMethod.Inline] + private static ushort MEM_read16(void* memPtr) + { + Ldarg(nameof(memPtr)); + Unaligned(1); + Ldind_U2(); + return IL.Return(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint MEM_read32(void* memPtr) + { + Ldarg(nameof(memPtr)); + Unaligned(1); + Ldind_U4(); + return IL.Return(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static ulong MEM_read64(void* memPtr) + { + Ldarg(nameof(memPtr)); + Unaligned(1); + Ldind_I8(); + return IL.Return(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static nuint MEM_readST(void* memPtr) + { + Ldarg(nameof(memPtr)); + Unaligned(1); + Ldind_I(); + return IL.Return(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void MEM_write64(void* memPtr, ulong value) + { + Ldarg(nameof(memPtr)); + Ldarg(nameof(value)); + Unaligned(1); + Stind_I8(); + } + + /*=== Little endian r/w ===*/ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static ushort MEM_readLE16(void* memPtr) + { + Ldarg(nameof(memPtr)); + Unaligned(1); + Ldind_U2(); + + if (!BitConverter.IsLittleEndian) + { + Call(new MethodRef(typeof(BinaryPrimitives), nameof(BinaryPrimitives.ReverseEndianness), typeof(ushort))); + } + + return IL.Return(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void MEM_writeLE16(void* memPtr, ushort val) + { + Ldarg(nameof(memPtr)); + Ldarg(nameof(val)); + + if (!BitConverter.IsLittleEndian) + { + Call(new MethodRef(typeof(BinaryPrimitives), nameof(BinaryPrimitives.ReverseEndianness), + typeof(ushort))); + } + + Unaligned(1); + Stind_I2(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint MEM_readLE24(void* memPtr) => + (uint) (MEM_readLE16(memPtr) + (((byte*) memPtr)[2] << 16)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void MEM_writeLE24(void* memPtr, uint val) + { + MEM_writeLE16(memPtr, (ushort) val); + ((byte*) memPtr)[2] = (byte) (val >> 16); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint MEM_readLE32(void* memPtr) + { + Ldarg(nameof(memPtr)); + Unaligned(1); + Ldind_U4(); + + if (!BitConverter.IsLittleEndian) + { + Call(new MethodRef(typeof(BinaryPrimitives), nameof(BinaryPrimitives.ReverseEndianness), typeof(uint))); + } + + return IL.Return(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void MEM_writeLE32(void* memPtr, uint val32) + { + Ldarg(nameof(memPtr)); + Ldarg(nameof(val32)); + + if (!BitConverter.IsLittleEndian) + { + Call(new MethodRef(typeof(BinaryPrimitives), nameof(BinaryPrimitives.ReverseEndianness), + typeof(uint))); + } + + Unaligned(1); + Stind_I4(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static ulong MEM_readLE64(void* memPtr) + { + Ldarg(nameof(memPtr)); + Unaligned(1); + Ldind_I8(); + + if (!BitConverter.IsLittleEndian) + { + Call(new MethodRef(typeof(BinaryPrimitives), nameof(BinaryPrimitives.ReverseEndianness), typeof(ulong))); + } + + return IL.Return(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void MEM_writeLE64(void* memPtr, ulong val64) + { + Ldarg(nameof(memPtr)); + Ldarg(nameof(val64)); + + if (!BitConverter.IsLittleEndian) + { + Call(new MethodRef(typeof(BinaryPrimitives), nameof(BinaryPrimitives.ReverseEndianness), + typeof(ulong))); + } + + Unaligned(1); + Stind_I8(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static nuint MEM_readLEST(void* memPtr) + { + Ldarg(nameof(memPtr)); + Unaligned(1); + Ldind_I(); + + if (!BitConverter.IsLittleEndian) + { + Conv_U8(); + Call(new MethodRef(typeof(BinaryPrimitives), nameof(BinaryPrimitives.ReverseEndianness), + typeof(ulong))); + Conv_U(); + } + + return IL.Return(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void MEM_writeLEST(void* memPtr, nuint val) + { + Ldarg(nameof(memPtr)); + Ldarg(nameof(val)); + + if (!BitConverter.IsLittleEndian) + { + Conv_U8(); + Call(new MethodRef(typeof(BinaryPrimitives), nameof(BinaryPrimitives.ReverseEndianness), + typeof(ulong))); + Conv_U(); + } + + Unaligned(1); + Stind_I(); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/SeqCollector.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/SeqCollector.cs new file mode 100644 index 00000000..184de76a --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/SeqCollector.cs @@ -0,0 +1,15 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct SeqCollector + { + public int collectSequences; + + public ZSTD_Sequence* seqStart; + + public nuint seqIndex; + + public nuint maxSequences; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/XXH32_canonical_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/XXH32_canonical_t.cs new file mode 100644 index 00000000..f9350c03 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/XXH32_canonical_t.cs @@ -0,0 +1,17 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* ************************** + * Canonical representation + ****************************/ + /* Default result type for XXH functions are primitive unsigned 32 and 64 bits. + * The canonical representation uses human-readable write convention, aka big-endian (large digits first). + * These functions allow transformation of hash result into and from its canonical format. + * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs. + */ + public unsafe partial struct XXH32_canonical_t + { + public fixed byte digest[4]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/XXH32_state_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/XXH32_state_s.cs new file mode 100644 index 00000000..734eb667 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/XXH32_state_s.cs @@ -0,0 +1,30 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* These definitions are only meant to allow allocation of XXH state + statically, on stack, or in a struct for example. + Do not use members directly. */ + public unsafe partial struct XXH32_state_s + { + public uint total_len_32; + + public uint large_len; + + public uint v1; + + public uint v2; + + public uint v3; + + public uint v4; + + /* buffer defined as U32 for alignment */ + public fixed uint mem32[4]; + + public uint memsize; + + /* never read nor write, will be removed in a future version */ + public uint reserved; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/XXH64_state_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/XXH64_state_s.cs new file mode 100644 index 00000000..8d6d4c49 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/XXH64_state_s.cs @@ -0,0 +1,25 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct XXH64_state_s + { + public ulong total_len; + + public ulong v1; + + public ulong v2; + + public ulong v3; + + public ulong v4; + + /* buffer defined as U64 for alignment */ + public fixed ulong mem64[4]; + + public uint memsize; + + /* never read nor write, will be removed in a future version */ + public fixed uint reserved[2]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Xxhash.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Xxhash.cs new file mode 100644 index 00000000..2cdb1ef4 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Xxhash.cs @@ -0,0 +1,260 @@ +using System; +using System.Buffers.Binary; +using System.Numerics; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; +// ReSharper disable InconsistentNaming +// ReSharper disable IdentifierTypo + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + private static void XXH_memcpy(void* dest, void* src, ulong size) + { + memcpy((dest), (src), (size)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint XXH_readLE32(void* ptr) => + BitConverter.IsLittleEndian ? *(uint*) ptr : BinaryPrimitives.ReverseEndianness(*(uint*) ptr); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ulong XXH_readLE64(void* ptr) => + BitConverter.IsLittleEndian ? *(ulong*) ptr : BinaryPrimitives.ReverseEndianness(*(ulong*) ptr); + + public const ulong PRIME64_1 = 11400714785074694791UL; + + public const ulong PRIME64_2 = 14029467366897019727UL; + + public const ulong PRIME64_3 = 1609587929392839161UL; + + public const ulong PRIME64_4 = 9650029242287828579UL; + + public const ulong PRIME64_5 = 2870177450012600261UL; + + public static uint XXH_versionNumber() + { + return 0 * 100 * 100 + 6 * 100 + 2; + } + + private static ulong XXH64_round(ulong acc, ulong input) + { + acc += input * PRIME64_2; + acc = BitOperations.RotateLeft(acc, 31); + acc *= PRIME64_1; + return acc; + } + + private static ulong XXH64_mergeRound(ulong acc, ulong val) + { + val = XXH64_round(0, val); + acc ^= val; + acc = acc * PRIME64_1 + PRIME64_4; + return acc; + } + + public static void XXH64_reset(XXH64_state_s* statePtr, ulong seed) + { + XXH64_state_s state; + + memset(&state, (0), ((ulong)((sizeof(XXH64_state_s) - 8)))); + state.v1 = seed + PRIME64_1 + PRIME64_2; + state.v2 = seed + PRIME64_2; + state.v3 = seed + 0; + state.v4 = seed - PRIME64_1; + memcpy(statePtr, &state, ((ulong)sizeof(XXH64_state_s))); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void XXH64_update(XXH64_state_s* state, void* input, ulong len) + { + byte* p = (byte*)(input); + byte* bEnd = p + len; + + state->total_len += len; + if (state->memsize + len < 32) + { + if (input != null) + { + XXH_memcpy(((byte*)(state->mem64)) + state->memsize, input, len); + } + + state->memsize += (uint)(len); + return; + } + + if (state->memsize != 0) + { + XXH_memcpy(((byte*)(state->mem64)) + state->memsize, input, 32 - state->memsize); + state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64 + 0)); + state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64 + 1)); + state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64 + 2)); + state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64 + 3)); + p += 32 - state->memsize; + state->memsize = 0; + } + + if (p + 32 <= bEnd) + { + byte* limit = bEnd - 32; + ulong v1 = state->v1; + ulong v2 = state->v2; + ulong v3 = state->v3; + ulong v4 = state->v4; + + do + { + v1 = XXH64_round(v1, XXH_readLE64(p)); + p += 8; + v2 = XXH64_round(v2, XXH_readLE64(p)); + p += 8; + v3 = XXH64_round(v3, XXH_readLE64(p)); + p += 8; + v4 = XXH64_round(v4, XXH_readLE64(p)); + p += 8; + } + while (p <= limit); + + state->v1 = v1; + state->v2 = v2; + state->v3 = v3; + state->v4 = v4; + } + + if (p < bEnd) + { + XXH_memcpy(state->mem64, p, (ulong)(bEnd - p)); + state->memsize = (uint)(bEnd - p); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ulong XXH64_digest(XXH64_state_s* state) + { + byte* p = (byte*)(state->mem64); + byte* bEnd = (byte*)(state->mem64) + state->memsize; + ulong h64; + + if (state->total_len >= 32) + { + ulong v1 = state->v1; + ulong v2 = state->v2; + ulong v3 = state->v3; + ulong v4 = state->v4; + + h64 = BitOperations.RotateLeft(v1, 1) + BitOperations.RotateLeft(v2, 7) + BitOperations.RotateLeft(v3, 12) + BitOperations.RotateLeft(v4, 18); + h64 = XXH64_mergeRound(h64, v1); + h64 = XXH64_mergeRound(h64, v2); + h64 = XXH64_mergeRound(h64, v3); + h64 = XXH64_mergeRound(h64, v4); + } + else + { + h64 = state->v3 + PRIME64_5; + } + + h64 += state->total_len; + while (p + 8 <= bEnd) + { + ulong k1 = XXH64_round(0, XXH_readLE64(p)); + + h64 ^= k1; + h64 = BitOperations.RotateLeft(h64, 27) * PRIME64_1 + PRIME64_4; + p += 8; + } + + if (p + 4 <= bEnd) + { + h64 ^= XXH_readLE32(p) * PRIME64_1; + h64 = BitOperations.RotateLeft(h64, 23) * PRIME64_2 + PRIME64_3; + p += 4; + } + + while (p < bEnd) + { + h64 ^= *p * PRIME64_5; + h64 = BitOperations.RotateLeft(h64, 11) * PRIME64_1; + p++; + } + + h64 ^= h64>>33; + h64 *= PRIME64_2; + h64 ^= h64>>29; + h64 *= PRIME64_3; + h64 ^= h64>>32; + return h64; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ulong XXH64(void* input, ulong len, ulong seed) + { + byte* p = (byte*)(input); + byte* bEnd = p + len; + ulong h64; + + if (len >= ((ulong)(32))) + { + byte* limit = bEnd - 32; + ulong v1 = seed + PRIME64_1 + PRIME64_2; + ulong v2 = seed + PRIME64_2; + ulong v3 = seed + ((ulong)(0)); + ulong v4 = seed - PRIME64_1; + + do + { + v1 = XXH64_round(v1, XXH_readLE64(((void*)(p)))); + p += 8; + v2 = XXH64_round(v2, XXH_readLE64(((void*)(p)))); + p += 8; + v3 = XXH64_round(v3, XXH_readLE64(((void*)(p)))); + p += 8; + v4 = XXH64_round(v4, XXH_readLE64(((void*)(p)))); + p += 8; + } + while (p <= limit); + + h64 = BitOperations.RotateLeft(v1, 1) + BitOperations.RotateLeft(v2, 7) + BitOperations.RotateLeft(v3, 12) + BitOperations.RotateLeft(v4, 18); + h64 = XXH64_mergeRound(h64, v1); + h64 = XXH64_mergeRound(h64, v2); + h64 = XXH64_mergeRound(h64, v3); + h64 = XXH64_mergeRound(h64, v4); + } + else + { + h64 = seed + PRIME64_5; + } + + h64 += (ulong)(len); + while (p + 8 <= bEnd) + { + ulong k1 = XXH64_round(((ulong)(0)), XXH_readLE64(((void*)(p)))); + + h64 ^= k1; + h64 = BitOperations.RotateLeft(h64, 27) * PRIME64_1 + PRIME64_4; + p += 8; + } + + if (p + 4 <= bEnd) + { + h64 ^= (ulong)(XXH_readLE32(((void*)(p)))) * PRIME64_1; + h64 = BitOperations.RotateLeft(h64, 23) * PRIME64_2 + PRIME64_3; + p += 4; + } + + while (p < bEnd) + { + h64 ^= ((ulong)((*p))) * PRIME64_5; + h64 = BitOperations.RotateLeft(h64, 11) * PRIME64_1; + p++; + } + + h64 ^= h64 >> 33; + h64 *= PRIME64_2; + h64 ^= h64 >> 29; + h64 *= PRIME64_3; + h64 ^= h64 >> 32; + return h64; + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_cover_params_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_cover_params_t.cs new file mode 100644 index 00000000..b27d369e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_cover_params_t.cs @@ -0,0 +1,34 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*! ZDICT_cover_params_t: + * k and d are the only required parameters. + * For others, value 0 means default. + */ + public partial struct ZDICT_cover_params_t + { + /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */ + public uint k; + + /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */ + public uint d; + + /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */ + public uint steps; + + /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */ + public uint nbThreads; + + /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (1.0), 1.0 when all samples are used for both training and testing */ + public double splitPoint; + + /* Train dictionaries to shrink in size starting from the minimum size and selects the smallest dictionary that is shrinkDictMaxRegression% worse than the largest dictionary. 0 means no shrinking and 1 means shrinking */ + public uint shrinkDict; + + /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */ + public uint shrinkDictMaxRegression; + + public ZDICT_params_t zParams; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_fastCover_params_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_fastCover_params_t.cs new file mode 100644 index 00000000..7b875416 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_fastCover_params_t.cs @@ -0,0 +1,36 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZDICT_fastCover_params_t + { + /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */ + public uint k; + + /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */ + public uint d; + + /* log of size of frequency array : constraint: 0 < f <= 31 : 1 means default(20)*/ + public uint f; + + /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */ + public uint steps; + + /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */ + public uint nbThreads; + + /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (0.75), 1.0 when all samples are used for both training and testing */ + public double splitPoint; + + /* Acceleration level: constraint: 0 < accel <= 10, higher means faster and less accurate, 0 means default(1) */ + public uint accel; + + /* Train dictionaries to shrink in size starting from the minimum size and selects the smallest dictionary that is shrinkDictMaxRegression% worse than the largest dictionary. 0 means no shrinking and 1 means shrinking */ + public uint shrinkDict; + + /* Sets shrinkDictMaxRegression so that a smaller dictionary can be at worse shrinkDictMaxRegression% worse than the max dict size dictionary. */ + public uint shrinkDictMaxRegression; + + public ZDICT_params_t zParams; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_legacy_params_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_legacy_params_t.cs new file mode 100644 index 00000000..4845b4d2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_legacy_params_t.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZDICT_legacy_params_t + { + /* 0 means default; larger => select more => larger dictionary */ + public uint selectivityLevel; + + public ZDICT_params_t zParams; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_params_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_params_t.cs new file mode 100644 index 00000000..31cc6f1b --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZDICT_params_t.cs @@ -0,0 +1,23 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZDICT_params_t + { + /*< optimize for a specific zstd compression level; 0 means default */ + public int compressionLevel; + + /*< Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */ + public uint notificationLevel; + + /*< force dictID value; 0 means auto mode (32-bits random value) + * NOTE: The zstd format reserves some dictionary IDs for future use. + * You may use them in private settings, but be warned that they + * may be used by zstd in a public dictionary registry in the future. + * These dictionary IDs are: + * - low range : <= 32767 + * - high range : >= (2^31) + */ + public uint dictID; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_BuildCTableWksp.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_BuildCTableWksp.cs new file mode 100644 index 00000000..25bb00e2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_BuildCTableWksp.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_BuildCTableWksp + { + public fixed short norm[53]; + + public fixed uint wksp[182]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_CCtx_params_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_CCtx_params_s.cs new file mode 100644 index 00000000..83059c25 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_CCtx_params_s.cs @@ -0,0 +1,70 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_CCtx_params_s + { + public ZSTD_format_e format; + + public ZSTD_compressionParameters cParams; + + public ZSTD_frameParameters fParams; + + public int compressionLevel; + + /* force back-references to respect limit of + * 1< 0: + * If litLength != 0: + * rep == 1 --> offset == repeat_offset_1 + * rep == 2 --> offset == repeat_offset_2 + * rep == 3 --> offset == repeat_offset_3 + * If litLength == 0: + * rep == 1 --> offset == repeat_offset_2 + * rep == 2 --> offset == repeat_offset_3 + * rep == 3 --> offset == repeat_offset_1 - 1 + * + * Note: This field is optional. ZSTD_generateSequences() will calculate the value of + * 'rep', but repeat offsets do not necessarily need to be calculated from an external + * sequence provider's perspective. For example, ZSTD_compressSequences() does not + * use this 'rep' field at all (as of now). + */ + public uint rep; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_Vec256.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_Vec256.cs new file mode 100644 index 00000000..299ccf73 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_Vec256.cs @@ -0,0 +1,12 @@ +using System; +using System.Runtime.Intrinsics; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_Vec256 + { + public Vector128 fst; + + public Vector128 snd; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_blockCompressor.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_blockCompressor.cs new file mode 100644 index 00000000..2c575a37 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_blockCompressor.cs @@ -0,0 +1,8 @@ +using System; +using System.Runtime.InteropServices; + +namespace ZstdSharp.Unsafe +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public unsafe delegate nuint ZSTD_blockCompressor(ZSTD_matchState_t* bs, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize); +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_blockState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_blockState_t.cs new file mode 100644 index 00000000..418a7076 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_blockState_t.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_blockState_t + { + public ZSTD_compressedBlockState_t* prevCBlock; + + public ZSTD_compressedBlockState_t* nextCBlock; + + public ZSTD_matchState_t matchState; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_bounds.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_bounds.cs new file mode 100644 index 00000000..f69a9703 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_bounds.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_bounds + { + public nuint error; + + public int lowerBound; + + public int upperBound; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_bufferMode_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_bufferMode_e.cs new file mode 100644 index 00000000..66781ff2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_bufferMode_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_bufferMode_e + { + ZSTD_bm_buffered = 0, + ZSTD_bm_stable = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_buffered_policy_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_buffered_policy_e.cs new file mode 100644 index 00000000..d6b5a7ec --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_buffered_policy_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_buffered_policy_e + { + ZSTDb_not_buffered, + ZSTDb_buffered, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_buildSeqStore_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_buildSeqStore_e.cs new file mode 100644 index 00000000..6522959e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_buildSeqStore_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_buildSeqStore_e + { + ZSTDbss_compress, + ZSTDbss_noCompress, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cParamMode_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cParamMode_e.cs new file mode 100644 index 00000000..e0b84f5e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cParamMode_e.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_cParamMode_e + { + ZSTD_cpm_noAttachDict = 0, + ZSTD_cpm_attachDict = 1, + ZSTD_cpm_createCDict = 2, + ZSTD_cpm_unknown = 3, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cParameter.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cParameter.cs new file mode 100644 index 00000000..ca42ead6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cParameter.cs @@ -0,0 +1,42 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_cParameter + { + ZSTD_c_compressionLevel = 100, + ZSTD_c_windowLog = 101, + ZSTD_c_hashLog = 102, + ZSTD_c_chainLog = 103, + ZSTD_c_searchLog = 104, + ZSTD_c_minMatch = 105, + ZSTD_c_targetLength = 106, + ZSTD_c_strategy = 107, + ZSTD_c_enableLongDistanceMatching = 160, + ZSTD_c_ldmHashLog = 161, + ZSTD_c_ldmMinMatch = 162, + ZSTD_c_ldmBucketSizeLog = 163, + ZSTD_c_ldmHashRateLog = 164, + ZSTD_c_contentSizeFlag = 200, + ZSTD_c_checksumFlag = 201, + ZSTD_c_dictIDFlag = 202, + ZSTD_c_nbWorkers = 400, + ZSTD_c_jobSize = 401, + ZSTD_c_overlapLog = 402, + ZSTD_c_experimentalParam1 = 500, + ZSTD_c_experimentalParam2 = 10, + ZSTD_c_experimentalParam3 = 1000, + ZSTD_c_experimentalParam4 = 1001, + ZSTD_c_experimentalParam5 = 1002, + ZSTD_c_experimentalParam6 = 1003, + ZSTD_c_experimentalParam7 = 1004, + ZSTD_c_experimentalParam8 = 1005, + ZSTD_c_experimentalParam9 = 1006, + ZSTD_c_experimentalParam10 = 1007, + ZSTD_c_experimentalParam11 = 1008, + ZSTD_c_experimentalParam12 = 1009, + ZSTD_c_experimentalParam13 = 1010, + ZSTD_c_experimentalParam14 = 1011, + ZSTD_c_experimentalParam15 = 1012, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cStreamStage.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cStreamStage.cs new file mode 100644 index 00000000..f408aaeb --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cStreamStage.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_cStreamStage + { + zcss_init = 0, + zcss_load, + zcss_flush, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compResetPolicy_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compResetPolicy_e.cs new file mode 100644 index 00000000..a6a55584 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compResetPolicy_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_compResetPolicy_e + { + ZSTDcrp_makeClean, + ZSTDcrp_leaveDirty, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressedBlockState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressedBlockState_t.cs new file mode 100644 index 00000000..acebdd8a --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressedBlockState_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_compressedBlockState_t + { + public ZSTD_entropyCTables_t entropy; + + public fixed uint rep[3]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressionParameters.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressionParameters.cs new file mode 100644 index 00000000..f8c602bc --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressionParameters.cs @@ -0,0 +1,28 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_compressionParameters + { + /**< largest match distance : larger == more compression, more memory needed during decompression */ + public uint windowLog; + + /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ + public uint chainLog; + + /**< dispatch table : larger == faster, more memory */ + public uint hashLog; + + /**< nb of searches : larger == more compression, slower */ + public uint searchLog; + + /**< match length searched : larger == faster decompression, sometimes less compression */ + public uint minMatch; + + /**< acceptable match size for optimal parser (only) : larger == more compression, slower */ + public uint targetLength; + + /**< see ZSTD_strategy definition above */ + public ZSTD_strategy strategy; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressionStage_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressionStage_e.cs new file mode 100644 index 00000000..87c924c0 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_compressionStage_e.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_compressionStage_e + { + ZSTDcs_created = 0, + ZSTDcs_init, + ZSTDcs_ongoing, + ZSTDcs_ending, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cpuid_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cpuid_t.cs new file mode 100644 index 00000000..2fbb6a6a --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cpuid_t.cs @@ -0,0 +1,15 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_cpuid_t + { + public uint f1c; + + public uint f1d; + + public uint f7b; + + public uint f7c; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_customMem.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_customMem.cs new file mode 100644 index 00000000..e2c4bf68 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_customMem.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_customMem + { + public void* customAlloc; + + public void* customFree; + + public void* opaque; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp.cs new file mode 100644 index 00000000..78ff9e26 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp.cs @@ -0,0 +1,111 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /** + * Zstd fits all its internal datastructures into a single continuous buffer, + * so that it only needs to perform a single OS allocation (or so that a buffer + * can be provided to it and it can perform no allocations at all). This buffer + * is called the workspace. + * + * Several optimizations complicate that process of allocating memory ranges + * from this workspace for each internal datastructure: + * + * - These different internal datastructures have different setup requirements: + * + * - The static objects need to be cleared once and can then be trivially + * reused for each compression. + * + * - Various buffers don't need to be initialized at all--they are always + * written into before they're read. + * + * - The matchstate tables have a unique requirement that they don't need + * their memory to be totally cleared, but they do need the memory to have + * some bound, i.e., a guarantee that all values in the memory they've been + * allocated is less than some maximum value (which is the starting value + * for the indices that they will then use for compression). When this + * guarantee is provided to them, they can use the memory without any setup + * work. When it can't, they have to clear the area. + * + * - These buffers also have different alignment requirements. + * + * - We would like to reuse the objects in the workspace for multiple + * compressions without having to perform any expensive reallocation or + * reinitialization work. + * + * - We would like to be able to efficiently reuse the workspace across + * multiple compressions **even when the compression parameters change** and + * we need to resize some of the objects (where possible). + * + * To attempt to manage this buffer, given these constraints, the ZSTD_cwksp + * abstraction was created. It works as follows: + * + * Workspace Layout: + * + * [ ... workspace ... ] + * [objects][tables ... ->] free space [<- ... aligned][<- ... buffers] + * + * The various objects that live in the workspace are divided into the + * following categories, and are allocated separately: + * + * - Static objects: this is optionally the enclosing ZSTD_CCtx or ZSTD_CDict, + * so that literally everything fits in a single buffer. Note: if present, + * this must be the first object in the workspace, since ZSTD_customFree{CCtx, + * CDict}() rely on a pointer comparison to see whether one or two frees are + * required. + * + * - Fixed size objects: these are fixed-size, fixed-count objects that are + * nonetheless "dynamically" allocated in the workspace so that we can + * control how they're initialized separately from the broader ZSTD_CCtx. + * Examples: + * - Entropy Workspace + * - 2 x ZSTD_compressedBlockState_t + * - CDict dictionary contents + * + * - Tables: these are any of several different datastructures (hash tables, + * chain tables, binary trees) that all respect a common format: they are + * uint32_t arrays, all of whose values are between 0 and (nextSrc - base). + * Their sizes depend on the cparams. These tables are 64-byte aligned. + * + * - Aligned: these buffers are used for various purposes that require 4 byte + * alignment, but don't require any initialization before they're used. These + * buffers are each aligned to 64 bytes. + * + * - Buffers: these buffers are used for various purposes that don't require + * any alignment or initialization before they're used. This means they can + * be moved around at no cost for a new compression. + * + * Allocating Memory: + * + * The various types of objects must be allocated in order, so they can be + * correctly packed into the workspace buffer. That order is: + * + * 1. Objects + * 2. Buffers + * 3. Aligned/Tables + * + * Attempts to reserve objects of different types out of order will fail. + */ + public unsafe partial struct ZSTD_cwksp + { + public void* workspace; + + public void* workspaceEnd; + + public void* objectEnd; + + public void* tableEnd; + + public void* tableValidEnd; + + public void* allocStart; + + public byte allocFailed; + + public int workspaceOversizedDuration; + + public ZSTD_cwksp_alloc_phase_e phase; + + public ZSTD_cwksp_static_alloc_e isStatic; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp_alloc_phase_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp_alloc_phase_e.cs new file mode 100644 index 00000000..134c50f6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp_alloc_phase_e.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_cwksp_alloc_phase_e + { + ZSTD_cwksp_alloc_objects, + ZSTD_cwksp_alloc_buffers, + ZSTD_cwksp_alloc_aligned, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp_static_alloc_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp_static_alloc_e.cs new file mode 100644 index 00000000..03d529e5 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_cwksp_static_alloc_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_cwksp_static_alloc_e + { + ZSTD_cwksp_dynamic_alloc, + ZSTD_cwksp_static_alloc, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dParameter.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dParameter.cs new file mode 100644 index 00000000..db84afe2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dParameter.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dParameter + { + ZSTD_d_windowLogMax = 100, + ZSTD_d_experimentalParam1 = 1000, + ZSTD_d_experimentalParam2 = 1001, + ZSTD_d_experimentalParam3 = 1002, + ZSTD_d_experimentalParam4 = 1003, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dStage.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dStage.cs new file mode 100644 index 00000000..bd04c57e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dStage.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dStage + { + ZSTDds_getFrameHeaderSize, + ZSTDds_decodeFrameHeader, + ZSTDds_decodeBlockHeader, + ZSTDds_decompressBlock, + ZSTDds_decompressLastBlock, + ZSTDds_checkChecksum, + ZSTDds_decodeSkippableHeader, + ZSTDds_skipFrame, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dStreamStage.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dStreamStage.cs new file mode 100644 index 00000000..dd472c22 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dStreamStage.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dStreamStage + { + zdss_init = 0, + zdss_loadHeader, + zdss_read, + zdss_load, + zdss_flush, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_decompressSequences_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_decompressSequences_t.cs new file mode 100644 index 00000000..653a298b --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_decompressSequences_t.cs @@ -0,0 +1,8 @@ +using System; +using System.Runtime.InteropServices; + +namespace ZstdSharp.Unsafe +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public unsafe delegate nuint ZSTD_decompressSequences_t(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame); +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_defaultPolicy_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_defaultPolicy_e.cs new file mode 100644 index 00000000..9e97db47 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_defaultPolicy_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_defaultPolicy_e + { + ZSTD_defaultDisallowed = 0, + ZSTD_defaultAllowed = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictAttachPref_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictAttachPref_e.cs new file mode 100644 index 00000000..c37ee4e2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictAttachPref_e.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dictAttachPref_e + { + ZSTD_dictDefaultAttach = 0, + ZSTD_dictForceAttach = 1, + ZSTD_dictForceCopy = 2, + ZSTD_dictForceLoad = 3, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictContentType_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictContentType_e.cs new file mode 100644 index 00000000..ce34db27 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictContentType_e.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dictContentType_e + { + ZSTD_dct_auto = 0, + ZSTD_dct_rawContent = 1, + ZSTD_dct_fullDict = 2, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictLoadMethod_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictLoadMethod_e.cs new file mode 100644 index 00000000..1d8c6bb7 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictLoadMethod_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dictLoadMethod_e + { + ZSTD_dlm_byCopy = 0, + ZSTD_dlm_byRef = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictMode_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictMode_e.cs new file mode 100644 index 00000000..b1a863b3 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictMode_e.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dictMode_e + { + ZSTD_noDict = 0, + ZSTD_extDict = 1, + ZSTD_dictMatchState = 2, + ZSTD_dedicatedDictSearch = 3, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictTableLoadMethod_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictTableLoadMethod_e.cs new file mode 100644 index 00000000..ea60b42e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictTableLoadMethod_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dictTableLoadMethod_e + { + ZSTD_dtlm_fast, + ZSTD_dtlm_full, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictUses_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictUses_e.cs new file mode 100644 index 00000000..32daa3de --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_dictUses_e.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_dictUses_e + { + ZSTD_use_indefinitely = -1, + ZSTD_dont_use = 0, + ZSTD_use_once = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyCTablesMetadata_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyCTablesMetadata_t.cs new file mode 100644 index 00000000..d75f40a8 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyCTablesMetadata_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_entropyCTablesMetadata_t + { + public ZSTD_hufCTablesMetadata_t hufMetadata; + + public ZSTD_fseCTablesMetadata_t fseMetadata; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyCTables_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyCTables_t.cs new file mode 100644 index 00000000..259135c6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyCTables_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_entropyCTables_t + { + public ZSTD_hufCTables_t huf; + + public ZSTD_fseCTables_t fse; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyDTables_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyDTables_t.cs new file mode 100644 index 00000000..2a3e66b5 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_entropyDTables_t.cs @@ -0,0 +1,1457 @@ +using InlineIL; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static InlineIL.IL.Emit; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_entropyDTables_t + { + /* Note : Space reserved for FSE Tables */ + public _LLTable_e__FixedBuffer LLTable; + + /* is also used as temporary workspace while building hufTable during DDict creation */ + public _OFTable_e__FixedBuffer OFTable; + + /* and therefore must be at least HUF_DECOMPRESS_WORKSPACE_SIZE large */ + public _MLTable_e__FixedBuffer MLTable; + + /* can accommodate HUF_decompress4X */ + public fixed uint hufTable[4097]; + + public fixed uint rep[3]; + + public fixed uint workspace[157]; + + public unsafe partial struct _LLTable_e__FixedBuffer + { + public ZSTD_seqSymbol e0; + public ZSTD_seqSymbol e1; + public ZSTD_seqSymbol e2; + public ZSTD_seqSymbol e3; + public ZSTD_seqSymbol e4; + public ZSTD_seqSymbol e5; + public ZSTD_seqSymbol e6; + public ZSTD_seqSymbol e7; + public ZSTD_seqSymbol e8; + public ZSTD_seqSymbol e9; + public ZSTD_seqSymbol e10; + public ZSTD_seqSymbol e11; + public ZSTD_seqSymbol e12; + public ZSTD_seqSymbol e13; + public ZSTD_seqSymbol e14; + public ZSTD_seqSymbol e15; + public ZSTD_seqSymbol e16; + public ZSTD_seqSymbol e17; + public ZSTD_seqSymbol e18; + public ZSTD_seqSymbol e19; + public ZSTD_seqSymbol e20; + public ZSTD_seqSymbol e21; + public ZSTD_seqSymbol e22; + public ZSTD_seqSymbol e23; + public ZSTD_seqSymbol e24; + public ZSTD_seqSymbol e25; + public ZSTD_seqSymbol e26; + public ZSTD_seqSymbol e27; + public ZSTD_seqSymbol e28; + public ZSTD_seqSymbol e29; + public ZSTD_seqSymbol e30; + public ZSTD_seqSymbol e31; + public ZSTD_seqSymbol e32; + public ZSTD_seqSymbol e33; + public ZSTD_seqSymbol e34; + public ZSTD_seqSymbol e35; + public ZSTD_seqSymbol e36; + public ZSTD_seqSymbol e37; + public ZSTD_seqSymbol e38; + public ZSTD_seqSymbol e39; + public ZSTD_seqSymbol e40; + public ZSTD_seqSymbol e41; + public ZSTD_seqSymbol e42; + public ZSTD_seqSymbol e43; + public ZSTD_seqSymbol e44; + public ZSTD_seqSymbol e45; + public ZSTD_seqSymbol e46; + public ZSTD_seqSymbol e47; + public ZSTD_seqSymbol e48; + public ZSTD_seqSymbol e49; + public ZSTD_seqSymbol e50; + public ZSTD_seqSymbol e51; + public ZSTD_seqSymbol e52; + public ZSTD_seqSymbol e53; + public ZSTD_seqSymbol e54; + public ZSTD_seqSymbol e55; + public ZSTD_seqSymbol e56; + public ZSTD_seqSymbol e57; + public ZSTD_seqSymbol e58; + public ZSTD_seqSymbol e59; + public ZSTD_seqSymbol e60; + public ZSTD_seqSymbol e61; + public ZSTD_seqSymbol e62; + public ZSTD_seqSymbol e63; + public ZSTD_seqSymbol e64; + public ZSTD_seqSymbol e65; + public ZSTD_seqSymbol e66; + public ZSTD_seqSymbol e67; + public ZSTD_seqSymbol e68; + public ZSTD_seqSymbol e69; + public ZSTD_seqSymbol e70; + public ZSTD_seqSymbol e71; + public ZSTD_seqSymbol e72; + public ZSTD_seqSymbol e73; + public ZSTD_seqSymbol e74; + public ZSTD_seqSymbol e75; + public ZSTD_seqSymbol e76; + public ZSTD_seqSymbol e77; + public ZSTD_seqSymbol e78; + public ZSTD_seqSymbol e79; + public ZSTD_seqSymbol e80; + public ZSTD_seqSymbol e81; + public ZSTD_seqSymbol e82; + public ZSTD_seqSymbol e83; + public ZSTD_seqSymbol e84; + public ZSTD_seqSymbol e85; + public ZSTD_seqSymbol e86; + public ZSTD_seqSymbol e87; + public ZSTD_seqSymbol e88; + public ZSTD_seqSymbol e89; + public ZSTD_seqSymbol e90; + public ZSTD_seqSymbol e91; + public ZSTD_seqSymbol e92; + public ZSTD_seqSymbol e93; + public ZSTD_seqSymbol e94; + public ZSTD_seqSymbol e95; + public ZSTD_seqSymbol e96; + public ZSTD_seqSymbol e97; + public ZSTD_seqSymbol e98; + public ZSTD_seqSymbol e99; + public ZSTD_seqSymbol e100; + public ZSTD_seqSymbol e101; + public ZSTD_seqSymbol e102; + public ZSTD_seqSymbol e103; + public ZSTD_seqSymbol e104; + public ZSTD_seqSymbol e105; + public ZSTD_seqSymbol e106; + public ZSTD_seqSymbol e107; + public ZSTD_seqSymbol e108; + public ZSTD_seqSymbol e109; + public ZSTD_seqSymbol e110; + public ZSTD_seqSymbol e111; + public ZSTD_seqSymbol e112; + public ZSTD_seqSymbol e113; + public ZSTD_seqSymbol e114; + public ZSTD_seqSymbol e115; + public ZSTD_seqSymbol e116; + public ZSTD_seqSymbol e117; + public ZSTD_seqSymbol e118; + public ZSTD_seqSymbol e119; + public ZSTD_seqSymbol e120; + public ZSTD_seqSymbol e121; + public ZSTD_seqSymbol e122; + public ZSTD_seqSymbol e123; + public ZSTD_seqSymbol e124; + public ZSTD_seqSymbol e125; + public ZSTD_seqSymbol e126; + public ZSTD_seqSymbol e127; + public ZSTD_seqSymbol e128; + public ZSTD_seqSymbol e129; + public ZSTD_seqSymbol e130; + public ZSTD_seqSymbol e131; + public ZSTD_seqSymbol e132; + public ZSTD_seqSymbol e133; + public ZSTD_seqSymbol e134; + public ZSTD_seqSymbol e135; + public ZSTD_seqSymbol e136; + public ZSTD_seqSymbol e137; + public ZSTD_seqSymbol e138; + public ZSTD_seqSymbol e139; + public ZSTD_seqSymbol e140; + public ZSTD_seqSymbol e141; + public ZSTD_seqSymbol e142; + public ZSTD_seqSymbol e143; + public ZSTD_seqSymbol e144; + public ZSTD_seqSymbol e145; + public ZSTD_seqSymbol e146; + public ZSTD_seqSymbol e147; + public ZSTD_seqSymbol e148; + public ZSTD_seqSymbol e149; + public ZSTD_seqSymbol e150; + public ZSTD_seqSymbol e151; + public ZSTD_seqSymbol e152; + public ZSTD_seqSymbol e153; + public ZSTD_seqSymbol e154; + public ZSTD_seqSymbol e155; + public ZSTD_seqSymbol e156; + public ZSTD_seqSymbol e157; + public ZSTD_seqSymbol e158; + public ZSTD_seqSymbol e159; + public ZSTD_seqSymbol e160; + public ZSTD_seqSymbol e161; + public ZSTD_seqSymbol e162; + public ZSTD_seqSymbol e163; + public ZSTD_seqSymbol e164; + public ZSTD_seqSymbol e165; + public ZSTD_seqSymbol e166; + public ZSTD_seqSymbol e167; + public ZSTD_seqSymbol e168; + public ZSTD_seqSymbol e169; + public ZSTD_seqSymbol e170; + public ZSTD_seqSymbol e171; + public ZSTD_seqSymbol e172; + public ZSTD_seqSymbol e173; + public ZSTD_seqSymbol e174; + public ZSTD_seqSymbol e175; + public ZSTD_seqSymbol e176; + public ZSTD_seqSymbol e177; + public ZSTD_seqSymbol e178; + public ZSTD_seqSymbol e179; + public ZSTD_seqSymbol e180; + public ZSTD_seqSymbol e181; + public ZSTD_seqSymbol e182; + public ZSTD_seqSymbol e183; + public ZSTD_seqSymbol e184; + public ZSTD_seqSymbol e185; + public ZSTD_seqSymbol e186; + public ZSTD_seqSymbol e187; + public ZSTD_seqSymbol e188; + public ZSTD_seqSymbol e189; + public ZSTD_seqSymbol e190; + public ZSTD_seqSymbol e191; + public ZSTD_seqSymbol e192; + public ZSTD_seqSymbol e193; + public ZSTD_seqSymbol e194; + public ZSTD_seqSymbol e195; + public ZSTD_seqSymbol e196; + public ZSTD_seqSymbol e197; + public ZSTD_seqSymbol e198; + public ZSTD_seqSymbol e199; + public ZSTD_seqSymbol e200; + public ZSTD_seqSymbol e201; + public ZSTD_seqSymbol e202; + public ZSTD_seqSymbol e203; + public ZSTD_seqSymbol e204; + public ZSTD_seqSymbol e205; + public ZSTD_seqSymbol e206; + public ZSTD_seqSymbol e207; + public ZSTD_seqSymbol e208; + public ZSTD_seqSymbol e209; + public ZSTD_seqSymbol e210; + public ZSTD_seqSymbol e211; + public ZSTD_seqSymbol e212; + public ZSTD_seqSymbol e213; + public ZSTD_seqSymbol e214; + public ZSTD_seqSymbol e215; + public ZSTD_seqSymbol e216; + public ZSTD_seqSymbol e217; + public ZSTD_seqSymbol e218; + public ZSTD_seqSymbol e219; + public ZSTD_seqSymbol e220; + public ZSTD_seqSymbol e221; + public ZSTD_seqSymbol e222; + public ZSTD_seqSymbol e223; + public ZSTD_seqSymbol e224; + public ZSTD_seqSymbol e225; + public ZSTD_seqSymbol e226; + public ZSTD_seqSymbol e227; + public ZSTD_seqSymbol e228; + public ZSTD_seqSymbol e229; + public ZSTD_seqSymbol e230; + public ZSTD_seqSymbol e231; + public ZSTD_seqSymbol e232; + public ZSTD_seqSymbol e233; + public ZSTD_seqSymbol e234; + public ZSTD_seqSymbol e235; + public ZSTD_seqSymbol e236; + public ZSTD_seqSymbol e237; + public ZSTD_seqSymbol e238; + public ZSTD_seqSymbol e239; + public ZSTD_seqSymbol e240; + public ZSTD_seqSymbol e241; + public ZSTD_seqSymbol e242; + public ZSTD_seqSymbol e243; + public ZSTD_seqSymbol e244; + public ZSTD_seqSymbol e245; + public ZSTD_seqSymbol e246; + public ZSTD_seqSymbol e247; + public ZSTD_seqSymbol e248; + public ZSTD_seqSymbol e249; + public ZSTD_seqSymbol e250; + public ZSTD_seqSymbol e251; + public ZSTD_seqSymbol e252; + public ZSTD_seqSymbol e253; + public ZSTD_seqSymbol e254; + public ZSTD_seqSymbol e255; + public ZSTD_seqSymbol e256; + public ZSTD_seqSymbol e257; + public ZSTD_seqSymbol e258; + public ZSTD_seqSymbol e259; + public ZSTD_seqSymbol e260; + public ZSTD_seqSymbol e261; + public ZSTD_seqSymbol e262; + public ZSTD_seqSymbol e263; + public ZSTD_seqSymbol e264; + public ZSTD_seqSymbol e265; + public ZSTD_seqSymbol e266; + public ZSTD_seqSymbol e267; + public ZSTD_seqSymbol e268; + public ZSTD_seqSymbol e269; + public ZSTD_seqSymbol e270; + public ZSTD_seqSymbol e271; + public ZSTD_seqSymbol e272; + public ZSTD_seqSymbol e273; + public ZSTD_seqSymbol e274; + public ZSTD_seqSymbol e275; + public ZSTD_seqSymbol e276; + public ZSTD_seqSymbol e277; + public ZSTD_seqSymbol e278; + public ZSTD_seqSymbol e279; + public ZSTD_seqSymbol e280; + public ZSTD_seqSymbol e281; + public ZSTD_seqSymbol e282; + public ZSTD_seqSymbol e283; + public ZSTD_seqSymbol e284; + public ZSTD_seqSymbol e285; + public ZSTD_seqSymbol e286; + public ZSTD_seqSymbol e287; + public ZSTD_seqSymbol e288; + public ZSTD_seqSymbol e289; + public ZSTD_seqSymbol e290; + public ZSTD_seqSymbol e291; + public ZSTD_seqSymbol e292; + public ZSTD_seqSymbol e293; + public ZSTD_seqSymbol e294; + public ZSTD_seqSymbol e295; + public ZSTD_seqSymbol e296; + public ZSTD_seqSymbol e297; + public ZSTD_seqSymbol e298; + public ZSTD_seqSymbol e299; + public ZSTD_seqSymbol e300; + public ZSTD_seqSymbol e301; + public ZSTD_seqSymbol e302; + public ZSTD_seqSymbol e303; + public ZSTD_seqSymbol e304; + public ZSTD_seqSymbol e305; + public ZSTD_seqSymbol e306; + public ZSTD_seqSymbol e307; + public ZSTD_seqSymbol e308; + public ZSTD_seqSymbol e309; + public ZSTD_seqSymbol e310; + public ZSTD_seqSymbol e311; + public ZSTD_seqSymbol e312; + public ZSTD_seqSymbol e313; + public ZSTD_seqSymbol e314; + public ZSTD_seqSymbol e315; + public ZSTD_seqSymbol e316; + public ZSTD_seqSymbol e317; + public ZSTD_seqSymbol e318; + public ZSTD_seqSymbol e319; + public ZSTD_seqSymbol e320; + public ZSTD_seqSymbol e321; + public ZSTD_seqSymbol e322; + public ZSTD_seqSymbol e323; + public ZSTD_seqSymbol e324; + public ZSTD_seqSymbol e325; + public ZSTD_seqSymbol e326; + public ZSTD_seqSymbol e327; + public ZSTD_seqSymbol e328; + public ZSTD_seqSymbol e329; + public ZSTD_seqSymbol e330; + public ZSTD_seqSymbol e331; + public ZSTD_seqSymbol e332; + public ZSTD_seqSymbol e333; + public ZSTD_seqSymbol e334; + public ZSTD_seqSymbol e335; + public ZSTD_seqSymbol e336; + public ZSTD_seqSymbol e337; + public ZSTD_seqSymbol e338; + public ZSTD_seqSymbol e339; + public ZSTD_seqSymbol e340; + public ZSTD_seqSymbol e341; + public ZSTD_seqSymbol e342; + public ZSTD_seqSymbol e343; + public ZSTD_seqSymbol e344; + public ZSTD_seqSymbol e345; + public ZSTD_seqSymbol e346; + public ZSTD_seqSymbol e347; + public ZSTD_seqSymbol e348; + public ZSTD_seqSymbol e349; + public ZSTD_seqSymbol e350; + public ZSTD_seqSymbol e351; + public ZSTD_seqSymbol e352; + public ZSTD_seqSymbol e353; + public ZSTD_seqSymbol e354; + public ZSTD_seqSymbol e355; + public ZSTD_seqSymbol e356; + public ZSTD_seqSymbol e357; + public ZSTD_seqSymbol e358; + public ZSTD_seqSymbol e359; + public ZSTD_seqSymbol e360; + public ZSTD_seqSymbol e361; + public ZSTD_seqSymbol e362; + public ZSTD_seqSymbol e363; + public ZSTD_seqSymbol e364; + public ZSTD_seqSymbol e365; + public ZSTD_seqSymbol e366; + public ZSTD_seqSymbol e367; + public ZSTD_seqSymbol e368; + public ZSTD_seqSymbol e369; + public ZSTD_seqSymbol e370; + public ZSTD_seqSymbol e371; + public ZSTD_seqSymbol e372; + public ZSTD_seqSymbol e373; + public ZSTD_seqSymbol e374; + public ZSTD_seqSymbol e375; + public ZSTD_seqSymbol e376; + public ZSTD_seqSymbol e377; + public ZSTD_seqSymbol e378; + public ZSTD_seqSymbol e379; + public ZSTD_seqSymbol e380; + public ZSTD_seqSymbol e381; + public ZSTD_seqSymbol e382; + public ZSTD_seqSymbol e383; + public ZSTD_seqSymbol e384; + public ZSTD_seqSymbol e385; + public ZSTD_seqSymbol e386; + public ZSTD_seqSymbol e387; + public ZSTD_seqSymbol e388; + public ZSTD_seqSymbol e389; + public ZSTD_seqSymbol e390; + public ZSTD_seqSymbol e391; + public ZSTD_seqSymbol e392; + public ZSTD_seqSymbol e393; + public ZSTD_seqSymbol e394; + public ZSTD_seqSymbol e395; + public ZSTD_seqSymbol e396; + public ZSTD_seqSymbol e397; + public ZSTD_seqSymbol e398; + public ZSTD_seqSymbol e399; + public ZSTD_seqSymbol e400; + public ZSTD_seqSymbol e401; + public ZSTD_seqSymbol e402; + public ZSTD_seqSymbol e403; + public ZSTD_seqSymbol e404; + public ZSTD_seqSymbol e405; + public ZSTD_seqSymbol e406; + public ZSTD_seqSymbol e407; + public ZSTD_seqSymbol e408; + public ZSTD_seqSymbol e409; + public ZSTD_seqSymbol e410; + public ZSTD_seqSymbol e411; + public ZSTD_seqSymbol e412; + public ZSTD_seqSymbol e413; + public ZSTD_seqSymbol e414; + public ZSTD_seqSymbol e415; + public ZSTD_seqSymbol e416; + public ZSTD_seqSymbol e417; + public ZSTD_seqSymbol e418; + public ZSTD_seqSymbol e419; + public ZSTD_seqSymbol e420; + public ZSTD_seqSymbol e421; + public ZSTD_seqSymbol e422; + public ZSTD_seqSymbol e423; + public ZSTD_seqSymbol e424; + public ZSTD_seqSymbol e425; + public ZSTD_seqSymbol e426; + public ZSTD_seqSymbol e427; + public ZSTD_seqSymbol e428; + public ZSTD_seqSymbol e429; + public ZSTD_seqSymbol e430; + public ZSTD_seqSymbol e431; + public ZSTD_seqSymbol e432; + public ZSTD_seqSymbol e433; + public ZSTD_seqSymbol e434; + public ZSTD_seqSymbol e435; + public ZSTD_seqSymbol e436; + public ZSTD_seqSymbol e437; + public ZSTD_seqSymbol e438; + public ZSTD_seqSymbol e439; + public ZSTD_seqSymbol e440; + public ZSTD_seqSymbol e441; + public ZSTD_seqSymbol e442; + public ZSTD_seqSymbol e443; + public ZSTD_seqSymbol e444; + public ZSTD_seqSymbol e445; + public ZSTD_seqSymbol e446; + public ZSTD_seqSymbol e447; + public ZSTD_seqSymbol e448; + public ZSTD_seqSymbol e449; + public ZSTD_seqSymbol e450; + public ZSTD_seqSymbol e451; + public ZSTD_seqSymbol e452; + public ZSTD_seqSymbol e453; + public ZSTD_seqSymbol e454; + public ZSTD_seqSymbol e455; + public ZSTD_seqSymbol e456; + public ZSTD_seqSymbol e457; + public ZSTD_seqSymbol e458; + public ZSTD_seqSymbol e459; + public ZSTD_seqSymbol e460; + public ZSTD_seqSymbol e461; + public ZSTD_seqSymbol e462; + public ZSTD_seqSymbol e463; + public ZSTD_seqSymbol e464; + public ZSTD_seqSymbol e465; + public ZSTD_seqSymbol e466; + public ZSTD_seqSymbol e467; + public ZSTD_seqSymbol e468; + public ZSTD_seqSymbol e469; + public ZSTD_seqSymbol e470; + public ZSTD_seqSymbol e471; + public ZSTD_seqSymbol e472; + public ZSTD_seqSymbol e473; + public ZSTD_seqSymbol e474; + public ZSTD_seqSymbol e475; + public ZSTD_seqSymbol e476; + public ZSTD_seqSymbol e477; + public ZSTD_seqSymbol e478; + public ZSTD_seqSymbol e479; + public ZSTD_seqSymbol e480; + public ZSTD_seqSymbol e481; + public ZSTD_seqSymbol e482; + public ZSTD_seqSymbol e483; + public ZSTD_seqSymbol e484; + public ZSTD_seqSymbol e485; + public ZSTD_seqSymbol e486; + public ZSTD_seqSymbol e487; + public ZSTD_seqSymbol e488; + public ZSTD_seqSymbol e489; + public ZSTD_seqSymbol e490; + public ZSTD_seqSymbol e491; + public ZSTD_seqSymbol e492; + public ZSTD_seqSymbol e493; + public ZSTD_seqSymbol e494; + public ZSTD_seqSymbol e495; + public ZSTD_seqSymbol e496; + public ZSTD_seqSymbol e497; + public ZSTD_seqSymbol e498; + public ZSTD_seqSymbol e499; + public ZSTD_seqSymbol e500; + public ZSTD_seqSymbol e501; + public ZSTD_seqSymbol e502; + public ZSTD_seqSymbol e503; + public ZSTD_seqSymbol e504; + public ZSTD_seqSymbol e505; + public ZSTD_seqSymbol e506; + public ZSTD_seqSymbol e507; + public ZSTD_seqSymbol e508; + public ZSTD_seqSymbol e509; + public ZSTD_seqSymbol e510; + public ZSTD_seqSymbol e511; + public ZSTD_seqSymbol e512; + + public ref ZSTD_seqSymbol this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref ZSTD_seqSymbol this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref ZSTD_seqSymbol this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator ZSTD_seqSymbol*(in _LLTable_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_LLTable_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static ZSTD_seqSymbol* operator +(in _LLTable_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_LLTable_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + + public unsafe partial struct _OFTable_e__FixedBuffer + { + public ZSTD_seqSymbol e0; + public ZSTD_seqSymbol e1; + public ZSTD_seqSymbol e2; + public ZSTD_seqSymbol e3; + public ZSTD_seqSymbol e4; + public ZSTD_seqSymbol e5; + public ZSTD_seqSymbol e6; + public ZSTD_seqSymbol e7; + public ZSTD_seqSymbol e8; + public ZSTD_seqSymbol e9; + public ZSTD_seqSymbol e10; + public ZSTD_seqSymbol e11; + public ZSTD_seqSymbol e12; + public ZSTD_seqSymbol e13; + public ZSTD_seqSymbol e14; + public ZSTD_seqSymbol e15; + public ZSTD_seqSymbol e16; + public ZSTD_seqSymbol e17; + public ZSTD_seqSymbol e18; + public ZSTD_seqSymbol e19; + public ZSTD_seqSymbol e20; + public ZSTD_seqSymbol e21; + public ZSTD_seqSymbol e22; + public ZSTD_seqSymbol e23; + public ZSTD_seqSymbol e24; + public ZSTD_seqSymbol e25; + public ZSTD_seqSymbol e26; + public ZSTD_seqSymbol e27; + public ZSTD_seqSymbol e28; + public ZSTD_seqSymbol e29; + public ZSTD_seqSymbol e30; + public ZSTD_seqSymbol e31; + public ZSTD_seqSymbol e32; + public ZSTD_seqSymbol e33; + public ZSTD_seqSymbol e34; + public ZSTD_seqSymbol e35; + public ZSTD_seqSymbol e36; + public ZSTD_seqSymbol e37; + public ZSTD_seqSymbol e38; + public ZSTD_seqSymbol e39; + public ZSTD_seqSymbol e40; + public ZSTD_seqSymbol e41; + public ZSTD_seqSymbol e42; + public ZSTD_seqSymbol e43; + public ZSTD_seqSymbol e44; + public ZSTD_seqSymbol e45; + public ZSTD_seqSymbol e46; + public ZSTD_seqSymbol e47; + public ZSTD_seqSymbol e48; + public ZSTD_seqSymbol e49; + public ZSTD_seqSymbol e50; + public ZSTD_seqSymbol e51; + public ZSTD_seqSymbol e52; + public ZSTD_seqSymbol e53; + public ZSTD_seqSymbol e54; + public ZSTD_seqSymbol e55; + public ZSTD_seqSymbol e56; + public ZSTD_seqSymbol e57; + public ZSTD_seqSymbol e58; + public ZSTD_seqSymbol e59; + public ZSTD_seqSymbol e60; + public ZSTD_seqSymbol e61; + public ZSTD_seqSymbol e62; + public ZSTD_seqSymbol e63; + public ZSTD_seqSymbol e64; + public ZSTD_seqSymbol e65; + public ZSTD_seqSymbol e66; + public ZSTD_seqSymbol e67; + public ZSTD_seqSymbol e68; + public ZSTD_seqSymbol e69; + public ZSTD_seqSymbol e70; + public ZSTD_seqSymbol e71; + public ZSTD_seqSymbol e72; + public ZSTD_seqSymbol e73; + public ZSTD_seqSymbol e74; + public ZSTD_seqSymbol e75; + public ZSTD_seqSymbol e76; + public ZSTD_seqSymbol e77; + public ZSTD_seqSymbol e78; + public ZSTD_seqSymbol e79; + public ZSTD_seqSymbol e80; + public ZSTD_seqSymbol e81; + public ZSTD_seqSymbol e82; + public ZSTD_seqSymbol e83; + public ZSTD_seqSymbol e84; + public ZSTD_seqSymbol e85; + public ZSTD_seqSymbol e86; + public ZSTD_seqSymbol e87; + public ZSTD_seqSymbol e88; + public ZSTD_seqSymbol e89; + public ZSTD_seqSymbol e90; + public ZSTD_seqSymbol e91; + public ZSTD_seqSymbol e92; + public ZSTD_seqSymbol e93; + public ZSTD_seqSymbol e94; + public ZSTD_seqSymbol e95; + public ZSTD_seqSymbol e96; + public ZSTD_seqSymbol e97; + public ZSTD_seqSymbol e98; + public ZSTD_seqSymbol e99; + public ZSTD_seqSymbol e100; + public ZSTD_seqSymbol e101; + public ZSTD_seqSymbol e102; + public ZSTD_seqSymbol e103; + public ZSTD_seqSymbol e104; + public ZSTD_seqSymbol e105; + public ZSTD_seqSymbol e106; + public ZSTD_seqSymbol e107; + public ZSTD_seqSymbol e108; + public ZSTD_seqSymbol e109; + public ZSTD_seqSymbol e110; + public ZSTD_seqSymbol e111; + public ZSTD_seqSymbol e112; + public ZSTD_seqSymbol e113; + public ZSTD_seqSymbol e114; + public ZSTD_seqSymbol e115; + public ZSTD_seqSymbol e116; + public ZSTD_seqSymbol e117; + public ZSTD_seqSymbol e118; + public ZSTD_seqSymbol e119; + public ZSTD_seqSymbol e120; + public ZSTD_seqSymbol e121; + public ZSTD_seqSymbol e122; + public ZSTD_seqSymbol e123; + public ZSTD_seqSymbol e124; + public ZSTD_seqSymbol e125; + public ZSTD_seqSymbol e126; + public ZSTD_seqSymbol e127; + public ZSTD_seqSymbol e128; + public ZSTD_seqSymbol e129; + public ZSTD_seqSymbol e130; + public ZSTD_seqSymbol e131; + public ZSTD_seqSymbol e132; + public ZSTD_seqSymbol e133; + public ZSTD_seqSymbol e134; + public ZSTD_seqSymbol e135; + public ZSTD_seqSymbol e136; + public ZSTD_seqSymbol e137; + public ZSTD_seqSymbol e138; + public ZSTD_seqSymbol e139; + public ZSTD_seqSymbol e140; + public ZSTD_seqSymbol e141; + public ZSTD_seqSymbol e142; + public ZSTD_seqSymbol e143; + public ZSTD_seqSymbol e144; + public ZSTD_seqSymbol e145; + public ZSTD_seqSymbol e146; + public ZSTD_seqSymbol e147; + public ZSTD_seqSymbol e148; + public ZSTD_seqSymbol e149; + public ZSTD_seqSymbol e150; + public ZSTD_seqSymbol e151; + public ZSTD_seqSymbol e152; + public ZSTD_seqSymbol e153; + public ZSTD_seqSymbol e154; + public ZSTD_seqSymbol e155; + public ZSTD_seqSymbol e156; + public ZSTD_seqSymbol e157; + public ZSTD_seqSymbol e158; + public ZSTD_seqSymbol e159; + public ZSTD_seqSymbol e160; + public ZSTD_seqSymbol e161; + public ZSTD_seqSymbol e162; + public ZSTD_seqSymbol e163; + public ZSTD_seqSymbol e164; + public ZSTD_seqSymbol e165; + public ZSTD_seqSymbol e166; + public ZSTD_seqSymbol e167; + public ZSTD_seqSymbol e168; + public ZSTD_seqSymbol e169; + public ZSTD_seqSymbol e170; + public ZSTD_seqSymbol e171; + public ZSTD_seqSymbol e172; + public ZSTD_seqSymbol e173; + public ZSTD_seqSymbol e174; + public ZSTD_seqSymbol e175; + public ZSTD_seqSymbol e176; + public ZSTD_seqSymbol e177; + public ZSTD_seqSymbol e178; + public ZSTD_seqSymbol e179; + public ZSTD_seqSymbol e180; + public ZSTD_seqSymbol e181; + public ZSTD_seqSymbol e182; + public ZSTD_seqSymbol e183; + public ZSTD_seqSymbol e184; + public ZSTD_seqSymbol e185; + public ZSTD_seqSymbol e186; + public ZSTD_seqSymbol e187; + public ZSTD_seqSymbol e188; + public ZSTD_seqSymbol e189; + public ZSTD_seqSymbol e190; + public ZSTD_seqSymbol e191; + public ZSTD_seqSymbol e192; + public ZSTD_seqSymbol e193; + public ZSTD_seqSymbol e194; + public ZSTD_seqSymbol e195; + public ZSTD_seqSymbol e196; + public ZSTD_seqSymbol e197; + public ZSTD_seqSymbol e198; + public ZSTD_seqSymbol e199; + public ZSTD_seqSymbol e200; + public ZSTD_seqSymbol e201; + public ZSTD_seqSymbol e202; + public ZSTD_seqSymbol e203; + public ZSTD_seqSymbol e204; + public ZSTD_seqSymbol e205; + public ZSTD_seqSymbol e206; + public ZSTD_seqSymbol e207; + public ZSTD_seqSymbol e208; + public ZSTD_seqSymbol e209; + public ZSTD_seqSymbol e210; + public ZSTD_seqSymbol e211; + public ZSTD_seqSymbol e212; + public ZSTD_seqSymbol e213; + public ZSTD_seqSymbol e214; + public ZSTD_seqSymbol e215; + public ZSTD_seqSymbol e216; + public ZSTD_seqSymbol e217; + public ZSTD_seqSymbol e218; + public ZSTD_seqSymbol e219; + public ZSTD_seqSymbol e220; + public ZSTD_seqSymbol e221; + public ZSTD_seqSymbol e222; + public ZSTD_seqSymbol e223; + public ZSTD_seqSymbol e224; + public ZSTD_seqSymbol e225; + public ZSTD_seqSymbol e226; + public ZSTD_seqSymbol e227; + public ZSTD_seqSymbol e228; + public ZSTD_seqSymbol e229; + public ZSTD_seqSymbol e230; + public ZSTD_seqSymbol e231; + public ZSTD_seqSymbol e232; + public ZSTD_seqSymbol e233; + public ZSTD_seqSymbol e234; + public ZSTD_seqSymbol e235; + public ZSTD_seqSymbol e236; + public ZSTD_seqSymbol e237; + public ZSTD_seqSymbol e238; + public ZSTD_seqSymbol e239; + public ZSTD_seqSymbol e240; + public ZSTD_seqSymbol e241; + public ZSTD_seqSymbol e242; + public ZSTD_seqSymbol e243; + public ZSTD_seqSymbol e244; + public ZSTD_seqSymbol e245; + public ZSTD_seqSymbol e246; + public ZSTD_seqSymbol e247; + public ZSTD_seqSymbol e248; + public ZSTD_seqSymbol e249; + public ZSTD_seqSymbol e250; + public ZSTD_seqSymbol e251; + public ZSTD_seqSymbol e252; + public ZSTD_seqSymbol e253; + public ZSTD_seqSymbol e254; + public ZSTD_seqSymbol e255; + public ZSTD_seqSymbol e256; + + public ref ZSTD_seqSymbol this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref ZSTD_seqSymbol this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref ZSTD_seqSymbol this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator ZSTD_seqSymbol*(in _OFTable_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_OFTable_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static ZSTD_seqSymbol* operator +(in _OFTable_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_OFTable_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + + public unsafe partial struct _MLTable_e__FixedBuffer + { + public ZSTD_seqSymbol e0; + public ZSTD_seqSymbol e1; + public ZSTD_seqSymbol e2; + public ZSTD_seqSymbol e3; + public ZSTD_seqSymbol e4; + public ZSTD_seqSymbol e5; + public ZSTD_seqSymbol e6; + public ZSTD_seqSymbol e7; + public ZSTD_seqSymbol e8; + public ZSTD_seqSymbol e9; + public ZSTD_seqSymbol e10; + public ZSTD_seqSymbol e11; + public ZSTD_seqSymbol e12; + public ZSTD_seqSymbol e13; + public ZSTD_seqSymbol e14; + public ZSTD_seqSymbol e15; + public ZSTD_seqSymbol e16; + public ZSTD_seqSymbol e17; + public ZSTD_seqSymbol e18; + public ZSTD_seqSymbol e19; + public ZSTD_seqSymbol e20; + public ZSTD_seqSymbol e21; + public ZSTD_seqSymbol e22; + public ZSTD_seqSymbol e23; + public ZSTD_seqSymbol e24; + public ZSTD_seqSymbol e25; + public ZSTD_seqSymbol e26; + public ZSTD_seqSymbol e27; + public ZSTD_seqSymbol e28; + public ZSTD_seqSymbol e29; + public ZSTD_seqSymbol e30; + public ZSTD_seqSymbol e31; + public ZSTD_seqSymbol e32; + public ZSTD_seqSymbol e33; + public ZSTD_seqSymbol e34; + public ZSTD_seqSymbol e35; + public ZSTD_seqSymbol e36; + public ZSTD_seqSymbol e37; + public ZSTD_seqSymbol e38; + public ZSTD_seqSymbol e39; + public ZSTD_seqSymbol e40; + public ZSTD_seqSymbol e41; + public ZSTD_seqSymbol e42; + public ZSTD_seqSymbol e43; + public ZSTD_seqSymbol e44; + public ZSTD_seqSymbol e45; + public ZSTD_seqSymbol e46; + public ZSTD_seqSymbol e47; + public ZSTD_seqSymbol e48; + public ZSTD_seqSymbol e49; + public ZSTD_seqSymbol e50; + public ZSTD_seqSymbol e51; + public ZSTD_seqSymbol e52; + public ZSTD_seqSymbol e53; + public ZSTD_seqSymbol e54; + public ZSTD_seqSymbol e55; + public ZSTD_seqSymbol e56; + public ZSTD_seqSymbol e57; + public ZSTD_seqSymbol e58; + public ZSTD_seqSymbol e59; + public ZSTD_seqSymbol e60; + public ZSTD_seqSymbol e61; + public ZSTD_seqSymbol e62; + public ZSTD_seqSymbol e63; + public ZSTD_seqSymbol e64; + public ZSTD_seqSymbol e65; + public ZSTD_seqSymbol e66; + public ZSTD_seqSymbol e67; + public ZSTD_seqSymbol e68; + public ZSTD_seqSymbol e69; + public ZSTD_seqSymbol e70; + public ZSTD_seqSymbol e71; + public ZSTD_seqSymbol e72; + public ZSTD_seqSymbol e73; + public ZSTD_seqSymbol e74; + public ZSTD_seqSymbol e75; + public ZSTD_seqSymbol e76; + public ZSTD_seqSymbol e77; + public ZSTD_seqSymbol e78; + public ZSTD_seqSymbol e79; + public ZSTD_seqSymbol e80; + public ZSTD_seqSymbol e81; + public ZSTD_seqSymbol e82; + public ZSTD_seqSymbol e83; + public ZSTD_seqSymbol e84; + public ZSTD_seqSymbol e85; + public ZSTD_seqSymbol e86; + public ZSTD_seqSymbol e87; + public ZSTD_seqSymbol e88; + public ZSTD_seqSymbol e89; + public ZSTD_seqSymbol e90; + public ZSTD_seqSymbol e91; + public ZSTD_seqSymbol e92; + public ZSTD_seqSymbol e93; + public ZSTD_seqSymbol e94; + public ZSTD_seqSymbol e95; + public ZSTD_seqSymbol e96; + public ZSTD_seqSymbol e97; + public ZSTD_seqSymbol e98; + public ZSTD_seqSymbol e99; + public ZSTD_seqSymbol e100; + public ZSTD_seqSymbol e101; + public ZSTD_seqSymbol e102; + public ZSTD_seqSymbol e103; + public ZSTD_seqSymbol e104; + public ZSTD_seqSymbol e105; + public ZSTD_seqSymbol e106; + public ZSTD_seqSymbol e107; + public ZSTD_seqSymbol e108; + public ZSTD_seqSymbol e109; + public ZSTD_seqSymbol e110; + public ZSTD_seqSymbol e111; + public ZSTD_seqSymbol e112; + public ZSTD_seqSymbol e113; + public ZSTD_seqSymbol e114; + public ZSTD_seqSymbol e115; + public ZSTD_seqSymbol e116; + public ZSTD_seqSymbol e117; + public ZSTD_seqSymbol e118; + public ZSTD_seqSymbol e119; + public ZSTD_seqSymbol e120; + public ZSTD_seqSymbol e121; + public ZSTD_seqSymbol e122; + public ZSTD_seqSymbol e123; + public ZSTD_seqSymbol e124; + public ZSTD_seqSymbol e125; + public ZSTD_seqSymbol e126; + public ZSTD_seqSymbol e127; + public ZSTD_seqSymbol e128; + public ZSTD_seqSymbol e129; + public ZSTD_seqSymbol e130; + public ZSTD_seqSymbol e131; + public ZSTD_seqSymbol e132; + public ZSTD_seqSymbol e133; + public ZSTD_seqSymbol e134; + public ZSTD_seqSymbol e135; + public ZSTD_seqSymbol e136; + public ZSTD_seqSymbol e137; + public ZSTD_seqSymbol e138; + public ZSTD_seqSymbol e139; + public ZSTD_seqSymbol e140; + public ZSTD_seqSymbol e141; + public ZSTD_seqSymbol e142; + public ZSTD_seqSymbol e143; + public ZSTD_seqSymbol e144; + public ZSTD_seqSymbol e145; + public ZSTD_seqSymbol e146; + public ZSTD_seqSymbol e147; + public ZSTD_seqSymbol e148; + public ZSTD_seqSymbol e149; + public ZSTD_seqSymbol e150; + public ZSTD_seqSymbol e151; + public ZSTD_seqSymbol e152; + public ZSTD_seqSymbol e153; + public ZSTD_seqSymbol e154; + public ZSTD_seqSymbol e155; + public ZSTD_seqSymbol e156; + public ZSTD_seqSymbol e157; + public ZSTD_seqSymbol e158; + public ZSTD_seqSymbol e159; + public ZSTD_seqSymbol e160; + public ZSTD_seqSymbol e161; + public ZSTD_seqSymbol e162; + public ZSTD_seqSymbol e163; + public ZSTD_seqSymbol e164; + public ZSTD_seqSymbol e165; + public ZSTD_seqSymbol e166; + public ZSTD_seqSymbol e167; + public ZSTD_seqSymbol e168; + public ZSTD_seqSymbol e169; + public ZSTD_seqSymbol e170; + public ZSTD_seqSymbol e171; + public ZSTD_seqSymbol e172; + public ZSTD_seqSymbol e173; + public ZSTD_seqSymbol e174; + public ZSTD_seqSymbol e175; + public ZSTD_seqSymbol e176; + public ZSTD_seqSymbol e177; + public ZSTD_seqSymbol e178; + public ZSTD_seqSymbol e179; + public ZSTD_seqSymbol e180; + public ZSTD_seqSymbol e181; + public ZSTD_seqSymbol e182; + public ZSTD_seqSymbol e183; + public ZSTD_seqSymbol e184; + public ZSTD_seqSymbol e185; + public ZSTD_seqSymbol e186; + public ZSTD_seqSymbol e187; + public ZSTD_seqSymbol e188; + public ZSTD_seqSymbol e189; + public ZSTD_seqSymbol e190; + public ZSTD_seqSymbol e191; + public ZSTD_seqSymbol e192; + public ZSTD_seqSymbol e193; + public ZSTD_seqSymbol e194; + public ZSTD_seqSymbol e195; + public ZSTD_seqSymbol e196; + public ZSTD_seqSymbol e197; + public ZSTD_seqSymbol e198; + public ZSTD_seqSymbol e199; + public ZSTD_seqSymbol e200; + public ZSTD_seqSymbol e201; + public ZSTD_seqSymbol e202; + public ZSTD_seqSymbol e203; + public ZSTD_seqSymbol e204; + public ZSTD_seqSymbol e205; + public ZSTD_seqSymbol e206; + public ZSTD_seqSymbol e207; + public ZSTD_seqSymbol e208; + public ZSTD_seqSymbol e209; + public ZSTD_seqSymbol e210; + public ZSTD_seqSymbol e211; + public ZSTD_seqSymbol e212; + public ZSTD_seqSymbol e213; + public ZSTD_seqSymbol e214; + public ZSTD_seqSymbol e215; + public ZSTD_seqSymbol e216; + public ZSTD_seqSymbol e217; + public ZSTD_seqSymbol e218; + public ZSTD_seqSymbol e219; + public ZSTD_seqSymbol e220; + public ZSTD_seqSymbol e221; + public ZSTD_seqSymbol e222; + public ZSTD_seqSymbol e223; + public ZSTD_seqSymbol e224; + public ZSTD_seqSymbol e225; + public ZSTD_seqSymbol e226; + public ZSTD_seqSymbol e227; + public ZSTD_seqSymbol e228; + public ZSTD_seqSymbol e229; + public ZSTD_seqSymbol e230; + public ZSTD_seqSymbol e231; + public ZSTD_seqSymbol e232; + public ZSTD_seqSymbol e233; + public ZSTD_seqSymbol e234; + public ZSTD_seqSymbol e235; + public ZSTD_seqSymbol e236; + public ZSTD_seqSymbol e237; + public ZSTD_seqSymbol e238; + public ZSTD_seqSymbol e239; + public ZSTD_seqSymbol e240; + public ZSTD_seqSymbol e241; + public ZSTD_seqSymbol e242; + public ZSTD_seqSymbol e243; + public ZSTD_seqSymbol e244; + public ZSTD_seqSymbol e245; + public ZSTD_seqSymbol e246; + public ZSTD_seqSymbol e247; + public ZSTD_seqSymbol e248; + public ZSTD_seqSymbol e249; + public ZSTD_seqSymbol e250; + public ZSTD_seqSymbol e251; + public ZSTD_seqSymbol e252; + public ZSTD_seqSymbol e253; + public ZSTD_seqSymbol e254; + public ZSTD_seqSymbol e255; + public ZSTD_seqSymbol e256; + public ZSTD_seqSymbol e257; + public ZSTD_seqSymbol e258; + public ZSTD_seqSymbol e259; + public ZSTD_seqSymbol e260; + public ZSTD_seqSymbol e261; + public ZSTD_seqSymbol e262; + public ZSTD_seqSymbol e263; + public ZSTD_seqSymbol e264; + public ZSTD_seqSymbol e265; + public ZSTD_seqSymbol e266; + public ZSTD_seqSymbol e267; + public ZSTD_seqSymbol e268; + public ZSTD_seqSymbol e269; + public ZSTD_seqSymbol e270; + public ZSTD_seqSymbol e271; + public ZSTD_seqSymbol e272; + public ZSTD_seqSymbol e273; + public ZSTD_seqSymbol e274; + public ZSTD_seqSymbol e275; + public ZSTD_seqSymbol e276; + public ZSTD_seqSymbol e277; + public ZSTD_seqSymbol e278; + public ZSTD_seqSymbol e279; + public ZSTD_seqSymbol e280; + public ZSTD_seqSymbol e281; + public ZSTD_seqSymbol e282; + public ZSTD_seqSymbol e283; + public ZSTD_seqSymbol e284; + public ZSTD_seqSymbol e285; + public ZSTD_seqSymbol e286; + public ZSTD_seqSymbol e287; + public ZSTD_seqSymbol e288; + public ZSTD_seqSymbol e289; + public ZSTD_seqSymbol e290; + public ZSTD_seqSymbol e291; + public ZSTD_seqSymbol e292; + public ZSTD_seqSymbol e293; + public ZSTD_seqSymbol e294; + public ZSTD_seqSymbol e295; + public ZSTD_seqSymbol e296; + public ZSTD_seqSymbol e297; + public ZSTD_seqSymbol e298; + public ZSTD_seqSymbol e299; + public ZSTD_seqSymbol e300; + public ZSTD_seqSymbol e301; + public ZSTD_seqSymbol e302; + public ZSTD_seqSymbol e303; + public ZSTD_seqSymbol e304; + public ZSTD_seqSymbol e305; + public ZSTD_seqSymbol e306; + public ZSTD_seqSymbol e307; + public ZSTD_seqSymbol e308; + public ZSTD_seqSymbol e309; + public ZSTD_seqSymbol e310; + public ZSTD_seqSymbol e311; + public ZSTD_seqSymbol e312; + public ZSTD_seqSymbol e313; + public ZSTD_seqSymbol e314; + public ZSTD_seqSymbol e315; + public ZSTD_seqSymbol e316; + public ZSTD_seqSymbol e317; + public ZSTD_seqSymbol e318; + public ZSTD_seqSymbol e319; + public ZSTD_seqSymbol e320; + public ZSTD_seqSymbol e321; + public ZSTD_seqSymbol e322; + public ZSTD_seqSymbol e323; + public ZSTD_seqSymbol e324; + public ZSTD_seqSymbol e325; + public ZSTD_seqSymbol e326; + public ZSTD_seqSymbol e327; + public ZSTD_seqSymbol e328; + public ZSTD_seqSymbol e329; + public ZSTD_seqSymbol e330; + public ZSTD_seqSymbol e331; + public ZSTD_seqSymbol e332; + public ZSTD_seqSymbol e333; + public ZSTD_seqSymbol e334; + public ZSTD_seqSymbol e335; + public ZSTD_seqSymbol e336; + public ZSTD_seqSymbol e337; + public ZSTD_seqSymbol e338; + public ZSTD_seqSymbol e339; + public ZSTD_seqSymbol e340; + public ZSTD_seqSymbol e341; + public ZSTD_seqSymbol e342; + public ZSTD_seqSymbol e343; + public ZSTD_seqSymbol e344; + public ZSTD_seqSymbol e345; + public ZSTD_seqSymbol e346; + public ZSTD_seqSymbol e347; + public ZSTD_seqSymbol e348; + public ZSTD_seqSymbol e349; + public ZSTD_seqSymbol e350; + public ZSTD_seqSymbol e351; + public ZSTD_seqSymbol e352; + public ZSTD_seqSymbol e353; + public ZSTD_seqSymbol e354; + public ZSTD_seqSymbol e355; + public ZSTD_seqSymbol e356; + public ZSTD_seqSymbol e357; + public ZSTD_seqSymbol e358; + public ZSTD_seqSymbol e359; + public ZSTD_seqSymbol e360; + public ZSTD_seqSymbol e361; + public ZSTD_seqSymbol e362; + public ZSTD_seqSymbol e363; + public ZSTD_seqSymbol e364; + public ZSTD_seqSymbol e365; + public ZSTD_seqSymbol e366; + public ZSTD_seqSymbol e367; + public ZSTD_seqSymbol e368; + public ZSTD_seqSymbol e369; + public ZSTD_seqSymbol e370; + public ZSTD_seqSymbol e371; + public ZSTD_seqSymbol e372; + public ZSTD_seqSymbol e373; + public ZSTD_seqSymbol e374; + public ZSTD_seqSymbol e375; + public ZSTD_seqSymbol e376; + public ZSTD_seqSymbol e377; + public ZSTD_seqSymbol e378; + public ZSTD_seqSymbol e379; + public ZSTD_seqSymbol e380; + public ZSTD_seqSymbol e381; + public ZSTD_seqSymbol e382; + public ZSTD_seqSymbol e383; + public ZSTD_seqSymbol e384; + public ZSTD_seqSymbol e385; + public ZSTD_seqSymbol e386; + public ZSTD_seqSymbol e387; + public ZSTD_seqSymbol e388; + public ZSTD_seqSymbol e389; + public ZSTD_seqSymbol e390; + public ZSTD_seqSymbol e391; + public ZSTD_seqSymbol e392; + public ZSTD_seqSymbol e393; + public ZSTD_seqSymbol e394; + public ZSTD_seqSymbol e395; + public ZSTD_seqSymbol e396; + public ZSTD_seqSymbol e397; + public ZSTD_seqSymbol e398; + public ZSTD_seqSymbol e399; + public ZSTD_seqSymbol e400; + public ZSTD_seqSymbol e401; + public ZSTD_seqSymbol e402; + public ZSTD_seqSymbol e403; + public ZSTD_seqSymbol e404; + public ZSTD_seqSymbol e405; + public ZSTD_seqSymbol e406; + public ZSTD_seqSymbol e407; + public ZSTD_seqSymbol e408; + public ZSTD_seqSymbol e409; + public ZSTD_seqSymbol e410; + public ZSTD_seqSymbol e411; + public ZSTD_seqSymbol e412; + public ZSTD_seqSymbol e413; + public ZSTD_seqSymbol e414; + public ZSTD_seqSymbol e415; + public ZSTD_seqSymbol e416; + public ZSTD_seqSymbol e417; + public ZSTD_seqSymbol e418; + public ZSTD_seqSymbol e419; + public ZSTD_seqSymbol e420; + public ZSTD_seqSymbol e421; + public ZSTD_seqSymbol e422; + public ZSTD_seqSymbol e423; + public ZSTD_seqSymbol e424; + public ZSTD_seqSymbol e425; + public ZSTD_seqSymbol e426; + public ZSTD_seqSymbol e427; + public ZSTD_seqSymbol e428; + public ZSTD_seqSymbol e429; + public ZSTD_seqSymbol e430; + public ZSTD_seqSymbol e431; + public ZSTD_seqSymbol e432; + public ZSTD_seqSymbol e433; + public ZSTD_seqSymbol e434; + public ZSTD_seqSymbol e435; + public ZSTD_seqSymbol e436; + public ZSTD_seqSymbol e437; + public ZSTD_seqSymbol e438; + public ZSTD_seqSymbol e439; + public ZSTD_seqSymbol e440; + public ZSTD_seqSymbol e441; + public ZSTD_seqSymbol e442; + public ZSTD_seqSymbol e443; + public ZSTD_seqSymbol e444; + public ZSTD_seqSymbol e445; + public ZSTD_seqSymbol e446; + public ZSTD_seqSymbol e447; + public ZSTD_seqSymbol e448; + public ZSTD_seqSymbol e449; + public ZSTD_seqSymbol e450; + public ZSTD_seqSymbol e451; + public ZSTD_seqSymbol e452; + public ZSTD_seqSymbol e453; + public ZSTD_seqSymbol e454; + public ZSTD_seqSymbol e455; + public ZSTD_seqSymbol e456; + public ZSTD_seqSymbol e457; + public ZSTD_seqSymbol e458; + public ZSTD_seqSymbol e459; + public ZSTD_seqSymbol e460; + public ZSTD_seqSymbol e461; + public ZSTD_seqSymbol e462; + public ZSTD_seqSymbol e463; + public ZSTD_seqSymbol e464; + public ZSTD_seqSymbol e465; + public ZSTD_seqSymbol e466; + public ZSTD_seqSymbol e467; + public ZSTD_seqSymbol e468; + public ZSTD_seqSymbol e469; + public ZSTD_seqSymbol e470; + public ZSTD_seqSymbol e471; + public ZSTD_seqSymbol e472; + public ZSTD_seqSymbol e473; + public ZSTD_seqSymbol e474; + public ZSTD_seqSymbol e475; + public ZSTD_seqSymbol e476; + public ZSTD_seqSymbol e477; + public ZSTD_seqSymbol e478; + public ZSTD_seqSymbol e479; + public ZSTD_seqSymbol e480; + public ZSTD_seqSymbol e481; + public ZSTD_seqSymbol e482; + public ZSTD_seqSymbol e483; + public ZSTD_seqSymbol e484; + public ZSTD_seqSymbol e485; + public ZSTD_seqSymbol e486; + public ZSTD_seqSymbol e487; + public ZSTD_seqSymbol e488; + public ZSTD_seqSymbol e489; + public ZSTD_seqSymbol e490; + public ZSTD_seqSymbol e491; + public ZSTD_seqSymbol e492; + public ZSTD_seqSymbol e493; + public ZSTD_seqSymbol e494; + public ZSTD_seqSymbol e495; + public ZSTD_seqSymbol e496; + public ZSTD_seqSymbol e497; + public ZSTD_seqSymbol e498; + public ZSTD_seqSymbol e499; + public ZSTD_seqSymbol e500; + public ZSTD_seqSymbol e501; + public ZSTD_seqSymbol e502; + public ZSTD_seqSymbol e503; + public ZSTD_seqSymbol e504; + public ZSTD_seqSymbol e505; + public ZSTD_seqSymbol e506; + public ZSTD_seqSymbol e507; + public ZSTD_seqSymbol e508; + public ZSTD_seqSymbol e509; + public ZSTD_seqSymbol e510; + public ZSTD_seqSymbol e511; + public ZSTD_seqSymbol e512; + + public ref ZSTD_seqSymbol this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref ZSTD_seqSymbol this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref ZSTD_seqSymbol this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator ZSTD_seqSymbol*(in _MLTable_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_MLTable_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static ZSTD_seqSymbol* operator +(in _MLTable_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_MLTable_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_forceIgnoreChecksum_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_forceIgnoreChecksum_e.cs new file mode 100644 index 00000000..48c75c7b --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_forceIgnoreChecksum_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_forceIgnoreChecksum_e + { + ZSTD_d_validateChecksum = 0, + ZSTD_d_ignoreChecksum = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_format_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_format_e.cs new file mode 100644 index 00000000..091074c6 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_format_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_format_e + { + ZSTD_f_zstd1 = 0, + ZSTD_f_zstd1_magicless = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameHeader.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameHeader.cs new file mode 100644 index 00000000..219734d9 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameHeader.cs @@ -0,0 +1,24 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_frameHeader + { + /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */ + public ulong frameContentSize; + + /* can be very large, up to <= frameContentSize */ + public ulong windowSize; + + public uint blockSizeMax; + + /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */ + public ZSTD_frameType_e frameType; + + public uint headerSize; + + public uint dictID; + + public uint checksumFlag; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameParameters.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameParameters.cs new file mode 100644 index 00000000..6bbf1682 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameParameters.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_frameParameters + { + /**< 1: content size will be in frame header (when known) */ + public int contentSizeFlag; + + /**< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */ + public int checksumFlag; + + /**< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */ + public int noDictIDFlag; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameProgression.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameProgression.cs new file mode 100644 index 00000000..f2a1d938 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameProgression.cs @@ -0,0 +1,25 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_frameProgression + { + /* nb input bytes read and buffered */ + public ulong ingested; + + /* nb input bytes actually compressed */ + public ulong consumed; + + /* nb of compressed bytes generated and buffered */ + public ulong produced; + + /* nb of compressed bytes flushed : not provided; can be tracked from caller side */ + public ulong flushed; + + /* MT only : latest started job nb */ + public uint currentJobID; + + /* MT only : nb of workers actively compressing at probe time */ + public uint nbActiveWorkers; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameSizeInfo.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameSizeInfo.cs new file mode 100644 index 00000000..30176dc7 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameSizeInfo.cs @@ -0,0 +1,17 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /** + * Contains the compressed frame size and an upper-bound for the decompressed frame size. + * Note: before using `compressedSize`, check for errors using ZSTD_isError(). + * similarly, before using `decompressedBound`, check for errors using: + * `decompressedBound != ZSTD_CONTENTSIZE_ERROR` + */ + public partial struct ZSTD_frameSizeInfo + { + public nuint compressedSize; + + public ulong decompressedBound; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameType_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameType_e.cs new file mode 100644 index 00000000..a2c7f08e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_frameType_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_frameType_e + { + ZSTD_frame, + ZSTD_skippableFrame, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseCTablesMetadata_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseCTablesMetadata_t.cs new file mode 100644 index 00000000..4b46cfc3 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseCTablesMetadata_t.cs @@ -0,0 +1,25 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /** ZSTD_fseCTablesMetadata_t : + * Stores symbol compression modes for a super-block in {ll, ol, ml}Type, and + * fse tables in fseTablesBuffer. + * fseTablesSize refers to the size of fse tables in bytes. + * This metadata is populated in ZSTD_buildBlockEntropyStats_sequences() */ + public unsafe partial struct ZSTD_fseCTablesMetadata_t + { + public symbolEncodingType_e llType; + + public symbolEncodingType_e ofType; + + public symbolEncodingType_e mlType; + + public fixed byte fseTablesBuffer[133]; + + public nuint fseTablesSize; + + /* This is to account for bug in 1.3.4. More detail in ZSTD_entropyCompressSeqStore_internal() */ + public nuint lastCountSize; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseCTables_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseCTables_t.cs new file mode 100644 index 00000000..d2c768c4 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseCTables_t.cs @@ -0,0 +1,19 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_fseCTables_t + { + public fixed uint offcodeCTable[193]; + + public fixed uint matchlengthCTable[363]; + + public fixed uint litlengthCTable[329]; + + public FSE_repeat offcode_repeatMode; + + public FSE_repeat matchlength_repeatMode; + + public FSE_repeat litlength_repeatMode; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseState.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseState.cs new file mode 100644 index 00000000..941781aa --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_fseState.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_fseState + { + public nuint state; + + public ZSTD_seqSymbol* table; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_hufCTablesMetadata_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_hufCTablesMetadata_t.cs new file mode 100644 index 00000000..bfec9590 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_hufCTablesMetadata_t.cs @@ -0,0 +1,21 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*********************************************** + * Entropy buffer statistics structs and funcs * + ***********************************************/ + /** ZSTD_hufCTablesMetadata_t : + * Stores Literals Block Type for a super-block in hType, and + * huffman tree description in hufDesBuffer. + * hufDesSize refers to the size of huffman tree description in bytes. + * This metadata is populated in ZSTD_buildBlockEntropyStats_literals() */ + public unsafe partial struct ZSTD_hufCTablesMetadata_t + { + public symbolEncodingType_e hType; + + public fixed byte hufDesBuffer[128]; + + public nuint hufDesSize; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_hufCTables_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_hufCTables_t.cs new file mode 100644 index 00000000..506c3e5c --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_hufCTables_t.cs @@ -0,0 +1,320 @@ +using InlineIL; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static InlineIL.IL.Emit; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_hufCTables_t + { + public _CTable_e__FixedBuffer CTable; + + public HUF_repeat repeatMode; + + public unsafe partial struct _CTable_e__FixedBuffer + { + public HUF_CElt_s e0; + public HUF_CElt_s e1; + public HUF_CElt_s e2; + public HUF_CElt_s e3; + public HUF_CElt_s e4; + public HUF_CElt_s e5; + public HUF_CElt_s e6; + public HUF_CElt_s e7; + public HUF_CElt_s e8; + public HUF_CElt_s e9; + public HUF_CElt_s e10; + public HUF_CElt_s e11; + public HUF_CElt_s e12; + public HUF_CElt_s e13; + public HUF_CElt_s e14; + public HUF_CElt_s e15; + public HUF_CElt_s e16; + public HUF_CElt_s e17; + public HUF_CElt_s e18; + public HUF_CElt_s e19; + public HUF_CElt_s e20; + public HUF_CElt_s e21; + public HUF_CElt_s e22; + public HUF_CElt_s e23; + public HUF_CElt_s e24; + public HUF_CElt_s e25; + public HUF_CElt_s e26; + public HUF_CElt_s e27; + public HUF_CElt_s e28; + public HUF_CElt_s e29; + public HUF_CElt_s e30; + public HUF_CElt_s e31; + public HUF_CElt_s e32; + public HUF_CElt_s e33; + public HUF_CElt_s e34; + public HUF_CElt_s e35; + public HUF_CElt_s e36; + public HUF_CElt_s e37; + public HUF_CElt_s e38; + public HUF_CElt_s e39; + public HUF_CElt_s e40; + public HUF_CElt_s e41; + public HUF_CElt_s e42; + public HUF_CElt_s e43; + public HUF_CElt_s e44; + public HUF_CElt_s e45; + public HUF_CElt_s e46; + public HUF_CElt_s e47; + public HUF_CElt_s e48; + public HUF_CElt_s e49; + public HUF_CElt_s e50; + public HUF_CElt_s e51; + public HUF_CElt_s e52; + public HUF_CElt_s e53; + public HUF_CElt_s e54; + public HUF_CElt_s e55; + public HUF_CElt_s e56; + public HUF_CElt_s e57; + public HUF_CElt_s e58; + public HUF_CElt_s e59; + public HUF_CElt_s e60; + public HUF_CElt_s e61; + public HUF_CElt_s e62; + public HUF_CElt_s e63; + public HUF_CElt_s e64; + public HUF_CElt_s e65; + public HUF_CElt_s e66; + public HUF_CElt_s e67; + public HUF_CElt_s e68; + public HUF_CElt_s e69; + public HUF_CElt_s e70; + public HUF_CElt_s e71; + public HUF_CElt_s e72; + public HUF_CElt_s e73; + public HUF_CElt_s e74; + public HUF_CElt_s e75; + public HUF_CElt_s e76; + public HUF_CElt_s e77; + public HUF_CElt_s e78; + public HUF_CElt_s e79; + public HUF_CElt_s e80; + public HUF_CElt_s e81; + public HUF_CElt_s e82; + public HUF_CElt_s e83; + public HUF_CElt_s e84; + public HUF_CElt_s e85; + public HUF_CElt_s e86; + public HUF_CElt_s e87; + public HUF_CElt_s e88; + public HUF_CElt_s e89; + public HUF_CElt_s e90; + public HUF_CElt_s e91; + public HUF_CElt_s e92; + public HUF_CElt_s e93; + public HUF_CElt_s e94; + public HUF_CElt_s e95; + public HUF_CElt_s e96; + public HUF_CElt_s e97; + public HUF_CElt_s e98; + public HUF_CElt_s e99; + public HUF_CElt_s e100; + public HUF_CElt_s e101; + public HUF_CElt_s e102; + public HUF_CElt_s e103; + public HUF_CElt_s e104; + public HUF_CElt_s e105; + public HUF_CElt_s e106; + public HUF_CElt_s e107; + public HUF_CElt_s e108; + public HUF_CElt_s e109; + public HUF_CElt_s e110; + public HUF_CElt_s e111; + public HUF_CElt_s e112; + public HUF_CElt_s e113; + public HUF_CElt_s e114; + public HUF_CElt_s e115; + public HUF_CElt_s e116; + public HUF_CElt_s e117; + public HUF_CElt_s e118; + public HUF_CElt_s e119; + public HUF_CElt_s e120; + public HUF_CElt_s e121; + public HUF_CElt_s e122; + public HUF_CElt_s e123; + public HUF_CElt_s e124; + public HUF_CElt_s e125; + public HUF_CElt_s e126; + public HUF_CElt_s e127; + public HUF_CElt_s e128; + public HUF_CElt_s e129; + public HUF_CElt_s e130; + public HUF_CElt_s e131; + public HUF_CElt_s e132; + public HUF_CElt_s e133; + public HUF_CElt_s e134; + public HUF_CElt_s e135; + public HUF_CElt_s e136; + public HUF_CElt_s e137; + public HUF_CElt_s e138; + public HUF_CElt_s e139; + public HUF_CElt_s e140; + public HUF_CElt_s e141; + public HUF_CElt_s e142; + public HUF_CElt_s e143; + public HUF_CElt_s e144; + public HUF_CElt_s e145; + public HUF_CElt_s e146; + public HUF_CElt_s e147; + public HUF_CElt_s e148; + public HUF_CElt_s e149; + public HUF_CElt_s e150; + public HUF_CElt_s e151; + public HUF_CElt_s e152; + public HUF_CElt_s e153; + public HUF_CElt_s e154; + public HUF_CElt_s e155; + public HUF_CElt_s e156; + public HUF_CElt_s e157; + public HUF_CElt_s e158; + public HUF_CElt_s e159; + public HUF_CElt_s e160; + public HUF_CElt_s e161; + public HUF_CElt_s e162; + public HUF_CElt_s e163; + public HUF_CElt_s e164; + public HUF_CElt_s e165; + public HUF_CElt_s e166; + public HUF_CElt_s e167; + public HUF_CElt_s e168; + public HUF_CElt_s e169; + public HUF_CElt_s e170; + public HUF_CElt_s e171; + public HUF_CElt_s e172; + public HUF_CElt_s e173; + public HUF_CElt_s e174; + public HUF_CElt_s e175; + public HUF_CElt_s e176; + public HUF_CElt_s e177; + public HUF_CElt_s e178; + public HUF_CElt_s e179; + public HUF_CElt_s e180; + public HUF_CElt_s e181; + public HUF_CElt_s e182; + public HUF_CElt_s e183; + public HUF_CElt_s e184; + public HUF_CElt_s e185; + public HUF_CElt_s e186; + public HUF_CElt_s e187; + public HUF_CElt_s e188; + public HUF_CElt_s e189; + public HUF_CElt_s e190; + public HUF_CElt_s e191; + public HUF_CElt_s e192; + public HUF_CElt_s e193; + public HUF_CElt_s e194; + public HUF_CElt_s e195; + public HUF_CElt_s e196; + public HUF_CElt_s e197; + public HUF_CElt_s e198; + public HUF_CElt_s e199; + public HUF_CElt_s e200; + public HUF_CElt_s e201; + public HUF_CElt_s e202; + public HUF_CElt_s e203; + public HUF_CElt_s e204; + public HUF_CElt_s e205; + public HUF_CElt_s e206; + public HUF_CElt_s e207; + public HUF_CElt_s e208; + public HUF_CElt_s e209; + public HUF_CElt_s e210; + public HUF_CElt_s e211; + public HUF_CElt_s e212; + public HUF_CElt_s e213; + public HUF_CElt_s e214; + public HUF_CElt_s e215; + public HUF_CElt_s e216; + public HUF_CElt_s e217; + public HUF_CElt_s e218; + public HUF_CElt_s e219; + public HUF_CElt_s e220; + public HUF_CElt_s e221; + public HUF_CElt_s e222; + public HUF_CElt_s e223; + public HUF_CElt_s e224; + public HUF_CElt_s e225; + public HUF_CElt_s e226; + public HUF_CElt_s e227; + public HUF_CElt_s e228; + public HUF_CElt_s e229; + public HUF_CElt_s e230; + public HUF_CElt_s e231; + public HUF_CElt_s e232; + public HUF_CElt_s e233; + public HUF_CElt_s e234; + public HUF_CElt_s e235; + public HUF_CElt_s e236; + public HUF_CElt_s e237; + public HUF_CElt_s e238; + public HUF_CElt_s e239; + public HUF_CElt_s e240; + public HUF_CElt_s e241; + public HUF_CElt_s e242; + public HUF_CElt_s e243; + public HUF_CElt_s e244; + public HUF_CElt_s e245; + public HUF_CElt_s e246; + public HUF_CElt_s e247; + public HUF_CElt_s e248; + public HUF_CElt_s e249; + public HUF_CElt_s e250; + public HUF_CElt_s e251; + public HUF_CElt_s e252; + public HUF_CElt_s e253; + public HUF_CElt_s e254; + public HUF_CElt_s e255; + + public ref HUF_CElt_s this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref HUF_CElt_s this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref HUF_CElt_s this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator HUF_CElt_s*(in _CTable_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_CTable_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static HUF_CElt_s* operator +(in _CTable_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_CTable_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_inBuffer_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_inBuffer_s.cs new file mode 100644 index 00000000..0fdcff35 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_inBuffer_s.cs @@ -0,0 +1,19 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /**************************** + * Streaming + ****************************/ + public unsafe partial struct ZSTD_inBuffer_s + { + /**< start of input buffer */ + public void* src; + + /**< size of input buffer */ + public nuint size; + + /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ + public nuint pos; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_indexResetPolicy_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_indexResetPolicy_e.cs new file mode 100644 index 00000000..0bd333da --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_indexResetPolicy_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_indexResetPolicy_e + { + ZSTDirp_continue, + ZSTDirp_reset, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_literalCompressionMode_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_literalCompressionMode_e.cs new file mode 100644 index 00000000..537ce580 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_literalCompressionMode_e.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_literalCompressionMode_e + { + ZSTD_lcm_auto = 0, + ZSTD_lcm_huffman = 1, + ZSTD_lcm_uncompressed = 2, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_localDict.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_localDict.cs new file mode 100644 index 00000000..cc6d673c --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_localDict.cs @@ -0,0 +1,17 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_localDict + { + public void* dictBuffer; + + public void* dict; + + public nuint dictSize; + + public ZSTD_dictContentType_e dictContentType; + + public ZSTD_CDict_s* cdict; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_longLengthType_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_longLengthType_e.cs new file mode 100644 index 00000000..ce0938f2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_longLengthType_e.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_longLengthType_e + { + ZSTD_llt_none = 0, + ZSTD_llt_literalLength = 1, + ZSTD_llt_matchLength = 2, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_longOffset_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_longOffset_e.cs new file mode 100644 index 00000000..fa8146f1 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_longOffset_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_longOffset_e + { + ZSTD_lo_isRegularOffset, + ZSTD_lo_isLongOffset = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_matchState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_matchState_t.cs new file mode 100644 index 00000000..dc0e6577 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_matchState_t.cs @@ -0,0 +1,57 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_matchState_t + { + /* State for window round buffer management */ + public ZSTD_window_t window; + + /* index of end of dictionary, within context's referential. + * When loadedDictEnd != 0, a dictionary is in use, and still valid. + * This relies on a mechanism to set loadedDictEnd=0 when dictionary is no longer within distance. + * Such mechanism is provided within ZSTD_window_enforceMaxDist() and ZSTD_checkDictValidity(). + * When dict referential is copied into active context (i.e. not attached), + * loadedDictEnd == dictSize, since referential starts from zero. + */ + public uint loadedDictEnd; + + /* index from which to continue table update */ + public uint nextToUpdate; + + /* dispatch table for matches of len==3 : larger == faster, more memory */ + public uint hashLog3; + + /* For row-based matchfinder: Hashlog based on nb of rows in the hashTable.*/ + public uint rowHashLog; + + /* For row-based matchFinder: A row-based table containing the hashes and head index. */ + public ushort* tagTable; + + /* For row-based matchFinder: a cache of hashes to improve speed */ + public fixed uint hashCache[8]; + + public uint* hashTable; + + public uint* hashTable3; + + public uint* chainTable; + + /* Non-zero if we should force non-contiguous load for the next window update. */ + public uint forceNonContiguous; + + /* Indicates whether this matchState is using the + * dedicated dictionary search structure. + */ + public int dedicatedDictSearch; + + /* optimal parser state */ + public optState_t opt; + + public ZSTD_matchState_t* dictMatchState; + + public ZSTD_compressionParameters cParams; + + public rawSeqStore_t* ldmSeqStore; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_match_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_match_t.cs new file mode 100644 index 00000000..7774e187 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_match_t.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /********************************* + * Compression internals structs * + *********************************/ + public partial struct ZSTD_match_t + { + /* Offset code (offset + ZSTD_REP_MOVE) for the match */ + public uint off; + + /* Raw length of match */ + public uint len; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_nextInputType_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_nextInputType_e.cs new file mode 100644 index 00000000..59a920fd --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_nextInputType_e.cs @@ -0,0 +1,14 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_nextInputType_e + { + ZSTDnit_frameHeader, + ZSTDnit_blockHeader, + ZSTDnit_block, + ZSTDnit_lastBlock, + ZSTDnit_checksum, + ZSTDnit_skippableFrame, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_optLdm_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_optLdm_t.cs new file mode 100644 index 00000000..3cf00593 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_optLdm_t.cs @@ -0,0 +1,20 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* Struct containing info needed to make decision about ldm inclusion */ + public partial struct ZSTD_optLdm_t + { + /* External match candidates store for this block */ + public rawSeqStore_t seqStore; + + /* Start position of the current match candidate */ + public uint startPosInBlock; + + /* End position of the current match candidate */ + public uint endPosInBlock; + + /* Offset of the match candidate */ + public uint offset; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_optimal_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_optimal_t.cs new file mode 100644 index 00000000..8db8a807 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_optimal_t.cs @@ -0,0 +1,17 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_optimal_t + { + public int price; + + public uint off; + + public uint mlen; + + public uint litlen; + + public fixed uint rep[3]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_outBuffer_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_outBuffer_s.cs new file mode 100644 index 00000000..a2c08154 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_outBuffer_s.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_outBuffer_s + { + /**< start of output buffer */ + public void* dst; + + /**< size of output buffer */ + public nuint size; + + /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ + public nuint pos; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_overlap_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_overlap_e.cs new file mode 100644 index 00000000..042938a7 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_overlap_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_overlap_e + { + ZSTD_no_overlap, + ZSTD_overlap_src_before_dst, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_parameters.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_parameters.cs new file mode 100644 index 00000000..bc85b0b8 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_parameters.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_parameters + { + public ZSTD_compressionParameters cParams; + + public ZSTD_frameParameters fParams; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_prefixDict_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_prefixDict_s.cs new file mode 100644 index 00000000..8c47590c --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_prefixDict_s.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_prefixDict_s + { + public void* dict; + + public nuint dictSize; + + public ZSTD_dictContentType_e dictContentType; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_refMultipleDDicts_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_refMultipleDDicts_e.cs new file mode 100644 index 00000000..9182b17a --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_refMultipleDDicts_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_refMultipleDDicts_e + { + ZSTD_rmd_refSingleDDict = 0, + ZSTD_rmd_refMultipleDDicts = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_resetTarget_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_resetTarget_e.cs new file mode 100644 index 00000000..e3c0ac22 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_resetTarget_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_resetTarget_e + { + ZSTD_resetTarget_CDict, + ZSTD_resetTarget_CCtx, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_seqSymbol.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_seqSymbol.cs new file mode 100644 index 00000000..cd9ee55a --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_seqSymbol.cs @@ -0,0 +1,15 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_seqSymbol + { + public ushort nextState; + + public byte nbAdditionalBits; + + public byte nbBits; + + public uint baseValue; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_seqSymbol_header.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_seqSymbol_header.cs new file mode 100644 index 00000000..f135b312 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_seqSymbol_header.cs @@ -0,0 +1,14 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*-******************************************************* + * Decompression types + *********************************************************/ + public partial struct ZSTD_seqSymbol_header + { + public uint fastMode; + + public uint tableLog; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceCopier.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceCopier.cs new file mode 100644 index 00000000..12eb3308 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceCopier.cs @@ -0,0 +1,8 @@ +using System; +using System.Runtime.InteropServices; + +namespace ZstdSharp.Unsafe +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public unsafe delegate nuint ZSTD_sequenceCopier(ZSTD_CCtx_s* cctx, ZSTD_sequencePosition* seqPos, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* src, nuint blockSize); +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceFormat_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceFormat_e.cs new file mode 100644 index 00000000..3ee4c9ac --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceFormat_e.cs @@ -0,0 +1,10 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_sequenceFormat_e + { + ZSTD_sf_noBlockDelimiters = 0, + ZSTD_sf_explicitBlockDelimiters = 1, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceLength.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceLength.cs new file mode 100644 index 00000000..869a4a40 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequenceLength.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_sequenceLength + { + public uint litLength; + + public uint matchLength; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequencePosition.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequencePosition.cs new file mode 100644 index 00000000..217e105a --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_sequencePosition.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ZSTD_sequencePosition + { + /* Index in array of ZSTD_Sequence */ + public uint idx; + + /* Position within sequence at idx */ + public uint posInSequence; + + /* Number of bytes given by sequences provided so far */ + public nuint posInSrc; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_strategy.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_strategy.cs new file mode 100644 index 00000000..1be190b5 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_strategy.cs @@ -0,0 +1,17 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_strategy + { + ZSTD_fast = 1, + ZSTD_dfast = 2, + ZSTD_greedy = 3, + ZSTD_lazy = 4, + ZSTD_lazy2 = 5, + ZSTD_btlazy2 = 6, + ZSTD_btopt = 7, + ZSTD_btultra = 8, + ZSTD_btultra2 = 9, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_symbolEncodingTypeStats_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_symbolEncodingTypeStats_t.cs new file mode 100644 index 00000000..7f2295df --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_symbolEncodingTypeStats_t.cs @@ -0,0 +1,21 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* Type returned by ZSTD_buildSequencesStatistics containing finalized symbol encoding types + * and size of the sequences statistics + */ + public partial struct ZSTD_symbolEncodingTypeStats_t + { + public uint LLtype; + + public uint Offtype; + + public uint MLtype; + + public nuint size; + + /* Accounts for bug in 1.3.4. More detail in ZSTD_entropyCompressSeqStore_internal() */ + public nuint lastCountSize; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_useRowMatchFinderMode_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_useRowMatchFinderMode_e.cs new file mode 100644 index 00000000..45395fb0 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_useRowMatchFinderMode_e.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum ZSTD_useRowMatchFinderMode_e + { + ZSTD_urm_auto = 0, + ZSTD_urm_disableRowMatchFinder = 1, + ZSTD_urm_enableRowMatchFinder = 2, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_window_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_window_t.cs new file mode 100644 index 00000000..67c24d70 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZSTD_window_t.cs @@ -0,0 +1,28 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ZSTD_window_t + { + /* next block here to continue on current prefix */ + public byte* nextSrc; + + /* All regular indexes relative to this position */ + public byte* @base; + + /* extDict indexes relative to this position */ + public byte* dictBase; + + /* below that point, need extDict */ + public uint dictLimit; + + /* below that point, no more valid data */ + public uint lowLimit; + + /* Number of times overflow correction has run since + * ZSTD_window_init(). Useful for debugging coredumps + * and for ZSTD_WINDOW_OVERFLOW_CORRECT_FREQUENTLY. + */ + public uint nbOverflowCorrections; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Zdict.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Zdict.cs new file mode 100644 index 00000000..f7064ac7 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Zdict.cs @@ -0,0 +1,572 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*-******************************************************** + * Helper functions + **********************************************************/ + public static uint ZDICT_isError(nuint errorCode) + { + return ERR_isError(errorCode); + } + + public static string ZDICT_getErrorName(nuint errorCode) + { + return ERR_getErrorName(errorCode); + } + + private static void ZDICT_countEStats(EStats_ress_t esr, ZSTD_parameters* @params, uint* countLit, uint* offsetcodeCount, uint* matchlengthCount, uint* litlengthCount, uint* repOffsets, void* src, nuint srcSize, uint notificationLevel) + { + nuint blockSizeMax = (nuint)((((1 << 17)) < (1 << (int)@params->cParams.windowLog) ? ((1 << 17)) : (1 << (int)@params->cParams.windowLog))); + nuint cSize; + + if (srcSize > blockSizeMax) + { + srcSize = blockSizeMax; + } + + + { + nuint errorCode = ZSTD_compressBegin_usingCDict(esr.zc, esr.dict); + + if ((ERR_isError(errorCode)) != 0) + { + return; + } + } + + cSize = ZSTD_compressBlock(esr.zc, esr.workPlace, (nuint)((1 << 17)), src, srcSize); + if ((ERR_isError(cSize)) != 0) + { + return; + } + + if (cSize != 0) + { + seqStore_t* seqStorePtr = ZSTD_getSeqStore(esr.zc); + + + { + byte* bytePtr; + + for (bytePtr = seqStorePtr->litStart; bytePtr < seqStorePtr->lit; bytePtr++) + { + countLit[*bytePtr]++; + } + } + + + { + uint nbSeq = (uint)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + + ZSTD_seqToCodes(seqStorePtr); + + { + byte* codePtr = seqStorePtr->ofCode; + uint u; + + for (u = 0; u < nbSeq; u++) + { + offsetcodeCount[codePtr[u]]++; + } + } + + + { + byte* codePtr = seqStorePtr->mlCode; + uint u; + + for (u = 0; u < nbSeq; u++) + { + matchlengthCount[codePtr[u]]++; + } + } + + + { + byte* codePtr = seqStorePtr->llCode; + uint u; + + for (u = 0; u < nbSeq; u++) + { + litlengthCount[codePtr[u]]++; + } + } + + if (nbSeq >= 2) + { + seqDef_s* seq = seqStorePtr->sequencesStart; + uint offset1 = seq[0].offset - 3; + uint offset2 = seq[1].offset - 3; + + if (offset1 >= 1024) + { + offset1 = 0; + } + + if (offset2 >= 1024) + { + offset2 = 0; + } + + repOffsets[offset1] += 3; + repOffsets[offset2] += 1; + } + } + } + } + + private static nuint ZDICT_totalSampleSize(nuint* fileSizes, uint nbFiles) + { + nuint total = 0; + uint u; + + for (u = 0; u < nbFiles; u++) + { + total += fileSizes[u]; + } + + return total; + } + + private static void ZDICT_insertSortCount(offsetCount_t* table, uint val, uint count) + { + uint u; + + table[3].offset = val; + table[3].count = count; + for (u = 3; u > 0; u--) + { + offsetCount_t tmp; + + if (table[u - 1].count >= table[u].count) + { + break; + } + + tmp = table[u - 1]; + table[u - 1] = table[u]; + table[u] = tmp; + } + } + + /* ZDICT_flatLit() : + * rewrite `countLit` to contain a mostly flat but still compressible distribution of literals. + * necessary to avoid generating a non-compressible distribution that HUF_writeCTable() cannot encode. + */ + private static void ZDICT_flatLit(uint* countLit) + { + int u; + + for (u = 1; u < 256; u++) + { + countLit[u] = 2; + } + + countLit[0] = 4; + countLit[253] = 1; + countLit[254] = 1; + } + + private static nuint ZDICT_analyzeEntropy(void* dstBuffer, nuint maxDstSize, int compressionLevel, void* srcBuffer, nuint* fileSizes, uint nbFiles, void* dictBuffer, nuint dictBufferSize, uint notificationLevel) + { + uint* countLit = stackalloc uint[256]; + HUF_CElt_s* hufTable = stackalloc HUF_CElt_s[256]; + uint* offcodeCount = stackalloc uint[31]; + short* offcodeNCount = stackalloc short[31]; + uint offcodeMax = ZSTD_highbit32((uint)(dictBufferSize + (uint)(128 * (1 << 10)))); + uint* matchLengthCount = stackalloc uint[53]; + short* matchLengthNCount = stackalloc short[53]; + uint* litLengthCount = stackalloc uint[36]; + short* litLengthNCount = stackalloc short[36]; + uint* repOffset = stackalloc uint[1024]; + offsetCount_t* bestRepOffset = stackalloc offsetCount_t[4]; + EStats_ress_t esr = new EStats_ress_t + { + dict = null, + zc = null, + workPlace = null, + }; + ZSTD_parameters @params; + uint u, huffLog = 11, Offlog = 8, mlLog = 9, llLog = 9, total; + nuint pos = 0, errorCode; + nuint eSize = 0; + nuint totalSrcSize = ZDICT_totalSampleSize(fileSizes, nbFiles); + nuint averageSampleSize = totalSrcSize / (nbFiles + (uint)(nbFiles == 0 ? 1 : 0)); + byte* dstPtr = (byte*)(dstBuffer); + + if (offcodeMax > 30) + { + eSize = (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionaryCreation_failed))); + goto _cleanup; + } + + for (u = 0; u < 256; u++) + { + countLit[u] = 1; + } + + for (u = 0; u <= offcodeMax; u++) + { + offcodeCount[u] = 1; + } + + for (u = 0; u <= 52; u++) + { + matchLengthCount[u] = 1; + } + + for (u = 0; u <= 35; u++) + { + litLengthCount[u] = 1; + } + + memset((void*)repOffset, 0, (nuint)(sizeof(uint) * 1024)); + repOffset[1] = repOffset[4] = repOffset[8] = 1; + memset((void*)bestRepOffset, 0, (nuint)(sizeof(offsetCount_t) * 4)); + if (compressionLevel == 0) + { + compressionLevel = 3; + } + + @params = ZSTD_getParams(compressionLevel, (ulong)averageSampleSize, dictBufferSize); + esr.dict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, ZSTD_dictContentType_e.ZSTD_dct_rawContent, @params.cParams, ZSTD_defaultCMem); + esr.zc = ZSTD_createCCtx(); + esr.workPlace = malloc((nuint)((1 << 17))); + if (esr.dict == null || esr.zc == null || esr.workPlace == null) + { + eSize = (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + goto _cleanup; + } + + for (u = 0; u < nbFiles; u++) + { + ZDICT_countEStats(esr, &@params, (uint*)countLit, (uint*)offcodeCount, (uint*)matchLengthCount, (uint*)litLengthCount, (uint*)repOffset, (void*)((sbyte*)(srcBuffer) + pos), fileSizes[u], notificationLevel); + pos += fileSizes[u]; + } + + + { + nuint maxNbBits = HUF_buildCTable((HUF_CElt_s*)hufTable, (uint*)countLit, 255, huffLog); + + if ((ERR_isError(maxNbBits)) != 0) + { + eSize = maxNbBits; + goto _cleanup; + } + + if (maxNbBits == 8) + { + ZDICT_flatLit((uint*)countLit); + maxNbBits = HUF_buildCTable((HUF_CElt_s*)hufTable, (uint*)countLit, 255, huffLog); + assert(maxNbBits == 9); + } + + huffLog = (uint)(maxNbBits); + } + + + { + uint offset; + + for (offset = 1; offset < 1024; offset++) + { + ZDICT_insertSortCount(bestRepOffset, offset, repOffset[offset]); + } + } + + total = 0; + for (u = 0; u <= offcodeMax; u++) + { + total += offcodeCount[u]; + } + + errorCode = FSE_normalizeCount((short*)offcodeNCount, Offlog, (uint*)offcodeCount, total, offcodeMax, 1); + if ((ERR_isError(errorCode)) != 0) + { + eSize = errorCode; + goto _cleanup; + } + + Offlog = (uint)(errorCode); + total = 0; + for (u = 0; u <= 52; u++) + { + total += matchLengthCount[u]; + } + + errorCode = FSE_normalizeCount((short*)matchLengthNCount, mlLog, (uint*)matchLengthCount, total, 52, 1); + if ((ERR_isError(errorCode)) != 0) + { + eSize = errorCode; + goto _cleanup; + } + + mlLog = (uint)(errorCode); + total = 0; + for (u = 0; u <= 35; u++) + { + total += litLengthCount[u]; + } + + errorCode = FSE_normalizeCount((short*)litLengthNCount, llLog, (uint*)litLengthCount, total, 35, 1); + if ((ERR_isError(errorCode)) != 0) + { + eSize = errorCode; + goto _cleanup; + } + + llLog = (uint)(errorCode); + + { + nuint hhSize = HUF_writeCTable((void*)dstPtr, maxDstSize, (HUF_CElt_s*)hufTable, 255, huffLog); + + if ((ERR_isError(hhSize)) != 0) + { + eSize = hhSize; + goto _cleanup; + } + + dstPtr += hhSize; + maxDstSize -= hhSize; + eSize += hhSize; + } + + + { + nuint ohSize = FSE_writeNCount((void*)dstPtr, maxDstSize, (short*)offcodeNCount, 30, Offlog); + + if ((ERR_isError(ohSize)) != 0) + { + eSize = ohSize; + goto _cleanup; + } + + dstPtr += ohSize; + maxDstSize -= ohSize; + eSize += ohSize; + } + + + { + nuint mhSize = FSE_writeNCount((void*)dstPtr, maxDstSize, (short*)matchLengthNCount, 52, mlLog); + + if ((ERR_isError(mhSize)) != 0) + { + eSize = mhSize; + goto _cleanup; + } + + dstPtr += mhSize; + maxDstSize -= mhSize; + eSize += mhSize; + } + + + { + nuint lhSize = FSE_writeNCount((void*)dstPtr, maxDstSize, (short*)litLengthNCount, 35, llLog); + + if ((ERR_isError(lhSize)) != 0) + { + eSize = lhSize; + goto _cleanup; + } + + dstPtr += lhSize; + maxDstSize -= lhSize; + eSize += lhSize; + } + + if (maxDstSize < 12) + { + eSize = (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + goto _cleanup; + } + + MEM_writeLE32((void*)(dstPtr + 0), repStartValue[0]); + MEM_writeLE32((void*)(dstPtr + 4), repStartValue[1]); + MEM_writeLE32((void*)(dstPtr + 8), repStartValue[2]); + eSize += 12; + _cleanup: + ZSTD_freeCDict(esr.dict); + ZSTD_freeCCtx(esr.zc); + free(esr.workPlace); + return eSize; + } + + /*! ZDICT_finalizeDictionary(): + * Given a custom content as a basis for dictionary, and a set of samples, + * finalize dictionary by adding headers and statistics according to the zstd + * dictionary format. + * + * Samples must be stored concatenated in a flat buffer `samplesBuffer`, + * supplied with an array of sizes `samplesSizes`, providing the size of each + * sample in order. The samples are used to construct the statistics, so they + * should be representative of what you will compress with this dictionary. + * + * The compression level can be set in `parameters`. You should pass the + * compression level you expect to use in production. The statistics for each + * compression level differ, so tuning the dictionary for the compression level + * can help quite a bit. + * + * You can set an explicit dictionary ID in `parameters`, or allow us to pick + * a random dictionary ID for you, but we can't guarantee no collisions. + * + * The dstDictBuffer and the dictContent may overlap, and the content will be + * appended to the end of the header. If the header + the content doesn't fit in + * maxDictSize the beginning of the content is truncated to make room, since it + * is presumed that the most profitable content is at the end of the dictionary, + * since that is the cheapest to reference. + * + * `dictContentSize` must be >= ZDICT_CONTENTSIZE_MIN bytes. + * `maxDictSize` must be >= max(dictContentSize, ZSTD_DICTSIZE_MIN). + * + * @return: size of dictionary stored into `dstDictBuffer` (<= `maxDictSize`), + * or an error code, which can be tested by ZDICT_isError(). + * Note: ZDICT_finalizeDictionary() will push notifications into stderr if + * instructed to, using notificationLevel>0. + * NOTE: This function currently may fail in several edge cases including: + * * Not enough samples + * * Samples are uncompressible + * * Samples are all exactly the same + */ + public static nuint ZDICT_finalizeDictionary(void* dictBuffer, nuint dictBufferCapacity, void* customDictContent, nuint dictContentSize, void* samplesBuffer, nuint* samplesSizes, uint nbSamples, ZDICT_params_t @params) + { + nuint hSize; + byte* header = stackalloc byte[256]; + int compressionLevel = (@params.compressionLevel == 0) ? 3 : @params.compressionLevel; + uint notificationLevel = @params.notificationLevel; + + if (dictBufferCapacity < dictContentSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (dictContentSize < 128) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (dictBufferCapacity < 256) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + MEM_writeLE32((void*)header, 0xEC30A437); + + { + ulong randomID = XXH64(customDictContent, dictContentSize, 0); + uint compliantID = (uint)((randomID % ((1U << 31) - 32768)) + 32768); + uint dictID = @params.dictID != 0 ? @params.dictID : compliantID; + + MEM_writeLE32((void*)(header + 4), dictID); + } + + hSize = 8; + + { + nuint eSize = ZDICT_analyzeEntropy((void*)(header + hSize), 256 - hSize, compressionLevel, samplesBuffer, samplesSizes, nbSamples, customDictContent, dictContentSize, notificationLevel); + + if ((ZDICT_isError(eSize)) != 0) + { + return eSize; + } + + hSize += eSize; + } + + if (hSize + dictContentSize > dictBufferCapacity) + { + dictContentSize = dictBufferCapacity - hSize; + } + + + { + nuint dictSize = hSize + dictContentSize; + sbyte* dictEnd = (sbyte*)(dictBuffer) + dictSize; + + memmove((void*)(dictEnd - dictContentSize), customDictContent, dictContentSize); + memcpy(dictBuffer, (void*)header, hSize); + return dictSize; + } + } + + private static nuint ZDICT_addEntropyTablesFromBuffer_advanced(void* dictBuffer, nuint dictContentSize, nuint dictBufferCapacity, void* samplesBuffer, nuint* samplesSizes, uint nbSamples, ZDICT_params_t @params) + { + int compressionLevel = (@params.compressionLevel == 0) ? 3 : @params.compressionLevel; + uint notificationLevel = @params.notificationLevel; + nuint hSize = 8; + + + { + nuint eSize = ZDICT_analyzeEntropy((void*)((sbyte*)(dictBuffer) + hSize), dictBufferCapacity - hSize, compressionLevel, samplesBuffer, samplesSizes, nbSamples, (void*)((sbyte*)(dictBuffer) + dictBufferCapacity - dictContentSize), dictContentSize, notificationLevel); + + if ((ZDICT_isError(eSize)) != 0) + { + return eSize; + } + + hSize += eSize; + } + + MEM_writeLE32(dictBuffer, 0xEC30A437); + + { + ulong randomID = XXH64((void*)((sbyte*)(dictBuffer) + dictBufferCapacity - dictContentSize), dictContentSize, 0); + uint compliantID = (uint)((randomID % ((1U << 31) - 32768)) + 32768); + uint dictID = @params.dictID != 0 ? @params.dictID : compliantID; + + MEM_writeLE32((void*)((sbyte*)(dictBuffer) + 4), dictID); + } + + if (hSize + dictContentSize < dictBufferCapacity) + { + memmove((void*)((sbyte*)(dictBuffer) + hSize), (void*)((sbyte*)(dictBuffer) + dictBufferCapacity - dictContentSize), dictContentSize); + } + + return ((dictBufferCapacity) < (hSize + dictContentSize) ? (dictBufferCapacity) : (hSize + dictContentSize)); + } + + /*! ZDICT_trainFromBuffer(): + * Train a dictionary from an array of samples. + * Redirect towards ZDICT_optimizeTrainFromBuffer_fastCover() single-threaded, with d=8, steps=4, + * f=20, and accel=1. + * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, + * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. + * The resulting dictionary will be saved into `dictBuffer`. + * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) + * or an error code, which can be tested with ZDICT_isError(). + * Note: Dictionary training will fail if there are not enough samples to construct a + * dictionary, or if most of the samples are too small (< 8 bytes being the lower limit). + * If dictionary training fails, you should use zstd without a dictionary, as the dictionary + * would've been ineffective anyways. If you believe your samples would benefit from a dictionary + * please open an issue with details, and we can look into it. + * Note: ZDICT_trainFromBuffer()'s memory usage is about 6 MB. + * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. + * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. + * In general, it's recommended to provide a few thousands samples, though this can vary a lot. + * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. + */ + public static nuint ZDICT_trainFromBuffer(void* dictBuffer, nuint dictBufferCapacity, void* samplesBuffer, nuint* samplesSizes, uint nbSamples) + { + ZDICT_fastCover_params_t @params; + + memset((void*)&@params, 0, (nuint)(sizeof(ZDICT_fastCover_params_t))); + @params.d = 8; + @params.steps = 4; + @params.zParams.compressionLevel = 3; + return ZDICT_optimizeTrainFromBuffer_fastCover(dictBuffer, dictBufferCapacity, samplesBuffer, samplesSizes, nbSamples, &@params); + } + + public static nuint ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, nuint dictContentSize, nuint dictBufferCapacity, void* samplesBuffer, nuint* samplesSizes, uint nbSamples) + { + ZDICT_params_t @params; + + memset((void*)&@params, 0, (nuint)(sizeof(ZDICT_params_t))); + return ZDICT_addEntropyTablesFromBuffer_advanced(dictBuffer, dictContentSize, dictBufferCapacity, samplesBuffer, samplesSizes, nbSamples, @params); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/Zstd.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/Zstd.cs new file mode 100644 index 00000000..9c442154 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/Zstd.cs @@ -0,0 +1,15 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static readonly ZSTD_customMem ZSTD_defaultCMem = new ZSTD_customMem + { + customAlloc = null, + customFree = null, + opaque = null, + }; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCommon.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCommon.cs new file mode 100644 index 00000000..d0eb165b --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCommon.cs @@ -0,0 +1,90 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*-**************************************** + * Version + ******************************************/ + public static uint ZSTD_versionNumber() + { + return (uint)((1 * 100 * 100 + 5 * 100 + 0)); + } + + /*! ZSTD_versionString() : + * Return runtime library version, like "1.4.5". Requires v1.3.0+. */ + public static string ZSTD_versionString() + { + return "1.5.0"; + } + + /*! ZSTD_isError() : + * tells if a return value is an error code + * symbol is required for external callers */ + public static uint ZSTD_isError(nuint code) + { + return ERR_isError(code); + } + + /*! ZSTD_getErrorName() : + * provides error code string from function result (useful for debugging) */ + public static string ZSTD_getErrorName(nuint code) + { + return ERR_getErrorName(code); + } + + /*! ZSTD_getError() : + * convert a `size_t` function result into a proper ZSTD_errorCode enum */ + public static ZSTD_ErrorCode ZSTD_getErrorCode(nuint code) + { + return ERR_getErrorCode(code); + } + + /*! ZSTD_getErrorString() : + * provides error code string from enum */ + public static string ZSTD_getErrorString(ZSTD_ErrorCode code) + { + return ERR_getErrorString(code); + } + + /*=************************************************************** + * Custom allocator + ****************************************************************/ + public static void* ZSTD_customMalloc(nuint size, ZSTD_customMem customMem) + { + if (customMem.customAlloc != null) + { + throw new NotImplementedException("customMem is not implemented"); + } + + return malloc(size); + } + + public static void* ZSTD_customCalloc(nuint size, ZSTD_customMem customMem) + { + if (customMem.customAlloc != null) + { + throw new NotImplementedException("customMem is not implemented"); + } + + return calloc((1), (size)); + } + + public static void ZSTD_customFree(void* ptr, ZSTD_customMem customMem) + { + if (ptr != null) + { + if (customMem.customFree != null) + { + throw new NotImplementedException("customMem is not implemented"); + } + else + { + free((ptr)); + } + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompress.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompress.cs new file mode 100644 index 00000000..6480c4b9 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompress.cs @@ -0,0 +1,8895 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*-************************************* + * Helper functions + ***************************************/ + /* ZSTD_compressBound() + * Note that the result from this function is only compatible with the "normal" + * full-block strategy. + * When there are a lot of small blocks due to frequent flush in streaming mode + * the overhead of headers can make the compressed data to be larger than the + * return value of ZSTD_compressBound(). + */ + public static nuint ZSTD_compressBound(nuint srcSize) + { + return ((srcSize) + ((srcSize) >> 8) + (((srcSize) < (uint)((128 << 10))) ? (((uint)((128 << 10)) - (srcSize)) >> 11) : 0)); + } + + public static ZSTD_CCtx_s* ZSTD_createCCtx() + { + return ZSTD_createCCtx_advanced(ZSTD_defaultCMem); + } + + private static void ZSTD_initCCtx(ZSTD_CCtx_s* cctx, ZSTD_customMem memManager) + { + assert(cctx != null); + memset((void*)(cctx), (0), ((nuint)(sizeof(ZSTD_CCtx_s)))); + cctx->customMem = memManager; + cctx->bmi2 = ((IsBmi2Supported) ? 1 : 0); + + { + nuint err = ZSTD_CCtx_reset(cctx, ZSTD_ResetDirective.ZSTD_reset_parameters); + + assert((ERR_isError(err)) == 0); + } + } + + public static ZSTD_CCtx_s* ZSTD_createCCtx_advanced(ZSTD_customMem customMem) + { + if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + { + return (ZSTD_CCtx_s*)null; + } + + + { + ZSTD_CCtx_s* cctx = (ZSTD_CCtx_s*)(ZSTD_customMalloc((nuint)(sizeof(ZSTD_CCtx_s)), customMem)); + + if (cctx == null) + { + return (ZSTD_CCtx_s*)null; + } + + ZSTD_initCCtx(cctx, customMem); + return cctx; + } + } + + /*! ZSTD_initStatic*() : + * Initialize an object using a pre-allocated fixed-size buffer. + * workspace: The memory area to emplace the object into. + * Provided pointer *must be 8-bytes aligned*. + * Buffer must outlive object. + * workspaceSize: Use ZSTD_estimate*Size() to determine + * how large workspace must be to support target scenario. + * @return : pointer to object (same address as workspace, just different type), + * or NULL if error (size too small, incorrect alignment, etc.) + * Note : zstd will never resize nor malloc() when using a static buffer. + * If the object requires more memory than available, + * zstd will just error out (typically ZSTD_error_memory_allocation). + * Note 2 : there is no corresponding "free" function. + * Since workspace is allocated externally, it must be freed externally too. + * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level + * into its associated cParams. + * Limitation 1 : currently not compatible with internal dictionary creation, triggered by + * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict(). + * Limitation 2 : static cctx currently not compatible with multi-threading. + * Limitation 3 : static dctx is incompatible with legacy support. + */ + public static ZSTD_CCtx_s* ZSTD_initStaticCCtx(void* workspace, nuint workspaceSize) + { + ZSTD_cwksp ws; + ZSTD_CCtx_s* cctx; + + if (workspaceSize <= (nuint)(sizeof(ZSTD_CCtx_s))) + { + return (ZSTD_CCtx_s*)null; + } + + if (((nuint)(workspace) & 7) != 0) + { + return (ZSTD_CCtx_s*)null; + } + + ZSTD_cwksp_init(&ws, workspace, workspaceSize, ZSTD_cwksp_static_alloc_e.ZSTD_cwksp_static_alloc); + cctx = (ZSTD_CCtx_s*)(ZSTD_cwksp_reserve_object(&ws, (nuint)(sizeof(ZSTD_CCtx_s)))); + if (cctx == null) + { + return (ZSTD_CCtx_s*)null; + } + + memset((void*)(cctx), (0), ((nuint)(sizeof(ZSTD_CCtx_s)))); + ZSTD_cwksp_move(&cctx->workspace, &ws); + cctx->staticSize = workspaceSize; + if ((ZSTD_cwksp_check_available(&cctx->workspace, ((uint)(((6 << 10) + 256)) + ((nuint)(sizeof(uint)) * (uint)((((35) > (52) ? (35) : (52)) + 2)))) + 2 * (nuint)(sizeof(ZSTD_compressedBlockState_t)))) == 0) + { + return (ZSTD_CCtx_s*)null; + } + + cctx->blockState.prevCBlock = (ZSTD_compressedBlockState_t*)(ZSTD_cwksp_reserve_object(&cctx->workspace, (nuint)(sizeof(ZSTD_compressedBlockState_t)))); + cctx->blockState.nextCBlock = (ZSTD_compressedBlockState_t*)(ZSTD_cwksp_reserve_object(&cctx->workspace, (nuint)(sizeof(ZSTD_compressedBlockState_t)))); + cctx->entropyWorkspace = (uint*)(ZSTD_cwksp_reserve_object(&cctx->workspace, ((uint)(((6 << 10) + 256)) + ((nuint)(sizeof(uint)) * (uint)((((35) > (52) ? (35) : (52)) + 2)))))); + cctx->bmi2 = ((IsBmi2Supported) ? 1 : 0); + return cctx; + } + + /** + * Clears and frees all of the dictionaries in the CCtx. + */ + private static void ZSTD_clearAllDicts(ZSTD_CCtx_s* cctx) + { + ZSTD_customFree(cctx->localDict.dictBuffer, cctx->customMem); + ZSTD_freeCDict(cctx->localDict.cdict); + memset((void*)(&cctx->localDict), (0), ((nuint)(sizeof(ZSTD_localDict)))); + memset((void*)(&cctx->prefixDict), (0), ((nuint)(sizeof(ZSTD_prefixDict_s)))); + cctx->cdict = null; + } + + private static nuint ZSTD_sizeof_localDict(ZSTD_localDict dict) + { + nuint bufferSize = dict.dictBuffer != null ? dict.dictSize : 0; + nuint cdictSize = ZSTD_sizeof_CDict(dict.cdict); + + return bufferSize + cdictSize; + } + + private static void ZSTD_freeCCtxContent(ZSTD_CCtx_s* cctx) + { + assert(cctx != null); + assert(cctx->staticSize == 0); + ZSTD_clearAllDicts(cctx); + ZSTD_cwksp_free(&cctx->workspace, cctx->customMem); + } + + public static nuint ZSTD_freeCCtx(ZSTD_CCtx_s* cctx) + { + if (cctx == null) + { + return 0; + } + + if (cctx->staticSize != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + + { + int cctxInWorkspace = ZSTD_cwksp_owns_buffer(&cctx->workspace, (void*)cctx); + + ZSTD_freeCCtxContent(cctx); + if (cctxInWorkspace == 0) + { + ZSTD_customFree((void*)cctx, cctx->customMem); + } + } + + return 0; + } + + private static nuint ZSTD_sizeof_mtctx(ZSTD_CCtx_s* cctx) + { + return 0; + } + + /*! ZSTD_sizeof_*() : Requires v1.4.0+ + * These functions give the _current_ memory usage of selected object. + * Note that object memory usage can evolve (increase or decrease) over time. */ + public static nuint ZSTD_sizeof_CCtx(ZSTD_CCtx_s* cctx) + { + if (cctx == null) + { + return 0; + } + + return (cctx->workspace.workspace == cctx ? 0 : (nuint)(sizeof(ZSTD_CCtx_s))) + ZSTD_cwksp_sizeof(&cctx->workspace) + ZSTD_sizeof_localDict(cctx->localDict) + ZSTD_sizeof_mtctx(cctx); + } + + public static nuint ZSTD_sizeof_CStream(ZSTD_CCtx_s* zcs) + { + return ZSTD_sizeof_CCtx(zcs); + } + + /* private API call, for dictBuilder only */ + public static seqStore_t* ZSTD_getSeqStore(ZSTD_CCtx_s* ctx) + { + return &(ctx->seqStore); + } + + /* Returns true if the strategy supports using a row based matchfinder */ + private static int ZSTD_rowMatchFinderSupported(ZSTD_strategy strategy) + { + return ((strategy >= ZSTD_strategy.ZSTD_greedy && strategy <= ZSTD_strategy.ZSTD_lazy2) ? 1 : 0); + } + + /* Returns true if the strategy and useRowMatchFinder mode indicate that we will use the row based matchfinder + * for this compression. + */ + private static int ZSTD_rowMatchFinderUsed(ZSTD_strategy strategy, ZSTD_useRowMatchFinderMode_e mode) + { + assert(mode != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + return (((ZSTD_rowMatchFinderSupported(strategy)) != 0 && (mode == ZSTD_useRowMatchFinderMode_e.ZSTD_urm_enableRowMatchFinder)) ? 1 : 0); + } + + /* Returns row matchfinder usage enum given an initial mode and cParams */ + private static ZSTD_useRowMatchFinderMode_e ZSTD_resolveRowMatchFinderMode(ZSTD_useRowMatchFinderMode_e mode, ZSTD_compressionParameters* cParams) + { + int kHasSIMD128 = 1; + + if (mode != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto) + { + return mode; + } + + mode = ZSTD_useRowMatchFinderMode_e.ZSTD_urm_disableRowMatchFinder; + if ((ZSTD_rowMatchFinderSupported(cParams->strategy)) == 0) + { + return mode; + } + + if (kHasSIMD128 != 0) + { + if (cParams->windowLog > 14) + { + mode = ZSTD_useRowMatchFinderMode_e.ZSTD_urm_enableRowMatchFinder; + } + } + else + { + if (cParams->windowLog > 17) + { + mode = ZSTD_useRowMatchFinderMode_e.ZSTD_urm_enableRowMatchFinder; + } + } + + return mode; + } + + /* Returns 1 if the arguments indicate that we should allocate a chainTable, 0 otherwise */ + private static int ZSTD_allocateChainTable(ZSTD_strategy strategy, ZSTD_useRowMatchFinderMode_e useRowMatchFinder, uint forDDSDict) + { + assert(useRowMatchFinder != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + return ((forDDSDict != 0 || ((strategy != ZSTD_strategy.ZSTD_fast) && (ZSTD_rowMatchFinderUsed(strategy, useRowMatchFinder)) == 0)) ? 1 : 0); + } + + /* Returns 1 if compression parameters are such that we should + * enable long distance matching (wlog >= 27, strategy >= btopt). + * Returns 0 otherwise. + */ + private static uint ZSTD_CParams_shouldEnableLdm(ZSTD_compressionParameters* cParams) + { + return ((cParams->strategy >= ZSTD_strategy.ZSTD_btopt && cParams->windowLog >= 27) ? 1U : 0U); + } + + /* Returns 1 if compression parameters are such that we should + * enable blockSplitter (wlog >= 17, strategy >= btopt). + * Returns 0 otherwise. + */ + private static uint ZSTD_CParams_useBlockSplitter(ZSTD_compressionParameters* cParams) + { + return ((cParams->strategy >= ZSTD_strategy.ZSTD_btopt && cParams->windowLog >= 17) ? 1U : 0U); + } + + private static ZSTD_CCtx_params_s ZSTD_makeCCtxParamsFromCParams(ZSTD_compressionParameters cParams) + { + ZSTD_CCtx_params_s cctxParams; + + ZSTD_CCtxParams_init(&cctxParams, 3); + cctxParams.cParams = cParams; + if ((ZSTD_CParams_shouldEnableLdm(&cParams)) != 0) + { + cctxParams.ldmParams.enableLdm = 1; + ZSTD_ldm_adjustParameters(&cctxParams.ldmParams, &cParams); + assert(cctxParams.ldmParams.hashLog >= cctxParams.ldmParams.bucketSizeLog); + assert(cctxParams.ldmParams.hashRateLog < 32); + } + + if ((ZSTD_CParams_useBlockSplitter(&cParams)) != 0) + { + cctxParams.splitBlocks = 1; + } + + cctxParams.useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams.useRowMatchFinder, &cParams); + assert((ZSTD_checkCParams(cParams)) == 0); + return cctxParams; + } + + private static ZSTD_CCtx_params_s* ZSTD_createCCtxParams_advanced(ZSTD_customMem customMem) + { + ZSTD_CCtx_params_s* @params; + + if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + { + return (ZSTD_CCtx_params_s*)null; + } + + @params = (ZSTD_CCtx_params_s*)(ZSTD_customCalloc((nuint)(sizeof(ZSTD_CCtx_params_s)), customMem)); + if (@params == null) + { + return (ZSTD_CCtx_params_s*)null; + } + + ZSTD_CCtxParams_init(@params, 3); + @params->customMem = customMem; + return @params; + } + + /*! ZSTD_CCtx_params : + * Quick howto : + * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure + * - ZSTD_CCtxParams_setParameter() : Push parameters one by one into + * an existing ZSTD_CCtx_params structure. + * This is similar to + * ZSTD_CCtx_setParameter(). + * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to + * an existing CCtx. + * These parameters will be applied to + * all subsequent frames. + * - ZSTD_compressStream2() : Do compression using the CCtx. + * - ZSTD_freeCCtxParams() : Free the memory, accept NULL pointer. + * + * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams() + * for static allocation of CCtx for single-threaded compression. + */ + public static ZSTD_CCtx_params_s* ZSTD_createCCtxParams() + { + return ZSTD_createCCtxParams_advanced(ZSTD_defaultCMem); + } + + public static nuint ZSTD_freeCCtxParams(ZSTD_CCtx_params_s* @params) + { + if (@params == null) + { + return 0; + } + + ZSTD_customFree((void*)@params, @params->customMem); + return 0; + } + + /*! ZSTD_CCtxParams_reset() : + * Reset params to default values. + */ + public static nuint ZSTD_CCtxParams_reset(ZSTD_CCtx_params_s* @params) + { + return ZSTD_CCtxParams_init(@params, 3); + } + + /*! ZSTD_CCtxParams_init() : + * Initializes the compression parameters of cctxParams according to + * compression level. All other parameters are reset to their default values. + */ + public static nuint ZSTD_CCtxParams_init(ZSTD_CCtx_params_s* cctxParams, int compressionLevel) + { + if (cctxParams == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + memset((void*)(cctxParams), (0), ((nuint)(sizeof(ZSTD_CCtx_params_s)))); + cctxParams->compressionLevel = compressionLevel; + cctxParams->fParams.contentSizeFlag = 1; + return 0; + } + + /** + * Initializes the cctxParams from params and compressionLevel. + * @param compressionLevel If params are derived from a compression level then that compression level, otherwise ZSTD_NO_CLEVEL. + */ + private static void ZSTD_CCtxParams_init_internal(ZSTD_CCtx_params_s* cctxParams, ZSTD_parameters* @params, int compressionLevel) + { + assert((ZSTD_checkCParams(@params->cParams)) == 0); + memset((void*)(cctxParams), (0), ((nuint)(sizeof(ZSTD_CCtx_params_s)))); + cctxParams->cParams = @params->cParams; + cctxParams->fParams = @params->fParams; + cctxParams->compressionLevel = compressionLevel; + cctxParams->useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams->useRowMatchFinder, &@params->cParams); + } + + /*! ZSTD_CCtxParams_init_advanced() : + * Initializes the compression and frame parameters of cctxParams according to + * params. All other parameters are reset to their default values. + */ + public static nuint ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params_s* cctxParams, ZSTD_parameters @params) + { + if (cctxParams == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + + { + nuint err_code = (ZSTD_checkCParams(@params.cParams)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ZSTD_CCtxParams_init_internal(cctxParams, &@params, 0); + return 0; + } + + /** + * Sets cctxParams' cParams and fParams from params, but otherwise leaves them alone. + * @param param Validated zstd parameters. + */ + private static void ZSTD_CCtxParams_setZstdParams(ZSTD_CCtx_params_s* cctxParams, ZSTD_parameters* @params) + { + assert((ZSTD_checkCParams(@params->cParams)) == 0); + cctxParams->cParams = @params->cParams; + cctxParams->fParams = @params->fParams; + cctxParams->compressionLevel = 0; + } + + /*! ZSTD_cParam_getBounds() : + * All parameters must belong to an interval with lower and upper bounds, + * otherwise they will either trigger an error or be automatically clamped. + * @return : a structure, ZSTD_bounds, which contains + * - an error status field, which must be tested using ZSTD_isError() + * - lower and upper bounds, both inclusive + */ + public static ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter param) + { + ZSTD_bounds bounds = new ZSTD_bounds + { + error = 0, + lowerBound = 0, + upperBound = 0, + }; + + switch (param) + { + case ZSTD_cParameter.ZSTD_c_compressionLevel: + { + bounds.lowerBound = ZSTD_minCLevel(); + } + + bounds.upperBound = ZSTD_maxCLevel(); + return bounds; + case ZSTD_cParameter.ZSTD_c_windowLog: + { + bounds.lowerBound = 10; + } + + bounds.upperBound = ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)); + return bounds; + case ZSTD_cParameter.ZSTD_c_hashLog: + { + bounds.lowerBound = 6; + } + + bounds.upperBound = ((((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) < 30) ? ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) : 30); + return bounds; + case ZSTD_cParameter.ZSTD_c_chainLog: + { + bounds.lowerBound = 6; + } + + bounds.upperBound = ((int)((nuint)(sizeof(nuint)) == 4 ? 29 : 30)); + return bounds; + case ZSTD_cParameter.ZSTD_c_searchLog: + { + bounds.lowerBound = 1; + } + + bounds.upperBound = (((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) - 1); + return bounds; + case ZSTD_cParameter.ZSTD_c_minMatch: + { + bounds.lowerBound = 3; + } + + bounds.upperBound = 7; + return bounds; + case ZSTD_cParameter.ZSTD_c_targetLength: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = (1 << 17); + return bounds; + case ZSTD_cParameter.ZSTD_c_strategy: + { + bounds.lowerBound = (int)ZSTD_strategy.ZSTD_fast; + } + + bounds.upperBound = (int)ZSTD_strategy.ZSTD_btultra2; + return bounds; + case ZSTD_cParameter.ZSTD_c_contentSizeFlag: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_checksumFlag: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_dictIDFlag: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_nbWorkers: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 0; + return bounds; + case ZSTD_cParameter.ZSTD_c_jobSize: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 0; + return bounds; + case ZSTD_cParameter.ZSTD_c_overlapLog: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 0; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam8: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_enableLongDistanceMatching: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_ldmHashLog: + { + bounds.lowerBound = 6; + } + + bounds.upperBound = ((((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) < 30) ? ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) : 30); + return bounds; + case ZSTD_cParameter.ZSTD_c_ldmMinMatch: + { + bounds.lowerBound = 4; + } + + bounds.upperBound = 4096; + return bounds; + case ZSTD_cParameter.ZSTD_c_ldmBucketSizeLog: + { + bounds.lowerBound = 1; + } + + bounds.upperBound = 8; + return bounds; + case ZSTD_cParameter.ZSTD_c_ldmHashRateLog: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = (((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) - 6); + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam1: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam3: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam2: + { + + } + + bounds.lowerBound = (int)ZSTD_format_e.ZSTD_f_zstd1; + bounds.upperBound = (int)ZSTD_format_e.ZSTD_f_zstd1_magicless; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam4: + { + + } + + bounds.lowerBound = (int)ZSTD_dictAttachPref_e.ZSTD_dictDefaultAttach; + bounds.upperBound = (int)ZSTD_dictAttachPref_e.ZSTD_dictForceLoad; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam5: + { + + } + + bounds.lowerBound = (int)ZSTD_literalCompressionMode_e.ZSTD_lcm_auto; + bounds.upperBound = (int)ZSTD_literalCompressionMode_e.ZSTD_lcm_uncompressed; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam6: + { + bounds.lowerBound = 64; + } + + bounds.upperBound = (1 << 17); + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam7: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 2147483647; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam9: + case ZSTD_cParameter.ZSTD_c_experimentalParam10: + { + bounds.lowerBound = (int)(ZSTD_bufferMode_e.ZSTD_bm_buffered); + } + + bounds.upperBound = (int)(ZSTD_bufferMode_e.ZSTD_bm_stable); + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam11: + { + bounds.lowerBound = (int)(ZSTD_sequenceFormat_e.ZSTD_sf_noBlockDelimiters); + } + + bounds.upperBound = (int)(ZSTD_sequenceFormat_e.ZSTD_sf_explicitBlockDelimiters); + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam12: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam13: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam14: + { + bounds.lowerBound = (int)(ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + } + + bounds.upperBound = (int)(ZSTD_useRowMatchFinderMode_e.ZSTD_urm_enableRowMatchFinder); + return bounds; + case ZSTD_cParameter.ZSTD_c_experimentalParam15: + { + bounds.lowerBound = 0; + } + + bounds.upperBound = 1; + return bounds; + default: + { + bounds.error = (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + + return bounds; + } + } + + /* ZSTD_cParam_clampBounds: + * Clamps the value into the bounded range. + */ + private static nuint ZSTD_cParam_clampBounds(ZSTD_cParameter cParam, int* value) + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(cParam); + + if ((ERR_isError(bounds.error)) != 0) + { + return bounds.error; + } + + if (*value < bounds.lowerBound) + { + *value = bounds.lowerBound; + } + + if (*value > bounds.upperBound) + { + *value = bounds.upperBound; + } + + return 0; + } + + private static int ZSTD_isUpdateAuthorized(ZSTD_cParameter param) + { + switch (param) + { + case ZSTD_cParameter.ZSTD_c_compressionLevel: + case ZSTD_cParameter.ZSTD_c_hashLog: + case ZSTD_cParameter.ZSTD_c_chainLog: + case ZSTD_cParameter.ZSTD_c_searchLog: + case ZSTD_cParameter.ZSTD_c_minMatch: + case ZSTD_cParameter.ZSTD_c_targetLength: + case ZSTD_cParameter.ZSTD_c_strategy: + { + return 1; + } + + case ZSTD_cParameter.ZSTD_c_experimentalParam2: + case ZSTD_cParameter.ZSTD_c_windowLog: + case ZSTD_cParameter.ZSTD_c_contentSizeFlag: + case ZSTD_cParameter.ZSTD_c_checksumFlag: + case ZSTD_cParameter.ZSTD_c_dictIDFlag: + case ZSTD_cParameter.ZSTD_c_experimentalParam3: + case ZSTD_cParameter.ZSTD_c_nbWorkers: + case ZSTD_cParameter.ZSTD_c_jobSize: + case ZSTD_cParameter.ZSTD_c_overlapLog: + case ZSTD_cParameter.ZSTD_c_experimentalParam1: + case ZSTD_cParameter.ZSTD_c_experimentalParam8: + case ZSTD_cParameter.ZSTD_c_enableLongDistanceMatching: + case ZSTD_cParameter.ZSTD_c_ldmHashLog: + case ZSTD_cParameter.ZSTD_c_ldmMinMatch: + case ZSTD_cParameter.ZSTD_c_ldmBucketSizeLog: + case ZSTD_cParameter.ZSTD_c_ldmHashRateLog: + case ZSTD_cParameter.ZSTD_c_experimentalParam4: + case ZSTD_cParameter.ZSTD_c_experimentalParam5: + case ZSTD_cParameter.ZSTD_c_experimentalParam6: + case ZSTD_cParameter.ZSTD_c_experimentalParam7: + case ZSTD_cParameter.ZSTD_c_experimentalParam9: + case ZSTD_cParameter.ZSTD_c_experimentalParam10: + case ZSTD_cParameter.ZSTD_c_experimentalParam11: + case ZSTD_cParameter.ZSTD_c_experimentalParam12: + case ZSTD_cParameter.ZSTD_c_experimentalParam13: + case ZSTD_cParameter.ZSTD_c_experimentalParam14: + case ZSTD_cParameter.ZSTD_c_experimentalParam15: + default: + { + return 0; + } + } + } + + /*! ZSTD_CCtx_setParameter() : + * Set one compression parameter, selected by enum ZSTD_cParameter. + * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds(). + * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). + * Setting a parameter is generally only possible during frame initialization (before starting compression). + * Exception : when using multi-threading mode (nbWorkers >= 1), + * the following parameters can be updated _during_ compression (within same frame): + * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy. + * new parameters will be active for next job only (after a flush()). + * @return : an error code (which can be tested using ZSTD_isError()). + */ + public static nuint ZSTD_CCtx_setParameter(ZSTD_CCtx_s* cctx, ZSTD_cParameter param, int value) + { + if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) + { + if ((ZSTD_isUpdateAuthorized(param)) != 0) + { + cctx->cParamsChanged = 1; + } + else + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + } + } + + switch (param) + { + case ZSTD_cParameter.ZSTD_c_nbWorkers: + { + if ((value != 0) && cctx->staticSize != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + break; + case ZSTD_cParameter.ZSTD_c_compressionLevel: + case ZSTD_cParameter.ZSTD_c_windowLog: + case ZSTD_cParameter.ZSTD_c_hashLog: + case ZSTD_cParameter.ZSTD_c_chainLog: + case ZSTD_cParameter.ZSTD_c_searchLog: + case ZSTD_cParameter.ZSTD_c_minMatch: + case ZSTD_cParameter.ZSTD_c_targetLength: + case ZSTD_cParameter.ZSTD_c_strategy: + case ZSTD_cParameter.ZSTD_c_ldmHashRateLog: + case ZSTD_cParameter.ZSTD_c_experimentalParam2: + case ZSTD_cParameter.ZSTD_c_contentSizeFlag: + case ZSTD_cParameter.ZSTD_c_checksumFlag: + case ZSTD_cParameter.ZSTD_c_dictIDFlag: + case ZSTD_cParameter.ZSTD_c_experimentalParam3: + case ZSTD_cParameter.ZSTD_c_experimentalParam4: + case ZSTD_cParameter.ZSTD_c_experimentalParam5: + case ZSTD_cParameter.ZSTD_c_jobSize: + case ZSTD_cParameter.ZSTD_c_overlapLog: + case ZSTD_cParameter.ZSTD_c_experimentalParam1: + case ZSTD_cParameter.ZSTD_c_experimentalParam8: + case ZSTD_cParameter.ZSTD_c_enableLongDistanceMatching: + case ZSTD_cParameter.ZSTD_c_ldmHashLog: + case ZSTD_cParameter.ZSTD_c_ldmMinMatch: + case ZSTD_cParameter.ZSTD_c_ldmBucketSizeLog: + case ZSTD_cParameter.ZSTD_c_experimentalParam6: + case ZSTD_cParameter.ZSTD_c_experimentalParam7: + case ZSTD_cParameter.ZSTD_c_experimentalParam9: + case ZSTD_cParameter.ZSTD_c_experimentalParam10: + case ZSTD_cParameter.ZSTD_c_experimentalParam11: + case ZSTD_cParameter.ZSTD_c_experimentalParam12: + case ZSTD_cParameter.ZSTD_c_experimentalParam13: + case ZSTD_cParameter.ZSTD_c_experimentalParam14: + case ZSTD_cParameter.ZSTD_c_experimentalParam15: + { + break; + } + + default: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + } + + return ZSTD_CCtxParams_setParameter(&cctx->requestedParams, param, value); + } + + /*! ZSTD_CCtxParams_setParameter() : Requires v1.4.0+ + * Similar to ZSTD_CCtx_setParameter. + * Set one compression parameter, selected by enum ZSTD_cParameter. + * Parameters must be applied to a ZSTD_CCtx using + * ZSTD_CCtx_setParametersUsingCCtxParams(). + * @result : a code representing success or failure (which can be tested with + * ZSTD_isError()). + */ + public static nuint ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params_s* CCtxParams, ZSTD_cParameter param, int value) + { + switch (param) + { + case ZSTD_cParameter.ZSTD_c_experimentalParam2: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam2, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->format = (ZSTD_format_e)(value); + return (nuint)(CCtxParams->format); + case ZSTD_cParameter.ZSTD_c_compressionLevel: + { + + { + nuint err_code = (ZSTD_cParam_clampBounds(param, &value)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (value == 0) + { + CCtxParams->compressionLevel = 3; + } + else + { + CCtxParams->compressionLevel = value; + } + + if (CCtxParams->compressionLevel >= 0) + { + return (nuint)(CCtxParams->compressionLevel); + } + + return 0; + } + + case ZSTD_cParameter.ZSTD_c_windowLog: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_windowLog, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->cParams.windowLog = (uint)(value); + return CCtxParams->cParams.windowLog; + case ZSTD_cParameter.ZSTD_c_hashLog: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_hashLog, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->cParams.hashLog = (uint)(value); + return CCtxParams->cParams.hashLog; + case ZSTD_cParameter.ZSTD_c_chainLog: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_chainLog, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->cParams.chainLog = (uint)(value); + return CCtxParams->cParams.chainLog; + case ZSTD_cParameter.ZSTD_c_searchLog: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_searchLog, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->cParams.searchLog = (uint)(value); + return (nuint)(value); + case ZSTD_cParameter.ZSTD_c_minMatch: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_minMatch, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->cParams.minMatch = (uint)value; + return CCtxParams->cParams.minMatch; + case ZSTD_cParameter.ZSTD_c_targetLength: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_targetLength, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->cParams.targetLength = (uint)value; + return CCtxParams->cParams.targetLength; + case ZSTD_cParameter.ZSTD_c_strategy: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_strategy, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->cParams.strategy = (ZSTD_strategy)(value); + return (nuint)(CCtxParams->cParams.strategy); + case ZSTD_cParameter.ZSTD_c_contentSizeFlag: + ; + CCtxParams->fParams.contentSizeFlag = ((value != 0) ? 1 : 0); + return (nuint)CCtxParams->fParams.contentSizeFlag; + case ZSTD_cParameter.ZSTD_c_checksumFlag: + { + CCtxParams->fParams.checksumFlag = ((value != 0) ? 1 : 0); + } + + return (nuint)CCtxParams->fParams.checksumFlag; + case ZSTD_cParameter.ZSTD_c_dictIDFlag: + ; + CCtxParams->fParams.noDictIDFlag = (value == 0 ? 1 : 0); + return (CCtxParams->fParams.noDictIDFlag == 0 ? 1U : 0U); + case ZSTD_cParameter.ZSTD_c_experimentalParam3: + { + CCtxParams->forceWindow = ((value != 0) ? 1 : 0); + } + + return (nuint)CCtxParams->forceWindow; + case ZSTD_cParameter.ZSTD_c_experimentalParam4: + { + ZSTD_dictAttachPref_e pref = (ZSTD_dictAttachPref_e)(value); + + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam4, (int)pref)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->attachDictPref = pref; + return (nuint)CCtxParams->attachDictPref; + } + + case ZSTD_cParameter.ZSTD_c_experimentalParam5: + { + ZSTD_literalCompressionMode_e lcm = (ZSTD_literalCompressionMode_e)(value); + + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam5, (int)lcm)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->literalCompressionMode = lcm; + return (nuint)CCtxParams->literalCompressionMode; + } + + case ZSTD_cParameter.ZSTD_c_nbWorkers: + { + if (value != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + return 0; + case ZSTD_cParameter.ZSTD_c_jobSize: + { + if (value != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + return 0; + case ZSTD_cParameter.ZSTD_c_overlapLog: + { + if (value != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + return 0; + case ZSTD_cParameter.ZSTD_c_experimentalParam1: + { + if (value != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + return 0; + case ZSTD_cParameter.ZSTD_c_experimentalParam8: + { + CCtxParams->enableDedicatedDictSearch = ((value != 0) ? 1 : 0); + } + + return (nuint)CCtxParams->enableDedicatedDictSearch; + case ZSTD_cParameter.ZSTD_c_enableLongDistanceMatching: + { + CCtxParams->ldmParams.enableLdm = (((value != 0)) ? 1U : 0U); + } + + return CCtxParams->ldmParams.enableLdm; + case ZSTD_cParameter.ZSTD_c_ldmHashLog: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_ldmHashLog, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->ldmParams.hashLog = (uint)value; + return CCtxParams->ldmParams.hashLog; + case ZSTD_cParameter.ZSTD_c_ldmMinMatch: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_ldmMinMatch, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->ldmParams.minMatchLength = (uint)value; + return CCtxParams->ldmParams.minMatchLength; + case ZSTD_cParameter.ZSTD_c_ldmBucketSizeLog: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_ldmBucketSizeLog, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->ldmParams.bucketSizeLog = (uint)value; + return CCtxParams->ldmParams.bucketSizeLog; + case ZSTD_cParameter.ZSTD_c_ldmHashRateLog: + { + if (value > ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) - 6) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + } + + CCtxParams->ldmParams.hashRateLog = (uint)value; + return CCtxParams->ldmParams.hashRateLog; + case ZSTD_cParameter.ZSTD_c_experimentalParam6: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam6, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->targetCBlockSize = (nuint)value; + return CCtxParams->targetCBlockSize; + case ZSTD_cParameter.ZSTD_c_experimentalParam7: + { + if (value != 0) + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam7, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + } + + CCtxParams->srcSizeHint = value; + return (nuint)CCtxParams->srcSizeHint; + case ZSTD_cParameter.ZSTD_c_experimentalParam9: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam9, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->inBufferMode = (ZSTD_bufferMode_e)(value); + return (nuint)CCtxParams->inBufferMode; + case ZSTD_cParameter.ZSTD_c_experimentalParam10: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam10, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->outBufferMode = (ZSTD_bufferMode_e)(value); + return (nuint)CCtxParams->outBufferMode; + case ZSTD_cParameter.ZSTD_c_experimentalParam11: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam11, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->blockDelimiters = (ZSTD_sequenceFormat_e)(value); + return (nuint)CCtxParams->blockDelimiters; + case ZSTD_cParameter.ZSTD_c_experimentalParam12: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam12, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->validateSequences = value; + return (nuint)CCtxParams->validateSequences; + case ZSTD_cParameter.ZSTD_c_experimentalParam13: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam13, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->splitBlocks = value; + return (nuint)CCtxParams->splitBlocks; + case ZSTD_cParameter.ZSTD_c_experimentalParam14: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam14, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->useRowMatchFinder = (ZSTD_useRowMatchFinderMode_e)(value); + return (nuint)CCtxParams->useRowMatchFinder; + case ZSTD_cParameter.ZSTD_c_experimentalParam15: + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam15, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + CCtxParams->deterministicRefPrefix = (!(value == 0) ? 1 : 0); + return (nuint)CCtxParams->deterministicRefPrefix; + default: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + } + } + + /*! ZSTD_CCtx_getParameter() : + * Get the requested compression parameter value, selected by enum ZSTD_cParameter, + * and store it into int* value. + * @return : 0, or an error code (which can be tested with ZSTD_isError()). + */ + public static nuint ZSTD_CCtx_getParameter(ZSTD_CCtx_s* cctx, ZSTD_cParameter param, int* value) + { + return ZSTD_CCtxParams_getParameter(&cctx->requestedParams, param, value); + } + + /*! ZSTD_CCtxParams_getParameter() : + * Similar to ZSTD_CCtx_getParameter. + * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter. + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + */ + public static nuint ZSTD_CCtxParams_getParameter(ZSTD_CCtx_params_s* CCtxParams, ZSTD_cParameter param, int* value) + { + switch (param) + { + case ZSTD_cParameter.ZSTD_c_experimentalParam2: + { + *value = (int)CCtxParams->format; + } + + break; + case ZSTD_cParameter.ZSTD_c_compressionLevel: + { + *value = CCtxParams->compressionLevel; + } + + break; + case ZSTD_cParameter.ZSTD_c_windowLog: + { + *value = (int)(CCtxParams->cParams.windowLog); + } + + break; + case ZSTD_cParameter.ZSTD_c_hashLog: + { + *value = (int)(CCtxParams->cParams.hashLog); + } + + break; + case ZSTD_cParameter.ZSTD_c_chainLog: + { + *value = (int)(CCtxParams->cParams.chainLog); + } + + break; + case ZSTD_cParameter.ZSTD_c_searchLog: + { + *value = (int)CCtxParams->cParams.searchLog; + } + + break; + case ZSTD_cParameter.ZSTD_c_minMatch: + { + *value = (int)CCtxParams->cParams.minMatch; + } + + break; + case ZSTD_cParameter.ZSTD_c_targetLength: + { + *value = (int)CCtxParams->cParams.targetLength; + } + + break; + case ZSTD_cParameter.ZSTD_c_strategy: + { + *value = (int)(uint)(CCtxParams->cParams.strategy); + } + + break; + case ZSTD_cParameter.ZSTD_c_contentSizeFlag: + { + *value = CCtxParams->fParams.contentSizeFlag; + } + + break; + case ZSTD_cParameter.ZSTD_c_checksumFlag: + { + *value = CCtxParams->fParams.checksumFlag; + } + + break; + case ZSTD_cParameter.ZSTD_c_dictIDFlag: + { + *value = (CCtxParams->fParams.noDictIDFlag == 0 ? 1 : 0); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam3: + { + *value = CCtxParams->forceWindow; + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam4: + { + *value = (int)CCtxParams->attachDictPref; + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam5: + { + *value = (int)CCtxParams->literalCompressionMode; + } + + break; + case ZSTD_cParameter.ZSTD_c_nbWorkers: + { + assert(CCtxParams->nbWorkers == 0); + } + + *value = CCtxParams->nbWorkers; + break; + case ZSTD_cParameter.ZSTD_c_jobSize: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + case ZSTD_cParameter.ZSTD_c_overlapLog: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + case ZSTD_cParameter.ZSTD_c_experimentalParam1: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + case ZSTD_cParameter.ZSTD_c_experimentalParam8: + { + *value = CCtxParams->enableDedicatedDictSearch; + } + + break; + case ZSTD_cParameter.ZSTD_c_enableLongDistanceMatching: + { + *value = (int)CCtxParams->ldmParams.enableLdm; + } + + break; + case ZSTD_cParameter.ZSTD_c_ldmHashLog: + { + *value = (int)CCtxParams->ldmParams.hashLog; + } + + break; + case ZSTD_cParameter.ZSTD_c_ldmMinMatch: + { + *value = (int)CCtxParams->ldmParams.minMatchLength; + } + + break; + case ZSTD_cParameter.ZSTD_c_ldmBucketSizeLog: + { + *value = (int)CCtxParams->ldmParams.bucketSizeLog; + } + + break; + case ZSTD_cParameter.ZSTD_c_ldmHashRateLog: + { + *value = (int)CCtxParams->ldmParams.hashRateLog; + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam6: + { + *value = (int)(CCtxParams->targetCBlockSize); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam7: + { + *value = (int)(CCtxParams->srcSizeHint); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam9: + { + *value = (int)(CCtxParams->inBufferMode); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam10: + { + *value = (int)(CCtxParams->outBufferMode); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam11: + { + *value = (int)(CCtxParams->blockDelimiters); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam12: + { + *value = (int)(CCtxParams->validateSequences); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam13: + { + *value = (int)(CCtxParams->splitBlocks); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam14: + { + *value = (int)(CCtxParams->useRowMatchFinder); + } + + break; + case ZSTD_cParameter.ZSTD_c_experimentalParam15: + { + *value = (int)(CCtxParams->deterministicRefPrefix); + } + + break; + default: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + } + + } + + return 0; + } + + /** ZSTD_CCtx_setParametersUsingCCtxParams() : + * just applies `params` into `cctx` + * no action is performed, parameters are merely stored. + * If ZSTDMT is enabled, parameters are pushed to cctx->mtctx. + * This is possible even if a compression is ongoing. + * In which case, new parameters will be applied on the fly, starting with next compression job. + */ + public static nuint ZSTD_CCtx_setParametersUsingCCtxParams(ZSTD_CCtx_s* cctx, ZSTD_CCtx_params_s* @params) + { + if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + if (cctx->cdict != null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + cctx->requestedParams = *@params; + return 0; + } + + /*! ZSTD_CCtx_setPledgedSrcSize() : + * Total input data size to be compressed as a single frame. + * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag. + * This value will also be controlled at end of frame, and trigger an error if not respected. + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame. + * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN. + * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame. + * Note 2 : pledgedSrcSize is only valid once, for the next frame. + * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN. + * Note 3 : Whenever all input data is provided and consumed in a single round, + * for example with ZSTD_compress2(), + * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end), + * this value is automatically overridden by srcSize instead. + */ + public static nuint ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx_s* cctx, ulong pledgedSrcSize) + { + if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + cctx->pledgedSrcSizePlusOne = pledgedSrcSize + 1; + return 0; + } + + /** + * Initializes the local dict using the requested parameters. + * NOTE: This does not use the pledged src size, because it may be used for more + * than one compression. + */ + private static nuint ZSTD_initLocalDict(ZSTD_CCtx_s* cctx) + { + ZSTD_localDict* dl = &cctx->localDict; + + if (dl->dict == null) + { + assert(dl->dictBuffer == null); + assert(dl->cdict == null); + assert(dl->dictSize == 0); + return 0; + } + + if (dl->cdict != null) + { + assert(cctx->cdict == dl->cdict); + return 0; + } + + assert(dl->dictSize > 0); + assert(cctx->cdict == null); + assert(cctx->prefixDict.dict == null); + dl->cdict = ZSTD_createCDict_advanced2(dl->dict, dl->dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, dl->dictContentType, &cctx->requestedParams, cctx->customMem); + if (dl->cdict == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + cctx->cdict = dl->cdict; + return 0; + } + + /*! ZSTD_CCtx_loadDictionary_advanced() : + * Same as ZSTD_CCtx_loadDictionary(), but gives finer control over + * how to load the dictionary (by copy ? by reference ?) + * and how to interpret it (automatic ? force raw mode ? full mode only ?) */ + public static nuint ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType) + { + if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + ZSTD_clearAllDicts(cctx); + if (dict == null || dictSize == 0) + { + return 0; + } + + if (dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef) + { + cctx->localDict.dict = dict; + } + else + { + void* dictBuffer; + + if (cctx->staticSize != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + dictBuffer = ZSTD_customMalloc(dictSize, cctx->customMem); + if (dictBuffer == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + memcpy((dictBuffer), (dict), (dictSize)); + cctx->localDict.dictBuffer = dictBuffer; + cctx->localDict.dict = dictBuffer; + } + + cctx->localDict.dictSize = dictSize; + cctx->localDict.dictContentType = dictContentType; + return 0; + } + + /*! ZSTD_CCtx_loadDictionary_byReference() : + * Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx. + * It saves some memory, but also requires that `dict` outlives its usage within `cctx` */ + public static nuint ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize) + { + return ZSTD_CCtx_loadDictionary_advanced(cctx, dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, ZSTD_dictContentType_e.ZSTD_dct_auto); + } + + /*! ZSTD_CCtx_loadDictionary() : Requires v1.4.0+ + * Create an internal CDict from `dict` buffer. + * Decompression will have to use same dictionary. + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + * Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary, + * meaning "return to no-dictionary mode". + * Note 1 : Dictionary is sticky, it will be used for all future compressed frames. + * To return to "no-dictionary" situation, load a NULL dictionary (or reset parameters). + * Note 2 : Loading a dictionary involves building tables. + * It's also a CPU consuming operation, with non-negligible impact on latency. + * Tables are dependent on compression parameters, and for this reason, + * compression parameters can no longer be changed after loading a dictionary. + * Note 3 :`dict` content will be copied internally. + * Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead. + * In such a case, dictionary buffer must outlive its users. + * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced() + * to precisely select how dictionary content must be interpreted. */ + public static nuint ZSTD_CCtx_loadDictionary(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize) + { + return ZSTD_CCtx_loadDictionary_advanced(cctx, dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy, ZSTD_dictContentType_e.ZSTD_dct_auto); + } + + /*! ZSTD_CCtx_refCDict() : Requires v1.4.0+ + * Reference a prepared dictionary, to be used for all next compressed frames. + * Note that compression parameters are enforced from within CDict, + * and supersede any compression parameter previously set within CCtx. + * The parameters ignored are labelled as "superseded-by-cdict" in the ZSTD_cParameter enum docs. + * The ignored parameters will be used again if the CCtx is returned to no-dictionary mode. + * The dictionary will remain valid for future compressed frames using same CCtx. + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + * Special : Referencing a NULL CDict means "return to no-dictionary mode". + * Note 1 : Currently, only one dictionary can be managed. + * Referencing a new dictionary effectively "discards" any previous one. + * Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */ + public static nuint ZSTD_CCtx_refCDict(ZSTD_CCtx_s* cctx, ZSTD_CDict_s* cdict) + { + if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + ZSTD_clearAllDicts(cctx); + cctx->cdict = cdict; + return 0; + } + + public static nuint ZSTD_CCtx_refThreadPool(ZSTD_CCtx_s* cctx, void* pool) + { + if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + cctx->pool = pool; + return 0; + } + + /*! ZSTD_CCtx_refPrefix() : Requires v1.4.0+ + * Reference a prefix (single-usage dictionary) for next compressed frame. + * A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end). + * Decompression will need same prefix to properly regenerate data. + * Compressing with a prefix is similar in outcome as performing a diff and compressing it, + * but performs much faster, especially during decompression (compression speed is tunable with compression level). + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary + * Note 1 : Prefix buffer is referenced. It **must** outlive compression. + * Its content must remain unmodified during compression. + * Note 2 : If the intention is to diff some large src data blob with some prior version of itself, + * ensure that the window size is large enough to contain the entire source. + * See ZSTD_c_windowLog. + * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters. + * It's a CPU consuming operation, with non-negligible impact on latency. + * If there is a need to use the same prefix multiple times, consider loadDictionary instead. + * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dct_rawContent). + * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */ + public static nuint ZSTD_CCtx_refPrefix(ZSTD_CCtx_s* cctx, void* prefix, nuint prefixSize) + { + return ZSTD_CCtx_refPrefix_advanced(cctx, prefix, prefixSize, ZSTD_dictContentType_e.ZSTD_dct_rawContent); + } + + /*! ZSTD_CCtx_refPrefix_advanced() : + * Same as ZSTD_CCtx_refPrefix(), but gives finer control over + * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ + public static nuint ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx_s* cctx, void* prefix, nuint prefixSize, ZSTD_dictContentType_e dictContentType) + { + if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + ZSTD_clearAllDicts(cctx); + if (prefix != null && prefixSize > 0) + { + cctx->prefixDict.dict = prefix; + cctx->prefixDict.dictSize = prefixSize; + cctx->prefixDict.dictContentType = dictContentType; + } + + return 0; + } + + /*! ZSTD_CCtx_reset() : + * Also dumps dictionary */ + public static nuint ZSTD_CCtx_reset(ZSTD_CCtx_s* cctx, ZSTD_ResetDirective reset) + { + if ((reset == ZSTD_ResetDirective.ZSTD_reset_session_only) || (reset == ZSTD_ResetDirective.ZSTD_reset_session_and_parameters)) + { + cctx->streamStage = ZSTD_cStreamStage.zcss_init; + cctx->pledgedSrcSizePlusOne = 0; + } + + if ((reset == ZSTD_ResetDirective.ZSTD_reset_parameters) || (reset == ZSTD_ResetDirective.ZSTD_reset_session_and_parameters)) + { + if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + ZSTD_clearAllDicts(cctx); + return ZSTD_CCtxParams_reset(&cctx->requestedParams); + } + + return 0; + } + + /** ZSTD_checkCParams() : + control CParam values remain within authorized range. + @return : 0, or an error code if one value is beyond authorized range */ + public static nuint ZSTD_checkCParams(ZSTD_compressionParameters cParams) + { + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_windowLog, (int)(cParams.windowLog))) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_chainLog, (int)(cParams.chainLog))) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_hashLog, (int)(cParams.hashLog))) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_searchLog, (int)(cParams.searchLog))) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_minMatch, (int)(cParams.minMatch))) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_targetLength, (int)(cParams.targetLength))) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + + { + if ((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_strategy, (int)cParams.strategy)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + return 0; + } + + /** ZSTD_clampCParams() : + * make CParam values within valid range. + * @return : valid CParams */ + private static ZSTD_compressionParameters ZSTD_clampCParams(ZSTD_compressionParameters cParams) + { + + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_cParameter.ZSTD_c_windowLog); + + if ((int)(cParams.windowLog) < bounds.lowerBound) + { + cParams.windowLog = (uint)(bounds.lowerBound); + } + else if ((int)(cParams.windowLog) > bounds.upperBound) + { + cParams.windowLog = (uint)(bounds.upperBound); + } + } + + + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_cParameter.ZSTD_c_chainLog); + + if ((int)(cParams.chainLog) < bounds.lowerBound) + { + cParams.chainLog = (uint)(bounds.lowerBound); + } + else if ((int)(cParams.chainLog) > bounds.upperBound) + { + cParams.chainLog = (uint)(bounds.upperBound); + } + } + + + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_cParameter.ZSTD_c_hashLog); + + if ((int)(cParams.hashLog) < bounds.lowerBound) + { + cParams.hashLog = (uint)(bounds.lowerBound); + } + else if ((int)(cParams.hashLog) > bounds.upperBound) + { + cParams.hashLog = (uint)(bounds.upperBound); + } + } + + + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_cParameter.ZSTD_c_searchLog); + + if ((int)(cParams.searchLog) < bounds.lowerBound) + { + cParams.searchLog = (uint)(bounds.lowerBound); + } + else if ((int)(cParams.searchLog) > bounds.upperBound) + { + cParams.searchLog = (uint)(bounds.upperBound); + } + } + + + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_cParameter.ZSTD_c_minMatch); + + if ((int)(cParams.minMatch) < bounds.lowerBound) + { + cParams.minMatch = (uint)(bounds.lowerBound); + } + else if ((int)(cParams.minMatch) > bounds.upperBound) + { + cParams.minMatch = (uint)(bounds.upperBound); + } + } + + + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_cParameter.ZSTD_c_targetLength); + + if ((int)(cParams.targetLength) < bounds.lowerBound) + { + cParams.targetLength = (uint)(bounds.lowerBound); + } + else if ((int)(cParams.targetLength) > bounds.upperBound) + { + cParams.targetLength = (uint)(bounds.upperBound); + } + } + + + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_cParameter.ZSTD_c_strategy); + + if ((int)(cParams.strategy) < bounds.lowerBound) + { + cParams.strategy = (ZSTD_strategy)(bounds.lowerBound); + } + else if ((int)(cParams.strategy) > bounds.upperBound) + { + cParams.strategy = (ZSTD_strategy)(bounds.upperBound); + } + } + + return cParams; + } + + /** ZSTD_cycleLog() : + * condition for correct operation : hashLog > 1 */ + public static uint ZSTD_cycleLog(uint hashLog, ZSTD_strategy strat) + { + uint btScale = ((((uint)(strat) >= (uint)(ZSTD_strategy.ZSTD_btlazy2))) ? 1U : 0U); + + return hashLog - btScale; + } + + /** ZSTD_dictAndWindowLog() : + * Returns an adjusted window log that is large enough to fit the source and the dictionary. + * The zstd format says that the entire dictionary is valid if one byte of the dictionary + * is within the window. So the hashLog and chainLog should be large enough to reference both + * the dictionary and the window. So we must use this adjusted dictAndWindowLog when downsizing + * the hashLog and windowLog. + * NOTE: srcSize must not be ZSTD_CONTENTSIZE_UNKNOWN. + */ + private static uint ZSTD_dictAndWindowLog(uint windowLog, ulong srcSize, ulong dictSize) + { + ulong maxWindowSize = 1UL << (unchecked((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31))); + + if (dictSize == 0) + { + return windowLog; + } + + assert(windowLog <= (uint)(((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)))); + assert(srcSize != (unchecked(0UL - 1))); + + { + ulong windowSize = 1UL << (int)windowLog; + ulong dictAndWindowSize = dictSize + windowSize; + + if (windowSize >= dictSize + srcSize) + { + return windowLog; + } + else if (dictAndWindowSize >= maxWindowSize) + { + return (uint)(((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31))); + } + else + { + return ZSTD_highbit32((uint)(dictAndWindowSize) - 1) + 1; + } + } + } + + /** ZSTD_adjustCParams_internal() : + * optimize `cPar` for a specified input (`srcSize` and `dictSize`). + * mostly downsize to reduce memory consumption and initialization latency. + * `srcSize` can be ZSTD_CONTENTSIZE_UNKNOWN when not known. + * `mode` is the mode for parameter adjustment. See docs for `ZSTD_cParamMode_e`. + * note : `srcSize==0` means 0! + * condition : cPar is presumed validated (can be checked using ZSTD_checkCParams()). */ + private static ZSTD_compressionParameters ZSTD_adjustCParams_internal(ZSTD_compressionParameters cPar, ulong srcSize, nuint dictSize, ZSTD_cParamMode_e mode) + { + ulong minSrcSize = 513; + ulong maxWindowResize = 1UL << (((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) - 1); + + assert(ZSTD_checkCParams(cPar) == 0); + switch (mode) + { + case ZSTD_cParamMode_e.ZSTD_cpm_unknown: + case ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict: + { + break; + } + + case ZSTD_cParamMode_e.ZSTD_cpm_createCDict: + { + if (dictSize != 0 && srcSize == (unchecked(0UL - 1))) + { + srcSize = minSrcSize; + } + } + + break; + case ZSTD_cParamMode_e.ZSTD_cpm_attachDict: + { + dictSize = 0; + } + + break; + default: + { + assert(0 != 0); + } + + break; + } + + if ((srcSize < maxWindowResize) && (dictSize < maxWindowResize)) + { + uint tSize = (uint)(srcSize + dictSize); + uint hashSizeMin = (uint)(1 << 6); + uint srcLog = (uint)((tSize < hashSizeMin) ? 6 : ZSTD_highbit32(tSize - 1) + 1); + + if (cPar.windowLog > srcLog) + { + cPar.windowLog = srcLog; + } + } + + if (srcSize != (unchecked(0UL - 1))) + { + uint dictAndWindowLog = ZSTD_dictAndWindowLog(cPar.windowLog, (ulong)(srcSize), (ulong)(dictSize)); + uint cycleLog = ZSTD_cycleLog(cPar.chainLog, cPar.strategy); + + if (cPar.hashLog > dictAndWindowLog + 1) + { + cPar.hashLog = dictAndWindowLog + 1; + } + + if (cycleLog > dictAndWindowLog) + { + cPar.chainLog -= (cycleLog - dictAndWindowLog); + } + } + + if (cPar.windowLog < 10) + { + cPar.windowLog = 10; + } + + return cPar; + } + + /*! ZSTD_adjustCParams() : + * optimize params for a given `srcSize` and `dictSize`. + * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN. + * `dictSize` must be `0` when there is no dictionary. + * cPar can be invalid : all parameters will be clamped within valid range in the @return struct. + * This function never fails (wide contract) */ + public static ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, ulong srcSize, nuint dictSize) + { + cPar = ZSTD_clampCParams(cPar); + if (srcSize == 0) + { + srcSize = (unchecked(0UL - 1)); + } + + return ZSTD_adjustCParams_internal(cPar, srcSize, dictSize, ZSTD_cParamMode_e.ZSTD_cpm_unknown); + } + + private static void ZSTD_overrideCParams(ZSTD_compressionParameters* cParams, ZSTD_compressionParameters* overrides) + { + if (overrides->windowLog != 0) + { + cParams->windowLog = overrides->windowLog; + } + + if (overrides->hashLog != 0) + { + cParams->hashLog = overrides->hashLog; + } + + if (overrides->chainLog != 0) + { + cParams->chainLog = overrides->chainLog; + } + + if (overrides->searchLog != 0) + { + cParams->searchLog = overrides->searchLog; + } + + if (overrides->minMatch != 0) + { + cParams->minMatch = overrides->minMatch; + } + + if (overrides->targetLength != 0) + { + cParams->targetLength = overrides->targetLength; + } + + if (overrides->strategy != default) + { + cParams->strategy = overrides->strategy; + } + } + + /* ZSTD_getCParamsFromCCtxParams() : + * cParams are built depending on compressionLevel, src size hints, + * LDM and manually set compression parameters. + * Note: srcSizeHint == 0 means 0! + */ + public static ZSTD_compressionParameters ZSTD_getCParamsFromCCtxParams(ZSTD_CCtx_params_s* CCtxParams, ulong srcSizeHint, nuint dictSize, ZSTD_cParamMode_e mode) + { + ZSTD_compressionParameters cParams; + + if (srcSizeHint == (unchecked(0UL - 1)) && CCtxParams->srcSizeHint > 0) + { + srcSizeHint = (ulong)CCtxParams->srcSizeHint; + } + + cParams = ZSTD_getCParams_internal(CCtxParams->compressionLevel, srcSizeHint, dictSize, mode); + if (CCtxParams->ldmParams.enableLdm != 0) + { + cParams.windowLog = 27; + } + + ZSTD_overrideCParams(&cParams, &CCtxParams->cParams); + assert((ZSTD_checkCParams(cParams)) == 0); + return ZSTD_adjustCParams_internal(cParams, srcSizeHint, dictSize, mode); + } + + private static nuint ZSTD_sizeof_matchState(ZSTD_compressionParameters* cParams, ZSTD_useRowMatchFinderMode_e useRowMatchFinder, uint enableDedicatedDictSearch, uint forCCtx) + { + nuint chainSize = (ZSTD_allocateChainTable(cParams->strategy, useRowMatchFinder, ((enableDedicatedDictSearch != 0 && forCCtx == 0) ? 1U : 0U))) != 0 ? ((nuint)(1) << (int)cParams->chainLog) : 0; + nuint hSize = ((nuint)(1)) << (int)cParams->hashLog; + uint hashLog3 = (uint)((forCCtx != 0 && cParams->minMatch == 3) ? ((17) < (cParams->windowLog) ? (17) : (cParams->windowLog)) : 0); + nuint h3Size = hashLog3 != 0 ? ((nuint)(1)) << (int)hashLog3 : 0; + nuint tableSpace = chainSize * (nuint)(4) + hSize * (nuint)(4) + h3Size * (nuint)(4); + nuint optPotentialSpace = ZSTD_cwksp_aligned_alloc_size((uint)((52 + 1)) * (nuint)(4)) + ZSTD_cwksp_aligned_alloc_size((uint)((35 + 1)) * (nuint)(4)) + ZSTD_cwksp_aligned_alloc_size((uint)((31 + 1)) * (nuint)(4)) + ZSTD_cwksp_aligned_alloc_size((uint)((1 << 8)) * (nuint)(4)) + ZSTD_cwksp_aligned_alloc_size((uint)(((1 << 12) + 1)) * (nuint)(8)) + ZSTD_cwksp_aligned_alloc_size((uint)(((1 << 12) + 1)) * (nuint)(28)); + nuint lazyAdditionalSpace = (ZSTD_rowMatchFinderUsed(cParams->strategy, useRowMatchFinder)) != 0 ? ZSTD_cwksp_aligned_alloc_size(hSize * (nuint)(2)) : 0; + nuint optSpace = (forCCtx != 0 && (cParams->strategy >= ZSTD_strategy.ZSTD_btopt)) ? optPotentialSpace : 0; + nuint slackSpace = ZSTD_cwksp_slack_space_required(); + + assert(useRowMatchFinder != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + return tableSpace + optSpace + slackSpace + lazyAdditionalSpace; + } + + private static nuint ZSTD_estimateCCtxSize_usingCCtxParams_internal(ZSTD_compressionParameters* cParams, ldmParams_t* ldmParams, int isStatic, ZSTD_useRowMatchFinderMode_e useRowMatchFinder, nuint buffInSize, nuint buffOutSize, ulong pledgedSrcSize) + { + nuint windowSize = ((1) > ((nuint)((((ulong)(1) << (int)cParams->windowLog)) < (pledgedSrcSize) ? (((ulong)(1) << (int)cParams->windowLog)) : (pledgedSrcSize))) ? (1) : ((nuint)((((ulong)(1) << (int)cParams->windowLog)) < (pledgedSrcSize) ? (((ulong)(1) << (int)cParams->windowLog)) : (pledgedSrcSize)))); + nuint blockSize = (nuint)((uint)(((1 << 17))) < (windowSize) ? ((1 << 17)) : (windowSize)); + uint divider = (uint)((cParams->minMatch == 3) ? 3 : 4); + nuint maxNbSeq = blockSize / divider; + nuint tokenSpace = ZSTD_cwksp_alloc_size(32 + blockSize) + ZSTD_cwksp_aligned_alloc_size(maxNbSeq * (nuint)(8)) + 3 * ZSTD_cwksp_alloc_size(maxNbSeq * (nuint)(1)); + nuint entropySpace = ZSTD_cwksp_alloc_size(((uint)(((6 << 10) + 256)) + ((nuint)(4) * (uint)((((35) > (52) ? (35) : (52)) + 2))))); + nuint blockStateSpace = 2 * ZSTD_cwksp_alloc_size((nuint)(4592)); + nuint matchStateSize = ZSTD_sizeof_matchState(cParams, useRowMatchFinder, 0, 1); + nuint ldmSpace = ZSTD_ldm_getTableSize(*ldmParams); + nuint maxNbLdmSeq = ZSTD_ldm_getMaxNbSeq(*ldmParams, blockSize); + nuint ldmSeqSpace = ldmParams->enableLdm != 0 ? ZSTD_cwksp_aligned_alloc_size(maxNbLdmSeq * (nuint)(12)) : 0; + nuint bufferSpace = ZSTD_cwksp_alloc_size(buffInSize) + ZSTD_cwksp_alloc_size(buffOutSize); + nuint cctxSpace = isStatic != 0 ? ZSTD_cwksp_alloc_size((nuint)(sizeof(ZSTD_CCtx_s))) : 0; + nuint neededSpace = cctxSpace + entropySpace + blockStateSpace + ldmSpace + ldmSeqSpace + matchStateSize + tokenSpace + bufferSpace; + + return neededSpace; + } + + public static nuint ZSTD_estimateCCtxSize_usingCCtxParams(ZSTD_CCtx_params_s* @params) + { + ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams(@params, (unchecked(0UL - 1)), 0, ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict); + ZSTD_useRowMatchFinderMode_e useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(@params->useRowMatchFinder, &cParams); + + if (@params->nbWorkers > 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + return ZSTD_estimateCCtxSize_usingCCtxParams_internal(&cParams, &@params->ldmParams, 1, useRowMatchFinder, 0, 0, (unchecked(0UL - 1))); + } + + public static nuint ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams) + { + ZSTD_CCtx_params_s initialParams = ZSTD_makeCCtxParamsFromCParams(cParams); + + if ((ZSTD_rowMatchFinderSupported(cParams.strategy)) != 0) + { + nuint noRowCCtxSize; + nuint rowCCtxSize; + + initialParams.useRowMatchFinder = ZSTD_useRowMatchFinderMode_e.ZSTD_urm_disableRowMatchFinder; + noRowCCtxSize = ZSTD_estimateCCtxSize_usingCCtxParams(&initialParams); + initialParams.useRowMatchFinder = ZSTD_useRowMatchFinderMode_e.ZSTD_urm_enableRowMatchFinder; + rowCCtxSize = ZSTD_estimateCCtxSize_usingCCtxParams(&initialParams); + return ((noRowCCtxSize) > (rowCCtxSize) ? (noRowCCtxSize) : (rowCCtxSize)); + } + else + { + return ZSTD_estimateCCtxSize_usingCCtxParams(&initialParams); + } + } + + private static nuint ZSTD_estimateCCtxSize_internal(int compressionLevel) + { + int tier = 0; + nuint largestSize = 0; + + + for (; tier < 4; ++tier) + { + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal(compressionLevel, srcSizeTiers[tier], 0, ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict); + + largestSize = ((ZSTD_estimateCCtxSize_usingCParams(cParams)) > (largestSize) ? (ZSTD_estimateCCtxSize_usingCParams(cParams)) : (largestSize)); + } + + return largestSize; + } + + /*! ZSTD_estimate*() : + * These functions make it possible to estimate memory usage + * of a future {D,C}Ctx, before its creation. + * + * ZSTD_estimateCCtxSize() will provide a memory budget large enough + * for any compression level up to selected one. + * Note : Unlike ZSTD_estimateCStreamSize*(), this estimate + * does not include space for a window buffer. + * Therefore, the estimation is only guaranteed for single-shot compressions, not streaming. + * The estimate will assume the input may be arbitrarily large, + * which is the worst case. + * + * When srcSize can be bound by a known and rather "small" value, + * this fact can be used to provide a tighter estimation + * because the CCtx compression context will need less memory. + * This tighter estimation can be provided by more advanced functions + * ZSTD_estimateCCtxSize_usingCParams(), which can be used in tandem with ZSTD_getCParams(), + * and ZSTD_estimateCCtxSize_usingCCtxParams(), which can be used in tandem with ZSTD_CCtxParams_setParameter(). + * Both can be used to estimate memory using custom compression parameters and arbitrary srcSize limits. + * + * Note 2 : only single-threaded compression is supported. + * ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1. + */ + public static nuint ZSTD_estimateCCtxSize(int compressionLevel) + { + int level; + nuint memBudget = 0; + + for (level = ((compressionLevel) < (1) ? (compressionLevel) : (1)); level <= compressionLevel; level++) + { + nuint newMB = ZSTD_estimateCCtxSize_internal(level); + + if (newMB > memBudget) + { + memBudget = newMB; + } + } + + return memBudget; + } + + public static nuint ZSTD_estimateCStreamSize_usingCCtxParams(ZSTD_CCtx_params_s* @params) + { + if (@params->nbWorkers > 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + + { + ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams(@params, (unchecked(0UL - 1)), 0, ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict); + nuint blockSize = (nuint)((uint)(((1 << 17))) < ((nuint)(1) << (int)cParams.windowLog) ? ((1 << 17)) : ((nuint)(1) << (int)cParams.windowLog)); + nuint inBuffSize = (@params->inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) ? ((nuint)(1) << (int)cParams.windowLog) + blockSize : 0; + nuint outBuffSize = (@params->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) ? ZSTD_compressBound(blockSize) + 1 : 0; + ZSTD_useRowMatchFinderMode_e useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(@params->useRowMatchFinder, &@params->cParams); + + return ZSTD_estimateCCtxSize_usingCCtxParams_internal(&cParams, &@params->ldmParams, 1, useRowMatchFinder, inBuffSize, outBuffSize, (unchecked(0UL - 1))); + } + } + + public static nuint ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams) + { + ZSTD_CCtx_params_s initialParams = ZSTD_makeCCtxParamsFromCParams(cParams); + + if ((ZSTD_rowMatchFinderSupported(cParams.strategy)) != 0) + { + nuint noRowCCtxSize; + nuint rowCCtxSize; + + initialParams.useRowMatchFinder = ZSTD_useRowMatchFinderMode_e.ZSTD_urm_disableRowMatchFinder; + noRowCCtxSize = ZSTD_estimateCStreamSize_usingCCtxParams(&initialParams); + initialParams.useRowMatchFinder = ZSTD_useRowMatchFinderMode_e.ZSTD_urm_enableRowMatchFinder; + rowCCtxSize = ZSTD_estimateCStreamSize_usingCCtxParams(&initialParams); + return ((noRowCCtxSize) > (rowCCtxSize) ? (noRowCCtxSize) : (rowCCtxSize)); + } + else + { + return ZSTD_estimateCStreamSize_usingCCtxParams(&initialParams); + } + } + + private static nuint ZSTD_estimateCStreamSize_internal(int compressionLevel) + { + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal(compressionLevel, (unchecked(0UL - 1)), 0, ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict); + + return ZSTD_estimateCStreamSize_usingCParams(cParams); + } + + /*! ZSTD_estimateCStreamSize() : + * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one. + * It will also consider src size to be arbitrarily "large", which is worst case. + * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation. + * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel. + * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1. + * Note : CStream size estimation is only correct for single-threaded compression. + * ZSTD_DStream memory budget depends on window Size. + * This information can be passed manually, using ZSTD_estimateDStreamSize, + * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame(); + * Note : if streaming is init with function ZSTD_init?Stream_usingDict(), + * an internal ?Dict will be created, which additional size is not estimated here. + * In this case, get total size by adding ZSTD_estimate?DictSize */ + public static nuint ZSTD_estimateCStreamSize(int compressionLevel) + { + int level; + nuint memBudget = 0; + + for (level = ((compressionLevel) < (1) ? (compressionLevel) : (1)); level <= compressionLevel; level++) + { + nuint newMB = ZSTD_estimateCStreamSize_internal(level); + + if (newMB > memBudget) + { + memBudget = newMB; + } + } + + return memBudget; + } + + /* ZSTD_getFrameProgression(): + * tells how much data has been consumed (input) and produced (output) for current frame. + * able to count progression inside worker threads (non-blocking mode). + */ + public static ZSTD_frameProgression ZSTD_getFrameProgression(ZSTD_CCtx_s* cctx) + { + + { + ZSTD_frameProgression fp; + nuint buffered = (cctx->inBuff == null) ? 0 : cctx->inBuffPos - cctx->inToCompress; + + if (buffered != 0) + { + assert(cctx->inBuffPos >= cctx->inToCompress); + } + + assert(buffered <= (uint)((1 << 17))); + fp.ingested = cctx->consumedSrcSize + buffered; + fp.consumed = cctx->consumedSrcSize; + fp.produced = cctx->producedCSize; + fp.flushed = cctx->producedCSize; + fp.currentJobID = 0; + fp.nbActiveWorkers = 0; + return fp; + } + } + + /*! ZSTD_toFlushNow() + * Only useful for multithreading scenarios currently (nbWorkers >= 1). + */ + public static nuint ZSTD_toFlushNow(ZSTD_CCtx_s* cctx) + { + return 0; + } + + private static void ZSTD_assertEqualCParams(ZSTD_compressionParameters cParams1, ZSTD_compressionParameters cParams2) + { + assert(cParams1.windowLog == cParams2.windowLog); + assert(cParams1.chainLog == cParams2.chainLog); + assert(cParams1.hashLog == cParams2.hashLog); + assert(cParams1.searchLog == cParams2.searchLog); + assert(cParams1.minMatch == cParams2.minMatch); + assert(cParams1.targetLength == cParams2.targetLength); + assert(cParams1.strategy == cParams2.strategy); + } + + public static void ZSTD_reset_compressedBlockState(ZSTD_compressedBlockState_t* bs) + { + int i; + + for (i = 0; i < 3; ++i) + { + bs->rep[i] = repStartValue[i]; + } + + bs->entropy.huf.repeatMode = HUF_repeat.HUF_repeat_none; + bs->entropy.fse.offcode_repeatMode = FSE_repeat.FSE_repeat_none; + bs->entropy.fse.matchlength_repeatMode = FSE_repeat.FSE_repeat_none; + bs->entropy.fse.litlength_repeatMode = FSE_repeat.FSE_repeat_none; + } + + /*! ZSTD_invalidateMatchState() + * Invalidate all the matches in the match finder tables. + * Requires nextSrc and base to be set (can be NULL). + */ + private static void ZSTD_invalidateMatchState(ZSTD_matchState_t* ms) + { + ZSTD_window_clear(&ms->window); + ms->nextToUpdate = ms->window.dictLimit; + ms->loadedDictEnd = 0; + ms->opt.litLengthSum = 0; + ms->dictMatchState = null; + } + + private static nuint ZSTD_reset_matchState(ZSTD_matchState_t* ms, ZSTD_cwksp* ws, ZSTD_compressionParameters* cParams, ZSTD_useRowMatchFinderMode_e useRowMatchFinder, ZSTD_compResetPolicy_e crp, ZSTD_indexResetPolicy_e forceResetIndex, ZSTD_resetTarget_e forWho) + { + nuint chainSize = (ZSTD_allocateChainTable(cParams->strategy, useRowMatchFinder, ((ms->dedicatedDictSearch != 0 && (forWho == ZSTD_resetTarget_e.ZSTD_resetTarget_CDict)) ? 1U : 0U))) != 0 ? ((nuint)(1) << (int)cParams->chainLog) : 0; + nuint hSize = ((nuint)(1)) << (int)cParams->hashLog; + uint hashLog3 = (uint)(((forWho == ZSTD_resetTarget_e.ZSTD_resetTarget_CCtx) && cParams->minMatch == 3) ? ((17) < (cParams->windowLog) ? (17) : (cParams->windowLog)) : 0); + nuint h3Size = hashLog3 != 0 ? ((nuint)(1)) << (int)hashLog3 : 0; + + assert(useRowMatchFinder != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + if (forceResetIndex == ZSTD_indexResetPolicy_e.ZSTDirp_reset) + { + ZSTD_window_init(&ms->window); + ZSTD_cwksp_mark_tables_dirty(ws); + } + + ms->hashLog3 = hashLog3; + ZSTD_invalidateMatchState(ms); + assert((ZSTD_cwksp_reserve_failed(ws)) == 0); + ZSTD_cwksp_clear_tables(ws); + ms->hashTable = (uint*)(ZSTD_cwksp_reserve_table(ws, hSize * (nuint)(sizeof(uint)))); + ms->chainTable = (uint*)(ZSTD_cwksp_reserve_table(ws, chainSize * (nuint)(sizeof(uint)))); + ms->hashTable3 = (uint*)(ZSTD_cwksp_reserve_table(ws, h3Size * (nuint)(sizeof(uint)))); + if ((ZSTD_cwksp_reserve_failed(ws)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + if (crp != ZSTD_compResetPolicy_e.ZSTDcrp_leaveDirty) + { + ZSTD_cwksp_clean_tables(ws); + } + + if ((forWho == ZSTD_resetTarget_e.ZSTD_resetTarget_CCtx) && (cParams->strategy >= ZSTD_strategy.ZSTD_btopt)) + { + ms->opt.litFreq = (uint*)(ZSTD_cwksp_reserve_aligned(ws, (uint)((1 << 8)) * (nuint)(sizeof(uint)))); + ms->opt.litLengthFreq = (uint*)(ZSTD_cwksp_reserve_aligned(ws, (uint)((35 + 1)) * (nuint)(sizeof(uint)))); + ms->opt.matchLengthFreq = (uint*)(ZSTD_cwksp_reserve_aligned(ws, (uint)((52 + 1)) * (nuint)(sizeof(uint)))); + ms->opt.offCodeFreq = (uint*)(ZSTD_cwksp_reserve_aligned(ws, (uint)((31 + 1)) * (nuint)(sizeof(uint)))); + ms->opt.matchTable = (ZSTD_match_t*)(ZSTD_cwksp_reserve_aligned(ws, (uint)(((1 << 12) + 1)) * (nuint)(sizeof(ZSTD_match_t)))); + ms->opt.priceTable = (ZSTD_optimal_t*)(ZSTD_cwksp_reserve_aligned(ws, (uint)(((1 << 12) + 1)) * (nuint)(sizeof(ZSTD_optimal_t)))); + } + + if ((ZSTD_rowMatchFinderUsed(cParams->strategy, useRowMatchFinder)) != 0) + { + + { + nuint tagTableSize = hSize * (nuint)(2); + + ms->tagTable = (ushort*)(ZSTD_cwksp_reserve_aligned(ws, tagTableSize)); + if (ms->tagTable != null) + { + memset((void*)(ms->tagTable), (0), (tagTableSize)); + } + } + + + { + uint rowLog = (uint)(cParams->searchLog < 5 ? 4 : 5); + + assert(cParams->hashLog > rowLog); + ms->rowHashLog = cParams->hashLog - rowLog; + } + } + + ms->cParams = *cParams; + if ((ZSTD_cwksp_reserve_failed(ws)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + return 0; + } + + private static int ZSTD_indexTooCloseToMax(ZSTD_window_t w) + { + return (((nuint)(w.nextSrc - w.@base) > (((3U << 29) + (1U << ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)))) - (uint)((16 * (1 << 20))))) ? 1 : 0); + } + + /** ZSTD_dictTooBig(): + * When dictionaries are larger than ZSTD_CHUNKSIZE_MAX they can't be loaded in + * one go generically. So we ensure that in that case we reset the tables to zero, + * so that we can load as much of the dictionary as possible. + */ + private static int ZSTD_dictTooBig(nuint loadedDictSize) + { + return ((loadedDictSize > ((unchecked((uint)(-1))) - ((3U << 29) + (1U << ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)))))) ? 1 : 0); + } + + /*! ZSTD_resetCCtx_internal() : + * @param loadedDictSize The size of the dictionary to be loaded + * into the context, if any. If no dictionary is used, or the + * dictionary is being attached / copied, then pass 0. + * note : `params` are assumed fully validated at this stage. + */ + private static nuint ZSTD_resetCCtx_internal(ZSTD_CCtx_s* zc, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize, nuint loadedDictSize, ZSTD_compResetPolicy_e crp, ZSTD_buffered_policy_e zbuff) + { + ZSTD_cwksp* ws = &zc->workspace; + + assert((ERR_isError(ZSTD_checkCParams(@params->cParams))) == 0); + zc->isFirstBlock = 1; + zc->appliedParams = *@params; + @params = &zc->appliedParams; + assert(@params->useRowMatchFinder != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + if (@params->ldmParams.enableLdm != 0) + { + ZSTD_ldm_adjustParameters(&zc->appliedParams.ldmParams, &@params->cParams); + assert(@params->ldmParams.hashLog >= @params->ldmParams.bucketSizeLog); + assert(@params->ldmParams.hashRateLog < 32); + } + + + { + nuint windowSize = ((1) > ((nuint)((((ulong)(1) << (int)@params->cParams.windowLog)) < (pledgedSrcSize) ? (((ulong)(1) << (int)@params->cParams.windowLog)) : (pledgedSrcSize))) ? (1) : ((nuint)((((ulong)(1) << (int)@params->cParams.windowLog)) < (pledgedSrcSize) ? (((ulong)(1) << (int)@params->cParams.windowLog)) : (pledgedSrcSize)))); + nuint blockSize = (nuint)((uint)(((1 << 17))) < (windowSize) ? ((1 << 17)) : (windowSize)); + uint divider = (uint)((@params->cParams.minMatch == 3) ? 3 : 4); + nuint maxNbSeq = blockSize / divider; + nuint buffOutSize = (zbuff == ZSTD_buffered_policy_e.ZSTDb_buffered && @params->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) ? ZSTD_compressBound(blockSize) + 1 : 0; + nuint buffInSize = (zbuff == ZSTD_buffered_policy_e.ZSTDb_buffered && @params->inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) ? windowSize + blockSize : 0; + nuint maxNbLdmSeq = ZSTD_ldm_getMaxNbSeq(@params->ldmParams, blockSize); + int indexTooClose = ZSTD_indexTooCloseToMax(zc->blockState.matchState.window); + int dictTooBig = ZSTD_dictTooBig(loadedDictSize); + ZSTD_indexResetPolicy_e needsIndexReset = (indexTooClose != 0 || dictTooBig != 0 || zc->initialized == 0) ? ZSTD_indexResetPolicy_e.ZSTDirp_reset : ZSTD_indexResetPolicy_e.ZSTDirp_continue; + nuint neededSpace = ZSTD_estimateCCtxSize_usingCCtxParams_internal(&@params->cParams, &@params->ldmParams, ((zc->staticSize != 0) ? 1 : 0), @params->useRowMatchFinder, buffInSize, buffOutSize, pledgedSrcSize); + int resizeWorkspace; + + + { + nuint err_code = (neededSpace); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (zc->staticSize == 0) + { + ZSTD_cwksp_bump_oversized_duration(ws, 0); + } + + + { + int workspaceTooSmall = ((ZSTD_cwksp_sizeof(ws) < neededSpace) ? 1 : 0); + int workspaceWasteful = ZSTD_cwksp_check_wasteful(ws, neededSpace); + + resizeWorkspace = ((workspaceTooSmall != 0 || workspaceWasteful != 0) ? 1 : 0); + if (resizeWorkspace != 0) + { + if (zc->staticSize != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + needsIndexReset = ZSTD_indexResetPolicy_e.ZSTDirp_reset; + ZSTD_cwksp_free(ws, zc->customMem); + + { + nuint err_code = (ZSTD_cwksp_create(ws, neededSpace, zc->customMem)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert((ZSTD_cwksp_check_available(ws, 2 * (nuint)(sizeof(ZSTD_compressedBlockState_t)))) != 0); + zc->blockState.prevCBlock = (ZSTD_compressedBlockState_t*)(ZSTD_cwksp_reserve_object(ws, (nuint)(sizeof(ZSTD_compressedBlockState_t)))); + if (zc->blockState.prevCBlock == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + zc->blockState.nextCBlock = (ZSTD_compressedBlockState_t*)(ZSTD_cwksp_reserve_object(ws, (nuint)(sizeof(ZSTD_compressedBlockState_t)))); + if (zc->blockState.nextCBlock == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + zc->entropyWorkspace = (uint*)(ZSTD_cwksp_reserve_object(ws, ((uint)(((6 << 10) + 256)) + ((nuint)(sizeof(uint)) * (uint)((((35) > (52) ? (35) : (52)) + 2)))))); + if (zc->blockState.nextCBlock == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + } + } + + ZSTD_cwksp_clear(ws); + zc->blockState.matchState.cParams = @params->cParams; + zc->pledgedSrcSizePlusOne = pledgedSrcSize + 1; + zc->consumedSrcSize = 0; + zc->producedCSize = 0; + if (pledgedSrcSize == (unchecked(0UL - 1))) + { + zc->appliedParams.fParams.contentSizeFlag = 0; + } + + zc->blockSize = blockSize; + XXH64_reset(&zc->xxhState, 0); + zc->stage = ZSTD_compressionStage_e.ZSTDcs_init; + zc->dictID = 0; + zc->dictContentSize = 0; + ZSTD_reset_compressedBlockState(zc->blockState.prevCBlock); + zc->seqStore.litStart = ZSTD_cwksp_reserve_buffer(ws, blockSize + 32); + zc->seqStore.maxNbLit = blockSize; + zc->bufferedPolicy = zbuff; + zc->inBuffSize = buffInSize; + zc->inBuff = (sbyte*)(ZSTD_cwksp_reserve_buffer(ws, buffInSize)); + zc->outBuffSize = buffOutSize; + zc->outBuff = (sbyte*)(ZSTD_cwksp_reserve_buffer(ws, buffOutSize)); + if (@params->ldmParams.enableLdm != 0) + { + nuint numBuckets = ((nuint)(1)) << (int)(@params->ldmParams.hashLog - @params->ldmParams.bucketSizeLog); + + zc->ldmState.bucketOffsets = ZSTD_cwksp_reserve_buffer(ws, numBuckets); + memset((void*)(zc->ldmState.bucketOffsets), (0), (numBuckets)); + } + + ZSTD_referenceExternalSequences(zc, (rawSeq*)null, 0); + zc->seqStore.maxNbSeq = maxNbSeq; + zc->seqStore.llCode = ZSTD_cwksp_reserve_buffer(ws, maxNbSeq * (nuint)(sizeof(byte))); + zc->seqStore.mlCode = ZSTD_cwksp_reserve_buffer(ws, maxNbSeq * (nuint)(sizeof(byte))); + zc->seqStore.ofCode = ZSTD_cwksp_reserve_buffer(ws, maxNbSeq * (nuint)(sizeof(byte))); + zc->seqStore.sequencesStart = (seqDef_s*)(ZSTD_cwksp_reserve_aligned(ws, maxNbSeq * (nuint)(sizeof(seqDef_s)))); + + { + nuint err_code = (ZSTD_reset_matchState(&zc->blockState.matchState, ws, &@params->cParams, @params->useRowMatchFinder, crp, needsIndexReset, ZSTD_resetTarget_e.ZSTD_resetTarget_CCtx)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (@params->ldmParams.enableLdm != 0) + { + nuint ldmHSize = ((nuint)(1)) << (int)@params->ldmParams.hashLog; + + zc->ldmState.hashTable = (ldmEntry_t*)(ZSTD_cwksp_reserve_aligned(ws, ldmHSize * (nuint)(sizeof(ldmEntry_t)))); + memset((void*)(zc->ldmState.hashTable), (0), (ldmHSize * (nuint)(sizeof(ldmEntry_t)))); + zc->ldmSequences = (rawSeq*)(ZSTD_cwksp_reserve_aligned(ws, maxNbLdmSeq * (nuint)(sizeof(rawSeq)))); + zc->maxNbLdmSequences = maxNbLdmSeq; + ZSTD_window_init(&zc->ldmState.window); + zc->ldmState.loadedDictEnd = 0; + } + + assert((ZSTD_cwksp_estimated_space_within_bounds(ws, neededSpace, resizeWorkspace)) != 0); + zc->initialized = 1; + return 0; + } + } + + /* ZSTD_invalidateRepCodes() : + * ensures next compression will not use repcodes from previous block. + * Note : only works with regular variant; + * do not use with extDict variant ! */ + public static void ZSTD_invalidateRepCodes(ZSTD_CCtx_s* cctx) + { + int i; + + for (i = 0; i < 3; i++) + { + cctx->blockState.prevCBlock->rep[i] = 0; + } + + assert((ZSTD_window_hasExtDict(cctx->blockState.matchState.window)) == 0); + } + + public static nuint* attachDictSizeCutoffs = GetArrayPointer(new nuint[10] + { + 8 * (1 << 10), + 8 * (1 << 10), + 16 * (1 << 10), + 32 * (1 << 10), + 32 * (1 << 10), + 32 * (1 << 10), + 32 * (1 << 10), + 32 * (1 << 10), + 8 * (1 << 10), + 8 * (1 << 10), + }); + + private static int ZSTD_shouldAttachDict(ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize) + { + nuint cutoff = attachDictSizeCutoffs[(int)cdict->matchState.cParams.strategy]; + int dedicatedDictSearch = cdict->matchState.dedicatedDictSearch; + + return ((dedicatedDictSearch != 0 || ((pledgedSrcSize <= cutoff || pledgedSrcSize == (unchecked(0UL - 1)) || @params->attachDictPref == ZSTD_dictAttachPref_e.ZSTD_dictForceAttach) && @params->attachDictPref != ZSTD_dictAttachPref_e.ZSTD_dictForceCopy && @params->forceWindow == 0)) ? 1 : 0); + } + + private static nuint ZSTD_resetCCtx_byAttachingCDict(ZSTD_CCtx_s* cctx, ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s @params, ulong pledgedSrcSize, ZSTD_buffered_policy_e zbuff) + { + + { + ZSTD_compressionParameters adjusted_cdict_cParams = cdict->matchState.cParams; + uint windowLog = @params.cParams.windowLog; + + assert(windowLog != 0); + if (cdict->matchState.dedicatedDictSearch != 0) + { + ZSTD_dedicatedDictSearch_revertCParams(&adjusted_cdict_cParams); + } + + @params.cParams = ZSTD_adjustCParams_internal(adjusted_cdict_cParams, pledgedSrcSize, cdict->dictContentSize, ZSTD_cParamMode_e.ZSTD_cpm_attachDict); + @params.cParams.windowLog = windowLog; + @params.useRowMatchFinder = cdict->useRowMatchFinder; + + { + nuint err_code = (ZSTD_resetCCtx_internal(cctx, &@params, pledgedSrcSize, 0, ZSTD_compResetPolicy_e.ZSTDcrp_makeClean, zbuff)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(cctx->appliedParams.cParams.strategy == adjusted_cdict_cParams.strategy); + } + + + { + uint cdictEnd = (uint)(cdict->matchState.window.nextSrc - cdict->matchState.window.@base); + uint cdictLen = cdictEnd - cdict->matchState.window.dictLimit; + + if (cdictLen == 0) + { + ; + } + else + { + cctx->blockState.matchState.dictMatchState = &cdict->matchState; + if (cctx->blockState.matchState.window.dictLimit < cdictEnd) + { + cctx->blockState.matchState.window.nextSrc = cctx->blockState.matchState.window.@base + cdictEnd; + ZSTD_window_clear(&cctx->blockState.matchState.window); + } + + cctx->blockState.matchState.loadedDictEnd = cctx->blockState.matchState.window.dictLimit; + } + } + + cctx->dictID = cdict->dictID; + cctx->dictContentSize = cdict->dictContentSize; + memcpy((void*)(cctx->blockState.prevCBlock), (void*)(&cdict->cBlockState), ((nuint)(sizeof(ZSTD_compressedBlockState_t)))); + return 0; + } + + private static nuint ZSTD_resetCCtx_byCopyingCDict(ZSTD_CCtx_s* cctx, ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s @params, ulong pledgedSrcSize, ZSTD_buffered_policy_e zbuff) + { + ZSTD_compressionParameters* cdict_cParams = &cdict->matchState.cParams; + + assert(cdict->matchState.dedicatedDictSearch == 0); + + { + uint windowLog = @params.cParams.windowLog; + + assert(windowLog != 0); + @params.cParams = *cdict_cParams; + @params.cParams.windowLog = windowLog; + @params.useRowMatchFinder = cdict->useRowMatchFinder; + + { + nuint err_code = (ZSTD_resetCCtx_internal(cctx, &@params, pledgedSrcSize, 0, ZSTD_compResetPolicy_e.ZSTDcrp_leaveDirty, zbuff)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(cctx->appliedParams.cParams.strategy == cdict_cParams->strategy); + assert(cctx->appliedParams.cParams.hashLog == cdict_cParams->hashLog); + assert(cctx->appliedParams.cParams.chainLog == cdict_cParams->chainLog); + } + + ZSTD_cwksp_mark_tables_dirty(&cctx->workspace); + assert(@params.useRowMatchFinder != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + + { + nuint chainSize = (ZSTD_allocateChainTable(cdict_cParams->strategy, cdict->useRowMatchFinder, 0)) != 0 ? ((nuint)(1) << (int)cdict_cParams->chainLog) : 0; + nuint hSize = (nuint)(1) << (int)cdict_cParams->hashLog; + + memcpy((void*)(cctx->blockState.matchState.hashTable), (void*)(cdict->matchState.hashTable), (hSize * (nuint)(sizeof(uint)))); + if ((ZSTD_allocateChainTable(cctx->appliedParams.cParams.strategy, cctx->appliedParams.useRowMatchFinder, 0)) != 0) + { + memcpy((void*)(cctx->blockState.matchState.chainTable), (void*)(cdict->matchState.chainTable), (chainSize * (nuint)(sizeof(uint)))); + } + + if ((ZSTD_rowMatchFinderUsed(cdict_cParams->strategy, cdict->useRowMatchFinder)) != 0) + { + nuint tagTableSize = hSize * (nuint)(2); + + memcpy((void*)(cctx->blockState.matchState.tagTable), (void*)(cdict->matchState.tagTable), (tagTableSize)); + } + } + + + { + int h3log = (int)cctx->blockState.matchState.hashLog3; + nuint h3Size = h3log != 0 ? ((nuint)(1) << h3log) : 0; + + assert(cdict->matchState.hashLog3 == 0); + memset((void*)(cctx->blockState.matchState.hashTable3), (0), (h3Size * (nuint)(sizeof(uint)))); + } + + ZSTD_cwksp_mark_tables_clean(&cctx->workspace); + + { + ZSTD_matchState_t* srcMatchState = &cdict->matchState; + ZSTD_matchState_t* dstMatchState = &cctx->blockState.matchState; + + dstMatchState->window = srcMatchState->window; + dstMatchState->nextToUpdate = srcMatchState->nextToUpdate; + dstMatchState->loadedDictEnd = srcMatchState->loadedDictEnd; + } + + cctx->dictID = cdict->dictID; + cctx->dictContentSize = cdict->dictContentSize; + memcpy((void*)(cctx->blockState.prevCBlock), (void*)(&cdict->cBlockState), ((nuint)(sizeof(ZSTD_compressedBlockState_t)))); + return 0; + } + + /* We have a choice between copying the dictionary context into the working + * context, or referencing the dictionary context from the working context + * in-place. We decide here which strategy to use. */ + private static nuint ZSTD_resetCCtx_usingCDict(ZSTD_CCtx_s* cctx, ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize, ZSTD_buffered_policy_e zbuff) + { + if ((ZSTD_shouldAttachDict(cdict, @params, pledgedSrcSize)) != 0) + { + return ZSTD_resetCCtx_byAttachingCDict(cctx, cdict, *@params, pledgedSrcSize, zbuff); + } + else + { + return ZSTD_resetCCtx_byCopyingCDict(cctx, cdict, *@params, pledgedSrcSize, zbuff); + } + } + + /*! ZSTD_copyCCtx_internal() : + * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. + * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). + * The "context", in this case, refers to the hash and chain tables, + * entropy tables, and dictionary references. + * `windowLog` value is enforced if != 0, otherwise value is copied from srcCCtx. + * @return : 0, or an error code */ + private static nuint ZSTD_copyCCtx_internal(ZSTD_CCtx_s* dstCCtx, ZSTD_CCtx_s* srcCCtx, ZSTD_frameParameters fParams, ulong pledgedSrcSize, ZSTD_buffered_policy_e zbuff) + { + if (srcCCtx->stage != ZSTD_compressionStage_e.ZSTDcs_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + memcpy((void*)(&dstCCtx->customMem), (void*)(&srcCCtx->customMem), ((nuint)(sizeof(ZSTD_customMem)))); + + { + ZSTD_CCtx_params_s @params = dstCCtx->requestedParams; + + @params.cParams = srcCCtx->appliedParams.cParams; + assert(srcCCtx->appliedParams.useRowMatchFinder != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + @params.useRowMatchFinder = srcCCtx->appliedParams.useRowMatchFinder; + @params.fParams = fParams; + ZSTD_resetCCtx_internal(dstCCtx, &@params, pledgedSrcSize, 0, ZSTD_compResetPolicy_e.ZSTDcrp_leaveDirty, zbuff); + assert(dstCCtx->appliedParams.cParams.windowLog == srcCCtx->appliedParams.cParams.windowLog); + assert(dstCCtx->appliedParams.cParams.strategy == srcCCtx->appliedParams.cParams.strategy); + assert(dstCCtx->appliedParams.cParams.hashLog == srcCCtx->appliedParams.cParams.hashLog); + assert(dstCCtx->appliedParams.cParams.chainLog == srcCCtx->appliedParams.cParams.chainLog); + assert(dstCCtx->blockState.matchState.hashLog3 == srcCCtx->blockState.matchState.hashLog3); + } + + ZSTD_cwksp_mark_tables_dirty(&dstCCtx->workspace); + + { + nuint chainSize = (ZSTD_allocateChainTable(srcCCtx->appliedParams.cParams.strategy, srcCCtx->appliedParams.useRowMatchFinder, 0)) != 0 ? ((nuint)(1) << (int)srcCCtx->appliedParams.cParams.chainLog) : 0; + nuint hSize = (nuint)(1) << (int)srcCCtx->appliedParams.cParams.hashLog; + int h3log = (int)srcCCtx->blockState.matchState.hashLog3; + nuint h3Size = h3log != 0 ? ((nuint)(1) << h3log) : 0; + + memcpy((void*)(dstCCtx->blockState.matchState.hashTable), (void*)(srcCCtx->blockState.matchState.hashTable), (hSize * (nuint)(sizeof(uint)))); + memcpy((void*)(dstCCtx->blockState.matchState.chainTable), (void*)(srcCCtx->blockState.matchState.chainTable), (chainSize * (nuint)(sizeof(uint)))); + memcpy((void*)(dstCCtx->blockState.matchState.hashTable3), (void*)(srcCCtx->blockState.matchState.hashTable3), (h3Size * (nuint)(sizeof(uint)))); + } + + ZSTD_cwksp_mark_tables_clean(&dstCCtx->workspace); + + { + ZSTD_matchState_t* srcMatchState = &srcCCtx->blockState.matchState; + ZSTD_matchState_t* dstMatchState = &dstCCtx->blockState.matchState; + + dstMatchState->window = srcMatchState->window; + dstMatchState->nextToUpdate = srcMatchState->nextToUpdate; + dstMatchState->loadedDictEnd = srcMatchState->loadedDictEnd; + } + + dstCCtx->dictID = srcCCtx->dictID; + dstCCtx->dictContentSize = srcCCtx->dictContentSize; + memcpy((void*)(dstCCtx->blockState.prevCBlock), (void*)(srcCCtx->blockState.prevCBlock), ((nuint)(sizeof(ZSTD_compressedBlockState_t)))); + return 0; + } + + /*! ZSTD_copyCCtx() : + * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. + * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). + * pledgedSrcSize==0 means "unknown". + * @return : 0, or an error code */ + public static nuint ZSTD_copyCCtx(ZSTD_CCtx_s* dstCCtx, ZSTD_CCtx_s* srcCCtx, ulong pledgedSrcSize) + { + ZSTD_frameParameters fParams = new ZSTD_frameParameters + { + contentSizeFlag = 1, + checksumFlag = 0, + noDictIDFlag = 0, + }; + ZSTD_buffered_policy_e zbuff = srcCCtx->bufferedPolicy; + + if (pledgedSrcSize == 0) + { + pledgedSrcSize = (unchecked(0UL - 1)); + } + + fParams.contentSizeFlag = ((pledgedSrcSize != (unchecked(0UL - 1))) ? 1 : 0); + return ZSTD_copyCCtx_internal(dstCCtx, srcCCtx, fParams, pledgedSrcSize, zbuff); + } + + /*! ZSTD_reduceTable() : + * reduce table indexes by `reducerValue`, or squash to zero. + * PreserveMark preserves "unsorted mark" for btlazy2 strategy. + * It must be set to a clear 0/1 value, to remove branch during inlining. + * Presume table size is a multiple of ZSTD_ROWSIZE + * to help auto-vectorization */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_reduceTable_internal(uint* table, uint size, uint reducerValue, int preserveMark) + { + int nbRows = (int)(size) / 16; + int cellNb = 0; + int rowNb; + + assert((size & (uint)((16 - 1))) == 0); + assert(size < (1U << 31)); + for (rowNb = 0; rowNb < nbRows; rowNb++) + { + int column; + + for (column = 0; column < 16; column++) + { + if (preserveMark != 0) + { + uint adder = (table[cellNb] == 1) ? reducerValue : 0; + + table[cellNb] += adder; + } + + if (table[cellNb] < reducerValue) + { + table[cellNb] = 0; + } + else + { + table[cellNb] -= reducerValue; + } + + cellNb++; + } + } + } + + private static void ZSTD_reduceTable(uint* table, uint size, uint reducerValue) + { + ZSTD_reduceTable_internal(table, size, reducerValue, 0); + } + + private static void ZSTD_reduceTable_btlazy2(uint* table, uint size, uint reducerValue) + { + ZSTD_reduceTable_internal(table, size, reducerValue, 1); + } + + /*! ZSTD_reduceIndex() : + * rescale all indexes to avoid future overflow (indexes are U32) */ + private static void ZSTD_reduceIndex(ZSTD_matchState_t* ms, ZSTD_CCtx_params_s* @params, uint reducerValue) + { + + { + uint hSize = (uint)(1) << (int)@params->cParams.hashLog; + + ZSTD_reduceTable(ms->hashTable, hSize, reducerValue); + } + + if ((ZSTD_allocateChainTable(@params->cParams.strategy, @params->useRowMatchFinder, (uint)(ms->dedicatedDictSearch))) != 0) + { + uint chainSize = (uint)(1) << (int)@params->cParams.chainLog; + + if (@params->cParams.strategy == ZSTD_strategy.ZSTD_btlazy2) + { + ZSTD_reduceTable_btlazy2(ms->chainTable, chainSize, reducerValue); + } + else + { + ZSTD_reduceTable(ms->chainTable, chainSize, reducerValue); + } + } + + if (ms->hashLog3 != 0) + { + uint h3Size = (uint)(1) << (int)ms->hashLog3; + + ZSTD_reduceTable(ms->hashTable3, h3Size, reducerValue); + } + } + + /* See doc/zstd_compression_format.md for detailed format description */ + public static void ZSTD_seqToCodes(seqStore_t* seqStorePtr) + { + seqDef_s* sequences = seqStorePtr->sequencesStart; + byte* llCodeTable = seqStorePtr->llCode; + byte* ofCodeTable = seqStorePtr->ofCode; + byte* mlCodeTable = seqStorePtr->mlCode; + uint nbSeq = (uint)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + uint u; + + assert(nbSeq <= seqStorePtr->maxNbSeq); + for (u = 0; u < nbSeq; u++) + { + uint llv = sequences[u].litLength; + uint mlv = sequences[u].matchLength; + + llCodeTable[u] = (byte)(ZSTD_LLcode(llv)); + ofCodeTable[u] = (byte)(ZSTD_highbit32(sequences[u].offset)); + mlCodeTable[u] = (byte)(ZSTD_MLcode(mlv)); + } + + if (seqStorePtr->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_literalLength) + { + llCodeTable[seqStorePtr->longLengthPos] = 35; + } + + if (seqStorePtr->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_matchLength) + { + mlCodeTable[seqStorePtr->longLengthPos] = 52; + } + } + + /* ZSTD_useTargetCBlockSize(): + * Returns if target compressed block size param is being used. + * If used, compression will do best effort to make a compressed block size to be around targetCBlockSize. + * Returns 1 if true, 0 otherwise. */ + private static int ZSTD_useTargetCBlockSize(ZSTD_CCtx_params_s* cctxParams) + { + return ((cctxParams->targetCBlockSize != 0) ? 1 : 0); + } + + /* ZSTD_blockSplitterEnabled(): + * Returns if block splitting param is being used + * If used, compression will do best effort to split a block in order to improve compression ratio. + * Returns 1 if true, 0 otherwise. */ + private static int ZSTD_blockSplitterEnabled(ZSTD_CCtx_params_s* cctxParams) + { + return ((cctxParams->splitBlocks != 0) ? 1 : 0); + } + + /* ZSTD_buildSequencesStatistics(): + * Returns a ZSTD_symbolEncodingTypeStats_t, or a zstd error code in the `size` field. + * Modifies `nextEntropy` to have the appropriate values as a side effect. + * nbSeq must be greater than 0. + * + * entropyWkspSize must be of size at least ENTROPY_WORKSPACE_SIZE - (MaxSeq + 1)*sizeof(U32) + */ + private static ZSTD_symbolEncodingTypeStats_t ZSTD_buildSequencesStatistics(seqStore_t* seqStorePtr, nuint nbSeq, ZSTD_fseCTables_t* prevEntropy, ZSTD_fseCTables_t* nextEntropy, byte* dst, byte* dstEnd, ZSTD_strategy strategy, uint* countWorkspace, void* entropyWorkspace, nuint entropyWkspSize) + { + byte* ostart = dst; + byte* oend = dstEnd; + byte* op = ostart; + uint* CTable_LitLength = (uint*)nextEntropy->litlengthCTable; + uint* CTable_OffsetBits = (uint*)nextEntropy->offcodeCTable; + uint* CTable_MatchLength = (uint*)nextEntropy->matchlengthCTable; + byte* ofCodeTable = seqStorePtr->ofCode; + byte* llCodeTable = seqStorePtr->llCode; + byte* mlCodeTable = seqStorePtr->mlCode; + ZSTD_symbolEncodingTypeStats_t stats; + var _ = &stats; + + stats.lastCountSize = 0; + ZSTD_seqToCodes(seqStorePtr); + assert(op <= oend); + assert(nbSeq != 0); + + { + uint max = 35; + nuint mostFrequent = HIST_countFast_wksp(countWorkspace, &max, (void*)llCodeTable, nbSeq, entropyWorkspace, entropyWkspSize); + + nextEntropy->litlength_repeatMode = prevEntropy->litlength_repeatMode; + stats.LLtype = (uint)(ZSTD_selectEncodingType(&nextEntropy->litlength_repeatMode, countWorkspace, max, mostFrequent, nbSeq, 9, (uint*)prevEntropy->litlengthCTable, (short*)LL_defaultNorm, LL_defaultNormLog, ZSTD_defaultPolicy_e.ZSTD_defaultAllowed, strategy)); + assert(symbolEncodingType_e.set_basic < symbolEncodingType_e.set_compressed && symbolEncodingType_e.set_rle < symbolEncodingType_e.set_compressed); + assert(!(stats.LLtype < (uint)symbolEncodingType_e.set_compressed && nextEntropy->litlength_repeatMode != FSE_repeat.FSE_repeat_none)); + + { + nuint countSize = ZSTD_buildCTable((void*)op, (nuint)(oend - op), CTable_LitLength, 9, (symbolEncodingType_e)(stats.LLtype), countWorkspace, max, llCodeTable, nbSeq, (short*)LL_defaultNorm, LL_defaultNormLog, 35, (uint*)prevEntropy->litlengthCTable, (nuint)(1316), entropyWorkspace, entropyWkspSize); + + if ((ERR_isError(countSize)) != 0) + { + stats.size = countSize; + return stats; + } + + if (stats.LLtype == (uint)symbolEncodingType_e.set_compressed) + { + stats.lastCountSize = countSize; + } + + op += countSize; + assert(op <= oend); + } + } + + + { + uint max = 31; + nuint mostFrequent = HIST_countFast_wksp(countWorkspace, &max, (void*)ofCodeTable, nbSeq, entropyWorkspace, entropyWkspSize); + ZSTD_defaultPolicy_e defaultPolicy = (max <= 28) ? ZSTD_defaultPolicy_e.ZSTD_defaultAllowed : ZSTD_defaultPolicy_e.ZSTD_defaultDisallowed; + + nextEntropy->offcode_repeatMode = prevEntropy->offcode_repeatMode; + stats.Offtype = (uint)(ZSTD_selectEncodingType(&nextEntropy->offcode_repeatMode, countWorkspace, max, mostFrequent, nbSeq, 8, (uint*)prevEntropy->offcodeCTable, (short*)OF_defaultNorm, OF_defaultNormLog, defaultPolicy, strategy)); + assert(!(stats.Offtype < (uint)symbolEncodingType_e.set_compressed && nextEntropy->offcode_repeatMode != FSE_repeat.FSE_repeat_none)); + + { + nuint countSize = ZSTD_buildCTable((void*)op, (nuint)(oend - op), CTable_OffsetBits, 8, (symbolEncodingType_e)(stats.Offtype), countWorkspace, max, ofCodeTable, nbSeq, (short*)OF_defaultNorm, OF_defaultNormLog, 28, (uint*)prevEntropy->offcodeCTable, (nuint)(772), entropyWorkspace, entropyWkspSize); + + if ((ERR_isError(countSize)) != 0) + { + stats.size = countSize; + return stats; + } + + if (stats.Offtype == (uint)symbolEncodingType_e.set_compressed) + { + stats.lastCountSize = countSize; + } + + op += countSize; + assert(op <= oend); + } + } + + + { + uint max = 52; + nuint mostFrequent = HIST_countFast_wksp(countWorkspace, &max, (void*)mlCodeTable, nbSeq, entropyWorkspace, entropyWkspSize); + + nextEntropy->matchlength_repeatMode = prevEntropy->matchlength_repeatMode; + stats.MLtype = (uint)(ZSTD_selectEncodingType(&nextEntropy->matchlength_repeatMode, countWorkspace, max, mostFrequent, nbSeq, 9, (uint*)prevEntropy->matchlengthCTable, (short*)ML_defaultNorm, ML_defaultNormLog, ZSTD_defaultPolicy_e.ZSTD_defaultAllowed, strategy)); + assert(!(stats.MLtype < (uint)symbolEncodingType_e.set_compressed && nextEntropy->matchlength_repeatMode != FSE_repeat.FSE_repeat_none)); + + { + nuint countSize = ZSTD_buildCTable((void*)op, (nuint)(oend - op), CTable_MatchLength, 9, (symbolEncodingType_e)(stats.MLtype), countWorkspace, max, mlCodeTable, nbSeq, (short*)ML_defaultNorm, ML_defaultNormLog, 52, (uint*)prevEntropy->matchlengthCTable, (nuint)(1452), entropyWorkspace, entropyWkspSize); + + if ((ERR_isError(countSize)) != 0) + { + stats.size = countSize; + return stats; + } + + if (stats.MLtype == (uint)symbolEncodingType_e.set_compressed) + { + stats.lastCountSize = countSize; + } + + op += countSize; + assert(op <= oend); + } + } + + stats.size = (nuint)(op - ostart); + return stats; + } + + /* ZSTD_entropyCompressSeqStore_internal(): + * compresses both literals and sequences + * Returns compressed size of block, or a zstd error. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_entropyCompressSeqStore_internal(seqStore_t* seqStorePtr, ZSTD_entropyCTables_t* prevEntropy, ZSTD_entropyCTables_t* nextEntropy, ZSTD_CCtx_params_s* cctxParams, void* dst, nuint dstCapacity, void* entropyWorkspace, nuint entropyWkspSize, int bmi2) + { + int longOffsets = ((cctxParams->cParams.windowLog > ((uint)(MEM_32bits ? 25 : 57))) ? 1 : 0); + ZSTD_strategy strategy = cctxParams->cParams.strategy; + uint* count = (uint*)(entropyWorkspace); + uint* CTable_LitLength = (uint*)nextEntropy->fse.litlengthCTable; + uint* CTable_OffsetBits = (uint*)nextEntropy->fse.offcodeCTable; + uint* CTable_MatchLength = (uint*)nextEntropy->fse.matchlengthCTable; + seqDef_s* sequences = seqStorePtr->sequencesStart; + nuint nbSeq = (nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + byte* ofCodeTable = seqStorePtr->ofCode; + byte* llCodeTable = seqStorePtr->llCode; + byte* mlCodeTable = seqStorePtr->mlCode; + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstCapacity; + byte* op = ostart; + nuint lastCountSize; + + entropyWorkspace = count + (((35) > (52) ? (35) : (52)) + 1); + entropyWkspSize -= (uint)((((35) > (52) ? (35) : (52)) + 1)) * (nuint)(sizeof(uint)); + assert(entropyWkspSize >= (uint)(((6 << 10) + 256))); + + { + byte* literals = seqStorePtr->litStart; + nuint litSize = (nuint)(seqStorePtr->lit - literals); + nuint cSize = ZSTD_compressLiterals(&prevEntropy->huf, &nextEntropy->huf, cctxParams->cParams.strategy, ZSTD_disableLiteralsCompression(cctxParams), (void*)op, dstCapacity, (void*)literals, litSize, entropyWorkspace, entropyWkspSize, bmi2); + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(cSize <= dstCapacity); + op += cSize; + } + + if ((oend - op) < 3 + 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (nbSeq < 128) + { + *op++ = (byte)(nbSeq); + } + else if (nbSeq < 0x7F00) + { + op[0] = (byte)((nbSeq >> 8) + 0x80); + op[1] = (byte)(nbSeq); + op += 2; + } + else + { + op[0] = 0xFF; + MEM_writeLE16((void*)(op + 1), (ushort)(nbSeq - 0x7F00)); + op += 3; + } + + assert(op <= oend); + if (nbSeq == 0) + { + memcpy((void*)(&nextEntropy->fse), (void*)(&prevEntropy->fse), ((nuint)(sizeof(ZSTD_fseCTables_t)))); + return (nuint)(op - ostart); + } + + + { + ZSTD_symbolEncodingTypeStats_t stats; + byte* seqHead = op++; + + stats = ZSTD_buildSequencesStatistics(seqStorePtr, nbSeq, &prevEntropy->fse, &nextEntropy->fse, op, oend, strategy, count, entropyWorkspace, entropyWkspSize); + + { + nuint err_code = (stats.size); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + *seqHead = (byte)((stats.LLtype << 6) + (stats.Offtype << 4) + (stats.MLtype << 2)); + lastCountSize = stats.lastCountSize; + op += stats.size; + } + + + { + nuint bitstreamSize = ZSTD_encodeSequences((void*)op, (nuint)(oend - op), CTable_MatchLength, mlCodeTable, CTable_OffsetBits, ofCodeTable, CTable_LitLength, llCodeTable, sequences, nbSeq, longOffsets, bmi2); + + + { + nuint err_code = (bitstreamSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + op += bitstreamSize; + assert(op <= oend); + if (lastCountSize != 0 && (lastCountSize + bitstreamSize) < 4) + { + assert(lastCountSize + bitstreamSize == 3); + return 0; + } + } + + return (nuint)(op - ostart); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_entropyCompressSeqStore(seqStore_t* seqStorePtr, ZSTD_entropyCTables_t* prevEntropy, ZSTD_entropyCTables_t* nextEntropy, ZSTD_CCtx_params_s* cctxParams, void* dst, nuint dstCapacity, nuint srcSize, void* entropyWorkspace, nuint entropyWkspSize, int bmi2) + { + nuint cSize = ZSTD_entropyCompressSeqStore_internal(seqStorePtr, prevEntropy, nextEntropy, cctxParams, dst, dstCapacity, entropyWorkspace, entropyWkspSize, bmi2); + + if (cSize == 0) + { + return 0; + } + + if (((cSize == (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)))) && (srcSize <= dstCapacity))) + { + return 0; + } + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint maxCSize = srcSize - ZSTD_minGain(srcSize, cctxParams->cParams.strategy); + + if (cSize >= maxCSize) + { + return 0; + } + } + + return cSize; + } + + /* ZSTD_selectBlockCompressor() : + * Not static, but internal use only (used by long distance matcher) + * assumption : strat is a valid strategy */ + public static ZSTD_blockCompressor? ZSTD_selectBlockCompressor(ZSTD_strategy strat, ZSTD_useRowMatchFinderMode_e useRowMatchFinder, ZSTD_dictMode_e dictMode) + { + + ZSTD_blockCompressor? selectedCompressor; + + assert((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_strategy, (int)strat)) != 0); + if ((ZSTD_rowMatchFinderUsed(strat, useRowMatchFinder)) != 0) + { + + + assert(useRowMatchFinder != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + selectedCompressor = rowBasedBlockCompressors[(int)(dictMode)][(int)(strat) - (int)(ZSTD_strategy.ZSTD_greedy)]; + } + else + { + selectedCompressor = blockCompressor[(int)(dictMode)][(int)(strat)]; + } + + assert(selectedCompressor != null); + return selectedCompressor; + } + + private static void ZSTD_storeLastLiterals(seqStore_t* seqStorePtr, byte* anchor, nuint lastLLSize) + { + memcpy((void*)(seqStorePtr->lit), (void*)(anchor), (lastLLSize)); + seqStorePtr->lit += lastLLSize; + } + + public static void ZSTD_resetSeqStore(seqStore_t* ssPtr) + { + ssPtr->lit = ssPtr->litStart; + ssPtr->sequences = ssPtr->sequencesStart; + ssPtr->longLengthType = ZSTD_longLengthType_e.ZSTD_llt_none; + } + + private static nuint ZSTD_buildSeqStore(ZSTD_CCtx_s* zc, void* src, nuint srcSize) + { + ZSTD_matchState_t* ms = &zc->blockState.matchState; + + assert(srcSize <= (uint)((1 << 17))); + ZSTD_assertEqualCParams(zc->appliedParams.cParams, ms->cParams); + if (srcSize < (uint)((1 + 1 + 1)) + ZSTD_blockHeaderSize + 1) + { + if (zc->appliedParams.cParams.strategy >= ZSTD_strategy.ZSTD_btopt) + { + ZSTD_ldm_skipRawSeqStoreBytes(&zc->externSeqStore, srcSize); + } + else + { + ZSTD_ldm_skipSequences(&zc->externSeqStore, srcSize, zc->appliedParams.cParams.minMatch); + } + + return (nuint)ZSTD_buildSeqStore_e.ZSTDbss_noCompress; + } + + ZSTD_resetSeqStore(&(zc->seqStore)); + ms->opt.symbolCosts = &zc->blockState.prevCBlock->entropy; + ms->opt.literalCompressionMode = zc->appliedParams.literalCompressionMode; + assert(ms->dictMatchState == null || ms->loadedDictEnd == ms->window.dictLimit); + + { + byte* @base = ms->window.@base; + byte* istart = (byte*)(src); + uint curr = (uint)(istart - @base); + + if ((nuint)(sizeof(nint)) == 8) + { + assert(istart - @base < unchecked((nint)(unchecked((uint)(-1))))); + } + + if (curr > ms->nextToUpdate + 384) + { + ms->nextToUpdate = curr - (uint)((192) < ((uint)(curr - ms->nextToUpdate - 384)) ? (192) : ((uint)(curr - ms->nextToUpdate - 384))); + } + } + + + { + ZSTD_dictMode_e dictMode = ZSTD_matchState_dictMode(ms); + nuint lastLLSize; + + + { + int i; + + for (i = 0; i < 3; ++i) + { + zc->blockState.nextCBlock->rep[i] = zc->blockState.prevCBlock->rep[i]; + } + } + + if (zc->externSeqStore.pos < zc->externSeqStore.size) + { + assert(zc->appliedParams.ldmParams.enableLdm == 0); + lastLLSize = ZSTD_ldm_blockCompress(&zc->externSeqStore, ms, &zc->seqStore, zc->blockState.nextCBlock->rep, zc->appliedParams.useRowMatchFinder, src, srcSize); + assert(zc->externSeqStore.pos <= zc->externSeqStore.size); + } + else if (zc->appliedParams.ldmParams.enableLdm != 0) + { + rawSeqStore_t ldmSeqStore = kNullRawSeqStore; + + ldmSeqStore.seq = zc->ldmSequences; + ldmSeqStore.capacity = zc->maxNbLdmSequences; + + { + nuint err_code = (ZSTD_ldm_generateSequences(&zc->ldmState, &ldmSeqStore, &zc->appliedParams.ldmParams, src, srcSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + lastLLSize = ZSTD_ldm_blockCompress(&ldmSeqStore, ms, &zc->seqStore, zc->blockState.nextCBlock->rep, zc->appliedParams.useRowMatchFinder, src, srcSize); + assert(ldmSeqStore.pos == ldmSeqStore.size); + } + else + { + var blockCompressor = ZSTD_selectBlockCompressor(zc -> appliedParams.cParams.strategy, zc -> appliedParams.useRowMatchFinder, dictMode) ?? throw new InvalidOperationException(); + + ms->ldmSeqStore = null; + lastLLSize = blockCompressor(ms, &zc->seqStore, zc->blockState.nextCBlock->rep, src, srcSize); + } + + + { + byte* lastLiterals = (byte*)(src) + srcSize - lastLLSize; + + ZSTD_storeLastLiterals(&zc->seqStore, lastLiterals, lastLLSize); + } + } + + return (nuint)ZSTD_buildSeqStore_e.ZSTDbss_compress; + } + + private static void ZSTD_copyBlockSequences(ZSTD_CCtx_s* zc) + { + seqStore_t* seqStore = ZSTD_getSeqStore(zc); + seqDef_s* seqStoreSeqs = seqStore->sequencesStart; + nuint seqStoreSeqSize = (nuint)(seqStore->sequences - seqStoreSeqs); + nuint seqStoreLiteralsSize = (nuint)(seqStore->lit - seqStore->litStart); + nuint literalsRead = 0; + nuint lastLLSize; + ZSTD_Sequence* outSeqs = &zc->seqCollector.seqStart[zc->seqCollector.seqIndex]; + nuint i; + repcodes_s updatedRepcodes; + + assert(zc->seqCollector.seqIndex + 1 < zc->seqCollector.maxSequences); + assert(zc->seqCollector.maxSequences >= seqStoreSeqSize + 1); + memcpy((void*)(updatedRepcodes.rep), (void*)(zc->blockState.prevCBlock->rep), ((nuint)(sizeof(repcodes_s)))); + for (i = 0; i < seqStoreSeqSize; ++i) + { + uint rawOffset = seqStoreSeqs[i].offset - 3; + + outSeqs[i].litLength = seqStoreSeqs[i].litLength; + outSeqs[i].matchLength = (uint)(seqStoreSeqs[i].matchLength + 3); + outSeqs[i].rep = 0; + if (i == seqStore->longLengthPos) + { + if (seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_literalLength) + { + outSeqs[i].litLength += 0x10000; + } + else if (seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_matchLength) + { + outSeqs[i].matchLength += 0x10000; + } + } + + if (seqStoreSeqs[i].offset <= 3) + { + outSeqs[i].rep = seqStoreSeqs[i].offset; + if (outSeqs[i].litLength != 0) + { + rawOffset = updatedRepcodes.rep[outSeqs[i].rep - 1]; + } + else + { + if (outSeqs[i].rep == 3) + { + rawOffset = updatedRepcodes.rep[0] - 1; + } + else + { + rawOffset = updatedRepcodes.rep[outSeqs[i].rep]; + } + } + } + + outSeqs[i].offset = rawOffset; + updatedRepcodes = ZSTD_updateRep(updatedRepcodes.rep, seqStoreSeqs[i].offset - 1, ((seqStoreSeqs[i].litLength == 0) ? 1U : 0U)); + literalsRead += outSeqs[i].litLength; + } + + assert(seqStoreLiteralsSize >= literalsRead); + lastLLSize = seqStoreLiteralsSize - literalsRead; + outSeqs[i].litLength = (uint)(lastLLSize); + outSeqs[i].matchLength = outSeqs[i].offset = outSeqs[i].rep = 0; + seqStoreSeqSize++; + zc->seqCollector.seqIndex += seqStoreSeqSize; + } + + /*! ZSTD_generateSequences() : + * Generate sequences using ZSTD_compress2, given a source buffer. + * + * Each block will end with a dummy sequence + * with offset == 0, matchLength == 0, and litLength == length of last literals. + * litLength may be == 0, and if so, then the sequence of (of: 0 ml: 0 ll: 0) + * simply acts as a block delimiter. + * + * zc can be used to insert custom compression params. + * This function invokes ZSTD_compress2 + * + * The output of this function can be fed into ZSTD_compressSequences() with CCtx + * setting of ZSTD_c_blockDelimiters as ZSTD_sf_explicitBlockDelimiters + * @return : number of sequences generated + */ + public static nuint ZSTD_generateSequences(ZSTD_CCtx_s* zc, ZSTD_Sequence* outSeqs, nuint outSeqsSize, void* src, nuint srcSize) + { + nuint dstCapacity = ZSTD_compressBound(srcSize); + void* dst = ZSTD_customMalloc(dstCapacity, ZSTD_defaultCMem); + SeqCollector seqCollector; + + if (dst == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + seqCollector.collectSequences = 1; + seqCollector.seqStart = outSeqs; + seqCollector.seqIndex = 0; + seqCollector.maxSequences = outSeqsSize; + zc->seqCollector = seqCollector; + ZSTD_compress2(zc, dst, dstCapacity, src, srcSize); + ZSTD_customFree(dst, ZSTD_defaultCMem); + return zc->seqCollector.seqIndex; + } + + /*! ZSTD_mergeBlockDelimiters() : + * Given an array of ZSTD_Sequence, remove all sequences that represent block delimiters/last literals + * by merging them into into the literals of the next sequence. + * + * As such, the final generated result has no explicit representation of block boundaries, + * and the final last literals segment is not represented in the sequences. + * + * The output of this function can be fed into ZSTD_compressSequences() with CCtx + * setting of ZSTD_c_blockDelimiters as ZSTD_sf_noBlockDelimiters + * @return : number of sequences left after merging + */ + public static nuint ZSTD_mergeBlockDelimiters(ZSTD_Sequence* sequences, nuint seqsSize) + { + nuint @in = 0; + nuint @out = 0; + + for (; @in < seqsSize; ++@in) + { + if (sequences[@in].offset == 0 && sequences[@in].matchLength == 0) + { + if (@in != seqsSize - 1) + { + sequences[@in + 1].litLength += sequences[@in].litLength; + } + } + else + { + sequences[@out] = sequences[@in]; + ++@out; + } + } + + return @out; + } + + /* Unrolled loop to read four size_ts of input at a time. Returns 1 if is RLE, 0 if not. */ + private static int ZSTD_isRLE(byte* src, nuint length) + { + byte* ip = src; + byte value = ip[0]; + nuint valueST = (nuint)((ulong)(value) * 0x0101010101010101UL); + nuint unrollSize = (nuint)(sizeof(nuint)) * 4; + nuint unrollMask = unrollSize - 1; + nuint prefixLength = length & unrollMask; + nuint i; + nuint u; + + if (length == 1) + { + return 1; + } + + if (prefixLength != 0 && ZSTD_count(ip + 1, ip, ip + prefixLength) != prefixLength - 1) + { + return 0; + } + + for (i = prefixLength; i != length; i += unrollSize) + { + for (u = 0; u < unrollSize; u += (nuint)(sizeof(nuint))) + { + if (MEM_readST((void*)(ip + i + u)) != valueST) + { + return 0; + } + } + } + + return 1; + } + + /* Returns true if the given block may be RLE. + * This is just a heuristic based on the compressibility. + * It may return both false positives and false negatives. + */ + private static int ZSTD_maybeRLE(seqStore_t* seqStore) + { + nuint nbSeqs = (nuint)(seqStore->sequences - seqStore->sequencesStart); + nuint nbLits = (nuint)(seqStore->lit - seqStore->litStart); + + return ((nbSeqs < 4 && nbLits < 10) ? 1 : 0); + } + + private static void ZSTD_blockState_confirmRepcodesAndEntropyTables(ZSTD_blockState_t* bs) + { + ZSTD_compressedBlockState_t* tmp = bs->prevCBlock; + + bs->prevCBlock = bs->nextCBlock; + bs->nextCBlock = tmp; + } + + /* Writes the block header */ + private static void writeBlockHeader(void* op, nuint cSize, nuint blockSize, uint lastBlock) + { + uint cBlockHeader = cSize == 1 ? lastBlock + (((uint)(blockType_e.bt_rle)) << 1) + (uint)(blockSize << 3) : lastBlock + (((uint)(blockType_e.bt_compressed)) << 1) + (uint)(cSize << 3); + + MEM_writeLE24(op, cBlockHeader); + } + + /** ZSTD_buildBlockEntropyStats_literals() : + * Builds entropy for the literals. + * Stores literals block type (raw, rle, compressed, repeat) and + * huffman description table to hufMetadata. + * Requires ENTROPY_WORKSPACE_SIZE workspace + * @return : size of huffman description table or error code */ + private static nuint ZSTD_buildBlockEntropyStats_literals(void* src, nuint srcSize, ZSTD_hufCTables_t* prevHuf, ZSTD_hufCTables_t* nextHuf, ZSTD_hufCTablesMetadata_t* hufMetadata, int disableLiteralsCompression, void* workspace, nuint wkspSize) + { + byte* wkspStart = (byte*)(workspace); + byte* wkspEnd = wkspStart + wkspSize; + byte* countWkspStart = wkspStart; + uint* countWksp = (uint*)(workspace); + nuint countWkspSize = (uint)((255 + 1)) * (nuint)(4); + byte* nodeWksp = countWkspStart + countWkspSize; + nuint nodeWkspSize = (nuint)(wkspEnd - nodeWksp); + uint maxSymbolValue = 255; + uint huffLog = 11; + HUF_repeat repeat = prevHuf->repeatMode; + + memcpy((void*)(nextHuf), (void*)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); + if (disableLiteralsCompression != 0) + { + hufMetadata->hType = symbolEncodingType_e.set_basic; + return 0; + } + + + { + nuint minLitSize = (nuint)((prevHuf->repeatMode == HUF_repeat.HUF_repeat_valid) ? 6 : 63); + + if (srcSize <= minLitSize) + { + hufMetadata->hType = symbolEncodingType_e.set_basic; + return 0; + } + } + + + { + nuint largest = HIST_count_wksp(countWksp, &maxSymbolValue, (void*)(byte*)(src), srcSize, workspace, wkspSize); + + + { + nuint err_code = (largest); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (largest == srcSize) + { + hufMetadata->hType = symbolEncodingType_e.set_rle; + return 0; + } + + if (largest <= (srcSize >> 7) + 4) + { + hufMetadata->hType = symbolEncodingType_e.set_basic; + return 0; + } + } + + if (repeat == HUF_repeat.HUF_repeat_check && (HUF_validateCTable((HUF_CElt_s*)(prevHuf->CTable), countWksp, maxSymbolValue)) == 0) + { + repeat = HUF_repeat.HUF_repeat_none; + } + + memset((void*)(nextHuf->CTable), (0), ((nuint)(sizeof(HUF_CElt_s) * 256))); + huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue); + + { + nuint maxBits = HUF_buildCTable_wksp((HUF_CElt_s*)(nextHuf->CTable), countWksp, maxSymbolValue, huffLog, (void*)nodeWksp, nodeWkspSize); + + + { + nuint err_code = (maxBits); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + huffLog = (uint)(maxBits); + + { + nuint newCSize = HUF_estimateCompressedSize((HUF_CElt_s*)(nextHuf->CTable), countWksp, maxSymbolValue); + nuint hSize = HUF_writeCTable_wksp((void*)hufMetadata->hufDesBuffer, (nuint)(128), (HUF_CElt_s*)(nextHuf->CTable), maxSymbolValue, huffLog, (void*)nodeWksp, nodeWkspSize); + + if (repeat != HUF_repeat.HUF_repeat_none) + { + nuint oldCSize = HUF_estimateCompressedSize((HUF_CElt_s*)(prevHuf->CTable), countWksp, maxSymbolValue); + + if (oldCSize < srcSize && (oldCSize <= hSize + newCSize || hSize + 12 >= srcSize)) + { + memcpy((void*)(nextHuf), (void*)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); + hufMetadata->hType = symbolEncodingType_e.set_repeat; + return 0; + } + } + + if (newCSize + hSize >= srcSize) + { + memcpy((void*)(nextHuf), (void*)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); + hufMetadata->hType = symbolEncodingType_e.set_basic; + return 0; + } + + hufMetadata->hType = symbolEncodingType_e.set_compressed; + nextHuf->repeatMode = HUF_repeat.HUF_repeat_check; + return hSize; + } + } + } + + /* ZSTD_buildDummySequencesStatistics(): + * Returns a ZSTD_symbolEncodingTypeStats_t with all encoding types as set_basic, + * and updates nextEntropy to the appropriate repeatMode. + */ + private static ZSTD_symbolEncodingTypeStats_t ZSTD_buildDummySequencesStatistics(ZSTD_fseCTables_t* nextEntropy) + { + ZSTD_symbolEncodingTypeStats_t stats = new ZSTD_symbolEncodingTypeStats_t + { + LLtype = (uint)symbolEncodingType_e.set_basic, + Offtype = (uint)symbolEncodingType_e.set_basic, + MLtype = (uint)symbolEncodingType_e.set_basic, + size = 0, + lastCountSize = 0, + }; + + nextEntropy->litlength_repeatMode = FSE_repeat.FSE_repeat_none; + nextEntropy->offcode_repeatMode = FSE_repeat.FSE_repeat_none; + nextEntropy->matchlength_repeatMode = FSE_repeat.FSE_repeat_none; + return stats; + } + + /** ZSTD_buildBlockEntropyStats_sequences() : + * Builds entropy for the sequences. + * Stores symbol compression modes and fse table to fseMetadata. + * Requires ENTROPY_WORKSPACE_SIZE wksp. + * @return : size of fse tables or error code */ + private static nuint ZSTD_buildBlockEntropyStats_sequences(seqStore_t* seqStorePtr, ZSTD_fseCTables_t* prevEntropy, ZSTD_fseCTables_t* nextEntropy, ZSTD_CCtx_params_s* cctxParams, ZSTD_fseCTablesMetadata_t* fseMetadata, void* workspace, nuint wkspSize) + { + ZSTD_strategy strategy = cctxParams->cParams.strategy; + nuint nbSeq = (nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + byte* ostart = (byte*)fseMetadata->fseTablesBuffer; + byte* oend = ostart + (nuint)(133); + byte* op = ostart; + uint* countWorkspace = (uint*)(workspace); + uint* entropyWorkspace = countWorkspace + (((35) > (52) ? (35) : (52)) + 1); + nuint entropyWorkspaceSize = wkspSize - (uint)((((35) > (52) ? (35) : (52)) + 1)) * (nuint)(4); + ZSTD_symbolEncodingTypeStats_t stats; + + stats = nbSeq != 0 ? ZSTD_buildSequencesStatistics(seqStorePtr, nbSeq, prevEntropy, nextEntropy, op, oend, strategy, countWorkspace, (void*)entropyWorkspace, entropyWorkspaceSize) : ZSTD_buildDummySequencesStatistics(nextEntropy); + + { + nuint err_code = (stats.size); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + fseMetadata->llType = (symbolEncodingType_e)(stats.LLtype); + fseMetadata->ofType = (symbolEncodingType_e)(stats.Offtype); + fseMetadata->mlType = (symbolEncodingType_e)(stats.MLtype); + fseMetadata->lastCountSize = stats.lastCountSize; + return stats.size; + } + + /** ZSTD_buildBlockEntropyStats() : + * Builds entropy for the block. + * Requires workspace size ENTROPY_WORKSPACE_SIZE + * + * @return : 0 on success or error code + */ + public static nuint ZSTD_buildBlockEntropyStats(seqStore_t* seqStorePtr, ZSTD_entropyCTables_t* prevEntropy, ZSTD_entropyCTables_t* nextEntropy, ZSTD_CCtx_params_s* cctxParams, ZSTD_entropyCTablesMetadata_t* entropyMetadata, void* workspace, nuint wkspSize) + { + nuint litSize = (nuint)(seqStorePtr->lit - seqStorePtr->litStart); + + entropyMetadata->hufMetadata.hufDesSize = ZSTD_buildBlockEntropyStats_literals((void*)seqStorePtr->litStart, litSize, &prevEntropy->huf, &nextEntropy->huf, &entropyMetadata->hufMetadata, ZSTD_disableLiteralsCompression(cctxParams), workspace, wkspSize); + + { + nuint err_code = (entropyMetadata->hufMetadata.hufDesSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + entropyMetadata->fseMetadata.fseTablesSize = ZSTD_buildBlockEntropyStats_sequences(seqStorePtr, &prevEntropy->fse, &nextEntropy->fse, cctxParams, &entropyMetadata->fseMetadata, workspace, wkspSize); + + { + nuint err_code = (entropyMetadata->fseMetadata.fseTablesSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /* Returns the size estimate for the literals section (header + content) of a block */ + private static nuint ZSTD_estimateBlockSize_literal(byte* literals, nuint litSize, ZSTD_hufCTables_t* huf, ZSTD_hufCTablesMetadata_t* hufMetadata, void* workspace, nuint wkspSize, int writeEntropy) + { + uint* countWksp = (uint*)(workspace); + uint maxSymbolValue = 255; + nuint literalSectionHeaderSize = (nuint)(3 + ((litSize >= (uint)(1 * (1 << 10))) ? 1 : 0) + ((litSize >= (uint)(16 * (1 << 10))) ? 1 : 0)); + uint singleStream = ((litSize < 256) ? 1U : 0U); + + if (hufMetadata->hType == symbolEncodingType_e.set_basic) + { + return litSize; + } + else if (hufMetadata->hType == symbolEncodingType_e.set_rle) + { + return 1; + } + else if (hufMetadata->hType == symbolEncodingType_e.set_compressed || hufMetadata->hType == symbolEncodingType_e.set_repeat) + { + nuint largest = HIST_count_wksp(countWksp, &maxSymbolValue, (void*)(byte*)(literals), litSize, workspace, wkspSize); + + if ((ERR_isError(largest)) != 0) + { + return litSize; + } + + + { + nuint cLitSizeEstimate = HUF_estimateCompressedSize((HUF_CElt_s*)(huf->CTable), countWksp, maxSymbolValue); + + if (writeEntropy != 0) + { + cLitSizeEstimate += hufMetadata->hufDesSize; + } + + if (singleStream == 0) + { + cLitSizeEstimate += 6; + } + + return cLitSizeEstimate + literalSectionHeaderSize; + } + } + + assert(0 != 0); + return 0; + } + + /* Returns the size estimate for the FSE-compressed symbols (of, ml, ll) of a block */ + private static nuint ZSTD_estimateBlockSize_symbolType(symbolEncodingType_e type, byte* codeTable, nuint nbSeq, uint maxCode, uint* fseCTable, uint* additionalBits, short* defaultNorm, uint defaultNormLog, uint defaultMax, void* workspace, nuint wkspSize) + { + uint* countWksp = (uint*)(workspace); + byte* ctp = codeTable; + byte* ctStart = ctp; + byte* ctEnd = ctStart + nbSeq; + nuint cSymbolTypeSizeEstimateInBits = 0; + uint max = maxCode; + + HIST_countFast_wksp(countWksp, &max, (void*)codeTable, nbSeq, workspace, wkspSize); + if (type == symbolEncodingType_e.set_basic) + { + assert(max <= defaultMax); + cSymbolTypeSizeEstimateInBits = ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, countWksp, max); + } + else if (type == symbolEncodingType_e.set_rle) + { + cSymbolTypeSizeEstimateInBits = 0; + } + else if (type == symbolEncodingType_e.set_compressed || type == symbolEncodingType_e.set_repeat) + { + cSymbolTypeSizeEstimateInBits = ZSTD_fseBitCost(fseCTable, countWksp, max); + } + + if ((ERR_isError(cSymbolTypeSizeEstimateInBits)) != 0) + { + return nbSeq * 10; + } + + while (ctp < ctEnd) + { + if (additionalBits != null) + { + cSymbolTypeSizeEstimateInBits += additionalBits[*ctp]; + } + else + { + cSymbolTypeSizeEstimateInBits += *ctp; + } + + ctp++; + } + + return cSymbolTypeSizeEstimateInBits >> 3; + } + + /* Returns the size estimate for the sequences section (header + content) of a block */ + private static nuint ZSTD_estimateBlockSize_sequences(byte* ofCodeTable, byte* llCodeTable, byte* mlCodeTable, nuint nbSeq, ZSTD_fseCTables_t* fseTables, ZSTD_fseCTablesMetadata_t* fseMetadata, void* workspace, nuint wkspSize, int writeEntropy) + { + nuint sequencesSectionHeaderSize = (nuint)(1 + 1 + ((nbSeq >= 128) ? 1 : 0) + ((nbSeq >= 0x7F00) ? 1 : 0)); + nuint cSeqSizeEstimate = 0; + + cSeqSizeEstimate += ZSTD_estimateBlockSize_symbolType(fseMetadata->ofType, ofCodeTable, nbSeq, 31, (uint*)fseTables->offcodeCTable, (uint*)null, (short*)OF_defaultNorm, OF_defaultNormLog, 28, workspace, wkspSize); + cSeqSizeEstimate += ZSTD_estimateBlockSize_symbolType(fseMetadata->llType, llCodeTable, nbSeq, 35, (uint*)fseTables->litlengthCTable, (uint*)LL_bits, (short*)LL_defaultNorm, LL_defaultNormLog, 35, workspace, wkspSize); + cSeqSizeEstimate += ZSTD_estimateBlockSize_symbolType(fseMetadata->mlType, mlCodeTable, nbSeq, 52, (uint*)fseTables->matchlengthCTable, (uint*)ML_bits, (short*)ML_defaultNorm, ML_defaultNormLog, 52, workspace, wkspSize); + if (writeEntropy != 0) + { + cSeqSizeEstimate += fseMetadata->fseTablesSize; + } + + return cSeqSizeEstimate + sequencesSectionHeaderSize; + } + + /* Returns the size estimate for a given stream of literals, of, ll, ml */ + private static nuint ZSTD_estimateBlockSize(byte* literals, nuint litSize, byte* ofCodeTable, byte* llCodeTable, byte* mlCodeTable, nuint nbSeq, ZSTD_entropyCTables_t* entropy, ZSTD_entropyCTablesMetadata_t* entropyMetadata, void* workspace, nuint wkspSize, int writeLitEntropy, int writeSeqEntropy) + { + nuint literalsSize = ZSTD_estimateBlockSize_literal(literals, litSize, &entropy->huf, &entropyMetadata->hufMetadata, workspace, wkspSize, writeLitEntropy); + nuint seqSize = ZSTD_estimateBlockSize_sequences(ofCodeTable, llCodeTable, mlCodeTable, nbSeq, &entropy->fse, &entropyMetadata->fseMetadata, workspace, wkspSize, writeSeqEntropy); + + return seqSize + literalsSize + ZSTD_blockHeaderSize; + } + + /* Builds entropy statistics and uses them for blocksize estimation. + * + * Returns the estimated compressed size of the seqStore, or a zstd error. + */ + private static nuint ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize(seqStore_t* seqStore, ZSTD_CCtx_s* zc) + { + ZSTD_entropyCTablesMetadata_t entropyMetadata; + + + { + nuint err_code = (ZSTD_buildBlockEntropyStats(seqStore, &zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy, &zc->appliedParams, &entropyMetadata, (void*)zc->entropyWorkspace, ((uint)(((6 << 10) + 256)) + ((nuint)(4) * (uint)((((35) > (52) ? (35) : (52)) + 2)))))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_estimateBlockSize(seqStore->litStart, (nuint)(seqStore->lit - seqStore->litStart), seqStore->ofCode, seqStore->llCode, seqStore->mlCode, (nuint)(seqStore->sequences - seqStore->sequencesStart), &zc->blockState.nextCBlock->entropy, &entropyMetadata, (void*)zc->entropyWorkspace, ((uint)(((6 << 10) + 256)) + ((nuint)(sizeof(uint)) * (uint)((((35) > (52) ? (35) : (52)) + 2)))), (entropyMetadata.hufMetadata.hType == symbolEncodingType_e.set_compressed ? 1 : 0), 1); + } + + /* Returns literals bytes represented in a seqStore */ + private static nuint ZSTD_countSeqStoreLiteralsBytes(seqStore_t* seqStore) + { + nuint literalsBytes = 0; + nuint nbSeqs = (nuint)(seqStore->sequences - seqStore->sequencesStart); + nuint i; + + for (i = 0; i < nbSeqs; ++i) + { + seqDef_s seq = seqStore->sequencesStart[i]; + + literalsBytes += seq.litLength; + if (i == seqStore->longLengthPos && seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_literalLength) + { + literalsBytes += 0x10000; + } + } + + return literalsBytes; + } + + /* Returns match bytes represented in a seqStore */ + private static nuint ZSTD_countSeqStoreMatchBytes(seqStore_t* seqStore) + { + nuint matchBytes = 0; + nuint nbSeqs = (nuint)(seqStore->sequences - seqStore->sequencesStart); + nuint i; + + for (i = 0; i < nbSeqs; ++i) + { + seqDef_s seq = seqStore->sequencesStart[i]; + + matchBytes += (nuint)(seq.matchLength + 3); + if (i == seqStore->longLengthPos && seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_matchLength) + { + matchBytes += 0x10000; + } + } + + return matchBytes; + } + + /* Derives the seqStore that is a chunk of the originalSeqStore from [startIdx, endIdx). + * Stores the result in resultSeqStore. + */ + private static void ZSTD_deriveSeqStoreChunk(seqStore_t* resultSeqStore, seqStore_t* originalSeqStore, nuint startIdx, nuint endIdx) + { + byte* litEnd = originalSeqStore->lit; + nuint literalsBytes; + nuint literalsBytesPreceding = 0; + + *resultSeqStore = *originalSeqStore; + if (startIdx > 0) + { + resultSeqStore->sequences = originalSeqStore->sequencesStart + startIdx; + literalsBytesPreceding = ZSTD_countSeqStoreLiteralsBytes(resultSeqStore); + } + + if (originalSeqStore->longLengthType != ZSTD_longLengthType_e.ZSTD_llt_none) + { + if (originalSeqStore->longLengthPos < startIdx || originalSeqStore->longLengthPos > endIdx) + { + resultSeqStore->longLengthType = ZSTD_longLengthType_e.ZSTD_llt_none; + } + else + { + resultSeqStore->longLengthPos -= (uint)(startIdx); + } + } + + resultSeqStore->sequencesStart = originalSeqStore->sequencesStart + startIdx; + resultSeqStore->sequences = originalSeqStore->sequencesStart + endIdx; + literalsBytes = ZSTD_countSeqStoreLiteralsBytes(resultSeqStore); + resultSeqStore->litStart += literalsBytesPreceding; + if (endIdx == (nuint)(originalSeqStore->sequences - originalSeqStore->sequencesStart)) + { + resultSeqStore->lit = litEnd; + } + else + { + resultSeqStore->lit = resultSeqStore->litStart + literalsBytes; + } + + resultSeqStore->llCode += startIdx; + resultSeqStore->mlCode += startIdx; + resultSeqStore->ofCode += startIdx; + } + + /** + * Returns the raw offset represented by the combination of offCode, ll0, and repcode history. + * offCode must be an offCode representing a repcode, therefore in the range of [0, 2]. + */ + private static uint ZSTD_resolveRepcodeToRawOffset(uint* rep, uint offCode, uint ll0) + { + uint adjustedOffCode = offCode + ll0; + + assert(offCode < 3); + if (adjustedOffCode == 3) + { + assert(rep[0] > 0); + return rep[0] - 1; + } + + return rep[adjustedOffCode]; + } + + /** + * ZSTD_seqStore_resolveOffCodes() reconciles any possible divergences in offset history that may arise + * due to emission of RLE/raw blocks that disturb the offset history, and replaces any repcodes within + * the seqStore that may be invalid. + * + * dRepcodes are updated as would be on the decompression side. cRepcodes are updated exactly in + * accordance with the seqStore. + */ + private static void ZSTD_seqStore_resolveOffCodes(repcodes_s* dRepcodes, repcodes_s* cRepcodes, seqStore_t* seqStore, uint nbSeq) + { + uint idx = 0; + + for (; idx < nbSeq; ++idx) + { + seqDef_s* seq = seqStore->sequencesStart + idx; + uint ll0 = (((seq->litLength == 0)) ? 1U : 0U); + uint offCode = seq->offset - 1; + + assert(seq->offset > 0); + if (offCode <= (uint)((3 - 1))) + { + uint dRawOffset = ZSTD_resolveRepcodeToRawOffset(dRepcodes->rep, offCode, ll0); + uint cRawOffset = ZSTD_resolveRepcodeToRawOffset(cRepcodes->rep, offCode, ll0); + + if (dRawOffset != cRawOffset) + { + seq->offset = cRawOffset + 3; + } + } + + *dRepcodes = ZSTD_updateRep(dRepcodes->rep, seq->offset - 1, ll0); + *cRepcodes = ZSTD_updateRep(cRepcodes->rep, offCode, ll0); + } + } + + /* ZSTD_compressSeqStore_singleBlock(): + * Compresses a seqStore into a block with a block header, into the buffer dst. + * + * Returns the total size of that block (including header) or a ZSTD error code. + */ + private static nuint ZSTD_compressSeqStore_singleBlock(ZSTD_CCtx_s* zc, seqStore_t* seqStore, repcodes_s* dRep, repcodes_s* cRep, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint lastBlock, uint isPartition) + { + uint rleMaxLength = 25; + byte* op = (byte*)(dst); + byte* ip = (byte*)(src); + nuint cSize; + nuint cSeqsSize; + repcodes_s dRepOriginal = *dRep; + + if (isPartition != 0) + { + ZSTD_seqStore_resolveOffCodes(dRep, cRep, seqStore, (uint)(seqStore->sequences - seqStore->sequencesStart)); + } + + cSeqsSize = ZSTD_entropyCompressSeqStore(seqStore, &zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy, &zc->appliedParams, (void*)(op + ZSTD_blockHeaderSize), dstCapacity - ZSTD_blockHeaderSize, srcSize, (void*)zc->entropyWorkspace, ((uint)(((6 << 10) + 256)) + ((nuint)(sizeof(uint)) * (uint)((((35) > (52) ? (35) : (52)) + 2)))), zc->bmi2); + + { + nuint err_code = (cSeqsSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (zc->isFirstBlock == 0 && cSeqsSize < rleMaxLength && (ZSTD_isRLE((byte*)(src), srcSize)) != 0) + { + cSeqsSize = 1; + } + + if (zc->seqCollector.collectSequences != 0) + { + ZSTD_copyBlockSequences(zc); + ZSTD_blockState_confirmRepcodesAndEntropyTables(&zc->blockState); + return 0; + } + + if (zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode == FSE_repeat.FSE_repeat_valid) + { + zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode = FSE_repeat.FSE_repeat_check; + } + + if (cSeqsSize == 0) + { + cSize = ZSTD_noCompressBlock((void*)op, dstCapacity, (void*)ip, srcSize, lastBlock); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + *dRep = dRepOriginal; + } + else if (cSeqsSize == 1) + { + cSize = ZSTD_rleCompressBlock((void*)op, dstCapacity, *ip, srcSize, lastBlock); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + *dRep = dRepOriginal; + } + else + { + ZSTD_blockState_confirmRepcodesAndEntropyTables(&zc->blockState); + writeBlockHeader((void*)op, cSeqsSize, srcSize, lastBlock); + cSize = ZSTD_blockHeaderSize + cSeqsSize; + } + + return cSize; + } + + /* Helper function to perform the recursive search for block splits. + * Estimates the cost of seqStore prior to split, and estimates the cost of splitting the sequences in half. + * If advantageous to split, then we recurse down the two sub-blocks. If not, or if an error occurred in estimation, then + * we do not recurse. + * + * Note: The recursion depth is capped by a heuristic minimum number of sequences, defined by MIN_SEQUENCES_BLOCK_SPLITTING. + * In theory, this means the absolute largest recursion depth is 10 == log2(maxNbSeqInBlock/MIN_SEQUENCES_BLOCK_SPLITTING). + * In practice, recursion depth usually doesn't go beyond 4. + * + * Furthermore, the number of splits is capped by MAX_NB_SPLITS. At MAX_NB_SPLITS == 196 with the current existing blockSize + * maximum of 128 KB, this value is actually impossible to reach. + */ + private static void ZSTD_deriveBlockSplitsHelper(seqStoreSplits* splits, nuint startIdx, nuint endIdx, ZSTD_CCtx_s* zc, seqStore_t* origSeqStore) + { + seqStore_t fullSeqStoreChunk; + seqStore_t firstHalfSeqStore; + seqStore_t secondHalfSeqStore; + nuint estimatedOriginalSize; + nuint estimatedFirstHalfSize; + nuint estimatedSecondHalfSize; + nuint midIdx = (startIdx + endIdx) / 2; + + if (endIdx - startIdx < 300 || splits->idx >= 196) + { + return; + } + + ZSTD_deriveSeqStoreChunk(&fullSeqStoreChunk, origSeqStore, startIdx, endIdx); + ZSTD_deriveSeqStoreChunk(&firstHalfSeqStore, origSeqStore, startIdx, midIdx); + ZSTD_deriveSeqStoreChunk(&secondHalfSeqStore, origSeqStore, midIdx, endIdx); + estimatedOriginalSize = ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize(&fullSeqStoreChunk, zc); + estimatedFirstHalfSize = ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize(&firstHalfSeqStore, zc); + estimatedSecondHalfSize = ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize(&secondHalfSeqStore, zc); + if ((ERR_isError(estimatedOriginalSize)) != 0 || (ERR_isError(estimatedFirstHalfSize)) != 0 || (ERR_isError(estimatedSecondHalfSize)) != 0) + { + return; + } + + if (estimatedFirstHalfSize + estimatedSecondHalfSize < estimatedOriginalSize) + { + ZSTD_deriveBlockSplitsHelper(splits, startIdx, midIdx, zc, origSeqStore); + splits->splitLocations[splits->idx] = (uint)(midIdx); + splits->idx++; + ZSTD_deriveBlockSplitsHelper(splits, midIdx, endIdx, zc, origSeqStore); + } + } + + /* Base recursive function. Populates a table with intra-block partition indices that can improve compression ratio. + * + * Returns the number of splits made (which equals the size of the partition table - 1). + */ + private static nuint ZSTD_deriveBlockSplits(ZSTD_CCtx_s* zc, uint* partitions, uint nbSeq) + { + seqStoreSplits splits = new seqStoreSplits + { + splitLocations = partitions, + idx = 0, + }; + + if (nbSeq <= 4) + { + return 0; + } + + ZSTD_deriveBlockSplitsHelper(&splits, 0, nbSeq, zc, &zc->seqStore); + splits.splitLocations[splits.idx] = nbSeq; + return splits.idx; + } + + /* ZSTD_compressBlock_splitBlock(): + * Attempts to split a given block into multiple blocks to improve compression ratio. + * + * Returns combined size of all blocks (which includes headers), or a ZSTD error code. + */ + private static nuint ZSTD_compressBlock_splitBlock_internal(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint blockSize, uint lastBlock, uint nbSeq) + { + nuint cSize = 0; + byte* ip = (byte*)(src); + byte* op = (byte*)(dst); + uint* partitions = stackalloc uint[196]; + nuint i = 0; + nuint srcBytesTotal = 0; + nuint numSplits = ZSTD_deriveBlockSplits(zc, partitions, nbSeq); + seqStore_t nextSeqStore; + var _ = &nextSeqStore; + seqStore_t currSeqStore; + repcodes_s dRep; + repcodes_s cRep; + + memcpy((void*)(dRep.rep), (void*)(zc->blockState.prevCBlock->rep), ((nuint)(sizeof(repcodes_s)))); + memcpy((void*)(cRep.rep), (void*)(zc->blockState.prevCBlock->rep), ((nuint)(sizeof(repcodes_s)))); + if (numSplits == 0) + { + nuint cSizeSingleBlock = ZSTD_compressSeqStore_singleBlock(zc, &zc->seqStore, &dRep, &cRep, (void*)op, dstCapacity, (void*)ip, blockSize, lastBlock, 0); + + + { + nuint err_code = (cSizeSingleBlock); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(cSizeSingleBlock <= (uint)((1 << 17)) + ZSTD_blockHeaderSize); + return cSizeSingleBlock; + } + + ZSTD_deriveSeqStoreChunk(&currSeqStore, &zc->seqStore, 0, partitions[0]); + for (i = 0; i <= numSplits; ++i) + { + nuint srcBytes; + nuint cSizeChunk; + uint lastPartition = (((i == numSplits)) ? 1U : 0U); + uint lastBlockEntireSrc = 0; + + srcBytes = ZSTD_countSeqStoreLiteralsBytes(&currSeqStore) + ZSTD_countSeqStoreMatchBytes(&currSeqStore); + srcBytesTotal += srcBytes; + if (lastPartition != 0) + { + srcBytes += blockSize - srcBytesTotal; + lastBlockEntireSrc = lastBlock; + } + else + { + ZSTD_deriveSeqStoreChunk(&nextSeqStore, &zc->seqStore, partitions[i], partitions[i + 1]); + } + + cSizeChunk = ZSTD_compressSeqStore_singleBlock(zc, &currSeqStore, &dRep, &cRep, (void*)op, dstCapacity, (void*)ip, srcBytes, lastBlockEntireSrc, 1); + + { + nuint err_code = (cSizeChunk); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ip += srcBytes; + op += cSizeChunk; + dstCapacity -= cSizeChunk; + cSize += cSizeChunk; + currSeqStore = nextSeqStore; + assert(cSizeChunk <= (uint)((1 << 17)) + ZSTD_blockHeaderSize); + } + + memcpy((void*)(zc->blockState.prevCBlock->rep), (void*)(dRep.rep), ((nuint)(sizeof(repcodes_s)))); + return cSize; + } + + private static nuint ZSTD_compressBlock_splitBlock(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint lastBlock) + { + byte* ip = (byte*)(src); + byte* op = (byte*)(dst); + uint nbSeq; + nuint cSize; + + + { + nuint bss = ZSTD_buildSeqStore(zc, src, srcSize); + + + { + nuint err_code = (bss); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (bss == (nuint)ZSTD_buildSeqStore_e.ZSTDbss_noCompress) + { + if (zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode == FSE_repeat.FSE_repeat_valid) + { + zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode = FSE_repeat.FSE_repeat_check; + } + + cSize = ZSTD_noCompressBlock((void*)op, dstCapacity, (void*)ip, srcSize, lastBlock); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return cSize; + } + + nbSeq = (uint)(zc->seqStore.sequences - zc->seqStore.sequencesStart); + } + + assert(zc->appliedParams.splitBlocks == 1); + cSize = ZSTD_compressBlock_splitBlock_internal(zc, dst, dstCapacity, src, srcSize, lastBlock, nbSeq); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return cSize; + } + + private static nuint ZSTD_compressBlock_internal(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint frame) + { + uint rleMaxLength = 25; + nuint cSize; + byte* ip = (byte*)(src); + byte* op = (byte*)(dst); + + + { + nuint bss = ZSTD_buildSeqStore(zc, src, srcSize); + + + { + nuint err_code = (bss); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (bss == (nuint)ZSTD_buildSeqStore_e.ZSTDbss_noCompress) + { + cSize = 0; + goto @out; + } + } + + if (zc->seqCollector.collectSequences != 0) + { + ZSTD_copyBlockSequences(zc); + ZSTD_blockState_confirmRepcodesAndEntropyTables(&zc->blockState); + return 0; + } + + cSize = ZSTD_entropyCompressSeqStore(&zc->seqStore, &zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy, &zc->appliedParams, dst, dstCapacity, srcSize, (void*)zc->entropyWorkspace, ((uint)(((6 << 10) + 256)) + ((nuint)(sizeof(uint)) * (uint)((((35) > (52) ? (35) : (52)) + 2)))), zc->bmi2); + if (zc->seqCollector.collectSequences != 0) + { + ZSTD_copyBlockSequences(zc); + return 0; + } + + if (frame != 0 && zc->isFirstBlock == 0 && cSize < rleMaxLength && (ZSTD_isRLE(ip, srcSize)) != 0) + { + cSize = 1; + op[0] = ip[0]; + } + + @out: + if ((ERR_isError(cSize)) == 0 && cSize > 1) + { + ZSTD_blockState_confirmRepcodesAndEntropyTables(&zc->blockState); + } + + if (zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode == FSE_repeat.FSE_repeat_valid) + { + zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode = FSE_repeat.FSE_repeat_check; + } + + return cSize; + } + + private static nuint ZSTD_compressBlock_targetCBlockSize_body(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint srcSize, nuint bss, uint lastBlock) + { + if (bss == (nuint)ZSTD_buildSeqStore_e.ZSTDbss_compress) + { + if (zc->isFirstBlock == 0 && (ZSTD_maybeRLE(&zc->seqStore)) != 0 && (ZSTD_isRLE((byte*)(src), srcSize)) != 0) + { + return ZSTD_rleCompressBlock(dst, dstCapacity, *(byte*)(src), srcSize, lastBlock); + } + + + { + nuint cSize = ZSTD_compressSuperBlock(zc, dst, dstCapacity, src, srcSize, lastBlock); + + if (cSize != (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)))) + { + nuint maxCSize = srcSize - ZSTD_minGain(srcSize, zc->appliedParams.cParams.strategy); + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (cSize != 0 && cSize < maxCSize + ZSTD_blockHeaderSize) + { + ZSTD_blockState_confirmRepcodesAndEntropyTables(&zc->blockState); + return cSize; + } + } + } + } + + return ZSTD_noCompressBlock(dst, dstCapacity, src, srcSize, lastBlock); + } + + private static nuint ZSTD_compressBlock_targetCBlockSize(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint lastBlock) + { + nuint cSize = 0; + nuint bss = ZSTD_buildSeqStore(zc, src, srcSize); + + + { + nuint err_code = (bss); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + cSize = ZSTD_compressBlock_targetCBlockSize_body(zc, dst, dstCapacity, src, srcSize, bss, lastBlock); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode == FSE_repeat.FSE_repeat_valid) + { + zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode = FSE_repeat.FSE_repeat_check; + } + + return cSize; + } + + private static void ZSTD_overflowCorrectIfNeeded(ZSTD_matchState_t* ms, ZSTD_cwksp* ws, ZSTD_CCtx_params_s* @params, void* ip, void* iend) + { + uint cycleLog = ZSTD_cycleLog(@params->cParams.chainLog, @params->cParams.strategy); + uint maxDist = (uint)(1) << (int)@params->cParams.windowLog; + + if ((ZSTD_window_needOverflowCorrection(ms->window, cycleLog, maxDist, ms->loadedDictEnd, ip, iend)) != 0) + { + uint correction = ZSTD_window_correctOverflow(&ms->window, cycleLog, maxDist, ip); + + ZSTD_cwksp_mark_tables_dirty(ws); + ZSTD_reduceIndex(ms, @params, correction); + ZSTD_cwksp_mark_tables_clean(ws); + if (ms->nextToUpdate < correction) + { + ms->nextToUpdate = 0; + } + else + { + ms->nextToUpdate -= correction; + } + + ms->loadedDictEnd = 0; + ms->dictMatchState = null; + } + } + + /*! ZSTD_compress_frameChunk() : + * Compress a chunk of data into one or multiple blocks. + * All blocks will be terminated, all input will be consumed. + * Function will issue an error if there is not enough `dstCapacity` to hold the compressed content. + * Frame is supposed already started (header already produced) + * @return : compressed size, or an error code + */ + private static nuint ZSTD_compress_frameChunk(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint lastFrameChunk) + { + nuint blockSize = cctx->blockSize; + nuint remaining = srcSize; + byte* ip = (byte*)(src); + byte* ostart = (byte*)(dst); + byte* op = ostart; + uint maxDist = (uint)(1) << (int)cctx->appliedParams.cParams.windowLog; + + assert(cctx->appliedParams.cParams.windowLog <= (uint)(((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)))); + if (cctx->appliedParams.fParams.checksumFlag != 0 && srcSize != 0) + { + XXH64_update(&cctx->xxhState, src, srcSize); + } + + while (remaining != 0) + { + ZSTD_matchState_t* ms = &cctx->blockState.matchState; + uint lastBlock = lastFrameChunk & (uint)((((blockSize >= remaining)) ? 1 : 0)); + + if (dstCapacity < ZSTD_blockHeaderSize + (uint)((1 + 1 + 1))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (remaining < blockSize) + { + blockSize = remaining; + } + + ZSTD_overflowCorrectIfNeeded(ms, &cctx->workspace, &cctx->appliedParams, (void*)ip, (void*)(ip + blockSize)); + ZSTD_checkDictValidity(&ms->window, (void*)(ip + blockSize), maxDist, &ms->loadedDictEnd, &ms->dictMatchState); + if (ms->nextToUpdate < ms->window.lowLimit) + { + ms->nextToUpdate = ms->window.lowLimit; + } + + + { + nuint cSize; + + if ((ZSTD_useTargetCBlockSize(&cctx->appliedParams)) != 0) + { + cSize = ZSTD_compressBlock_targetCBlockSize(cctx, (void*)op, dstCapacity, (void*)ip, blockSize, lastBlock); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(cSize > 0); + assert(cSize <= blockSize + ZSTD_blockHeaderSize); + } + else if ((ZSTD_blockSplitterEnabled(&cctx->appliedParams)) != 0) + { + cSize = ZSTD_compressBlock_splitBlock(cctx, (void*)op, dstCapacity, (void*)ip, blockSize, lastBlock); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(cSize > 0 || cctx->seqCollector.collectSequences == 1); + } + else + { + cSize = ZSTD_compressBlock_internal(cctx, (void*)(op + ZSTD_blockHeaderSize), dstCapacity - ZSTD_blockHeaderSize, (void*)ip, blockSize, 1); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (cSize == 0) + { + cSize = ZSTD_noCompressBlock((void*)op, dstCapacity, (void*)ip, blockSize, lastBlock); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + else + { + uint cBlockHeader = cSize == 1 ? lastBlock + (((uint)(blockType_e.bt_rle)) << 1) + (uint)(blockSize << 3) : lastBlock + (((uint)(blockType_e.bt_compressed)) << 1) + (uint)(cSize << 3); + + MEM_writeLE24((void*)op, cBlockHeader); + cSize += ZSTD_blockHeaderSize; + } + } + + ip += blockSize; + assert(remaining >= blockSize); + remaining -= blockSize; + op += cSize; + assert(dstCapacity >= cSize); + dstCapacity -= cSize; + cctx->isFirstBlock = 0; + } + } + + if (lastFrameChunk != 0 && (op > ostart)) + { + cctx->stage = ZSTD_compressionStage_e.ZSTDcs_ending; + } + + return (nuint)(op - ostart); + } + + private static nuint ZSTD_writeFrameHeader(void* dst, nuint dstCapacity, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize, uint dictID) + { + byte* op = (byte*)(dst); + uint dictIDSizeCodeLength = (uint)(((dictID > 0) ? 1 : 0) + ((dictID >= 256) ? 1 : 0) + ((dictID >= 65536) ? 1 : 0)); + uint dictIDSizeCode = (uint)(@params->fParams.noDictIDFlag != 0 ? 0 : dictIDSizeCodeLength); + uint checksumFlag = ((@params->fParams.checksumFlag > 0) ? 1U : 0U); + uint windowSize = (uint)(1) << (int)@params->cParams.windowLog; + uint singleSegment = ((@params->fParams.contentSizeFlag != 0 && (windowSize >= pledgedSrcSize)) ? 1U : 0U); + byte windowLogByte = (byte)((@params->cParams.windowLog - 10) << 3); + uint fcsCode = (uint)(@params->fParams.contentSizeFlag != 0 ? ((pledgedSrcSize >= 256) ? 1 : 0) + ((pledgedSrcSize >= (uint)(65536 + 256)) ? 1 : 0) + ((pledgedSrcSize >= 0xFFFFFFFFU) ? 1 : 0) : 0); + byte frameHeaderDescriptionByte = (byte)(dictIDSizeCode + (checksumFlag << 2) + (singleSegment << 5) + (fcsCode << 6)); + nuint pos = 0; + + assert(!(@params->fParams.contentSizeFlag != 0 && pledgedSrcSize == (unchecked(0UL - 1)))); + if (dstCapacity < 18) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (@params->format == ZSTD_format_e.ZSTD_f_zstd1) + { + MEM_writeLE32(dst, 0xFD2FB528); + pos = 4; + } + + op[pos++] = frameHeaderDescriptionByte; + if (singleSegment == 0) + { + op[pos++] = windowLogByte; + } + + switch (dictIDSizeCode) + { + default: + { + assert(0 != 0); + } + + + goto case 0; + case 0: + { + break; + } + + case 1: + { + op[pos] = (byte)(dictID); + } + + pos++; + break; + case 2: + { + MEM_writeLE16((void*)(op + pos), (ushort)(dictID)); + } + + pos += 2; + break; + case 3: + { + MEM_writeLE32((void*)(op + pos), dictID); + } + + pos += 4; + break; + } + + switch (fcsCode) + { + default: + { + assert(0 != 0); + } + + + goto case 0; + case 0: + { + if (singleSegment != 0) + { + op[pos++] = (byte)(pledgedSrcSize); + } + } + + break; + case 1: + { + MEM_writeLE16((void*)(op + pos), (ushort)(pledgedSrcSize - 256)); + } + + pos += 2; + break; + case 2: + { + MEM_writeLE32((void*)(op + pos), (uint)(pledgedSrcSize)); + } + + pos += 4; + break; + case 3: + { + MEM_writeLE64((void*)(op + pos), (ulong)(pledgedSrcSize)); + } + + pos += 8; + break; + } + + return pos; + } + + /* ZSTD_writeSkippableFrame_advanced() : + * Writes out a skippable frame with the specified magic number variant (16 are supported), + * from ZSTD_MAGIC_SKIPPABLE_START to ZSTD_MAGIC_SKIPPABLE_START+15, and the desired source data. + * + * Returns the total number of bytes written, or a ZSTD error code. + */ + public static nuint ZSTD_writeSkippableFrame(void* dst, nuint dstCapacity, void* src, nuint srcSize, uint magicVariant) + { + byte* op = (byte*)(dst); + + if (dstCapacity < srcSize + 8) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (srcSize > (uint)(0xFFFFFFFF)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (magicVariant > 15) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + MEM_writeLE32((void*)op, (uint)(0x184D2A50 + magicVariant)); + MEM_writeLE32((void*)(op + 4), (uint)(srcSize)); + memcpy((void*)((op + 8)), (src), (srcSize)); + return srcSize + 8; + } + + /* ZSTD_writeLastEmptyBlock() : + * output an empty Block with end-of-frame mark to complete a frame + * @return : size of data written into `dst` (== ZSTD_blockHeaderSize (defined in zstd_internal.h)) + * or an error code if `dstCapacity` is too small (stage != ZSTD_compressionStage_e.ZSTDcs_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + if (cctx->appliedParams.ldmParams.enableLdm != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + + cctx->externSeqStore.seq = seq; + cctx->externSeqStore.size = nbSeq; + cctx->externSeqStore.capacity = nbSeq; + cctx->externSeqStore.pos = 0; + cctx->externSeqStore.posInSequence = 0; + return 0; + } + + private static nuint ZSTD_compressContinue_internal(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint frame, uint lastFrameChunk) + { + ZSTD_matchState_t* ms = &cctx->blockState.matchState; + nuint fhSize = 0; + + if (cctx->stage == ZSTD_compressionStage_e.ZSTDcs_created) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + if (frame != 0 && (cctx->stage == ZSTD_compressionStage_e.ZSTDcs_init)) + { + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, &cctx->appliedParams, cctx->pledgedSrcSizePlusOne - 1, cctx->dictID); + + { + nuint err_code = (fhSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(fhSize <= dstCapacity); + dstCapacity -= fhSize; + dst = (sbyte*)(dst) + fhSize; + cctx->stage = ZSTD_compressionStage_e.ZSTDcs_ongoing; + } + + if (srcSize == 0) + { + return fhSize; + } + + if ((ZSTD_window_update(&ms->window, src, srcSize, (int)ms->forceNonContiguous)) == 0) + { + ms->forceNonContiguous = 0; + ms->nextToUpdate = ms->window.dictLimit; + } + + if (cctx->appliedParams.ldmParams.enableLdm != 0) + { + ZSTD_window_update(&cctx->ldmState.window, src, srcSize, 0); + } + + if (frame == 0) + { + ZSTD_overflowCorrectIfNeeded(ms, &cctx->workspace, &cctx->appliedParams, src, (void*)((byte*)(src) + srcSize)); + } + + + { + nuint cSize = frame != 0 ? ZSTD_compress_frameChunk(cctx, dst, dstCapacity, src, srcSize, lastFrameChunk) : ZSTD_compressBlock_internal(cctx, dst, dstCapacity, src, srcSize, 0); + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + cctx->consumedSrcSize += (ulong)srcSize; + cctx->producedCSize += (ulong)(cSize + fhSize); + assert(!(cctx->appliedParams.fParams.contentSizeFlag != 0 && cctx->pledgedSrcSizePlusOne == 0)); + if (cctx->pledgedSrcSizePlusOne != 0) + { + if (cctx->consumedSrcSize + 1 > cctx->pledgedSrcSizePlusOne) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + } + + return cSize + fhSize; + } + } + + public static nuint ZSTD_compressContinue(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1, 0); + } + + /*===== Raw zstd block functions =====*/ + public static nuint ZSTD_getBlockSize(ZSTD_CCtx_s* cctx) + { + ZSTD_compressionParameters cParams = cctx->appliedParams.cParams; + + assert((ZSTD_checkCParams(cParams)) == 0); + return ((uint)(((1 << 17))) < ((uint)(1) << (int)cParams.windowLog) ? ((1 << 17)) : ((uint)(1) << (int)cParams.windowLog)); + } + + public static nuint ZSTD_compressBlock(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + + { + nuint blockSizeMax = ZSTD_getBlockSize(cctx); + + if (srcSize > blockSizeMax) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + } + + return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 0, 0); + } + + /*! ZSTD_loadDictionaryContent() : + * @return : 0, or an error code + */ + private static nuint ZSTD_loadDictionaryContent(ZSTD_matchState_t* ms, ldmState_t* ls, ZSTD_cwksp* ws, ZSTD_CCtx_params_s* @params, void* src, nuint srcSize, ZSTD_dictTableLoadMethod_e dtlm) + { + byte* ip = (byte*)(src); + byte* iend = ip + srcSize; + int loadLdmDict = ((@params->ldmParams.enableLdm != 0 && ls != null) ? 1 : 0); + + ZSTD_assertEqualCParams(@params->cParams, ms->cParams); + if (srcSize > ((unchecked((uint)(-1))) - ((3U << 29) + (1U << ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)))))) + { + uint maxDictSize = ((3U << 29) + (1U << (unchecked((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31))))) - 1; + + assert((ZSTD_window_isEmpty(ms->window)) != 0); + if (loadLdmDict != 0) + { + assert((ZSTD_window_isEmpty(ls->window)) != 0); + } + + if (srcSize > maxDictSize) + { + ip = iend - maxDictSize; + src = ip; + srcSize = maxDictSize; + } + } + + ZSTD_window_update(&ms->window, src, srcSize, 0); + ms->loadedDictEnd = (uint)(@params->forceWindow != 0 ? 0 : (uint)(iend - ms->window.@base)); + ms->forceNonContiguous = (uint)@params->deterministicRefPrefix; + if (loadLdmDict != 0) + { + ZSTD_window_update(&ls->window, src, srcSize, 0); + ls->loadedDictEnd = (uint)(@params->forceWindow != 0 ? 0 : (uint)(iend - ls->window.@base)); + } + + if (srcSize <= 8) + { + return 0; + } + + ZSTD_overflowCorrectIfNeeded(ms, ws, @params, (void*)ip, (void*)iend); + if (loadLdmDict != 0) + { + ZSTD_ldm_fillHashTable(ls, ip, iend, &@params->ldmParams); + } + + switch (@params->cParams.strategy) + { + case ZSTD_strategy.ZSTD_fast: + { + ZSTD_fillHashTable(ms, (void*)iend, dtlm); + } + + break; + case ZSTD_strategy.ZSTD_dfast: + { + ZSTD_fillDoubleHashTable(ms, (void*)iend, dtlm); + } + + break; + case ZSTD_strategy.ZSTD_greedy: + case ZSTD_strategy.ZSTD_lazy: + case ZSTD_strategy.ZSTD_lazy2: + { + assert(srcSize >= 8); + } + + if (ms->dedicatedDictSearch != 0) + { + assert(ms->chainTable != null); + ZSTD_dedicatedDictSearch_lazy_loadDictionary(ms, iend - 8); + } + else + { + assert(@params->useRowMatchFinder != ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto); + if (@params->useRowMatchFinder == ZSTD_useRowMatchFinderMode_e.ZSTD_urm_enableRowMatchFinder) + { + nuint tagTableSize = ((nuint)(1) << (int)@params->cParams.hashLog) * (nuint)(2); + + memset((void*)(ms->tagTable), (0), (tagTableSize)); + ZSTD_row_update(ms, iend - 8); + } + else + { + ZSTD_insertAndFindFirstIndex(ms, iend - 8); + } + } + + break; + case ZSTD_strategy.ZSTD_btlazy2: + case ZSTD_strategy.ZSTD_btopt: + case ZSTD_strategy.ZSTD_btultra: + case ZSTD_strategy.ZSTD_btultra2: + { + assert(srcSize >= 8); + } + + ZSTD_updateTree(ms, iend - 8, iend); + break; + default: + { + assert(0 != 0); + } + break; + } + + ms->nextToUpdate = (uint)(iend - ms->window.@base); + return 0; + } + + /* Dictionaries that assign zero probability to symbols that show up causes problems + * when FSE encoding. Mark dictionaries with zero probability symbols as FSE_repeat_check + * and only dictionaries with 100% valid symbols can be assumed valid. + */ + private static FSE_repeat ZSTD_dictNCountRepeat(short* normalizedCounter, uint dictMaxSymbolValue, uint maxSymbolValue) + { + uint s; + + if (dictMaxSymbolValue < maxSymbolValue) + { + return FSE_repeat.FSE_repeat_check; + } + + for (s = 0; s <= maxSymbolValue; ++s) + { + if (normalizedCounter[s] == 0) + { + return FSE_repeat.FSE_repeat_check; + } + } + + return FSE_repeat.FSE_repeat_valid; + } + + /* ZSTD_loadCEntropy() : + * dict : must point at beginning of a valid zstd dictionary. + * return : size of dictionary header (size of magic number + dict ID + entropy tables) + * assumptions : magic number supposed already checked + * and dictSize >= 8 */ + public static nuint ZSTD_loadCEntropy(ZSTD_compressedBlockState_t* bs, void* workspace, void* dict, nuint dictSize) + { + short* offcodeNCount = stackalloc short[32]; + uint offcodeMaxValue = 31; + byte* dictPtr = (byte*)(dict); + byte* dictEnd = dictPtr + dictSize; + + dictPtr += 8; + bs->entropy.huf.repeatMode = HUF_repeat.HUF_repeat_check; + + { + uint maxSymbolValue = 255; + uint hasZeroWeights = 1; + nuint hufHeaderSize = HUF_readCTable((HUF_CElt_s*)(bs->entropy.huf.CTable), &maxSymbolValue, (void*)dictPtr, (nuint)(dictEnd - dictPtr), &hasZeroWeights); + + if (hasZeroWeights == 0) + { + bs->entropy.huf.repeatMode = HUF_repeat.HUF_repeat_valid; + } + + if ((ERR_isError(hufHeaderSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (maxSymbolValue < 255) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + dictPtr += hufHeaderSize; + } + + + { + uint offcodeLog; + nuint offcodeHeaderSize = FSE_readNCount((short*)offcodeNCount, &offcodeMaxValue, &offcodeLog, (void*)dictPtr, (nuint)(dictEnd - dictPtr)); + + if ((ERR_isError(offcodeHeaderSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (offcodeLog > 8) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if ((ERR_isError(FSE_buildCTable_wksp((uint*)bs->entropy.fse.offcodeCTable, (short*)offcodeNCount, 31, offcodeLog, workspace, (nuint)(((6 << 10) + 256))))) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + dictPtr += offcodeHeaderSize; + } + + + { + short* matchlengthNCount = stackalloc short[53]; + uint matchlengthMaxValue = 52, matchlengthLog; + nuint matchlengthHeaderSize = FSE_readNCount((short*)matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, (void*)dictPtr, (nuint)(dictEnd - dictPtr)); + + if ((ERR_isError(matchlengthHeaderSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (matchlengthLog > 9) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if ((ERR_isError(FSE_buildCTable_wksp((uint*)bs->entropy.fse.matchlengthCTable, (short*)matchlengthNCount, matchlengthMaxValue, matchlengthLog, workspace, (nuint)(((6 << 10) + 256))))) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + bs->entropy.fse.matchlength_repeatMode = ZSTD_dictNCountRepeat((short*)matchlengthNCount, matchlengthMaxValue, 52); + dictPtr += matchlengthHeaderSize; + } + + + { + short* litlengthNCount = stackalloc short[36]; + uint litlengthMaxValue = 35, litlengthLog; + nuint litlengthHeaderSize = FSE_readNCount((short*)litlengthNCount, &litlengthMaxValue, &litlengthLog, (void*)dictPtr, (nuint)(dictEnd - dictPtr)); + + if ((ERR_isError(litlengthHeaderSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (litlengthLog > 9) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if ((ERR_isError(FSE_buildCTable_wksp((uint*)bs->entropy.fse.litlengthCTable, (short*)litlengthNCount, litlengthMaxValue, litlengthLog, workspace, (nuint)(((6 << 10) + 256))))) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + bs->entropy.fse.litlength_repeatMode = ZSTD_dictNCountRepeat((short*)litlengthNCount, litlengthMaxValue, 35); + dictPtr += litlengthHeaderSize; + } + + if (dictPtr + 12 > dictEnd) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + bs->rep[0] = MEM_readLE32((void*)(dictPtr + 0)); + bs->rep[1] = MEM_readLE32((void*)(dictPtr + 4)); + bs->rep[2] = MEM_readLE32((void*)(dictPtr + 8)); + dictPtr += 12; + + { + nuint dictContentSize = (nuint)(dictEnd - dictPtr); + uint offcodeMax = 31; + + if (dictContentSize <= (unchecked((uint)(-1))) - (uint)(128 * (1 << 10))) + { + uint maxOffset = (uint)(dictContentSize) + (uint)(128 * (1 << 10)); + + offcodeMax = ZSTD_highbit32(maxOffset); + } + + bs->entropy.fse.offcode_repeatMode = ZSTD_dictNCountRepeat((short*)offcodeNCount, offcodeMaxValue, ((offcodeMax) < (31) ? (offcodeMax) : (31))); + + { + uint u; + + for (u = 0; u < 3; u++) + { + if (bs->rep[u] == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (bs->rep[u] > dictContentSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + } + } + } + + return (nuint)(dictPtr - (byte*)(dict)); + } + + /* Dictionary format : + * See : + * https://github.com/facebook/zstd/blob/release/doc/zstd_compression_format.md#dictionary-format + */ + /*! ZSTD_loadZstdDictionary() : + * @return : dictID, or an error code + * assumptions : magic number supposed already checked + * dictSize supposed >= 8 + */ + private static nuint ZSTD_loadZstdDictionary(ZSTD_compressedBlockState_t* bs, ZSTD_matchState_t* ms, ZSTD_cwksp* ws, ZSTD_CCtx_params_s* @params, void* dict, nuint dictSize, ZSTD_dictTableLoadMethod_e dtlm, void* workspace) + { + byte* dictPtr = (byte*)(dict); + byte* dictEnd = dictPtr + dictSize; + nuint dictID; + nuint eSize; + + assert(dictSize >= 8); + assert(MEM_readLE32((void*)dictPtr) == 0xEC30A437); + dictID = (nuint)(@params->fParams.noDictIDFlag != 0 ? 0 : MEM_readLE32((void*)(dictPtr + 4))); + eSize = ZSTD_loadCEntropy(bs, workspace, dict, dictSize); + + { + nuint err_code = (eSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + dictPtr += eSize; + + { + nuint dictContentSize = (nuint)(dictEnd - dictPtr); + + + { + nuint err_code = (ZSTD_loadDictionaryContent(ms, (ldmState_t*)null, ws, @params, (void*)dictPtr, dictContentSize, dtlm)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + + return dictID; + } + + /** ZSTD_compress_insertDictionary() : + * @return : dictID, or an error code */ + private static nuint ZSTD_compress_insertDictionary(ZSTD_compressedBlockState_t* bs, ZSTD_matchState_t* ms, ldmState_t* ls, ZSTD_cwksp* ws, ZSTD_CCtx_params_s* @params, void* dict, nuint dictSize, ZSTD_dictContentType_e dictContentType, ZSTD_dictTableLoadMethod_e dtlm, void* workspace) + { + if ((dict == null) || (dictSize < 8)) + { + if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_fullDict) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_wrong))); + } + + return 0; + } + + ZSTD_reset_compressedBlockState(bs); + if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_rawContent) + { + return ZSTD_loadDictionaryContent(ms, ls, ws, @params, dict, dictSize, dtlm); + } + + if (MEM_readLE32(dict) != 0xEC30A437) + { + if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_auto) + { + return ZSTD_loadDictionaryContent(ms, ls, ws, @params, dict, dictSize, dtlm); + } + + if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_fullDict) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_wrong))); + } + + assert(0 != 0); + } + + return ZSTD_loadZstdDictionary(bs, ms, ws, @params, dict, dictSize, dtlm, workspace); + } + + /*! ZSTD_compressBegin_internal() : + * @return : 0, or an error code */ + private static nuint ZSTD_compressBegin_internal(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize, ZSTD_dictContentType_e dictContentType, ZSTD_dictTableLoadMethod_e dtlm, ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize, ZSTD_buffered_policy_e zbuff) + { + nuint dictContentSize = cdict != null ? cdict->dictContentSize : dictSize; + + assert((ERR_isError(ZSTD_checkCParams(@params->cParams))) == 0); + assert(!((dict) != null && (cdict) != null)); + if ((cdict) != null && (cdict->dictContentSize > 0) && (pledgedSrcSize < (uint)((128 * (1 << 10))) || pledgedSrcSize < cdict->dictContentSize * (6UL) || pledgedSrcSize == (unchecked(0UL - 1)) || cdict->compressionLevel == 0) && (@params->attachDictPref != ZSTD_dictAttachPref_e.ZSTD_dictForceLoad)) + { + return ZSTD_resetCCtx_usingCDict(cctx, cdict, @params, pledgedSrcSize, zbuff); + } + + + { + nuint err_code = (ZSTD_resetCCtx_internal(cctx, @params, pledgedSrcSize, dictContentSize, ZSTD_compResetPolicy_e.ZSTDcrp_makeClean, zbuff)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint dictID = cdict != null ? ZSTD_compress_insertDictionary(cctx->blockState.prevCBlock, &cctx->blockState.matchState, &cctx->ldmState, &cctx->workspace, &cctx->appliedParams, cdict->dictContent, cdict->dictContentSize, cdict->dictContentType, dtlm, (void*)cctx->entropyWorkspace) : ZSTD_compress_insertDictionary(cctx->blockState.prevCBlock, &cctx->blockState.matchState, &cctx->ldmState, &cctx->workspace, &cctx->appliedParams, dict, dictSize, dictContentType, dtlm, (void*)cctx->entropyWorkspace); + + + { + nuint err_code = (dictID); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(dictID <= 0xffffffff); + cctx->dictID = (uint)(dictID); + cctx->dictContentSize = dictContentSize; + } + + return 0; + } + + /* ZSTD_compressBegin_advanced_internal() : + * Private use only. To be called from zstdmt_compress.c. */ + public static nuint ZSTD_compressBegin_advanced_internal(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize, ZSTD_dictContentType_e dictContentType, ZSTD_dictTableLoadMethod_e dtlm, ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize) + { + + { + nuint err_code = (ZSTD_checkCParams(@params->cParams)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_compressBegin_internal(cctx, dict, dictSize, dictContentType, dtlm, cdict, @params, pledgedSrcSize, ZSTD_buffered_policy_e.ZSTDb_not_buffered); + } + + /*! ZSTD_compressBegin_advanced() : + * @return : 0, or an error code */ + public static nuint ZSTD_compressBegin_advanced(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize, ZSTD_parameters @params, ulong pledgedSrcSize) + { + ZSTD_CCtx_params_s cctxParams; + + ZSTD_CCtxParams_init_internal(&cctxParams, &@params, 0); + return ZSTD_compressBegin_advanced_internal(cctx, dict, dictSize, ZSTD_dictContentType_e.ZSTD_dct_auto, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, (ZSTD_CDict_s*)null, &cctxParams, pledgedSrcSize); + } + + public static nuint ZSTD_compressBegin_usingDict(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize, int compressionLevel) + { + ZSTD_CCtx_params_s cctxParams; + + + { + ZSTD_parameters @params = ZSTD_getParams_internal(compressionLevel, (unchecked(0UL - 1)), dictSize, ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict); + + ZSTD_CCtxParams_init_internal(&cctxParams, &@params, (compressionLevel == 0) ? 3 : compressionLevel); + } + + return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dictContentType_e.ZSTD_dct_auto, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, (ZSTD_CDict_s*)null, &cctxParams, (unchecked(0UL - 1)), ZSTD_buffered_policy_e.ZSTDb_not_buffered); + } + + /*===== Buffer-less streaming compression functions =====*/ + public static nuint ZSTD_compressBegin(ZSTD_CCtx_s* cctx, int compressionLevel) + { + return ZSTD_compressBegin_usingDict(cctx, null, 0, compressionLevel); + } + + /*! ZSTD_writeEpilogue() : + * Ends a frame. + * @return : nb of bytes written into dst (or an error code) */ + private static nuint ZSTD_writeEpilogue(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity) + { + byte* ostart = (byte*)(dst); + byte* op = ostart; + nuint fhSize = 0; + + if (cctx->stage == ZSTD_compressionStage_e.ZSTDcs_created) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + if (cctx->stage == ZSTD_compressionStage_e.ZSTDcs_init) + { + fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, &cctx->appliedParams, 0, 0); + + { + nuint err_code = (fhSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + dstCapacity -= fhSize; + op += fhSize; + cctx->stage = ZSTD_compressionStage_e.ZSTDcs_ongoing; + } + + if (cctx->stage != ZSTD_compressionStage_e.ZSTDcs_ending) + { + uint cBlockHeader24 = 1 + (((uint)(blockType_e.bt_raw)) << 1) + 0; + + if (dstCapacity < 4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + MEM_writeLE32((void*)op, cBlockHeader24); + op += ZSTD_blockHeaderSize; + dstCapacity -= ZSTD_blockHeaderSize; + } + + if (cctx->appliedParams.fParams.checksumFlag != 0) + { + uint checksum = (uint)(XXH64_digest(&cctx->xxhState)); + + if (dstCapacity < 4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + MEM_writeLE32((void*)op, checksum); + op += 4; + } + + cctx->stage = ZSTD_compressionStage_e.ZSTDcs_created; + return (nuint)(op - ostart); + } + + /** ZSTD_CCtx_trace() : + * Trace the end of a compression call. + */ + public static void ZSTD_CCtx_trace(ZSTD_CCtx_s* cctx, nuint extraCSize) + { + } + + public static nuint ZSTD_compressEnd(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + nuint endResult; + nuint cSize = ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1, 1); + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + endResult = ZSTD_writeEpilogue(cctx, (void*)((sbyte*)(dst) + cSize), dstCapacity - cSize); + + { + nuint err_code = (endResult); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(!(cctx->appliedParams.fParams.contentSizeFlag != 0 && cctx->pledgedSrcSizePlusOne == 0)); + if (cctx->pledgedSrcSizePlusOne != 0) + { + if (cctx->pledgedSrcSizePlusOne != cctx->consumedSrcSize + 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + } + + ZSTD_CCtx_trace(cctx, endResult); + return cSize + endResult; + } + + /*! ZSTD_compress_advanced() : + * Note : this function is now DEPRECATED. + * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters. + * This prototype will generate compilation warnings. */ + public static nuint ZSTD_compress_advanced(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, void* dict, nuint dictSize, ZSTD_parameters @params) + { + + { + nuint err_code = (ZSTD_checkCParams(@params.cParams)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ZSTD_CCtxParams_init_internal(&cctx->simpleApiParams, &@params, 0); + return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, dict, dictSize, &cctx->simpleApiParams); + } + + /* Internal */ + public static nuint ZSTD_compress_advanced_internal(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, void* dict, nuint dictSize, ZSTD_CCtx_params_s* @params) + { + + { + nuint err_code = (ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dictContentType_e.ZSTD_dct_auto, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, (ZSTD_CDict_s*)null, @params, (ulong)srcSize, ZSTD_buffered_policy_e.ZSTDb_not_buffered)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); + } + + /************************** + * Simple dictionary API + ***************************/ + /*! ZSTD_compress_usingDict() : + * Compression at an explicit compression level using a Dictionary. + * A dictionary can be any arbitrary data segment (also called a prefix), + * or a buffer with specified information (see zdict.h). + * Note : This function loads the dictionary, resulting in significant startup delay. + * It's intended for a dictionary used only once. + * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */ + public static nuint ZSTD_compress_usingDict(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, void* dict, nuint dictSize, int compressionLevel) + { + + { + ZSTD_parameters @params = ZSTD_getParams_internal(compressionLevel, (ulong)srcSize, dict != null ? dictSize : 0, ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict); + + assert(@params.fParams.contentSizeFlag == 1); + ZSTD_CCtxParams_init_internal(&cctx->simpleApiParams, &@params, (compressionLevel == 0) ? 3 : compressionLevel); + } + + return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, dict, dictSize, &cctx->simpleApiParams); + } + + /*! ZSTD_compressCCtx() : + * Same as ZSTD_compress(), using an explicit ZSTD_CCtx. + * Important : in order to behave similarly to `ZSTD_compress()`, + * this function compresses at requested compression level, + * __ignoring any other parameter__ . + * If any advanced parameter was set using the advanced API, + * they will all be reset. Only `compressionLevel` remains. + */ + public static nuint ZSTD_compressCCtx(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, int compressionLevel) + { + assert(cctx != null); + return ZSTD_compress_usingDict(cctx, dst, dstCapacity, src, srcSize, null, 0, compressionLevel); + } + + /*************************************** + * Simple API + ***************************************/ + /*! ZSTD_compress() : + * Compresses `src` content as a single zstd compressed frame into already allocated `dst`. + * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. + * @return : compressed size written into `dst` (<= `dstCapacity), + * or an error code if it fails (which can be tested using ZSTD_isError()). */ + public static nuint ZSTD_compress(void* dst, nuint dstCapacity, void* src, nuint srcSize, int compressionLevel) + { + nuint result; + ZSTD_CCtx_s ctxBody; + + ZSTD_initCCtx(&ctxBody, ZSTD_defaultCMem); + result = ZSTD_compressCCtx(&ctxBody, dst, dstCapacity, src, srcSize, compressionLevel); + ZSTD_freeCCtxContent(&ctxBody); + return result; + } + + /*! ZSTD_estimateCDictSize_advanced() : + * Estimate amount of memory that will be needed to create a dictionary with following arguments */ + public static nuint ZSTD_estimateCDictSize_advanced(nuint dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod) + { + return ZSTD_cwksp_alloc_size((nuint)(sizeof(ZSTD_CDict_s))) + ZSTD_cwksp_alloc_size((nuint)(((6 << 10) + 256))) + ZSTD_sizeof_matchState(&cParams, ZSTD_resolveRowMatchFinderMode(ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto, &cParams), 1, 0) + (dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef ? 0 : ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(dictSize, (nuint)(sizeof(void*))))); + } + + /*! ZSTD_estimate?DictSize() : + * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict(). + * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced(). + * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller. + */ + public static nuint ZSTD_estimateCDictSize(nuint dictSize, int compressionLevel) + { + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal(compressionLevel, (unchecked(0UL - 1)), dictSize, ZSTD_cParamMode_e.ZSTD_cpm_createCDict); + + return ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy); + } + + public static nuint ZSTD_sizeof_CDict(ZSTD_CDict_s* cdict) + { + if (cdict == null) + { + return 0; + } + + return (cdict->workspace.workspace == cdict ? 0 : (nuint)(sizeof(ZSTD_CDict_s))) + ZSTD_cwksp_sizeof(&cdict->workspace); + } + + private static nuint ZSTD_initCDict_internal(ZSTD_CDict_s* cdict, void* dictBuffer, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType, ZSTD_CCtx_params_s @params) + { + assert((ZSTD_checkCParams(@params.cParams)) == 0); + cdict->matchState.cParams = @params.cParams; + cdict->matchState.dedicatedDictSearch = @params.enableDedicatedDictSearch; + if ((dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef) || dictBuffer == null || dictSize == 0) + { + cdict->dictContent = dictBuffer; + } + else + { + void* internalBuffer = ZSTD_cwksp_reserve_object(&cdict->workspace, ZSTD_cwksp_align(dictSize, (nuint)(sizeof(void*)))); + + if (internalBuffer == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + cdict->dictContent = internalBuffer; + memcpy((internalBuffer), (dictBuffer), (dictSize)); + } + + cdict->dictContentSize = dictSize; + cdict->dictContentType = dictContentType; + cdict->entropyWorkspace = (uint*)(ZSTD_cwksp_reserve_object(&cdict->workspace, (nuint)(((6 << 10) + 256)))); + ZSTD_reset_compressedBlockState(&cdict->cBlockState); + + { + nuint err_code = (ZSTD_reset_matchState(&cdict->matchState, &cdict->workspace, &@params.cParams, @params.useRowMatchFinder, ZSTD_compResetPolicy_e.ZSTDcrp_makeClean, ZSTD_indexResetPolicy_e.ZSTDirp_reset, ZSTD_resetTarget_e.ZSTD_resetTarget_CDict)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + @params.compressionLevel = 3; + @params.fParams.contentSizeFlag = 1; + + { + nuint dictID = ZSTD_compress_insertDictionary(&cdict->cBlockState, &cdict->matchState, (ldmState_t*)null, &cdict->workspace, &@params, cdict->dictContent, cdict->dictContentSize, dictContentType, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_full, (void*)cdict->entropyWorkspace); + + + { + nuint err_code = (dictID); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(dictID <= (nuint)(unchecked((uint)(-1)))); + cdict->dictID = (uint)(dictID); + } + } + + return 0; + } + + private static ZSTD_CDict_s* ZSTD_createCDict_advanced_internal(nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_compressionParameters cParams, ZSTD_useRowMatchFinderMode_e useRowMatchFinder, uint enableDedicatedDictSearch, ZSTD_customMem customMem) + { + if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + { + return (ZSTD_CDict_s*)null; + } + + + { + nuint workspaceSize = ZSTD_cwksp_alloc_size((nuint)(sizeof(ZSTD_CDict_s))) + ZSTD_cwksp_alloc_size((nuint)(((6 << 10) + 256))) + ZSTD_sizeof_matchState(&cParams, useRowMatchFinder, enableDedicatedDictSearch, 0) + (dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef ? 0 : ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(dictSize, (nuint)(sizeof(void*))))); + void* workspace = ZSTD_customMalloc(workspaceSize, customMem); + ZSTD_cwksp ws; + ZSTD_CDict_s* cdict; + + if (workspace == null) + { + ZSTD_customFree(workspace, customMem); + return (ZSTD_CDict_s*)null; + } + + ZSTD_cwksp_init(&ws, workspace, workspaceSize, ZSTD_cwksp_static_alloc_e.ZSTD_cwksp_dynamic_alloc); + cdict = (ZSTD_CDict_s*)(ZSTD_cwksp_reserve_object(&ws, (nuint)(sizeof(ZSTD_CDict_s)))); + assert(cdict != null); + ZSTD_cwksp_move(&cdict->workspace, &ws); + cdict->customMem = customMem; + cdict->compressionLevel = 0; + cdict->useRowMatchFinder = useRowMatchFinder; + return cdict; + } + } + + public static ZSTD_CDict_s* ZSTD_createCDict_advanced(void* dictBuffer, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType, ZSTD_compressionParameters cParams, ZSTD_customMem customMem) + { + ZSTD_CCtx_params_s cctxParams; + + memset((void*)(&cctxParams), (0), ((nuint)(sizeof(ZSTD_CCtx_params_s)))); + ZSTD_CCtxParams_init(&cctxParams, 0); + cctxParams.cParams = cParams; + cctxParams.customMem = customMem; + return ZSTD_createCDict_advanced2(dictBuffer, dictSize, dictLoadMethod, dictContentType, &cctxParams, customMem); + } + + /* + * This API is temporary and is expected to change or disappear in the future! + */ + public static ZSTD_CDict_s* ZSTD_createCDict_advanced2(void* dict, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType, ZSTD_CCtx_params_s* originalCctxParams, ZSTD_customMem customMem) + { + ZSTD_CCtx_params_s cctxParams = *originalCctxParams; + ZSTD_compressionParameters cParams; + ZSTD_CDict_s* cdict; + + if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + { + return (ZSTD_CDict_s*)null; + } + + if (cctxParams.enableDedicatedDictSearch != 0) + { + cParams = ZSTD_dedicatedDictSearch_getCParams(cctxParams.compressionLevel, dictSize); + ZSTD_overrideCParams(&cParams, &cctxParams.cParams); + } + else + { + cParams = ZSTD_getCParamsFromCCtxParams(&cctxParams, (unchecked(0UL - 1)), dictSize, ZSTD_cParamMode_e.ZSTD_cpm_createCDict); + } + + if ((ZSTD_dedicatedDictSearch_isSupported(&cParams)) == 0) + { + cctxParams.enableDedicatedDictSearch = 0; + cParams = ZSTD_getCParamsFromCCtxParams(&cctxParams, (unchecked(0UL - 1)), dictSize, ZSTD_cParamMode_e.ZSTD_cpm_createCDict); + } + + cctxParams.cParams = cParams; + cctxParams.useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams.useRowMatchFinder, &cParams); + cdict = ZSTD_createCDict_advanced_internal(dictSize, dictLoadMethod, cctxParams.cParams, cctxParams.useRowMatchFinder, (uint)cctxParams.enableDedicatedDictSearch, customMem); + if ((ERR_isError(ZSTD_initCDict_internal(cdict, dict, dictSize, dictLoadMethod, dictContentType, cctxParams))) != 0) + { + ZSTD_freeCDict(cdict); + return (ZSTD_CDict_s*)null; + } + + return cdict; + } + + /*! ZSTD_createCDict() : + * When compressing multiple messages or blocks using the same dictionary, + * it's recommended to digest the dictionary only once, since it's a costly operation. + * ZSTD_createCDict() will create a state from digesting a dictionary. + * The resulting state can be used for future compression operations with very limited startup cost. + * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. + * @dictBuffer can be released after ZSTD_CDict creation, because its content is copied within CDict. + * Note 1 : Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate @dictBuffer content. + * Note 2 : A ZSTD_CDict can be created from an empty @dictBuffer, + * in which case the only thing that it transports is the @compressionLevel. + * This can be useful in a pipeline featuring ZSTD_compress_usingCDict() exclusively, + * expecting a ZSTD_CDict parameter with any data, including those without a known dictionary. */ + public static ZSTD_CDict_s* ZSTD_createCDict(void* dict, nuint dictSize, int compressionLevel) + { + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal(compressionLevel, (unchecked(0UL - 1)), dictSize, ZSTD_cParamMode_e.ZSTD_cpm_createCDict); + ZSTD_CDict_s* cdict = ZSTD_createCDict_advanced(dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy, ZSTD_dictContentType_e.ZSTD_dct_auto, cParams, ZSTD_defaultCMem); + + if (cdict != null) + { + cdict->compressionLevel = (compressionLevel == 0) ? 3 : compressionLevel; + } + + return cdict; + } + + /*! ZSTD_createCDict_byReference() : + * Create a digested dictionary for compression + * Dictionary content is just referenced, not duplicated. + * As a consequence, `dictBuffer` **must** outlive CDict, + * and its content must remain unmodified throughout the lifetime of CDict. + * note: equivalent to ZSTD_createCDict_advanced(), with dictLoadMethod==ZSTD_dlm_byRef */ + public static ZSTD_CDict_s* ZSTD_createCDict_byReference(void* dict, nuint dictSize, int compressionLevel) + { + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal(compressionLevel, (unchecked(0UL - 1)), dictSize, ZSTD_cParamMode_e.ZSTD_cpm_createCDict); + ZSTD_CDict_s* cdict = ZSTD_createCDict_advanced(dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, ZSTD_dictContentType_e.ZSTD_dct_auto, cParams, ZSTD_defaultCMem); + + if (cdict != null) + { + cdict->compressionLevel = (compressionLevel == 0) ? 3 : compressionLevel; + } + + return cdict; + } + + /*! ZSTD_freeCDict() : + * Function frees memory allocated by ZSTD_createCDict(). + * If a NULL pointer is passed, no operation is performed. */ + public static nuint ZSTD_freeCDict(ZSTD_CDict_s* cdict) + { + if (cdict == null) + { + return 0; + } + + + { + ZSTD_customMem cMem = cdict->customMem; + int cdictInWorkspace = ZSTD_cwksp_owns_buffer(&cdict->workspace, (void*)cdict); + + ZSTD_cwksp_free(&cdict->workspace, cMem); + if (cdictInWorkspace == 0) + { + ZSTD_customFree((void*)cdict, cMem); + } + + return 0; + } + } + + /*! ZSTD_initStaticCDict_advanced() : + * Generate a digested dictionary in provided memory area. + * workspace: The memory area to emplace the dictionary into. + * Provided pointer must 8-bytes aligned. + * It must outlive dictionary usage. + * workspaceSize: Use ZSTD_estimateCDictSize() + * to determine how large workspace must be. + * cParams : use ZSTD_getCParams() to transform a compression level + * into its relevants cParams. + * @return : pointer to ZSTD_CDict*, or NULL if error (size too small) + * Note : there is no corresponding "free" function. + * Since workspace was allocated externally, it must be freed externally. + */ + public static ZSTD_CDict_s* ZSTD_initStaticCDict(void* workspace, nuint workspaceSize, void* dict, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType, ZSTD_compressionParameters cParams) + { + ZSTD_useRowMatchFinderMode_e useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(ZSTD_useRowMatchFinderMode_e.ZSTD_urm_auto, &cParams); + nuint matchStateSize = ZSTD_sizeof_matchState(&cParams, useRowMatchFinder, 1, 0); + nuint neededSize = ZSTD_cwksp_alloc_size((nuint)(sizeof(ZSTD_CDict_s))) + (dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef ? 0 : ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(dictSize, (nuint)(sizeof(void*))))) + ZSTD_cwksp_alloc_size((nuint)(((6 << 10) + 256))) + matchStateSize; + ZSTD_CDict_s* cdict; + ZSTD_CCtx_params_s @params; + + if (((nuint)(workspace) & 7) != 0) + { + return (ZSTD_CDict_s*)null; + } + + + { + ZSTD_cwksp ws; + + ZSTD_cwksp_init(&ws, workspace, workspaceSize, ZSTD_cwksp_static_alloc_e.ZSTD_cwksp_static_alloc); + cdict = (ZSTD_CDict_s*)(ZSTD_cwksp_reserve_object(&ws, (nuint)(sizeof(ZSTD_CDict_s)))); + if (cdict == null) + { + return (ZSTD_CDict_s*)null; + } + + ZSTD_cwksp_move(&cdict->workspace, &ws); + } + + if (workspaceSize < neededSize) + { + return (ZSTD_CDict_s*)null; + } + + ZSTD_CCtxParams_init(&@params, 0); + @params.cParams = cParams; + @params.useRowMatchFinder = useRowMatchFinder; + cdict->useRowMatchFinder = useRowMatchFinder; + if ((ERR_isError(ZSTD_initCDict_internal(cdict, dict, dictSize, dictLoadMethod, dictContentType, @params))) != 0) + { + return (ZSTD_CDict_s*)null; + } + + return cdict; + } + + /*! ZSTD_getCParamsFromCDict() : + * as the name implies */ + public static ZSTD_compressionParameters ZSTD_getCParamsFromCDict(ZSTD_CDict_s* cdict) + { + assert(cdict != null); + return cdict->matchState.cParams; + } + + /*! ZSTD_getDictID_fromCDict() : + * Provides the dictID of the dictionary loaded into `cdict`. + * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. + * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ + public static uint ZSTD_getDictID_fromCDict(ZSTD_CDict_s* cdict) + { + if (cdict == null) + { + return 0; + } + + return cdict->dictID; + } + + /* ZSTD_compressBegin_usingCDict_internal() : + * Implementation of various ZSTD_compressBegin_usingCDict* functions. + */ + private static nuint ZSTD_compressBegin_usingCDict_internal(ZSTD_CCtx_s* cctx, ZSTD_CDict_s* cdict, ZSTD_frameParameters fParams, ulong pledgedSrcSize) + { + ZSTD_CCtx_params_s cctxParams; + + if (cdict == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_wrong))); + } + + + { + ZSTD_parameters @params; + + @params.fParams = fParams; + @params.cParams = (pledgedSrcSize < (uint)((128 * (1 << 10))) || pledgedSrcSize < cdict->dictContentSize * (6UL) || pledgedSrcSize == (unchecked(0UL - 1)) || cdict->compressionLevel == 0) ? ZSTD_getCParamsFromCDict(cdict) : ZSTD_getCParams(cdict->compressionLevel, pledgedSrcSize, cdict->dictContentSize); + ZSTD_CCtxParams_init_internal(&cctxParams, &@params, cdict->compressionLevel); + } + + if (pledgedSrcSize != (unchecked(0UL - 1))) + { + uint limitedSrcSize = (uint)((pledgedSrcSize) < (1U << 19) ? (pledgedSrcSize) : (1U << 19)); + uint limitedSrcLog = limitedSrcSize > 1 ? ZSTD_highbit32(limitedSrcSize - 1) + 1 : 1; + + cctxParams.cParams.windowLog = ((cctxParams.cParams.windowLog) > (limitedSrcLog) ? (cctxParams.cParams.windowLog) : (limitedSrcLog)); + } + + return ZSTD_compressBegin_internal(cctx, null, 0, ZSTD_dictContentType_e.ZSTD_dct_auto, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, cdict, &cctxParams, pledgedSrcSize, ZSTD_buffered_policy_e.ZSTDb_not_buffered); + } + + /* ZSTD_compressBegin_usingCDict_advanced() : + * This function is DEPRECATED. + * cdict must be != NULL */ + public static nuint ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx_s* cctx, ZSTD_CDict_s* cdict, ZSTD_frameParameters fParams, ulong pledgedSrcSize) + { + return ZSTD_compressBegin_usingCDict_internal(cctx, cdict, fParams, pledgedSrcSize); + } + + /* ZSTD_compressBegin_usingCDict() : + * cdict must be != NULL */ + public static nuint ZSTD_compressBegin_usingCDict(ZSTD_CCtx_s* cctx, ZSTD_CDict_s* cdict) + { + ZSTD_frameParameters fParams = new ZSTD_frameParameters + { + contentSizeFlag = 0, + checksumFlag = 0, + noDictIDFlag = 0, + }; + + return ZSTD_compressBegin_usingCDict_internal(cctx, cdict, fParams, (unchecked(0UL - 1))); + } + + /*! ZSTD_compress_usingCDict_internal(): + * Implementation of various ZSTD_compress_usingCDict* functions. + */ + private static nuint ZSTD_compress_usingCDict_internal(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, ZSTD_CDict_s* cdict, ZSTD_frameParameters fParams) + { + + { + nuint err_code = (ZSTD_compressBegin_usingCDict_internal(cctx, cdict, fParams, (ulong)srcSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); + } + + /*! ZSTD_compress_usingCDict_advanced(): + * This function is DEPRECATED. + */ + public static nuint ZSTD_compress_usingCDict_advanced(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, ZSTD_CDict_s* cdict, ZSTD_frameParameters fParams) + { + return ZSTD_compress_usingCDict_internal(cctx, dst, dstCapacity, src, srcSize, cdict, fParams); + } + + /*! ZSTD_compress_usingCDict() : + * Compression using a digested Dictionary. + * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. + * Note that compression parameters are decided at CDict creation time + * while frame parameters are hardcoded */ + public static nuint ZSTD_compress_usingCDict(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, ZSTD_CDict_s* cdict) + { + ZSTD_frameParameters fParams = new ZSTD_frameParameters + { + contentSizeFlag = 1, + checksumFlag = 0, + noDictIDFlag = 0, + }; + + return ZSTD_compress_usingCDict_internal(cctx, dst, dstCapacity, src, srcSize, cdict, fParams); + } + + /* ****************************************************************** + * Streaming + ********************************************************************/ + public static ZSTD_CCtx_s* ZSTD_createCStream() + { + return ZSTD_createCStream_advanced(ZSTD_defaultCMem); + } + + public static ZSTD_CCtx_s* ZSTD_initStaticCStream(void* workspace, nuint workspaceSize) + { + return ZSTD_initStaticCCtx(workspace, workspaceSize); + } + + public static ZSTD_CCtx_s* ZSTD_createCStream_advanced(ZSTD_customMem customMem) + { + return ZSTD_createCCtx_advanced(customMem); + } + + public static nuint ZSTD_freeCStream(ZSTD_CCtx_s* zcs) + { + return ZSTD_freeCCtx(zcs); + } + + /*====== Initialization ======*/ + public static nuint ZSTD_CStreamInSize() + { + return (nuint)((1 << 17)); + } + + public static nuint ZSTD_CStreamOutSize() + { + return ZSTD_compressBound((nuint)((1 << 17))) + ZSTD_blockHeaderSize + 4; + } + + private static ZSTD_cParamMode_e ZSTD_getCParamMode(ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize) + { + if (cdict != null && (ZSTD_shouldAttachDict(cdict, @params, pledgedSrcSize)) != 0) + { + return ZSTD_cParamMode_e.ZSTD_cpm_attachDict; + } + else + { + return ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict; + } + } + + /* ZSTD_resetCStream(): + * pledgedSrcSize == 0 means "unknown" */ + public static nuint ZSTD_resetCStream(ZSTD_CCtx_s* zcs, ulong pss) + { + ulong pledgedSrcSize = (pss == 0) ? (unchecked(0UL - 1)) : pss; + + + { + nuint err_code = (ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_setPledgedSrcSize(zcs, unchecked(pledgedSrcSize))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /*! ZSTD_initCStream_internal() : + * Note : for lib/compress only. Used by zstdmt_compress.c. + * Assumption 1 : params are valid + * Assumption 2 : either dict, or cdict, is defined, not both */ + public static nuint ZSTD_initCStream_internal(ZSTD_CCtx_s* zcs, void* dict, nuint dictSize, ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize) + { + + { + nuint err_code = (ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert((ERR_isError(ZSTD_checkCParams(@params->cParams))) == 0); + zcs->requestedParams = *@params; + assert(!((dict) != null && (cdict) != null)); + if (dict != null) + { + + { + nuint err_code = (ZSTD_CCtx_loadDictionary(zcs, dict, dictSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + else + { + + { + nuint err_code = (ZSTD_CCtx_refCDict(zcs, cdict)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + + return 0; + } + + /* ZSTD_initCStream_usingCDict_advanced() : + * same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ + public static nuint ZSTD_initCStream_usingCDict_advanced(ZSTD_CCtx_s* zcs, ZSTD_CDict_s* cdict, ZSTD_frameParameters fParams, ulong pledgedSrcSize) + { + + { + nuint err_code = (ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + zcs->requestedParams.fParams = fParams; + + { + nuint err_code = (ZSTD_CCtx_refCDict(zcs, cdict)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /* note : cdict must outlive compression session */ + public static nuint ZSTD_initCStream_usingCDict(ZSTD_CCtx_s* zcs, ZSTD_CDict_s* cdict) + { + + { + nuint err_code = (ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_refCDict(zcs, cdict)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /* ZSTD_initCStream_advanced() : + * pledgedSrcSize must be exact. + * if srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. + * dict is loaded with default parameters ZSTD_dct_auto and ZSTD_dlm_byCopy. */ + public static nuint ZSTD_initCStream_advanced(ZSTD_CCtx_s* zcs, void* dict, nuint dictSize, ZSTD_parameters @params, ulong pss) + { + ulong pledgedSrcSize = (pss == 0 && @params.fParams.contentSizeFlag == 0) ? (unchecked(0UL - 1)) : pss; + + + { + nuint err_code = (ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_setPledgedSrcSize(zcs, unchecked(pledgedSrcSize))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_checkCParams(@params.cParams)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ZSTD_CCtxParams_setZstdParams(&zcs->requestedParams, &@params); + + { + nuint err_code = (ZSTD_CCtx_loadDictionary(zcs, dict, dictSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /*! ZSTD_initCStream_usingDict() : + * This function is DEPRECATED, and is equivalent to: + * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); + * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); + * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); + * + * Creates of an internal CDict (incompatible with static CCtx), except if + * dict == NULL or dictSize < 8, in which case no dict is used. + * Note: dict is loaded with ZSTD_dct_auto (treated as a full zstd dictionary if + * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy. + * This prototype will generate compilation warnings. + */ + public static nuint ZSTD_initCStream_usingDict(ZSTD_CCtx_s* zcs, void* dict, nuint dictSize, int compressionLevel) + { + + { + nuint err_code = (ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_setParameter(zcs, ZSTD_cParameter.ZSTD_c_compressionLevel, compressionLevel)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_loadDictionary(zcs, dict, dictSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /*! ZSTD_initCStream_srcSize() : + * This function is DEPRECATED, and equivalent to: + * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); + * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) + * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); + * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); + * + * pledgedSrcSize must be correct. If it is not known at init time, use + * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, + * "0" also disables frame content size field. It may be enabled in the future. + * This prototype will generate compilation warnings. + */ + public static nuint ZSTD_initCStream_srcSize(ZSTD_CCtx_s* zcs, int compressionLevel, ulong pss) + { + ulong pledgedSrcSize = (pss == 0) ? (unchecked(0UL - 1)) : pss; + + + { + nuint err_code = (ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_refCDict(zcs, (ZSTD_CDict_s*)null)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_setParameter(zcs, ZSTD_cParameter.ZSTD_c_compressionLevel, compressionLevel)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_setPledgedSrcSize(zcs, unchecked(pledgedSrcSize))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /*! + * Equivalent to: + * + * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); + * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) + * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); + */ + public static nuint ZSTD_initCStream(ZSTD_CCtx_s* zcs, int compressionLevel) + { + + { + nuint err_code = (ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_refCDict(zcs, (ZSTD_CDict_s*)null)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_CCtx_setParameter(zcs, ZSTD_cParameter.ZSTD_c_compressionLevel, compressionLevel)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /*====== Compression ======*/ + private static nuint ZSTD_nextInputSizeHint(ZSTD_CCtx_s* cctx) + { + nuint hintInSize = cctx->inBuffTarget - cctx->inBuffPos; + + if (hintInSize == 0) + { + hintInSize = cctx->blockSize; + } + + return hintInSize; + } + + /** ZSTD_compressStream_generic(): + * internal function for all *compressStream*() variants + * non-static, because can be called from zstdmt_compress.c + * @return : hint size for next input */ + private static nuint ZSTD_compressStream_generic(ZSTD_CCtx_s* zcs, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input, ZSTD_EndDirective flushMode) + { + sbyte* istart = (sbyte*)(input->src); + sbyte* iend = input->size != 0 ? istart + input->size : istart; + sbyte* ip = input->pos != 0 ? istart + input->pos : istart; + sbyte* ostart = (sbyte*)(output->dst); + sbyte* oend = output->size != 0 ? ostart + output->size : ostart; + sbyte* op = output->pos != 0 ? ostart + output->pos : ostart; + uint someMoreWork = 1; + + if (zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) + { + assert(zcs->inBuff != null); + assert(zcs->inBuffSize > 0); + } + + if (zcs->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) + { + assert(zcs->outBuff != null); + assert(zcs->outBuffSize > 0); + } + + assert(output->pos <= output->size); + assert(input->pos <= input->size); + assert((uint)(flushMode) <= (uint)(ZSTD_EndDirective.ZSTD_e_end)); + while (someMoreWork != 0) + { + switch (zcs->streamStage) + { + case ZSTD_cStreamStage.zcss_init: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_init_missing))); + } + } + + case ZSTD_cStreamStage.zcss_load: + { + if ((flushMode == ZSTD_EndDirective.ZSTD_e_end) && ((nuint)(oend - op) >= ZSTD_compressBound((nuint)(iend - ip)) || zcs->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable) && (zcs->inBuffPos == 0)) + { + nuint cSize = ZSTD_compressEnd(zcs, (void*)op, (nuint)(oend - op), (void*)ip, (nuint)(iend - ip)); + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ip = iend; + op += cSize; + zcs->frameEnded = 1; + ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only); + someMoreWork = 0; + break; + } + } + + if (zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) + { + nuint toLoad = zcs->inBuffTarget - zcs->inBuffPos; + nuint loaded = ZSTD_limitCopy((void*)(zcs->inBuff + zcs->inBuffPos), toLoad, (void*)ip, (nuint)(iend - ip)); + + zcs->inBuffPos += loaded; + if (loaded != 0) + { + ip += loaded; + } + + if ((flushMode == ZSTD_EndDirective.ZSTD_e_continue) && (zcs->inBuffPos < zcs->inBuffTarget)) + { + someMoreWork = 0; + break; + } + + if ((flushMode == ZSTD_EndDirective.ZSTD_e_flush) && (zcs->inBuffPos == zcs->inToCompress)) + { + someMoreWork = 0; + break; + } + } + + + { + int inputBuffered = ((zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) ? 1 : 0); + void* cDst; + nuint cSize; + nuint oSize = (nuint)(oend - op); + nuint iSize = inputBuffered != 0 ? zcs->inBuffPos - zcs->inToCompress : (((nuint)(iend - ip)) < (zcs->blockSize) ? ((nuint)(iend - ip)) : (zcs->blockSize)); + + if (oSize >= ZSTD_compressBound(iSize) || zcs->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable) + { + cDst = op; + } + else + { + cDst = zcs->outBuff; oSize = zcs->outBuffSize; + } + + if (inputBuffered != 0) + { + uint lastBlock = (((flushMode == ZSTD_EndDirective.ZSTD_e_end) && (ip == iend)) ? 1U : 0U); + + cSize = lastBlock != 0 ? ZSTD_compressEnd(zcs, cDst, oSize, (void*)(zcs->inBuff + zcs->inToCompress), iSize) : ZSTD_compressContinue(zcs, cDst, oSize, (void*)(zcs->inBuff + zcs->inToCompress), iSize); + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + zcs->frameEnded = lastBlock; + zcs->inBuffTarget = zcs->inBuffPos + zcs->blockSize; + if (zcs->inBuffTarget > zcs->inBuffSize) + { + zcs->inBuffPos = 0; zcs->inBuffTarget = zcs->blockSize; + } + + if (lastBlock == 0) + { + assert(zcs->inBuffTarget <= zcs->inBuffSize); + } + + zcs->inToCompress = zcs->inBuffPos; + } + else + { + uint lastBlock = (((ip + iSize == iend)) ? 1U : 0U); + + assert(flushMode == ZSTD_EndDirective.ZSTD_e_end); + cSize = lastBlock != 0 ? ZSTD_compressEnd(zcs, cDst, oSize, (void*)ip, iSize) : ZSTD_compressContinue(zcs, cDst, oSize, (void*)ip, iSize); + if (iSize > 0) + { + ip += iSize; + } + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + zcs->frameEnded = lastBlock; + if (lastBlock != 0) + { + assert(ip == iend); + } + } + + if (cDst == op) + { + op += cSize; + if (zcs->frameEnded != 0) + { + someMoreWork = 0; + ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only); + } + + break; + } + + zcs->outBuffContentSize = cSize; + zcs->outBuffFlushedSize = 0; + zcs->streamStage = ZSTD_cStreamStage.zcss_flush; + } + + + goto case ZSTD_cStreamStage.zcss_flush; + case ZSTD_cStreamStage.zcss_flush: + ; + assert(zcs->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered); + + { + nuint toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; + nuint flushed = ZSTD_limitCopy((void*)op, (nuint)(oend - op), (void*)(zcs->outBuff + zcs->outBuffFlushedSize), toFlush); + + if (flushed != 0) + { + op += flushed; + } + + zcs->outBuffFlushedSize += flushed; + if (toFlush != flushed) + { + assert(op == oend); + someMoreWork = 0; + break; + } + + zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; + if (zcs->frameEnded != 0) + { + someMoreWork = 0; + ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only); + break; + } + + zcs->streamStage = ZSTD_cStreamStage.zcss_load; + break; + } + + default: + { + assert(0 != 0); + } + break; + } + } + + input->pos = (nuint)(ip - istart); + output->pos = (nuint)(op - ostart); + if (zcs->frameEnded != 0) + { + return 0; + } + + return ZSTD_nextInputSizeHint(zcs); + } + + private static nuint ZSTD_nextInputSizeHint_MTorST(ZSTD_CCtx_s* cctx) + { + return ZSTD_nextInputSizeHint(cctx); + } + + /*! + * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue). + * NOTE: The return value is different. ZSTD_compressStream() returns a hint for + * the next read size (if non-zero and not an error). ZSTD_compressStream2() + * returns the minimum nb of bytes left to flush (if non-zero and not an error). + */ + public static nuint ZSTD_compressStream(ZSTD_CCtx_s* zcs, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input) + { + + { + nuint err_code = (ZSTD_compressStream2(zcs, output, input, ZSTD_EndDirective.ZSTD_e_continue)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_nextInputSizeHint_MTorST(zcs); + } + + /* After a compression call set the expected input/output buffer. + * This is validated at the start of the next compression call. + */ + private static void ZSTD_setBufferExpectations(ZSTD_CCtx_s* cctx, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input) + { + if (cctx->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable) + { + cctx->expectedInBuffer = *input; + } + + if (cctx->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable) + { + cctx->expectedOutBufferSize = output->size - output->pos; + } + } + + /* Validate that the input/output buffers match the expectations set by + * ZSTD_setBufferExpectations. + */ + private static nuint ZSTD_checkBufferStability(ZSTD_CCtx_s* cctx, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input, ZSTD_EndDirective endOp) + { + if (cctx->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable) + { + ZSTD_inBuffer_s expect = cctx->expectedInBuffer; + + if (expect.src != input->src || expect.pos != input->pos || expect.size != input->size) + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcBuffer_wrong))); + } + } + + if (endOp != ZSTD_EndDirective.ZSTD_e_end) + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcBuffer_wrong))); + } + } + + } + + if (cctx->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable) + { + nuint outBufferSize = output->size - output->pos; + + if (cctx->expectedOutBufferSize != outBufferSize) + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstBuffer_wrong))); + } + } + + } + + return 0; + } + + private static nuint ZSTD_CCtx_init_compressStream2(ZSTD_CCtx_s* cctx, ZSTD_EndDirective endOp, nuint inSize) + { + ZSTD_CCtx_params_s @params = cctx->requestedParams; + ZSTD_prefixDict_s prefixDict = cctx->prefixDict; + + + { + nuint err_code = (ZSTD_initLocalDict(cctx)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + memset((void*)(&cctx->prefixDict), (0), ((nuint)(sizeof(ZSTD_prefixDict_s)))); + assert(prefixDict.dict == null || cctx->cdict == null); + if (cctx->cdict != null && cctx->localDict.cdict == null) + { + @params.compressionLevel = cctx->cdict->compressionLevel; + } + + if (endOp == ZSTD_EndDirective.ZSTD_e_end) + { + cctx->pledgedSrcSizePlusOne = (ulong)(inSize + 1); + } + + + { + nuint dictSize = prefixDict.dict != null ? prefixDict.dictSize : (cctx->cdict != null ? cctx->cdict->dictContentSize : 0); + ZSTD_cParamMode_e mode = ZSTD_getCParamMode(cctx->cdict, &@params, cctx->pledgedSrcSizePlusOne - 1); + + @params.cParams = ZSTD_getCParamsFromCCtxParams(&@params, cctx->pledgedSrcSizePlusOne - 1, dictSize, mode); + } + + if ((ZSTD_CParams_shouldEnableLdm(&@params.cParams)) != 0) + { + @params.ldmParams.enableLdm = 1; + } + + if ((ZSTD_CParams_useBlockSplitter(&@params.cParams)) != 0) + { + @params.splitBlocks = 1; + } + + @params.useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(@params.useRowMatchFinder, &@params.cParams); + + { + ulong pledgedSrcSize = cctx->pledgedSrcSizePlusOne - 1; + + assert((ERR_isError(ZSTD_checkCParams(@params.cParams))) == 0); + + { + nuint err_code = (ZSTD_compressBegin_internal(cctx, prefixDict.dict, prefixDict.dictSize, prefixDict.dictContentType, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, cctx->cdict, &@params, pledgedSrcSize, ZSTD_buffered_policy_e.ZSTDb_buffered)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(cctx->appliedParams.nbWorkers == 0); + cctx->inToCompress = 0; + cctx->inBuffPos = 0; + if (cctx->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) + { + cctx->inBuffTarget = cctx->blockSize + (uint)((((cctx->blockSize == pledgedSrcSize)) ? 1 : 0)); + } + else + { + cctx->inBuffTarget = 0; + } + + cctx->outBuffContentSize = cctx->outBuffFlushedSize = 0; + cctx->streamStage = ZSTD_cStreamStage.zcss_load; + cctx->frameEnded = 0; + } + + return 0; + } + + /*! ZSTD_compressStream2() : Requires v1.4.0+ + * Behaves about the same as ZSTD_compressStream, with additional control on end directive. + * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() + * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode) + * - output->pos must be <= dstCapacity, input->pos must be <= srcSize + * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. + * - endOp must be a valid directive + * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller. + * - When nbWorkers>=1, function is non-blocking : it copies a portion of input, distributes jobs to internal worker threads, flush to output whatever is available, + * and then immediately returns, just indicating that there is some data remaining to be flushed. + * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. + * - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking. + * - @return provides a minimum amount of data remaining to be flushed from internal buffers + * or an error code, which can be tested using ZSTD_isError(). + * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. + * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. + * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. + * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), + * only ZSTD_e_end or ZSTD_e_flush operations are allowed. + * Before starting a new compression job, or changing compression parameters, + * it is required to fully flush internal buffers. + */ + public static nuint ZSTD_compressStream2(ZSTD_CCtx_s* cctx, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input, ZSTD_EndDirective endOp) + { + if (output->pos > output->size) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (input->pos > input->size) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if ((uint)(endOp) > (uint)(ZSTD_EndDirective.ZSTD_e_end)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + assert(cctx != null); + if (cctx->streamStage == ZSTD_cStreamStage.zcss_init) + { + + { + nuint err_code = (ZSTD_CCtx_init_compressStream2(cctx, endOp, input->size)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ZSTD_setBufferExpectations(cctx, output, input); + } + + + { + nuint err_code = (ZSTD_checkBufferStability(cctx, output, input, endOp)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_compressStream_generic(cctx, output, input, endOp)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ZSTD_setBufferExpectations(cctx, output, input); + return cctx->outBuffContentSize - cctx->outBuffFlushedSize; + } + + /*! ZSTD_compressStream2_simpleArgs() : + * Same as ZSTD_compressStream2(), + * but using only integral types as arguments. + * This variant might be helpful for binders from dynamic languages + * which have troubles handling structures containing memory pointers. + */ + public static nuint ZSTD_compressStream2_simpleArgs(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, nuint* dstPos, void* src, nuint srcSize, nuint* srcPos, ZSTD_EndDirective endOp) + { + ZSTD_outBuffer_s output = new ZSTD_outBuffer_s + { + dst = dst, + size = dstCapacity, + pos = *dstPos, + }; + ZSTD_inBuffer_s input = new ZSTD_inBuffer_s + { + src = src, + size = srcSize, + pos = *srcPos, + }; + nuint cErr = ZSTD_compressStream2(cctx, &output, &input, endOp); + + *dstPos = output.pos; + *srcPos = input.pos; + return cErr; + } + + /*! ZSTD_compress2() : + * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API. + * ZSTD_compress2() always starts a new frame. + * Should cctx hold data from a previously unfinished frame, everything about it is forgotten. + * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() + * - The function is always blocking, returns when compression is completed. + * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. + * @return : compressed size written into `dst` (<= `dstCapacity), + * or an error code if it fails (which can be tested using ZSTD_isError()). + */ + public static nuint ZSTD_compress2(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + ZSTD_bufferMode_e originalInBufferMode = cctx->requestedParams.inBufferMode; + ZSTD_bufferMode_e originalOutBufferMode = cctx->requestedParams.outBufferMode; + + ZSTD_CCtx_reset(cctx, ZSTD_ResetDirective.ZSTD_reset_session_only); + cctx->requestedParams.inBufferMode = ZSTD_bufferMode_e.ZSTD_bm_stable; + cctx->requestedParams.outBufferMode = ZSTD_bufferMode_e.ZSTD_bm_stable; + + { + nuint oPos = 0; + nuint iPos = 0; + nuint result = ZSTD_compressStream2_simpleArgs(cctx, dst, dstCapacity, &oPos, src, srcSize, &iPos, ZSTD_EndDirective.ZSTD_e_end); + + cctx->requestedParams.inBufferMode = originalInBufferMode; + cctx->requestedParams.outBufferMode = originalOutBufferMode; + + { + nuint err_code = (result); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (result != 0) + { + assert(oPos == dstCapacity); + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + } + + assert(iPos == srcSize); + return oPos; + } + } + + /* Returns a ZSTD error code if sequence is not valid */ + private static nuint ZSTD_validateSequence(uint offCode, uint matchLength, nuint posInSrc, uint windowLog, nuint dictSize, uint minMatch) + { + nuint offsetBound; + uint windowSize = (uint)(1 << (int)windowLog); + + offsetBound = posInSrc > windowSize ? (nuint)(windowSize) : posInSrc + (nuint)(dictSize); + if (offCode > offsetBound + (uint)((3 - 1))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (matchLength < minMatch) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + return 0; + } + + /* Returns an offset code, given a sequence's raw offset, the ongoing repcode array, and whether litLength == 0 */ + private static uint ZSTD_finalizeOffCode(uint rawOffset, uint* rep, uint ll0) + { + uint offCode = rawOffset + (uint)((3 - 1)); + uint repCode = 0; + + if (ll0 == 0 && rawOffset == rep[0]) + { + repCode = 1; + } + else if (rawOffset == rep[1]) + { + repCode = 2 - ll0; + } + else if (rawOffset == rep[2]) + { + repCode = 3 - ll0; + } + else if (ll0 != 0 && rawOffset == rep[0] - 1) + { + repCode = 3; + } + + if (repCode != 0) + { + offCode = repCode - 1; + } + + return offCode; + } + + /* Returns 0 on success, and a ZSTD_error otherwise. This function scans through an array of + * ZSTD_Sequence, storing the sequences it finds, until it reaches a block delimiter. + */ + private static nuint ZSTD_copySequencesToSeqStoreExplicitBlockDelim(ZSTD_CCtx_s* cctx, ZSTD_sequencePosition* seqPos, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* src, nuint blockSize) + { + uint idx = seqPos->idx; + byte* ip = (byte*)(src); + byte* iend = ip + blockSize; + repcodes_s updatedRepcodes; + uint dictSize; + uint litLength; + uint matchLength; + uint ll0; + uint offCode; + + if (cctx->cdict != null) + { + dictSize = (uint)(cctx->cdict->dictContentSize); + } + else if (cctx->prefixDict.dict != null) + { + dictSize = (uint)(cctx->prefixDict.dictSize); + } + else + { + dictSize = 0; + } + + memcpy((void*)(updatedRepcodes.rep), (void*)(cctx->blockState.prevCBlock->rep), ((nuint)(sizeof(repcodes_s)))); + for (; (inSeqs[idx].matchLength != 0 || inSeqs[idx].offset != 0) && idx < inSeqsSize; ++idx) + { + litLength = inSeqs[idx].litLength; + matchLength = inSeqs[idx].matchLength; + ll0 = ((litLength == 0) ? 1U : 0U); + offCode = ZSTD_finalizeOffCode(inSeqs[idx].offset, updatedRepcodes.rep, ll0); + updatedRepcodes = ZSTD_updateRep(updatedRepcodes.rep, offCode, ll0); + if (cctx->appliedParams.validateSequences != 0) + { + seqPos->posInSrc += litLength + matchLength; + + { + nuint err_code = (ZSTD_validateSequence(offCode, matchLength, seqPos->posInSrc, cctx->appliedParams.cParams.windowLog, dictSize, cctx->appliedParams.cParams.minMatch)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + + if (idx - seqPos->idx > cctx->seqStore.maxNbSeq) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offCode, matchLength - 3); + ip += matchLength + litLength; + } + + memcpy((void*)(cctx->blockState.nextCBlock->rep), (void*)(updatedRepcodes.rep), ((nuint)(sizeof(repcodes_s)))); + if ((inSeqs[idx].litLength) != 0) + { + ZSTD_storeLastLiterals(&cctx->seqStore, ip, inSeqs[idx].litLength); + ip += inSeqs[idx].litLength; + seqPos->posInSrc += inSeqs[idx].litLength; + } + + if (ip != iend) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + seqPos->idx = idx + 1; + return 0; + } + + /* Returns the number of bytes to move the current read position back by. Only non-zero + * if we ended up splitting a sequence. Otherwise, it may return a ZSTD error if something + * went wrong. + * + * This function will attempt to scan through blockSize bytes represented by the sequences + * in inSeqs, storing any (partial) sequences. + * + * Occasionally, we may want to change the actual number of bytes we consumed from inSeqs to + * avoid splitting a match, or to avoid splitting a match such that it would produce a match + * smaller than MINMATCH. In this case, we return the number of bytes that we didn't read from this block. + */ + private static nuint ZSTD_copySequencesToSeqStoreNoBlockDelim(ZSTD_CCtx_s* cctx, ZSTD_sequencePosition* seqPos, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* src, nuint blockSize) + { + uint idx = seqPos->idx; + uint startPosInSequence = seqPos->posInSequence; + uint endPosInSequence = seqPos->posInSequence + (uint)(blockSize); + nuint dictSize; + byte* ip = (byte*)(src); + byte* iend = ip + blockSize; + repcodes_s updatedRepcodes; + uint bytesAdjustment = 0; + uint finalMatchSplit = 0; + uint litLength; + uint matchLength; + uint rawOffset; + uint offCode; + + if (cctx->cdict != null) + { + dictSize = cctx->cdict->dictContentSize; + } + else if (cctx->prefixDict.dict != null) + { + dictSize = cctx->prefixDict.dictSize; + } + else + { + dictSize = 0; + } + + memcpy((void*)(updatedRepcodes.rep), (void*)(cctx->blockState.prevCBlock->rep), ((nuint)(sizeof(repcodes_s)))); + while (endPosInSequence != 0 && idx < inSeqsSize && finalMatchSplit == 0) + { + ZSTD_Sequence currSeq = inSeqs[idx]; + + litLength = currSeq.litLength; + matchLength = currSeq.matchLength; + rawOffset = currSeq.offset; + if (endPosInSequence >= currSeq.litLength + currSeq.matchLength) + { + if (startPosInSequence >= litLength) + { + startPosInSequence -= litLength; + litLength = 0; + matchLength -= startPosInSequence; + } + else + { + litLength -= startPosInSequence; + } + + endPosInSequence -= currSeq.litLength + currSeq.matchLength; + startPosInSequence = 0; + idx++; + } + else + { + if (endPosInSequence > litLength) + { + uint firstHalfMatchLength; + + litLength = (uint)(startPosInSequence >= litLength ? 0 : litLength - startPosInSequence); + firstHalfMatchLength = endPosInSequence - startPosInSequence - litLength; + if (matchLength > blockSize && firstHalfMatchLength >= cctx->appliedParams.cParams.minMatch) + { + uint secondHalfMatchLength = currSeq.matchLength + currSeq.litLength - endPosInSequence; + + if (secondHalfMatchLength < cctx->appliedParams.cParams.minMatch) + { + endPosInSequence -= cctx->appliedParams.cParams.minMatch - secondHalfMatchLength; + bytesAdjustment = cctx->appliedParams.cParams.minMatch - secondHalfMatchLength; + firstHalfMatchLength -= bytesAdjustment; + } + + matchLength = firstHalfMatchLength; + finalMatchSplit = 1; + } + else + { + bytesAdjustment = endPosInSequence - currSeq.litLength; + endPosInSequence = currSeq.litLength; + break; + } + } + else + { + break; + } + } + + + { + uint ll0 = (((litLength == 0)) ? 1U : 0U); + + offCode = ZSTD_finalizeOffCode(rawOffset, updatedRepcodes.rep, ll0); + updatedRepcodes = ZSTD_updateRep(updatedRepcodes.rep, offCode, ll0); + } + + if (cctx->appliedParams.validateSequences != 0) + { + seqPos->posInSrc += litLength + matchLength; + + { + nuint err_code = (ZSTD_validateSequence(offCode, matchLength, seqPos->posInSrc, cctx->appliedParams.cParams.windowLog, dictSize, cctx->appliedParams.cParams.minMatch)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + + if (idx - seqPos->idx > cctx->seqStore.maxNbSeq) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offCode, matchLength - 3); + ip += matchLength + litLength; + } + + assert(idx == inSeqsSize || endPosInSequence <= inSeqs[idx].litLength + inSeqs[idx].matchLength); + seqPos->idx = idx; + seqPos->posInSequence = endPosInSequence; + memcpy((void*)(cctx->blockState.nextCBlock->rep), (void*)(updatedRepcodes.rep), ((nuint)(sizeof(repcodes_s)))); + iend -= bytesAdjustment; + if (ip != iend) + { + uint lastLLSize = (uint)(iend - ip); + + assert(ip <= iend); + ZSTD_storeLastLiterals(&cctx->seqStore, ip, lastLLSize); + seqPos->posInSrc += lastLLSize; + } + + return bytesAdjustment; + } + + private static ZSTD_sequenceCopier? ZSTD_selectSequenceCopier(ZSTD_sequenceFormat_e mode) + { + ZSTD_sequenceCopier? sequenceCopier = null; + + assert((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam11, (int)mode)) != 0); + if (mode == ZSTD_sequenceFormat_e.ZSTD_sf_explicitBlockDelimiters) + { + return (ZSTD_sequenceCopier)ZSTD_copySequencesToSeqStoreExplicitBlockDelim; + } + else if (mode == ZSTD_sequenceFormat_e.ZSTD_sf_noBlockDelimiters) + { + return (ZSTD_sequenceCopier)ZSTD_copySequencesToSeqStoreNoBlockDelim; + } + + assert(sequenceCopier != null); + return sequenceCopier; + } + + /* Compress, block-by-block, all of the sequences given. + * + * Returns the cumulative size of all compressed blocks (including their headers), otherwise a ZSTD error. + */ + private static nuint ZSTD_compressSequences_internal(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* src, nuint srcSize) + { + nuint cSize = 0; + uint lastBlock; + nuint blockSize; + nuint compressedSeqsSize; + nuint remaining = srcSize; + ZSTD_sequencePosition seqPos = new ZSTD_sequencePosition + { + idx = 0, + posInSequence = 0, + posInSrc = 0, + }; + byte* ip = (byte*)(src); + byte* op = (byte*)(dst); + ZSTD_sequenceCopier sequenceCopier = ZSTD_selectSequenceCopier(cctx->appliedParams.blockDelimiters) ?? throw new InvalidOperationException(); + + if (remaining == 0) + { + uint cBlockHeader24 = 1 + (((uint)(blockType_e.bt_raw)) << 1); + + if (dstCapacity < 4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + MEM_writeLE32((void*)op, cBlockHeader24); + op += ZSTD_blockHeaderSize; + dstCapacity -= ZSTD_blockHeaderSize; + cSize += ZSTD_blockHeaderSize; + } + + while (remaining != 0) + { + nuint cBlockSize; + nuint additionalByteAdjustment; + + lastBlock = ((remaining <= cctx->blockSize) ? 1U : 0U); + blockSize = lastBlock != 0 ? (uint)(remaining) : (uint)(cctx->blockSize); + ZSTD_resetSeqStore(&cctx->seqStore); + additionalByteAdjustment = sequenceCopier(cctx, &seqPos, inSeqs, inSeqsSize, (void*)ip, blockSize); + + { + nuint err_code = (additionalByteAdjustment); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + blockSize -= additionalByteAdjustment; + if (blockSize < (uint)((1 + 1 + 1)) + ZSTD_blockHeaderSize + 1) + { + cBlockSize = ZSTD_noCompressBlock((void*)op, dstCapacity, (void*)ip, blockSize, lastBlock); + + { + nuint err_code = (cBlockSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + cSize += cBlockSize; + ip += blockSize; + op += cBlockSize; + remaining -= blockSize; + dstCapacity -= cBlockSize; + continue; + } + + compressedSeqsSize = ZSTD_entropyCompressSeqStore(&cctx->seqStore, &cctx->blockState.prevCBlock->entropy, &cctx->blockState.nextCBlock->entropy, &cctx->appliedParams, (void*)(op + ZSTD_blockHeaderSize), dstCapacity - ZSTD_blockHeaderSize, blockSize, (void*)cctx->entropyWorkspace, ((uint)(((6 << 10) + 256)) + ((nuint)(sizeof(uint)) * (uint)((((35) > (52) ? (35) : (52)) + 2)))), cctx->bmi2); + + { + nuint err_code = (compressedSeqsSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (cctx->isFirstBlock == 0 && (ZSTD_maybeRLE(&cctx->seqStore)) != 0 && (ZSTD_isRLE((byte*)(src), srcSize)) != 0) + { + compressedSeqsSize = 1; + } + + if (compressedSeqsSize == 0) + { + cBlockSize = ZSTD_noCompressBlock((void*)op, dstCapacity, (void*)ip, blockSize, lastBlock); + + { + nuint err_code = (cBlockSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + else if (compressedSeqsSize == 1) + { + cBlockSize = ZSTD_rleCompressBlock((void*)op, dstCapacity, *ip, blockSize, lastBlock); + + { + nuint err_code = (cBlockSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + else + { + uint cBlockHeader; + + ZSTD_blockState_confirmRepcodesAndEntropyTables(&cctx->blockState); + if (cctx->blockState.prevCBlock->entropy.fse.offcode_repeatMode == FSE_repeat.FSE_repeat_valid) + { + cctx->blockState.prevCBlock->entropy.fse.offcode_repeatMode = FSE_repeat.FSE_repeat_check; + } + + cBlockHeader = lastBlock + (((uint)(blockType_e.bt_compressed)) << 1) + (uint)(compressedSeqsSize << 3); + MEM_writeLE24((void*)op, cBlockHeader); + cBlockSize = ZSTD_blockHeaderSize + compressedSeqsSize; + } + + cSize += cBlockSize; + if (lastBlock != 0) + { + break; + } + else + { + ip += blockSize; + op += cBlockSize; + remaining -= blockSize; + dstCapacity -= cBlockSize; + cctx->isFirstBlock = 0; + } + } + + return cSize; + } + + /*! ZSTD_compressSequences() : + * Compress an array of ZSTD_Sequence, generated from the original source buffer, into dst. + * If a dictionary is included, then the cctx should reference the dict. (see: ZSTD_CCtx_refCDict(), ZSTD_CCtx_loadDictionary(), etc.) + * The entire source is compressed into a single frame. + * + * The compression behavior changes based on cctx params. In particular: + * If ZSTD_c_blockDelimiters == ZSTD_sf_noBlockDelimiters, the array of ZSTD_Sequence is expected to contain + * no block delimiters (defined in ZSTD_Sequence). Block boundaries are roughly determined based on + * the block size derived from the cctx, and sequences may be split. This is the default setting. + * + * If ZSTD_c_blockDelimiters == ZSTD_sf_explicitBlockDelimiters, the array of ZSTD_Sequence is expected to contain + * block delimiters (defined in ZSTD_Sequence). Behavior is undefined if no block delimiters are provided. + * + * If ZSTD_c_validateSequences == 0, this function will blindly accept the sequences provided. Invalid sequences cause undefined + * behavior. If ZSTD_c_validateSequences == 1, then if sequence is invalid (see doc/zstd_compression_format.md for + * specifics regarding offset/matchlength requirements) then the function will bail out and return an error. + * + * In addition to the two adjustable experimental params, there are other important cctx params. + * - ZSTD_c_minMatch MUST be set as less than or equal to the smallest match generated by the match finder. It has a minimum value of ZSTD_MINMATCH_MIN. + * - ZSTD_c_compressionLevel accordingly adjusts the strength of the entropy coder, as it would in typical compression. + * - ZSTD_c_windowLog affects offset validation: this function will return an error at higher debug levels if a provided offset + * is larger than what the spec allows for a given window log and dictionary (if present). See: doc/zstd_compression_format.md + * + * Note: Repcodes are, as of now, always re-calculated within this function, so ZSTD_Sequence::rep is unused. + * Note 2: Once we integrate ability to ingest repcodes, the explicit block delims mode must respect those repcodes exactly, + * and cannot emit an RLE block that disagrees with the repcode history + * @return : final compressed size or a ZSTD error. + */ + public static nuint ZSTD_compressSequences(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* src, nuint srcSize) + { + byte* op = (byte*)(dst); + nuint cSize = 0; + nuint compressedBlocksSize = 0; + nuint frameHeaderSize = 0; + + assert(cctx != null); + + { + nuint err_code = (ZSTD_CCtx_init_compressStream2(cctx, ZSTD_EndDirective.ZSTD_e_end, srcSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + frameHeaderSize = ZSTD_writeFrameHeader((void*)op, dstCapacity, &cctx->appliedParams, (ulong)srcSize, cctx->dictID); + op += frameHeaderSize; + dstCapacity -= frameHeaderSize; + cSize += frameHeaderSize; + if (cctx->appliedParams.fParams.checksumFlag != 0 && srcSize != 0) + { + XXH64_update(&cctx->xxhState, src, srcSize); + } + + compressedBlocksSize = ZSTD_compressSequences_internal(cctx, (void*)op, dstCapacity, inSeqs, inSeqsSize, src, srcSize); + + { + nuint err_code = (compressedBlocksSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + cSize += compressedBlocksSize; + dstCapacity -= compressedBlocksSize; + if (cctx->appliedParams.fParams.checksumFlag != 0) + { + uint checksum = (uint)(XXH64_digest(&cctx->xxhState)); + + if (dstCapacity < 4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + MEM_writeLE32((void*)((sbyte*)(dst) + cSize), checksum); + cSize += 4; + } + + return cSize; + } + + /*! ZSTD_flushStream() : + * @return : amount of data remaining to flush */ + public static nuint ZSTD_flushStream(ZSTD_CCtx_s* zcs, ZSTD_outBuffer_s* output) + { + ZSTD_inBuffer_s input = new ZSTD_inBuffer_s + { + src = null, + size = 0, + pos = 0, + }; + + return ZSTD_compressStream2(zcs, output, &input, ZSTD_EndDirective.ZSTD_e_flush); + } + + /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */ + public static nuint ZSTD_endStream(ZSTD_CCtx_s* zcs, ZSTD_outBuffer_s* output) + { + ZSTD_inBuffer_s input = new ZSTD_inBuffer_s + { + src = null, + size = 0, + pos = 0, + }; + nuint remainingToFlush = ZSTD_compressStream2(zcs, output, &input, ZSTD_EndDirective.ZSTD_e_end); + + + { + nuint err_code = (remainingToFlush); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (zcs->appliedParams.nbWorkers > 0) + { + return remainingToFlush; + } + + + { + nuint lastBlockSize = (nuint)(zcs->frameEnded != 0 ? 0 : 3); + nuint checksumSize = (nuint)(zcs->frameEnded != 0 ? 0 : zcs->appliedParams.fParams.checksumFlag * 4); + nuint toFlush = remainingToFlush + lastBlockSize + checksumSize; + + return toFlush; + } + } + + public static int ZSTD_maxCLevel() + { + return 22; + } + + public static int ZSTD_minCLevel() + { + return (int)(-(1 << 17)); + } + + public static int ZSTD_defaultCLevel() + { + return 3; + } + + public static ZSTD_compressionParameters[][] ZSTD_defaultCParameters = new ZSTD_compressionParameters[4][] + { + new ZSTD_compressionParameters[23] + { + new ZSTD_compressionParameters + { + windowLog = 19, + chainLog = 12, + hashLog = 13, + searchLog = 1, + minMatch = 6, + targetLength = 1, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 19, + chainLog = 13, + hashLog = 14, + searchLog = 1, + minMatch = 7, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 20, + chainLog = 15, + hashLog = 16, + searchLog = 1, + minMatch = 6, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 21, + chainLog = 16, + hashLog = 17, + searchLog = 1, + minMatch = 5, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_dfast, + }, + new ZSTD_compressionParameters + { + windowLog = 21, + chainLog = 18, + hashLog = 18, + searchLog = 1, + minMatch = 5, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_dfast, + }, + new ZSTD_compressionParameters + { + windowLog = 21, + chainLog = 18, + hashLog = 19, + searchLog = 2, + minMatch = 5, + targetLength = 2, + strategy = ZSTD_strategy.ZSTD_greedy, + }, + new ZSTD_compressionParameters + { + windowLog = 21, + chainLog = 19, + hashLog = 19, + searchLog = 3, + minMatch = 5, + targetLength = 4, + strategy = ZSTD_strategy.ZSTD_greedy, + }, + new ZSTD_compressionParameters + { + windowLog = 21, + chainLog = 19, + hashLog = 19, + searchLog = 3, + minMatch = 5, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy, + }, + new ZSTD_compressionParameters + { + windowLog = 21, + chainLog = 19, + hashLog = 19, + searchLog = 3, + minMatch = 5, + targetLength = 16, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 21, + chainLog = 19, + hashLog = 20, + searchLog = 4, + minMatch = 5, + targetLength = 16, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 22, + chainLog = 20, + hashLog = 21, + searchLog = 4, + minMatch = 5, + targetLength = 16, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 22, + chainLog = 21, + hashLog = 22, + searchLog = 4, + minMatch = 5, + targetLength = 16, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 22, + chainLog = 21, + hashLog = 22, + searchLog = 5, + minMatch = 5, + targetLength = 16, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 22, + chainLog = 21, + hashLog = 22, + searchLog = 5, + minMatch = 5, + targetLength = 32, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 22, + chainLog = 22, + hashLog = 23, + searchLog = 5, + minMatch = 5, + targetLength = 32, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 22, + chainLog = 23, + hashLog = 23, + searchLog = 6, + minMatch = 5, + targetLength = 32, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 22, + chainLog = 22, + hashLog = 22, + searchLog = 5, + minMatch = 5, + targetLength = 48, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 23, + chainLog = 23, + hashLog = 22, + searchLog = 5, + minMatch = 4, + targetLength = 64, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 23, + chainLog = 23, + hashLog = 22, + searchLog = 6, + minMatch = 3, + targetLength = 64, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 23, + chainLog = 24, + hashLog = 22, + searchLog = 7, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 25, + chainLog = 25, + hashLog = 23, + searchLog = 7, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 26, + chainLog = 26, + hashLog = 24, + searchLog = 7, + minMatch = 3, + targetLength = 512, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 27, + chainLog = 27, + hashLog = 25, + searchLog = 9, + minMatch = 3, + targetLength = 999, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + }, + new ZSTD_compressionParameters[23] + { + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 12, + hashLog = 13, + searchLog = 1, + minMatch = 5, + targetLength = 1, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 13, + hashLog = 14, + searchLog = 1, + minMatch = 6, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 14, + hashLog = 14, + searchLog = 1, + minMatch = 5, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_dfast, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 16, + hashLog = 16, + searchLog = 1, + minMatch = 4, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_dfast, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 16, + hashLog = 17, + searchLog = 2, + minMatch = 5, + targetLength = 2, + strategy = ZSTD_strategy.ZSTD_greedy, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 18, + searchLog = 3, + minMatch = 5, + targetLength = 2, + strategy = ZSTD_strategy.ZSTD_greedy, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 3, + minMatch = 5, + targetLength = 4, + strategy = ZSTD_strategy.ZSTD_lazy, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 4, + minMatch = 4, + targetLength = 4, + strategy = ZSTD_strategy.ZSTD_lazy, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 4, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 5, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 6, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 5, + minMatch = 4, + targetLength = 12, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 19, + hashLog = 19, + searchLog = 7, + minMatch = 4, + targetLength = 12, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 4, + minMatch = 4, + targetLength = 16, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 4, + minMatch = 3, + targetLength = 32, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 18, + hashLog = 19, + searchLog = 6, + minMatch = 3, + targetLength = 128, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 19, + hashLog = 19, + searchLog = 6, + minMatch = 3, + targetLength = 128, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 19, + hashLog = 19, + searchLog = 8, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 19, + hashLog = 19, + searchLog = 6, + minMatch = 3, + targetLength = 128, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 19, + hashLog = 19, + searchLog = 8, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 19, + hashLog = 19, + searchLog = 10, + minMatch = 3, + targetLength = 512, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 19, + hashLog = 19, + searchLog = 12, + minMatch = 3, + targetLength = 512, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 18, + chainLog = 19, + hashLog = 19, + searchLog = 13, + minMatch = 3, + targetLength = 999, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + }, + new ZSTD_compressionParameters[23] + { + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 12, + hashLog = 12, + searchLog = 1, + minMatch = 5, + targetLength = 1, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 12, + hashLog = 13, + searchLog = 1, + minMatch = 6, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 13, + hashLog = 15, + searchLog = 1, + minMatch = 5, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 15, + hashLog = 16, + searchLog = 2, + minMatch = 5, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_dfast, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 17, + hashLog = 17, + searchLog = 2, + minMatch = 4, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_dfast, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 16, + hashLog = 17, + searchLog = 3, + minMatch = 4, + targetLength = 2, + strategy = ZSTD_strategy.ZSTD_greedy, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 17, + hashLog = 17, + searchLog = 3, + minMatch = 4, + targetLength = 4, + strategy = ZSTD_strategy.ZSTD_lazy, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 17, + hashLog = 17, + searchLog = 3, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 17, + hashLog = 17, + searchLog = 4, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 17, + hashLog = 17, + searchLog = 5, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 17, + hashLog = 17, + searchLog = 6, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 17, + hashLog = 17, + searchLog = 5, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 7, + minMatch = 4, + targetLength = 12, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 3, + minMatch = 4, + targetLength = 12, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 4, + minMatch = 3, + targetLength = 32, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 6, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 6, + minMatch = 3, + targetLength = 128, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 8, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 10, + minMatch = 3, + targetLength = 512, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 5, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 7, + minMatch = 3, + targetLength = 512, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 9, + minMatch = 3, + targetLength = 512, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 17, + chainLog = 18, + hashLog = 17, + searchLog = 11, + minMatch = 3, + targetLength = 999, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + }, + new ZSTD_compressionParameters[23] + { + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 12, + hashLog = 13, + searchLog = 1, + minMatch = 5, + targetLength = 1, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 14, + hashLog = 15, + searchLog = 1, + minMatch = 5, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 14, + hashLog = 15, + searchLog = 1, + minMatch = 4, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_fast, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 14, + hashLog = 15, + searchLog = 2, + minMatch = 4, + targetLength = 0, + strategy = ZSTD_strategy.ZSTD_dfast, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 14, + hashLog = 14, + searchLog = 4, + minMatch = 4, + targetLength = 2, + strategy = ZSTD_strategy.ZSTD_greedy, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 14, + hashLog = 14, + searchLog = 3, + minMatch = 4, + targetLength = 4, + strategy = ZSTD_strategy.ZSTD_lazy, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 14, + hashLog = 14, + searchLog = 4, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 14, + hashLog = 14, + searchLog = 6, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 14, + hashLog = 14, + searchLog = 8, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_lazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 14, + searchLog = 5, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 14, + searchLog = 9, + minMatch = 4, + targetLength = 8, + strategy = ZSTD_strategy.ZSTD_btlazy2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 14, + searchLog = 3, + minMatch = 4, + targetLength = 12, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 14, + searchLog = 4, + minMatch = 3, + targetLength = 24, + strategy = ZSTD_strategy.ZSTD_btopt, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 14, + searchLog = 5, + minMatch = 3, + targetLength = 32, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 6, + minMatch = 3, + targetLength = 64, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 7, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 5, + minMatch = 3, + targetLength = 48, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 6, + minMatch = 3, + targetLength = 128, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 7, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 8, + minMatch = 3, + targetLength = 256, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 8, + minMatch = 3, + targetLength = 512, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 9, + minMatch = 3, + targetLength = 512, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + new ZSTD_compressionParameters + { + windowLog = 14, + chainLog = 15, + hashLog = 15, + searchLog = 10, + minMatch = 3, + targetLength = 999, + strategy = ZSTD_strategy.ZSTD_btultra2, + }, + }, + }; + + private static ZSTD_compressionParameters ZSTD_dedicatedDictSearch_getCParams(int compressionLevel, nuint dictSize) + { + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal(compressionLevel, 0, dictSize, ZSTD_cParamMode_e.ZSTD_cpm_createCDict); + + switch (cParams.strategy) + { + case ZSTD_strategy.ZSTD_fast: + case ZSTD_strategy.ZSTD_dfast: + { + break; + } + + case ZSTD_strategy.ZSTD_greedy: + case ZSTD_strategy.ZSTD_lazy: + case ZSTD_strategy.ZSTD_lazy2: + { + cParams.hashLog += 2; + } + + break; + case ZSTD_strategy.ZSTD_btlazy2: + case ZSTD_strategy.ZSTD_btopt: + case ZSTD_strategy.ZSTD_btultra: + case ZSTD_strategy.ZSTD_btultra2: + { + break; + } + } + + return cParams; + } + + private static int ZSTD_dedicatedDictSearch_isSupported(ZSTD_compressionParameters* cParams) + { + return (((cParams->strategy >= ZSTD_strategy.ZSTD_greedy) && (cParams->strategy <= ZSTD_strategy.ZSTD_lazy2) && (cParams->hashLog > cParams->chainLog) && (cParams->chainLog <= 24)) ? 1 : 0); + } + + /** + * Reverses the adjustment applied to cparams when enabling dedicated dict + * search. This is used to recover the params set to be used in the working + * context. (Otherwise, those tables would also grow.) + */ + private static void ZSTD_dedicatedDictSearch_revertCParams(ZSTD_compressionParameters* cParams) + { + switch (cParams->strategy) + { + case ZSTD_strategy.ZSTD_fast: + case ZSTD_strategy.ZSTD_dfast: + { + break; + } + + case ZSTD_strategy.ZSTD_greedy: + case ZSTD_strategy.ZSTD_lazy: + case ZSTD_strategy.ZSTD_lazy2: + { + cParams->hashLog -= 2; + } + + if (cParams->hashLog < 6) + { + cParams->hashLog = 6; + } + + break; + case ZSTD_strategy.ZSTD_btlazy2: + case ZSTD_strategy.ZSTD_btopt: + case ZSTD_strategy.ZSTD_btultra: + case ZSTD_strategy.ZSTD_btultra2: + { + break; + } + } + } + + private static ulong ZSTD_getCParamRowSize(ulong srcSizeHint, nuint dictSize, ZSTD_cParamMode_e mode) + { + switch (mode) + { + case ZSTD_cParamMode_e.ZSTD_cpm_unknown: + case ZSTD_cParamMode_e.ZSTD_cpm_noAttachDict: + case ZSTD_cParamMode_e.ZSTD_cpm_createCDict: + { + break; + } + + case ZSTD_cParamMode_e.ZSTD_cpm_attachDict: + { + dictSize = 0; + } + + break; + default: + { + assert(0 != 0); + } + + break; + } + + + { + int unknown = ((srcSizeHint == (unchecked(0UL - 1))) ? 1 : 0); + nuint addedSize = (nuint)(unchecked(unknown) != 0 && dictSize > 0 ? 500 : 0); + + return unchecked(unknown) != 0 && dictSize == 0 ? (unchecked(0UL - 1)) : srcSizeHint + dictSize + addedSize; + } + } + + /*! ZSTD_getCParams_internal() : + * @return ZSTD_compressionParameters structure for a selected compression level, srcSize and dictSize. + * Note: srcSizeHint 0 means 0, use ZSTD_CONTENTSIZE_UNKNOWN for unknown. + * Use dictSize == 0 for unknown or unused. + * Note: `mode` controls how we treat the `dictSize`. See docs for `ZSTD_cParamMode_e`. */ + private static ZSTD_compressionParameters ZSTD_getCParams_internal(int compressionLevel, ulong srcSizeHint, nuint dictSize, ZSTD_cParamMode_e mode) + { + ulong rSize = ZSTD_getCParamRowSize(srcSizeHint, dictSize, mode); + uint tableID = (uint)(((rSize <= (uint)(256 * (1 << 10))) ? 1 : 0) + ((rSize <= (uint)(128 * (1 << 10))) ? 1 : 0) + ((rSize <= (uint)(16 * (1 << 10))) ? 1 : 0)); + int row; + + if (compressionLevel == 0) + { + row = 3; + } + else if (compressionLevel < 0) + { + row = 0; + } + else if (compressionLevel > 22) + { + row = 22; + } + else + { + row = compressionLevel; + } + + + { + ZSTD_compressionParameters cp = ZSTD_defaultCParameters[tableID][row]; + + if (compressionLevel < 0) + { + int clampedCompressionLevel = ((ZSTD_minCLevel()) > (compressionLevel) ? (ZSTD_minCLevel()) : (compressionLevel)); + + cp.targetLength = unchecked((uint)(-clampedCompressionLevel)); + } + + return ZSTD_adjustCParams_internal(cp, srcSizeHint, dictSize, mode); + } + } + + /*! ZSTD_getCParams() : + * @return ZSTD_compressionParameters structure for a selected compression level, srcSize and dictSize. + * Size values are optional, provide 0 if not known or unused */ + public static ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, ulong srcSizeHint, nuint dictSize) + { + if (srcSizeHint == 0) + { + srcSizeHint = (unchecked(0UL - 1)); + } + + return ZSTD_getCParams_internal(compressionLevel, srcSizeHint, dictSize, ZSTD_cParamMode_e.ZSTD_cpm_unknown); + } + + /*! ZSTD_getParams() : + * same idea as ZSTD_getCParams() + * @return a `ZSTD_parameters` structure (instead of `ZSTD_compressionParameters`). + * Fields of `ZSTD_frameParameters` are set to default values */ + private static ZSTD_parameters ZSTD_getParams_internal(int compressionLevel, ulong srcSizeHint, nuint dictSize, ZSTD_cParamMode_e mode) + { + ZSTD_parameters @params; + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal(compressionLevel, srcSizeHint, dictSize, mode); + + memset((void*)(&@params), (0), ((nuint)(sizeof(ZSTD_parameters)))); + @params.cParams = cParams; + @params.fParams.contentSizeFlag = 1; + return @params; + } + + /*! ZSTD_getParams() : + * same idea as ZSTD_getCParams() + * @return a `ZSTD_parameters` structure (instead of `ZSTD_compressionParameters`). + * Fields of `ZSTD_frameParameters` are set to default values */ + public static ZSTD_parameters ZSTD_getParams(int compressionLevel, ulong srcSizeHint, nuint dictSize) + { + if (srcSizeHint == 0) + { + srcSizeHint = (unchecked(0UL - 1)); + } + + return ZSTD_getParams_internal(compressionLevel, srcSizeHint, dictSize, ZSTD_cParamMode_e.ZSTD_cpm_unknown); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressInternal.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressInternal.cs new file mode 100644 index 00000000..05097e6d --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressInternal.cs @@ -0,0 +1,838 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static readonly rawSeqStore_t kNullRawSeqStore = new rawSeqStore_t + { + seq = (rawSeq*)null, + pos = 0, + posInSequence = 0, + size = 0, + capacity = 0, + }; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_LLcode(uint litLength) + { + + uint LL_deltaCode = 19; + + return (litLength > 63) ? ZSTD_highbit32(litLength) + LL_deltaCode : LL_Code[litLength]; + } + + /* ZSTD_MLcode() : + * note : mlBase = matchLength - MINMATCH; + * because it's the format it's stored in seqStore->sequences */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_MLcode(uint mlBase) + { + + uint ML_deltaCode = 36; + + return (mlBase > 127) ? ZSTD_highbit32(mlBase) + ML_deltaCode : ML_Code[mlBase]; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static repcodes_s ZSTD_updateRep(uint* rep, uint offset, uint ll0) + { + repcodes_s newReps; + + if (offset >= 3) + { + newReps.rep[2] = rep[1]; + newReps.rep[1] = rep[0]; + newReps.rep[0] = offset - (uint)((3 - 1)); + } + else + { + uint repCode = offset + ll0; + + if (repCode > 0) + { + uint currentOffset = (repCode == 3) ? (rep[0] - 1) : rep[repCode]; + + newReps.rep[2] = (repCode >= 2) ? rep[1] : rep[2]; + newReps.rep[1] = rep[0]; + newReps.rep[0] = currentOffset; + } + else + { + memcpy((void*)(&newReps), (void*)(rep), ((nuint)(sizeof(repcodes_s)))); + } + } + + return newReps; + } + + /* ZSTD_cParam_withinBounds: + * @return 1 if value is within cParam bounds, + * 0 otherwise */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_cParam_withinBounds(ZSTD_cParameter cParam, int value) + { + ZSTD_bounds bounds = ZSTD_cParam_getBounds(cParam); + + if ((ERR_isError(bounds.error)) != 0) + { + return 0; + } + + if (value < bounds.lowerBound) + { + return 0; + } + + if (value > bounds.upperBound) + { + return 0; + } + + return 1; + } + + /* ZSTD_noCompressBlock() : + * Writes uncompressed block to dst buffer from given src. + * Returns the size of the block */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_noCompressBlock(void* dst, nuint dstCapacity, void* src, nuint srcSize, uint lastBlock) + { + uint cBlockHeader24 = lastBlock + (((uint)(blockType_e.bt_raw)) << 1) + (uint)(srcSize << 3); + + if (srcSize + ZSTD_blockHeaderSize > dstCapacity) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + MEM_writeLE24(dst, cBlockHeader24); + memcpy((void*)(((byte*)(dst) + ZSTD_blockHeaderSize)), (src), (srcSize)); + return ZSTD_blockHeaderSize + srcSize; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_rleCompressBlock(void* dst, nuint dstCapacity, byte src, nuint srcSize, uint lastBlock) + { + byte* op = (byte*)(dst); + uint cBlockHeader = lastBlock + (((uint)(blockType_e.bt_rle)) << 1) + (uint)(srcSize << 3); + + if (dstCapacity < 4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + MEM_writeLE24((void*)op, cBlockHeader); + op[3] = src; + return 4; + } + + /* ZSTD_minGain() : + * minimum compression required + * to generate a compress block or a compressed literals section. + * note : use same formula for both situations */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_minGain(nuint srcSize, ZSTD_strategy strat) + { + uint minlog = (strat >= ZSTD_strategy.ZSTD_btultra) ? (uint)(strat) - 1 : 6; + + assert((ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_strategy, (int)strat)) != 0); + return (srcSize >> (int)minlog) + 2; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_disableLiteralsCompression(ZSTD_CCtx_params_s* cctxParams) + { + switch (cctxParams->literalCompressionMode) + { + case ZSTD_literalCompressionMode_e.ZSTD_lcm_huffman: + { + return 0; + } + + case ZSTD_literalCompressionMode_e.ZSTD_lcm_uncompressed: + { + return 1; + } + + default: + { + assert(0 != 0); + } + + + goto case ZSTD_literalCompressionMode_e.ZSTD_lcm_auto; + case ZSTD_literalCompressionMode_e.ZSTD_lcm_auto: + { + return (((cctxParams->cParams.strategy == ZSTD_strategy.ZSTD_fast) && (cctxParams->cParams.targetLength > 0)) ? 1 : 0); + } + } + } + + /*! ZSTD_safecopyLiterals() : + * memcpy() function that won't read beyond more than WILDCOPY_OVERLENGTH bytes past ilimit_w. + * Only called when the sequence ends past ilimit_w, so it only needs to be optimized for single + * large copies. + */ + private static void ZSTD_safecopyLiterals(byte* op, byte* ip, byte* iend, byte* ilimit_w) + { + assert(iend > ilimit_w); + if (ip <= ilimit_w) + { + ZSTD_wildcopy((void*)op, (void*)ip, (nint)(ilimit_w - ip), ZSTD_overlap_e.ZSTD_no_overlap); + op += ilimit_w - ip; + ip = ilimit_w; + } + + while (ip < iend) + { + *op++ = *ip++; + } + } + + /*! ZSTD_storeSeq() : + * Store a sequence (litlen, litPtr, offCode and mlBase) into seqStore_t. + * `offCode` : distance to match + ZSTD_REP_MOVE (values <= ZSTD_REP_MOVE are repCodes). + * `mlBase` : matchLength - MINMATCH + * Allowed to overread literals up to litLimit. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void ZSTD_storeSeq(seqStore_t* seqStorePtr, nuint litLength, byte* literals, byte* litLimit, uint offCode, nuint mlBase) + { + byte* litLimit_w = litLimit - 32; + byte* litEnd = literals + litLength; + + assert((nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq); + assert(seqStorePtr->maxNbLit <= (uint)(128 * (1 << 10))); + assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + seqStorePtr->maxNbLit); + assert(literals + litLength <= litLimit); + if (litEnd <= litLimit_w) + { + assert(32 >= 16); + ZSTD_copy16((void*)seqStorePtr->lit, (void*)literals); + if (litLength > 16) + { + ZSTD_wildcopy((void*)(seqStorePtr->lit + 16), (void*)(literals + 16), (nint)(litLength) - 16, ZSTD_overlap_e.ZSTD_no_overlap); + } + } + else + { + ZSTD_safecopyLiterals(seqStorePtr->lit, literals, litEnd, litLimit_w); + } + + seqStorePtr->lit += litLength; + if (litLength > 0xFFFF) + { + assert(seqStorePtr->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_none); + seqStorePtr->longLengthType = ZSTD_longLengthType_e.ZSTD_llt_literalLength; + seqStorePtr->longLengthPos = (uint)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + } + + seqStorePtr->sequences[0].litLength = (ushort)(litLength); + seqStorePtr->sequences[0].offset = offCode + 1; + if (mlBase > 0xFFFF) + { + assert(seqStorePtr->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_none); + seqStorePtr->longLengthType = ZSTD_longLengthType_e.ZSTD_llt_matchLength; + seqStorePtr->longLengthPos = (uint)(seqStorePtr->sequences - seqStorePtr->sequencesStart); + } + + seqStorePtr->sequences[0].matchLength = (ushort)(mlBase); + seqStorePtr->sequences++; + } + + /*-************************************* + * Match length counter + ***************************************/ + [InlineMethod.Inline] + private static uint ZSTD_NbCommonBytes(nuint val) + { + if (val == 0) + { + return 0; + } + + if (BitConverter.IsLittleEndian) + { + return (uint)(BitOperations.TrailingZeroCount(val) >> 3); + } + + return (uint)(BitOperations.Log2(val) >> 3); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static nuint ZSTD_count(byte* pIn, byte* pMatch, byte* pInLimit) + { + byte* pStart = pIn; + byte* pInLoopLimit = pInLimit - ((nuint)(sizeof(nuint)) - 1); + + if (pIn < pInLoopLimit) + { + + { + nuint diff = MEM_readST((void*)pMatch) ^ MEM_readST((void*)pIn); + + if (diff != 0) + { + return ZSTD_NbCommonBytes(diff); + } + } + + pIn += (nuint)(sizeof(nuint)); + pMatch += (nuint)(sizeof(nuint)); + while (pIn < pInLoopLimit) + { + nuint diff = MEM_readST((void*)pMatch) ^ MEM_readST((void*)pIn); + + if (diff == 0) + { + pIn += (nuint)(sizeof(nuint)); + pMatch += (nuint)(sizeof(nuint)); + continue; + } + + pIn += ZSTD_NbCommonBytes(diff); + return (nuint)(pIn - pStart); + } + } + + if (MEM_64bits && (pIn < (pInLimit - 3)) && (MEM_read32((void*)pMatch) == MEM_read32((void*)pIn))) + { + pIn += 4; + pMatch += 4; + } + + if ((pIn < (pInLimit - 1)) && (MEM_read16((void*)pMatch) == MEM_read16((void*)pIn))) + { + pIn += 2; + pMatch += 2; + } + + if ((pIn < pInLimit) && (*pMatch == *pIn)) + { + pIn++; + } + + return (nuint)(pIn - pStart); + } + + /** ZSTD_count_2segments() : + * can count match length with `ip` & `match` in 2 different segments. + * convention : on reaching mEnd, match count continue starting from iStart + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_count_2segments(byte* ip, byte* match, byte* iEnd, byte* mEnd, byte* iStart) + { + byte* vEnd = ((ip + (mEnd - match)) < (iEnd) ? (ip + (mEnd - match)) : (iEnd)); + nuint matchLength = ZSTD_count(ip, match, vEnd); + + if (match + matchLength != mEnd) + { + return matchLength; + } + + return matchLength + ZSTD_count(ip + matchLength, iStart, iEnd); + } + + public const uint prime3bytes = 506832829U; + + [InlineMethod.Inline] + private static uint ZSTD_hash3(uint u, uint h) + { + return ((u << (32 - 24)) * prime3bytes) >> (int)(32 - h); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static nuint ZSTD_hash3Ptr(void* ptr, uint h) + { + return ZSTD_hash3(MEM_readLE32(ptr), h); + } + + public const uint prime4bytes = 2654435761U; + + [InlineMethod.Inline] + private static uint ZSTD_hash4(uint u, uint h) + { + return (u * prime4bytes) >> (int)(32 - h); + } + + [InlineMethod.Inline] + private static nuint ZSTD_hash4Ptr(void* ptr, uint h) + { + return ZSTD_hash4(MEM_read32(ptr), h); + } + + public const ulong prime5bytes = 889523592379UL; + + [InlineMethod.Inline] + private static nuint ZSTD_hash5(ulong u, uint h) + { + return (nuint)(((u << (64 - 40)) * prime5bytes) >> (int)(64 - h)); + } + + [InlineMethod.Inline] + private static nuint ZSTD_hash5Ptr(void* p, uint h) + { + return ZSTD_hash5(MEM_readLE64(p), h); + } + + public const ulong prime6bytes = 227718039650203UL; + + [InlineMethod.Inline] + private static nuint ZSTD_hash6(ulong u, uint h) + { + return (nuint)(((u << (64 - 48)) * prime6bytes) >> (int)(64 - h)); + } + + [InlineMethod.Inline] + private static nuint ZSTD_hash6Ptr(void* p, uint h) + { + return ZSTD_hash6(MEM_readLE64(p), h); + } + + public const ulong prime7bytes = 58295818150454627UL; + + [InlineMethod.Inline] + private static nuint ZSTD_hash7(ulong u, uint h) + { + return (nuint)(((u << (64 - 56)) * prime7bytes) >> (int)(64 - h)); + } + + [InlineMethod.Inline] + private static nuint ZSTD_hash7Ptr(void* p, uint h) + { + return ZSTD_hash7(MEM_readLE64(p), h); + } + + public const ulong prime8bytes = 0xCF1BBCDCB7A56463UL; + + [InlineMethod.Inline] + private static nuint ZSTD_hash8(ulong u, uint h) + { + return (nuint)(((u) * prime8bytes) >> (int)(64 - h)); + } + + [InlineMethod.Inline] + private static nuint ZSTD_hash8Ptr(void* p, uint h) + { + return ZSTD_hash8(MEM_readLE64(p), h); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static nuint ZSTD_hashPtr(void* p, uint hBits, uint mls) + { + switch (mls) + { + default: + case 4: + { + return ZSTD_hash4Ptr(p, hBits); + } + + case 5: + { + return ZSTD_hash5Ptr(p, hBits); + } + + case 6: + { + return ZSTD_hash6Ptr(p, hBits); + } + + case 7: + { + return ZSTD_hash7Ptr(p, hBits); + } + + case 8: + { + return ZSTD_hash8Ptr(p, hBits); + } + } + } + + /** ZSTD_ipow() : + * Return base^exponent. + */ + private static ulong ZSTD_ipow(ulong @base, ulong exponent) + { + ulong power = 1; + + while (exponent != 0) + { + if ((exponent & 1) != 0) + { + power *= @base; + } + + exponent >>= 1; + @base *= @base; + } + + return power; + } + + /** ZSTD_rollingHash_append() : + * Add the buffer to the hash value. + */ + private static ulong ZSTD_rollingHash_append(ulong hash, void* buf, nuint size) + { + byte* istart = (byte*)(buf); + nuint pos; + + for (pos = 0; pos < size; ++pos) + { + hash *= prime8bytes; + hash += (ulong)(istart[pos] + 10); + } + + return hash; + } + + /** ZSTD_rollingHash_compute() : + * Compute the rolling hash value of the buffer. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ulong ZSTD_rollingHash_compute(void* buf, nuint size) + { + return ZSTD_rollingHash_append(0, buf, size); + } + + /** ZSTD_rollingHash_primePower() : + * Compute the primePower to be passed to ZSTD_rollingHash_rotate() for a hash + * over a window of length bytes. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ulong ZSTD_rollingHash_primePower(uint length) + { + return ZSTD_ipow(prime8bytes, length - 1); + } + + /** ZSTD_rollingHash_rotate() : + * Rotate the rolling hash by one byte. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ulong ZSTD_rollingHash_rotate(ulong hash, byte toRemove, byte toAdd, ulong primePower) + { + hash -= (uint)((toRemove + 10)) * primePower; + hash *= prime8bytes; + hash += (ulong)(toAdd + 10); + return hash; + } + + /** + * ZSTD_window_clear(): + * Clears the window containing the history by simply setting it to empty. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_window_clear(ZSTD_window_t* window) + { + nuint endT = (nuint)(window->nextSrc - window->@base); + uint end = (uint)(endT); + + window->lowLimit = end; + window->dictLimit = end; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_window_isEmpty(ZSTD_window_t window) + { + return ((window.dictLimit == 1 && window.lowLimit == 1 && (window.nextSrc - window.@base) == 1) ? 1U : 0U); + } + + /** + * ZSTD_window_hasExtDict(): + * Returns non-zero if the window has a non-empty extDict. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_window_hasExtDict(ZSTD_window_t window) + { + return ((window.lowLimit < window.dictLimit) ? 1U : 0U); + } + + /** + * ZSTD_matchState_dictMode(): + * Inspects the provided matchState and figures out what dictMode should be + * passed to the compressor. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ZSTD_dictMode_e ZSTD_matchState_dictMode(ZSTD_matchState_t* ms) + { + return (ZSTD_window_hasExtDict(ms->window)) != 0 ? ZSTD_dictMode_e.ZSTD_extDict : ms->dictMatchState != null ? (ms->dictMatchState->dedicatedDictSearch != 0 ? ZSTD_dictMode_e.ZSTD_dedicatedDictSearch : ZSTD_dictMode_e.ZSTD_dictMatchState) : ZSTD_dictMode_e.ZSTD_noDict; + } + + /** + * ZSTD_window_canOverflowCorrect(): + * Returns non-zero if the indices are large enough for overflow correction + * to work correctly without impacting compression ratio. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_window_canOverflowCorrect(ZSTD_window_t window, uint cycleLog, uint maxDist, uint loadedDictEnd, void* src) + { + uint cycleSize = 1U << (int)cycleLog; + uint curr = (uint)((byte*)(src) - window.@base); + uint minIndexToOverflowCorrect = cycleSize + ((maxDist) > (cycleSize) ? (maxDist) : (cycleSize)); + uint adjustment = window.nbOverflowCorrections + 1; + uint adjustedIndex = ((minIndexToOverflowCorrect * adjustment) > (minIndexToOverflowCorrect) ? (minIndexToOverflowCorrect * adjustment) : (minIndexToOverflowCorrect)); + uint indexLargeEnough = ((curr > adjustedIndex) ? 1U : 0U); + uint dictionaryInvalidated = ((curr > maxDist + loadedDictEnd) ? 1U : 0U); + + return ((indexLargeEnough != 0 && dictionaryInvalidated != 0) ? 1U : 0U); + } + + /** + * ZSTD_window_needOverflowCorrection(): + * Returns non-zero if the indices are getting too large and need overflow + * protection. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_window_needOverflowCorrection(ZSTD_window_t window, uint cycleLog, uint maxDist, uint loadedDictEnd, void* src, void* srcEnd) + { + uint curr = (uint)((byte*)(srcEnd) - window.@base); + + ; + return ((curr > ((3U << 29) + (1U << ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31))))) ? 1U : 0U); + } + + /** + * ZSTD_window_correctOverflow(): + * Reduces the indices to protect from index overflow. + * Returns the correction made to the indices, which must be applied to every + * stored index. + * + * The least significant cycleLog bits of the indices must remain the same, + * which may be 0. Every index up to maxDist in the past must be valid. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_window_correctOverflow(ZSTD_window_t* window, uint cycleLog, uint maxDist, void* src) + { + uint cycleSize = 1U << (int)cycleLog; + uint cycleMask = cycleSize - 1; + uint curr = (uint)((byte*)(src) - window->@base); + uint currentCycle0 = curr & cycleMask; + uint currentCycle1 = currentCycle0 == 0 ? cycleSize : currentCycle0; + uint newCurrent = currentCycle1 + ((maxDist) > (cycleSize) ? (maxDist) : (cycleSize)); + uint correction = curr - newCurrent; + + assert((maxDist & (maxDist - 1)) == 0); + assert((curr & cycleMask) == (newCurrent & cycleMask)); + assert(curr > newCurrent); + if (0 == 0) + { + assert(correction > (uint)(1 << 28)); + } + + window->@base += correction; + window->dictBase += correction; + if (window->lowLimit <= correction) + { + window->lowLimit = 1; + } + else + { + window->lowLimit -= correction; + } + + if (window->dictLimit <= correction) + { + window->dictLimit = 1; + } + else + { + window->dictLimit -= correction; + } + + assert(newCurrent >= maxDist); + assert(newCurrent - maxDist >= 1); + assert(window->lowLimit <= newCurrent); + assert(window->dictLimit <= newCurrent); + ++window->nbOverflowCorrections; + return correction; + } + + /** + * ZSTD_window_enforceMaxDist(): + * Updates lowLimit so that: + * (srcEnd - base) - lowLimit == maxDist + loadedDictEnd + * + * It ensures index is valid as long as index >= lowLimit. + * This must be called before a block compression call. + * + * loadedDictEnd is only defined if a dictionary is in use for current compression. + * As the name implies, loadedDictEnd represents the index at end of dictionary. + * The value lies within context's referential, it can be directly compared to blockEndIdx. + * + * If loadedDictEndPtr is NULL, no dictionary is in use, and we use loadedDictEnd == 0. + * If loadedDictEndPtr is not NULL, we set it to zero after updating lowLimit. + * This is because dictionaries are allowed to be referenced fully + * as long as the last byte of the dictionary is in the window. + * Once input has progressed beyond window size, dictionary cannot be referenced anymore. + * + * In normal dict mode, the dictionary lies between lowLimit and dictLimit. + * In dictMatchState mode, lowLimit and dictLimit are the same, + * and the dictionary is below them. + * forceWindow and dictMatchState are therefore incompatible. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_window_enforceMaxDist(ZSTD_window_t* window, void* blockEnd, uint maxDist, uint* loadedDictEndPtr, ZSTD_matchState_t** dictMatchStatePtr) + { + uint blockEndIdx = (uint)((byte*)(blockEnd) - window->@base); + uint loadedDictEnd = (loadedDictEndPtr != null) ? *loadedDictEndPtr : 0; + + if (blockEndIdx > maxDist + loadedDictEnd) + { + uint newLowLimit = blockEndIdx - maxDist; + + if (window->lowLimit < newLowLimit) + { + window->lowLimit = newLowLimit; + } + + if (window->dictLimit < window->lowLimit) + { + window->dictLimit = window->lowLimit; + } + + if (loadedDictEndPtr != null) + { + *loadedDictEndPtr = 0; + } + + if (dictMatchStatePtr != null) + { + *dictMatchStatePtr = null; + } + } + } + + /* Similar to ZSTD_window_enforceMaxDist(), + * but only invalidates dictionary + * when input progresses beyond window size. + * assumption : loadedDictEndPtr and dictMatchStatePtr are valid (non NULL) + * loadedDictEnd uses same referential as window->base + * maxDist is the window size */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_checkDictValidity(ZSTD_window_t* window, void* blockEnd, uint maxDist, uint* loadedDictEndPtr, ZSTD_matchState_t** dictMatchStatePtr) + { + assert(loadedDictEndPtr != null); + assert(dictMatchStatePtr != null); + + { + uint blockEndIdx = (uint)((byte*)(blockEnd) - window->@base); + uint loadedDictEnd = *loadedDictEndPtr; + + assert(blockEndIdx >= loadedDictEnd); + if (blockEndIdx > loadedDictEnd + maxDist) + { + *loadedDictEndPtr = 0; + *dictMatchStatePtr = null; + } + else + { + if (*loadedDictEndPtr != 0) + { + ; + } + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_window_init(ZSTD_window_t* window) + { + memset((void*)(window), (0), ((nuint)(sizeof(ZSTD_window_t)))); + window->@base = emptyString; + window->dictBase = emptyString; + window->dictLimit = 1; + window->lowLimit = 1; + window->nextSrc = window->@base + 1; + window->nbOverflowCorrections = 0; + } + + /** + * ZSTD_window_update(): + * Updates the window by appending [src, src + srcSize) to the window. + * If it is not contiguous, the current prefix becomes the extDict, and we + * forget about the extDict. Handles overlap of the prefix and extDict. + * Returns non-zero if the segment is contiguous. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_window_update(ZSTD_window_t* window, void* src, nuint srcSize, int forceNonContiguous) + { + byte* ip = (byte*)(src); + uint contiguous = 1; + + if (srcSize == 0) + { + return contiguous; + } + + assert(window->@base != null); + assert(window->dictBase != null); + if (src != window->nextSrc || forceNonContiguous != 0) + { + nuint distanceFromBase = (nuint)(window->nextSrc - window->@base); + + window->lowLimit = window->dictLimit; + assert(distanceFromBase == (nuint)((uint)(distanceFromBase))); + window->dictLimit = (uint)(distanceFromBase); + window->dictBase = window->@base; + window->@base = ip - distanceFromBase; + if (window->dictLimit - window->lowLimit < 8) + { + window->lowLimit = window->dictLimit; + } + + contiguous = 0; + } + + window->nextSrc = ip + srcSize; + if (((ip + srcSize > window->dictBase + window->lowLimit) && (ip < window->dictBase + window->dictLimit))) + { + nint highInputIdx = (nint)((ip + srcSize) - window->dictBase); + uint lowLimitMax = (highInputIdx > (nint)(window->dictLimit)) ? window->dictLimit : (uint)(highInputIdx); + + window->lowLimit = lowLimitMax; + } + + return contiguous; + } + + /** + * Returns the lowest allowed match index. It may either be in the ext-dict or the prefix. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint ZSTD_getLowestMatchIndex(ZSTD_matchState_t* ms, uint curr, uint windowLog) + { + uint maxDistance = 1U << (int)windowLog; + uint lowestValid = ms->window.lowLimit; + uint withinWindow = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid; + uint isDictionary = (((ms->loadedDictEnd != 0)) ? 1U : 0U); + uint matchLowest = isDictionary != 0 ? lowestValid : withinWindow; + + return matchLowest; + } + + /** + * Returns the lowest allowed match index in the prefix. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_getLowestPrefixIndex(ZSTD_matchState_t* ms, uint curr, uint windowLog) + { + uint maxDistance = 1U << (int)windowLog; + uint lowestValid = ms->window.dictLimit; + uint withinWindow = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid; + uint isDictionary = (((ms->loadedDictEnd != 0)) ? 1U : 0U); + uint matchLowest = isDictionary != 0 ? lowestValid : withinWindow; + + return matchLowest; + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressLiterals.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressLiterals.cs new file mode 100644 index 00000000..402aabce --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressLiterals.cs @@ -0,0 +1,186 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static nuint ZSTD_noCompressLiterals(void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + byte* ostart = (byte*)(dst); + uint flSize = (uint)(1 + ((srcSize > 31) ? 1 : 0) + ((srcSize > 4095) ? 1 : 0)); + + if (srcSize + flSize > dstCapacity) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + switch (flSize) + { + case 1: + { + ostart[0] = (byte)((uint)(symbolEncodingType_e.set_basic) + (srcSize << 3)); + } + + break; + case 2: + { + MEM_writeLE16((void*)ostart, (ushort)((uint)(symbolEncodingType_e.set_basic) + (uint)((1 << 2)) + (srcSize << 4))); + } + + break; + case 3: + { + MEM_writeLE32((void*)ostart, (uint)((uint)(symbolEncodingType_e.set_basic) + (uint)((3 << 2)) + (srcSize << 4))); + } + + break; + default: + { + assert(0 != 0); + } + break; + } + + memcpy((void*)((ostart + flSize)), (src), (srcSize)); + return srcSize + flSize; + } + + public static nuint ZSTD_compressRleLiteralsBlock(void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + byte* ostart = (byte*)(dst); + uint flSize = (uint)(1 + ((srcSize > 31) ? 1 : 0) + ((srcSize > 4095) ? 1 : 0)); + + switch (flSize) + { + case 1: + { + ostart[0] = (byte)((uint)(symbolEncodingType_e.set_rle) + (srcSize << 3)); + } + + break; + case 2: + { + MEM_writeLE16((void*)ostart, (ushort)((uint)(symbolEncodingType_e.set_rle) + (uint)((1 << 2)) + (srcSize << 4))); + } + + break; + case 3: + { + MEM_writeLE32((void*)ostart, (uint)((uint)(symbolEncodingType_e.set_rle) + (uint)((3 << 2)) + (srcSize << 4))); + } + + break; + default: + { + assert(0 != 0); + } + break; + } + + ostart[flSize] = *(byte*)(src); + return flSize + 1; + } + + public static nuint ZSTD_compressLiterals(ZSTD_hufCTables_t* prevHuf, ZSTD_hufCTables_t* nextHuf, ZSTD_strategy strategy, int disableLiteralCompression, void* dst, nuint dstCapacity, void* src, nuint srcSize, void* entropyWorkspace, nuint entropyWorkspaceSize, int bmi2) + { + nuint minGain = ZSTD_minGain(srcSize, strategy); + nuint lhSize = (nuint)(3 + ((srcSize >= (uint)(1 * (1 << 10))) ? 1 : 0) + ((srcSize >= (uint)(16 * (1 << 10))) ? 1 : 0)); + byte* ostart = (byte*)(dst); + uint singleStream = ((srcSize < 256) ? 1U : 0U); + symbolEncodingType_e hType = symbolEncodingType_e.set_compressed; + nuint cLitSize; + + memcpy((void*)(nextHuf), (void*)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); + if (disableLiteralCompression != 0) + { + return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); + } + + + { + nuint minLitSize = (nuint)((prevHuf->repeatMode == HUF_repeat.HUF_repeat_valid) ? 6 : 63); + + if (srcSize <= minLitSize) + { + return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); + } + } + + if (dstCapacity < lhSize + 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + + { + HUF_repeat repeat = prevHuf->repeatMode; + int preferRepeat = strategy < ZSTD_strategy.ZSTD_lazy ? ((srcSize <= 1024) ? 1 : 0) : 0; + + if (repeat == HUF_repeat.HUF_repeat_valid && lhSize == 3) + { + singleStream = 1; + } + + cLitSize = singleStream != 0 ? HUF_compress1X_repeat((void*)(ostart + lhSize), dstCapacity - lhSize, src, srcSize, 255, 11, entropyWorkspace, entropyWorkspaceSize, (HUF_CElt_s*)(nextHuf->CTable), &repeat, preferRepeat, bmi2) : HUF_compress4X_repeat((void*)(ostart + lhSize), dstCapacity - lhSize, src, srcSize, 255, 11, entropyWorkspace, entropyWorkspaceSize, (HUF_CElt_s*)(nextHuf->CTable), &repeat, preferRepeat, bmi2); + if (repeat != HUF_repeat.HUF_repeat_none) + { + hType = symbolEncodingType_e.set_repeat; + } + } + + if ((cLitSize == 0) || (cLitSize >= srcSize - minGain) || (ERR_isError(cLitSize)) != 0) + { + memcpy((void*)(nextHuf), (void*)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); + return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); + } + + if (cLitSize == 1) + { + memcpy((void*)(nextHuf), (void*)(prevHuf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); + return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize); + } + + if (hType == symbolEncodingType_e.set_compressed) + { + nextHuf->repeatMode = HUF_repeat.HUF_repeat_check; + } + + switch (lhSize) + { + case 3: + { + uint lhc = (uint)(hType + ((singleStream == 0 ? 1 : 0) << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 14); + + MEM_writeLE24((void*)ostart, lhc); + break; + } + + case 4: + { + uint lhc = (uint)(hType + (2 << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 18); + + MEM_writeLE32((void*)ostart, lhc); + break; + } + + case 5: + { + uint lhc = (uint)(hType + (3 << 2)) + ((uint)(srcSize) << 4) + ((uint)(cLitSize) << 22); + + MEM_writeLE32((void*)ostart, lhc); + ostart[4] = (byte)(cLitSize >> 10); + break; + } + + default: + { + assert(0 != 0); + } + break; + } + + return lhSize + cLitSize; + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressSequences.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressSequences.cs new file mode 100644 index 00000000..2ae887c2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressSequences.cs @@ -0,0 +1,725 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static uint* kInverseProbabilityLog256 = GetArrayPointer(new uint[256] + { + 0, + 2048, + 1792, + 1642, + 1536, + 1453, + 1386, + 1329, + 1280, + 1236, + 1197, + 1162, + 1130, + 1100, + 1073, + 1047, + 1024, + 1001, + 980, + 960, + 941, + 923, + 906, + 889, + 874, + 859, + 844, + 830, + 817, + 804, + 791, + 779, + 768, + 756, + 745, + 734, + 724, + 714, + 704, + 694, + 685, + 676, + 667, + 658, + 650, + 642, + 633, + 626, + 618, + 610, + 603, + 595, + 588, + 581, + 574, + 567, + 561, + 554, + 548, + 542, + 535, + 529, + 523, + 517, + 512, + 506, + 500, + 495, + 489, + 484, + 478, + 473, + 468, + 463, + 458, + 453, + 448, + 443, + 438, + 434, + 429, + 424, + 420, + 415, + 411, + 407, + 402, + 398, + 394, + 390, + 386, + 382, + 377, + 373, + 370, + 366, + 362, + 358, + 354, + 350, + 347, + 343, + 339, + 336, + 332, + 329, + 325, + 322, + 318, + 315, + 311, + 308, + 305, + 302, + 298, + 295, + 292, + 289, + 286, + 282, + 279, + 276, + 273, + 270, + 267, + 264, + 261, + 258, + 256, + 253, + 250, + 247, + 244, + 241, + 239, + 236, + 233, + 230, + 228, + 225, + 222, + 220, + 217, + 215, + 212, + 209, + 207, + 204, + 202, + 199, + 197, + 194, + 192, + 190, + 187, + 185, + 182, + 180, + 178, + 175, + 173, + 171, + 168, + 166, + 164, + 162, + 159, + 157, + 155, + 153, + 151, + 149, + 146, + 144, + 142, + 140, + 138, + 136, + 134, + 132, + 130, + 128, + 126, + 123, + 121, + 119, + 117, + 115, + 114, + 112, + 110, + 108, + 106, + 104, + 102, + 100, + 98, + 96, + 94, + 93, + 91, + 89, + 87, + 85, + 83, + 82, + 80, + 78, + 76, + 74, + 73, + 71, + 69, + 67, + 66, + 64, + 62, + 61, + 59, + 57, + 55, + 54, + 52, + 50, + 49, + 47, + 46, + 44, + 42, + 41, + 39, + 37, + 36, + 34, + 33, + 31, + 30, + 28, + 26, + 25, + 23, + 22, + 20, + 19, + 17, + 16, + 14, + 13, + 11, + 10, + 8, + 7, + 5, + 4, + 2, + 1, + }); + + private static uint ZSTD_getFSEMaxSymbolValue(uint* ctable) + { + void* ptr = (void*)ctable; + ushort* u16ptr = (ushort*)(ptr); + uint maxSymbolValue = MEM_read16((void*)(u16ptr + 1)); + + return maxSymbolValue; + } + + /** + * Returns true if we should use ncount=-1 else we should + * use ncount=1 for low probability symbols instead. + */ + private static uint ZSTD_useLowProbCount(nuint nbSeq) + { + return ((nbSeq >= 2048) ? 1U : 0U); + } + + /** + * Returns the cost in bytes of encoding the normalized count header. + * Returns an error if any of the helper functions return an error. + */ + private static nuint ZSTD_NCountCost(uint* count, uint max, nuint nbSeq, uint FSELog) + { + byte* wksp = stackalloc byte[512]; + short* norm = stackalloc short[53]; + uint tableLog = FSE_optimalTableLog(FSELog, nbSeq, max); + + + { + nuint err_code = (FSE_normalizeCount((short*)norm, tableLog, count, nbSeq, max, ZSTD_useLowProbCount(nbSeq))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return FSE_writeNCount((void*)wksp, (nuint)(sizeof(byte) * 512), (short*)norm, max, tableLog); + } + + /** + * Returns the cost in bits of encoding the distribution described by count + * using the entropy bound. + */ + private static nuint ZSTD_entropyCost(uint* count, uint max, nuint total) + { + uint cost = 0; + uint s; + + assert(total > 0); + for (s = 0; s <= max; ++s) + { + uint norm = (uint)((256 * count[s]) / total); + + if (count[s] != 0 && norm == 0) + { + norm = 1; + } + + assert(count[s] < total); + cost += count[s] * kInverseProbabilityLog256[norm]; + } + + return cost >> 8; + } + + /** + * Returns the cost in bits of encoding the distribution in count using ctable. + * Returns an error if ctable cannot represent all the symbols in count. + */ + public static nuint ZSTD_fseBitCost(uint* ctable, uint* count, uint max) + { + uint kAccuracyLog = 8; + nuint cost = 0; + uint s; + FSE_CState_t cstate; + + FSE_initCState(&cstate, ctable); + if (ZSTD_getFSEMaxSymbolValue(ctable) < max) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + for (s = 0; s <= max; ++s) + { + uint tableLog = cstate.stateLog; + uint badCost = (tableLog + 1) << (int)kAccuracyLog; + uint bitCost = FSE_bitCost(cstate.symbolTT, tableLog, s, kAccuracyLog); + + if (count[s] == 0) + { + continue; + } + + if (bitCost >= badCost) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + cost += (nuint)(count[s]) * bitCost; + } + + return cost >> (int)kAccuracyLog; + } + + /** + * Returns the cost in bits of encoding the distribution in count using the + * table described by norm. The max symbol support by norm is assumed >= max. + * norm must be valid for every symbol with non-zero probability in count. + */ + public static nuint ZSTD_crossEntropyCost(short* norm, uint accuracyLog, uint* count, uint max) + { + uint shift = 8 - accuracyLog; + nuint cost = 0; + uint s; + + assert(accuracyLog <= 8); + for (s = 0; s <= max; ++s) + { + uint normAcc = (norm[s] != -1) ? (uint)(norm[s]) : 1; + uint norm256 = normAcc << (int)shift; + + assert(norm256 > 0); + assert(norm256 < 256); + cost += count[s] * kInverseProbabilityLog256[norm256]; + } + + return cost >> 8; + } + + public static symbolEncodingType_e ZSTD_selectEncodingType(FSE_repeat* repeatMode, uint* count, uint max, nuint mostFrequent, nuint nbSeq, uint FSELog, uint* prevCTable, short* defaultNorm, uint defaultNormLog, ZSTD_defaultPolicy_e isDefaultAllowed, ZSTD_strategy strategy) + { + if (mostFrequent == nbSeq) + { + *repeatMode = FSE_repeat.FSE_repeat_none; + if (isDefaultAllowed != default && nbSeq <= 2) + { + return symbolEncodingType_e.set_basic; + } + + return symbolEncodingType_e.set_rle; + } + + if (strategy < ZSTD_strategy.ZSTD_lazy) + { + if (isDefaultAllowed != default) + { + nuint staticFse_nbSeq_max = 1000; + nuint mult = (nuint)(10 - strategy); + nuint baseLog = 3; + nuint dynamicFse_nbSeq_min = (((nuint)(1) << (int)defaultNormLog) * mult) >> (int)baseLog; + + assert(defaultNormLog >= 5 && defaultNormLog <= 6); + assert(mult <= 9 && mult >= 7); + if ((*repeatMode == FSE_repeat.FSE_repeat_valid) && (nbSeq < staticFse_nbSeq_max)) + { + return symbolEncodingType_e.set_repeat; + } + + if ((nbSeq < dynamicFse_nbSeq_min) || (mostFrequent < (nbSeq >> (int)(defaultNormLog - 1)))) + { + *repeatMode = FSE_repeat.FSE_repeat_none; + return symbolEncodingType_e.set_basic; + } + } + } + else + { + nuint basicCost = isDefaultAllowed != default ? ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, count, max) : (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + nuint repeatCost = *repeatMode != FSE_repeat.FSE_repeat_none ? ZSTD_fseBitCost(prevCTable, count, max) : (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + nuint NCountCost = ZSTD_NCountCost(count, max, nbSeq, FSELog); + nuint compressedCost = (NCountCost << 3) + ZSTD_entropyCost(count, max, nbSeq); + + if (isDefaultAllowed != default) + { + assert((ERR_isError(basicCost)) == 0); + assert(!(*repeatMode == FSE_repeat.FSE_repeat_valid && (ERR_isError(repeatCost)) != 0)); + } + + assert((ERR_isError(NCountCost)) == 0); + assert(compressedCost < (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxCode)))); + if (basicCost <= repeatCost && basicCost <= compressedCost) + { + assert(isDefaultAllowed != default); + *repeatMode = FSE_repeat.FSE_repeat_none; + return symbolEncodingType_e.set_basic; + } + + if (repeatCost <= compressedCost) + { + assert((ERR_isError(repeatCost)) == 0); + return symbolEncodingType_e.set_repeat; + } + + assert(compressedCost < basicCost && compressedCost < repeatCost); + } + + *repeatMode = FSE_repeat.FSE_repeat_check; + return symbolEncodingType_e.set_compressed; + } + + public static nuint ZSTD_buildCTable(void* dst, nuint dstCapacity, uint* nextCTable, uint FSELog, symbolEncodingType_e type, uint* count, uint max, byte* codeTable, nuint nbSeq, short* defaultNorm, uint defaultNormLog, uint defaultMax, uint* prevCTable, nuint prevCTableSize, void* entropyWorkspace, nuint entropyWorkspaceSize) + { + byte* op = (byte*)(dst); + byte* oend = op + dstCapacity; + + switch (type) + { + case symbolEncodingType_e.set_rle: + { + + { + nuint err_code = (FSE_buildCTable_rle(nextCTable, (byte)(max))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + } + + if (dstCapacity == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + *op = codeTable[0]; + return 1; + case symbolEncodingType_e.set_repeat: + { + memcpy((void*)(nextCTable), (void*)(prevCTable), (prevCTableSize)); + } + + return 0; + case symbolEncodingType_e.set_basic: + { + + { + nuint err_code = (FSE_buildCTable_wksp(nextCTable, defaultNorm, defaultMax, defaultNormLog, entropyWorkspace, entropyWorkspaceSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + } + + return 0; + case symbolEncodingType_e.set_compressed: + { + ZSTD_BuildCTableWksp* wksp = (ZSTD_BuildCTableWksp*)(entropyWorkspace); + nuint nbSeq_1 = nbSeq; + uint tableLog = FSE_optimalTableLog(FSELog, nbSeq, max); + + if (count[codeTable[nbSeq - 1]] > 1) + { + count[codeTable[nbSeq - 1]]--; + nbSeq_1--; + } + + assert(nbSeq_1 > 1); + assert(entropyWorkspaceSize >= (nuint)(sizeof(ZSTD_BuildCTableWksp))); + + { + nuint err_code = (FSE_normalizeCount((short*)wksp->norm, tableLog, count, nbSeq_1, max, ZSTD_useLowProbCount(nbSeq_1))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint NCountSize = FSE_writeNCount((void*)op, (nuint)(oend - op), (short*)wksp->norm, max, tableLog); + + + { + nuint err_code = (NCountSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (FSE_buildCTable_wksp(nextCTable, (short*)wksp->norm, max, tableLog, (void*)wksp->wksp, (nuint)(728))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return NCountSize; + } + } + + default: + { + assert(0 != 0); + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_encodeSequences_body(void* dst, nuint dstCapacity, uint* CTable_MatchLength, byte* mlCodeTable, uint* CTable_OffsetBits, byte* ofCodeTable, uint* CTable_LitLength, byte* llCodeTable, seqDef_s* sequences, nuint nbSeq, int longOffsets) + { + BIT_CStream_t blockStream; + FSE_CState_t stateMatchLength; + FSE_CState_t stateOffsetBits; + FSE_CState_t stateLitLength; + + if ((ERR_isError(BIT_initCStream(&blockStream, dst, dstCapacity))) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq - 1]); + FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq - 1]); + FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq - 1]); + BIT_addBits(&blockStream, sequences[nbSeq - 1].litLength, LL_bits[llCodeTable[nbSeq - 1]]); + if (MEM_32bits) + { + BIT_flushBits(&blockStream); + } + + BIT_addBits(&blockStream, sequences[nbSeq - 1].matchLength, ML_bits[mlCodeTable[nbSeq - 1]]); + if (MEM_32bits) + { + BIT_flushBits(&blockStream); + } + + if (longOffsets != 0) + { + uint ofBits = ofCodeTable[nbSeq - 1]; + uint extraBits = ofBits - ((ofBits) < (((uint)(MEM_32bits ? 25 : 57)) - 1) ? (ofBits) : (((uint)(MEM_32bits ? 25 : 57)) - 1)); + + if (extraBits != 0) + { + BIT_addBits(&blockStream, sequences[nbSeq - 1].offset, extraBits); + BIT_flushBits(&blockStream); + } + + BIT_addBits(&blockStream, sequences[nbSeq - 1].offset >> (int)extraBits, ofBits - extraBits); + } + else + { + BIT_addBits(&blockStream, sequences[nbSeq - 1].offset, ofCodeTable[nbSeq - 1]); + } + + BIT_flushBits(&blockStream); + + { + nuint n; + + for (n = nbSeq - 2; n < nbSeq; n--) + { + byte llCode = llCodeTable[n]; + byte ofCode = ofCodeTable[n]; + byte mlCode = mlCodeTable[n]; + uint llBits = LL_bits[llCode]; + uint ofBits = ofCode; + uint mlBits = ML_bits[mlCode]; + + FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); + FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); + if (MEM_32bits) + { + BIT_flushBits(&blockStream); + } + + FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); + if (MEM_32bits || (ofBits + mlBits + llBits >= (uint)(64 - 7 - (9 + 9 + 8)))) + { + BIT_flushBits(&blockStream); + } + + BIT_addBits(&blockStream, sequences[n].litLength, llBits); + if (MEM_32bits && ((llBits + mlBits) > 24)) + { + BIT_flushBits(&blockStream); + } + + BIT_addBits(&blockStream, sequences[n].matchLength, mlBits); + if (MEM_32bits || (ofBits + mlBits + llBits > 56)) + { + BIT_flushBits(&blockStream); + } + + if (longOffsets != 0) + { + uint extraBits = ofBits - ((ofBits) < (((uint)(MEM_32bits ? 25 : 57)) - 1) ? (ofBits) : (((uint)(MEM_32bits ? 25 : 57)) - 1)); + + if (extraBits != 0) + { + BIT_addBits(&blockStream, sequences[n].offset, extraBits); + BIT_flushBits(&blockStream); + } + + BIT_addBits(&blockStream, sequences[n].offset >> (int)extraBits, ofBits - extraBits); + } + else + { + BIT_addBits(&blockStream, sequences[n].offset, ofBits); + } + + BIT_flushBits(&blockStream); + } + } + + FSE_flushCState(&blockStream, &stateMatchLength); + FSE_flushCState(&blockStream, &stateOffsetBits); + FSE_flushCState(&blockStream, &stateLitLength); + + { + nuint streamSize = BIT_closeCStream(&blockStream); + + if (streamSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + return streamSize; + } + } + + private static nuint ZSTD_encodeSequences_default(void* dst, nuint dstCapacity, uint* CTable_MatchLength, byte* mlCodeTable, uint* CTable_OffsetBits, byte* ofCodeTable, uint* CTable_LitLength, byte* llCodeTable, seqDef_s* sequences, nuint nbSeq, int longOffsets) + { + return ZSTD_encodeSequences_body(dst, dstCapacity, CTable_MatchLength, mlCodeTable, CTable_OffsetBits, ofCodeTable, CTable_LitLength, llCodeTable, sequences, nbSeq, longOffsets); + } + + private static nuint ZSTD_encodeSequences_bmi2(void* dst, nuint dstCapacity, uint* CTable_MatchLength, byte* mlCodeTable, uint* CTable_OffsetBits, byte* ofCodeTable, uint* CTable_LitLength, byte* llCodeTable, seqDef_s* sequences, nuint nbSeq, int longOffsets) + { + return ZSTD_encodeSequences_body(dst, dstCapacity, CTable_MatchLength, mlCodeTable, CTable_OffsetBits, ofCodeTable, CTable_LitLength, llCodeTable, sequences, nbSeq, longOffsets); + } + + public static nuint ZSTD_encodeSequences(void* dst, nuint dstCapacity, uint* CTable_MatchLength, byte* mlCodeTable, uint* CTable_OffsetBits, byte* ofCodeTable, uint* CTable_LitLength, byte* llCodeTable, seqDef_s* sequences, nuint nbSeq, int longOffsets, int bmi2) + { + if (bmi2 != 0) + { + return ZSTD_encodeSequences_bmi2(dst, dstCapacity, CTable_MatchLength, mlCodeTable, CTable_OffsetBits, ofCodeTable, CTable_LitLength, llCodeTable, sequences, nbSeq, longOffsets); + } + + return ZSTD_encodeSequences_default(dst, dstCapacity, CTable_MatchLength, mlCodeTable, CTable_OffsetBits, ofCodeTable, CTable_LitLength, llCodeTable, sequences, nbSeq, longOffsets); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressSuperblock.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressSuperblock.cs new file mode 100644 index 00000000..a6aca191 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCompressSuperblock.cs @@ -0,0 +1,601 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /** ZSTD_compressSubBlock_literal() : + * Compresses literals section for a sub-block. + * When we have to write the Huffman table we will sometimes choose a header + * size larger than necessary. This is because we have to pick the header size + * before we know the table size + compressed size, so we have a bound on the + * table size. If we guessed incorrectly, we fall back to uncompressed literals. + * + * We write the header when writeEntropy=1 and set entropyWritten=1 when we succeeded + * in writing the header, otherwise it is set to 0. + * + * hufMetadata->hType has literals block type info. + * If it is set_basic, all sub-blocks literals section will be Raw_Literals_Block. + * If it is set_rle, all sub-blocks literals section will be RLE_Literals_Block. + * If it is set_compressed, first sub-block's literals section will be Compressed_Literals_Block + * If it is set_compressed, first sub-block's literals section will be Treeless_Literals_Block + * and the following sub-blocks' literals sections will be Treeless_Literals_Block. + * @return : compressed size of literals section of a sub-block + * Or 0 if it unable to compress. + * Or error code */ + private static nuint ZSTD_compressSubBlock_literal(HUF_CElt_s* hufTable, ZSTD_hufCTablesMetadata_t* hufMetadata, byte* literals, nuint litSize, void* dst, nuint dstSize, int bmi2, int writeEntropy, int* entropyWritten) + { + nuint header = (nuint)(writeEntropy != 0 ? 200 : 0); + nuint lhSize = (nuint)(3 + ((litSize >= ((uint)(1 * (1 << 10)) - header)) ? 1 : 0) + ((litSize >= ((uint)(16 * (1 << 10)) - header)) ? 1 : 0)); + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstSize; + byte* op = ostart + lhSize; + uint singleStream = ((lhSize == 3) ? 1U : 0U); + symbolEncodingType_e hType = writeEntropy != 0 ? hufMetadata->hType : symbolEncodingType_e.set_repeat; + nuint cLitSize = 0; + + *entropyWritten = 0; + if (litSize == 0 || hufMetadata->hType == symbolEncodingType_e.set_basic) + { + return ZSTD_noCompressLiterals(dst, dstSize, (void*)literals, litSize); + } + else if (hufMetadata->hType == symbolEncodingType_e.set_rle) + { + return ZSTD_compressRleLiteralsBlock(dst, dstSize, (void*)literals, litSize); + } + + assert(litSize > 0); + assert(hufMetadata->hType == symbolEncodingType_e.set_compressed || hufMetadata->hType == symbolEncodingType_e.set_repeat); + if (writeEntropy != 0 && hufMetadata->hType == symbolEncodingType_e.set_compressed) + { + memcpy((void*)(op), (void*)(hufMetadata->hufDesBuffer), (hufMetadata->hufDesSize)); + op += hufMetadata->hufDesSize; + cLitSize += hufMetadata->hufDesSize; + } + + + { + nuint cSize = singleStream != 0 ? HUF_compress1X_usingCTable((void*)op, (nuint)(oend - op), (void*)literals, litSize, hufTable) : HUF_compress4X_usingCTable((void*)op, (nuint)(oend - op), (void*)literals, litSize, hufTable); + + op += cSize; + cLitSize += cSize; + if (cSize == 0 || (ERR_isError(cSize)) != 0) + { + return 0; + } + + if (writeEntropy == 0 && cLitSize >= litSize) + { + return ZSTD_noCompressLiterals(dst, dstSize, (void*)literals, litSize); + } + + if (lhSize < (nuint)(3 + ((cLitSize >= (uint)(1 * (1 << 10))) ? 1 : 0) + ((cLitSize >= (uint)(16 * (1 << 10))) ? 1 : 0))) + { + assert(cLitSize > litSize); + return ZSTD_noCompressLiterals(dst, dstSize, (void*)literals, litSize); + } + + } + + switch (lhSize) + { + case 3: + { + uint lhc = (uint)(hType + ((singleStream == 0 ? 1 : 0) << 2)) + ((uint)(litSize) << 4) + ((uint)(cLitSize) << 14); + + MEM_writeLE24((void*)ostart, lhc); + break; + } + + case 4: + { + uint lhc = (uint)(hType + (2 << 2)) + ((uint)(litSize) << 4) + ((uint)(cLitSize) << 18); + + MEM_writeLE32((void*)ostart, lhc); + break; + } + + case 5: + { + uint lhc = (uint)(hType + (3 << 2)) + ((uint)(litSize) << 4) + ((uint)(cLitSize) << 22); + + MEM_writeLE32((void*)ostart, lhc); + ostart[4] = (byte)(cLitSize >> 10); + break; + } + + default: + { + assert(0 != 0); + } + break; + } + + *entropyWritten = 1; + return (nuint)(op - ostart); + } + + private static nuint ZSTD_seqDecompressedSize(seqStore_t* seqStore, seqDef_s* sequences, nuint nbSeq, nuint litSize, int lastSequence) + { + seqDef_s* sstart = sequences; + seqDef_s* send = sequences + nbSeq; + seqDef_s* sp = sstart; + nuint matchLengthSum = 0; + nuint litLengthSum = 0; + + while (send - sp > 0) + { + ZSTD_sequenceLength seqLen = ZSTD_getSequenceLength(seqStore, sp); + + litLengthSum += seqLen.litLength; + matchLengthSum += seqLen.matchLength; + sp++; + } + + assert(litLengthSum <= litSize); + if (lastSequence == 0) + { + assert(litLengthSum == litSize); + } + + return matchLengthSum + litSize; + } + + /** ZSTD_compressSubBlock_sequences() : + * Compresses sequences section for a sub-block. + * fseMetadata->llType, fseMetadata->ofType, and fseMetadata->mlType have + * symbol compression modes for the super-block. + * The first successfully compressed block will have these in its header. + * We set entropyWritten=1 when we succeed in compressing the sequences. + * The following sub-blocks will always have repeat mode. + * @return : compressed size of sequences section of a sub-block + * Or 0 if it is unable to compress + * Or error code. */ + private static nuint ZSTD_compressSubBlock_sequences(ZSTD_fseCTables_t* fseTables, ZSTD_fseCTablesMetadata_t* fseMetadata, seqDef_s* sequences, nuint nbSeq, byte* llCode, byte* mlCode, byte* ofCode, ZSTD_CCtx_params_s* cctxParams, void* dst, nuint dstCapacity, int bmi2, int writeEntropy, int* entropyWritten) + { + int longOffsets = ((cctxParams->cParams.windowLog > ((uint)(MEM_32bits ? 25 : 57))) ? 1 : 0); + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstCapacity; + byte* op = ostart; + byte* seqHead; + + *entropyWritten = 0; + if ((oend - op) < 3 + 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (nbSeq < 0x7F) + { + *op++ = (byte)(nbSeq); + } + else if (nbSeq < 0x7F00) + { + op[0] = (byte)((nbSeq >> 8) + 0x80); op[1] = (byte)(nbSeq); op += 2; + } + else + { + op[0] = 0xFF; MEM_writeLE16((void*)(op + 1), (ushort)(nbSeq - 0x7F00)); op += 3; + } + + if (nbSeq == 0) + { + return (nuint)(op - ostart); + } + + seqHead = op++; + if (writeEntropy != 0) + { + uint LLtype = (uint)fseMetadata->llType; + uint Offtype = (uint)fseMetadata->ofType; + uint MLtype = (uint)fseMetadata->mlType; + + *seqHead = (byte)((LLtype << 6) + (Offtype << 4) + (MLtype << 2)); + memcpy((void*)(op), (void*)(fseMetadata->fseTablesBuffer), (fseMetadata->fseTablesSize)); + op += fseMetadata->fseTablesSize; + } + else + { + uint repeat = (uint)symbolEncodingType_e.set_repeat; + + *seqHead = (byte)((repeat << 6) + (repeat << 4) + (repeat << 2)); + } + + + { + nuint bitstreamSize = ZSTD_encodeSequences((void*)op, (nuint)(oend - op), (uint*)fseTables->matchlengthCTable, mlCode, (uint*)fseTables->offcodeCTable, ofCode, (uint*)fseTables->litlengthCTable, llCode, sequences, nbSeq, longOffsets, bmi2); + + + { + nuint err_code = (bitstreamSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + op += bitstreamSize; + if (writeEntropy != 0 && fseMetadata->lastCountSize != 0 && fseMetadata->lastCountSize + bitstreamSize < 4) + { + assert(fseMetadata->lastCountSize + bitstreamSize == 3); + return 0; + } + + } + + if (op - seqHead < 4) + { + return 0; + } + + *entropyWritten = 1; + return (nuint)(op - ostart); + } + + /** ZSTD_compressSubBlock() : + * Compresses a single sub-block. + * @return : compressed size of the sub-block + * Or 0 if it failed to compress. */ + private static nuint ZSTD_compressSubBlock(ZSTD_entropyCTables_t* entropy, ZSTD_entropyCTablesMetadata_t* entropyMetadata, seqDef_s* sequences, nuint nbSeq, byte* literals, nuint litSize, byte* llCode, byte* mlCode, byte* ofCode, ZSTD_CCtx_params_s* cctxParams, void* dst, nuint dstCapacity, int bmi2, int writeLitEntropy, int writeSeqEntropy, int* litEntropyWritten, int* seqEntropyWritten, uint lastBlock) + { + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstCapacity; + byte* op = ostart + ZSTD_blockHeaderSize; + + + { + nuint cLitSize = ZSTD_compressSubBlock_literal((HUF_CElt_s*)(entropy->huf.CTable), &entropyMetadata->hufMetadata, literals, litSize, (void*)op, (nuint)(oend - op), bmi2, writeLitEntropy, litEntropyWritten); + + + { + nuint err_code = (cLitSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (cLitSize == 0) + { + return 0; + } + + op += cLitSize; + } + + + { + nuint cSeqSize = ZSTD_compressSubBlock_sequences(&entropy->fse, &entropyMetadata->fseMetadata, sequences, nbSeq, llCode, mlCode, ofCode, cctxParams, (void*)op, (nuint)(oend - op), bmi2, writeSeqEntropy, seqEntropyWritten); + + + { + nuint err_code = (cSeqSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (cSeqSize == 0) + { + return 0; + } + + op += cSeqSize; + } + + + { + nuint cSize = (nuint)((ulong)((op - ostart)) - ZSTD_blockHeaderSize); + uint cBlockHeader24 = lastBlock + (((uint)(blockType_e.bt_compressed)) << 1) + (uint)(cSize << 3); + + MEM_writeLE24((void*)ostart, cBlockHeader24); + } + + return (nuint)(op - ostart); + } + + private static nuint ZSTD_estimateSubBlockSize_literal(byte* literals, nuint litSize, ZSTD_hufCTables_t* huf, ZSTD_hufCTablesMetadata_t* hufMetadata, void* workspace, nuint wkspSize, int writeEntropy) + { + uint* countWksp = (uint*)(workspace); + uint maxSymbolValue = 255; + nuint literalSectionHeaderSize = 3; + + if (hufMetadata->hType == symbolEncodingType_e.set_basic) + { + return litSize; + } + else if (hufMetadata->hType == symbolEncodingType_e.set_rle) + { + return 1; + } + else if (hufMetadata->hType == symbolEncodingType_e.set_compressed || hufMetadata->hType == symbolEncodingType_e.set_repeat) + { + nuint largest = HIST_count_wksp(countWksp, &maxSymbolValue, (void*)(byte*)(literals), litSize, workspace, wkspSize); + + if ((ERR_isError(largest)) != 0) + { + return litSize; + } + + + { + nuint cLitSizeEstimate = HUF_estimateCompressedSize((HUF_CElt_s*)(huf->CTable), countWksp, maxSymbolValue); + + if (writeEntropy != 0) + { + cLitSizeEstimate += hufMetadata->hufDesSize; + } + + return cLitSizeEstimate + literalSectionHeaderSize; + } + } + + assert(0 != 0); + return 0; + } + + private static nuint ZSTD_estimateSubBlockSize_symbolType(symbolEncodingType_e type, byte* codeTable, uint maxCode, nuint nbSeq, uint* fseCTable, uint* additionalBits, short* defaultNorm, uint defaultNormLog, uint defaultMax, void* workspace, nuint wkspSize) + { + uint* countWksp = (uint*)(workspace); + byte* ctp = codeTable; + byte* ctStart = ctp; + byte* ctEnd = ctStart + nbSeq; + nuint cSymbolTypeSizeEstimateInBits = 0; + uint max = maxCode; + + HIST_countFast_wksp(countWksp, &max, (void*)codeTable, nbSeq, workspace, wkspSize); + if (type == symbolEncodingType_e.set_basic) + { + assert(max <= defaultMax); + cSymbolTypeSizeEstimateInBits = max <= defaultMax ? ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, countWksp, max) : (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + else if (type == symbolEncodingType_e.set_rle) + { + cSymbolTypeSizeEstimateInBits = 0; + } + else if (type == symbolEncodingType_e.set_compressed || type == symbolEncodingType_e.set_repeat) + { + cSymbolTypeSizeEstimateInBits = ZSTD_fseBitCost(fseCTable, countWksp, max); + } + + if ((ERR_isError(cSymbolTypeSizeEstimateInBits)) != 0) + { + return nbSeq * 10; + } + + while (ctp < ctEnd) + { + if (additionalBits != null) + { + cSymbolTypeSizeEstimateInBits += additionalBits[*ctp]; + } + else + { + cSymbolTypeSizeEstimateInBits += *ctp; + } + + ctp++; + } + + return cSymbolTypeSizeEstimateInBits / 8; + } + + private static nuint ZSTD_estimateSubBlockSize_sequences(byte* ofCodeTable, byte* llCodeTable, byte* mlCodeTable, nuint nbSeq, ZSTD_fseCTables_t* fseTables, ZSTD_fseCTablesMetadata_t* fseMetadata, void* workspace, nuint wkspSize, int writeEntropy) + { + nuint sequencesSectionHeaderSize = 3; + nuint cSeqSizeEstimate = 0; + + if (nbSeq == 0) + { + return sequencesSectionHeaderSize; + } + + cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->ofType, ofCodeTable, 31, nbSeq, (uint*)fseTables->offcodeCTable, (uint*)null, (short*)OF_defaultNorm, OF_defaultNormLog, 28, workspace, wkspSize); + cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->llType, llCodeTable, 35, nbSeq, (uint*)fseTables->litlengthCTable, (uint*)LL_bits, (short*)LL_defaultNorm, LL_defaultNormLog, 35, workspace, wkspSize); + cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->mlType, mlCodeTable, 52, nbSeq, (uint*)fseTables->matchlengthCTable, (uint*)ML_bits, (short*)ML_defaultNorm, ML_defaultNormLog, 52, workspace, wkspSize); + if (writeEntropy != 0) + { + cSeqSizeEstimate += fseMetadata->fseTablesSize; + } + + return cSeqSizeEstimate + sequencesSectionHeaderSize; + } + + private static nuint ZSTD_estimateSubBlockSize(byte* literals, nuint litSize, byte* ofCodeTable, byte* llCodeTable, byte* mlCodeTable, nuint nbSeq, ZSTD_entropyCTables_t* entropy, ZSTD_entropyCTablesMetadata_t* entropyMetadata, void* workspace, nuint wkspSize, int writeLitEntropy, int writeSeqEntropy) + { + nuint cSizeEstimate = 0; + + cSizeEstimate += ZSTD_estimateSubBlockSize_literal(literals, litSize, &entropy->huf, &entropyMetadata->hufMetadata, workspace, wkspSize, writeLitEntropy); + cSizeEstimate += ZSTD_estimateSubBlockSize_sequences(ofCodeTable, llCodeTable, mlCodeTable, nbSeq, &entropy->fse, &entropyMetadata->fseMetadata, workspace, wkspSize, writeSeqEntropy); + return cSizeEstimate + ZSTD_blockHeaderSize; + } + + private static int ZSTD_needSequenceEntropyTables(ZSTD_fseCTablesMetadata_t* fseMetadata) + { + if (fseMetadata->llType == symbolEncodingType_e.set_compressed || fseMetadata->llType == symbolEncodingType_e.set_rle) + { + return 1; + } + + if (fseMetadata->mlType == symbolEncodingType_e.set_compressed || fseMetadata->mlType == symbolEncodingType_e.set_rle) + { + return 1; + } + + if (fseMetadata->ofType == symbolEncodingType_e.set_compressed || fseMetadata->ofType == symbolEncodingType_e.set_rle) + { + return 1; + } + + return 0; + } + + /** ZSTD_compressSubBlock_multi() : + * Breaks super-block into multiple sub-blocks and compresses them. + * Entropy will be written to the first block. + * The following blocks will use repeat mode to compress. + * All sub-blocks are compressed blocks (no raw or rle blocks). + * @return : compressed size of the super block (which is multiple ZSTD blocks) + * Or 0 if it failed to compress. */ + private static nuint ZSTD_compressSubBlock_multi(seqStore_t* seqStorePtr, ZSTD_compressedBlockState_t* prevCBlock, ZSTD_compressedBlockState_t* nextCBlock, ZSTD_entropyCTablesMetadata_t* entropyMetadata, ZSTD_CCtx_params_s* cctxParams, void* dst, nuint dstCapacity, void* src, nuint srcSize, int bmi2, uint lastBlock, void* workspace, nuint wkspSize) + { + seqDef_s* sstart = seqStorePtr->sequencesStart; + seqDef_s* send = seqStorePtr->sequences; + seqDef_s* sp = sstart; + byte* lstart = seqStorePtr->litStart; + byte* lend = seqStorePtr->lit; + byte* lp = lstart; + byte* ip = (byte*)(src); + byte* iend = ip + srcSize; + byte* ostart = (byte*)(dst); + byte* oend = ostart + dstCapacity; + byte* op = ostart; + byte* llCodePtr = seqStorePtr->llCode; + byte* mlCodePtr = seqStorePtr->mlCode; + byte* ofCodePtr = seqStorePtr->ofCode; + nuint targetCBlockSize = cctxParams->targetCBlockSize; + nuint litSize, seqCount; + int writeLitEntropy = ((entropyMetadata->hufMetadata.hType == symbolEncodingType_e.set_compressed) ? 1 : 0); + int writeSeqEntropy = 1; + int lastSequence = 0; + + litSize = 0; + seqCount = 0; + do + { + nuint cBlockSizeEstimate = 0; + + if (sstart == send) + { + lastSequence = 1; + } + else + { + seqDef_s* sequence = sp + seqCount; + + lastSequence = ((sequence == send - 1) ? 1 : 0); + litSize += ZSTD_getSequenceLength(seqStorePtr, sequence).litLength; + seqCount++; + } + + if (lastSequence != 0) + { + assert(lp <= lend); + assert(litSize <= (nuint)(lend - lp)); + litSize = (nuint)(lend - lp); + } + + cBlockSizeEstimate = ZSTD_estimateSubBlockSize(lp, litSize, ofCodePtr, llCodePtr, mlCodePtr, seqCount, &nextCBlock->entropy, entropyMetadata, workspace, wkspSize, writeLitEntropy, writeSeqEntropy); + if (cBlockSizeEstimate > targetCBlockSize || lastSequence != 0) + { + int litEntropyWritten = 0; + int seqEntropyWritten = 0; + nuint decompressedSize = ZSTD_seqDecompressedSize(seqStorePtr, sp, seqCount, litSize, lastSequence); + nuint cSize = ZSTD_compressSubBlock(&nextCBlock->entropy, entropyMetadata, sp, seqCount, lp, litSize, llCodePtr, mlCodePtr, ofCodePtr, cctxParams, (void*)op, (nuint)(oend - op), bmi2, writeLitEntropy, writeSeqEntropy, &litEntropyWritten, &seqEntropyWritten, ((lastBlock != 0 && lastSequence != 0) ? 1U : 0U)); + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (cSize > 0 && cSize < decompressedSize) + { + assert(ip + decompressedSize <= iend); + ip += decompressedSize; + sp += seqCount; + lp += litSize; + op += cSize; + llCodePtr += seqCount; + mlCodePtr += seqCount; + ofCodePtr += seqCount; + litSize = 0; + seqCount = 0; + if (litEntropyWritten != 0) + { + writeLitEntropy = 0; + } + + if (seqEntropyWritten != 0) + { + writeSeqEntropy = 0; + } + } + } + } + while (lastSequence == 0); + + if (writeLitEntropy != 0) + { + memcpy((void*)(&nextCBlock->entropy.huf), (void*)(&prevCBlock->entropy.huf), ((nuint)(sizeof(ZSTD_hufCTables_t)))); + } + + if (writeSeqEntropy != 0 && (ZSTD_needSequenceEntropyTables(&entropyMetadata->fseMetadata)) != 0) + { + return 0; + } + + if (ip < iend) + { + nuint cSize = ZSTD_noCompressBlock((void*)op, (nuint)(oend - op), (void*)ip, (nuint)(iend - ip), lastBlock); + + + { + nuint err_code = (cSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(cSize != 0); + op += cSize; + if (sp < send) + { + seqDef_s* seq; + repcodes_s rep; + + memcpy((void*)(&rep), (void*)(prevCBlock->rep), ((nuint)(sizeof(repcodes_s)))); + for (seq = sstart; seq < sp; ++seq) + { + rep = ZSTD_updateRep(rep.rep, seq->offset - 1, ((ZSTD_getSequenceLength(seqStorePtr, seq).litLength == 0) ? 1U : 0U)); + } + + memcpy((void*)(nextCBlock->rep), (void*)(&rep), ((nuint)(sizeof(repcodes_s)))); + } + } + + return (nuint)(op - ostart); + } + + /* ZSTD_compressSuperBlock() : + * Used to compress a super block when targetCBlockSize is being used. + * The given block will be compressed into multiple sub blocks that are around targetCBlockSize. */ + public static nuint ZSTD_compressSuperBlock(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint lastBlock) + { + ZSTD_entropyCTablesMetadata_t entropyMetadata; + + + { + nuint err_code = (ZSTD_buildBlockEntropyStats(&zc->seqStore, &zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy, &zc->appliedParams, &entropyMetadata, (void*)zc->entropyWorkspace, ((uint)(((6 << 10) + 256)) + ((nuint)(4) * (uint)((((35) > (52) ? (35) : (52)) + 2)))))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_compressSubBlock_multi(&zc->seqStore, zc->blockState.prevCBlock, zc->blockState.nextCBlock, &entropyMetadata, &zc->appliedParams, dst, dstCapacity, src, srcSize, zc->bmi2, lastBlock, (void*)zc->entropyWorkspace, ((uint)(((6 << 10) + 256)) + ((nuint)(sizeof(uint)) * (uint)((((35) > (52) ? (35) : (52)) + 2))))); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCwksp.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCwksp.cs new file mode 100644 index 00000000..102c9b34 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdCwksp.cs @@ -0,0 +1,478 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_assert_internal_consistency(ZSTD_cwksp* ws) + { + assert(ws->workspace <= ws->objectEnd); + assert(ws->objectEnd <= ws->tableEnd); + assert(ws->objectEnd <= ws->tableValidEnd); + assert(ws->tableEnd <= ws->allocStart); + assert(ws->tableValidEnd <= ws->allocStart); + assert(ws->allocStart <= ws->workspaceEnd); + } + + /** + * Align must be a power of 2. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_align(nuint size, nuint align) + { + nuint mask = align - 1; + + assert((align & mask) == 0); + return (size + mask) & ~mask; + } + + /** + * Use this to determine how much space in the workspace we will consume to + * allocate this object. (Normally it should be exactly the size of the object, + * but under special conditions, like ASAN, where we pad each object, it might + * be larger.) + * + * Since tables aren't currently redzoned, you don't need to call through this + * to figure out how much space you need for the matchState tables. Everything + * else is though. + * + * Do not use for sizing aligned buffers. Instead, use ZSTD_cwksp_aligned_alloc_size(). + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_alloc_size(nuint size) + { + if (size == 0) + { + return 0; + } + + return size; + } + + /** + * Returns an adjusted alloc size that is the nearest larger multiple of 64 bytes. + * Used to determine the number of bytes required for a given "aligned". + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_aligned_alloc_size(nuint size) + { + return ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(size, 64)); + } + + /** + * Returns the amount of additional space the cwksp must allocate + * for internal purposes (currently only alignment). + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_slack_space_required() + { + nuint slackSpace = 64; + + return slackSpace; + } + + /** + * Return the number of additional bytes required to align a pointer to the given number of bytes. + * alignBytes must be a power of two. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_bytes_to_align_ptr(void* ptr, nuint alignBytes) + { + nuint alignBytesMask = alignBytes - 1; + nuint bytes = (alignBytes - ((nuint)(ptr) & (alignBytesMask))) & alignBytesMask; + + assert((alignBytes & alignBytesMask) == 0); + assert(bytes != 64); + return bytes; + } + + /** + * Internal function. Do not use directly. + * Reserves the given number of bytes within the aligned/buffer segment of the wksp, which + * counts from the end of the wksp. (as opposed to the object/table segment) + * + * Returns a pointer to the beginning of that space. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void* ZSTD_cwksp_reserve_internal_buffer_space(ZSTD_cwksp* ws, nuint bytes) + { + void* alloc = (void*)((byte*)(ws->allocStart) - bytes); + void* bottom = ws->tableEnd; + + ZSTD_cwksp_assert_internal_consistency(ws); + assert(alloc >= bottom); + if (alloc < bottom) + { + ws->allocFailed = 1; + return null; + } + + if (alloc < ws->tableValidEnd) + { + ws->tableValidEnd = alloc; + } + + ws->allocStart = alloc; + return alloc; + } + + /** + * Moves the cwksp to the next phase, and does any necessary allocations. + * Returns a 0 on success, or zstd error + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_internal_advance_phase(ZSTD_cwksp* ws, ZSTD_cwksp_alloc_phase_e phase) + { + assert(phase >= ws->phase); + if (phase > ws->phase) + { + if (ws->phase < ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_buffers && phase >= ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_buffers) + { + ws->tableValidEnd = ws->objectEnd; + } + + if (ws->phase < ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned && phase >= ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned) + { + + { + nuint bytesToAlign = 64 - ZSTD_cwksp_bytes_to_align_ptr(ws->allocStart, 64); + + if ((ZSTD_cwksp_reserve_internal_buffer_space(ws, bytesToAlign)) == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + } + + + { + void* alloc = ws->objectEnd; + nuint bytesToAlign = ZSTD_cwksp_bytes_to_align_ptr(alloc, 64); + void* end = (void*)((byte*)(alloc) + bytesToAlign); + + if (end > ws->workspaceEnd) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + ws->objectEnd = end; + ws->tableEnd = end; + ws->tableValidEnd = end; + } + } + + ws->phase = phase; + ZSTD_cwksp_assert_internal_consistency(ws); + } + + return 0; + } + + /** + * Returns whether this object/buffer/etc was allocated in this workspace. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_cwksp_owns_buffer(ZSTD_cwksp* ws, void* ptr) + { + return (((ptr != null) && (ws->workspace <= ptr) && (ptr <= ws->workspaceEnd)) ? 1 : 0); + } + + /** + * Internal function. Do not use directly. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void* ZSTD_cwksp_reserve_internal(ZSTD_cwksp* ws, nuint bytes, ZSTD_cwksp_alloc_phase_e phase) + { + void* alloc; + + if ((ERR_isError(ZSTD_cwksp_internal_advance_phase(ws, phase))) != 0 || bytes == 0) + { + return null; + } + + alloc = ZSTD_cwksp_reserve_internal_buffer_space(ws, bytes); + return alloc; + } + + /** + * Reserves and returns unaligned memory. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static byte* ZSTD_cwksp_reserve_buffer(ZSTD_cwksp* ws, nuint bytes) + { + return (byte*)(ZSTD_cwksp_reserve_internal(ws, bytes, ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_buffers)); + } + + /** + * Reserves and returns memory sized on and aligned on ZSTD_CWKSP_ALIGNMENT_BYTES (64 bytes). + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void* ZSTD_cwksp_reserve_aligned(ZSTD_cwksp* ws, nuint bytes) + { + void* ptr = ZSTD_cwksp_reserve_internal(ws, ZSTD_cwksp_align(bytes, 64), ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned); + + assert(((nuint)(ptr) & (uint)((64 - 1))) == 0); + return ptr; + } + + /** + * Aligned on 64 bytes. These buffers have the special property that + * their values remain constrained, allowing us to re-use them without + * memset()-ing them. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void* ZSTD_cwksp_reserve_table(ZSTD_cwksp* ws, nuint bytes) + { + ZSTD_cwksp_alloc_phase_e phase = ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned; + void* alloc; + void* end; + void* top; + + if ((ERR_isError(ZSTD_cwksp_internal_advance_phase(ws, phase))) != 0) + { + return null; + } + + alloc = ws->tableEnd; + end = (byte*)(alloc) + bytes; + top = ws->allocStart; + assert((bytes & ((nuint)(sizeof(uint)) - 1)) == 0); + ZSTD_cwksp_assert_internal_consistency(ws); + assert(end <= top); + if (end > top) + { + ws->allocFailed = 1; + return null; + } + + ws->tableEnd = end; + assert((bytes & (uint)((64 - 1))) == 0); + assert(((nuint)(alloc) & (uint)((64 - 1))) == 0); + return alloc; + } + + /** + * Aligned on sizeof(void*). + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void* ZSTD_cwksp_reserve_object(ZSTD_cwksp* ws, nuint bytes) + { + nuint roundedBytes = ZSTD_cwksp_align(bytes, (nuint)(sizeof(void*))); + void* alloc = ws->objectEnd; + void* end = (void*)((byte*)(alloc) + roundedBytes); + + assert(((nuint)(alloc) & ((nuint)(sizeof(void*)) - 1)) == 0); + assert((bytes & ((nuint)(sizeof(void*)) - 1)) == 0); + ZSTD_cwksp_assert_internal_consistency(ws); + if (ws->phase != ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_objects || end > ws->workspaceEnd) + { + ws->allocFailed = 1; + return null; + } + + ws->objectEnd = end; + ws->tableEnd = end; + ws->tableValidEnd = end; + return alloc; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_mark_tables_dirty(ZSTD_cwksp* ws) + { + assert(ws->tableValidEnd >= ws->objectEnd); + assert(ws->tableValidEnd <= ws->allocStart); + ws->tableValidEnd = ws->objectEnd; + ZSTD_cwksp_assert_internal_consistency(ws); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_mark_tables_clean(ZSTD_cwksp* ws) + { + assert(ws->tableValidEnd >= ws->objectEnd); + assert(ws->tableValidEnd <= ws->allocStart); + if (ws->tableValidEnd < ws->tableEnd) + { + ws->tableValidEnd = ws->tableEnd; + } + + ZSTD_cwksp_assert_internal_consistency(ws); + } + + /** + * Zero the part of the allocated tables not already marked clean. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_clean_tables(ZSTD_cwksp* ws) + { + assert(ws->tableValidEnd >= ws->objectEnd); + assert(ws->tableValidEnd <= ws->allocStart); + if (ws->tableValidEnd < ws->tableEnd) + { + memset((ws->tableValidEnd), (0), (nuint)(((byte*)(ws->tableEnd) - (byte*)(ws->tableValidEnd)))); + } + + ZSTD_cwksp_mark_tables_clean(ws); + } + + /** + * Invalidates table allocations. + * All other allocations remain valid. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_clear_tables(ZSTD_cwksp* ws) + { + ws->tableEnd = ws->objectEnd; + ZSTD_cwksp_assert_internal_consistency(ws); + } + + /** + * Invalidates all buffer, aligned, and table allocations. + * Object allocations remain valid. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_clear(ZSTD_cwksp* ws) + { + ws->tableEnd = ws->objectEnd; + ws->allocStart = ws->workspaceEnd; + ws->allocFailed = 0; + if (ws->phase > ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_buffers) + { + ws->phase = ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_buffers; + } + + ZSTD_cwksp_assert_internal_consistency(ws); + } + + /** + * The provided workspace takes ownership of the buffer [start, start+size). + * Any existing values in the workspace are ignored (the previously managed + * buffer, if present, must be separately freed). + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_init(ZSTD_cwksp* ws, void* start, nuint size, ZSTD_cwksp_static_alloc_e isStatic) + { + assert(((nuint)(start) & ((nuint)(sizeof(void*)) - 1)) == 0); + ws->workspace = start; + ws->workspaceEnd = (byte*)(start) + size; + ws->objectEnd = ws->workspace; + ws->tableValidEnd = ws->objectEnd; + ws->phase = ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_objects; + ws->isStatic = isStatic; + ZSTD_cwksp_clear(ws); + ws->workspaceOversizedDuration = 0; + ZSTD_cwksp_assert_internal_consistency(ws); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_create(ZSTD_cwksp* ws, nuint size, ZSTD_customMem customMem) + { + void* workspace = ZSTD_customMalloc(size, customMem); + + if (workspace == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + ZSTD_cwksp_init(ws, workspace, size, ZSTD_cwksp_static_alloc_e.ZSTD_cwksp_dynamic_alloc); + return 0; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_free(ZSTD_cwksp* ws, ZSTD_customMem customMem) + { + void* ptr = ws->workspace; + + memset((void*)(ws), (0), ((nuint)(sizeof(ZSTD_cwksp)))); + ZSTD_customFree(ptr, customMem); + } + + /** + * Moves the management of a workspace from one cwksp to another. The src cwksp + * is left in an invalid state (src must be re-init()'ed before it's used again). + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_move(ZSTD_cwksp* dst, ZSTD_cwksp* src) + { + *dst = *src; + memset((void*)(src), (0), ((nuint)(sizeof(ZSTD_cwksp)))); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_sizeof(ZSTD_cwksp* ws) + { + return (nuint)((byte*)(ws->workspaceEnd) - (byte*)(ws->workspace)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_used(ZSTD_cwksp* ws) + { + return (nuint)((byte*)(ws->tableEnd) - (byte*)(ws->workspace)) + (nuint)((byte*)(ws->workspaceEnd) - (byte*)(ws->allocStart)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_cwksp_reserve_failed(ZSTD_cwksp* ws) + { + return (int)ws->allocFailed; + } + + /* ZSTD_alignmentSpaceWithinBounds() : + * Returns if the estimated space needed for a wksp is within an acceptable limit of the + * actual amount of space used. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_cwksp_estimated_space_within_bounds(ZSTD_cwksp* ws, nuint estimatedSpace, int resizedWorkspace) + { + if (resizedWorkspace != 0) + { + return ((ZSTD_cwksp_used(ws) == estimatedSpace) ? 1 : 0); + } + else + { + return (((ZSTD_cwksp_used(ws) >= estimatedSpace - 63) && (ZSTD_cwksp_used(ws) <= estimatedSpace + 63)) ? 1 : 0); + } + } + + /*-************************************* + * Functions + ***************************************/ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_cwksp_available_space(ZSTD_cwksp* ws) + { + return (nuint)((byte*)(ws->allocStart) - (byte*)(ws->tableEnd)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_cwksp_check_available(ZSTD_cwksp* ws, nuint additionalNeededSpace) + { + return ((ZSTD_cwksp_available_space(ws) >= additionalNeededSpace) ? 1 : 0); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_cwksp_check_too_large(ZSTD_cwksp* ws, nuint additionalNeededSpace) + { + return ZSTD_cwksp_check_available(ws, additionalNeededSpace * 3); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_cwksp_check_wasteful(ZSTD_cwksp* ws, nuint additionalNeededSpace) + { + return (((ZSTD_cwksp_check_too_large(ws, additionalNeededSpace)) != 0 && ws->workspaceOversizedDuration > 128) ? 1 : 0); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_cwksp_bump_oversized_duration(ZSTD_cwksp* ws, nuint additionalNeededSpace) + { + if ((ZSTD_cwksp_check_too_large(ws, additionalNeededSpace)) != 0) + { + ws->workspaceOversizedDuration++; + } + else + { + ws->workspaceOversizedDuration = 0; + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDdict.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDdict.cs new file mode 100644 index 00000000..b878db51 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDdict.cs @@ -0,0 +1,289 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /* note: several prototypes are already published in `zstd.h` : + * ZSTD_createDDict() + * ZSTD_createDDict_byReference() + * ZSTD_createDDict_advanced() + * ZSTD_freeDDict() + * ZSTD_initStaticDDict() + * ZSTD_sizeof_DDict() + * ZSTD_estimateDDictSize() + * ZSTD_getDictID_fromDict() + */ + public static void* ZSTD_DDict_dictContent(ZSTD_DDict_s* ddict) + { + assert(ddict != null); + return ddict->dictContent; + } + + public static nuint ZSTD_DDict_dictSize(ZSTD_DDict_s* ddict) + { + assert(ddict != null); + return ddict->dictSize; + } + + public static void ZSTD_copyDDictParameters(ZSTD_DCtx_s* dctx, ZSTD_DDict_s* ddict) + { + assert(dctx != null); + assert(ddict != null); + dctx->dictID = ddict->dictID; + dctx->prefixStart = ddict->dictContent; + dctx->virtualStart = ddict->dictContent; + dctx->dictEnd = (byte*)(ddict->dictContent) + ddict->dictSize; + dctx->previousDstEnd = dctx->dictEnd; + if (ddict->entropyPresent != 0) + { + dctx->litEntropy = 1; + dctx->fseEntropy = 1; + dctx->LLTptr = ddict->entropy.LLTable; + dctx->MLTptr = ddict->entropy.MLTable; + dctx->OFTptr = ddict->entropy.OFTable; + dctx->HUFptr = ddict->entropy.hufTable; + dctx->entropy.rep[0] = ddict->entropy.rep[0]; + dctx->entropy.rep[1] = ddict->entropy.rep[1]; + dctx->entropy.rep[2] = ddict->entropy.rep[2]; + } + else + { + dctx->litEntropy = 0; + dctx->fseEntropy = 0; + } + } + + private static nuint ZSTD_loadEntropy_intoDDict(ZSTD_DDict_s* ddict, ZSTD_dictContentType_e dictContentType) + { + ddict->dictID = 0; + ddict->entropyPresent = 0; + if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_rawContent) + { + return 0; + } + + if (ddict->dictSize < 8) + { + if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_fullDict) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + return 0; + } + + + { + uint magic = MEM_readLE32(ddict->dictContent); + + if (magic != 0xEC30A437) + { + if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_fullDict) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + return 0; + } + } + + ddict->dictID = MEM_readLE32((void*)((sbyte*)(ddict->dictContent) + 4)); + if ((ERR_isError(ZSTD_loadDEntropy(&ddict->entropy, ddict->dictContent, ddict->dictSize))) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + ddict->entropyPresent = 1; + return 0; + } + + private static nuint ZSTD_initDDict_internal(ZSTD_DDict_s* ddict, void* dict, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType) + { + if ((dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef) || dict == null || dictSize == 0) + { + ddict->dictBuffer = null; + ddict->dictContent = dict; + if (dict == null) + { + dictSize = 0; + } + } + else + { + void* internalBuffer = ZSTD_customMalloc(dictSize, ddict->cMem); + + ddict->dictBuffer = internalBuffer; + ddict->dictContent = internalBuffer; + if (internalBuffer == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + memcpy((internalBuffer), (dict), (dictSize)); + } + + ddict->dictSize = dictSize; + ddict->entropy.hufTable[0] = (uint)((12) * 0x1000001); + + { + nuint err_code = (ZSTD_loadEntropy_intoDDict(ddict, dictContentType)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + public static ZSTD_DDict_s* ZSTD_createDDict_advanced(void* dict, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType, ZSTD_customMem customMem) + { + if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + { + return (ZSTD_DDict_s*)null; + } + + + { + ZSTD_DDict_s* ddict = (ZSTD_DDict_s*)(ZSTD_customMalloc((nuint)(sizeof(ZSTD_DDict_s)), customMem)); + + if (ddict == null) + { + return (ZSTD_DDict_s*)null; + } + + ddict->cMem = customMem; + + { + nuint initResult = ZSTD_initDDict_internal(ddict, dict, dictSize, dictLoadMethod, dictContentType); + + if ((ERR_isError(initResult)) != 0) + { + ZSTD_freeDDict(ddict); + return (ZSTD_DDict_s*)null; + } + } + + return ddict; + } + } + + /*! ZSTD_createDDict() : + * Create a digested dictionary, to start decompression without startup delay. + * `dict` content is copied inside DDict. + * Consequently, `dict` can be released after `ZSTD_DDict` creation */ + public static ZSTD_DDict_s* ZSTD_createDDict(void* dict, nuint dictSize) + { + ZSTD_customMem allocator = new ZSTD_customMem + { + customAlloc = null, + customFree = null, + opaque = null, + }; + + return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy, ZSTD_dictContentType_e.ZSTD_dct_auto, allocator); + } + + /*! ZSTD_createDDict_byReference() : + * Create a digested dictionary, to start decompression without startup delay. + * Dictionary content is simply referenced, it will be accessed during decompression. + * Warning : dictBuffer must outlive DDict (DDict must be freed before dictBuffer) */ + public static ZSTD_DDict_s* ZSTD_createDDict_byReference(void* dictBuffer, nuint dictSize) + { + ZSTD_customMem allocator = new ZSTD_customMem + { + customAlloc = null, + customFree = null, + opaque = null, + }; + + return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, ZSTD_dictContentType_e.ZSTD_dct_auto, allocator); + } + + public static ZSTD_DDict_s* ZSTD_initStaticDDict(void* sBuffer, nuint sBufferSize, void* dict, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType) + { + nuint neededSpace = (nuint)(sizeof(ZSTD_DDict_s)) + (dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef ? 0 : dictSize); + ZSTD_DDict_s* ddict = (ZSTD_DDict_s*)(sBuffer); + + assert(sBuffer != null); + assert(dict != null); + if (((nuint)(sBuffer) & 7) != 0) + { + return (ZSTD_DDict_s*)null; + } + + if (sBufferSize < neededSpace) + { + return (ZSTD_DDict_s*)null; + } + + if (dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy) + { + memcpy((void*)((ddict + 1)), (dict), (dictSize)); + dict = ddict + 1; + } + + if ((ERR_isError(ZSTD_initDDict_internal(ddict, dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, dictContentType))) != 0) + { + return (ZSTD_DDict_s*)null; + } + + return ddict; + } + + /*! ZSTD_freeDDict() : + * Function frees memory allocated with ZSTD_createDDict() + * If a NULL pointer is passed, no operation is performed. */ + public static nuint ZSTD_freeDDict(ZSTD_DDict_s* ddict) + { + if (ddict == null) + { + return 0; + } + + + { + ZSTD_customMem cMem = ddict->cMem; + + ZSTD_customFree(ddict->dictBuffer, cMem); + ZSTD_customFree((void*)ddict, cMem); + return 0; + } + } + + /*! ZSTD_estimateDDictSize() : + * Estimate amount of memory that will be needed to create a dictionary for decompression. + * Note : dictionary created by reference using ZSTD_dlm_byRef are smaller */ + public static nuint ZSTD_estimateDDictSize(nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod) + { + return (nuint)(sizeof(ZSTD_DDict_s)) + (dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef ? 0 : dictSize); + } + + public static nuint ZSTD_sizeof_DDict(ZSTD_DDict_s* ddict) + { + if (ddict == null) + { + return 0; + } + + return (nuint)(sizeof(ZSTD_DDict_s)) + (ddict->dictBuffer != null ? ddict->dictSize : 0); + } + + /*! ZSTD_getDictID_fromDDict() : + * Provides the dictID of the dictionary loaded into `ddict`. + * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. + * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ + public static uint ZSTD_getDictID_fromDDict(ZSTD_DDict_s* ddict) + { + if (ddict == null) + { + return 0; + } + + return ZSTD_getDictID_fromDict(ddict->dictContent, ddict->dictSize); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompress.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompress.cs new file mode 100644 index 00000000..ad67ae37 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompress.cs @@ -0,0 +1,3130 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /* Hash function to determine starting position of dict insertion within the table + * Returns an index between [0, hashSet->ddictPtrTableSize] + */ + private static nuint ZSTD_DDictHashSet_getIndex(ZSTD_DDictHashSet* hashSet, uint dictID) + { + ulong hash = XXH64((void*)&dictID, (nuint)(4), 0); + + return (nuint)(hash & (hashSet->ddictPtrTableSize - 1)); + } + + /* Adds DDict to a hashset without resizing it. + * If inserting a DDict with a dictID that already exists in the set, replaces the one in the set. + * Returns 0 if successful, or a zstd error code if something went wrong. + */ + private static nuint ZSTD_DDictHashSet_emplaceDDict(ZSTD_DDictHashSet* hashSet, ZSTD_DDict_s* ddict) + { + uint dictID = ZSTD_getDictID_fromDDict(ddict); + nuint idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID); + nuint idxRangeMask = hashSet->ddictPtrTableSize - 1; + + if (hashSet->ddictPtrCount == hashSet->ddictPtrTableSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + while (hashSet->ddictPtrTable[idx] != null) + { + if (ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]) == dictID) + { + hashSet->ddictPtrTable[idx] = ddict; + return 0; + } + + idx &= idxRangeMask; + idx++; + } + + hashSet->ddictPtrTable[idx] = ddict; + hashSet->ddictPtrCount++; + return 0; + } + + /* Expands hash table by factor of DDICT_HASHSET_RESIZE_FACTOR and + * rehashes all values, allocates new table, frees old table. + * Returns 0 on success, otherwise a zstd error code. + */ + private static nuint ZSTD_DDictHashSet_expand(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) + { + nuint newTableSize = hashSet->ddictPtrTableSize * 2; + ZSTD_DDict_s** newTable = (ZSTD_DDict_s**)(ZSTD_customCalloc((nuint)(sizeof(ZSTD_DDict_s*)) * newTableSize, customMem)); + ZSTD_DDict_s** oldTable = hashSet->ddictPtrTable; + nuint oldTableSize = hashSet->ddictPtrTableSize; + nuint i; + + if (newTable == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + hashSet->ddictPtrTable = newTable; + hashSet->ddictPtrTableSize = newTableSize; + hashSet->ddictPtrCount = 0; + for (i = 0; i < oldTableSize; ++i) + { + if (oldTable[i] != null) + { + + { + nuint err_code = (ZSTD_DDictHashSet_emplaceDDict(hashSet, oldTable[i])); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + } + + ZSTD_customFree((void*)(oldTable), customMem); + return 0; + } + + /* Fetches a DDict with the given dictID + * Returns the ZSTD_DDict* with the requested dictID. If it doesn't exist, then returns NULL. + */ + private static ZSTD_DDict_s* ZSTD_DDictHashSet_getDDict(ZSTD_DDictHashSet* hashSet, uint dictID) + { + nuint idx = ZSTD_DDictHashSet_getIndex(hashSet, dictID); + nuint idxRangeMask = hashSet->ddictPtrTableSize - 1; + + for (;;) + { + nuint currDictID = ZSTD_getDictID_fromDDict(hashSet->ddictPtrTable[idx]); + + if (currDictID == dictID || currDictID == 0) + { + break; + } + else + { + idx &= idxRangeMask; + idx++; + } + } + + return hashSet->ddictPtrTable[idx]; + } + + /* Allocates space for and returns a ddict hash set + * The hash set's ZSTD_DDict* table has all values automatically set to NULL to begin with. + * Returns NULL if allocation failed. + */ + private static ZSTD_DDictHashSet* ZSTD_createDDictHashSet(ZSTD_customMem customMem) + { + ZSTD_DDictHashSet* ret = (ZSTD_DDictHashSet*)(ZSTD_customMalloc((nuint)(sizeof(ZSTD_DDictHashSet)), customMem)); + + ret->ddictPtrTable = (ZSTD_DDict_s**)(ZSTD_customCalloc(64 * (nuint)(sizeof(ZSTD_DDict_s*)), customMem)); + ret->ddictPtrTableSize = 64; + ret->ddictPtrCount = 0; + if (ret == null || ret->ddictPtrTable == null) + { + return (ZSTD_DDictHashSet*)null; + } + + return ret; + } + + /* Frees the table of ZSTD_DDict* within a hashset, then frees the hashset itself. + * Note: The ZSTD_DDict* within the table are NOT freed. + */ + private static void ZSTD_freeDDictHashSet(ZSTD_DDictHashSet* hashSet, ZSTD_customMem customMem) + { + if (hashSet != null && hashSet->ddictPtrTable != null) + { + ZSTD_customFree((void*)(hashSet->ddictPtrTable), customMem); + } + + if (hashSet != null) + { + ZSTD_customFree((void*)hashSet, customMem); + } + } + + /* Public function: Adds a DDict into the ZSTD_DDictHashSet, possibly triggering a resize of the hash set. + * Returns 0 on success, or a ZSTD error. + */ + private static nuint ZSTD_DDictHashSet_addDDict(ZSTD_DDictHashSet* hashSet, ZSTD_DDict_s* ddict, ZSTD_customMem customMem) + { + if (hashSet->ddictPtrCount * 4 / hashSet->ddictPtrTableSize * 3 != 0) + { + + { + nuint err_code = (ZSTD_DDictHashSet_expand(hashSet, customMem)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + + + { + nuint err_code = (ZSTD_DDictHashSet_emplaceDDict(hashSet, ddict)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return 0; + } + + /*-************************************************************* + * Context management + ***************************************************************/ + public static nuint ZSTD_sizeof_DCtx(ZSTD_DCtx_s* dctx) + { + if (dctx == null) + { + return 0; + } + + return (nuint)(sizeof(ZSTD_DCtx_s)) + ZSTD_sizeof_DDict(dctx->ddictLocal) + dctx->inBuffSize + dctx->outBuffSize; + } + + public static nuint ZSTD_estimateDCtxSize() + { + return (nuint)(sizeof(ZSTD_DCtx_s)); + } + + private static nuint ZSTD_startingInputLength(ZSTD_format_e format) + { + nuint startingInputLength = (nuint)(((format) == ZSTD_format_e.ZSTD_f_zstd1 ? 5 : 1)); + + assert((format == ZSTD_format_e.ZSTD_f_zstd1) || (format == ZSTD_format_e.ZSTD_f_zstd1_magicless)); + return startingInputLength; + } + + private static void ZSTD_DCtx_resetParameters(ZSTD_DCtx_s* dctx) + { + assert(dctx->streamStage == ZSTD_dStreamStage.zdss_init); + dctx->format = ZSTD_format_e.ZSTD_f_zstd1; + dctx->maxWindowSize = (((uint)(1) << 27) + 1); + dctx->outBufferMode = ZSTD_bufferMode_e.ZSTD_bm_buffered; + dctx->forceIgnoreChecksum = ZSTD_forceIgnoreChecksum_e.ZSTD_d_validateChecksum; + dctx->refMultipleDDicts = ZSTD_refMultipleDDicts_e.ZSTD_rmd_refSingleDDict; + } + + private static void ZSTD_initDCtx_internal(ZSTD_DCtx_s* dctx) + { + dctx->staticSize = 0; + dctx->ddict = null; + dctx->ddictLocal = null; + dctx->dictEnd = null; + dctx->ddictIsCold = 0; + dctx->dictUses = ZSTD_dictUses_e.ZSTD_dont_use; + dctx->inBuff = null; + dctx->inBuffSize = 0; + dctx->outBuffSize = 0; + dctx->streamStage = ZSTD_dStreamStage.zdss_init; + dctx->legacyContext = null; + dctx->previousLegacyVersion = 0; + dctx->noForwardProgress = 0; + dctx->oversizedDuration = 0; + dctx->bmi2 = ((IsBmi2Supported) ? 1 : 0); + dctx->ddictSet = null; + ZSTD_DCtx_resetParameters(dctx); + } + + public static ZSTD_DCtx_s* ZSTD_initStaticDCtx(void* workspace, nuint workspaceSize) + { + ZSTD_DCtx_s* dctx = (ZSTD_DCtx_s*)(workspace); + + if (((nuint)(workspace) & 7) != 0) + { + return (ZSTD_DCtx_s*)null; + } + + if (workspaceSize < (nuint)(sizeof(ZSTD_DCtx_s))) + { + return (ZSTD_DCtx_s*)null; + } + + ZSTD_initDCtx_internal(dctx); + dctx->staticSize = workspaceSize; + dctx->inBuff = (sbyte*)(dctx + 1); + return dctx; + } + + public static ZSTD_DCtx_s* ZSTD_createDCtx_advanced(ZSTD_customMem customMem) + { + if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + { + return (ZSTD_DCtx_s*)null; + } + + + { + ZSTD_DCtx_s* dctx = (ZSTD_DCtx_s*)(ZSTD_customMalloc((nuint)(sizeof(ZSTD_DCtx_s)), customMem)); + + if (dctx == null) + { + return (ZSTD_DCtx_s*)null; + } + + dctx->customMem = customMem; + ZSTD_initDCtx_internal(dctx); + return dctx; + } + } + + public static ZSTD_DCtx_s* ZSTD_createDCtx() + { + return ZSTD_createDCtx_advanced(ZSTD_defaultCMem); + } + + private static void ZSTD_clearDict(ZSTD_DCtx_s* dctx) + { + ZSTD_freeDDict(dctx->ddictLocal); + dctx->ddictLocal = null; + dctx->ddict = null; + dctx->dictUses = ZSTD_dictUses_e.ZSTD_dont_use; + } + + public static nuint ZSTD_freeDCtx(ZSTD_DCtx_s* dctx) + { + if (dctx == null) + { + return 0; + } + + if (dctx->staticSize != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + + { + ZSTD_customMem cMem = dctx->customMem; + + ZSTD_clearDict(dctx); + ZSTD_customFree((void*)dctx->inBuff, cMem); + dctx->inBuff = null; + if (dctx->ddictSet != null) + { + ZSTD_freeDDictHashSet(dctx->ddictSet, cMem); + dctx->ddictSet = null; + } + + ZSTD_customFree((void*)dctx, cMem); + return 0; + } + } + + /* no longer useful */ + public static void ZSTD_copyDCtx(ZSTD_DCtx_s* dstDCtx, ZSTD_DCtx_s* srcDCtx) + { + nuint toCopy = (nuint)((sbyte*)(&dstDCtx->inBuff) - (sbyte*)(dstDCtx)); + + memcpy((void*)(dstDCtx), (void*)(srcDCtx), (toCopy)); + } + + /* Given a dctx with a digested frame params, re-selects the correct ZSTD_DDict based on + * the requested dict ID from the frame. If there exists a reference to the correct ZSTD_DDict, then + * accordingly sets the ddict to be used to decompress the frame. + * + * If no DDict is found, then no action is taken, and the ZSTD_DCtx::ddict remains as-is. + * + * ZSTD_d_refMultipleDDicts must be enabled for this function to be called. + */ + private static void ZSTD_DCtx_selectFrameDDict(ZSTD_DCtx_s* dctx) + { + assert(dctx->refMultipleDDicts != default && dctx->ddictSet != null); + if (dctx->ddict != null) + { + ZSTD_DDict_s* frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dctx->fParams.dictID); + + if (frameDDict != null) + { + ZSTD_clearDict(dctx); + dctx->dictID = dctx->fParams.dictID; + dctx->ddict = frameDDict; + dctx->dictUses = ZSTD_dictUses_e.ZSTD_use_indefinitely; + } + } + } + + /*! ZSTD_isFrame() : + * Tells if the content of `buffer` starts with a valid Frame Identifier. + * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. + * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. + * Note 3 : Skippable Frame Identifiers are considered valid. */ + public static uint ZSTD_isFrame(void* buffer, nuint size) + { + if (size < 4) + { + return 0; + } + + + { + uint magic = MEM_readLE32(buffer); + + if (magic == 0xFD2FB528) + { + return 1; + } + + if ((magic & 0xFFFFFFF0) == 0x184D2A50) + { + return 1; + } + } + + return 0; + } + + /** ZSTD_frameHeaderSize_internal() : + * srcSize must be large enough to reach header size fields. + * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless. + * @return : size of the Frame Header + * or an error code, which can be tested with ZSTD_isError() */ + private static nuint ZSTD_frameHeaderSize_internal(void* src, nuint srcSize, ZSTD_format_e format) + { + nuint minInputSize = ZSTD_startingInputLength(format); + + if (srcSize < minInputSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + + { + byte fhd = ((byte*)(src))[minInputSize - 1]; + uint dictID = (uint)(fhd & 3); + uint singleSegment = (uint)((fhd >> 5) & 1); + uint fcsId = (uint)(fhd >> 6); + + return minInputSize + (uint)(singleSegment == 0 ? 1 : 0) + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId] + (uint)((((singleSegment != 0 && fcsId == 0)) ? 1 : 0)); + } + } + + /** ZSTD_frameHeaderSize() : + * srcSize must be >= ZSTD_frameHeaderSize_prefix. + * @return : size of the Frame Header, + * or an error code (if srcSize is too small) */ + public static nuint ZSTD_frameHeaderSize(void* src, nuint srcSize) + { + return ZSTD_frameHeaderSize_internal(src, srcSize, ZSTD_format_e.ZSTD_f_zstd1); + } + + /** ZSTD_getFrameHeader_advanced() : + * decode Frame Header, or require larger `srcSize`. + * note : only works for formats ZSTD_f_zstd1 and ZSTD_f_zstd1_magicless + * @return : 0, `zfhPtr` is correctly filled, + * >0, `srcSize` is too small, value is wanted `srcSize` amount, + * or an error code, which can be tested using ZSTD_isError() */ + public static nuint ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, void* src, nuint srcSize, ZSTD_format_e format) + { + byte* ip = (byte*)(src); + nuint minInputSize = ZSTD_startingInputLength(format); + + memset((void*)(zfhPtr), (0), ((nuint)(sizeof(ZSTD_frameHeader)))); + if (srcSize < minInputSize) + { + return minInputSize; + } + + if (src == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + if ((format != ZSTD_format_e.ZSTD_f_zstd1_magicless) && (MEM_readLE32(src) != 0xFD2FB528)) + { + if ((MEM_readLE32(src) & 0xFFFFFFF0) == 0x184D2A50) + { + if (srcSize < 8) + { + return 8; + } + + memset((void*)(zfhPtr), (0), ((nuint)(sizeof(ZSTD_frameHeader)))); + zfhPtr->frameContentSize = MEM_readLE32((void*)((sbyte*)(src) + 4)); + zfhPtr->frameType = ZSTD_frameType_e.ZSTD_skippableFrame; + return 0; + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_prefix_unknown))); + } + + } + + + { + nuint fhsize = ZSTD_frameHeaderSize_internal(src, srcSize, format); + + if (srcSize < fhsize) + { + return fhsize; + } + + zfhPtr->headerSize = (uint)(fhsize); + } + + + { + byte fhdByte = ip[minInputSize - 1]; + nuint pos = minInputSize; + uint dictIDSizeCode = (uint)(fhdByte & 3); + uint checksumFlag = (uint)((fhdByte >> 2) & 1); + uint singleSegment = (uint)((fhdByte >> 5) & 1); + uint fcsID = (uint)(fhdByte >> 6); + ulong windowSize = 0; + uint dictID = 0; + ulong frameContentSize = (unchecked(0UL - 1)); + + if ((fhdByte & 0x08) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported))); + } + + if (singleSegment == 0) + { + byte wlByte = ip[pos++]; + uint windowLog = (uint)((wlByte >> 3) + 10); + + if (windowLog > (uint)(((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge))); + } + + windowSize = (1UL << (int)windowLog); + windowSize += (windowSize >> 3) * (uint)((wlByte & 7)); + } + + switch (dictIDSizeCode) + { + default: + { + assert(0 != 0); + } + + + goto case 0; + case 0: + { + break; + } + + case 1: + { + dictID = ip[pos]; + } + + pos++; + break; + case 2: + { + dictID = MEM_readLE16((void*)(ip + pos)); + } + + pos += 2; + break; + case 3: + { + dictID = MEM_readLE32((void*)(ip + pos)); + } + + pos += 4; + break; + } + + switch (fcsID) + { + default: + { + assert(0 != 0); + } + + + goto case 0; + case 0: + { + if (singleSegment != 0) + { + frameContentSize = ip[pos]; + } + } + + break; + case 1: + { + frameContentSize = (ulong)(MEM_readLE16((void*)(ip + pos)) + 256); + } + + break; + case 2: + { + frameContentSize = MEM_readLE32((void*)(ip + pos)); + } + + break; + case 3: + { + frameContentSize = MEM_readLE64((void*)(ip + pos)); + } + + break; + } + + if (singleSegment != 0) + { + windowSize = unchecked(frameContentSize); + } + + zfhPtr->frameType = ZSTD_frameType_e.ZSTD_frame; + zfhPtr->frameContentSize = unchecked(frameContentSize); + zfhPtr->windowSize = windowSize; + zfhPtr->blockSizeMax = (uint)((windowSize) < (uint)(((1 << 17))) ? (windowSize) : ((1 << 17))); + zfhPtr->dictID = dictID; + zfhPtr->checksumFlag = checksumFlag; + } + + return 0; + } + + /** ZSTD_getFrameHeader() : + * decode Frame Header, or require larger `srcSize`. + * note : this function does not consume input, it only reads it. + * @return : 0, `zfhPtr` is correctly filled, + * >0, `srcSize` is too small, value is wanted `srcSize` amount, + * or an error code, which can be tested using ZSTD_isError() */ + public static nuint ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, void* src, nuint srcSize) + { + return ZSTD_getFrameHeader_advanced(zfhPtr, src, srcSize, ZSTD_format_e.ZSTD_f_zstd1); + } + + /** ZSTD_getFrameContentSize() : + * compatible with legacy mode + * @return : decompressed size of the single frame pointed to be `src` if known, otherwise + * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined + * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */ + public static ulong ZSTD_getFrameContentSize(void* src, nuint srcSize) + { + + { + ZSTD_frameHeader zfh; + + if (ZSTD_getFrameHeader(&zfh, src, srcSize) != 0) + { + return (unchecked(0UL - 2)); + } + + if (zfh.frameType == ZSTD_frameType_e.ZSTD_skippableFrame) + { + return 0; + } + else + { + return zfh.frameContentSize; + } + } + } + + private static nuint readSkippableFrameSize(void* src, nuint srcSize) + { + nuint skippableHeaderSize = 8; + uint sizeU32; + + if (srcSize < 8) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + sizeU32 = MEM_readLE32((void*)((byte*)(src) + 4)); + if ((uint)(sizeU32 + 8) < sizeU32) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported))); + } + + + { + nuint skippableSize = skippableHeaderSize + sizeU32; + + if (skippableSize > srcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + return skippableSize; + } + } + + /** ZSTD_findDecompressedSize() : + * compatible with legacy mode + * `srcSize` must be the exact length of some number of ZSTD compressed and/or + * skippable frames + * @return : decompressed size of the frames contained */ + public static ulong ZSTD_findDecompressedSize(void* src, nuint srcSize) + { + ulong totalDstSize = 0; + + while (srcSize >= ZSTD_startingInputLength(ZSTD_format_e.ZSTD_f_zstd1)) + { + uint magicNumber = MEM_readLE32(src); + + if ((magicNumber & 0xFFFFFFF0) == 0x184D2A50) + { + nuint skippableSize = readSkippableFrameSize(src, srcSize); + + if ((ERR_isError(skippableSize)) != 0) + { + return (unchecked(0UL - 2)); + } + + assert(skippableSize <= srcSize); + src = (byte*)(src) + skippableSize; + srcSize -= skippableSize; + continue; + } + + + { + ulong ret = ZSTD_getFrameContentSize(src, srcSize); + + if (ret >= (unchecked(0UL - 2))) + { + return ret; + } + + if (totalDstSize + ret < totalDstSize) + { + return (unchecked(0UL - 2)); + } + + totalDstSize += ret; + } + + + { + nuint frameSrcSize = ZSTD_findFrameCompressedSize(src, srcSize); + + if ((ERR_isError(frameSrcSize)) != 0) + { + return (unchecked(0UL - 2)); + } + + src = (byte*)(src) + frameSrcSize; + srcSize -= frameSrcSize; + } + } + + if (srcSize != 0) + { + return (unchecked(0UL - 2)); + } + + return totalDstSize; + } + + /** ZSTD_getDecompressedSize() : + * compatible with legacy mode + * @return : decompressed size if known, 0 otherwise + note : 0 can mean any of the following : + - frame content is empty + - decompressed size field is not present in frame header + - frame header unknown / not supported + - frame header not complete (`srcSize` too small) */ + public static ulong ZSTD_getDecompressedSize(void* src, nuint srcSize) + { + ulong ret = ZSTD_getFrameContentSize(src, srcSize); + + return (ret >= (unchecked(0UL - 2))) ? 0 : ret; + } + + /** ZSTD_decodeFrameHeader() : + * `headerSize` must be the size provided by ZSTD_frameHeaderSize(). + * If multiple DDict references are enabled, also will choose the correct DDict to use. + * @return : 0 if success, or an error code, which can be tested using ZSTD_isError() */ + private static nuint ZSTD_decodeFrameHeader(ZSTD_DCtx_s* dctx, void* src, nuint headerSize) + { + nuint result = ZSTD_getFrameHeader_advanced(&(dctx->fParams), src, headerSize, dctx->format); + + if ((ERR_isError(result)) != 0) + { + return result; + } + + if (result > 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (dctx->refMultipleDDicts == ZSTD_refMultipleDDicts_e.ZSTD_rmd_refMultipleDDicts && dctx->ddictSet != null) + { + ZSTD_DCtx_selectFrameDDict(dctx); + } + + if (dctx->fParams.dictID != 0 && (dctx->dictID != dctx->fParams.dictID)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_wrong))); + } + + dctx->validateChecksum = (uint)((dctx->fParams.checksumFlag != 0 && dctx->forceIgnoreChecksum == default) ? 1 : 0); + if (dctx->validateChecksum != 0) + { + XXH64_reset(&dctx->xxhState, 0); + } + + dctx->processedCSize += (ulong)headerSize; + return 0; + } + + private static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(nuint ret) + { + ZSTD_frameSizeInfo frameSizeInfo; + + frameSizeInfo.compressedSize = ret; + frameSizeInfo.decompressedBound = (unchecked(0UL - 2)); + return frameSizeInfo; + } + + private static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(void* src, nuint srcSize) + { + ZSTD_frameSizeInfo frameSizeInfo; + + memset((void*)(&frameSizeInfo), (0), ((nuint)(sizeof(ZSTD_frameSizeInfo)))); + if ((srcSize >= 8) && (MEM_readLE32(src) & 0xFFFFFFF0) == 0x184D2A50) + { + frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize); + assert((ERR_isError(frameSizeInfo.compressedSize)) != 0 || frameSizeInfo.compressedSize <= srcSize); + return frameSizeInfo; + } + else + { + byte* ip = (byte*)(src); + byte* ipstart = ip; + nuint remainingSize = srcSize; + nuint nbBlocks = 0; + ZSTD_frameHeader zfh; + + + { + nuint ret = ZSTD_getFrameHeader(&zfh, src, srcSize); + + if ((ERR_isError(ret)) != 0) + { + return ZSTD_errorFrameSizeInfo(ret); + } + + if (ret > 0) + { + return ZSTD_errorFrameSizeInfo((unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)))); + } + } + + ip += zfh.headerSize; + remainingSize -= zfh.headerSize; + while (1 != 0) + { + blockProperties_t blockProperties; + nuint cBlockSize = ZSTD_getcBlockSize((void*)ip, remainingSize, &blockProperties); + + if ((ERR_isError(cBlockSize)) != 0) + { + return ZSTD_errorFrameSizeInfo(cBlockSize); + } + + if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) + { + return ZSTD_errorFrameSizeInfo((unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)))); + } + + ip += ZSTD_blockHeaderSize + cBlockSize; + remainingSize -= ZSTD_blockHeaderSize + cBlockSize; + nbBlocks++; + if (blockProperties.lastBlock != 0) + { + break; + } + } + + if (zfh.checksumFlag != 0) + { + if (remainingSize < 4) + { + return ZSTD_errorFrameSizeInfo((unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)))); + } + + ip += 4; + } + + frameSizeInfo.compressedSize = (nuint)(ip - ipstart); + frameSizeInfo.decompressedBound = (zfh.frameContentSize != (unchecked(0UL - 1))) ? zfh.frameContentSize : nbBlocks * zfh.blockSizeMax; + return frameSizeInfo; + } + } + + /** ZSTD_findFrameCompressedSize() : + * compatible with legacy mode + * `src` must point to the start of a ZSTD frame, ZSTD legacy frame, or skippable frame + * `srcSize` must be at least as large as the frame contained + * @return : the compressed size of the frame starting at `src` */ + public static nuint ZSTD_findFrameCompressedSize(void* src, nuint srcSize) + { + ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize); + + return frameSizeInfo.compressedSize; + } + + /** ZSTD_decompressBound() : + * compatible with legacy mode + * `src` must point to the start of a ZSTD frame or a skippeable frame + * `srcSize` must be at least as large as the frame contained + * @return : the maximum decompressed size of the compressed source + */ + public static ulong ZSTD_decompressBound(void* src, nuint srcSize) + { + ulong bound = 0; + + while (srcSize > 0) + { + ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize); + nuint compressedSize = frameSizeInfo.compressedSize; + ulong decompressedBound = frameSizeInfo.decompressedBound; + + if ((ERR_isError(compressedSize)) != 0 || decompressedBound == (unchecked(0UL - 2))) + { + return (unchecked(0UL - 2)); + } + + assert(srcSize >= compressedSize); + src = (byte*)(src) + compressedSize; + srcSize -= compressedSize; + bound += decompressedBound; + } + + return bound; + } + + /** ZSTD_insertBlock() : + * insert `src` block into `dctx` history. Useful to track uncompressed blocks. */ + public static nuint ZSTD_insertBlock(ZSTD_DCtx_s* dctx, void* blockStart, nuint blockSize) + { + ZSTD_checkContinuity(dctx, blockStart, blockSize); + dctx->previousDstEnd = (sbyte*)(blockStart) + blockSize; + return blockSize; + } + + private static nuint ZSTD_copyRawBlock(void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + if (srcSize > dstCapacity) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (dst == null) + { + if (srcSize == 0) + { + return 0; + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstBuffer_null))); + } + + } + + memcpy((dst), (src), (srcSize)); + return srcSize; + } + + private static nuint ZSTD_setRleBlock(void* dst, nuint dstCapacity, byte b, nuint regenSize) + { + if (regenSize > dstCapacity) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (dst == null) + { + if (regenSize == 0) + { + return 0; + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstBuffer_null))); + } + + } + + memset((dst), (int)(b), (regenSize)); + return regenSize; + } + + private static void ZSTD_DCtx_trace_end(ZSTD_DCtx_s* dctx, ulong uncompressedSize, ulong compressedSize, uint streaming) + { + } + + /*! ZSTD_decompressFrame() : + * @dctx must be properly initialized + * will update *srcPtr and *srcSizePtr, + * to make *srcPtr progress by one frame. */ + private static nuint ZSTD_decompressFrame(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void** srcPtr, nuint* srcSizePtr) + { + byte* istart = (byte*)(*srcPtr); + byte* ip = istart; + byte* ostart = (byte*)(dst); + byte* oend = dstCapacity != 0 ? ostart + dstCapacity : ostart; + byte* op = ostart; + nuint remainingSrcSize = *srcSizePtr; + + if (remainingSrcSize < (uint)(((dctx->format) == ZSTD_format_e.ZSTD_f_zstd1 ? 6 : 2)) + ZSTD_blockHeaderSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + + { + nuint frameHeaderSize = ZSTD_frameHeaderSize_internal((void*)ip, (nuint)(((dctx->format) == ZSTD_format_e.ZSTD_f_zstd1 ? 5 : 1)), dctx->format); + + if ((ERR_isError(frameHeaderSize)) != 0) + { + return frameHeaderSize; + } + + if (remainingSrcSize < frameHeaderSize + ZSTD_blockHeaderSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + + { + nuint err_code = (ZSTD_decodeFrameHeader(dctx, (void*)ip, frameHeaderSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ip += frameHeaderSize; + remainingSrcSize -= frameHeaderSize; + } + + while (1 != 0) + { + nuint decodedSize; + blockProperties_t blockProperties; + nuint cBlockSize = ZSTD_getcBlockSize((void*)ip, remainingSrcSize, &blockProperties); + + if ((ERR_isError(cBlockSize)) != 0) + { + return cBlockSize; + } + + ip += ZSTD_blockHeaderSize; + remainingSrcSize -= ZSTD_blockHeaderSize; + if (cBlockSize > remainingSrcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + switch (blockProperties.blockType) + { + case blockType_e.bt_compressed: + { + decodedSize = ZSTD_decompressBlock_internal(dctx, (void*)op, (nuint)(oend - op), (void*)ip, cBlockSize, 1); + } + + break; + case blockType_e.bt_raw: + { + decodedSize = ZSTD_copyRawBlock((void*)op, (nuint)(oend - op), (void*)ip, cBlockSize); + } + + break; + case blockType_e.bt_rle: + { + decodedSize = ZSTD_setRleBlock((void*)op, (nuint)(oend - op), *ip, blockProperties.origSize); + } + + break; + case blockType_e.bt_reserved: + default: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + } + + } + + if ((ERR_isError(decodedSize)) != 0) + { + return decodedSize; + } + + if (dctx->validateChecksum != 0) + { + XXH64_update(&dctx->xxhState, (void*)op, decodedSize); + } + + if (decodedSize != 0) + { + op += decodedSize; + } + + assert(ip != null); + ip += cBlockSize; + remainingSrcSize -= cBlockSize; + if (blockProperties.lastBlock != 0) + { + break; + } + } + + if (dctx->fParams.frameContentSize != (unchecked(0UL - 1))) + { + if ((ulong)(op - ostart) != dctx->fParams.frameContentSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + } + + if (dctx->fParams.checksumFlag != 0) + { + if (remainingSrcSize < 4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_checksum_wrong))); + } + + if (dctx->forceIgnoreChecksum == default) + { + uint checkCalc = (uint)(XXH64_digest(&dctx->xxhState)); + uint checkRead; + + checkRead = MEM_readLE32((void*)ip); + if (checkRead != checkCalc) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_checksum_wrong))); + } + + } + + ip += 4; + remainingSrcSize -= 4; + } + + ZSTD_DCtx_trace_end(dctx, (ulong)(op - ostart), (ulong)(ip - istart), 0); + *srcPtr = ip; + *srcSizePtr = remainingSrcSize; + return (nuint)(op - ostart); + } + + private static nuint ZSTD_decompressMultiFrame(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, void* dict, nuint dictSize, ZSTD_DDict_s* ddict) + { + void* dststart = dst; + int moreThan1Frame = 0; + + assert(dict == null || ddict == null); + if (ddict != null) + { + dict = ZSTD_DDict_dictContent(ddict); + dictSize = ZSTD_DDict_dictSize(ddict); + } + + while (srcSize >= ZSTD_startingInputLength(dctx->format)) + { + + { + uint magicNumber = MEM_readLE32(src); + + if ((magicNumber & 0xFFFFFFF0) == 0x184D2A50) + { + nuint skippableSize = readSkippableFrameSize(src, srcSize); + + + { + nuint err_code = (skippableSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(skippableSize <= srcSize); + src = (byte*)(src) + skippableSize; + srcSize -= skippableSize; + continue; + } + } + + if (ddict != null) + { + + { + nuint err_code = (ZSTD_decompressBegin_usingDDict(dctx, ddict)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + else + { + + { + nuint err_code = (ZSTD_decompressBegin_usingDict(dctx, dict, dictSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + + ZSTD_checkContinuity(dctx, dst, dstCapacity); + + { + nuint res = ZSTD_decompressFrame(dctx, dst, dstCapacity, &src, &srcSize); + + if ((ZSTD_getErrorCode(res) == ZSTD_ErrorCode.ZSTD_error_prefix_unknown) && (moreThan1Frame == 1)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if ((ERR_isError(res)) != 0) + { + return res; + } + + assert(res <= dstCapacity); + if (res != 0) + { + dst = (byte*)(dst) + res; + } + + dstCapacity -= res; + } + + moreThan1Frame = 1; + } + + if (srcSize != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + return (nuint)((byte*)(dst) - (byte*)(dststart)); + } + + /*! ZSTD_decompress_usingDict() : + * Decompression using a known Dictionary. + * Dictionary must be identical to the one used during compression. + * Note : This function loads the dictionary, resulting in significant startup delay. + * It's intended for a dictionary used only once. + * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ + public static nuint ZSTD_decompress_usingDict(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, void* dict, nuint dictSize) + { + return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize, (ZSTD_DDict_s*)null); + } + + private static ZSTD_DDict_s* ZSTD_getDDict(ZSTD_DCtx_s* dctx) + { + switch (dctx->dictUses) + { + default: + { + assert(0 != 0); + } + + + goto case ZSTD_dictUses_e.ZSTD_dont_use; + case ZSTD_dictUses_e.ZSTD_dont_use: + { + ZSTD_clearDict(dctx); + } + + return (ZSTD_DDict_s*)null; + case ZSTD_dictUses_e.ZSTD_use_indefinitely: + { + return dctx->ddict; + } + + case ZSTD_dictUses_e.ZSTD_use_once: + { + dctx->dictUses = ZSTD_dictUses_e.ZSTD_dont_use; + } + + return dctx->ddict; + } + } + + /*! ZSTD_decompressDCtx() : + * Same as ZSTD_decompress(), + * requires an allocated ZSTD_DCtx. + * Compatible with sticky parameters. + */ + public static nuint ZSTD_decompressDCtx(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + return ZSTD_decompress_usingDDict(dctx, dst, dstCapacity, src, srcSize, ZSTD_getDDict(dctx)); + } + + /*! ZSTD_decompress() : + * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. + * `dstCapacity` is an upper bound of originalSize to regenerate. + * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. + * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), + * or an errorCode if it fails (which can be tested using ZSTD_isError()). */ + public static nuint ZSTD_decompress(void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + nuint regenSize; + ZSTD_DCtx_s* dctx = ZSTD_createDCtx(); + + if (dctx == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + regenSize = ZSTD_decompressDCtx(dctx, dst, dstCapacity, src, srcSize); + ZSTD_freeDCtx(dctx); + return regenSize; + } + + /*-************************************** + * Advanced Streaming Decompression API + * Bufferless and synchronous + ****************************************/ + public static nuint ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx_s* dctx) + { + return dctx->expected; + } + + /** + * Similar to ZSTD_nextSrcSizeToDecompress(), but when when a block input can be streamed, + * we allow taking a partial block as the input. Currently only raw uncompressed blocks can + * be streamed. + * + * For blocks that can be streamed, this allows us to reduce the latency until we produce + * output, and avoid copying the input. + * + * @param inputSize - The total amount of input that the caller currently has. + */ + private static nuint ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx_s* dctx, nuint inputSize) + { + if (!(dctx->stage == ZSTD_dStage.ZSTDds_decompressBlock || dctx->stage == ZSTD_dStage.ZSTDds_decompressLastBlock)) + { + return dctx->expected; + } + + if (dctx->bType != blockType_e.bt_raw) + { + return dctx->expected; + } + + return ((((inputSize) > (1) ? (inputSize) : (1))) < (dctx->expected) ? (((inputSize) > (1) ? (inputSize) : (1))) : (dctx->expected)); + } + + public static ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx_s* dctx) + { + switch (dctx->stage) + { + default: + { + assert(0 != 0); + } + + + goto case ZSTD_dStage.ZSTDds_getFrameHeaderSize; + case ZSTD_dStage.ZSTDds_getFrameHeaderSize: + case ZSTD_dStage.ZSTDds_decodeFrameHeader: + { + return ZSTD_nextInputType_e.ZSTDnit_frameHeader; + } + + case ZSTD_dStage.ZSTDds_decodeBlockHeader: + { + return ZSTD_nextInputType_e.ZSTDnit_blockHeader; + } + + case ZSTD_dStage.ZSTDds_decompressBlock: + { + return ZSTD_nextInputType_e.ZSTDnit_block; + } + + case ZSTD_dStage.ZSTDds_decompressLastBlock: + { + return ZSTD_nextInputType_e.ZSTDnit_lastBlock; + } + + case ZSTD_dStage.ZSTDds_checkChecksum: + { + return ZSTD_nextInputType_e.ZSTDnit_checksum; + } + + case ZSTD_dStage.ZSTDds_decodeSkippableHeader: + case ZSTD_dStage.ZSTDds_skipFrame: + { + return ZSTD_nextInputType_e.ZSTDnit_skippableFrame; + } + } + } + + private static int ZSTD_isSkipFrame(ZSTD_DCtx_s* dctx) + { + return ((dctx->stage == ZSTD_dStage.ZSTDds_skipFrame) ? 1 : 0); + } + + /** ZSTD_decompressContinue() : + * srcSize : must be the exact nb of bytes expected (see ZSTD_nextSrcSizeToDecompress()) + * @return : nb of bytes generated into `dst` (necessarily <= `dstCapacity) + * or an error code, which can be tested using ZSTD_isError() */ + public static nuint ZSTD_decompressContinue(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + if (srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + ZSTD_checkContinuity(dctx, dst, dstCapacity); + dctx->processedCSize += (ulong)srcSize; + switch (dctx->stage) + { + case ZSTD_dStage.ZSTDds_getFrameHeaderSize: + { + assert(src != null); + } + + if (dctx->format == ZSTD_format_e.ZSTD_f_zstd1) + { + assert(srcSize >= 4); + if ((MEM_readLE32(src) & 0xFFFFFFF0) == 0x184D2A50) + { + memcpy((void*)(dctx->headerBuffer), (src), (srcSize)); + dctx->expected = 8 - srcSize; + dctx->stage = ZSTD_dStage.ZSTDds_decodeSkippableHeader; + return 0; + } + } + + dctx->headerSize = ZSTD_frameHeaderSize_internal(src, srcSize, dctx->format); + if ((ERR_isError(dctx->headerSize)) != 0) + { + return dctx->headerSize; + } + + memcpy((void*)(dctx->headerBuffer), (src), (srcSize)); + dctx->expected = dctx->headerSize - srcSize; + dctx->stage = ZSTD_dStage.ZSTDds_decodeFrameHeader; + return 0; + case ZSTD_dStage.ZSTDds_decodeFrameHeader: + { + assert(src != null); + } + + memcpy((void*)((dctx->headerBuffer + (dctx->headerSize - srcSize))), (src), (srcSize)); + + { + nuint err_code = (ZSTD_decodeFrameHeader(dctx, (void*)dctx->headerBuffer, dctx->headerSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + dctx->expected = ZSTD_blockHeaderSize; + dctx->stage = ZSTD_dStage.ZSTDds_decodeBlockHeader; + return 0; + case ZSTD_dStage.ZSTDds_decodeBlockHeader: + { + blockProperties_t bp; + nuint cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); + + if ((ERR_isError(cBlockSize)) != 0) + { + return cBlockSize; + } + + if (cBlockSize > dctx->fParams.blockSizeMax) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + dctx->expected = cBlockSize; + dctx->bType = bp.blockType; + dctx->rleSize = bp.origSize; + if (cBlockSize != 0) + { + dctx->stage = bp.lastBlock != 0 ? ZSTD_dStage.ZSTDds_decompressLastBlock : ZSTD_dStage.ZSTDds_decompressBlock; + return 0; + } + + if (bp.lastBlock != 0) + { + if (dctx->fParams.checksumFlag != 0) + { + dctx->expected = 4; + dctx->stage = ZSTD_dStage.ZSTDds_checkChecksum; + } + else + { + dctx->expected = 0; + dctx->stage = ZSTD_dStage.ZSTDds_getFrameHeaderSize; + } + } + else + { + dctx->expected = ZSTD_blockHeaderSize; + dctx->stage = ZSTD_dStage.ZSTDds_decodeBlockHeader; + } + + return 0; + } + + case ZSTD_dStage.ZSTDds_decompressLastBlock: + case ZSTD_dStage.ZSTDds_decompressBlock: + ; + + { + nuint rSize; + + switch (dctx->bType) + { + case blockType_e.bt_compressed: + ; + rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, 1); + dctx->expected = 0; + break; + case blockType_e.bt_raw: + { + assert(srcSize <= dctx->expected); + } + + rSize = ZSTD_copyRawBlock(dst, dstCapacity, src, srcSize); + + { + nuint err_code = (rSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + assert(rSize == srcSize); + dctx->expected -= rSize; + break; + case blockType_e.bt_rle: + { + rSize = ZSTD_setRleBlock(dst, dstCapacity, *(byte*)(src), dctx->rleSize); + } + + dctx->expected = 0; + break; + case blockType_e.bt_reserved: + default: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + } + + } + + + { + nuint err_code = (rSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (rSize > dctx->fParams.blockSizeMax) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + dctx->decodedSize += (ulong)rSize; + if (dctx->validateChecksum != 0) + { + XXH64_update(&dctx->xxhState, dst, rSize); + } + + dctx->previousDstEnd = (sbyte*)(dst) + rSize; + if (dctx->expected > 0) + { + return rSize; + } + + if (dctx->stage == ZSTD_dStage.ZSTDds_decompressLastBlock) + { + if (dctx->fParams.frameContentSize != (unchecked(0UL - 1)) && dctx->decodedSize != dctx->fParams.frameContentSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (dctx->fParams.checksumFlag != 0) + { + dctx->expected = 4; + dctx->stage = ZSTD_dStage.ZSTDds_checkChecksum; + } + else + { + ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, 1); + dctx->expected = 0; + dctx->stage = ZSTD_dStage.ZSTDds_getFrameHeaderSize; + } + } + else + { + dctx->stage = ZSTD_dStage.ZSTDds_decodeBlockHeader; + dctx->expected = ZSTD_blockHeaderSize; + } + + return rSize; + } + + case ZSTD_dStage.ZSTDds_checkChecksum: + { + assert(srcSize == 4); + } + + + { + if (dctx->validateChecksum != 0) + { + uint h32 = (uint)(XXH64_digest(&dctx->xxhState)); + uint check32 = MEM_readLE32(src); + + if (check32 != h32) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_checksum_wrong))); + } + + } + + ZSTD_DCtx_trace_end(dctx, dctx->decodedSize, dctx->processedCSize, 1); + dctx->expected = 0; + dctx->stage = ZSTD_dStage.ZSTDds_getFrameHeaderSize; + return 0; + } + + case ZSTD_dStage.ZSTDds_decodeSkippableHeader: + { + assert(src != null); + } + + assert(srcSize <= 8); + memcpy((void*)((dctx->headerBuffer + (8 - srcSize))), (src), (srcSize)); + dctx->expected = MEM_readLE32((void*)(dctx->headerBuffer + 4)); + dctx->stage = ZSTD_dStage.ZSTDds_skipFrame; + return 0; + case ZSTD_dStage.ZSTDds_skipFrame: + { + dctx->expected = 0; + } + + dctx->stage = ZSTD_dStage.ZSTDds_getFrameHeaderSize; + return 0; + default: + { + assert(0 != 0); + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + } + } + + private static nuint ZSTD_refDictContent(ZSTD_DCtx_s* dctx, void* dict, nuint dictSize) + { + dctx->dictEnd = dctx->previousDstEnd; + dctx->virtualStart = (sbyte*)(dict) - ((sbyte*)(dctx->previousDstEnd) - (sbyte*)(dctx->prefixStart)); + dctx->prefixStart = dict; + dctx->previousDstEnd = (sbyte*)(dict) + dictSize; + return 0; + } + + /*! ZSTD_loadDEntropy() : + * dict : must point at beginning of a valid zstd dictionary. + * @return : size of entropy tables read */ + public static nuint ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy, void* dict, nuint dictSize) + { + byte* dictPtr = (byte*)(dict); + byte* dictEnd = dictPtr + dictSize; + + if (dictSize <= 8) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + assert(MEM_readLE32(dict) == 0xEC30A437); + dictPtr += 8; + + { + void* workspace = (void*)&entropy->LLTable; + nuint workspaceSize = (nuint)(4104) + (nuint)(2056) + (nuint)(4104); + nuint hSize = HUF_readDTableX2_wksp((uint*)entropy->hufTable, (void*)dictPtr, (nuint)(dictEnd - dictPtr), workspace, workspaceSize); + + if ((ERR_isError(hSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + dictPtr += hSize; + } + + + { + short* offcodeNCount = stackalloc short[32]; + uint offcodeMaxValue = 31, offcodeLog; + nuint offcodeHeaderSize = FSE_readNCount((short*)offcodeNCount, &offcodeMaxValue, &offcodeLog, (void*)dictPtr, (nuint)(dictEnd - dictPtr)); + + if ((ERR_isError(offcodeHeaderSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (offcodeMaxValue > 31) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (offcodeLog > 8) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + ZSTD_buildFSETable((ZSTD_seqSymbol*)entropy->OFTable, (short*)offcodeNCount, offcodeMaxValue, (uint*)OF_base, (uint*)OF_bits, offcodeLog, (void*)entropy->workspace, (nuint)(sizeof(uint) * 157), 0); + dictPtr += offcodeHeaderSize; + } + + + { + short* matchlengthNCount = stackalloc short[53]; + uint matchlengthMaxValue = 52, matchlengthLog; + nuint matchlengthHeaderSize = FSE_readNCount((short*)matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, (void*)dictPtr, (nuint)(dictEnd - dictPtr)); + + if ((ERR_isError(matchlengthHeaderSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (matchlengthMaxValue > 52) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (matchlengthLog > 9) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + ZSTD_buildFSETable((ZSTD_seqSymbol*)entropy->MLTable, (short*)matchlengthNCount, matchlengthMaxValue, (uint*)ML_base, (uint*)ML_bits, matchlengthLog, (void*)entropy->workspace, (nuint)(sizeof(uint) * 157), 0); + dictPtr += matchlengthHeaderSize; + } + + + { + short* litlengthNCount = stackalloc short[36]; + uint litlengthMaxValue = 35, litlengthLog; + nuint litlengthHeaderSize = FSE_readNCount((short*)litlengthNCount, &litlengthMaxValue, &litlengthLog, (void*)dictPtr, (nuint)(dictEnd - dictPtr)); + + if ((ERR_isError(litlengthHeaderSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (litlengthMaxValue > 35) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + if (litlengthLog > 9) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + ZSTD_buildFSETable((ZSTD_seqSymbol*)entropy->LLTable, (short*)litlengthNCount, litlengthMaxValue, (uint*)LL_base, (uint*)LL_bits, litlengthLog, (void*)entropy->workspace, (nuint)(sizeof(uint) * 157), 0); + dictPtr += litlengthHeaderSize; + } + + if (dictPtr + 12 > dictEnd) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + + { + int i; + nuint dictContentSize = (nuint)(dictEnd - (dictPtr + 12)); + + for (i = 0; i < 3; i++) + { + uint rep = MEM_readLE32((void*)dictPtr); + + dictPtr += 4; + if (rep == 0 || rep > dictContentSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + entropy->rep[i] = rep; + } + } + + return (nuint)(dictPtr - (byte*)(dict)); + } + + private static nuint ZSTD_decompress_insertDictionary(ZSTD_DCtx_s* dctx, void* dict, nuint dictSize) + { + if (dictSize < 8) + { + return ZSTD_refDictContent(dctx, dict, dictSize); + } + + + { + uint magic = MEM_readLE32(dict); + + if (magic != 0xEC30A437) + { + return ZSTD_refDictContent(dctx, dict, dictSize); + } + } + + dctx->dictID = MEM_readLE32((void*)((sbyte*)(dict) + 4)); + + { + nuint eSize = ZSTD_loadDEntropy(&dctx->entropy, dict, dictSize); + + if ((ERR_isError(eSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + dict = (sbyte*)(dict) + eSize; + dictSize -= eSize; + } + + dctx->litEntropy = dctx->fseEntropy = 1; + return ZSTD_refDictContent(dctx, dict, dictSize); + } + + public static nuint ZSTD_decompressBegin(ZSTD_DCtx_s* dctx) + { + assert(dctx != null); + dctx->expected = ZSTD_startingInputLength(dctx->format); + dctx->stage = ZSTD_dStage.ZSTDds_getFrameHeaderSize; + dctx->processedCSize = 0; + dctx->decodedSize = 0; + dctx->previousDstEnd = null; + dctx->prefixStart = null; + dctx->virtualStart = null; + dctx->dictEnd = null; + dctx->entropy.hufTable[0] = (uint)((12) * 0x1000001); + dctx->litEntropy = dctx->fseEntropy = 0; + dctx->dictID = 0; + dctx->bType = blockType_e.bt_reserved; + memcpy((void*)(dctx->entropy.rep), (void*)(repStartValue), ((nuint)(sizeof(uint) * 3))); + dctx->LLTptr = dctx->entropy.LLTable; + dctx->MLTptr = dctx->entropy.MLTable; + dctx->OFTptr = dctx->entropy.OFTable; + dctx->HUFptr = dctx->entropy.hufTable; + return 0; + } + + public static nuint ZSTD_decompressBegin_usingDict(ZSTD_DCtx_s* dctx, void* dict, nuint dictSize) + { + + { + nuint err_code = (ZSTD_decompressBegin(dctx)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (dict != null && dictSize != 0) + { + if ((ERR_isError(ZSTD_decompress_insertDictionary(dctx, dict, dictSize))) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + } + + return 0; + } + + /* ====== ZSTD_DDict ====== */ + public static nuint ZSTD_decompressBegin_usingDDict(ZSTD_DCtx_s* dctx, ZSTD_DDict_s* ddict) + { + assert(dctx != null); + if (ddict != null) + { + sbyte* dictStart = (sbyte*)(ZSTD_DDict_dictContent(ddict)); + nuint dictSize = ZSTD_DDict_dictSize(ddict); + void* dictEnd = (void*)(dictStart + dictSize); + + dctx->ddictIsCold = ((dctx->dictEnd != dictEnd) ? 1 : 0); + } + + + { + nuint err_code = (ZSTD_decompressBegin(dctx)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (ddict != null) + { + ZSTD_copyDDictParameters(dctx, ddict); + } + + return 0; + } + + /*! ZSTD_getDictID_fromDict() : + * Provides the dictID stored within dictionary. + * if @return == 0, the dictionary is not conformant with Zstandard specification. + * It can still be loaded, but as a content-only dictionary. */ + public static uint ZSTD_getDictID_fromDict(void* dict, nuint dictSize) + { + if (dictSize < 8) + { + return 0; + } + + if (MEM_readLE32(dict) != 0xEC30A437) + { + return 0; + } + + return MEM_readLE32((void*)((sbyte*)(dict) + 4)); + } + + /*! ZSTD_getDictID_fromFrame() : + * Provides the dictID required to decompress frame stored within `src`. + * If @return == 0, the dictID could not be decoded. + * This could for one of the following reasons : + * - The frame does not require a dictionary (most common case). + * - The frame was built with dictID intentionally removed. + * Needed dictionary is a hidden information. + * Note : this use case also happens when using a non-conformant dictionary. + * - `srcSize` is too small, and as a result, frame header could not be decoded. + * Note : possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`. + * - This is not a Zstandard frame. + * When identifying the exact failure cause, it's possible to use + * ZSTD_getFrameHeader(), which will provide a more precise error code. */ + public static uint ZSTD_getDictID_fromFrame(void* src, nuint srcSize) + { + ZSTD_frameHeader zfp = new ZSTD_frameHeader + { + frameContentSize = 0, + windowSize = 0, + blockSizeMax = 0, + frameType = ZSTD_frameType_e.ZSTD_frame, + headerSize = 0, + dictID = 0, + checksumFlag = 0, + }; + nuint hError = ZSTD_getFrameHeader(&zfp, src, srcSize); + + if ((ERR_isError(hError)) != 0) + { + return 0; + } + + return zfp.dictID; + } + + /*! ZSTD_decompress_usingDDict() : + * Decompression using a pre-digested Dictionary + * Use dictionary without significant overhead. */ + public static nuint ZSTD_decompress_usingDDict(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, ZSTD_DDict_s* ddict) + { + return ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, null, 0, ddict); + } + + /*===================================== + * Streaming decompression + *====================================*/ + public static ZSTD_DCtx_s* ZSTD_createDStream() + { + return ZSTD_createDStream_advanced(ZSTD_defaultCMem); + } + + public static ZSTD_DCtx_s* ZSTD_initStaticDStream(void* workspace, nuint workspaceSize) + { + return ZSTD_initStaticDCtx(workspace, workspaceSize); + } + + public static ZSTD_DCtx_s* ZSTD_createDStream_advanced(ZSTD_customMem customMem) + { + return ZSTD_createDCtx_advanced(customMem); + } + + public static nuint ZSTD_freeDStream(ZSTD_DCtx_s* zds) + { + return ZSTD_freeDCtx(zds); + } + + /* *** Initialization *** */ + public static nuint ZSTD_DStreamInSize() + { + return (uint)((1 << 17)) + ZSTD_blockHeaderSize; + } + + public static nuint ZSTD_DStreamOutSize() + { + return (nuint)((1 << 17)); + } + + /*! ZSTD_DCtx_loadDictionary_advanced() : + * Same as ZSTD_DCtx_loadDictionary(), + * but gives direct control over + * how to load the dictionary (by copy ? by reference ?) + * and how to interpret it (automatic ? force raw mode ? full mode only ?). */ + public static nuint ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx_s* dctx, void* dict, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType) + { + if (dctx->streamStage != ZSTD_dStreamStage.zdss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + ZSTD_clearDict(dctx); + if (dict != null && dictSize != 0) + { + dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem); + if (dctx->ddictLocal == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + dctx->ddict = dctx->ddictLocal; + dctx->dictUses = ZSTD_dictUses_e.ZSTD_use_indefinitely; + } + + return 0; + } + + /*! ZSTD_DCtx_loadDictionary_byReference() : + * Same as ZSTD_DCtx_loadDictionary(), + * but references `dict` content instead of copying it into `dctx`. + * This saves memory if `dict` remains around., + * However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */ + public static nuint ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx_s* dctx, void* dict, nuint dictSize) + { + return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, ZSTD_dictContentType_e.ZSTD_dct_auto); + } + + /*! ZSTD_DCtx_loadDictionary() : Requires v1.4.0+ + * Create an internal DDict from dict buffer, + * to be used to decompress next frames. + * The dictionary remains valid for all future frames, until explicitly invalidated. + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary, + * meaning "return to no-dictionary mode". + * Note 1 : Loading a dictionary involves building tables, + * which has a non-negligible impact on CPU usage and latency. + * It's recommended to "load once, use many times", to amortize the cost + * Note 2 :`dict` content will be copied internally, so `dict` can be released after loading. + * Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead. + * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of + * how dictionary content is loaded and interpreted. + */ + public static nuint ZSTD_DCtx_loadDictionary(ZSTD_DCtx_s* dctx, void* dict, nuint dictSize) + { + return ZSTD_DCtx_loadDictionary_advanced(dctx, dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy, ZSTD_dictContentType_e.ZSTD_dct_auto); + } + + /*! ZSTD_DCtx_refPrefix_advanced() : + * Same as ZSTD_DCtx_refPrefix(), but gives finer control over + * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */ + public static nuint ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx_s* dctx, void* prefix, nuint prefixSize, ZSTD_dictContentType_e dictContentType) + { + + { + nuint err_code = (ZSTD_DCtx_loadDictionary_advanced(dctx, prefix, prefixSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, dictContentType)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + dctx->dictUses = ZSTD_dictUses_e.ZSTD_use_once; + return 0; + } + + /*! ZSTD_DCtx_refPrefix() : Requires v1.4.0+ + * Reference a prefix (single-usage dictionary) to decompress next frame. + * This is the reverse operation of ZSTD_CCtx_refPrefix(), + * and must use the same prefix as the one used during compression. + * Prefix is **only used once**. Reference is discarded at end of frame. + * End of frame is reached when ZSTD_decompressStream() returns 0. + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary + * Note 2 : Prefix buffer is referenced. It **must** outlive decompression. + * Prefix buffer must remain unmodified up to the end of frame, + * reached when ZSTD_decompressStream() returns 0. + * Note 3 : By default, the prefix is treated as raw content (ZSTD_dct_rawContent). + * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section) + * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost. + * A full dictionary is more costly, as it requires building tables. + */ + public static nuint ZSTD_DCtx_refPrefix(ZSTD_DCtx_s* dctx, void* prefix, nuint prefixSize) + { + return ZSTD_DCtx_refPrefix_advanced(dctx, prefix, prefixSize, ZSTD_dictContentType_e.ZSTD_dct_rawContent); + } + + /* ZSTD_initDStream_usingDict() : + * return : expected size, aka ZSTD_startingInputLength(). + * this function cannot fail */ + public static nuint ZSTD_initDStream_usingDict(ZSTD_DCtx_s* zds, void* dict, nuint dictSize) + { + + { + nuint err_code = (ZSTD_DCtx_reset(zds, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_DCtx_loadDictionary(zds, dict, dictSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_startingInputLength(zds->format); + } + + /* note : this variant can't fail */ + public static nuint ZSTD_initDStream(ZSTD_DCtx_s* zds) + { + return ZSTD_initDStream_usingDDict(zds, (ZSTD_DDict_s*)null); + } + + /* ZSTD_initDStream_usingDDict() : + * ddict will just be referenced, and must outlive decompression session + * this function cannot fail */ + public static nuint ZSTD_initDStream_usingDDict(ZSTD_DCtx_s* dctx, ZSTD_DDict_s* ddict) + { + + { + nuint err_code = (ZSTD_DCtx_reset(dctx, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + + { + nuint err_code = (ZSTD_DCtx_refDDict(dctx, ddict)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_startingInputLength(dctx->format); + } + + /* ZSTD_resetDStream() : + * return : expected size, aka ZSTD_startingInputLength(). + * this function cannot fail */ + public static nuint ZSTD_resetDStream(ZSTD_DCtx_s* dctx) + { + + { + nuint err_code = (ZSTD_DCtx_reset(dctx, ZSTD_ResetDirective.ZSTD_reset_session_only)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + return ZSTD_startingInputLength(dctx->format); + } + + /*! ZSTD_DCtx_refDDict() : Requires v1.4.0+ + * Reference a prepared dictionary, to be used to decompress next frames. + * The dictionary remains active for decompression of future frames using same DCtx. + * + * If called with ZSTD_d_refMultipleDDicts enabled, repeated calls of this function + * will store the DDict references in a table, and the DDict used for decompression + * will be determined at decompression time, as per the dict ID in the frame. + * The memory for the table is allocated on the first call to refDDict, and can be + * freed with ZSTD_freeDCtx(). + * + * @result : 0, or an error code (which can be tested with ZSTD_isError()). + * Note 1 : Currently, only one dictionary can be managed. + * Referencing a new dictionary effectively "discards" any previous one. + * Special: referencing a NULL DDict means "return to no-dictionary mode". + * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx. + */ + public static nuint ZSTD_DCtx_refDDict(ZSTD_DCtx_s* dctx, ZSTD_DDict_s* ddict) + { + if (dctx->streamStage != ZSTD_dStreamStage.zdss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + ZSTD_clearDict(dctx); + if (ddict != null) + { + dctx->ddict = ddict; + dctx->dictUses = ZSTD_dictUses_e.ZSTD_use_indefinitely; + if (dctx->refMultipleDDicts == ZSTD_refMultipleDDicts_e.ZSTD_rmd_refMultipleDDicts) + { + if (dctx->ddictSet == null) + { + dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem); + if (dctx->ddictSet == null) + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + } + } + + assert(dctx->staticSize == 0); + + { + nuint err_code = (ZSTD_DDictHashSet_addDDict(dctx->ddictSet, ddict, dctx->customMem)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + } + } + + return 0; + } + + /* ZSTD_DCtx_setMaxWindowSize() : + * note : no direct equivalence in ZSTD_DCtx_setParameter, + * since this version sets windowSize, and the other sets windowLog */ + public static nuint ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx_s* dctx, nuint maxWindowSize) + { + ZSTD_bounds bounds = ZSTD_dParam_getBounds(ZSTD_dParameter.ZSTD_d_windowLogMax); + nuint min = (nuint)(1) << bounds.lowerBound; + nuint max = (nuint)(1) << bounds.upperBound; + + if (dctx->streamStage != ZSTD_dStreamStage.zdss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + if (maxWindowSize < min) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + if (maxWindowSize > max) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + dctx->maxWindowSize = maxWindowSize; + return 0; + } + + /*! ZSTD_DCtx_setFormat() : + * This function is REDUNDANT. Prefer ZSTD_DCtx_setParameter(). + * Instruct the decoder context about what kind of data to decode next. + * This instruction is mandatory to decode data without a fully-formed header, + * such ZSTD_f_zstd1_magicless for example. + * @return : 0, or an error code (which can be tested using ZSTD_isError()). */ + public static nuint ZSTD_DCtx_setFormat(ZSTD_DCtx_s* dctx, ZSTD_format_e format) + { + return ZSTD_DCtx_setParameter(dctx, ZSTD_dParameter.ZSTD_d_experimentalParam1, (int)(format)); + } + + /*! ZSTD_dParam_getBounds() : + * All parameters must belong to an interval with lower and upper bounds, + * otherwise they will either trigger an error or be automatically clamped. + * @return : a structure, ZSTD_bounds, which contains + * - an error status field, which must be tested using ZSTD_isError() + * - both lower and upper bounds, inclusive + */ + public static ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam) + { + ZSTD_bounds bounds = new ZSTD_bounds + { + error = 0, + lowerBound = 0, + upperBound = 0, + }; + + switch (dParam) + { + case ZSTD_dParameter.ZSTD_d_windowLogMax: + { + bounds.lowerBound = 10; + } + + bounds.upperBound = ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)); + return bounds; + case ZSTD_dParameter.ZSTD_d_experimentalParam1: + { + bounds.lowerBound = (int)(ZSTD_format_e.ZSTD_f_zstd1); + } + + bounds.upperBound = (int)(ZSTD_format_e.ZSTD_f_zstd1_magicless); + return bounds; + case ZSTD_dParameter.ZSTD_d_experimentalParam2: + { + bounds.lowerBound = (int)(ZSTD_bufferMode_e.ZSTD_bm_buffered); + } + + bounds.upperBound = (int)(ZSTD_bufferMode_e.ZSTD_bm_stable); + return bounds; + case ZSTD_dParameter.ZSTD_d_experimentalParam3: + { + bounds.lowerBound = (int)(ZSTD_forceIgnoreChecksum_e.ZSTD_d_validateChecksum); + } + + bounds.upperBound = (int)(ZSTD_forceIgnoreChecksum_e.ZSTD_d_ignoreChecksum); + return bounds; + case ZSTD_dParameter.ZSTD_d_experimentalParam4: + { + bounds.lowerBound = (int)(ZSTD_refMultipleDDicts_e.ZSTD_rmd_refSingleDDict); + } + + bounds.upperBound = (int)(ZSTD_refMultipleDDicts_e.ZSTD_rmd_refMultipleDDicts); + return bounds; + default: + { + ; + } + break; + } + + bounds.error = (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + return bounds; + } + + /* ZSTD_dParam_withinBounds: + * @return 1 if value is within dParam bounds, + * 0 otherwise */ + private static int ZSTD_dParam_withinBounds(ZSTD_dParameter dParam, int value) + { + ZSTD_bounds bounds = ZSTD_dParam_getBounds(dParam); + + if ((ERR_isError(bounds.error)) != 0) + { + return 0; + } + + if (value < bounds.lowerBound) + { + return 0; + } + + if (value > bounds.upperBound) + { + return 0; + } + + return 1; + } + + /*! ZSTD_DCtx_getParameter() : + * Get the requested decompression parameter value, selected by enum ZSTD_dParameter, + * and store it into int* value. + * @return : 0, or an error code (which can be tested with ZSTD_isError()). + */ + public static nuint ZSTD_DCtx_getParameter(ZSTD_DCtx_s* dctx, ZSTD_dParameter param, int* value) + { + switch (param) + { + case ZSTD_dParameter.ZSTD_d_windowLogMax: + { + *value = (int)(ZSTD_highbit32((uint)(dctx->maxWindowSize))); + } + + return 0; + case ZSTD_dParameter.ZSTD_d_experimentalParam1: + { + *value = (int)(dctx->format); + } + + return 0; + case ZSTD_dParameter.ZSTD_d_experimentalParam2: + { + *value = (int)(dctx->outBufferMode); + } + + return 0; + case ZSTD_dParameter.ZSTD_d_experimentalParam3: + { + *value = (int)(dctx->forceIgnoreChecksum); + } + + return 0; + case ZSTD_dParameter.ZSTD_d_experimentalParam4: + { + *value = (int)(dctx->refMultipleDDicts); + } + + return 0; + default: + { + ; + } + break; + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + + } + + /*! ZSTD_DCtx_setParameter() : + * Set one compression parameter, selected by enum ZSTD_dParameter. + * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds(). + * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). + * Setting a parameter is only possible during frame initialization (before starting decompression). + * @return : 0, or an error code (which can be tested using ZSTD_isError()). + */ + public static nuint ZSTD_DCtx_setParameter(ZSTD_DCtx_s* dctx, ZSTD_dParameter dParam, int value) + { + if (dctx->streamStage != ZSTD_dStreamStage.zdss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + switch (dParam) + { + case ZSTD_dParameter.ZSTD_d_windowLogMax: + { + if (value == 0) + { + value = 27; + } + } + + + { + if ((ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_windowLogMax, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + dctx->maxWindowSize = ((nuint)(1)) << value; + return 0; + case ZSTD_dParameter.ZSTD_d_experimentalParam1: + { + if ((ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam1, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + dctx->format = (ZSTD_format_e)(value); + return 0; + case ZSTD_dParameter.ZSTD_d_experimentalParam2: + { + if ((ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam2, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + dctx->outBufferMode = (ZSTD_bufferMode_e)(value); + return 0; + case ZSTD_dParameter.ZSTD_d_experimentalParam3: + { + if ((ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam3, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + dctx->forceIgnoreChecksum = (ZSTD_forceIgnoreChecksum_e)(value); + return 0; + case ZSTD_dParameter.ZSTD_d_experimentalParam4: + { + if ((ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam4, value)) == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound))); + } + + } + + if (dctx->staticSize != 0) + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + + } + + dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)(value); + return 0; + default: + { + ; + } + break; + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported))); + } + + } + + /*! ZSTD_DCtx_reset() : + * Return a DCtx to clean state. + * Session and parameters can be reset jointly or separately. + * Parameters can only be reset when no active frame is being decompressed. + * @return : 0, or an error code, which can be tested with ZSTD_isError() + */ + public static nuint ZSTD_DCtx_reset(ZSTD_DCtx_s* dctx, ZSTD_ResetDirective reset) + { + if ((reset == ZSTD_ResetDirective.ZSTD_reset_session_only) || (reset == ZSTD_ResetDirective.ZSTD_reset_session_and_parameters)) + { + dctx->streamStage = ZSTD_dStreamStage.zdss_init; + dctx->noForwardProgress = 0; + } + + if ((reset == ZSTD_ResetDirective.ZSTD_reset_parameters) || (reset == ZSTD_ResetDirective.ZSTD_reset_session_and_parameters)) + { + if (dctx->streamStage != ZSTD_dStreamStage.zdss_init) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stage_wrong))); + } + + ZSTD_clearDict(dctx); + ZSTD_DCtx_resetParameters(dctx); + } + + return 0; + } + + public static nuint ZSTD_sizeof_DStream(ZSTD_DCtx_s* dctx) + { + return ZSTD_sizeof_DCtx(dctx); + } + + public static nuint ZSTD_decodingBufferSize_min(ulong windowSize, ulong frameContentSize) + { + nuint blockSize = (nuint)((windowSize) < (uint)(((1 << 17))) ? (windowSize) : ((1 << 17))); + ulong neededRBSize = windowSize + blockSize + (uint)((32 * 2)); + ulong neededSize = ((frameContentSize) < (neededRBSize) ? (frameContentSize) : (neededRBSize)); + nuint minRBSize = (nuint)(neededSize); + + if ((ulong)(minRBSize) != neededSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge))); + } + + return minRBSize; + } + + public static nuint ZSTD_estimateDStreamSize(nuint windowSize) + { + nuint blockSize = (nuint)((windowSize) < (uint)(((1 << 17))) ? (windowSize) : ((1 << 17))); + nuint inBuffSize = blockSize; + nuint outBuffSize = ZSTD_decodingBufferSize_min((ulong)windowSize, (unchecked(0UL - 1))); + + return ZSTD_estimateDCtxSize() + inBuffSize + outBuffSize; + } + + public static nuint ZSTD_estimateDStreamSize_fromFrame(void* src, nuint srcSize) + { + uint windowSizeMax = 1U << (unchecked((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31))); + ZSTD_frameHeader zfh; + nuint err = ZSTD_getFrameHeader(&zfh, src, srcSize); + + if ((ERR_isError(err)) != 0) + { + return err; + } + + if (err > 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (zfh.windowSize > windowSizeMax) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge))); + } + + return ZSTD_estimateDStreamSize((nuint)(zfh.windowSize)); + } + + /* ***** Decompression ***** */ + private static int ZSTD_DCtx_isOverflow(ZSTD_DCtx_s* zds, nuint neededInBuffSize, nuint neededOutBuffSize) + { + return (((zds->inBuffSize + zds->outBuffSize) >= (neededInBuffSize + neededOutBuffSize) * 3) ? 1 : 0); + } + + private static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DCtx_s* zds, nuint neededInBuffSize, nuint neededOutBuffSize) + { + if ((ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize)) != 0) + { + zds->oversizedDuration++; + } + else + { + zds->oversizedDuration = 0; + } + } + + private static int ZSTD_DCtx_isOversizedTooLong(ZSTD_DCtx_s* zds) + { + return ((zds->oversizedDuration >= 128) ? 1 : 0); + } + + /* Checks that the output buffer hasn't changed if ZSTD_obm_stable is used. */ + private static nuint ZSTD_checkOutBuffer(ZSTD_DCtx_s* zds, ZSTD_outBuffer_s* output) + { + ZSTD_outBuffer_s expect = zds->expectedOutBuffer; + + if (zds->outBufferMode != ZSTD_bufferMode_e.ZSTD_bm_stable) + { + return 0; + } + + if (zds->streamStage == ZSTD_dStreamStage.zdss_init) + { + return 0; + } + + if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size) + { + return 0; + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstBuffer_wrong))); + } + + } + + /* Calls ZSTD_decompressContinue() with the right parameters for ZSTD_decompressStream() + * and updates the stage and the output buffer state. This call is extracted so it can be + * used both when reading directly from the ZSTD_inBuffer, and in buffered input mode. + * NOTE: You must break after calling this function since the streamStage is modified. + */ + private static nuint ZSTD_decompressContinueStream(ZSTD_DCtx_s* zds, sbyte** op, sbyte* oend, void* src, nuint srcSize) + { + int isSkipFrame = ZSTD_isSkipFrame(zds); + + if (zds->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) + { + nuint dstSize = isSkipFrame != 0 ? 0 : zds->outBuffSize - zds->outStart; + nuint decodedSize = ZSTD_decompressContinue(zds, (void*)(zds->outBuff + zds->outStart), dstSize, src, srcSize); + + + { + nuint err_code = (decodedSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if (decodedSize == 0 && isSkipFrame == 0) + { + zds->streamStage = ZSTD_dStreamStage.zdss_read; + } + else + { + zds->outEnd = zds->outStart + decodedSize; + zds->streamStage = ZSTD_dStreamStage.zdss_flush; + } + } + else + { + nuint dstSize = isSkipFrame != 0 ? 0 : (nuint)(oend - *op); + nuint decodedSize = ZSTD_decompressContinue(zds, (void*)*op, dstSize, src, srcSize); + + + { + nuint err_code = (decodedSize); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + *op += decodedSize; + zds->streamStage = ZSTD_dStreamStage.zdss_read; + assert(*op <= oend); + assert(zds->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable); + } + + return 0; + } + + public static nuint ZSTD_decompressStream(ZSTD_DCtx_s* zds, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input) + { + sbyte* src = (sbyte*)(input->src); + sbyte* istart = input->pos != 0 ? src + input->pos : src; + sbyte* iend = input->size != 0 ? src + input->size : src; + sbyte* ip = istart; + sbyte* dst = (sbyte*)(output->dst); + sbyte* ostart = output->pos != 0 ? dst + output->pos : dst; + sbyte* oend = output->size != 0 ? dst + output->size : dst; + sbyte* op = ostart; + uint someMoreWork = 1; + + if (input->pos > input->size) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + if (output->pos > output->size) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + + { + nuint err_code = (ZSTD_checkOutBuffer(zds, output)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + while (someMoreWork != 0) + { + switch (zds->streamStage) + { + case ZSTD_dStreamStage.zdss_init: + ; + zds->streamStage = ZSTD_dStreamStage.zdss_loadHeader; + zds->lhSize = zds->inPos = zds->outStart = zds->outEnd = 0; + zds->legacyVersion = 0; + zds->hostageByte = 0; + zds->expectedOutBuffer = *output; + + goto case ZSTD_dStreamStage.zdss_loadHeader; + case ZSTD_dStreamStage.zdss_loadHeader: + ; + + { + nuint hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, (void*)zds->headerBuffer, zds->lhSize, zds->format); + + if (zds->refMultipleDDicts != default && zds->ddictSet != null) + { + ZSTD_DCtx_selectFrameDDict(zds); + } + + if ((ERR_isError(hSize)) != 0) + { + return hSize; + } + + if (hSize != 0) + { + nuint toLoad = hSize - zds->lhSize; + nuint remainingInput = (nuint)(iend - ip); + + assert(iend >= ip); + if (toLoad > remainingInput) + { + if (remainingInput > 0) + { + memcpy((void*)((zds->headerBuffer + zds->lhSize)), (void*)(ip), (remainingInput)); + zds->lhSize += remainingInput; + } + + input->pos = input->size; + return ((((nuint)((zds->format) == ZSTD_format_e.ZSTD_f_zstd1 ? 6 : 2)) > (hSize) ? ((nuint)((zds->format) == ZSTD_format_e.ZSTD_f_zstd1 ? 6 : 2)) : (hSize)) - zds->lhSize) + ZSTD_blockHeaderSize; + } + + assert(ip != null); + memcpy((void*)((zds->headerBuffer + zds->lhSize)), (void*)(ip), (toLoad)); + zds->lhSize = hSize; + ip += toLoad; + break; + } + } + + if (zds->fParams.frameContentSize != (unchecked(0UL - 1)) && zds->fParams.frameType != ZSTD_frameType_e.ZSTD_skippableFrame && (ulong)((nuint)(oend - op)) >= zds->fParams.frameContentSize) + { + nuint cSize = ZSTD_findFrameCompressedSize((void*)istart, (nuint)(iend - istart)); + + if (cSize <= (nuint)(iend - istart)) + { + nuint decompressedSize = ZSTD_decompress_usingDDict(zds, (void*)op, (nuint)(oend - op), (void*)istart, cSize, ZSTD_getDDict(zds)); + + if ((ERR_isError(decompressedSize)) != 0) + { + return decompressedSize; + } + + ip = istart + cSize; + op += decompressedSize; + zds->expected = 0; + zds->streamStage = ZSTD_dStreamStage.zdss_init; + someMoreWork = 0; + break; + } + } + + if (zds->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable && zds->fParams.frameType != ZSTD_frameType_e.ZSTD_skippableFrame && zds->fParams.frameContentSize != (unchecked(0UL - 1)) && (ulong)((nuint)(oend - op)) < zds->fParams.frameContentSize) + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + } + + + { + nuint err_code = (ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds))); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + if ((MEM_readLE32((void*)zds->headerBuffer) & 0xFFFFFFF0) == 0x184D2A50) + { + zds->expected = MEM_readLE32((void*)(zds->headerBuffer + 4)); + zds->stage = ZSTD_dStage.ZSTDds_skipFrame; + } + else + { + + { + nuint err_code = (ZSTD_decodeFrameHeader(zds, (void*)zds->headerBuffer, zds->lhSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + zds->expected = ZSTD_blockHeaderSize; + zds->stage = ZSTD_dStage.ZSTDds_decodeBlockHeader; + } + + zds->fParams.windowSize = ((zds->fParams.windowSize) > (1U << 10) ? (zds->fParams.windowSize) : (1U << 10)); + if (zds->fParams.windowSize > zds->maxWindowSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge))); + } + + + { + nuint neededInBuffSize = ((zds->fParams.blockSizeMax) > (4) ? (zds->fParams.blockSizeMax) : (4)); + nuint neededOutBuffSize = zds->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered ? ZSTD_decodingBufferSize_min(zds->fParams.windowSize, zds->fParams.frameContentSize) : 0; + + ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize); + + { + int tooSmall = (((zds->inBuffSize < neededInBuffSize) || (zds->outBuffSize < neededOutBuffSize)) ? 1 : 0); + int tooLarge = ZSTD_DCtx_isOversizedTooLong(zds); + + if (tooSmall != 0 || tooLarge != 0) + { + nuint bufferSize = neededInBuffSize + neededOutBuffSize; + + if (zds->staticSize != 0) + { + assert(zds->staticSize >= (nuint)(sizeof(ZSTD_DCtx_s))); + if (bufferSize > zds->staticSize - (nuint)(sizeof(ZSTD_DCtx_s))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + } + else + { + ZSTD_customFree((void*)zds->inBuff, zds->customMem); + zds->inBuffSize = 0; + zds->outBuffSize = 0; + zds->inBuff = (sbyte*)(ZSTD_customMalloc(bufferSize, zds->customMem)); + if (zds->inBuff == null) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation))); + } + + } + + zds->inBuffSize = neededInBuffSize; + zds->outBuff = zds->inBuff + zds->inBuffSize; + zds->outBuffSize = neededOutBuffSize; + } + } + } + + zds->streamStage = ZSTD_dStreamStage.zdss_read; + + goto case ZSTD_dStreamStage.zdss_read; + case ZSTD_dStreamStage.zdss_read: + ; + + { + nuint neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (nuint)(iend - ip)); + + if (neededInSize == 0) + { + zds->streamStage = ZSTD_dStreamStage.zdss_init; + someMoreWork = 0; + break; + } + + if ((nuint)(iend - ip) >= neededInSize) + { + + { + nuint err_code = (ZSTD_decompressContinueStream(zds, &op, oend, (void*)ip, neededInSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + ip += neededInSize; + break; + } + } + + if (ip == iend) + { + someMoreWork = 0; + break; + } + + zds->streamStage = ZSTD_dStreamStage.zdss_load; + + goto case ZSTD_dStreamStage.zdss_load; + case ZSTD_dStreamStage.zdss_load: + { + nuint neededInSize = ZSTD_nextSrcSizeToDecompress(zds); + nuint toLoad = neededInSize - zds->inPos; + int isSkipFrame = ZSTD_isSkipFrame(zds); + nuint loadedSize; + + assert(neededInSize == ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (nuint)(iend - ip))); + if (isSkipFrame != 0) + { + loadedSize = ((toLoad) < ((nuint)(iend - ip)) ? (toLoad) : ((nuint)(iend - ip))); + } + else + { + if (toLoad > zds->inBuffSize - zds->inPos) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + loadedSize = ZSTD_limitCopy((void*)(zds->inBuff + zds->inPos), toLoad, (void*)ip, (nuint)(iend - ip)); + } + + ip += loadedSize; + zds->inPos += loadedSize; + if (loadedSize < toLoad) + { + someMoreWork = 0; + break; + } + + zds->inPos = 0; + + { + nuint err_code = (ZSTD_decompressContinueStream(zds, &op, oend, (void*)zds->inBuff, neededInSize)); + + if ((ERR_isError(err_code)) != 0) + { + return err_code; + } + } + + break; + } + + case ZSTD_dStreamStage.zdss_flush: + { + nuint toFlushSize = zds->outEnd - zds->outStart; + nuint flushedSize = ZSTD_limitCopy((void*)op, (nuint)(oend - op), (void*)(zds->outBuff + zds->outStart), toFlushSize); + + op += flushedSize; + zds->outStart += flushedSize; + if (flushedSize == toFlushSize) + { + zds->streamStage = ZSTD_dStreamStage.zdss_read; + if ((zds->outBuffSize < zds->fParams.frameContentSize) && (zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize)) + { + zds->outStart = zds->outEnd = 0; + } + + break; + } + } + + someMoreWork = 0; + break; + default: + { + assert(0 != 0); + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + } + } + + input->pos = (nuint)(ip - (sbyte*)(input->src)); + output->pos = (nuint)(op - (sbyte*)(output->dst)); + zds->expectedOutBuffer = *output; + if ((ip == istart) && (op == ostart)) + { + zds->noForwardProgress++; + if (zds->noForwardProgress >= 16) + { + if (op == oend) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (ip == iend) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + assert(0 != 0); + } + } + else + { + zds->noForwardProgress = 0; + } + + + { + nuint nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zds); + + if (nextSrcSizeHint == 0) + { + if (zds->outEnd == zds->outStart) + { + if (zds->hostageByte != 0) + { + if (input->pos >= input->size) + { + zds->streamStage = ZSTD_dStreamStage.zdss_read; + return 1; + } + + input->pos++; + } + + return 0; + } + + if (zds->hostageByte == 0) + { + input->pos--; + zds->hostageByte = 1; + } + + return 1; + } + + nextSrcSizeHint += ZSTD_blockHeaderSize * (uint)((((ZSTD_nextInputType(zds) == ZSTD_nextInputType_e.ZSTDnit_block)) ? 1 : 0)); + assert(zds->inPos <= nextSrcSizeHint); + nextSrcSizeHint -= zds->inPos; + return nextSrcSizeHint; + } + } + + /*! ZSTD_decompressStream_simpleArgs() : + * Same as ZSTD_decompressStream(), + * but using only integral types as arguments. + * This can be helpful for binders from dynamic languages + * which have troubles handling structures containing memory pointers. + */ + public static nuint ZSTD_decompressStream_simpleArgs(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, nuint* dstPos, void* src, nuint srcSize, nuint* srcPos) + { + ZSTD_outBuffer_s output = new ZSTD_outBuffer_s + { + dst = dst, + size = dstCapacity, + pos = *dstPos, + }; + ZSTD_inBuffer_s input = new ZSTD_inBuffer_s + { + src = src, + size = srcSize, + pos = *srcPos, + }; + nuint cErr = ZSTD_decompressStream(dctx, &output, &input); + + *dstPos = output.pos; + *srcPos = input.pos; + return cErr; + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompressBlock.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompressBlock.cs new file mode 100644 index 00000000..19332b54 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompressBlock.cs @@ -0,0 +1,2649 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*_******************************************************* + * Memory operations + **********************************************************/ + private static void ZSTD_copy4(void* dst, void* src) + { + memcpy((dst), (src), (4)); + } + + /*! ZSTD_getcBlockSize() : + * Provides the size of compressed block from block header `src` */ + public static nuint ZSTD_getcBlockSize(void* src, nuint srcSize, blockProperties_t* bpPtr) + { + if (srcSize < ZSTD_blockHeaderSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + + { + uint cBlockHeader = MEM_readLE24(src); + uint cSize = cBlockHeader >> 3; + + bpPtr->lastBlock = cBlockHeader & 1; + bpPtr->blockType = (blockType_e)((cBlockHeader >> 1) & 3); + bpPtr->origSize = cSize; + if (bpPtr->blockType == blockType_e.bt_rle) + { + return 1; + } + + if (bpPtr->blockType == blockType_e.bt_reserved) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + return cSize; + } + } + + /*! ZSTD_decodeLiteralsBlock() : + * @return : nb of bytes read from src (< srcSize ) + * note : symbol not declared but exposed for fullbench */ + public static nuint ZSTD_decodeLiteralsBlock(ZSTD_DCtx_s* dctx, void* src, nuint srcSize) + { + if (srcSize < (uint)((1 + 1 + 1))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + byte* istart = (byte*)(src); + symbolEncodingType_e litEncType = (symbolEncodingType_e)(istart[0] & 3); + + switch (litEncType) + { + case symbolEncodingType_e.set_repeat: + ; + if (dctx->litEntropy == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted))); + } + + + goto case symbolEncodingType_e.set_compressed; + case symbolEncodingType_e.set_compressed: + { + if (srcSize < 5) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + } + + + { + nuint lhSize, litSize, litCSize; + uint singleStream = 0; + uint lhlCode = (uint)((istart[0] >> 2) & 3); + uint lhc = MEM_readLE32((void*)istart); + nuint hufSuccess; + + switch (lhlCode) + { + case 0: + case 1: + default: + { + singleStream = (lhlCode == 0 ? 1U : 0U); + } + + lhSize = 3; + litSize = (lhc >> 4) & 0x3FF; + litCSize = (lhc >> 14) & 0x3FF; + break; + case 2: + { + lhSize = 4; + } + + litSize = (lhc >> 4) & 0x3FFF; + litCSize = lhc >> 18; + break; + case 3: + { + lhSize = 5; + } + + litSize = (lhc >> 4) & 0x3FFFF; + litCSize = (lhc >> 22) + ((nuint)(istart[4]) << 10); + break; + } + + if (litSize > (uint)((1 << 17))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (litCSize + lhSize > srcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (dctx->ddictIsCold != 0 && (litSize > 768)) + { + + { + sbyte* _ptr = (sbyte*)(dctx->HUFptr); + nuint _size = (nuint)((nuint)(16388)); + nuint _pos; + + for (_pos = 0; _pos < _size; _pos += 64) + { + Prefetch1((void*)(_ptr + _pos)); + } + } + + } + + if (litEncType == symbolEncodingType_e.set_repeat) + { + if (singleStream != 0) + { + hufSuccess = HUF_decompress1X_usingDTable_bmi2((void*)dctx->litBuffer, litSize, (void*)(istart + lhSize), litCSize, dctx->HUFptr, dctx->bmi2); + } + else + { + hufSuccess = HUF_decompress4X_usingDTable_bmi2((void*)dctx->litBuffer, litSize, (void*)(istart + lhSize), litCSize, dctx->HUFptr, dctx->bmi2); + } + } + else + { + if (singleStream != 0) + { + hufSuccess = HUF_decompress1X1_DCtx_wksp_bmi2((uint*)dctx->entropy.hufTable, (void*)dctx->litBuffer, litSize, (void*)(istart + lhSize), litCSize, (void*)dctx->workspace, (nuint)(sizeof(uint) * 640), dctx->bmi2); + } + else + { + hufSuccess = HUF_decompress4X_hufOnly_wksp_bmi2((uint*)dctx->entropy.hufTable, (void*)dctx->litBuffer, litSize, (void*)(istart + lhSize), litCSize, (void*)dctx->workspace, (nuint)(sizeof(uint) * 640), dctx->bmi2); + } + } + + if ((ERR_isError(hufSuccess)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + dctx->litPtr = dctx->litBuffer; + dctx->litSize = litSize; + dctx->litEntropy = 1; + if (litEncType == symbolEncodingType_e.set_compressed) + { + dctx->HUFptr = dctx->entropy.hufTable; + } + + memset((void*)((dctx->litBuffer + dctx->litSize)), (0), (32)); + return litCSize + lhSize; + } + + case symbolEncodingType_e.set_basic: + { + nuint litSize, lhSize; + uint lhlCode = (uint)(((istart[0]) >> 2) & 3); + + switch (lhlCode) + { + case 0: + case 2: + default: + { + lhSize = 1; + } + + litSize = (nuint)(istart[0] >> 3); + break; + case 1: + { + lhSize = 2; + } + + litSize = (nuint)(MEM_readLE16((void*)istart) >> 4); + break; + case 3: + { + lhSize = 3; + } + + litSize = MEM_readLE24((void*)istart) >> 4; + break; + } + + if (lhSize + litSize + 32 > srcSize) + { + if (litSize + lhSize > srcSize) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + memcpy((void*)(dctx->litBuffer), (void*)((istart + lhSize)), (litSize)); + dctx->litPtr = dctx->litBuffer; + dctx->litSize = litSize; + memset((void*)((dctx->litBuffer + dctx->litSize)), (0), (32)); + return lhSize + litSize; + } + + dctx->litPtr = istart + lhSize; + dctx->litSize = litSize; + return lhSize + litSize; + } + + case symbolEncodingType_e.set_rle: + { + uint lhlCode = (uint)(((istart[0]) >> 2) & 3); + nuint litSize, lhSize; + + switch (lhlCode) + { + case 0: + case 2: + default: + { + lhSize = 1; + } + + litSize = (nuint)(istart[0] >> 3); + break; + case 1: + { + lhSize = 2; + } + + litSize = (nuint)(MEM_readLE16((void*)istart) >> 4); + break; + case 3: + { + lhSize = 3; + } + + litSize = MEM_readLE24((void*)istart) >> 4; + if (srcSize < 4) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + break; + } + + if (litSize > (uint)((1 << 17))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + memset((void*)(dctx->litBuffer), (int)((istart[lhSize])), (litSize + 32)); + dctx->litPtr = dctx->litBuffer; + dctx->litSize = litSize; + return lhSize + 1; + } + + default: + { + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + } + + } + } + } + + public static ZSTD_seqSymbol* LL_defaultDTable = GetArrayPointer(new ZSTD_seqSymbol[65] + { + new ZSTD_seqSymbol + { + nextState = 1, + nbAdditionalBits = 1, + nbBits = 1, + baseValue = 6, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 0, + }, + new ZSTD_seqSymbol + { + nextState = 16, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 0, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 1, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 3, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 4, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 6, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 7, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 9, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 10, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 12, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 14, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 16, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 20, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 22, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 2, + nbBits = 5, + baseValue = 28, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 3, + nbBits = 5, + baseValue = 32, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 4, + nbBits = 5, + baseValue = 48, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 6, + nbBits = 5, + baseValue = 64, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 7, + nbBits = 5, + baseValue = 128, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 8, + nbBits = 6, + baseValue = 256, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 10, + nbBits = 6, + baseValue = 1024, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 12, + nbBits = 6, + baseValue = 4096, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 0, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 1, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 2, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 4, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 5, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 7, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 8, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 10, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 11, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 13, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 16, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 18, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 22, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 2, + nbBits = 5, + baseValue = 24, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 3, + nbBits = 5, + baseValue = 32, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 3, + nbBits = 5, + baseValue = 40, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 6, + nbBits = 4, + baseValue = 64, + }, + new ZSTD_seqSymbol + { + nextState = 16, + nbAdditionalBits = 6, + nbBits = 4, + baseValue = 64, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 7, + nbBits = 5, + baseValue = 128, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 9, + nbBits = 6, + baseValue = 512, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 11, + nbBits = 6, + baseValue = 2048, + }, + new ZSTD_seqSymbol + { + nextState = 48, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 0, + }, + new ZSTD_seqSymbol + { + nextState = 16, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 1, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 2, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 3, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 5, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 6, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 8, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 9, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 11, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 12, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 15, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 18, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 20, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 2, + nbBits = 5, + baseValue = 24, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 2, + nbBits = 5, + baseValue = 28, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 3, + nbBits = 5, + baseValue = 40, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 4, + nbBits = 5, + baseValue = 48, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 16, + nbBits = 6, + baseValue = 65536, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 15, + nbBits = 6, + baseValue = 32768, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 14, + nbBits = 6, + baseValue = 16384, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 13, + nbBits = 6, + baseValue = 8192, + }, + }); + + public static ZSTD_seqSymbol* OF_defaultDTable = GetArrayPointer(new ZSTD_seqSymbol[33] + { + new ZSTD_seqSymbol + { + nextState = 1, + nbAdditionalBits = 1, + nbBits = 1, + baseValue = 5, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 0, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 6, + nbBits = 4, + baseValue = 61, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 9, + nbBits = 5, + baseValue = 509, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 15, + nbBits = 5, + baseValue = 32765, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 21, + nbBits = 5, + baseValue = 2097149, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 3, + nbBits = 5, + baseValue = 5, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 7, + nbBits = 4, + baseValue = 125, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 12, + nbBits = 5, + baseValue = 4093, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 18, + nbBits = 5, + baseValue = 262141, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 23, + nbBits = 5, + baseValue = 8388605, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 5, + nbBits = 5, + baseValue = 29, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 8, + nbBits = 4, + baseValue = 253, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 14, + nbBits = 5, + baseValue = 16381, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 20, + nbBits = 5, + baseValue = 1048573, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 2, + nbBits = 5, + baseValue = 1, + }, + new ZSTD_seqSymbol + { + nextState = 16, + nbAdditionalBits = 7, + nbBits = 4, + baseValue = 125, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 11, + nbBits = 5, + baseValue = 2045, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 17, + nbBits = 5, + baseValue = 131069, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 22, + nbBits = 5, + baseValue = 4194301, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 4, + nbBits = 5, + baseValue = 13, + }, + new ZSTD_seqSymbol + { + nextState = 16, + nbAdditionalBits = 8, + nbBits = 4, + baseValue = 253, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 13, + nbBits = 5, + baseValue = 8189, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 19, + nbBits = 5, + baseValue = 524285, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 5, + baseValue = 1, + }, + new ZSTD_seqSymbol + { + nextState = 16, + nbAdditionalBits = 6, + nbBits = 4, + baseValue = 61, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 10, + nbBits = 5, + baseValue = 1021, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 16, + nbBits = 5, + baseValue = 65533, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 28, + nbBits = 5, + baseValue = 268435453, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 27, + nbBits = 5, + baseValue = 134217725, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 26, + nbBits = 5, + baseValue = 67108861, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 25, + nbBits = 5, + baseValue = 33554429, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 24, + nbBits = 5, + baseValue = 16777213, + }, + }); + + public static ZSTD_seqSymbol* ML_defaultDTable = GetArrayPointer(new ZSTD_seqSymbol[65] + { + new ZSTD_seqSymbol + { + nextState = 1, + nbAdditionalBits = 1, + nbBits = 1, + baseValue = 6, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 3, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 4, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 5, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 6, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 8, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 9, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 11, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 13, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 16, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 19, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 22, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 25, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 28, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 31, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 34, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 6, + baseValue = 37, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 6, + baseValue = 41, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 2, + nbBits = 6, + baseValue = 47, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 3, + nbBits = 6, + baseValue = 59, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 4, + nbBits = 6, + baseValue = 83, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 7, + nbBits = 6, + baseValue = 131, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 9, + nbBits = 6, + baseValue = 515, + }, + new ZSTD_seqSymbol + { + nextState = 16, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 4, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 5, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 6, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 7, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 9, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 10, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 12, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 15, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 18, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 21, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 24, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 27, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 30, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 33, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 6, + baseValue = 35, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 1, + nbBits = 6, + baseValue = 39, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 2, + nbBits = 6, + baseValue = 43, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 3, + nbBits = 6, + baseValue = 51, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 4, + nbBits = 6, + baseValue = 67, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 5, + nbBits = 6, + baseValue = 99, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 8, + nbBits = 6, + baseValue = 259, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 4, + }, + new ZSTD_seqSymbol + { + nextState = 48, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 4, + }, + new ZSTD_seqSymbol + { + nextState = 16, + nbAdditionalBits = 0, + nbBits = 4, + baseValue = 5, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 7, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 8, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 10, + }, + new ZSTD_seqSymbol + { + nextState = 32, + nbAdditionalBits = 0, + nbBits = 5, + baseValue = 11, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 14, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 17, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 20, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 23, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 26, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 29, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 0, + nbBits = 6, + baseValue = 32, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 16, + nbBits = 6, + baseValue = 65539, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 15, + nbBits = 6, + baseValue = 32771, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 14, + nbBits = 6, + baseValue = 16387, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 13, + nbBits = 6, + baseValue = 8195, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 12, + nbBits = 6, + baseValue = 4099, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 11, + nbBits = 6, + baseValue = 2051, + }, + new ZSTD_seqSymbol + { + nextState = 0, + nbAdditionalBits = 10, + nbBits = 6, + baseValue = 1027, + }, + }); + + private static void ZSTD_buildSeqTable_rle(ZSTD_seqSymbol* dt, uint baseValue, uint nbAddBits) + { + void* ptr = (void*)dt; + ZSTD_seqSymbol_header* DTableH = (ZSTD_seqSymbol_header*)(ptr); + ZSTD_seqSymbol* cell = dt + 1; + + DTableH->tableLog = 0; + DTableH->fastMode = 0; + cell->nbBits = 0; + cell->nextState = 0; + assert(nbAddBits < 255); + cell->nbAdditionalBits = (byte)(nbAddBits); + cell->baseValue = baseValue; + } + + /* ZSTD_buildFSETable() : + * generate FSE decoding table for one symbol (ll, ml or off) + * cannot fail if input is valid => + * all inputs are presumed validated at this stage */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_buildFSETable_body(ZSTD_seqSymbol* dt, short* normalizedCounter, uint maxSymbolValue, uint* baseValue, uint* nbAdditionalBits, uint tableLog, void* wksp, nuint wkspSize) + { + ZSTD_seqSymbol* tableDecode = dt + 1; + uint maxSV1 = maxSymbolValue + 1; + uint tableSize = (uint)(1 << (int)tableLog); + ushort* symbolNext = (ushort*)(wksp); + byte* spread = (byte*)(symbolNext + ((35) > (52) ? (35) : (52)) + 1); + uint highThreshold = tableSize - 1; + + assert(maxSymbolValue <= (uint)(((35) > (52) ? (35) : (52)))); + assert(tableLog <= (uint)(((((9) > (9) ? (9) : (9))) > (8) ? (((9) > (9) ? (9) : (9))) : (8)))); + assert(wkspSize >= ((nuint)(sizeof(short)) * (uint)((((35) > (52) ? (35) : (52)) + 1)) + (1U << ((((9) > (9) ? (9) : (9))) > (8) ? (((9) > (9) ? (9) : (9))) : (8))) + (nuint)(sizeof(ulong)))); + + { + ZSTD_seqSymbol_header DTableH; + + DTableH.tableLog = tableLog; + DTableH.fastMode = 1; + + { + short largeLimit = (short)(1 << (int)(tableLog - 1)); + uint s; + + for (s = 0; s < maxSV1; s++) + { + if (normalizedCounter[s] == -1) + { + tableDecode[highThreshold--].baseValue = s; + symbolNext[s] = 1; + } + else + { + if (normalizedCounter[s] >= largeLimit) + { + DTableH.fastMode = 0; + } + + assert(normalizedCounter[s] >= 0); + symbolNext[s] = (ushort)(normalizedCounter[s]); + } + } + } + + memcpy((void*)(dt), (void*)(&DTableH), ((nuint)(sizeof(ZSTD_seqSymbol_header)))); + } + + assert(tableSize <= 512); + if (highThreshold == tableSize - 1) + { + nuint tableMask = tableSize - 1; + nuint step = (((tableSize) >> 1) + ((tableSize) >> 3) + 3); + + + { + ulong add = 0x0101010101010101UL; + nuint pos = 0; + ulong sv = 0; + uint s; + + for (s = 0; s < maxSV1; ++s , sv += add) + { + int i; + int n = normalizedCounter[s]; + + MEM_write64((void*)(spread + pos), sv); + for (i = 8; i < n; i += 8) + { + MEM_write64((void*)(spread + pos + i), sv); + } + + pos += (nuint)n; + } + } + + + { + nuint position = 0; + nuint s; + nuint unroll = 2; + + assert(tableSize % unroll == 0); + for (s = 0; s < (nuint)(tableSize); s += unroll) + { + nuint u; + + for (u = 0; u < unroll; ++u) + { + nuint uPosition = (position + (u * step)) & tableMask; + + tableDecode[uPosition].baseValue = spread[s + u]; + } + + position = (position + (unroll * step)) & tableMask; + } + + assert(position == 0); + } + } + else + { + uint tableMask = tableSize - 1; + uint step = (((tableSize) >> 1) + ((tableSize) >> 3) + 3); + uint s, position = 0; + + for (s = 0; s < maxSV1; s++) + { + int i; + int n = normalizedCounter[s]; + + for (i = 0; i < n; i++) + { + tableDecode[position].baseValue = s; + position = (position + step) & tableMask; + while (position > highThreshold) + { + position = (position + step) & tableMask; + } + } + } + + assert(position == 0); + } + + + { + uint u; + + for (u = 0; u < tableSize; u++) + { + uint symbol = tableDecode[u].baseValue; + uint nextState = symbolNext[symbol]++; + + tableDecode[u].nbBits = (byte)(tableLog - BIT_highbit32(nextState)); + tableDecode[u].nextState = (ushort)((nextState << (int)(tableDecode[u].nbBits)) - tableSize); + assert(nbAdditionalBits[symbol] < 255); + tableDecode[u].nbAdditionalBits = (byte)(nbAdditionalBits[symbol]); + tableDecode[u].baseValue = baseValue[symbol]; + } + } + } + + /* Avoids the FORCE_INLINE of the _body() function. */ + private static void ZSTD_buildFSETable_body_default(ZSTD_seqSymbol* dt, short* normalizedCounter, uint maxSymbolValue, uint* baseValue, uint* nbAdditionalBits, uint tableLog, void* wksp, nuint wkspSize) + { + ZSTD_buildFSETable_body(dt, normalizedCounter, maxSymbolValue, baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); + } + + private static void ZSTD_buildFSETable_body_bmi2(ZSTD_seqSymbol* dt, short* normalizedCounter, uint maxSymbolValue, uint* baseValue, uint* nbAdditionalBits, uint tableLog, void* wksp, nuint wkspSize) + { + ZSTD_buildFSETable_body(dt, normalizedCounter, maxSymbolValue, baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); + } + + /* ZSTD_buildFSETable() : + * generate FSE decoding table for one symbol (ll, ml or off) + * this function must be called with valid parameters only + * (dt is large enough, normalizedCounter distribution total is a power of 2, max is within range, etc.) + * in which case it cannot fail. + * The workspace must be 4-byte aligned and at least ZSTD_BUILD_FSE_TABLE_WKSP_SIZE bytes, which is + * defined in zstd_decompress_internal.h. + * Internal use only. + */ + public static void ZSTD_buildFSETable(ZSTD_seqSymbol* dt, short* normalizedCounter, uint maxSymbolValue, uint* baseValue, uint* nbAdditionalBits, uint tableLog, void* wksp, nuint wkspSize, int bmi2) + { + if (bmi2 != 0) + { + ZSTD_buildFSETable_body_bmi2(dt, normalizedCounter, maxSymbolValue, baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); + return; + } + + ZSTD_buildFSETable_body_default(dt, normalizedCounter, maxSymbolValue, baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); + } + + /*! ZSTD_buildSeqTable() : + * @return : nb bytes read from src, + * or an error code if it fails */ + private static nuint ZSTD_buildSeqTable(ZSTD_seqSymbol* DTableSpace, ZSTD_seqSymbol** DTablePtr, symbolEncodingType_e type, uint max, uint maxLog, void* src, nuint srcSize, uint* baseValue, uint* nbAdditionalBits, ZSTD_seqSymbol* defaultTable, uint flagRepeatTable, int ddictIsCold, int nbSeq, uint* wksp, nuint wkspSize, int bmi2) + { + switch (type) + { + case symbolEncodingType_e.set_rle: + { + if (srcSize == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + } + + if ((*(byte*)(src)) > max) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + uint symbol = *(byte*)(src); + uint baseline = baseValue[symbol]; + uint nbBits = nbAdditionalBits[symbol]; + + ZSTD_buildSeqTable_rle(DTableSpace, baseline, nbBits); + } + + *DTablePtr = DTableSpace; + return 1; + case symbolEncodingType_e.set_basic: + { + *DTablePtr = defaultTable; + } + + return 0; + case symbolEncodingType_e.set_repeat: + { + if (flagRepeatTable == 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + } + + if (ddictIsCold != 0 && (nbSeq > 24)) + { + void* pStart = (void*)*DTablePtr; + nuint pSize = (nuint)(8) * (uint)(((1 + (1 << (int)(maxLog))))); + + + { + sbyte* _ptr = (sbyte*)(pStart); + nuint _size = (nuint)(pSize); + nuint _pos; + + for (_pos = 0; _pos < _size; _pos += 64) + { + Prefetch1((void*)(_ptr + _pos)); + } + } + + } + + return 0; + case symbolEncodingType_e.set_compressed: + { + uint tableLog; + short* norm = stackalloc short[53]; + nuint headerSize = FSE_readNCount((short*)norm, &max, &tableLog, src, srcSize); + + if ((ERR_isError(headerSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (tableLog > maxLog) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + ZSTD_buildFSETable(DTableSpace, (short*)norm, max, baseValue, nbAdditionalBits, tableLog, (void*)wksp, wkspSize, bmi2); + *DTablePtr = DTableSpace; + return headerSize; + } + + default: + { + assert(0 != 0); + } + + + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + } + + } + } + + /*! ZSTD_decodeSeqHeaders() : + * decode sequence header from src */ + /* Used by: decompress, fullbench (does not get its definition from here) */ + public static nuint ZSTD_decodeSeqHeaders(ZSTD_DCtx_s* dctx, int* nbSeqPtr, void* src, nuint srcSize) + { + byte* istart = (byte*)(src); + byte* iend = istart + srcSize; + byte* ip = istart; + int nbSeq; + + if (srcSize < 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + nbSeq = (int)*ip++; + if (nbSeq == 0) + { + *nbSeqPtr = 0; + if (srcSize != 1) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + return 1; + } + + if (nbSeq > 0x7F) + { + if (nbSeq == 0xFF) + { + if (ip + 2 > iend) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + nbSeq = MEM_readLE16((void*)ip) + 0x7F00; + ip += 2; + } + else + { + if (ip >= iend) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + nbSeq = ((nbSeq - 0x80) << 8) + *ip++; + } + } + + *nbSeqPtr = nbSeq; + if (ip + 1 > iend) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + + { + symbolEncodingType_e LLtype = (symbolEncodingType_e)(*ip >> 6); + symbolEncodingType_e OFtype = (symbolEncodingType_e)((*ip >> 4) & 3); + symbolEncodingType_e MLtype = (symbolEncodingType_e)((*ip >> 2) & 3); + + ip++; + + { + nuint llhSize = ZSTD_buildSeqTable((ZSTD_seqSymbol*)dctx->entropy.LLTable, &dctx->LLTptr, LLtype, 35, 9, (void*)ip, (nuint)(iend - ip), (uint*)LL_base, (uint*)LL_bits, (ZSTD_seqSymbol*)LL_defaultDTable, dctx->fseEntropy, dctx->ddictIsCold, nbSeq, (uint*)dctx->workspace, (nuint)(2560), dctx->bmi2); + + if ((ERR_isError(llhSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + ip += llhSize; + } + + + { + nuint ofhSize = ZSTD_buildSeqTable((ZSTD_seqSymbol*)dctx->entropy.OFTable, &dctx->OFTptr, OFtype, 31, 8, (void*)ip, (nuint)(iend - ip), (uint*)OF_base, (uint*)OF_bits, (ZSTD_seqSymbol*)OF_defaultDTable, dctx->fseEntropy, dctx->ddictIsCold, nbSeq, (uint*)dctx->workspace, (nuint)(2560), dctx->bmi2); + + if ((ERR_isError(ofhSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + ip += ofhSize; + } + + + { + nuint mlhSize = ZSTD_buildSeqTable((ZSTD_seqSymbol*)dctx->entropy.MLTable, &dctx->MLTptr, MLtype, 52, 9, (void*)ip, (nuint)(iend - ip), (uint*)ML_base, (uint*)ML_bits, (ZSTD_seqSymbol*)ML_defaultDTable, dctx->fseEntropy, dctx->ddictIsCold, nbSeq, (uint*)dctx->workspace, (nuint)(2560), dctx->bmi2); + + if ((ERR_isError(mlhSize)) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + ip += mlhSize; + } + } + + return (nuint)(ip - istart); + } + + /*! ZSTD_overlapCopy8() : + * Copies 8 bytes from ip to op and updates op and ip where ip <= op. + * If the offset is < 8 then the offset is spread to at least 8 bytes. + * + * Precondition: *ip <= *op + * Postcondition: *op - *op >= 8 + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_overlapCopy8(byte** op, byte** ip, nuint offset) + { + assert(*ip <= *op); + if (offset < 8) + { + + + int sub2 = dec64table[offset]; + + (*op)[0] = (*ip)[0]; + (*op)[1] = (*ip)[1]; + (*op)[2] = (*ip)[2]; + (*op)[3] = (*ip)[3]; + *ip += dec32table[offset]; + ZSTD_copy4((void*)(*op + 4), (void*)*ip); + *ip -= sub2; + } + else + { + ZSTD_copy8((void*)*op, (void*)*ip); + } + + *ip += 8; + *op += 8; + assert(*op - *ip >= 8); + } + + /*! ZSTD_safecopy() : + * Specialized version of memcpy() that is allowed to READ up to WILDCOPY_OVERLENGTH past the input buffer + * and write up to 16 bytes past oend_w (op >= oend_w is allowed). + * This function is only called in the uncommon case where the sequence is near the end of the block. It + * should be fast for a single long sequence, but can be slow for several short sequences. + * + * @param ovtype controls the overlap detection + * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart. + * - ZSTD_overlap_src_before_dst: The src and dst may overlap and may be any distance apart. + * The src buffer must be before the dst buffer. + */ + private static void ZSTD_safecopy(byte* op, byte* oend_w, byte* ip, nint length, ZSTD_overlap_e ovtype) + { + nint diff = (nint)(op - ip); + byte* oend = op + length; + + assert((ovtype == ZSTD_overlap_e.ZSTD_no_overlap && (diff <= -8 || diff >= 8 || op >= oend_w)) || (ovtype == ZSTD_overlap_e.ZSTD_overlap_src_before_dst && diff >= 0)); + if (length < 8) + { + while (op < oend) + { + *op++ = *ip++; + } + + return; + } + + if (ovtype == ZSTD_overlap_e.ZSTD_overlap_src_before_dst) + { + assert(length >= 8); + ZSTD_overlapCopy8(&op, &ip, (nuint)diff); + assert(op - ip >= 8); + assert(op <= oend); + } + + if (oend <= oend_w) + { + ZSTD_wildcopy((void*)op, (void*)ip, length, ovtype); + return; + } + + if (op <= oend_w) + { + assert(oend > oend_w); + ZSTD_wildcopy((void*)op, (void*)ip, (nint)(oend_w - op), ovtype); + ip += oend_w - op; + op = oend_w; + } + + while (op < oend) + { + *op++ = *ip++; + } + } + + /* ZSTD_execSequenceEnd(): + * This version handles cases that are near the end of the output buffer. It requires + * more careful checks to make sure there is no overflow. By separating out these hard + * and unlikely cases, we can speed up the common cases. + * + * NOTE: This function needs to be fast for a single long sequence, but doesn't need + * to be optimized for many small sequences, since those fall into ZSTD_execSequence(). + */ + private static nuint ZSTD_execSequenceEnd(byte* op, byte* oend, seq_t sequence, byte** litPtr, byte* litLimit, byte* prefixStart, byte* virtualStart, byte* dictEnd) + { + byte* oLitEnd = op + sequence.litLength; + nuint sequenceLength = sequence.litLength + sequence.matchLength; + byte* iLitEnd = *litPtr + sequence.litLength; + byte* match = oLitEnd - sequence.offset; + byte* oend_w = oend - 32; + + if (sequenceLength > (nuint)(oend - op)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (sequence.litLength > (nuint)(litLimit - *litPtr)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + assert(op < op + sequenceLength); + assert(oLitEnd < op + sequenceLength); + ZSTD_safecopy(op, oend_w, *litPtr, (nint)sequence.litLength, ZSTD_overlap_e.ZSTD_no_overlap); + op = oLitEnd; + *litPtr = iLitEnd; + if (sequence.offset > (nuint)(oLitEnd - prefixStart)) + { + if (sequence.offset > (nuint)(oLitEnd - virtualStart)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + match = dictEnd - (prefixStart - match); + if (match + sequence.matchLength <= dictEnd) + { + memmove((void*)(oLitEnd), (void*)(match), (sequence.matchLength)); + return sequenceLength; + } + + + { + nuint length1 = (nuint)(dictEnd - match); + + memmove((void*)(oLitEnd), (void*)(match), (length1)); + op = oLitEnd + length1; + sequence.matchLength -= length1; + match = prefixStart; + } + } + + ZSTD_safecopy(op, oend_w, match, (nint)sequence.matchLength, ZSTD_overlap_e.ZSTD_overlap_src_before_dst); + return sequenceLength; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_execSequence(byte* op, byte* oend, seq_t sequence, byte** litPtr, byte* litLimit, byte* prefixStart, byte* virtualStart, byte* dictEnd) + { + byte* oLitEnd = op + sequence.litLength; + nuint sequenceLength = sequence.litLength + sequence.matchLength; + byte* oMatchEnd = op + sequenceLength; + byte* oend_w = oend - 32; + byte* iLitEnd = *litPtr + sequence.litLength; + byte* match = oLitEnd - sequence.offset; + + assert(op != null); + assert(oend_w < oend); + if ((iLitEnd > litLimit || oMatchEnd > oend_w || (MEM_32bits && (nuint)(oend - op) < sequenceLength + 32))) + { + return ZSTD_execSequenceEnd(op, oend, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd); + } + + assert(op <= oLitEnd); + assert(oLitEnd < oMatchEnd); + assert(oMatchEnd <= oend); + assert(iLitEnd <= litLimit); + assert(oLitEnd <= oend_w); + assert(oMatchEnd <= oend_w); + assert(32 >= 16); + ZSTD_copy16((void*)op, (void*)(*litPtr)); + if ((sequence.litLength > 16)) + { + ZSTD_wildcopy((void*)(op + 16), (void*)((*litPtr) + 16), (nint)(sequence.litLength - 16), ZSTD_overlap_e.ZSTD_no_overlap); + } + + op = oLitEnd; + *litPtr = iLitEnd; + if (sequence.offset > (nuint)(oLitEnd - prefixStart)) + { + if ((sequence.offset > (nuint)(oLitEnd - virtualStart))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + match = dictEnd + (match - prefixStart); + if (match + sequence.matchLength <= dictEnd) + { + memmove((void*)(oLitEnd), (void*)(match), (sequence.matchLength)); + return sequenceLength; + } + + + { + nuint length1 = (nuint)(dictEnd - match); + + memmove((void*)(oLitEnd), (void*)(match), (length1)); + op = oLitEnd + length1; + sequence.matchLength -= length1; + match = prefixStart; + } + } + + assert(op <= oMatchEnd); + assert(oMatchEnd <= oend_w); + assert(match >= prefixStart); + assert(sequence.matchLength >= 1); + if ((sequence.offset >= 16)) + { + ZSTD_wildcopy((void*)op, (void*)match, (nint)(sequence.matchLength), ZSTD_overlap_e.ZSTD_no_overlap); + return sequenceLength; + } + + assert(sequence.offset < 16); + ZSTD_overlapCopy8(&op, &match, sequence.offset); + if (sequence.matchLength > 8) + { + assert(op < oMatchEnd); + ZSTD_wildcopy((void*)op, (void*)match, (nint)(sequence.matchLength) - 8, ZSTD_overlap_e.ZSTD_overlap_src_before_dst); + } + + return sequenceLength; + } + + private static void ZSTD_initFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, ZSTD_seqSymbol* dt) + { + void* ptr = (void*)dt; + ZSTD_seqSymbol_header* DTableH = (ZSTD_seqSymbol_header*)(ptr); + + DStatePtr->state = BIT_readBits(bitD, DTableH->tableLog); + BIT_reloadDStream(bitD); + DStatePtr->table = dt + 1; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_updateFseState(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD) + { + ZSTD_seqSymbol DInfo = DStatePtr->table[DStatePtr->state]; + uint nbBits = DInfo.nbBits; + nuint lowBits = BIT_readBits(bitD, nbBits); + + DStatePtr->state = DInfo.nextState + lowBits; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_updateFseStateWithDInfo(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, ZSTD_seqSymbol DInfo) + { + uint nbBits = DInfo.nbBits; + nuint lowBits = BIT_readBits(bitD, nbBits); + + DStatePtr->state = DInfo.nextState + lowBits; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static seq_t ZSTD_decodeSequence(seqState_t* seqState, ZSTD_longOffset_e longOffsets) + { + seq_t seq; + var _ = &seq; + ZSTD_seqSymbol llDInfo = seqState->stateLL.table[seqState->stateLL.state]; + ZSTD_seqSymbol mlDInfo = seqState->stateML.table[seqState->stateML.state]; + ZSTD_seqSymbol ofDInfo = seqState->stateOffb.table[seqState->stateOffb.state]; + uint llBase = llDInfo.baseValue; + uint mlBase = mlDInfo.baseValue; + uint ofBase = ofDInfo.baseValue; + byte llBits = llDInfo.nbAdditionalBits; + byte mlBits = mlDInfo.nbAdditionalBits; + byte ofBits = ofDInfo.nbAdditionalBits; + byte totalBits = (byte)(llBits + mlBits + ofBits); + + + { + nuint offset; + + if (ofBits > 1) + { + assert(ofBits <= 31); + if (MEM_32bits && longOffsets != default && (ofBits >= 25)) + { + uint extraBits = ofBits - ((ofBits) < (32 - seqState->DStream.bitsConsumed) ? (ofBits) : (32 - seqState->DStream.bitsConsumed)); + + offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << (int)extraBits); + BIT_reloadDStream(&seqState->DStream); + if (extraBits != 0) + { + offset += BIT_readBitsFast(&seqState->DStream, extraBits); + } + + assert(extraBits <= (uint)((30 > 25 ? 30 - 25 : 0))); + } + else + { + offset = ofBase + BIT_readBitsFast(&seqState->DStream, ofBits); + if (MEM_32bits) + { + BIT_reloadDStream(&seqState->DStream); + } + } + + seqState->prevOffset[2] = seqState->prevOffset[1]; + seqState->prevOffset[1] = seqState->prevOffset[0]; + seqState->prevOffset[0] = offset; + } + else + { + uint ll0 = (((llBase == 0)) ? 1U : 0U); + + if (((ofBits == 0))) + { + if (ll0 == 0) + { + offset = seqState->prevOffset[0]; + } + else + { + offset = seqState->prevOffset[1]; + seqState->prevOffset[1] = seqState->prevOffset[0]; + seqState->prevOffset[0] = offset; + } + } + else + { + offset = ofBase + ll0 + BIT_readBitsFast(&seqState->DStream, 1); + + { + nuint temp = (offset == 3) ? seqState->prevOffset[0] - 1 : seqState->prevOffset[offset]; + + temp += (temp == 0 ? 1U : 0U); + if (offset != 1) + { + seqState->prevOffset[2] = seqState->prevOffset[1]; + } + + seqState->prevOffset[1] = seqState->prevOffset[0]; + seqState->prevOffset[0] = offset = temp; + } + } + } + + seq.offset = offset; + } + + seq.matchLength = mlBase; + if (mlBits > 0) + { + seq.matchLength += BIT_readBitsFast(&seqState->DStream, mlBits); + } + + if (MEM_32bits && (mlBits + llBits >= 25 - (30 > 25 ? 30 - 25 : 0))) + { + BIT_reloadDStream(&seqState->DStream); + } + + if (MEM_64bits && (totalBits >= 57 - (9 + 9 + 8))) + { + BIT_reloadDStream(&seqState->DStream); + } + + seq.litLength = llBase; + if (llBits > 0) + { + seq.litLength += BIT_readBitsFast(&seqState->DStream, llBits); + } + + if (MEM_32bits) + { + BIT_reloadDStream(&seqState->DStream); + } + + + { + int kUseUpdateFseState = 0; + + if (kUseUpdateFseState != 0) + { + ZSTD_updateFseState(&seqState->stateLL, &seqState->DStream); + ZSTD_updateFseState(&seqState->stateML, &seqState->DStream); + if (MEM_32bits) + { + BIT_reloadDStream(&seqState->DStream); + } + + ZSTD_updateFseState(&seqState->stateOffb, &seqState->DStream); + } + else + { + ZSTD_updateFseStateWithDInfo(&seqState->stateLL, &seqState->DStream, llDInfo); + ZSTD_updateFseStateWithDInfo(&seqState->stateML, &seqState->DStream, mlDInfo); + if (MEM_32bits) + { + BIT_reloadDStream(&seqState->DStream); + } + + ZSTD_updateFseStateWithDInfo(&seqState->stateOffb, &seqState->DStream, ofDInfo); + } + } + + return seq; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_decompressSequences_body(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame) + { + byte* ip = (byte*)(seqStart); + byte* iend = ip + seqSize; + byte* ostart = (byte*)(dst); + byte* oend = ostart + maxDstSize; + byte* op = ostart; + byte* litPtr = dctx->litPtr; + byte* litEnd = litPtr + dctx->litSize; + byte* prefixStart = (byte*)(dctx->prefixStart); + byte* vBase = (byte*)(dctx->virtualStart); + byte* dictEnd = (byte*)(dctx->dictEnd); + + if (nbSeq != 0) + { + seqState_t seqState; + var _ = &seqState; + + dctx->fseEntropy = 1; + + { + uint i; + + for (i = 0; i < 3; i++) + { + seqState.prevOffset[i] = dctx->entropy.rep[i]; + } + } + + if ((ERR_isError(BIT_initDStream(&seqState.DStream, (void*)ip, (nuint)(iend - ip)))) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); + ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); + ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); + assert(dst != null); + for (;;) + { + seq_t sequence = ZSTD_decodeSequence(&seqState, isLongOffset); + nuint oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litEnd, prefixStart, vBase, dictEnd); + + if ((ERR_isError(oneSeqSize)) != 0) + { + return oneSeqSize; + } + + op += oneSeqSize; + if (--nbSeq == 0) + { + break; + } + + BIT_reloadDStream(&(seqState.DStream)); + } + + if (nbSeq != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + if (BIT_reloadDStream(&seqState.DStream) < BIT_DStream_status.BIT_DStream_completed) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + + { + uint i; + + for (i = 0; i < 3; i++) + { + dctx->entropy.rep[i] = (uint)(seqState.prevOffset[i]); + } + } + } + + + { + nuint lastLLSize = (nuint)(litEnd - litPtr); + + if (lastLLSize > (nuint)(oend - op)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (op != null) + { + memcpy((void*)(op), (void*)(litPtr), (lastLLSize)); + op += lastLLSize; + } + } + + return (nuint)(op - ostart); + } + + private static nuint ZSTD_decompressSequences_default(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame) + { + return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_prefetchMatch(nuint prefetchPos, seq_t sequence, byte* prefixStart, byte* dictEnd) + { + prefetchPos += sequence.litLength; + + { + byte* matchBase = (sequence.offset > prefetchPos) ? dictEnd : prefixStart; + byte* match = matchBase + prefetchPos - sequence.offset; + + Prefetch0((void*)match); + Prefetch0((void*)(match + 64)); + } + + return prefetchPos + sequence.matchLength; + } + + /* This decoding function employs prefetching + * to reduce latency impact of cache misses. + * It's generally employed when block contains a significant portion of long-distance matches + * or when coupled with a "cold" dictionary */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_decompressSequencesLong_body(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame) + { + byte* ip = (byte*)(seqStart); + byte* iend = ip + seqSize; + byte* ostart = (byte*)(dst); + byte* oend = ostart + maxDstSize; + byte* op = ostart; + byte* litPtr = dctx->litPtr; + byte* litEnd = litPtr + dctx->litSize; + byte* prefixStart = (byte*)(dctx->prefixStart); + byte* dictStart = (byte*)(dctx->virtualStart); + byte* dictEnd = (byte*)(dctx->dictEnd); + + if (nbSeq != 0) + { + seq_t* sequences = stackalloc seq_t[8]; + int seqAdvance = ((nbSeq) < (8) ? (nbSeq) : (8)); + seqState_t seqState; + var _ = &seqState; + int seqNb; + nuint prefetchPos = (nuint)(op - prefixStart); + + dctx->fseEntropy = 1; + + { + int i; + + for (i = 0; i < 3; i++) + { + seqState.prevOffset[i] = dctx->entropy.rep[i]; + } + } + + assert(dst != null); + assert(iend >= ip); + if ((ERR_isError(BIT_initDStream(&seqState.DStream, (void*)ip, (nuint)(iend - ip)))) != 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + ZSTD_initFseState(&seqState.stateLL, &seqState.DStream, dctx->LLTptr); + ZSTD_initFseState(&seqState.stateOffb, &seqState.DStream, dctx->OFTptr); + ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); + for (seqNb = 0; (BIT_reloadDStream(&seqState.DStream) <= BIT_DStream_status.BIT_DStream_completed) && (seqNb < seqAdvance); seqNb++) + { + seq_t sequence = ZSTD_decodeSequence(&seqState, isLongOffset); + + prefetchPos = ZSTD_prefetchMatch(prefetchPos, sequence, prefixStart, dictEnd); + sequences[seqNb] = sequence; + } + + if (seqNb < seqAdvance) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + for (; (BIT_reloadDStream(&(seqState.DStream)) <= BIT_DStream_status.BIT_DStream_completed) && (seqNb < nbSeq); seqNb++) + { + seq_t sequence = ZSTD_decodeSequence(&seqState, isLongOffset); + nuint oneSeqSize = ZSTD_execSequence(op, oend, sequences[(seqNb - 8) & (8 - 1)], &litPtr, litEnd, prefixStart, dictStart, dictEnd); + + if ((ERR_isError(oneSeqSize)) != 0) + { + return oneSeqSize; + } + + prefetchPos = ZSTD_prefetchMatch(prefetchPos, sequence, prefixStart, dictEnd); + sequences[seqNb & (8 - 1)] = sequence; + op += oneSeqSize; + } + + if (seqNb < nbSeq) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected))); + } + + seqNb -= seqAdvance; + for (; seqNb < nbSeq; seqNb++) + { + nuint oneSeqSize = ZSTD_execSequence(op, oend, sequences[seqNb & (8 - 1)], &litPtr, litEnd, prefixStart, dictStart, dictEnd); + + if ((ERR_isError(oneSeqSize)) != 0) + { + return oneSeqSize; + } + + op += oneSeqSize; + } + + + { + uint i; + + for (i = 0; i < 3; i++) + { + dctx->entropy.rep[i] = (uint)(seqState.prevOffset[i]); + } + } + } + + + { + nuint lastLLSize = (nuint)(litEnd - litPtr); + + if (lastLLSize > (nuint)(oend - op)) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (op != null) + { + memcpy((void*)(op), (void*)(litPtr), (lastLLSize)); + op += lastLLSize; + } + } + + return (nuint)(op - ostart); + } + + private static nuint ZSTD_decompressSequencesLong_default(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame) + { + return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame); + } + + private static nuint ZSTD_decompressSequences_bmi2(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame) + { + return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame); + } + + private static nuint ZSTD_decompressSequencesLong_bmi2(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame) + { + return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame); + } + + private static nuint ZSTD_decompressSequences(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame) + { + if (dctx->bmi2 != 0) + { + return ZSTD_decompressSequences_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame); + } + + return ZSTD_decompressSequences_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame); + } + + /* ZSTD_decompressSequencesLong() : + * decompression function triggered when a minimum share of offsets is considered "long", + * aka out of cache. + * note : "long" definition seems overloaded here, sometimes meaning "wider than bitstream register", and sometimes meaning "farther than memory cache distance". + * This function will try to mitigate main memory latency through the use of prefetching */ + private static nuint ZSTD_decompressSequencesLong(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset, int frame) + { + if (dctx->bmi2 != 0) + { + return ZSTD_decompressSequencesLong_bmi2(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame); + } + + return ZSTD_decompressSequencesLong_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset, frame); + } + + /* ZSTD_getLongOffsetsShare() : + * condition : offTable must be valid + * @return : "share" of long offsets (arbitrarily defined as > (1<<23)) + * compared to maximum possible of (1< 22) + { + total += 1; + } + } + + assert(tableLog <= 8); + total <<= (int)(8 - tableLog); + return total; + } + + /* ZSTD_decompressBlock_internal() : + * decompress block, starting at `src`, + * into destination buffer `dst`. + * @return : decompressed block size, + * or an error code (which can be tested using ZSTD_isError()) + */ + public static nuint ZSTD_decompressBlock_internal(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, int frame) + { + byte* ip = (byte*)(src); + ZSTD_longOffset_e isLongOffset = (ZSTD_longOffset_e)((MEM_32bits && (frame == 0 || (dctx->fParams.windowSize > (1UL << (int)((uint)(MEM_32bits ? 25 : 57)))))) ? 1 : 0); + + if (srcSize >= (uint)((1 << 17))) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + } + + + { + nuint litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize); + + if ((ERR_isError(litCSize)) != 0) + { + return litCSize; + } + + ip += litCSize; + srcSize -= litCSize; + } + + + { + int usePrefetchDecoder = dctx->ddictIsCold; + int nbSeq; + nuint seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, (void*)ip, srcSize); + + if ((ERR_isError(seqHSize)) != 0) + { + return seqHSize; + } + + ip += seqHSize; + srcSize -= seqHSize; + if (dst == null && nbSeq > 0) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + if (usePrefetchDecoder == 0 && (frame == 0 || (dctx->fParams.windowSize > (uint)((1 << 24)))) && (nbSeq > 8)) + { + uint shareLongOffsets = ZSTD_getLongOffsetsShare(dctx->OFTptr); + uint minShare = (uint)(MEM_64bits ? 7 : 20); + + usePrefetchDecoder = ((shareLongOffsets >= minShare) ? 1 : 0); + } + + dctx->ddictIsCold = 0; + if (usePrefetchDecoder != 0) + { + return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, (void*)ip, srcSize, nbSeq, isLongOffset, frame); + } + + return ZSTD_decompressSequences(dctx, dst, dstCapacity, (void*)ip, srcSize, nbSeq, isLongOffset, frame); + } + } + + /*! ZSTD_checkContinuity() : + * check if next `dst` follows previous position, where decompression ended. + * If yes, do nothing (continue on current segment). + * If not, classify previous segment as "external dictionary", and start a new segment. + * This function cannot fail. */ + public static void ZSTD_checkContinuity(ZSTD_DCtx_s* dctx, void* dst, nuint dstSize) + { + if (dst != dctx->previousDstEnd && dstSize > 0) + { + dctx->dictEnd = dctx->previousDstEnd; + dctx->virtualStart = (sbyte*)(dst) - ((sbyte*)(dctx->previousDstEnd) - (sbyte*)(dctx->prefixStart)); + dctx->prefixStart = dst; + dctx->previousDstEnd = dst; + } + } + + public static nuint ZSTD_decompressBlock(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + nuint dSize; + + ZSTD_checkContinuity(dctx, dst, dstCapacity); + dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, 0); + dctx->previousDstEnd = (sbyte*)(dst) + dSize; + return dSize; + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompressInternal.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompressInternal.cs new file mode 100644 index 00000000..7053d444 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDecompressInternal.cs @@ -0,0 +1,177 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static uint* LL_base = GetArrayPointer(new uint[36] + { + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 18, + 20, + 22, + 24, + 28, + 32, + 40, + 48, + 64, + 0x80, + 0x100, + 0x200, + 0x400, + 0x800, + 0x1000, + 0x2000, + 0x4000, + 0x8000, + 0x10000, + }); + + public static uint* OF_base = GetArrayPointer(new uint[32] + { + 0, + 1, + 1, + 5, + 0xD, + 0x1D, + 0x3D, + 0x7D, + 0xFD, + 0x1FD, + 0x3FD, + 0x7FD, + 0xFFD, + 0x1FFD, + 0x3FFD, + 0x7FFD, + 0xFFFD, + 0x1FFFD, + 0x3FFFD, + 0x7FFFD, + 0xFFFFD, + 0x1FFFFD, + 0x3FFFFD, + 0x7FFFFD, + 0xFFFFFD, + 0x1FFFFFD, + 0x3FFFFFD, + 0x7FFFFFD, + 0xFFFFFFD, + 0x1FFFFFFD, + 0x3FFFFFFD, + 0x7FFFFFFD, + }); + + public static uint* OF_bits = GetArrayPointer(new uint[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, + }); + + public static uint* ML_base = GetArrayPointer(new uint[53] + { + 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, + 33, + 34, + 35, + 37, + 39, + 41, + 43, + 47, + 51, + 59, + 67, + 83, + 99, + 0x83, + 0x103, + 0x203, + 0x403, + 0x803, + 0x1003, + 0x2003, + 0x4003, + 0x8003, + 0x10003, + }); + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDoubleFast.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDoubleFast.cs new file mode 100644 index 00000000..803dd677 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdDoubleFast.cs @@ -0,0 +1,606 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms, void* end, ZSTD_dictTableLoadMethod_e dtlm) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashLarge = ms->hashTable; + uint hBitsL = cParams->hashLog; + uint mls = cParams->minMatch; + uint* hashSmall = ms->chainTable; + uint hBitsS = cParams->chainLog; + byte* @base = ms->window.@base; + byte* ip = @base + ms->nextToUpdate; + byte* iend = ((byte*)(end)) - 8; + uint fastHashFillStep = 3; + + for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) + { + uint curr = (uint)(ip - @base); + uint i; + + for (i = 0; i < fastHashFillStep; ++i) + { + nuint smHash = ZSTD_hashPtr((void*)(ip + i), hBitsS, mls); + nuint lgHash = ZSTD_hashPtr((void*)(ip + i), hBitsL, 8); + + if (i == 0) + { + hashSmall[smHash] = curr + i; + } + + if (i == 0 || hashLarge[lgHash] == 0) + { + hashLarge[lgHash] = curr + i; + } + + if (dtlm == ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast) + { + break; + } + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_compressBlock_doubleFast_generic(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls, ZSTD_dictMode_e dictMode) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashLong = ms->hashTable; + uint hBitsL = cParams->hashLog; + uint* hashSmall = ms->chainTable; + uint hBitsS = cParams->chainLog; + byte* @base = ms->window.@base; + byte* istart = (byte*)(src); + byte* ip = istart; + byte* anchor = istart; + uint endIndex = (uint)((nuint)(istart - @base) + srcSize); + uint prefixLowestIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog); + byte* prefixLowest = @base + prefixLowestIndex; + byte* iend = istart + srcSize; + byte* ilimit = iend - 8; + uint offset_1 = rep[0], offset_2 = rep[1]; + uint offsetSaved = 0; + ZSTD_matchState_t* dms = ms->dictMatchState; + ZSTD_compressionParameters* dictCParams = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? &dms->cParams : null; + uint* dictHashLong = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dms->hashTable : null; + uint* dictHashSmall = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dms->chainTable : null; + uint dictStartIndex = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dms->window.dictLimit : 0; + byte* dictBase = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dms->window.@base : null; + byte* dictStart = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dictBase + dictStartIndex : null; + byte* dictEnd = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dms->window.nextSrc : null; + uint dictIndexDelta = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? prefixLowestIndex - (uint)(dictEnd - dictBase) : 0; + uint dictHBitsL = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dictCParams->hashLog : hBitsL; + uint dictHBitsS = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dictCParams->chainLog : hBitsS; + uint dictAndPrefixLength = (uint)((ip - prefixLowest) + (dictEnd - dictStart)); + + assert(dictMode == ZSTD_dictMode_e.ZSTD_noDict || dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState); + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + assert(ms->window.dictLimit + (1U << (int)cParams->windowLog) >= endIndex); + } + + ip += ((dictAndPrefixLength == 0) ? 1 : 0); + if (dictMode == ZSTD_dictMode_e.ZSTD_noDict) + { + uint curr = (uint)(ip - @base); + uint windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog); + uint maxRep = curr - windowLow; + + if (offset_2 > maxRep) + { + offsetSaved = offset_2; offset_2 = 0; + } + + if (offset_1 > maxRep) + { + offsetSaved = offset_1; offset_1 = 0; + } + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + assert(offset_1 <= dictAndPrefixLength); + assert(offset_2 <= dictAndPrefixLength); + } + + while (ip < ilimit) + { + nuint mLength; + uint offset; + nuint h2 = ZSTD_hashPtr((void*)ip, hBitsL, 8); + nuint h = ZSTD_hashPtr((void*)ip, hBitsS, mls); + nuint dictHL = ZSTD_hashPtr((void*)ip, dictHBitsL, 8); + nuint dictHS = ZSTD_hashPtr((void*)ip, dictHBitsS, mls); + uint curr = (uint)(ip - @base); + uint matchIndexL = hashLong[h2]; + uint matchIndexS = hashSmall[h]; + byte* matchLong = @base + matchIndexL; + byte* match = @base + matchIndexS; + uint repIndex = curr + 1 - offset_1; + byte* repMatch = (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && repIndex < prefixLowestIndex) ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; + + hashLong[h2] = hashSmall[h] = curr; + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && ((uint)((prefixLowestIndex - 1) - repIndex) >= 3) && (MEM_read32((void*)repMatch) == MEM_read32((void*)(ip + 1)))) + { + byte* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; + + mLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixLowest) + 4; + ip++; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, 0, mLength - 3); + goto _match_stored; + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_noDict && (((offset_1 > 0) && (MEM_read32((void*)(ip + 1 - offset_1)) == MEM_read32((void*)(ip + 1)))))) + { + mLength = ZSTD_count(ip + 1 + 4, ip + 1 + 4 - offset_1, iend) + 4; + ip++; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, 0, mLength - 3); + goto _match_stored; + } + + if (matchIndexL > prefixLowestIndex) + { + if (MEM_read64((void*)matchLong) == MEM_read64((void*)ip)) + { + mLength = ZSTD_count(ip + 8, matchLong + 8, iend) + 8; + offset = (uint)(ip - matchLong); + while ((((ip > anchor) && (matchLong > prefixLowest))) && (ip[-1] == matchLong[-1])) + { + ip--; + matchLong--; + mLength++; + } + + goto _match_found; + } + } + else if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + uint dictMatchIndexL = dictHashLong[dictHL]; + byte* dictMatchL = dictBase + dictMatchIndexL; + + assert(dictMatchL < dictEnd); + if (dictMatchL > dictStart && MEM_read64((void*)dictMatchL) == MEM_read64((void*)ip)) + { + mLength = ZSTD_count_2segments(ip + 8, dictMatchL + 8, iend, dictEnd, prefixLowest) + 8; + offset = (uint)(curr - dictMatchIndexL - dictIndexDelta); + while ((((ip > anchor) && (dictMatchL > dictStart))) && (ip[-1] == dictMatchL[-1])) + { + ip--; + dictMatchL--; + mLength++; + } + + goto _match_found; + } + } + + if (matchIndexS > prefixLowestIndex) + { + if (MEM_read32((void*)match) == MEM_read32((void*)ip)) + { + goto _search_next_long; + } + } + else if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + uint dictMatchIndexS = dictHashSmall[dictHS]; + + match = dictBase + dictMatchIndexS; + matchIndexS = dictMatchIndexS + dictIndexDelta; + if (match > dictStart && MEM_read32((void*)match) == MEM_read32((void*)ip)) + { + goto _search_next_long; + } + } + + ip += ((ip - anchor) >> 8) + 1; + continue; + _search_next_long: + { + nuint hl3 = ZSTD_hashPtr((void*)(ip + 1), hBitsL, 8); + nuint dictHLNext = ZSTD_hashPtr((void*)(ip + 1), dictHBitsL, 8); + uint matchIndexL3 = hashLong[hl3]; + byte* matchL3 = @base + matchIndexL3; + + hashLong[hl3] = curr + 1; + if (matchIndexL3 > prefixLowestIndex) + { + if (MEM_read64((void*)matchL3) == MEM_read64((void*)(ip + 1))) + { + mLength = ZSTD_count(ip + 9, matchL3 + 8, iend) + 8; + ip++; + offset = (uint)(ip - matchL3); + while ((((ip > anchor) && (matchL3 > prefixLowest))) && (ip[-1] == matchL3[-1])) + { + ip--; + matchL3--; + mLength++; + } + + goto _match_found; + } + } + else if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + uint dictMatchIndexL3 = dictHashLong[dictHLNext]; + byte* dictMatchL3 = dictBase + dictMatchIndexL3; + + assert(dictMatchL3 < dictEnd); + if (dictMatchL3 > dictStart && MEM_read64((void*)dictMatchL3) == MEM_read64((void*)(ip + 1))) + { + mLength = ZSTD_count_2segments(ip + 1 + 8, dictMatchL3 + 8, iend, dictEnd, prefixLowest) + 8; + ip++; + offset = (uint)(curr + 1 - dictMatchIndexL3 - dictIndexDelta); + while ((((ip > anchor) && (dictMatchL3 > dictStart))) && (ip[-1] == dictMatchL3[-1])) + { + ip--; + dictMatchL3--; + mLength++; + } + + goto _match_found; + } + } + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) + { + mLength = ZSTD_count_2segments(ip + 4, match + 4, iend, dictEnd, prefixLowest) + 4; + offset = (uint)(curr - matchIndexS); + while ((((ip > anchor) && (match > dictStart))) && (ip[-1] == match[-1])) + { + ip--; + match--; + mLength++; + } + } + else + { + mLength = ZSTD_count(ip + 4, match + 4, iend) + 4; + offset = (uint)(ip - match); + while ((((ip > anchor) && (match > prefixLowest))) && (ip[-1] == match[-1])) + { + ip--; + match--; + mLength++; + } + } + + _match_found: + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + (uint)((3 - 1)), mLength - 3); + _match_stored: + ip += mLength; + anchor = ip; + if (ip <= ilimit) + { + + { + uint indexToInsert = curr + 2; + + hashLong[ZSTD_hashPtr((void*)(@base + indexToInsert), hBitsL, 8)] = indexToInsert; + hashLong[ZSTD_hashPtr((void*)(ip - 2), hBitsL, 8)] = (uint)(ip - 2 - @base); + hashSmall[ZSTD_hashPtr((void*)(@base + indexToInsert), hBitsS, mls)] = indexToInsert; + hashSmall[ZSTD_hashPtr((void*)(ip - 1), hBitsS, mls)] = (uint)(ip - 1 - @base); + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + while (ip <= ilimit) + { + uint current2 = (uint)(ip - @base); + uint repIndex2 = current2 - offset_2; + byte* repMatch2 = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && repIndex2 < prefixLowestIndex ? dictBase + repIndex2 - dictIndexDelta : @base + repIndex2; + + if (((uint)((prefixLowestIndex - 1) - (uint)(repIndex2)) >= 3) && (MEM_read32((void*)repMatch2) == MEM_read32((void*)ip))) + { + byte* repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend; + nuint repLength2 = ZSTD_count_2segments(ip + 4, repMatch2 + 4, iend, repEnd2, prefixLowest) + 4; + uint tmpOffset = offset_2; + + offset_2 = offset_1; + offset_1 = tmpOffset; + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2 - 3); + hashSmall[ZSTD_hashPtr((void*)ip, hBitsS, mls)] = current2; + hashLong[ZSTD_hashPtr((void*)ip, hBitsL, 8)] = current2; + ip += repLength2; + anchor = ip; + continue; + } + + break; + } + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_noDict) + { + while ((ip <= ilimit) && (((offset_2 > 0) && (MEM_read32((void*)ip) == MEM_read32((void*)(ip - offset_2)))))) + { + nuint rLength = ZSTD_count(ip + 4, ip + 4 - offset_2, iend) + 4; + uint tmpOff = offset_2; + + offset_2 = offset_1; + offset_1 = tmpOff; + hashSmall[ZSTD_hashPtr((void*)ip, hBitsS, mls)] = (uint)(ip - @base); + hashLong[ZSTD_hashPtr((void*)ip, hBitsL, 8)] = (uint)(ip - @base); + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, rLength - 3); + ip += rLength; + anchor = ip; + continue; + } + } + } + } + + rep[0] = offset_1 != 0 ? offset_1 : offsetSaved; + rep[1] = offset_2 != 0 ? offset_2 : offsetSaved; + return (nuint)(iend - anchor); + } + + public static nuint ZSTD_compressBlock_doubleFast(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + uint mls = ms->cParams.minMatch; + + switch (mls) + { + default: + case 4: + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMode_e.ZSTD_noDict); + } + + case 5: + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMode_e.ZSTD_noDict); + } + + case 6: + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMode_e.ZSTD_noDict); + } + + case 7: + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMode_e.ZSTD_noDict); + } + } + } + + public static nuint ZSTD_compressBlock_doubleFast_dictMatchState(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + uint mls = ms->cParams.minMatch; + + switch (mls) + { + default: + case 4: + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + case 5: + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + case 6: + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + case 7: + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + } + } + + private static nuint ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashLong = ms->hashTable; + uint hBitsL = cParams->hashLog; + uint* hashSmall = ms->chainTable; + uint hBitsS = cParams->chainLog; + byte* istart = (byte*)(src); + byte* ip = istart; + byte* anchor = istart; + byte* iend = istart + srcSize; + byte* ilimit = iend - 8; + byte* @base = ms->window.@base; + uint endIndex = (uint)((nuint)(istart - @base) + srcSize); + uint lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog); + uint dictStartIndex = lowLimit; + uint dictLimit = ms->window.dictLimit; + uint prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit; + byte* prefixStart = @base + prefixStartIndex; + byte* dictBase = ms->window.dictBase; + byte* dictStart = dictBase + dictStartIndex; + byte* dictEnd = dictBase + prefixStartIndex; + uint offset_1 = rep[0], offset_2 = rep[1]; + + if (prefixStartIndex == dictStartIndex) + { + return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, mls, ZSTD_dictMode_e.ZSTD_noDict); + } + + while (ip < ilimit) + { + nuint hSmall = ZSTD_hashPtr((void*)ip, hBitsS, mls); + uint matchIndex = hashSmall[hSmall]; + byte* matchBase = matchIndex < prefixStartIndex ? dictBase : @base; + byte* match = matchBase + matchIndex; + nuint hLong = ZSTD_hashPtr((void*)ip, hBitsL, 8); + uint matchLongIndex = hashLong[hLong]; + byte* matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : @base; + byte* matchLong = matchLongBase + matchLongIndex; + uint curr = (uint)(ip - @base); + uint repIndex = curr + 1 - offset_1; + byte* repBase = repIndex < prefixStartIndex ? dictBase : @base; + byte* repMatch = repBase + repIndex; + nuint mLength; + + hashSmall[hSmall] = hashLong[hLong] = curr; + if (((((uint)((prefixStartIndex - 1) - repIndex) >= 3) && (offset_1 < curr + 1 - dictStartIndex))) && (MEM_read32((void*)repMatch) == MEM_read32((void*)(ip + 1)))) + { + byte* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; + + mLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixStart) + 4; + ip++; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, 0, mLength - 3); + } + else + { + if ((matchLongIndex > dictStartIndex) && (MEM_read64((void*)matchLong) == MEM_read64((void*)ip))) + { + byte* matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend; + byte* lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart; + uint offset; + + mLength = ZSTD_count_2segments(ip + 8, matchLong + 8, iend, matchEnd, prefixStart) + 8; + offset = curr - matchLongIndex; + while ((((ip > anchor) && (matchLong > lowMatchPtr))) && (ip[-1] == matchLong[-1])) + { + ip--; + matchLong--; + mLength++; + } + + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + (uint)((3 - 1)), mLength - 3); + } + else if ((matchIndex > dictStartIndex) && (MEM_read32((void*)match) == MEM_read32((void*)ip))) + { + nuint h3 = ZSTD_hashPtr((void*)(ip + 1), hBitsL, 8); + uint matchIndex3 = hashLong[h3]; + byte* match3Base = matchIndex3 < prefixStartIndex ? dictBase : @base; + byte* match3 = match3Base + matchIndex3; + uint offset; + + hashLong[h3] = curr + 1; + if ((matchIndex3 > dictStartIndex) && (MEM_read64((void*)match3) == MEM_read64((void*)(ip + 1)))) + { + byte* matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend; + byte* lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart; + + mLength = ZSTD_count_2segments(ip + 9, match3 + 8, iend, matchEnd, prefixStart) + 8; + ip++; + offset = curr + 1 - matchIndex3; + while ((((ip > anchor) && (match3 > lowMatchPtr))) && (ip[-1] == match3[-1])) + { + ip--; + match3--; + mLength++; + } + } + else + { + byte* matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend; + byte* lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart; + + mLength = ZSTD_count_2segments(ip + 4, match + 4, iend, matchEnd, prefixStart) + 4; + offset = curr - matchIndex; + while ((((ip > anchor) && (match > lowMatchPtr))) && (ip[-1] == match[-1])) + { + ip--; + match--; + mLength++; + } + } + + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + (uint)((3 - 1)), mLength - 3); + } + else + { + ip += ((ip - anchor) >> 8) + 1; + continue; + } + } + + ip += mLength; + anchor = ip; + if (ip <= ilimit) + { + + { + uint indexToInsert = curr + 2; + + hashLong[ZSTD_hashPtr((void*)(@base + indexToInsert), hBitsL, 8)] = indexToInsert; + hashLong[ZSTD_hashPtr((void*)(ip - 2), hBitsL, 8)] = (uint)(ip - 2 - @base); + hashSmall[ZSTD_hashPtr((void*)(@base + indexToInsert), hBitsS, mls)] = indexToInsert; + hashSmall[ZSTD_hashPtr((void*)(ip - 1), hBitsS, mls)] = (uint)(ip - 1 - @base); + } + + while (ip <= ilimit) + { + uint current2 = (uint)(ip - @base); + uint repIndex2 = current2 - offset_2; + byte* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : @base + repIndex2; + + if (((((uint)((prefixStartIndex - 1) - repIndex2) >= 3) && (offset_2 < current2 - dictStartIndex))) && (MEM_read32((void*)repMatch2) == MEM_read32((void*)ip))) + { + byte* repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; + nuint repLength2 = ZSTD_count_2segments(ip + 4, repMatch2 + 4, iend, repEnd2, prefixStart) + 4; + uint tmpOffset = offset_2; + + offset_2 = offset_1; + offset_1 = tmpOffset; + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2 - 3); + hashSmall[ZSTD_hashPtr((void*)ip, hBitsS, mls)] = current2; + hashLong[ZSTD_hashPtr((void*)ip, hBitsL, 8)] = current2; + ip += repLength2; + anchor = ip; + continue; + } + + break; + } + } + } + + rep[0] = offset_1; + rep[1] = offset_2; + return (nuint)(iend - anchor); + } + + public static nuint ZSTD_compressBlock_doubleFast_extDict(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + uint mls = ms->cParams.minMatch; + + switch (mls) + { + default: + case 4: + { + return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4); + } + + case 5: + { + return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5); + } + + case 6: + { + return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6); + } + + case 7: + { + return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7); + } + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdFast.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdFast.cs new file mode 100644 index 00000000..57a617e9 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdFast.cs @@ -0,0 +1,551 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static void ZSTD_fillHashTable(ZSTD_matchState_t* ms, void* end, ZSTD_dictTableLoadMethod_e dtlm) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashTable = ms->hashTable; + uint hBits = cParams->hashLog; + uint mls = cParams->minMatch; + byte* @base = ms->window.@base; + byte* ip = @base + ms->nextToUpdate; + byte* iend = ((byte*)(end)) - 8; + uint fastHashFillStep = 3; + + for (; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) + { + uint curr = (uint)(ip - @base); + nuint hash0 = ZSTD_hashPtr((void*)ip, hBits, mls); + + hashTable[hash0] = curr; + if (dtlm == ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast) + { + continue; + } + + + { + uint p; + + for (p = 1; p < fastHashFillStep; ++p) + { + nuint hash = ZSTD_hashPtr((void*)(ip + p), hBits, mls); + + if (hashTable[hash] == 0) + { + hashTable[hash] = curr + p; + } + } + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_compressBlock_fast_generic(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashTable = ms->hashTable; + uint hlog = cParams->hashLog; + nuint stepSize = cParams->targetLength + (uint)((cParams->targetLength) == 0 ? 1 : 0) + 1; + byte* @base = ms->window.@base; + byte* istart = (byte*)(src); + byte* ip0 = istart; + byte* ip1; + byte* anchor = istart; + uint endIndex = (uint)((nuint)(istart - @base) + srcSize); + uint prefixStartIndex = ZSTD_getLowestPrefixIndex(ms, endIndex, cParams->windowLog); + byte* prefixStart = @base + prefixStartIndex; + byte* iend = istart + srcSize; + byte* ilimit = iend - 8; + uint offset_1 = rep[0], offset_2 = rep[1]; + uint offsetSaved = 0; + + ip0 += ((ip0 == prefixStart) ? 1 : 0); + ip1 = ip0 + 1; + + { + uint curr = (uint)(ip0 - @base); + uint windowLow = ZSTD_getLowestPrefixIndex(ms, curr, cParams->windowLog); + uint maxRep = curr - windowLow; + + if (offset_2 > maxRep) + { + offsetSaved = offset_2; offset_2 = 0; + } + + if (offset_1 > maxRep) + { + offsetSaved = offset_1; offset_1 = 0; + } + } + + while (ip1 < ilimit) + { + byte* match0; + byte* match1; + uint current0, matchIndex0, matchIndex1; + { + nuint h0 = ZSTD_hashPtr((void*)ip0, hlog, mls); + nuint h1 = ZSTD_hashPtr((void*)ip1, hlog, mls); + matchIndex0 = hashTable[h0]; + matchIndex1 = hashTable[h1]; + match0 = @base + matchIndex0; + match1 = @base + matchIndex1; + current0 = (uint)(ip0 - @base); + hashTable[h0] = current0; + hashTable[h1] = (uint)(ip1 - @base); + } + + uint offcode; + nuint mLength; + + assert(ip0 + 1 == ip1); + { + byte* ip2 = ip0 + 2; + byte* repMatch = ip2 - offset_1; + if (((offset_1 > 0) && (MEM_read32((void*) repMatch) == MEM_read32((void*) ip2)))) + { + mLength = (nuint) ((ip2[-1] == repMatch[-1]) ? 1 : 0); + ip0 = ip2 - mLength; + match0 = repMatch - mLength; + mLength += 4; + offcode = 0; + goto _match; + } + } + + if ((matchIndex0 > prefixStartIndex) && MEM_read32((void*)match0) == MEM_read32((void*)ip0)) + { + goto _offset; + } + + if ((matchIndex1 > prefixStartIndex) && MEM_read32((void*)match1) == MEM_read32((void*)ip1)) + { + ip0 = ip1; + match0 = match1; + goto _offset; + } + + + { + nuint step = ((nuint)(ip0 - anchor) >> (8 - 1)) + stepSize; + + assert(step >= 2); + ip0 += step; + ip1 += step; + continue; + } + + _offset: + offset_2 = offset_1; + offset_1 = (uint)(ip0 - match0); + offcode = offset_1 + (uint)((3 - 1)); + mLength = 4; + while ((((ip0 > anchor) && (match0 > prefixStart))) && (ip0[-1] == match0[-1])) + { + ip0--; + match0--; + mLength++; + } + + _match: + mLength += ZSTD_count(ip0 + mLength, match0 + mLength, iend); + ZSTD_storeSeq(seqStore, (nuint)(ip0 - anchor), anchor, iend, offcode, mLength - 3); + ip0 += mLength; + anchor = ip0; + if (ip0 <= ilimit) + { + assert(@base + current0 + 2 > istart); + hashTable[ZSTD_hashPtr((void*)(@base + current0 + 2), hlog, mls)] = current0 + 2; + hashTable[ZSTD_hashPtr((void*)(ip0 - 2), hlog, mls)] = (uint)(ip0 - 2 - @base); + if (offset_2 > 0) + { + while ((ip0 <= ilimit) && (MEM_read32((void*)ip0) == MEM_read32((void*)(ip0 - offset_2)))) + { + nuint rLength = ZSTD_count(ip0 + 4, ip0 + 4 - offset_2, iend) + 4; + + + { + uint tmpOff = offset_2; + + offset_2 = offset_1; + offset_1 = tmpOff; + } + + hashTable[ZSTD_hashPtr((void*)ip0, hlog, mls)] = (uint)(ip0 - @base); + ip0 += rLength; + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, rLength - 3); + anchor = ip0; + continue; + } + } + } + + ip1 = ip0 + 1; + } + + rep[0] = offset_1 != 0 ? offset_1 : offsetSaved; + rep[1] = offset_2 != 0 ? offset_2 : offsetSaved; + return (nuint)(iend - anchor); + } + + public static nuint ZSTD_compressBlock_fast(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + uint mls = ms->cParams.minMatch; + + assert(ms->dictMatchState == null); + switch (mls) + { + default: + case 4: + { + return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4); + } + + case 5: + { + return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5); + } + + case 6: + { + return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6); + } + + case 7: + { + return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7); + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_compressBlock_fast_dictMatchState_generic(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashTable = ms->hashTable; + uint hlog = cParams->hashLog; + uint stepSize = cParams->targetLength + (uint)((cParams->targetLength) == 0 ? 1 : 0); + byte* @base = ms->window.@base; + byte* istart = (byte*)(src); + byte* ip = istart; + byte* anchor = istart; + uint prefixStartIndex = ms->window.dictLimit; + byte* prefixStart = @base + prefixStartIndex; + byte* iend = istart + srcSize; + byte* ilimit = iend - 8; + uint offset_1 = rep[0], offset_2 = rep[1]; + uint offsetSaved = 0; + ZSTD_matchState_t* dms = ms->dictMatchState; + ZSTD_compressionParameters* dictCParams = &dms->cParams; + uint* dictHashTable = dms->hashTable; + uint dictStartIndex = dms->window.dictLimit; + byte* dictBase = dms->window.@base; + byte* dictStart = dictBase + dictStartIndex; + byte* dictEnd = dms->window.nextSrc; + uint dictIndexDelta = prefixStartIndex - (uint)(dictEnd - dictBase); + uint dictAndPrefixLength = (uint)(ip - prefixStart + dictEnd - dictStart); + uint dictHLog = dictCParams->hashLog; + uint maxDistance = 1U << (int)cParams->windowLog; + uint endIndex = (uint)((nuint)(ip - @base) + srcSize); + + assert(endIndex - prefixStartIndex <= maxDistance); + assert(prefixStartIndex >= (uint)(dictEnd - dictBase)); + ip += ((dictAndPrefixLength == 0) ? 1 : 0); + assert(offset_1 <= dictAndPrefixLength); + assert(offset_2 <= dictAndPrefixLength); + while (ip < ilimit) + { + nuint mLength; + nuint h = ZSTD_hashPtr((void*)ip, hlog, mls); + uint curr = (uint)(ip - @base); + uint matchIndex = hashTable[h]; + byte* match = @base + matchIndex; + uint repIndex = curr + 1 - offset_1; + byte* repMatch = (repIndex < prefixStartIndex) ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; + + hashTable[h] = curr; + if (((uint)((prefixStartIndex - 1) - repIndex) >= 3) && (MEM_read32((void*)repMatch) == MEM_read32((void*)(ip + 1)))) + { + byte* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; + + mLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixStart) + 4; + ip++; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, 0, mLength - 3); + } + else if ((matchIndex <= prefixStartIndex)) + { + nuint dictHash = ZSTD_hashPtr((void*)ip, dictHLog, mls); + uint dictMatchIndex = dictHashTable[dictHash]; + byte* dictMatch = dictBase + dictMatchIndex; + + if (dictMatchIndex <= dictStartIndex || MEM_read32((void*)dictMatch) != MEM_read32((void*)ip)) + { + assert(stepSize >= 1); + ip += (ulong)((ip - anchor) >> 8) + stepSize; + continue; + } + else + { + uint offset = (uint)(curr - dictMatchIndex - dictIndexDelta); + + mLength = ZSTD_count_2segments(ip + 4, dictMatch + 4, iend, dictEnd, prefixStart) + 4; + while ((((ip > anchor) && (dictMatch > dictStart))) && (ip[-1] == dictMatch[-1])) + { + ip--; + dictMatch--; + mLength++; + } + + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + (uint)((3 - 1)), mLength - 3); + } + } + else if (MEM_read32((void*)match) != MEM_read32((void*)ip)) + { + assert(stepSize >= 1); + ip += (ulong)((ip - anchor) >> 8) + stepSize; + continue; + } + else + { + uint offset = (uint)(ip - match); + + mLength = ZSTD_count(ip + 4, match + 4, iend) + 4; + while ((((ip > anchor) && (match > prefixStart))) && (ip[-1] == match[-1])) + { + ip--; + match--; + mLength++; + } + + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + (uint)((3 - 1)), mLength - 3); + } + + ip += mLength; + anchor = ip; + if (ip <= ilimit) + { + assert(@base + curr + 2 > istart); + hashTable[ZSTD_hashPtr((void*)(@base + curr + 2), hlog, mls)] = curr + 2; + hashTable[ZSTD_hashPtr((void*)(ip - 2), hlog, mls)] = (uint)(ip - 2 - @base); + while (ip <= ilimit) + { + uint current2 = (uint)(ip - @base); + uint repIndex2 = current2 - offset_2; + byte* repMatch2 = repIndex2 < prefixStartIndex ? dictBase - dictIndexDelta + repIndex2 : @base + repIndex2; + + if (((uint)((prefixStartIndex - 1) - (uint)(repIndex2)) >= 3) && (MEM_read32((void*)repMatch2) == MEM_read32((void*)ip))) + { + byte* repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; + nuint repLength2 = ZSTD_count_2segments(ip + 4, repMatch2 + 4, iend, repEnd2, prefixStart) + 4; + uint tmpOffset = offset_2; + + offset_2 = offset_1; + offset_1 = tmpOffset; + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2 - 3); + hashTable[ZSTD_hashPtr((void*)ip, hlog, mls)] = current2; + ip += repLength2; + anchor = ip; + continue; + } + + break; + } + } + } + + rep[0] = offset_1 != 0 ? offset_1 : offsetSaved; + rep[1] = offset_2 != 0 ? offset_2 : offsetSaved; + return (nuint)(iend - anchor); + } + + public static nuint ZSTD_compressBlock_fast_dictMatchState(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + uint mls = ms->cParams.minMatch; + + assert(ms->dictMatchState != null); + switch (mls) + { + default: + case 4: + { + return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4); + } + + case 5: + { + return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5); + } + + case 6: + { + return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6); + } + + case 7: + { + return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7); + } + } + } + + private static nuint ZSTD_compressBlock_fast_extDict_generic(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashTable = ms->hashTable; + uint hlog = cParams->hashLog; + uint stepSize = cParams->targetLength + (uint)((cParams->targetLength) == 0 ? 1 : 0); + byte* @base = ms->window.@base; + byte* dictBase = ms->window.dictBase; + byte* istart = (byte*)(src); + byte* ip = istart; + byte* anchor = istart; + uint endIndex = (uint)((nuint)(istart - @base) + srcSize); + uint lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog); + uint dictStartIndex = lowLimit; + byte* dictStart = dictBase + dictStartIndex; + uint dictLimit = ms->window.dictLimit; + uint prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit; + byte* prefixStart = @base + prefixStartIndex; + byte* dictEnd = dictBase + prefixStartIndex; + byte* iend = istart + srcSize; + byte* ilimit = iend - 8; + uint offset_1 = rep[0], offset_2 = rep[1]; + + if (prefixStartIndex == dictStartIndex) + { + return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, mls); + } + + while (ip < ilimit) + { + nuint h = ZSTD_hashPtr((void*)ip, hlog, mls); + uint matchIndex = hashTable[h]; + byte* matchBase = matchIndex < prefixStartIndex ? dictBase : @base; + byte* match = matchBase + matchIndex; + uint curr = (uint)(ip - @base); + uint repIndex = curr + 1 - offset_1; + byte* repBase = repIndex < prefixStartIndex ? dictBase : @base; + byte* repMatch = repBase + repIndex; + + hashTable[h] = curr; + if (((((uint)((prefixStartIndex - 1) - repIndex) >= 3) && (offset_1 < curr + 1 - dictStartIndex))) && (MEM_read32((void*)repMatch) == MEM_read32((void*)(ip + 1)))) + { + byte* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; + nuint rLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixStart) + 4; + + ip++; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, 0, rLength - 3); + ip += rLength; + anchor = ip; + } + else + { + if ((matchIndex < dictStartIndex) || (MEM_read32((void*)match) != MEM_read32((void*)ip))) + { + assert(stepSize >= 1); + ip += (ulong)((ip - anchor) >> 8) + stepSize; + continue; + } + + + { + byte* matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend; + byte* lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart; + uint offset = curr - matchIndex; + nuint mLength = ZSTD_count_2segments(ip + 4, match + 4, iend, matchEnd, prefixStart) + 4; + + while ((((ip > anchor) && (match > lowMatchPtr))) && (ip[-1] == match[-1])) + { + ip--; + match--; + mLength++; + } + + offset_2 = offset_1; + offset_1 = offset; + ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + (uint)((3 - 1)), mLength - 3); + ip += mLength; + anchor = ip; + } + } + + if (ip <= ilimit) + { + hashTable[ZSTD_hashPtr((void*)(@base + curr + 2), hlog, mls)] = curr + 2; + hashTable[ZSTD_hashPtr((void*)(ip - 2), hlog, mls)] = (uint)(ip - 2 - @base); + while (ip <= ilimit) + { + uint current2 = (uint)(ip - @base); + uint repIndex2 = current2 - offset_2; + byte* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : @base + repIndex2; + + if (((((uint)((prefixStartIndex - 1) - repIndex2) >= 3) && (offset_2 < curr - dictStartIndex))) && (MEM_read32((void*)repMatch2) == MEM_read32((void*)ip))) + { + byte* repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; + nuint repLength2 = ZSTD_count_2segments(ip + 4, repMatch2 + 4, iend, repEnd2, prefixStart) + 4; + + + { + uint tmpOffset = offset_2; + + offset_2 = offset_1; + offset_1 = tmpOffset; + } + + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2 - 3); + hashTable[ZSTD_hashPtr((void*)ip, hlog, mls)] = current2; + ip += repLength2; + anchor = ip; + continue; + } + + break; + } + } + } + + rep[0] = offset_1; + rep[1] = offset_2; + return (nuint)(iend - anchor); + } + + public static nuint ZSTD_compressBlock_fast_extDict(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + uint mls = ms->cParams.minMatch; + + switch (mls) + { + default: + case 4: + { + return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4); + } + + case 5: + { + return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5); + } + + case 6: + { + return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6); + } + + case 7: + { + return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7); + } + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdInternal.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdInternal.cs new file mode 100644 index 00000000..be0bf611 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdInternal.cs @@ -0,0 +1,409 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /** + * Ignore: this is an internal helper. + * + * This is a helper function to help force C99-correctness during compilation. + * Under strict compilation modes, variadic macro arguments can't be empty. + * However, variadic function arguments can be. Using a function therefore lets + * us statically check that at least one (string) argument was passed, + * independent of the compilation flags. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void _force_has_format_string(sbyte* format) + { + } + + public static uint* repStartValue = GetArrayPointer(new uint[3] + { + 1, + 4, + 8, + }); + + public static nuint* ZSTD_fcs_fieldSize = GetArrayPointer(new nuint[4] + { + 0, + 2, + 4, + 8, + }); + + public static nuint* ZSTD_did_fieldSize = GetArrayPointer(new nuint[4] + { + 0, + 1, + 2, + 4, + }); + + public const nuint ZSTD_blockHeaderSize = 3; + + public static uint* LL_bits = GetArrayPointer(new uint[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, + }); + + public static short* LL_defaultNorm = 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, + }); + + public const uint LL_defaultNormLog = 6; + + public static uint* ML_bits = GetArrayPointer(new uint[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, + }); + + public static short* ML_defaultNorm = 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, + }); + + public const uint ML_defaultNormLog = 6; + + public static short* OF_defaultNorm = 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, + }); + + public const uint OF_defaultNormLog = 5; + + /*-******************************************* + * Shared functions to include for inlining + *********************************************/ + private static void ZSTD_copy8(void* dst, void* src) + { + memcpy((dst), (src), (8)); + } + + private static void ZSTD_copy16(void* dst, void* src) + { + memcpy((dst), (src), (16)); + } + + /*! ZSTD_wildcopy() : + * Custom version of ZSTD_memcpy(), can over read/write up to WILDCOPY_OVERLENGTH bytes (if length==0) + * @param ovtype controls the overlap detection + * - ZSTD_no_overlap: The source and destination are guaranteed to be at least WILDCOPY_VECLEN bytes apart. + * - ZSTD_overlap_src_before_dst: The src and dst may overlap, but they MUST be at least 8 bytes apart. + * The src buffer must be before the dst buffer. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_wildcopy(void* dst, void* src, nint length, ZSTD_overlap_e ovtype) + { + nint diff = (nint)((byte*)(dst) - (byte*)(src)); + byte* ip = (byte*)(src); + byte* op = (byte*)(dst); + byte* oend = op + length; + + assert(diff >= 8 || (ovtype == ZSTD_overlap_e.ZSTD_no_overlap && diff <= -16)); + if (ovtype == ZSTD_overlap_e.ZSTD_overlap_src_before_dst && diff < 16) + { + do + { + + { + ZSTD_copy8((void*)op, (void*)ip); + op += 8; + ip += 8; + } + } + while (op < oend); + + } + else + { + assert(diff >= 16 || diff <= -16); + ZSTD_copy16((void*)op, (void*)ip); + if (16 >= length) + { + return; + } + + op += 16; + ip += 16; + do + { + + { + ZSTD_copy16((void*)op, (void*)ip); + op += 16; + ip += 16; + } + + + { + ZSTD_copy16((void*)op, (void*)ip); + op += 16; + ip += 16; + } + + } + while (op < oend); + + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_limitCopy(void* dst, nuint dstCapacity, void* src, nuint srcSize) + { + nuint length = ((dstCapacity) < (srcSize) ? (dstCapacity) : (srcSize)); + + if (length > 0) + { + memcpy((dst), (src), (length)); + } + + return length; + } + + /** + * Returns the ZSTD_sequenceLength for the given sequences. It handles the decoding of long sequences + * indicated by longLengthPos and longLengthType, and adds MINMATCH back to matchLength. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ZSTD_sequenceLength ZSTD_getSequenceLength(seqStore_t* seqStore, seqDef_s* seq) + { + ZSTD_sequenceLength seqLen; + + seqLen.litLength = seq->litLength; + seqLen.matchLength = (uint)(seq->matchLength + 3); + if (seqStore->longLengthPos == (uint)(seq - seqStore->sequencesStart)) + { + if (seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_literalLength) + { + seqLen.litLength += 0xFFFF; + } + + if (seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_matchLength) + { + seqLen.matchLength += 0xFFFF; + } + } + + return seqLen; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint ZSTD_highbit32(uint val) + { + assert(val != 0); + + { + return (uint)BitOperations.Log2(val); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLazy.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLazy.cs new file mode 100644 index 00000000..f77da94d --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLazy.cs @@ -0,0 +1,2248 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.Intrinsics; +#if NET5_0_OR_GREATER +using System.Runtime.Intrinsics.Arm; +#endif +#if NETCOREAPP3_0_OR_GREATER +using System.Runtime.Intrinsics.X86; +#endif +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /*-************************************* + * Binary Tree search + ***************************************/ + [InlineMethod.Inline] + private static void ZSTD_updateDUBT(ZSTD_matchState_t* ms, byte* ip, byte* iend, uint mls) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashTable = ms->hashTable; + uint hashLog = cParams->hashLog; + uint* bt = ms->chainTable; + uint btLog = cParams->chainLog - 1; + uint btMask = (uint)((1 << (int)btLog) - 1); + byte* @base = ms->window.@base; + uint target = (uint)(ip - @base); + uint idx = ms->nextToUpdate; + + if (idx != target) + { + ; + } + + assert(ip + 8 <= iend); + assert(idx >= ms->window.dictLimit); + for (; idx < target; idx++) + { + nuint h = ZSTD_hashPtr((void*)(@base + idx), hashLog, mls); + uint matchIndex = hashTable[h]; + uint* nextCandidatePtr = bt + 2 * (idx & btMask); + uint* sortMarkPtr = nextCandidatePtr + 1; + + hashTable[h] = idx; + *nextCandidatePtr = matchIndex; + *sortMarkPtr = 1; + } + + ms->nextToUpdate = target; + } + + /** ZSTD_insertDUBT1() : + * sort one already inserted but unsorted position + * assumption : curr >= btlow == (curr - btmask) + * doesn't fail */ + [InlineMethod.Inline] + private static void ZSTD_insertDUBT1(ZSTD_matchState_t* ms, uint curr, byte* inputEnd, uint nbCompares, uint btLow, ZSTD_dictMode_e dictMode) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* bt = ms->chainTable; + uint btLog = cParams->chainLog - 1; + uint btMask = (uint)((1 << (int)btLog) - 1); + nuint commonLengthSmaller = 0, commonLengthLarger = 0; + byte* @base = ms->window.@base; + byte* dictBase = ms->window.dictBase; + uint dictLimit = ms->window.dictLimit; + byte* ip = (curr >= dictLimit) ? @base + curr : dictBase + curr; + byte* iend = (curr >= dictLimit) ? inputEnd : dictBase + dictLimit; + byte* dictEnd = dictBase + dictLimit; + byte* prefixStart = @base + dictLimit; + byte* match; + uint* smallerPtr = bt + 2 * (curr & btMask); + uint* largerPtr = smallerPtr + 1; + uint matchIndex = *smallerPtr; + uint dummy32; + uint windowValid = ms->window.lowLimit; + uint maxDistance = 1U << (int)cParams->windowLog; + uint windowLow = (curr - windowValid > maxDistance) ? curr - maxDistance : windowValid; + + assert(curr >= btLow); + assert(ip < iend); + while (nbCompares-- != 0 && (matchIndex > windowLow)) + { + uint* nextPtr = bt + 2 * (matchIndex & btMask); + nuint matchLength = ((commonLengthSmaller) < (commonLengthLarger) ? (commonLengthSmaller) : (commonLengthLarger)); + + assert(matchIndex < curr); + if ((dictMode != ZSTD_dictMode_e.ZSTD_extDict) || (matchIndex + matchLength >= dictLimit) || (curr < dictLimit)) + { + byte* mBase = ((dictMode != ZSTD_dictMode_e.ZSTD_extDict) || (matchIndex + matchLength >= dictLimit)) ? @base : dictBase; + + assert((matchIndex + matchLength >= dictLimit) || (curr < dictLimit)); + match = mBase + matchIndex; + matchLength += ZSTD_count(ip + matchLength, match + matchLength, iend); + } + else + { + match = dictBase + matchIndex; + matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart); + if (matchIndex + matchLength >= dictLimit) + { + match = @base + matchIndex; + } + } + + if (ip + matchLength == iend) + { + break; + } + + if (match[matchLength] < ip[matchLength]) + { + *smallerPtr = matchIndex; + commonLengthSmaller = matchLength; + if (matchIndex <= btLow) + { + smallerPtr = &dummy32; + break; + } + + smallerPtr = nextPtr + 1; + matchIndex = nextPtr[1]; + } + else + { + *largerPtr = matchIndex; + commonLengthLarger = matchLength; + if (matchIndex <= btLow) + { + largerPtr = &dummy32; + break; + } + + largerPtr = nextPtr; + matchIndex = nextPtr[0]; + } + } + + *smallerPtr = *largerPtr = 0; + } + + private static nuint ZSTD_DUBT_findBetterDictMatch(ZSTD_matchState_t* ms, byte* ip, byte* iend, nuint* offsetPtr, nuint bestLength, uint nbCompares, uint mls, ZSTD_dictMode_e dictMode) + { + ZSTD_matchState_t* dms = ms->dictMatchState; + ZSTD_compressionParameters* dmsCParams = &dms->cParams; + uint* dictHashTable = dms->hashTable; + uint hashLog = dmsCParams->hashLog; + nuint h = ZSTD_hashPtr((void*)ip, hashLog, mls); + uint dictMatchIndex = dictHashTable[h]; + byte* @base = ms->window.@base; + byte* prefixStart = @base + ms->window.dictLimit; + uint curr = (uint)(ip - @base); + byte* dictBase = dms->window.@base; + byte* dictEnd = dms->window.nextSrc; + uint dictHighLimit = (uint)(dms->window.nextSrc - dms->window.@base); + uint dictLowLimit = dms->window.lowLimit; + uint dictIndexDelta = ms->window.lowLimit - dictHighLimit; + uint* dictBt = dms->chainTable; + uint btLog = dmsCParams->chainLog - 1; + uint btMask = (uint)((1 << (int)btLog) - 1); + uint btLow = (btMask >= dictHighLimit - dictLowLimit) ? dictLowLimit : dictHighLimit - btMask; + nuint commonLengthSmaller = 0, commonLengthLarger = 0; + + assert(dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState); + while (nbCompares-- != 0 && (dictMatchIndex > dictLowLimit)) + { + uint* nextPtr = dictBt + 2 * (dictMatchIndex & btMask); + nuint matchLength = ((commonLengthSmaller) < (commonLengthLarger) ? (commonLengthSmaller) : (commonLengthLarger)); + byte* match = dictBase + dictMatchIndex; + + matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart); + if (dictMatchIndex + matchLength >= dictHighLimit) + { + match = @base + dictMatchIndex + dictIndexDelta; + } + + if (matchLength > bestLength) + { + uint matchIndex = dictMatchIndex + dictIndexDelta; + + if ((4 * (int)(matchLength - bestLength)) > (int)(ZSTD_highbit32(curr - matchIndex + 1) - ZSTD_highbit32((uint)(offsetPtr[0]) + 1))) + { + bestLength = matchLength; *offsetPtr = (uint)((3 - 1)) + curr - matchIndex; + } + + if (ip + matchLength == iend) + { + break; + } + } + + if (match[matchLength] < ip[matchLength]) + { + if (dictMatchIndex <= btLow) + { + break; + } + + commonLengthSmaller = matchLength; + dictMatchIndex = nextPtr[1]; + } + else + { + if (dictMatchIndex <= btLow) + { + break; + } + + commonLengthLarger = matchLength; + dictMatchIndex = nextPtr[0]; + } + } + + if (bestLength >= 3) + { + uint mIndex = curr - ((uint)(*offsetPtr) - (uint)((3 - 1))); + } + + return bestLength; + } + + [InlineMethod.Inline] + private static nuint ZSTD_DUBT_findBestMatch(ZSTD_matchState_t* ms, byte* ip, byte* iend, nuint* offsetPtr, uint mls, ZSTD_dictMode_e dictMode) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashTable = ms->hashTable; + uint hashLog = cParams->hashLog; + nuint h = ZSTD_hashPtr((void*)ip, hashLog, mls); + uint matchIndex = hashTable[h]; + byte* @base = ms->window.@base; + uint curr = (uint)(ip - @base); + uint windowLow = ZSTD_getLowestMatchIndex(ms, curr, cParams->windowLog); + uint* bt = ms->chainTable; + uint btLog = cParams->chainLog - 1; + uint btMask = (uint)((1 << (int)btLog) - 1); + uint btLow = (uint)((btMask >= curr) ? 0 : curr - btMask); + uint unsortLimit = ((btLow) > (windowLow) ? (btLow) : (windowLow)); + uint* nextCandidate = bt + 2 * (matchIndex & btMask); + uint* unsortedMark = bt + 2 * (matchIndex & btMask) + 1; + uint nbCompares = 1U << (int)cParams->searchLog; + uint nbCandidates = nbCompares; + uint previousCandidate = 0; + + assert(ip <= iend - 8); + assert(dictMode != ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + while ((matchIndex > unsortLimit) && (*unsortedMark == 1) && (nbCandidates > 1)) + { + *unsortedMark = previousCandidate; + previousCandidate = matchIndex; + matchIndex = *nextCandidate; + nextCandidate = bt + 2 * (matchIndex & btMask); + unsortedMark = bt + 2 * (matchIndex & btMask) + 1; + nbCandidates--; + } + + if ((matchIndex > unsortLimit) && (*unsortedMark == 1)) + { + *nextCandidate = *unsortedMark = 0; + } + + matchIndex = previousCandidate; + while (matchIndex != 0) + { + uint* nextCandidateIdxPtr = bt + 2 * (matchIndex & btMask) + 1; + uint nextCandidateIdx = *nextCandidateIdxPtr; + + ZSTD_insertDUBT1(ms, matchIndex, iend, nbCandidates, unsortLimit, dictMode); + matchIndex = nextCandidateIdx; + nbCandidates++; + } + + + { + nuint commonLengthSmaller = 0, commonLengthLarger = 0; + byte* dictBase = ms->window.dictBase; + uint dictLimit = ms->window.dictLimit; + byte* dictEnd = dictBase + dictLimit; + byte* prefixStart = @base + dictLimit; + uint* smallerPtr = bt + 2 * (curr & btMask); + uint* largerPtr = bt + 2 * (curr & btMask) + 1; + uint matchEndIdx = curr + 8 + 1; + uint dummy32; + nuint bestLength = 0; + + matchIndex = hashTable[h]; + hashTable[h] = curr; + while (nbCompares-- != 0 && (matchIndex > windowLow)) + { + uint* nextPtr = bt + 2 * (matchIndex & btMask); + nuint matchLength = ((commonLengthSmaller) < (commonLengthLarger) ? (commonLengthSmaller) : (commonLengthLarger)); + byte* match; + + if ((dictMode != ZSTD_dictMode_e.ZSTD_extDict) || (matchIndex + matchLength >= dictLimit)) + { + match = @base + matchIndex; + matchLength += ZSTD_count(ip + matchLength, match + matchLength, iend); + } + else + { + match = dictBase + matchIndex; + matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart); + if (matchIndex + matchLength >= dictLimit) + { + match = @base + matchIndex; + } + } + + if (matchLength > bestLength) + { + if (matchLength > matchEndIdx - matchIndex) + { + matchEndIdx = matchIndex + (uint)(matchLength); + } + + if ((4 * (int)(matchLength - bestLength)) > (int)(ZSTD_highbit32(curr - matchIndex + 1) - ZSTD_highbit32((uint)(offsetPtr[0]) + 1))) + { + bestLength = matchLength; *offsetPtr = (uint)((3 - 1)) + curr - matchIndex; + } + + if (ip + matchLength == iend) + { + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + nbCompares = 0; + } + + break; + } + } + + if (match[matchLength] < ip[matchLength]) + { + *smallerPtr = matchIndex; + commonLengthSmaller = matchLength; + if (matchIndex <= btLow) + { + smallerPtr = &dummy32; + break; + } + + smallerPtr = nextPtr + 1; + matchIndex = nextPtr[1]; + } + else + { + *largerPtr = matchIndex; + commonLengthLarger = matchLength; + if (matchIndex <= btLow) + { + largerPtr = &dummy32; + break; + } + + largerPtr = nextPtr; + matchIndex = nextPtr[0]; + } + } + + *smallerPtr = *largerPtr = 0; + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && nbCompares != 0) + { + bestLength = ZSTD_DUBT_findBetterDictMatch(ms, ip, iend, offsetPtr, bestLength, nbCompares, mls, dictMode); + } + + assert(matchEndIdx > curr + 8); + ms->nextToUpdate = matchEndIdx - 8; + if (bestLength >= 3) + { + uint mIndex = curr - ((uint)(*offsetPtr) - (uint)((3 - 1))); + } + + return bestLength; + } + } + + /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static nuint ZSTD_BtFindBestMatch(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr, uint mls, ZSTD_dictMode_e dictMode) + { + if (ip < ms->window.@base + ms->nextToUpdate) + { + return 0; + } + + ZSTD_updateDUBT(ms, ip, iLimit, mls); + return ZSTD_DUBT_findBestMatch(ms, ip, iLimit, offsetPtr, mls, dictMode); + } + + private static nuint ZSTD_BtFindBestMatch_selectMLS(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint mls; + switch (ms->cParams.minMatch) + { + default: + case 4: + { + mls = 4; + break; + } + + case 5: + { + mls = 5; + break; + } + + case 7: + case 6: + { + mls = 6; + break; + } + } + return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_dictMode_e.ZSTD_noDict); + } + + private static nuint ZSTD_BtFindBestMatch_dictMatchState_selectMLS(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint mls; + switch (ms->cParams.minMatch) + { + default: + case 4: + { + mls = 4; + break; + } + + case 5: + { + mls = 5; + break; + } + + case 7: + case 6: + { + mls = 6; + break; + } + } + return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + private static nuint ZSTD_BtFindBestMatch_extDict_selectMLS(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint mls; + switch (ms->cParams.minMatch) + { + default: + case 4: + { + mls = 4; + break; + } + + case 5: + { + mls = 5; + break; + } + + case 7: + case 6: + { + mls = 6; + break; + } + } + return ZSTD_BtFindBestMatch(ms, ip, iLimit, offsetPtr, mls, ZSTD_dictMode_e.ZSTD_extDict); + } + + /*********************************** + * Dedicated dict search + ***********************************/ + public static void ZSTD_dedicatedDictSearch_lazy_loadDictionary(ZSTD_matchState_t* ms, byte* ip) + { + byte* @base = ms->window.@base; + uint target = (uint)(ip - @base); + uint* hashTable = ms->hashTable; + uint* chainTable = ms->chainTable; + uint chainSize = (uint)(1 << (int)ms->cParams.chainLog); + uint idx = ms->nextToUpdate; + uint minChain = chainSize < target ? target - chainSize : idx; + uint bucketSize = (uint)(1 << 2); + uint cacheSize = bucketSize - 1; + uint chainAttempts = (uint)((1 << (int)ms->cParams.searchLog)) - cacheSize; + uint chainLimit = (uint)(chainAttempts > 255 ? 255 : chainAttempts); + uint hashLog = ms->cParams.hashLog - 2; + uint* tmpHashTable = hashTable; + uint* tmpChainTable = hashTable + ((nuint)(1) << (int)hashLog); + uint tmpChainSize = (uint)(((1 << 2) - 1) << (int)hashLog); + uint tmpMinChain = tmpChainSize < target ? target - tmpChainSize : idx; + uint hashIdx; + + assert(ms->cParams.chainLog <= 24); + assert(ms->cParams.hashLog > ms->cParams.chainLog); + assert(idx != 0); + assert(tmpMinChain <= minChain); + for (; idx < target; idx++) + { + uint h = (uint)(ZSTD_hashPtr((void*)(@base + idx), hashLog, ms->cParams.minMatch)); + + if (idx >= tmpMinChain) + { + tmpChainTable[idx - tmpMinChain] = hashTable[h]; + } + + tmpHashTable[h] = idx; + } + + + { + uint chainPos = 0; + + for (hashIdx = 0; hashIdx < (1U << (int)hashLog); hashIdx++) + { + uint count; + uint countBeyondMinChain = 0; + uint i = tmpHashTable[hashIdx]; + + for (count = 0; i >= tmpMinChain && count < cacheSize; count++) + { + if (i < minChain) + { + countBeyondMinChain++; + } + + i = tmpChainTable[i - tmpMinChain]; + } + + if (count == cacheSize) + { + for (count = 0; count < chainLimit;) + { + if (i < minChain) + { + if (i == 0 || ++countBeyondMinChain > cacheSize) + { + break; + } + } + + chainTable[chainPos++] = i; + count++; + if (i < tmpMinChain) + { + break; + } + + i = tmpChainTable[i - tmpMinChain]; + } + } + else + { + count = 0; + } + + if (count != 0) + { + tmpHashTable[hashIdx] = ((chainPos - count) << 8) + count; + } + else + { + tmpHashTable[hashIdx] = 0; + } + } + + assert(chainPos <= chainSize); + } + + for (hashIdx = (uint)((1 << (int)hashLog)); hashIdx != 0;) + { + uint bucketIdx = --hashIdx << 2; + uint chainPackedPointer = tmpHashTable[hashIdx]; + uint i; + + for (i = 0; i < cacheSize; i++) + { + hashTable[bucketIdx + i] = 0; + } + + hashTable[bucketIdx + bucketSize - 1] = chainPackedPointer; + } + + for (idx = ms->nextToUpdate; idx < target; idx++) + { + uint h = (uint)(ZSTD_hashPtr((void*)(@base + idx), hashLog, ms->cParams.minMatch)) << 2; + uint i; + + for (i = cacheSize - 1; i != 0; i--) + { + hashTable[h + i] = hashTable[h + i - 1]; + } + + hashTable[h] = idx; + } + + ms->nextToUpdate = target; + } + + /* Returns the longest match length found in the dedicated dict search structure. + * If none are longer than the argument ml, then ml will be returned. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_dedicatedDictSearch_lazy_search(nuint* offsetPtr, nuint ml, uint nbAttempts, ZSTD_matchState_t* dms, byte* ip, byte* iLimit, byte* prefixStart, uint curr, uint dictLimit, nuint ddsIdx) + { + uint ddsLowestIndex = dms->window.dictLimit; + byte* ddsBase = dms->window.@base; + byte* ddsEnd = dms->window.nextSrc; + uint ddsSize = (uint)(ddsEnd - ddsBase); + uint ddsIndexDelta = dictLimit - ddsSize; + uint bucketSize = (uint)((1 << 2)); + uint bucketLimit = nbAttempts < bucketSize - 1 ? nbAttempts : bucketSize - 1; + uint ddsAttempt; + uint matchIndex; + + for (ddsAttempt = 0; ddsAttempt < bucketSize - 1; ddsAttempt++) + { + Prefetch0((void*)(ddsBase + dms->hashTable[ddsIdx + ddsAttempt])); + } + + + { + uint chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1]; + uint chainIndex = chainPackedPointer >> 8; + + Prefetch0((void*)(&dms->chainTable[chainIndex])); + } + + for (ddsAttempt = 0; ddsAttempt < bucketLimit; ddsAttempt++) + { + nuint currentMl = 0; + byte* match; + + matchIndex = dms->hashTable[ddsIdx + ddsAttempt]; + match = ddsBase + matchIndex; + if (matchIndex == 0) + { + return ml; + } + + assert(matchIndex >= ddsLowestIndex); + assert(match + 4 <= ddsEnd); + if (MEM_read32((void*)match) == MEM_read32((void*)ip)) + { + currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, ddsEnd, prefixStart) + 4; + } + + if (currentMl > ml) + { + ml = currentMl; + *offsetPtr = curr - (matchIndex + ddsIndexDelta) + (uint)((3 - 1)); + if (ip + currentMl == iLimit) + { + return ml; + } + } + } + + + { + uint chainPackedPointer = dms->hashTable[ddsIdx + bucketSize - 1]; + uint chainIndex = chainPackedPointer >> 8; + uint chainLength = chainPackedPointer & 0xFF; + uint chainAttempts = nbAttempts - ddsAttempt; + uint chainLimit = chainAttempts > chainLength ? chainLength : chainAttempts; + uint chainAttempt; + + for (chainAttempt = 0; chainAttempt < chainLimit; chainAttempt++) + { + Prefetch0((void*)(ddsBase + dms->chainTable[chainIndex + chainAttempt])); + } + + for (chainAttempt = 0; chainAttempt < chainLimit; chainAttempt++ , chainIndex++) + { + nuint currentMl = 0; + byte* match; + + matchIndex = dms->chainTable[chainIndex]; + match = ddsBase + matchIndex; + assert(matchIndex >= ddsLowestIndex); + assert(match + 4 <= ddsEnd); + if (MEM_read32((void*)match) == MEM_read32((void*)ip)) + { + currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, ddsEnd, prefixStart) + 4; + } + + if (currentMl > ml) + { + ml = currentMl; + *offsetPtr = curr - (matchIndex + ddsIndexDelta) + (uint)((3 - 1)); + if (ip + currentMl == iLimit) + { + break; + } + } + } + } + + return ml; + } + + /* Update chains up to ip (excluded) + Assumption : always within prefix (i.e. not within extDict) */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_insertAndFindFirstIndex_internal(ZSTD_matchState_t* ms, ZSTD_compressionParameters* cParams, byte* ip, uint mls) + { + uint* hashTable = ms->hashTable; + uint hashLog = cParams->hashLog; + uint* chainTable = ms->chainTable; + uint chainMask = (uint)((1 << (int)cParams->chainLog) - 1); + byte* @base = ms->window.@base; + uint target = (uint)(ip - @base); + uint idx = ms->nextToUpdate; + + while (idx < target) + { + nuint h = ZSTD_hashPtr((void*)(@base + idx), hashLog, mls); + + chainTable[(idx) & (chainMask)] = hashTable[h]; + hashTable[h] = idx; + idx++; + } + + ms->nextToUpdate = target; + return hashTable[ZSTD_hashPtr((void*)ip, hashLog, mls)]; + } + + public static uint ZSTD_insertAndFindFirstIndex(ZSTD_matchState_t* ms, byte* ip) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + + return ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, ms->cParams.minMatch); + } + + /* inlining is important to hardwire a hot branch (template emulation) */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_HcFindBestMatch_generic(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr, uint mls, ZSTD_dictMode_e dictMode) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* chainTable = ms->chainTable; + uint chainSize = (uint)((1 << (int)cParams->chainLog)); + uint chainMask = chainSize - 1; + byte* @base = ms->window.@base; + byte* dictBase = ms->window.dictBase; + uint dictLimit = ms->window.dictLimit; + byte* prefixStart = @base + dictLimit; + byte* dictEnd = dictBase + dictLimit; + uint curr = (uint)(ip - @base); + uint maxDistance = 1U << (int)cParams->windowLog; + uint lowestValid = ms->window.lowLimit; + uint withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid; + uint isDictionary = (((ms->loadedDictEnd != 0)) ? 1U : 0U); + uint lowLimit = isDictionary != 0 ? lowestValid : withinMaxDistance; + uint minChain = curr > chainSize ? curr - chainSize : 0; + uint nbAttempts = 1U << (int)cParams->searchLog; + nuint ml = (nuint)(4 - 1); + ZSTD_matchState_t* dms = ms->dictMatchState; + uint ddsHashLog = dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch ? dms->cParams.hashLog - 2 : 0; + nuint ddsIdx = dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch ? ZSTD_hashPtr((void*)ip, ddsHashLog, mls) << 2 : 0; + uint matchIndex; + + if (dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) + { + uint* entry = &dms->hashTable[ddsIdx]; + + Prefetch0((void*)entry); + } + + matchIndex = ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, mls); + for (; ((matchIndex >= lowLimit) && (nbAttempts > 0)); nbAttempts--) + { + nuint currentMl = 0; + + if ((dictMode != ZSTD_dictMode_e.ZSTD_extDict) || matchIndex >= dictLimit) + { + byte* match = @base + matchIndex; + + assert(matchIndex >= dictLimit); + if (match[ml] == ip[ml]) + { + currentMl = ZSTD_count(ip, match, iLimit); + } + } + else + { + byte* match = dictBase + matchIndex; + + assert(match + 4 <= dictEnd); + if (MEM_read32((void*)match) == MEM_read32((void*)ip)) + { + currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, dictEnd, prefixStart) + 4; + } + } + + if (currentMl > ml) + { + ml = currentMl; + *offsetPtr = curr - matchIndex + (uint)((3 - 1)); + if (ip + currentMl == iLimit) + { + break; + } + } + + if (matchIndex <= minChain) + { + break; + } + + matchIndex = chainTable[(matchIndex) & (chainMask)]; + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) + { + ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts, dms, ip, iLimit, prefixStart, curr, dictLimit, ddsIdx); + } + else if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + uint* dmsChainTable = dms->chainTable; + uint dmsChainSize = (uint)((1 << (int)dms->cParams.chainLog)); + uint dmsChainMask = dmsChainSize - 1; + uint dmsLowestIndex = dms->window.dictLimit; + byte* dmsBase = dms->window.@base; + byte* dmsEnd = dms->window.nextSrc; + uint dmsSize = (uint)(dmsEnd - dmsBase); + uint dmsIndexDelta = dictLimit - dmsSize; + uint dmsMinChain = dmsSize > dmsChainSize ? dmsSize - dmsChainSize : 0; + + matchIndex = dms->hashTable[ZSTD_hashPtr((void*)ip, dms->cParams.hashLog, mls)]; + for (; ((matchIndex >= dmsLowestIndex) && (nbAttempts > 0)); nbAttempts--) + { + nuint currentMl = 0; + byte* match = dmsBase + matchIndex; + + assert(match + 4 <= dmsEnd); + if (MEM_read32((void*)match) == MEM_read32((void*)ip)) + { + currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, dmsEnd, prefixStart) + 4; + } + + if (currentMl > ml) + { + ml = currentMl; + *offsetPtr = curr - (matchIndex + dmsIndexDelta) + (uint)((3 - 1)); + if (ip + currentMl == iLimit) + { + break; + } + } + + if (matchIndex <= dmsMinChain) + { + break; + } + + matchIndex = dmsChainTable[matchIndex & dmsChainMask]; + } + } + + return ml; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_HcFindBestMatch_selectMLS(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint mls; + switch (ms->cParams.minMatch) + { + default: + case 4: + { + mls = 4; + break; + } + + case 5: + { + mls = 5; + break; + } + + case 7: + case 6: + { + mls = 6; + break; + } + } + return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, mls, ZSTD_dictMode_e.ZSTD_noDict); + } + + private static nuint ZSTD_HcFindBestMatch_dictMatchState_selectMLS(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint mls; + switch (ms->cParams.minMatch) + { + default: + case 4: + { + mls = 4; + break; + } + + case 5: + { + mls = 5; + break; + } + + case 7: + case 6: + { + mls = 6; + break; + } + } + return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, mls, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + private static nuint ZSTD_HcFindBestMatch_dedicatedDictSearch_selectMLS(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint mls; + switch (ms->cParams.minMatch) + { + default: + case 4: + { + mls = 4; + break; + } + + case 5: + { + mls = 5; + break; + } + + case 7: + case 6: + { + mls = 6; + break; + } + } + return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, mls, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_HcFindBestMatch_extDict_selectMLS(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint mls; + switch (ms->cParams.minMatch) + { + default: + case 4: + { + mls = 4; + break; + } + + case 5: + { + mls = 5; + break; + } + + case 7: + case 6: + { + mls = 6; + break; + } + } + return ZSTD_HcFindBestMatch_generic(ms, ip, iLimit, offsetPtr, mls, ZSTD_dictMode_e.ZSTD_extDict); + } + + /* Returns a 128-bit container with 128-bits from src */ + [InlineMethod.Inline] + private static Vector128 ZSTD_Vec128_read(void* src) + { +#if NETCOREAPP3_0_OR_GREATER + if (Sse2.IsSupported) + { + return Sse2.LoadVector128((byte*) src); + } +#endif +#if NET5_0_OR_GREATER + if (AdvSimd.IsSupported) + { + return AdvSimd.LoadVector128((byte*) src); + } +#endif + Vector128 ret; + memcpy(&ret, src, sizeof(Vector128)); + return ret; + } + + /* Returns a ZSTD_Vec128 with the byte "val" packed 16 times */ + [InlineMethod.Inline] + private static Vector128 ZSTD_Vec128_set8(byte val) + { + return Vector128.Create(val); + } + +#if NET5_0_OR_GREATER + /* Mimics '_mm_movemask_epi8()' from SSE */ + static uint ZSTD_vmovmaskq_u8(Vector128 val) + { + /* Shift out everything but the MSB bits in each byte */ + Vector128 highBits = AdvSimd.ShiftRightLogical(val, 7).As(); + /* Merge the even lanes together with vsra (right shift and add) */ + Vector128 paired16 = AdvSimd.ShiftRightLogicalAdd(highBits, highBits, 7).As(); + Vector128 paired32 = AdvSimd.ShiftRightLogicalAdd(paired16, paired16, 14).As(); + Vector128 paired64 = AdvSimd.ShiftRightLogicalAdd(paired32, paired32, 28).As(); + /* Extract the low 8 bits from each lane, merge */ + return AdvSimd.Extract(paired64, 0) | ((uint) AdvSimd.Extract(paired64, 8) << 8); + } +#endif + + /* Do byte-by-byte comparison result of x and y. Then collapse 128-bit resultant mask + * into a 32-bit mask that is the MSB of each byte. + * */ + [InlineMethod.Inline] + private static uint ZSTD_Vec128_cmpMask8(Vector128 x, Vector128 y) + { +#if NETCOREAPP3_0_OR_GREATER + if (Sse2.IsSupported) + { + return (uint) Sse2.MoveMask(Sse2.CompareEqual(x, y)); + } +#endif +#if NET5_0_OR_GREATER + if (AdvSimd.IsSupported) + { + return ZSTD_vmovmaskq_u8(AdvSimd.CompareEqual(x, y)); + } +#endif + uint res = 0; + int i = 0; + int l = 0; + for (; i < Vector128.Count; ++i) + { + nuint cmp1 = x.As().GetElement(i); + nuint cmp2 = y.As().GetElement(i); + int j = 0; + for (; j < sizeof(nuint); ++j, ++l) + { + if (((cmp1 >> j * 8) & 0xFF) == ((cmp2 >> j * 8) & 0xFF)) + { + res |= ((uint)1 << (j + i * sizeof(nuint))); + } + } + } + return res; + } + + [InlineMethod.Inline] + private static ZSTD_Vec256 ZSTD_Vec256_read(void* ptr) + { + ZSTD_Vec256 v; + + v.fst = ZSTD_Vec128_read(ptr); + v.snd = ZSTD_Vec128_read((Vector128*) ptr + 1); + return v; + } + + [InlineMethod.Inline] + private static ZSTD_Vec256 ZSTD_Vec256_set8(byte val) + { + ZSTD_Vec256 v; + + v.fst = ZSTD_Vec128_set8(val); + v.snd = ZSTD_Vec128_set8(val); + return v; + } + + [InlineMethod.Inline] + private static uint ZSTD_Vec256_cmpMask8(ZSTD_Vec256 x, ZSTD_Vec256 y) + { + uint fstMask; + uint sndMask; + + fstMask = ZSTD_Vec128_cmpMask8(x.fst, y.fst); + sndMask = ZSTD_Vec128_cmpMask8(x.snd, y.snd); + return fstMask | (sndMask << 16); + } + + /* ZSTD_VecMask_next(): + * Starting from the LSB, returns the idx of the next non-zero bit. + * Basically counting the nb of trailing zeroes. + */ + [InlineMethod.Inline] + private static uint ZSTD_VecMask_next(uint val) + { + if (val == 0) + { + return 0; + } + + return (uint)BitOperations.TrailingZeroCount(val); + } + + /* ZSTD_VecMask_rotateRight(): + * Rotates a bitfield to the right by "rotation" bits. + * If the rotation is greater than totalBits, the returned mask is 0. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint ZSTD_VecMask_rotateRight(uint mask, uint rotation, uint totalBits) + { + if (rotation == 0) + { + return mask; + } + + switch (totalBits) + { + default: + { + assert(0 != 0); + } + + + goto case 16; + case 16: + { + return (mask >> (int)rotation) | (ushort)(mask << (int)(16 - rotation)); + } + + case 32: + { + return (mask >> (int)rotation) | (uint)(mask << (int)(32 - rotation)); + } + } + } + + /* ZSTD_row_nextIndex(): + * Returns the next index to insert at within a tagTable row, and updates the "head" + * value to reflect the update. Essentially cycles backwards from [0, {entries per row}) + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint ZSTD_row_nextIndex(byte* tagRow, uint rowMask) + { + uint next = (uint)((*tagRow - 1)) & rowMask; + + *tagRow = (byte)(next); + return next; + } + + /* ZSTD_isAligned(): + * Checks that a pointer is aligned to "align" bytes which must be a power of 2. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int ZSTD_isAligned(void* ptr, nuint align) + { + assert((align & (align - 1)) == 0); + return (((((nuint)(ptr)) & (align - 1)) == 0) ? 1 : 0); + } + + /* ZSTD_row_prefetch(): + * Performs prefetching for the hashTable and tagTable at a given row. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void ZSTD_row_prefetch(uint* hashTable, ushort* tagTable, uint relRow, uint rowLog) + { + Prefetch0((void*)(hashTable + relRow)); + if (rowLog == 5) + { + Prefetch0((void*)(hashTable + relRow + 16)); + } + + Prefetch0((void*)(tagTable + relRow)); + assert(rowLog == 4 || rowLog == 5); + assert((ZSTD_isAligned((void*)(hashTable + relRow), 64)) != 0); + assert((ZSTD_isAligned((void*)(tagTable + relRow), (nuint)(1) << (int)rowLog)) != 0); + } + + /* ZSTD_row_fillHashCache(): + * Fill up the hash cache starting at idx, prefetching up to ZSTD_ROW_HASH_CACHE_SIZE entries, + * but not beyond iLimit. + */ + private static void ZSTD_row_fillHashCache(ZSTD_matchState_t* ms, byte* @base, uint rowLog, uint mls, uint idx, byte* iLimit) + { + uint* hashTable = ms->hashTable; + ushort* tagTable = ms->tagTable; + uint hashLog = ms->rowHashLog; + uint maxElemsToPrefetch = (uint)((@base + idx) > iLimit ? 0 : (uint)(iLimit - (@base + idx) + 1)); + uint lim = idx + (uint)((8) < (maxElemsToPrefetch) ? (8) : (maxElemsToPrefetch)); + + for (; idx < lim; ++idx) + { + uint hash = (uint)(ZSTD_hashPtr((void*)(@base + idx), hashLog + 8, mls)); + uint row = (hash >> 8) << (int)rowLog; + + ZSTD_row_prefetch(hashTable, tagTable, row, rowLog); + ms->hashCache[idx & (uint)((8 - 1))] = hash; + } + + } + + /* ZSTD_row_nextCachedHash(): + * Returns the hash of base + idx, and replaces the hash in the hash cache with the byte at + * base + idx + ZSTD_ROW_HASH_CACHE_SIZE. Also prefetches the appropriate rows from hashTable and tagTable. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint ZSTD_row_nextCachedHash(uint* cache, uint* hashTable, ushort* tagTable, byte* @base, uint idx, uint hashLog, uint rowLog, uint mls) + { + uint newHash = (uint)(ZSTD_hashPtr((void*)(@base + idx + 8), hashLog + 8, mls)); + uint row = (newHash >> 8) << (int)rowLog; + + ZSTD_row_prefetch(hashTable, tagTable, row, rowLog); + + { + uint hash = cache[idx & (uint)((8 - 1))]; + + cache[idx & (uint)((8 - 1))] = newHash; + return hash; + } + } + + /* ZSTD_row_update_internal(): + * Inserts the byte at ip into the appropriate position in the hash table. + * Determines the relative row, and the position within the {16, 32} entry row to insert at. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static void ZSTD_row_update_internal(ZSTD_matchState_t* ms, byte* ip, uint mls, uint rowLog, uint rowMask, uint useCache) + { + uint* hashTable = ms->hashTable; + ushort* tagTable = ms->tagTable; + uint hashLog = ms->rowHashLog; + byte* @base = ms->window.@base; + uint target = (uint)(ip - @base); + uint idx = ms->nextToUpdate; + + for (; idx < target; ++idx) + { + uint hash = useCache != 0 ? ZSTD_row_nextCachedHash((uint*)ms->hashCache, hashTable, tagTable, @base, idx, hashLog, rowLog, mls) : (uint)(ZSTD_hashPtr((void*)(@base + idx), hashLog + 8, mls)); + uint relRow = (hash >> 8) << (int)rowLog; + uint* row = hashTable + relRow; + byte* tagRow = (byte*)(tagTable + relRow); + uint pos = ZSTD_row_nextIndex(tagRow, rowMask); + + assert(hash == ZSTD_hashPtr((void*)(@base + idx), hashLog + 8, mls)); + ((byte*)(tagRow))[pos + 1] = (byte)(hash & ((1U << 8) - 1)); + row[pos] = idx; + } + + ms->nextToUpdate = target; + } + + /* ZSTD_row_update(): + * External wrapper for ZSTD_row_update_internal(). Used for filling the hashtable during dictionary + * processing. + */ + public static void ZSTD_row_update(ZSTD_matchState_t* ms, byte* ip) + { + uint rowLog = (uint)(ms->cParams.searchLog < 5 ? 4 : 5); + uint rowMask = (1U << (int)rowLog) - 1; + uint mls = ((ms->cParams.minMatch) < (6) ? (ms->cParams.minMatch) : (6)); + + ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 0); + } + + /* Returns a ZSTD_VecMask (U32) that has the nth bit set to 1 if the newly-computed "tag" matches + * the hash at the nth position in a row of the tagTable. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static uint ZSTD_row_getMatchMask(byte* tagRow, byte tag, uint head, uint rowEntries) + { + uint matches = 0; + + if (rowEntries == 16) + { + var hashes = ZSTD_Vec128_read((void*)(tagRow + 1)); + var expandedTags = ZSTD_Vec128_set8(tag); + + matches = ZSTD_Vec128_cmpMask8(hashes, expandedTags); + } + else if (rowEntries == 32) + { + ZSTD_Vec256 hashes = ZSTD_Vec256_read((void*)(tagRow + 1)); + ZSTD_Vec256 expandedTags = ZSTD_Vec256_set8(tag); + + matches = ZSTD_Vec256_cmpMask8(hashes, expandedTags); + } + else + { + assert(0 != 0); + } + + return ZSTD_VecMask_rotateRight(matches, head, rowEntries); + } + + /* The high-level approach of the SIMD row based match finder is as follows: + * - Figure out where to insert the new entry: + * - Generate a hash from a byte along with an additional 1-byte "short hash". The additional byte is our "tag" + * - The hashTable is effectively split into groups or "rows" of 16 or 32 entries of U32, and the hash determines + * which row to insert into. + * - Determine the correct position within the row to insert the entry into. Each row of 16 or 32 can + * be considered as a circular buffer with a "head" index that resides in the tagTable. + * - Also insert the "tag" into the equivalent row and position in the tagTable. + * - Note: The tagTable has 17 or 33 1-byte entries per row, due to 16 or 32 tags, and 1 "head" entry. + * The 17 or 33 entry rows are spaced out to occur every 32 or 64 bytes, respectively, + * for alignment/performance reasons, leaving some bytes unused. + * - Use SIMD to efficiently compare the tags in the tagTable to the 1-byte "short hash" and + * generate a bitfield that we can cycle through to check the collisions in the hash table. + * - Pick the longest match. + */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static nuint ZSTD_RowFindBestMatch_generic(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr, uint mls, ZSTD_dictMode_e dictMode, uint rowLog) + { + uint* hashTable = ms->hashTable; + ushort* tagTable = ms->tagTable; + uint* hashCache = (uint*)ms->hashCache; + uint hashLog = ms->rowHashLog; + ZSTD_compressionParameters* cParams = &ms->cParams; + byte* @base = ms->window.@base; + byte* dictBase = ms->window.dictBase; + uint dictLimit = ms->window.dictLimit; + byte* prefixStart = @base + dictLimit; + byte* dictEnd = dictBase + dictLimit; + uint curr = (uint)(ip - @base); + uint maxDistance = 1U << (int)cParams->windowLog; + uint lowestValid = ms->window.lowLimit; + uint withinMaxDistance = (curr - lowestValid > maxDistance) ? curr - maxDistance : lowestValid; + uint isDictionary = (((ms->loadedDictEnd != 0)) ? 1U : 0U); + uint lowLimit = isDictionary != 0 ? lowestValid : withinMaxDistance; + uint rowEntries = (1U << (int)rowLog); + uint rowMask = rowEntries - 1; + uint cappedSearchLog = ((cParams->searchLog) < (rowLog) ? (cParams->searchLog) : (rowLog)); + uint nbAttempts = 1U << (int)cappedSearchLog; + nuint ml = (nuint)(4 - 1); + ZSTD_matchState_t* dms = ms->dictMatchState; + nuint ddsIdx = default; + uint ddsExtraAttempts = default; + uint dmsTag = default; + uint* dmsRow = default; + byte* dmsTagRow = default; + + if (dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) + { + uint ddsHashLog = dms->cParams.hashLog - 2; + + + { + ddsIdx = ZSTD_hashPtr((void*)ip, ddsHashLog, mls) << 2; + Prefetch0((void*)(&dms->hashTable[ddsIdx])); + } + + ddsExtraAttempts = cParams->searchLog > rowLog ? 1U << (int)(cParams->searchLog - rowLog) : 0; + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + uint* dmsHashTable = dms->hashTable; + ushort* dmsTagTable = dms->tagTable; + uint dmsHash = (uint)(ZSTD_hashPtr((void*)ip, dms->rowHashLog + 8, mls)); + uint dmsRelRow = (dmsHash >> 8) << (int)rowLog; + + dmsTag = dmsHash & ((1U << 8) - 1); + dmsTagRow = (byte*)(dmsTagTable + dmsRelRow); + dmsRow = dmsHashTable + dmsRelRow; + ZSTD_row_prefetch(dmsHashTable, dmsTagTable, dmsRelRow, rowLog); + } + + ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 1); + + { + uint hash = ZSTD_row_nextCachedHash(hashCache, hashTable, tagTable, @base, curr, hashLog, rowLog, mls); + uint relRow = (hash >> 8) << (int)rowLog; + uint tag = hash & ((1U << 8) - 1); + uint* row = hashTable + relRow; + byte* tagRow = (byte*)(tagTable + relRow); + uint head = *tagRow & rowMask; + uint* matchBuffer = stackalloc uint[32]; + nuint numMatches = 0; + nuint currMatch = 0; + uint matches = ZSTD_row_getMatchMask(tagRow, (byte)(tag), head, rowEntries); + + for (; (matches > 0) && (nbAttempts > 0); --nbAttempts , matches &= (matches - 1)) + { + uint matchPos = (head + ZSTD_VecMask_next(matches)) & rowMask; + uint matchIndex = row[matchPos]; + + assert(numMatches < rowEntries); + if (matchIndex < lowLimit) + { + break; + } + + if ((dictMode != ZSTD_dictMode_e.ZSTD_extDict) || matchIndex >= dictLimit) + { + Prefetch0((void*)(@base + matchIndex)); + } + else + { + Prefetch0((void*)(dictBase + matchIndex)); + } + + matchBuffer[numMatches++] = matchIndex; + } + + + { + uint pos = ZSTD_row_nextIndex(tagRow, rowMask); + + tagRow[pos + 1] = (byte)(tag); + row[pos] = ms->nextToUpdate++; + } + + for (; currMatch < numMatches; ++currMatch) + { + uint matchIndex = matchBuffer[currMatch]; + nuint currentMl = 0; + + assert(matchIndex < curr); + assert(matchIndex >= lowLimit); + if ((dictMode != ZSTD_dictMode_e.ZSTD_extDict) || matchIndex >= dictLimit) + { + byte* match = @base + matchIndex; + + assert(matchIndex >= dictLimit); + if (match[ml] == ip[ml]) + { + currentMl = ZSTD_count(ip, match, iLimit); + } + } + else + { + byte* match = dictBase + matchIndex; + + assert(match + 4 <= dictEnd); + if (MEM_read32((void*)match) == MEM_read32((void*)ip)) + { + currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, dictEnd, prefixStart) + 4; + } + } + + if (currentMl > ml) + { + ml = currentMl; + *offsetPtr = curr - matchIndex + (uint)((3 - 1)); + if (ip + currentMl == iLimit) + { + break; + } + } + } + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) + { + ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts + ddsExtraAttempts, dms, ip, iLimit, prefixStart, curr, dictLimit, ddsIdx); + } + else if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + uint dmsLowestIndex = dms->window.dictLimit; + byte* dmsBase = dms->window.@base; + byte* dmsEnd = dms->window.nextSrc; + uint dmsSize = (uint)(dmsEnd - dmsBase); + uint dmsIndexDelta = dictLimit - dmsSize; + + + { + uint head = *dmsTagRow & rowMask; + uint* matchBuffer = stackalloc uint[32]; + nuint numMatches = 0; + nuint currMatch = 0; + uint matches = ZSTD_row_getMatchMask(dmsTagRow, (byte)(dmsTag), head, rowEntries); + + for (; (matches > 0) && (nbAttempts > 0); --nbAttempts , matches &= (matches - 1)) + { + uint matchPos = (head + ZSTD_VecMask_next(matches)) & rowMask; + uint matchIndex = dmsRow[matchPos]; + + if (matchIndex < dmsLowestIndex) + { + break; + } + + Prefetch0((void*)(dmsBase + matchIndex)); + matchBuffer[numMatches++] = matchIndex; + } + + for (; currMatch < numMatches; ++currMatch) + { + uint matchIndex = matchBuffer[currMatch]; + nuint currentMl = 0; + + assert(matchIndex >= dmsLowestIndex); + assert(matchIndex < curr); + + { + byte* match = dmsBase + matchIndex; + + assert(match + 4 <= dmsEnd); + if (MEM_read32((void*)match) == MEM_read32((void*)ip)) + { + currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, dmsEnd, prefixStart) + 4; + } + } + + if (currentMl > ml) + { + ml = currentMl; + *offsetPtr = curr - (matchIndex + dmsIndexDelta) + (uint)((3 - 1)); + if (ip + currentMl == iLimit) + { + break; + } + } + } + } + } + + return ml; + } + + /* Inlining is important to hardwire a hot branch (template emulation) */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + private static nuint ZSTD_RowFindBestMatch_selectMLS(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, ZSTD_dictMode_e dictMode, nuint* offsetPtr, uint rowLog) + { + uint mls; + switch (ms->cParams.minMatch) + { + default: + case 4: + { + mls = 4; + break; + } + + case 5: + { + mls = 5; + break; + } + + case 7: + case 6: + { + mls = 6; + break; + } + } + return ZSTD_RowFindBestMatch_generic(ms, ip, iLimit, offsetPtr, mls, dictMode, rowLog); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_RowFindBestMatch_selectRowLog(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint cappedSearchLog = ((ms->cParams.searchLog) < (5) ? (ms->cParams.searchLog) : (5)); + + return ZSTD_RowFindBestMatch_selectMLS(ms, ip, iLimit, ZSTD_dictMode_e.ZSTD_noDict, offsetPtr, cappedSearchLog == 5U ? 5U : 4U); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_RowFindBestMatch_dictMatchState_selectRowLog(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint cappedSearchLog = ((ms->cParams.searchLog) < (5) ? (ms->cParams.searchLog) : (5)); + + return ZSTD_RowFindBestMatch_selectMLS(ms, ip, iLimit, ZSTD_dictMode_e.ZSTD_dictMatchState, offsetPtr, cappedSearchLog == 5U ? 5U : 4U); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_selectRowLog(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint cappedSearchLog = ((ms->cParams.searchLog) < (5) ? (ms->cParams.searchLog) : (5)); + + return ZSTD_RowFindBestMatch_selectMLS(ms, ip, iLimit, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, offsetPtr, cappedSearchLog == 5U ? 5U : 4U); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_RowFindBestMatch_extDict_selectRowLog(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + { + uint cappedSearchLog = ((ms->cParams.searchLog) < (5) ? (ms->cParams.searchLog) : (5)); + + return ZSTD_RowFindBestMatch_selectMLS(ms, ip, iLimit, ZSTD_dictMode_e.ZSTD_extDict, offsetPtr, cappedSearchLog == 5U ? 5U : 4U); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_compressBlock_lazy_generic(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize, searchMethod_e searchMethod, uint depth, ZSTD_dictMode_e dictMode) + { + byte* istart = (byte*)(src); + byte* ip = istart; + byte* anchor = istart; + byte* iend = istart + srcSize; + byte* ilimit = searchMethod == searchMethod_e.search_rowHash ? iend - 8 - 8 : iend - 8; + byte* @base = ms->window.@base; + uint prefixLowestIndex = ms->window.dictLimit; + byte* prefixLowest = @base + prefixLowestIndex; + uint rowLog = (uint)(ms->cParams.searchLog < 5 ? 4 : 5); + ; + + var searchMax = searchFuncs[(int)dictMode][(int)(searchMethod)] ?? throw new InvalidOperationException(); + uint offset_1 = rep[0], offset_2 = rep[1], savedOffset = 0; + int isDMS = ((dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) ? 1 : 0); + int isDDS = ((dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) ? 1 : 0); + int isDxS = ((isDMS != 0 || isDDS != 0) ? 1 : 0); + ZSTD_matchState_t* dms = ms->dictMatchState; + uint dictLowestIndex = isDxS != 0 ? dms->window.dictLimit : 0; + byte* dictBase = isDxS != 0 ? dms->window.@base : null; + byte* dictLowest = isDxS != 0 ? dictBase + dictLowestIndex : null; + byte* dictEnd = isDxS != 0 ? dms->window.nextSrc : null; + uint dictIndexDelta = isDxS != 0 ? prefixLowestIndex - (uint)(dictEnd - dictBase) : 0; + uint dictAndPrefixLength = (uint)((ip - prefixLowest) + (dictEnd - dictLowest)); + + assert(searchMax != null); + ip += ((dictAndPrefixLength == 0) ? 1 : 0); + if (dictMode == ZSTD_dictMode_e.ZSTD_noDict) + { + uint curr = (uint)(ip - @base); + uint windowLow = ZSTD_getLowestPrefixIndex(ms, curr, ms->cParams.windowLog); + uint maxRep = curr - windowLow; + + if (offset_2 > maxRep) + { + savedOffset = offset_2; offset_2 = 0; + } + + if (offset_1 > maxRep) + { + savedOffset = offset_1; offset_1 = 0; + } + } + + if (isDxS != 0) + { + assert(offset_1 <= dictAndPrefixLength); + assert(offset_2 <= dictAndPrefixLength); + } + + if (searchMethod == searchMethod_e.search_rowHash) + { + ZSTD_row_fillHashCache(ms, @base, rowLog, ((ms->cParams.minMatch) < (6) ? (ms->cParams.minMatch) : (6)), ms->nextToUpdate, ilimit); + } + + while (ip < ilimit) + { + nuint matchLength = 0; + nuint offset = 0; + byte* start = ip + 1; + + if (isDxS != 0) + { + uint repIndex = (uint)(ip - @base) + 1 - offset_1; + byte* repMatch = ((dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState || dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) && repIndex < prefixLowestIndex) ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; + + if (((uint)((prefixLowestIndex - 1) - repIndex) >= 3) && (MEM_read32((void*)repMatch) == MEM_read32((void*)(ip + 1)))) + { + byte* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; + + matchLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixLowest) + 4; + if (depth == 0) + { + goto _storeSequence; + } + } + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_noDict && (((offset_1 > 0) && (MEM_read32((void*)(ip + 1 - offset_1)) == MEM_read32((void*)(ip + 1)))))) + { + matchLength = ZSTD_count(ip + 1 + 4, ip + 1 + 4 - offset_1, iend) + 4; + if (depth == 0) + { + goto _storeSequence; + } + } + + + { + nuint offsetFound = 999999999; + searchMax = searchMax ?? throw new InvalidOperationException(); + nuint ml2 = searchMax(ms, ip, iend, &offsetFound); + + if (ml2 > matchLength) + { + matchLength = ml2; start = ip; offset = offsetFound; + } + } + + if (matchLength < 4) + { + ip += ((ip - anchor) >> 8) + 1; + continue; + } + + if (depth >= 1) + { + while (ip < ilimit) + { + ip++; + if ((dictMode == ZSTD_dictMode_e.ZSTD_noDict) && (offset) != 0 && (((offset_1 > 0) && (MEM_read32((void*)ip) == MEM_read32((void*)(ip - offset_1)))))) + { + nuint mlRep = ZSTD_count(ip + 4, ip + 4 - offset_1, iend) + 4; + int gain2 = (int)(mlRep * 3); + int gain1 = (int)(matchLength * 3 - ZSTD_highbit32((uint)(offset) + 1) + 1); + + if ((mlRep >= 4) && (gain2 > gain1)) + { + matchLength = mlRep; offset = 0; start = ip; + } + } + + if (isDxS != 0) + { + uint repIndex = (uint)(ip - @base) - offset_1; + byte* repMatch = repIndex < prefixLowestIndex ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; + + if (((uint)((prefixLowestIndex - 1) - repIndex) >= 3) && (MEM_read32((void*)repMatch) == MEM_read32((void*)ip))) + { + byte* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; + nuint mlRep = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repMatchEnd, prefixLowest) + 4; + int gain2 = (int)(mlRep * 3); + int gain1 = (int)(matchLength * 3 - ZSTD_highbit32((uint)(offset) + 1) + 1); + + if ((mlRep >= 4) && (gain2 > gain1)) + { + matchLength = mlRep; offset = 0; start = ip; + } + } + } + + + { + nuint offset2 = 999999999; + nuint ml2 = searchMax(ms, ip, iend, &offset2); + int gain2 = (int)(ml2 * 4 - ZSTD_highbit32((uint)(offset2) + 1)); + int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)(offset) + 1) + 4); + + if ((ml2 >= 4) && (gain2 > gain1)) + { + matchLength = ml2; offset = offset2; start = ip; + continue; + } + } + + if ((depth == 2) && (ip < ilimit)) + { + ip++; + if ((dictMode == ZSTD_dictMode_e.ZSTD_noDict) && (offset) != 0 && (((offset_1 > 0) && (MEM_read32((void*)ip) == MEM_read32((void*)(ip - offset_1)))))) + { + nuint mlRep = ZSTD_count(ip + 4, ip + 4 - offset_1, iend) + 4; + int gain2 = (int)(mlRep * 4); + int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)(offset) + 1) + 1); + + if ((mlRep >= 4) && (gain2 > gain1)) + { + matchLength = mlRep; offset = 0; start = ip; + } + } + + if (isDxS != 0) + { + uint repIndex = (uint)(ip - @base) - offset_1; + byte* repMatch = repIndex < prefixLowestIndex ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; + + if (((uint)((prefixLowestIndex - 1) - repIndex) >= 3) && (MEM_read32((void*)repMatch) == MEM_read32((void*)ip))) + { + byte* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; + nuint mlRep = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repMatchEnd, prefixLowest) + 4; + int gain2 = (int)(mlRep * 4); + int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)(offset) + 1) + 1); + + if ((mlRep >= 4) && (gain2 > gain1)) + { + matchLength = mlRep; offset = 0; start = ip; + } + } + } + + + { + nuint offset2 = 999999999; + nuint ml2 = searchMax(ms, ip, iend, &offset2); + int gain2 = (int)(ml2 * 4 - ZSTD_highbit32((uint)(offset2) + 1)); + int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)(offset) + 1) + 7); + + if ((ml2 >= 4) && (gain2 > gain1)) + { + matchLength = ml2; offset = offset2; start = ip; + continue; + } + } + } + + break; + } + } + + if (offset != 0) + { + if (dictMode == ZSTD_dictMode_e.ZSTD_noDict) + { + while ((((start > anchor) && (start - (offset - (uint)((3 - 1))) > prefixLowest))) && (start[-1] == (start - (offset - (uint)((3 - 1))))[-1])) + { + start--; + matchLength++; + } + } + + if (isDxS != 0) + { + uint matchIndex = (uint)((ulong)((start - @base)) - (offset - (uint)((3 - 1)))); + byte* match = (matchIndex < prefixLowestIndex) ? dictBase + matchIndex - dictIndexDelta : @base + matchIndex; + byte* mStart = (matchIndex < prefixLowestIndex) ? dictLowest : prefixLowest; + + while ((start > anchor) && (match > mStart) && (start[-1] == match[-1])) + { + start--; + match--; + matchLength++; + } + } + + offset_2 = offset_1; + offset_1 = (uint)(offset - (uint)((3 - 1))); + } + + _storeSequence: + { + nuint litLength = (nuint)(start - anchor); + + ZSTD_storeSeq(seqStore, litLength, anchor, iend, (uint)(offset), matchLength - 3); + anchor = ip = start + matchLength; + } + + if (isDxS != 0) + { + while (ip <= ilimit) + { + uint current2 = (uint)(ip - @base); + uint repIndex = current2 - offset_2; + byte* repMatch = repIndex < prefixLowestIndex ? dictBase - dictIndexDelta + repIndex : @base + repIndex; + + if (((uint)((prefixLowestIndex - 1) - (uint)(repIndex)) >= 3) && (MEM_read32((void*)repMatch) == MEM_read32((void*)ip))) + { + byte* repEnd2 = repIndex < prefixLowestIndex ? dictEnd : iend; + + matchLength = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repEnd2, prefixLowest) + 4; + offset = offset_2; + offset_2 = offset_1; + offset_1 = (uint)(offset); + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, matchLength - 3); + ip += matchLength; + anchor = ip; + continue; + } + + break; + } + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_noDict) + { + while ((((ip <= ilimit) && (offset_2 > 0))) && (MEM_read32((void*)ip) == MEM_read32((void*)(ip - offset_2)))) + { + matchLength = ZSTD_count(ip + 4, ip + 4 - offset_2, iend) + 4; + offset = offset_2; + offset_2 = offset_1; + offset_1 = (uint)(offset); + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, matchLength - 3); + ip += matchLength; + anchor = ip; + continue; + } + } + } + + rep[0] = offset_1 != 0 ? offset_1 : savedOffset; + rep[1] = offset_2 != 0 ? offset_2 : savedOffset; + return (nuint)(iend - anchor); + } + + /*! used in ZSTD_reduceIndex(). preemptively increase value of ZSTD_DUBT_UNSORTED_MARK */ + public static nuint ZSTD_compressBlock_btlazy2(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_binaryTree, 2, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_lazy2(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 2, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_lazy(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 1, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_greedy(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 0, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_btlazy2_dictMatchState(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_binaryTree, 2, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_lazy2_dictMatchState(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 2, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_lazy_dictMatchState(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 1, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_greedy_dictMatchState(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 0, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_lazy2_dedicatedDictSearch(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 2, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + } + + public static nuint ZSTD_compressBlock_lazy_dedicatedDictSearch(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 1, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + } + + public static nuint ZSTD_compressBlock_greedy_dedicatedDictSearch(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 0, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + } + + /* Row-based matchfinder */ + public static nuint ZSTD_compressBlock_lazy2_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 2, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_lazy_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 1, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_greedy_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 0, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_lazy2_dictMatchState_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 2, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_lazy_dictMatchState_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 1, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_greedy_dictMatchState_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 0, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_lazy2_dedicatedDictSearch_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 2, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + } + + public static nuint ZSTD_compressBlock_lazy_dedicatedDictSearch_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 1, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + } + + public static nuint ZSTD_compressBlock_greedy_dedicatedDictSearch_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 0, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_compressBlock_lazy_extDict_generic(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize, searchMethod_e searchMethod, uint depth) + { + byte* istart = (byte*)(src); + byte* ip = istart; + byte* anchor = istart; + byte* iend = istart + srcSize; + byte* ilimit = searchMethod == searchMethod_e.search_rowHash ? iend - 8 - 8 : iend - 8; + byte* @base = ms->window.@base; + uint dictLimit = ms->window.dictLimit; + byte* prefixStart = @base + dictLimit; + byte* dictBase = ms->window.dictBase; + byte* dictEnd = dictBase + dictLimit; + byte* dictStart = dictBase + ms->window.lowLimit; + uint windowLog = ms->cParams.windowLog; + uint rowLog = (uint)(ms->cParams.searchLog < 5 ? 4 : 5); + ; + + searchMax_f searchMax = searchFuncsExtGeneric[(int)(searchMethod)]; + uint offset_1 = rep[0], offset_2 = rep[1]; + + ip += ((ip == prefixStart) ? 1 : 0); + if (searchMethod == searchMethod_e.search_rowHash) + { + ZSTD_row_fillHashCache(ms, @base, rowLog, ((ms->cParams.minMatch) < (6) ? (ms->cParams.minMatch) : (6)), ms->nextToUpdate, ilimit); + } + + while (ip < ilimit) + { + nuint matchLength = 0; + nuint offset = 0; + byte* start = ip + 1; + uint curr = (uint)(ip - @base); + + + { + uint windowLow = ZSTD_getLowestMatchIndex(ms, curr + 1, windowLog); + uint repIndex = (uint)(curr + 1 - offset_1); + byte* repBase = repIndex < dictLimit ? dictBase : @base; + byte* repMatch = repBase + repIndex; + + if ((((uint)((dictLimit - 1) - repIndex) >= 3) && (offset_1 < curr + 1 - windowLow))) + { + if (MEM_read32((void*)(ip + 1)) == MEM_read32((void*)repMatch)) + { + byte* repEnd = repIndex < dictLimit ? dictEnd : iend; + + matchLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repEnd, prefixStart) + 4; + if (depth == 0) + { + goto _storeSequence; + } + } + } + } + + + { + nuint offsetFound = 999999999; + nuint ml2 = searchMax(ms, ip, iend, &offsetFound); + + if (ml2 > matchLength) + { + matchLength = ml2; start = ip; offset = offsetFound; + } + } + + if (matchLength < 4) + { + ip += ((ip - anchor) >> 8) + 1; + continue; + } + + if (depth >= 1) + { + while (ip < ilimit) + { + ip++; + curr++; + if (offset != 0) + { + uint windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog); + uint repIndex = (uint)(curr - offset_1); + byte* repBase = repIndex < dictLimit ? dictBase : @base; + byte* repMatch = repBase + repIndex; + + if ((((uint)((dictLimit - 1) - repIndex) >= 3) && (offset_1 < curr - windowLow))) + { + if (MEM_read32((void*)ip) == MEM_read32((void*)repMatch)) + { + byte* repEnd = repIndex < dictLimit ? dictEnd : iend; + nuint repLength = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repEnd, prefixStart) + 4; + int gain2 = (int)(repLength * 3); + int gain1 = (int)(matchLength * 3 - ZSTD_highbit32((uint)(offset) + 1) + 1); + + if ((repLength >= 4) && (gain2 > gain1)) + { + matchLength = repLength; offset = 0; start = ip; + } + } + } + } + + + { + nuint offset2 = 999999999; + nuint ml2 = searchMax(ms, ip, iend, &offset2); + int gain2 = (int)(ml2 * 4 - ZSTD_highbit32((uint)(offset2) + 1)); + int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)(offset) + 1) + 4); + + if ((ml2 >= 4) && (gain2 > gain1)) + { + matchLength = ml2; offset = offset2; start = ip; + continue; + } + } + + if ((depth == 2) && (ip < ilimit)) + { + ip++; + curr++; + if (offset != 0) + { + uint windowLow = ZSTD_getLowestMatchIndex(ms, curr, windowLog); + uint repIndex = (uint)(curr - offset_1); + byte* repBase = repIndex < dictLimit ? dictBase : @base; + byte* repMatch = repBase + repIndex; + + if ((((uint)((dictLimit - 1) - repIndex) >= 3) && (offset_1 < curr - windowLow))) + { + if (MEM_read32((void*)ip) == MEM_read32((void*)repMatch)) + { + byte* repEnd = repIndex < dictLimit ? dictEnd : iend; + nuint repLength = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repEnd, prefixStart) + 4; + int gain2 = (int)(repLength * 4); + int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)(offset) + 1) + 1); + + if ((repLength >= 4) && (gain2 > gain1)) + { + matchLength = repLength; offset = 0; start = ip; + } + } + } + } + + + { + nuint offset2 = 999999999; + nuint ml2 = searchMax(ms, ip, iend, &offset2); + int gain2 = (int)(ml2 * 4 - ZSTD_highbit32((uint)(offset2) + 1)); + int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)(offset) + 1) + 7); + + if ((ml2 >= 4) && (gain2 > gain1)) + { + matchLength = ml2; offset = offset2; start = ip; + continue; + } + } + } + + break; + } + } + + if (offset != 0) + { + uint matchIndex = (uint)((ulong)((start - @base)) - (offset - (uint)((3 - 1)))); + byte* match = (matchIndex < dictLimit) ? dictBase + matchIndex : @base + matchIndex; + byte* mStart = (matchIndex < dictLimit) ? dictStart : prefixStart; + + while ((start > anchor) && (match > mStart) && (start[-1] == match[-1])) + { + start--; + match--; + matchLength++; + } + + offset_2 = offset_1; + offset_1 = (uint)(offset - (uint)((3 - 1))); + } + + _storeSequence: + { + nuint litLength = (nuint)(start - anchor); + + ZSTD_storeSeq(seqStore, litLength, anchor, iend, (uint)(offset), matchLength - 3); + anchor = ip = start + matchLength; + } + + while (ip <= ilimit) + { + uint repCurrent = (uint)(ip - @base); + uint windowLow = ZSTD_getLowestMatchIndex(ms, repCurrent, windowLog); + uint repIndex = repCurrent - offset_2; + byte* repBase = repIndex < dictLimit ? dictBase : @base; + byte* repMatch = repBase + repIndex; + + if ((((uint)((dictLimit - 1) - repIndex) >= 3) && (offset_2 < repCurrent - windowLow))) + { + if (MEM_read32((void*)ip) == MEM_read32((void*)repMatch)) + { + byte* repEnd = repIndex < dictLimit ? dictEnd : iend; + + matchLength = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repEnd, prefixStart) + 4; + offset = offset_2; + offset_2 = offset_1; + offset_1 = (uint)(offset); + ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, matchLength - 3); + ip += matchLength; + anchor = ip; + continue; + } + } + + break; + } + } + + rep[0] = offset_1; + rep[1] = offset_2; + return (nuint)(iend - anchor); + } + + public static nuint ZSTD_compressBlock_greedy_extDict(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 0); + } + + public static nuint ZSTD_compressBlock_lazy_extDict(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 1); + } + + public static nuint ZSTD_compressBlock_lazy2_extDict(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_hashChain, 2); + } + + public static nuint ZSTD_compressBlock_btlazy2_extDict(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_binaryTree, 2); + } + + public static nuint ZSTD_compressBlock_greedy_extDict_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 0); + } + + public static nuint ZSTD_compressBlock_lazy_extDict_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 1); + } + + public static nuint ZSTD_compressBlock_lazy2_extDict_row(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_lazy_extDict_generic(ms, seqStore, rep, src, srcSize, searchMethod_e.search_rowHash, 2); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLdm.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLdm.cs new file mode 100644 index 00000000..ff047689 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLdm.cs @@ -0,0 +1,816 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + /** ZSTD_ldm_gear_init(): + * + * Initializes the rolling hash state such that it will honor the + * settings in params. */ + private static void ZSTD_ldm_gear_init(ldmRollingHashState_t* state, ldmParams_t* @params) + { + uint maxBitsInMask = ((@params->minMatchLength) < (64) ? (@params->minMatchLength) : (64)); + uint hashRateLog = @params->hashRateLog; + + state->rolling = unchecked(~(uint)(0)); + if (hashRateLog > 0 && hashRateLog <= maxBitsInMask) + { + state->stopMask = (((ulong)(1) << (int)hashRateLog) - 1) << (int)(maxBitsInMask - hashRateLog); + } + else + { + state->stopMask = ((ulong)(1) << (int)hashRateLog) - 1; + } + } + + /** ZSTD_ldm_gear_reset() + * Feeds [data, data + minMatchLength) into the hash without registering any + * splits. This effectively resets the hash state. This is used when skipping + * over data, either at the beginning of a block, or skipping sections. + */ + private static void ZSTD_ldm_gear_reset(ldmRollingHashState_t* state, byte* data, nuint minMatchLength) + { + ulong hash = state->rolling; + nuint n = 0; + + while (n + 3 < minMatchLength) + { + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + } + + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + } + + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + } + + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + } + } + + while (n < minMatchLength) + { + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + } + } + } + + /** ZSTD_ldm_gear_feed(): + * + * Registers in the splits array all the split points found in the first + * size bytes following the data pointer. This function terminates when + * either all the data has been processed or LDM_BATCH_SIZE splits are + * present in the splits array. + * + * Precondition: The splits array must not be full. + * Returns: The number of bytes processed. */ + private static nuint ZSTD_ldm_gear_feed(ldmRollingHashState_t* state, byte* data, nuint size, nuint* splits, uint* numSplits) + { + nuint n; + ulong hash, mask; + + hash = state->rolling; + mask = state->stopMask; + n = 0; + while (n + 3 < size) + { + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + if (((hash & mask) == 0)) + { + splits[*numSplits] = n; + *numSplits += 1; + if (*numSplits == 64) + { + goto done; + } + } + } + + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + if (((hash & mask) == 0)) + { + splits[*numSplits] = n; + *numSplits += 1; + if (*numSplits == 64) + { + goto done; + } + } + } + + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + if (((hash & mask) == 0)) + { + splits[*numSplits] = n; + *numSplits += 1; + if (*numSplits == 64) + { + goto done; + } + } + } + + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + if (((hash & mask) == 0)) + { + splits[*numSplits] = n; + *numSplits += 1; + if (*numSplits == 64) + { + goto done; + } + } + } + } + + while (n < size) + { + + { + hash = (hash << 1) + ZSTD_ldm_gearTab[data[n] & 0xff]; + n += 1; + if (((hash & mask) == 0)) + { + splits[*numSplits] = n; + *numSplits += 1; + if (*numSplits == 64) + { + goto done; + } + } + } + } + + done: + state->rolling = hash; + return n; + } + + /** ZSTD_ldm_adjustParameters() : + * If the params->hashRateLog is not set, set it to its default value based on + * windowLog and params->hashLog. + * + * Ensures that params->bucketSizeLog is <= params->hashLog (setting it to + * params->hashLog if it is not). + * + * Ensures that the minMatchLength >= targetLength during optimal parsing. + */ + public static void ZSTD_ldm_adjustParameters(ldmParams_t* @params, ZSTD_compressionParameters* cParams) + { + @params->windowLog = cParams->windowLog; + if (@params->bucketSizeLog == 0) + { + @params->bucketSizeLog = 3; + } + + if (@params->minMatchLength == 0) + { + @params->minMatchLength = 64; + } + + if (@params->hashLog == 0) + { + @params->hashLog = (uint)((6) > (@params->windowLog - 7) ? (6) : (@params->windowLog - 7)); + assert(@params->hashLog <= (uint)(((((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) < 30) ? ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31)) : 30))); + } + + if (@params->hashRateLog == 0) + { + @params->hashRateLog = (uint)(@params->windowLog < @params->hashLog ? 0 : @params->windowLog - @params->hashLog); + } + + @params->bucketSizeLog = ((@params->bucketSizeLog) < (@params->hashLog) ? (@params->bucketSizeLog) : (@params->hashLog)); + } + + /** ZSTD_ldm_getTableSize() : + * Estimate the space needed for long distance matching tables or 0 if LDM is + * disabled. + */ + public static nuint ZSTD_ldm_getTableSize(ldmParams_t @params) + { + nuint ldmHSize = ((nuint)(1)) << (int)@params.hashLog; + nuint ldmBucketSizeLog = ((@params.bucketSizeLog) < (@params.hashLog) ? (@params.bucketSizeLog) : (@params.hashLog)); + nuint ldmBucketSize = ((nuint)(1)) << (int)(@params.hashLog - ldmBucketSizeLog); + nuint totalSize = ZSTD_cwksp_alloc_size(ldmBucketSize) + ZSTD_cwksp_alloc_size(ldmHSize * (nuint)(8)); + + return @params.enableLdm != 0 ? totalSize : 0; + } + + /** ZSTD_ldm_getSeqSpace() : + * Return an upper bound on the number of sequences that can be produced by + * the long distance matcher, or 0 if LDM is disabled. + */ + public static nuint ZSTD_ldm_getMaxNbSeq(ldmParams_t @params, nuint maxChunkSize) + { + return @params.enableLdm != 0 ? (maxChunkSize / @params.minMatchLength) : 0; + } + + /** ZSTD_ldm_getBucket() : + * Returns a pointer to the start of the bucket associated with hash. */ + private static ldmEntry_t* ZSTD_ldm_getBucket(ldmState_t* ldmState, nuint hash, ldmParams_t ldmParams) + { + return ldmState->hashTable + (hash << (int)ldmParams.bucketSizeLog); + } + + /** ZSTD_ldm_insertEntry() : + * Insert the entry with corresponding hash into the hash table */ + private static void ZSTD_ldm_insertEntry(ldmState_t* ldmState, nuint hash, ldmEntry_t entry, ldmParams_t ldmParams) + { + byte* pOffset = ldmState->bucketOffsets + hash; + uint offset = *pOffset; + + *(ZSTD_ldm_getBucket(ldmState, hash, ldmParams) + offset) = entry; + *pOffset = (byte)((offset + 1) & ((1U << (int)ldmParams.bucketSizeLog) - 1)); + } + + /** ZSTD_ldm_countBackwardsMatch() : + * Returns the number of bytes that match backwards before pIn and pMatch. + * + * We count only bytes where pMatch >= pBase and pIn >= pAnchor. */ + private static nuint ZSTD_ldm_countBackwardsMatch(byte* pIn, byte* pAnchor, byte* pMatch, byte* pMatchBase) + { + nuint matchLength = 0; + + while (pIn > pAnchor && pMatch > pMatchBase && pIn[-1] == pMatch[-1]) + { + pIn--; + pMatch--; + matchLength++; + } + + return matchLength; + } + + /** ZSTD_ldm_countBackwardsMatch_2segments() : + * Returns the number of bytes that match backwards from pMatch, + * even with the backwards match spanning 2 different segments. + * + * On reaching `pMatchBase`, start counting from mEnd */ + private static nuint ZSTD_ldm_countBackwardsMatch_2segments(byte* pIn, byte* pAnchor, byte* pMatch, byte* pMatchBase, byte* pExtDictStart, byte* pExtDictEnd) + { + nuint matchLength = ZSTD_ldm_countBackwardsMatch(pIn, pAnchor, pMatch, pMatchBase); + + if (pMatch - matchLength != pMatchBase || pMatchBase == pExtDictStart) + { + return matchLength; + } + + matchLength += ZSTD_ldm_countBackwardsMatch(pIn - matchLength, pAnchor, pExtDictEnd, pExtDictStart); + return matchLength; + } + + /** ZSTD_ldm_fillFastTables() : + * + * Fills the relevant tables for the ZSTD_fast and ZSTD_dfast strategies. + * This is similar to ZSTD_loadDictionaryContent. + * + * The tables for the other strategies are filled within their + * block compressors. */ + private static nuint ZSTD_ldm_fillFastTables(ZSTD_matchState_t* ms, void* end) + { + byte* iend = (byte*)(end); + + switch (ms->cParams.strategy) + { + case ZSTD_strategy.ZSTD_fast: + { + ZSTD_fillHashTable(ms, (void*)iend, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast); + } + + break; + case ZSTD_strategy.ZSTD_dfast: + { + ZSTD_fillDoubleHashTable(ms, (void*)iend, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast); + } + + break; + case ZSTD_strategy.ZSTD_greedy: + case ZSTD_strategy.ZSTD_lazy: + case ZSTD_strategy.ZSTD_lazy2: + case ZSTD_strategy.ZSTD_btlazy2: + case ZSTD_strategy.ZSTD_btopt: + case ZSTD_strategy.ZSTD_btultra: + case ZSTD_strategy.ZSTD_btultra2: + { + break; + } + + default: + { + assert(0 != 0); + } + break; + } + + return 0; + } + + public static void ZSTD_ldm_fillHashTable(ldmState_t* ldmState, byte* ip, byte* iend, ldmParams_t* @params) + { + uint minMatchLength = @params->minMatchLength; + uint hBits = @params->hashLog - @params->bucketSizeLog; + byte* @base = ldmState->window.@base; + byte* istart = ip; + ldmRollingHashState_t hashState; + nuint* splits = (nuint*)ldmState->splitIndices; + uint numSplits; + + ZSTD_ldm_gear_init(&hashState, @params); + while (ip < iend) + { + nuint hashed; + uint n; + + numSplits = 0; + hashed = ZSTD_ldm_gear_feed(&hashState, ip, (nuint)(iend - ip), splits, &numSplits); + for (n = 0; n < numSplits; n++) + { + if (ip + splits[n] >= istart + minMatchLength) + { + byte* split = ip + splits[n] - minMatchLength; + ulong xxhash = XXH64((void*)split, minMatchLength, 0); + uint hash = (uint)(xxhash & (((uint)(1) << (int)hBits) - 1)); + ldmEntry_t entry; + + entry.offset = (uint)(split - @base); + entry.checksum = (uint)(xxhash >> 32); + ZSTD_ldm_insertEntry(ldmState, hash, entry, *@params); + } + } + + ip += hashed; + } + } + + /** ZSTD_ldm_limitTableUpdate() : + * + * Sets cctx->nextToUpdate to a position corresponding closer to anchor + * if it is far way + * (after a long match, only update tables a limited amount). */ + private static void ZSTD_ldm_limitTableUpdate(ZSTD_matchState_t* ms, byte* anchor) + { + uint curr = (uint)(anchor - ms->window.@base); + + if (curr > ms->nextToUpdate + 1024) + { + ms->nextToUpdate = curr - (uint)((512) < (curr - ms->nextToUpdate - 1024) ? (512) : (curr - ms->nextToUpdate - 1024)); + } + } + + private static nuint ZSTD_ldm_generateSequences_internal(ldmState_t* ldmState, rawSeqStore_t* rawSeqStore, ldmParams_t* @params, void* src, nuint srcSize) + { + int extDict = (int)(ZSTD_window_hasExtDict(ldmState->window)); + uint minMatchLength = @params->minMatchLength; + uint entsPerBucket = 1U << (int)@params->bucketSizeLog; + uint hBits = @params->hashLog - @params->bucketSizeLog; + uint dictLimit = ldmState->window.dictLimit; + uint lowestIndex = extDict != 0 ? ldmState->window.lowLimit : dictLimit; + byte* @base = ldmState->window.@base; + byte* dictBase = extDict != 0 ? ldmState->window.dictBase : null; + byte* dictStart = extDict != 0 ? dictBase + lowestIndex : null; + byte* dictEnd = extDict != 0 ? dictBase + dictLimit : null; + byte* lowPrefixPtr = @base + dictLimit; + byte* istart = (byte*)(src); + byte* iend = istart + srcSize; + byte* ilimit = iend - 8; + byte* anchor = istart; + byte* ip = istart; + ldmRollingHashState_t hashState; + nuint* splits = (nuint*)ldmState->splitIndices; + ldmMatchCandidate_t* candidates = (ldmMatchCandidate_t*)ldmState->matchCandidates; + uint numSplits; + + if (srcSize < minMatchLength) + { + return (nuint)(iend - anchor); + } + + ZSTD_ldm_gear_init(&hashState, @params); + ZSTD_ldm_gear_reset(&hashState, ip, minMatchLength); + ip += minMatchLength; + while (ip < ilimit) + { + nuint hashed; + uint n; + + numSplits = 0; + hashed = ZSTD_ldm_gear_feed(&hashState, ip, (nuint)(ilimit - ip), splits, &numSplits); + for (n = 0; n < numSplits; n++) + { + byte* split = ip + splits[n] - minMatchLength; + ulong xxhash = XXH64((void*)split, minMatchLength, 0); + uint hash = (uint)(xxhash & (((uint)(1) << (int)hBits) - 1)); + + candidates[n].split = split; + candidates[n].hash = hash; + candidates[n].checksum = (uint)(xxhash >> 32); + candidates[n].bucket = ZSTD_ldm_getBucket(ldmState, hash, *@params); + Prefetch0((void*)(candidates[n].bucket)); + } + + for (n = 0; n < numSplits; n++) + { + nuint forwardMatchLength = 0, backwardMatchLength = 0, bestMatchLength = 0, mLength; + uint offset; + byte* split = candidates[n].split; + uint checksum = candidates[n].checksum; + uint hash = candidates[n].hash; + ldmEntry_t* bucket = candidates[n].bucket; + ldmEntry_t* cur; + ldmEntry_t* bestEntry = (ldmEntry_t*)null; + ldmEntry_t newEntry; + + newEntry.offset = (uint)(split - @base); + newEntry.checksum = checksum; + if (split < anchor) + { + ZSTD_ldm_insertEntry(ldmState, hash, newEntry, *@params); + continue; + } + + for (cur = bucket; cur < bucket + entsPerBucket; cur++) + { + nuint curForwardMatchLength, curBackwardMatchLength, curTotalMatchLength; + + if (cur->checksum != checksum || cur->offset <= lowestIndex) + { + continue; + } + + if (extDict != 0) + { + byte* curMatchBase = cur->offset < dictLimit ? dictBase : @base; + byte* pMatch = curMatchBase + cur->offset; + byte* matchEnd = cur->offset < dictLimit ? dictEnd : iend; + byte* lowMatchPtr = cur->offset < dictLimit ? dictStart : lowPrefixPtr; + + curForwardMatchLength = ZSTD_count_2segments(split, pMatch, iend, matchEnd, lowPrefixPtr); + if (curForwardMatchLength < minMatchLength) + { + continue; + } + + curBackwardMatchLength = ZSTD_ldm_countBackwardsMatch_2segments(split, anchor, pMatch, lowMatchPtr, dictStart, dictEnd); + } + else + { + byte* pMatch = @base + cur->offset; + + curForwardMatchLength = ZSTD_count(split, pMatch, iend); + if (curForwardMatchLength < minMatchLength) + { + continue; + } + + curBackwardMatchLength = ZSTD_ldm_countBackwardsMatch(split, anchor, pMatch, lowPrefixPtr); + } + + curTotalMatchLength = curForwardMatchLength + curBackwardMatchLength; + if (curTotalMatchLength > bestMatchLength) + { + bestMatchLength = curTotalMatchLength; + forwardMatchLength = curForwardMatchLength; + backwardMatchLength = curBackwardMatchLength; + bestEntry = cur; + } + } + + if (bestEntry == null) + { + ZSTD_ldm_insertEntry(ldmState, hash, newEntry, *@params); + continue; + } + + offset = (uint)(split - @base) - bestEntry->offset; + mLength = forwardMatchLength + backwardMatchLength; + + { + rawSeq* seq = rawSeqStore->seq + rawSeqStore->size; + + if (rawSeqStore->size == rawSeqStore->capacity) + { + return (unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall))); + } + + seq->litLength = (uint)(split - backwardMatchLength - anchor); + seq->matchLength = (uint)(mLength); + seq->offset = offset; + rawSeqStore->size++; + } + + ZSTD_ldm_insertEntry(ldmState, hash, newEntry, *@params); + anchor = split + forwardMatchLength; + if (anchor > ip + hashed) + { + ZSTD_ldm_gear_reset(&hashState, anchor - minMatchLength, minMatchLength); + ip = anchor - hashed; + break; + } + } + + ip += hashed; + } + + return (nuint)(iend - anchor); + } + + /*! ZSTD_ldm_reduceTable() : + * reduce table indexes by `reducerValue` */ + private static void ZSTD_ldm_reduceTable(ldmEntry_t* table, uint size, uint reducerValue) + { + uint u; + + for (u = 0; u < size; u++) + { + if (table[u].offset < reducerValue) + { + table[u].offset = 0; + } + else + { + table[u].offset -= reducerValue; + } + } + } + + /** + * ZSTD_ldm_generateSequences(): + * + * Generates the sequences using the long distance match finder. + * Generates long range matching sequences in `sequences`, which parse a prefix + * of the source. `sequences` must be large enough to store every sequence, + * which can be checked with `ZSTD_ldm_getMaxNbSeq()`. + * @returns 0 or an error code. + * + * NOTE: The user must have called ZSTD_window_update() for all of the input + * they have, even if they pass it to ZSTD_ldm_generateSequences() in chunks. + * NOTE: This function returns an error if it runs out of space to store + * sequences. + */ + public static nuint ZSTD_ldm_generateSequences(ldmState_t* ldmState, rawSeqStore_t* sequences, ldmParams_t* @params, void* src, nuint srcSize) + { + uint maxDist = 1U << (int)@params->windowLog; + byte* istart = (byte*)(src); + byte* iend = istart + srcSize; + nuint kMaxChunkSize = (nuint)(1 << 20); + nuint nbChunks = (srcSize / kMaxChunkSize) + (uint)(((((srcSize % kMaxChunkSize) != 0)) ? 1 : 0)); + nuint chunk; + nuint leftoverSize = 0; + + assert(((unchecked((uint)(-1))) - ((3U << 29) + (1U << ((int)((nuint)(sizeof(nuint)) == 4 ? 30 : 31))))) >= kMaxChunkSize); + assert(ldmState->window.nextSrc >= (byte*)(src) + srcSize); + assert(sequences->pos <= sequences->size); + assert(sequences->size <= sequences->capacity); + for (chunk = 0; chunk < nbChunks && sequences->size < sequences->capacity; ++chunk) + { + byte* chunkStart = istart + chunk * kMaxChunkSize; + nuint remaining = (nuint)(iend - chunkStart); + byte* chunkEnd = (remaining < kMaxChunkSize) ? iend : chunkStart + kMaxChunkSize; + nuint chunkSize = (nuint)(chunkEnd - chunkStart); + nuint newLeftoverSize; + nuint prevSize = sequences->size; + + assert(chunkStart < iend); + if ((ZSTD_window_needOverflowCorrection(ldmState->window, 0, maxDist, ldmState->loadedDictEnd, (void*)chunkStart, (void*)chunkEnd)) != 0) + { + uint ldmHSize = 1U << (int)@params->hashLog; + uint correction = ZSTD_window_correctOverflow(&ldmState->window, 0, maxDist, (void*)chunkStart); + + ZSTD_ldm_reduceTable(ldmState->hashTable, ldmHSize, correction); + ldmState->loadedDictEnd = 0; + } + + ZSTD_window_enforceMaxDist(&ldmState->window, (void*)chunkEnd, maxDist, &ldmState->loadedDictEnd, (ZSTD_matchState_t**)null); + newLeftoverSize = ZSTD_ldm_generateSequences_internal(ldmState, sequences, @params, (void*)chunkStart, chunkSize); + if ((ERR_isError(newLeftoverSize)) != 0) + { + return newLeftoverSize; + } + + if (prevSize < sequences->size) + { + sequences->seq[prevSize].litLength += (uint)(leftoverSize); + leftoverSize = newLeftoverSize; + } + else + { + assert(newLeftoverSize == chunkSize); + leftoverSize += chunkSize; + } + } + + return 0; + } + + /** + * ZSTD_ldm_skipSequences(): + * + * Skip past `srcSize` bytes worth of sequences in `rawSeqStore`. + * Avoids emitting matches less than `minMatch` bytes. + * Must be called for data that is not passed to ZSTD_ldm_blockCompress(). + */ + public static void ZSTD_ldm_skipSequences(rawSeqStore_t* rawSeqStore, nuint srcSize, uint minMatch) + { + while (srcSize > 0 && rawSeqStore->pos < rawSeqStore->size) + { + rawSeq* seq = rawSeqStore->seq + rawSeqStore->pos; + + if (srcSize <= seq->litLength) + { + seq->litLength -= (uint)(srcSize); + return; + } + + srcSize -= seq->litLength; + seq->litLength = 0; + if (srcSize < seq->matchLength) + { + seq->matchLength -= (uint)(srcSize); + if (seq->matchLength < minMatch) + { + if (rawSeqStore->pos + 1 < rawSeqStore->size) + { + seq[1].litLength += seq[0].matchLength; + } + + rawSeqStore->pos++; + } + + return; + } + + srcSize -= seq->matchLength; + seq->matchLength = 0; + rawSeqStore->pos++; + } + } + + /** + * If the sequence length is longer than remaining then the sequence is split + * between this block and the next. + * + * Returns the current sequence to handle, or if the rest of the block should + * be literals, it returns a sequence with offset == 0. + */ + private static rawSeq maybeSplitSequence(rawSeqStore_t* rawSeqStore, uint remaining, uint minMatch) + { + rawSeq sequence = rawSeqStore->seq[rawSeqStore->pos]; + + assert(sequence.offset > 0); + if (remaining >= sequence.litLength + sequence.matchLength) + { + rawSeqStore->pos++; + return sequence; + } + + if (remaining <= sequence.litLength) + { + sequence.offset = 0; + } + else if (remaining < sequence.litLength + sequence.matchLength) + { + sequence.matchLength = remaining - sequence.litLength; + if (sequence.matchLength < minMatch) + { + sequence.offset = 0; + } + } + + ZSTD_ldm_skipSequences(rawSeqStore, remaining, minMatch); + return sequence; + } + + /* ZSTD_ldm_skipRawSeqStoreBytes(): + * Moves forward in rawSeqStore by nbBytes, updating fields 'pos' and 'posInSequence'. + * Not to be used in conjunction with ZSTD_ldm_skipSequences(). + * Must be called for data with is not passed to ZSTD_ldm_blockCompress(). + */ + public static void ZSTD_ldm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, nuint nbBytes) + { + uint currPos = (uint)(rawSeqStore->posInSequence + nbBytes); + + while (currPos != 0 && rawSeqStore->pos < rawSeqStore->size) + { + rawSeq currSeq = rawSeqStore->seq[rawSeqStore->pos]; + + if (currPos >= currSeq.litLength + currSeq.matchLength) + { + currPos -= currSeq.litLength + currSeq.matchLength; + rawSeqStore->pos++; + } + else + { + rawSeqStore->posInSequence = currPos; + break; + } + } + + if (currPos == 0 || rawSeqStore->pos == rawSeqStore->size) + { + rawSeqStore->posInSequence = 0; + } + } + + /** + * ZSTD_ldm_blockCompress(): + * + * Compresses a block using the predefined sequences, along with a secondary + * block compressor. The literals section of every sequence is passed to the + * secondary block compressor, and those sequences are interspersed with the + * predefined sequences. Returns the length of the last literals. + * Updates `rawSeqStore.pos` to indicate how many sequences have been consumed. + * `rawSeqStore.seq` may also be updated to split the last sequence between two + * blocks. + * @return The length of the last literals. + * + * NOTE: The source must be at most the maximum block size, but the predefined + * sequences can be any size, and may be longer than the block. In the case that + * they are longer than the block, the last sequences may need to be split into + * two. We handle that case correctly, and update `rawSeqStore` appropriately. + * NOTE: This function does not return any errors. + */ + public static nuint ZSTD_ldm_blockCompress(rawSeqStore_t* rawSeqStore, ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, ZSTD_useRowMatchFinderMode_e useRowMatchFinder, void* src, nuint srcSize) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint minMatch = cParams->minMatch; + ZSTD_blockCompressor blockCompressor = ZSTD_selectBlockCompressor(cParams -> strategy, useRowMatchFinder, ZSTD_matchState_dictMode(ms)) ?? throw new InvalidOperationException(); + byte* istart = (byte*)(src); + byte* iend = istart + srcSize; + byte* ip = istart; + + if (cParams->strategy >= ZSTD_strategy.ZSTD_btopt) + { + nuint lastLLSize; + + ms->ldmSeqStore = rawSeqStore; + lastLLSize = blockCompressor(ms, seqStore, rep, src, srcSize); + ZSTD_ldm_skipRawSeqStoreBytes(rawSeqStore, srcSize); + return lastLLSize; + } + + assert(rawSeqStore->pos <= rawSeqStore->size); + assert(rawSeqStore->size <= rawSeqStore->capacity); + while (rawSeqStore->pos < rawSeqStore->size && ip < iend) + { + rawSeq sequence = maybeSplitSequence(rawSeqStore, (uint)(iend - ip), minMatch); + int i; + + if (sequence.offset == 0) + { + break; + } + + assert(ip + sequence.litLength + sequence.matchLength <= iend); + ZSTD_ldm_limitTableUpdate(ms, ip); + ZSTD_ldm_fillFastTables(ms, (void*)ip); + + { + nuint newLitLength = blockCompressor(ms, seqStore, rep, (void*)ip, sequence.litLength); + + ip += sequence.litLength; + for (i = 3 - 1; i > 0; i--) + { + rep[i] = rep[i - 1]; + } + + rep[0] = sequence.offset; + ZSTD_storeSeq(seqStore, newLitLength, ip - newLitLength, iend, sequence.offset + (uint)((3 - 1)), sequence.matchLength - 3); + ip += sequence.matchLength; + } + } + + ZSTD_ldm_limitTableUpdate(ms, ip); + ZSTD_ldm_fillFastTables(ms, (void*)ip); + return blockCompressor(ms, seqStore, rep, (void*)ip, (nuint)(iend - ip)); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLdmGeartab.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLdmGeartab.cs new file mode 100644 index 00000000..5fd66296 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdLdmGeartab.cs @@ -0,0 +1,268 @@ +using System; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + public static ulong* ZSTD_ldm_gearTab = GetArrayPointer(new ulong[256] + { + 0xf5b8f72c5f77775c, + 0x84935f266b7ac412, + 0xb647ada9ca730ccc, + 0xb065bb4b114fb1de, + 0x34584e7e8c3a9fd0, + 0x4e97e17c6ae26b05, + 0x3a03d743bc99a604, + 0xcecd042422c4044f, + 0x76de76c58524259e, + 0x9c8528f65badeaca, + 0x86563706e2097529, + 0x2902475fa375d889, + 0xafb32a9739a5ebe6, + 0xce2714da3883e639, + 0x21eaf821722e69e, + 0x37b628620b628, + 0x49a8d455d88caf5, + 0x8556d711e6958140, + 0x4f7ae74fc605c1f, + 0x829f0c3468bd3a20, + 0x4ffdc885c625179e, + 0x8473de048a3daf1b, + 0x51008822b05646b2, + 0x69d75d12b2d1cc5f, + 0x8c9d4a19159154bc, + 0xc3cc10f4abbd4003, + 0xd06ddc1cecb97391, + 0xbe48e6e7ed80302e, + 0x3481db31cee03547, + 0xacc3f67cdaa1d210, + 0x65cb771d8c7f96cc, + 0x8eb27177055723dd, + 0xc789950d44cd94be, + 0x934feadc3700b12b, + 0x5e485f11edbdf182, + 0x1e2e2a46fd64767a, + 0x2969ca71d82efa7c, + 0x9d46e9935ebbba2e, + 0xe056b67e05e6822b, + 0x94d73f55739d03a0, + 0xcd7010bdb69b5a03, + 0x455ef9fcd79b82f4, + 0x869cb54a8749c161, + 0x38d1a4fa6185d225, + 0xb475166f94bbe9bb, + 0xa4143548720959f1, + 0x7aed4780ba6b26ba, + 0xd0ce264439e02312, + 0x84366d746078d508, + 0xa8ce973c72ed17be, + 0x21c323a29a430b01, + 0x9962d617e3af80ee, + 0xab0ce91d9c8cf75b, + 0x530e8ee6d19a4dbc, + 0x2ef68c0cf53f5d72, + 0xc03a681640a85506, + 0x496e4e9f9c310967, + 0x78580472b59b14a0, + 0x273824c23b388577, + 0x66bf923ad45cb553, + 0x47ae1a5a2492ba86, + 0x35e304569e229659, + 0x4765182a46870b6f, + 0x6cbab625e9099412, + 0xddac9a2e598522c1, + 0x7172086e666624f2, + 0xdf5003ca503b7837, + 0x88c0c1db78563d09, + 0x58d51865acfc289d, + 0x177671aec65224f1, + 0xfb79d8a241e967d7, + 0x2be1e101cad9a49a, + 0x6625682f6e29186b, + 0x399553457ac06e50, + 0x35dffb4c23abb74, + 0x429db2591f54aade, + 0xc52802a8037d1009, + 0x6acb27381f0b25f3, + 0xf45e2551ee4f823b, + 0x8b0ea2d99580c2f7, + 0x3bed519cbcb4e1e1, + 0xff452823dbb010a, + 0x9d42ed614f3dd267, + 0x5b9313c06257c57b, + 0xa114b8008b5e1442, + 0xc1fe311c11c13d4b, + 0x66e8763ea34c5568, + 0x8b982af1c262f05d, + 0xee8876faaa75fbb7, + 0x8a62a4d0d172bb2a, + 0xc13d94a3b7449a97, + 0x6dbbba9dc15d037c, + 0xc786101f1d92e0f1, + 0xd78681a907a0b79b, + 0xf61aaf2962c9abb9, + 0x2cfd16fcd3cb7ad9, + 0x868c5b6744624d21, + 0x25e650899c74ddd7, + 0xba042af4a7c37463, + 0x4eb1a539465a3eca, + 0xbe09dbf03b05d5ca, + 0x774e5a362b5472ba, + 0x47a1221229d183cd, + 0x504b0ca18ef5a2df, + 0xdffbdfbde2456eb9, + 0x46cd2b2fbee34634, + 0xf2aef8fe819d98c3, + 0x357f5276d4599d61, + 0x24a5483879c453e3, + 0x88026889192b4b9, + 0x28da96671782dbec, + 0x4ef37c40588e9aaa, + 0x8837b90651bc9fb3, + 0xc164f741d3f0e5d6, + 0xbc135a0a704b70ba, + 0x69cd868f7622ada, + 0xbc37ba89e0b9c0ab, + 0x47c14a01323552f6, + 0x4f00794bacee98bb, + 0x7107de7d637a69d5, + 0x88af793bb6f2255e, + 0xf3c6466b8799b598, + 0xc288c616aa7f3b59, + 0x81ca63cf42fca3fd, + 0x88d85ace36a2674b, + 0xd056bd3792389e7, + 0xe55c396c4e9dd32d, + 0xbefb504571e6c0a6, + 0x96ab32115e91e8cc, + 0xbf8acb18de8f38d1, + 0x66dae58801672606, + 0x833b6017872317fb, + 0xb87c16f2d1c92864, + 0xdb766a74e58b669c, + 0x89659f85c61417be, + 0xc8daad856011ea0c, + 0x76a4b565b6fe7eae, + 0xa469d085f6237312, + 0xaaf0365683a3e96c, + 0x4dbb746f8424f7b8, + 0x638755af4e4acc1, + 0x3d7807f5bde64486, + 0x17be6d8f5bbb7639, + 0x903f0cd44dc35dc, + 0x67b672eafdf1196c, + 0xa676ff93ed4c82f1, + 0x521d1004c5053d9d, + 0x37ba9ad09ccc9202, + 0x84e54d297aacfb51, + 0xa0b4b776a143445, + 0x820d471e20b348e, + 0x1874383cb83d46dc, + 0x97edeec7a1efe11c, + 0xb330e50b1bdc42aa, + 0x1dd91955ce70e032, + 0xa514cdb88f2939d5, + 0x2791233fd90db9d3, + 0x7b670a4cc50f7a9b, + 0x77c07d2a05c6dfa5, + 0xe3778b6646d0a6fa, + 0xb39c8eda47b56749, + 0x933ed448addbef28, + 0xaf846af6ab7d0bf4, + 0xe5af208eb666e49, + 0x5e6622f73534cd6a, + 0x297daeca42ef5b6e, + 0x862daef3d35539a6, + 0xe68722498f8e1ea9, + 0x981c53093dc0d572, + 0xfa09b0bfbf86fbf5, + 0x30b1e96166219f15, + 0x70e7d466bdc4fb83, + 0x5a66736e35f2a8e9, + 0xcddb59d2b7c1baef, + 0xd6c7d247d26d8996, + 0xea4e39eac8de1ba3, + 0x539c8bb19fa3aff2, + 0x9f90e4c5fd508d8, + 0xa34e5956fbaf3385, + 0x2e2f8e151d3ef375, + 0x173691e9b83faec1, + 0xb85a8d56bf016379, + 0x8382381267408ae3, + 0xb90f901bbdc0096d, + 0x7c6ad32933bcec65, + 0x76bb5e2f2c8ad595, + 0x390f851a6cf46d28, + 0xc3e6064da1c2da72, + 0xc52a0c101cfa5389, + 0xd78eaf84a3fbc530, + 0x3781b9e2288b997e, + 0x73c2f6dea83d05c4, + 0x4228e364c5b5ed7, + 0x9d7a3edf0da43911, + 0x8edcfeda24686756, + 0x5e7667a7b7a9b3a1, + 0x4c4f389fa143791d, + 0xb08bc1023da7cddc, + 0x7ab4be3ae529b1cc, + 0x754e6132dbe74ff9, + 0x71635442a839df45, + 0x2f6fb1643fbe52de, + 0x961e0a42cf7a8177, + 0xf3b45d83d89ef2ea, + 0xee3de4cf4a6e3e9b, + 0xcd6848542c3295e7, + 0xe4cee1664c78662f, + 0x9947548b474c68c4, + 0x25d73777a5ed8b0b, + 0xc915b1d636b7fc, + 0x21c2ba75d9b0d2da, + 0x5f6b5dcf608a64a1, + 0xdcf333255ff9570c, + 0x633b922418ced4ee, + 0xc136dde0b004b34a, + 0x58cc83b05d4b2f5a, + 0x5eb424dda28e42d2, + 0x62df47369739cd98, + 0xb4e0b42485e4ce17, + 0x16e1f0c1f9a8d1e7, + 0x8ec3916707560ebf, + 0x62ba6e2df2cc9db3, + 0xcbf9f4ff77d83a16, + 0x78d9d7d07d2bbcc4, + 0xef554ce1e02c41f4, + 0x8d7581127eccf94d, + 0xa9b53336cb3c8a05, + 0x38c42c0bf45c4f91, + 0x640893cdf4488863, + 0x80ec34bc575ea568, + 0x39f324f5b48eaa40, + 0xe9d9ed1f8eff527f, + 0x9224fc058cc5a214, + 0xbaba00b04cfe7741, + 0x309a9f120fcf52af, + 0xa558f3ec65626212, + 0x424bec8b7adabe2f, + 0x41622513a6aea433, + 0xb88da2d5324ca798, + 0xd287733b245528a4, + 0x9a44697e6d68aec3, + 0x7b1093be2f49bb28, + 0x50bbec632e3d8aad, + 0x6cd90723e1ea8283, + 0x897b9e7431b02bf3, + 0x219efdcb338a7047, + 0x3b0311f0a27c0656, + 0xdb17bf91c0db96e7, + 0x8cd4fd6b4e85a5b2, + 0xfab071054ba6409d, + 0x40d6fe831fa9dfd9, + 0xaf358debad7d791e, + 0xeb8d0e25a65e3e58, + 0xbbcbd3df14e08580, + 0xcf751f27ecdab2b, + 0x2b4da14f2613d8f4, + }); + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdOpt.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdOpt.cs new file mode 100644 index 00000000..e4fb937f --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ZstdOpt.cs @@ -0,0 +1,1366 @@ +using System; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; + +namespace ZstdSharp.Unsafe +{ + public static unsafe partial class Methods + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_bitWeight(uint stat) + { + return (ZSTD_highbit32(stat + 1) * (uint)((1 << 8))); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_fracWeight(uint rawStat) + { + uint stat = rawStat + 1; + uint hb = ZSTD_highbit32(stat); + uint BWeight = hb * (uint)((1 << 8)); + uint FWeight = (stat << 8) >> (int)hb; + uint weight = BWeight + FWeight; + + assert(hb + 8 < 31); + return weight; + } + + private static int ZSTD_compressedLiterals(optState_t* optPtr) + { + return ((optPtr->literalCompressionMode != ZSTD_literalCompressionMode_e.ZSTD_lcm_uncompressed) ? 1 : 0); + } + + private static void ZSTD_setBasePrices(optState_t* optPtr, int optLevel) + { + if ((ZSTD_compressedLiterals(optPtr)) != 0) + { + optPtr->litSumBasePrice = (optLevel != 0 ? ZSTD_fracWeight(optPtr->litSum) : ZSTD_bitWeight(optPtr->litSum)); + } + + optPtr->litLengthSumBasePrice = (optLevel != 0 ? ZSTD_fracWeight(optPtr->litLengthSum) : ZSTD_bitWeight(optPtr->litLengthSum)); + optPtr->matchLengthSumBasePrice = (optLevel != 0 ? ZSTD_fracWeight(optPtr->matchLengthSum) : ZSTD_bitWeight(optPtr->matchLengthSum)); + optPtr->offCodeSumBasePrice = (optLevel != 0 ? ZSTD_fracWeight(optPtr->offCodeSum) : ZSTD_bitWeight(optPtr->offCodeSum)); + } + + /* ZSTD_downscaleStat() : + * reduce all elements in table by a factor 2^(ZSTD_FREQ_DIV+malus) + * return the resulting sum of elements */ + private static uint ZSTD_downscaleStat(uint* table, uint lastEltIndex, int malus) + { + uint s, sum = 0; + + assert(4 + malus > 0 && 4 + malus < 31); + for (s = 0; s < lastEltIndex + 1; s++) + { + table[s] = 1 + (table[s] >> (4 + malus)); + sum += table[s]; + } + + return sum; + } + + /* ZSTD_rescaleFreqs() : + * if first block (detected by optPtr->litLengthSum == 0) : init statistics + * take hints from dictionary if there is one + * or init from zero, using src for literals stats, or flat 1 for match symbols + * otherwise downscale existing stats, to be used as seed for next block. + */ + private static void ZSTD_rescaleFreqs(optState_t* optPtr, byte* src, nuint srcSize, int optLevel) + { + int compressedLiterals = ZSTD_compressedLiterals(optPtr); + + optPtr->priceType = ZSTD_OptPrice_e.zop_dynamic; + if (optPtr->litLengthSum == 0) + { + if (srcSize <= 1024) + { + optPtr->priceType = ZSTD_OptPrice_e.zop_predef; + } + + assert(optPtr->symbolCosts != null); + if (optPtr->symbolCosts->huf.repeatMode == HUF_repeat.HUF_repeat_valid) + { + optPtr->priceType = ZSTD_OptPrice_e.zop_dynamic; + if (compressedLiterals != 0) + { + uint lit; + + assert(optPtr->litFreq != null); + optPtr->litSum = 0; + for (lit = 0; lit <= (uint)(((1 << 8) - 1)); lit++) + { + uint scaleLog = 11; + uint bitCost = HUF_getNbBits((void*)optPtr->symbolCosts->huf.CTable, lit); + + assert(bitCost <= scaleLog); + optPtr->litFreq[lit] = (uint)(bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1); + optPtr->litSum += optPtr->litFreq[lit]; + } + } + + + { + uint ll; + FSE_CState_t llstate; + + FSE_initCState(&llstate, (uint*)optPtr->symbolCosts->fse.litlengthCTable); + optPtr->litLengthSum = 0; + for (ll = 0; ll <= 35; ll++) + { + uint scaleLog = 10; + uint bitCost = FSE_getMaxNbBits(llstate.symbolTT, ll); + + assert(bitCost < scaleLog); + optPtr->litLengthFreq[ll] = (uint)(bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1); + optPtr->litLengthSum += optPtr->litLengthFreq[ll]; + } + } + + + { + uint ml; + FSE_CState_t mlstate; + + FSE_initCState(&mlstate, (uint*)optPtr->symbolCosts->fse.matchlengthCTable); + optPtr->matchLengthSum = 0; + for (ml = 0; ml <= 52; ml++) + { + uint scaleLog = 10; + uint bitCost = FSE_getMaxNbBits(mlstate.symbolTT, ml); + + assert(bitCost < scaleLog); + optPtr->matchLengthFreq[ml] = (uint)(bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1); + optPtr->matchLengthSum += optPtr->matchLengthFreq[ml]; + } + } + + + { + uint of; + FSE_CState_t ofstate; + + FSE_initCState(&ofstate, (uint*)optPtr->symbolCosts->fse.offcodeCTable); + optPtr->offCodeSum = 0; + for (of = 0; of <= 31; of++) + { + uint scaleLog = 10; + uint bitCost = FSE_getMaxNbBits(ofstate.symbolTT, of); + + assert(bitCost < scaleLog); + optPtr->offCodeFreq[of] = (uint)(bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1); + optPtr->offCodeSum += optPtr->offCodeFreq[of]; + } + } + } + else + { + assert(optPtr->litFreq != null); + if (compressedLiterals != 0) + { + uint lit = (uint)(((1 << 8) - 1)); + + HIST_count_simple(optPtr->litFreq, &lit, (void*)src, srcSize); + optPtr->litSum = ZSTD_downscaleStat(optPtr->litFreq, (uint)(((1 << 8) - 1)), 1); + } + + + { + uint ll; + + for (ll = 0; ll <= 35; ll++) + { + optPtr->litLengthFreq[ll] = 1; + } + } + + optPtr->litLengthSum = (uint)(35 + 1); + + { + uint ml; + + for (ml = 0; ml <= 52; ml++) + { + optPtr->matchLengthFreq[ml] = 1; + } + } + + optPtr->matchLengthSum = (uint)(52 + 1); + + { + uint of; + + for (of = 0; of <= 31; of++) + { + optPtr->offCodeFreq[of] = 1; + } + } + + optPtr->offCodeSum = (uint)(31 + 1); + } + } + else + { + if (compressedLiterals != 0) + { + optPtr->litSum = ZSTD_downscaleStat(optPtr->litFreq, (uint)(((1 << 8) - 1)), 1); + } + + optPtr->litLengthSum = ZSTD_downscaleStat(optPtr->litLengthFreq, 35, 0); + optPtr->matchLengthSum = ZSTD_downscaleStat(optPtr->matchLengthFreq, 52, 0); + optPtr->offCodeSum = ZSTD_downscaleStat(optPtr->offCodeFreq, 31, 0); + } + + ZSTD_setBasePrices(optPtr, optLevel); + } + + /* ZSTD_rawLiteralsCost() : + * price of literals (only) in specified segment (which length can be 0). + * does not include price of literalLength symbol */ + private static uint ZSTD_rawLiteralsCost(byte* literals, uint litLength, optState_t* optPtr, int optLevel) + { + if (litLength == 0) + { + return 0; + } + + if ((ZSTD_compressedLiterals(optPtr)) == 0) + { + return (litLength << 3) * (uint)((1 << 8)); + } + + if (optPtr->priceType == ZSTD_OptPrice_e.zop_predef) + { + return (litLength * 6) * (uint)((1 << 8)); + } + + + { + uint price = litLength * optPtr->litSumBasePrice; + uint u; + + for (u = 0; u < litLength; u++) + { + assert((optLevel != 0 ? ZSTD_fracWeight(optPtr->litFreq[literals[u]]) : ZSTD_bitWeight(optPtr->litFreq[literals[u]])) <= optPtr->litSumBasePrice); + price -= (optLevel != 0 ? ZSTD_fracWeight(optPtr->litFreq[literals[u]]) : ZSTD_bitWeight(optPtr->litFreq[literals[u]])); + } + + return price; + } + } + + /* ZSTD_litLengthPrice() : + * cost of literalLength symbol */ + private static uint ZSTD_litLengthPrice(uint litLength, optState_t* optPtr, int optLevel) + { + if (optPtr->priceType == ZSTD_OptPrice_e.zop_predef) + { + return (optLevel != 0 ? ZSTD_fracWeight(litLength) : ZSTD_bitWeight(litLength)); + } + + + { + uint llCode = ZSTD_LLcode(litLength); + + return (LL_bits[llCode] * (uint)((1 << 8))) + optPtr->litLengthSumBasePrice - (optLevel != 0 ? ZSTD_fracWeight(optPtr->litLengthFreq[llCode]) : ZSTD_bitWeight(optPtr->litLengthFreq[llCode])); + } + } + + /* ZSTD_getMatchPrice() : + * Provides the cost of the match part (offset + matchLength) of a sequence + * Must be combined with ZSTD_fullLiteralsCost() to get the full cost of a sequence. + * optLevel: when <2, favors small offset for decompression speed (improved cache efficiency) */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_getMatchPrice(uint offset, uint matchLength, optState_t* optPtr, int optLevel) + { + uint price; + uint offCode = ZSTD_highbit32(offset + 1); + uint mlBase = matchLength - 3; + + assert(matchLength >= 3); + if (optPtr->priceType == ZSTD_OptPrice_e.zop_predef) + { + return (optLevel != 0 ? ZSTD_fracWeight(mlBase) : ZSTD_bitWeight(mlBase)) + ((16 + offCode) * (uint)((1 << 8))); + } + + price = (offCode * (uint)((1 << 8))) + (optPtr->offCodeSumBasePrice - (optLevel != 0 ? ZSTD_fracWeight(optPtr->offCodeFreq[offCode]) : ZSTD_bitWeight(optPtr->offCodeFreq[offCode]))); + if ((optLevel < 2) && offCode >= 20) + { + price += (offCode - 19) * 2 * (uint)((1 << 8)); + } + + + { + uint mlCode = ZSTD_MLcode(mlBase); + + price += (ML_bits[mlCode] * (uint)((1 << 8))) + (optPtr->matchLengthSumBasePrice - (optLevel != 0 ? ZSTD_fracWeight(optPtr->matchLengthFreq[mlCode]) : ZSTD_bitWeight(optPtr->matchLengthFreq[mlCode]))); + } + + price += (uint)((1 << 8) / 5); + return price; + } + + /* ZSTD_updateStats() : + * assumption : literals + litLengtn <= iend */ + private static void ZSTD_updateStats(optState_t* optPtr, uint litLength, byte* literals, uint offsetCode, uint matchLength) + { + if ((ZSTD_compressedLiterals(optPtr)) != 0) + { + uint u; + + for (u = 0; u < litLength; u++) + { + optPtr->litFreq[literals[u]] += 2; + } + + optPtr->litSum += litLength * 2; + } + + + { + uint llCode = ZSTD_LLcode(litLength); + + optPtr->litLengthFreq[llCode]++; + optPtr->litLengthSum++; + } + + + { + uint offCode = ZSTD_highbit32(offsetCode + 1); + + assert(offCode <= 31); + optPtr->offCodeFreq[offCode]++; + optPtr->offCodeSum++; + } + + + { + uint mlBase = matchLength - 3; + uint mlCode = ZSTD_MLcode(mlBase); + + optPtr->matchLengthFreq[mlCode]++; + optPtr->matchLengthSum++; + } + } + + /* ZSTD_readMINMATCH() : + * function safe only for comparisons + * assumption : memPtr must be at least 4 bytes before end of buffer */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_readMINMATCH(void* memPtr, uint length) + { + switch (length) + { + default: + case 4: + { + return MEM_read32(memPtr); + } + + case 3: + { + if (BitConverter.IsLittleEndian) + { + return MEM_read32(memPtr) << 8; + } + else + { + return MEM_read32(memPtr) >> 8; + } + } + } + } + + /* Update hashTable3 up to ip (excluded) + Assumption : always within prefix (i.e. not within extDict) */ + private static uint ZSTD_insertAndFindFirstIndexHash3(ZSTD_matchState_t* ms, uint* nextToUpdate3, byte* ip) + { + uint* hashTable3 = ms->hashTable3; + uint hashLog3 = ms->hashLog3; + byte* @base = ms->window.@base; + uint idx = *nextToUpdate3; + uint target = (uint)(ip - @base); + nuint hash3 = ZSTD_hash3Ptr((void*)ip, hashLog3); + + assert(hashLog3 > 0); + while (idx < target) + { + hashTable3[ZSTD_hash3Ptr((void*)(@base + idx), hashLog3)] = idx; + idx++; + } + + *nextToUpdate3 = target; + return hashTable3[hash3]; + } + + /*-************************************* + * Binary Tree search + ***************************************/ + /** ZSTD_insertBt1() : add one or multiple positions to tree. + * ip : assumed <= iend-8 . + * @return : nb of positions added */ + private static uint ZSTD_insertBt1(ZSTD_matchState_t* ms, byte* ip, byte* iend, uint mls, int extDict) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint* hashTable = ms->hashTable; + uint hashLog = cParams->hashLog; + nuint h = ZSTD_hashPtr((void*)ip, hashLog, mls); + uint* bt = ms->chainTable; + uint btLog = cParams->chainLog - 1; + uint btMask = (uint)((1 << (int)btLog) - 1); + uint matchIndex = hashTable[h]; + nuint commonLengthSmaller = 0, commonLengthLarger = 0; + byte* @base = ms->window.@base; + byte* dictBase = ms->window.dictBase; + uint dictLimit = ms->window.dictLimit; + byte* dictEnd = dictBase + dictLimit; + byte* prefixStart = @base + dictLimit; + byte* match; + uint curr = (uint)(ip - @base); + uint btLow = (uint)(btMask >= curr ? 0 : curr - btMask); + uint* smallerPtr = bt + 2 * (curr & btMask); + uint* largerPtr = smallerPtr + 1; + uint dummy32; + uint windowLow = ms->window.lowLimit; + uint matchEndIdx = curr + 8 + 1; + nuint bestLength = 8; + uint nbCompares = 1U << (int)cParams->searchLog; + + assert(ip <= iend - 8); + hashTable[h] = curr; + assert(windowLow > 0); + while (nbCompares-- != 0 && (matchIndex >= windowLow)) + { + uint* nextPtr = bt + 2 * (matchIndex & btMask); + nuint matchLength = ((commonLengthSmaller) < (commonLengthLarger) ? (commonLengthSmaller) : (commonLengthLarger)); + + assert(matchIndex < curr); + if (extDict == 0 || (matchIndex + matchLength >= dictLimit)) + { + assert(matchIndex + matchLength >= dictLimit); + match = @base + matchIndex; + matchLength += ZSTD_count(ip + matchLength, match + matchLength, iend); + } + else + { + match = dictBase + matchIndex; + matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart); + if (matchIndex + matchLength >= dictLimit) + { + match = @base + matchIndex; + } + } + + if (matchLength > bestLength) + { + bestLength = matchLength; + if (matchLength > matchEndIdx - matchIndex) + { + matchEndIdx = matchIndex + (uint)(matchLength); + } + } + + if (ip + matchLength == iend) + { + break; + } + + if (match[matchLength] < ip[matchLength]) + { + *smallerPtr = matchIndex; + commonLengthSmaller = matchLength; + if (matchIndex <= btLow) + { + smallerPtr = &dummy32; + break; + } + + smallerPtr = nextPtr + 1; + matchIndex = nextPtr[1]; + } + else + { + *largerPtr = matchIndex; + commonLengthLarger = matchLength; + if (matchIndex <= btLow) + { + largerPtr = &dummy32; + break; + } + + largerPtr = nextPtr; + matchIndex = nextPtr[0]; + } + } + + *smallerPtr = *largerPtr = 0; + + { + uint positions = 0; + + if (bestLength > 384) + { + positions = (uint)((192) < ((uint)(bestLength - 384)) ? (192) : ((uint)(bestLength - 384))); + } + + assert(matchEndIdx > curr + 8); + return ((positions) > (matchEndIdx - (curr + 8)) ? (positions) : (matchEndIdx - (curr + 8))); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_updateTree_internal(ZSTD_matchState_t* ms, byte* ip, byte* iend, uint mls, ZSTD_dictMode_e dictMode) + { + byte* @base = ms->window.@base; + uint target = (uint)(ip - @base); + uint idx = ms->nextToUpdate; + + while (idx < target) + { + uint forward = ZSTD_insertBt1(ms, @base + idx, iend, mls, ((dictMode == ZSTD_dictMode_e.ZSTD_extDict) ? 1 : 0)); + + assert(idx < (uint)(idx + forward)); + idx += forward; + } + + assert((nuint)(ip - @base) <= (nuint)(unchecked((uint)(-1)))); + assert((nuint)(iend - @base) <= (nuint)(unchecked((uint)(-1)))); + ms->nextToUpdate = target; + } + + /* used in ZSTD_loadDictionaryContent() */ + public static void ZSTD_updateTree(ZSTD_matchState_t* ms, byte* ip, byte* iend) + { + ZSTD_updateTree_internal(ms, ip, iend, ms->cParams.minMatch, ZSTD_dictMode_e.ZSTD_noDict); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_insertBtAndGetAllMatches(ZSTD_match_t* matches, ZSTD_matchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iLimit, ZSTD_dictMode_e dictMode, uint* rep, uint ll0, uint lengthToBeat, uint mls) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint sufficient_len = ((cParams->targetLength) < (uint)(((1 << 12) - 1)) ? (cParams->targetLength) : ((1 << 12) - 1)); + byte* @base = ms->window.@base; + uint curr = (uint)(ip - @base); + uint hashLog = cParams->hashLog; + uint minMatch = (uint)((mls == 3) ? 3 : 4); + uint* hashTable = ms->hashTable; + nuint h = ZSTD_hashPtr((void*)ip, hashLog, mls); + uint matchIndex = hashTable[h]; + uint* bt = ms->chainTable; + uint btLog = cParams->chainLog - 1; + uint btMask = (1U << (int)btLog) - 1; + nuint commonLengthSmaller = 0, commonLengthLarger = 0; + byte* dictBase = ms->window.dictBase; + uint dictLimit = ms->window.dictLimit; + byte* dictEnd = dictBase + dictLimit; + byte* prefixStart = @base + dictLimit; + uint btLow = (uint)((btMask >= curr) ? 0 : curr - btMask); + uint windowLow = ZSTD_getLowestMatchIndex(ms, curr, cParams->windowLog); + uint matchLow = windowLow != 0 ? windowLow : 1; + uint* smallerPtr = bt + 2 * (curr & btMask); + uint* largerPtr = bt + 2 * (curr & btMask) + 1; + uint matchEndIdx = curr + 8 + 1; + uint dummy32; + uint mnum = 0; + uint nbCompares = 1U << (int)cParams->searchLog; + ZSTD_matchState_t* dms = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? ms->dictMatchState : null; + ZSTD_compressionParameters* dmsCParams = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? &dms->cParams : null; + byte* dmsBase = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dms->window.@base : null; + byte* dmsEnd = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dms->window.nextSrc : null; + uint dmsHighLimit = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? (uint)(dmsEnd - dmsBase) : 0; + uint dmsLowLimit = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dms->window.lowLimit : 0; + uint dmsIndexDelta = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? windowLow - dmsHighLimit : 0; + uint dmsHashLog = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dmsCParams->hashLog : hashLog; + uint dmsBtLog = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dmsCParams->chainLog - 1 : btLog; + uint dmsBtMask = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? (1U << (int)dmsBtLog) - 1 : 0; + uint dmsBtLow = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && dmsBtMask < dmsHighLimit - dmsLowLimit ? dmsHighLimit - dmsBtMask : dmsLowLimit; + nuint bestLength = lengthToBeat - 1; + + assert(ll0 <= 1); + + { + uint lastR = 3 + ll0; + uint repCode; + + for (repCode = ll0; repCode < lastR; repCode++) + { + uint repOffset = (repCode == 3) ? (rep[0] - 1) : rep[repCode]; + uint repIndex = curr - repOffset; + uint repLen = 0; + + assert(curr >= dictLimit); + if (repOffset - 1 < curr - dictLimit) + { + if (((repIndex >= windowLow) && (ZSTD_readMINMATCH((void*)ip, minMatch) == ZSTD_readMINMATCH((void*)(ip - repOffset), minMatch)))) + { + repLen = (uint)(ZSTD_count(ip + minMatch, ip + minMatch - repOffset, iLimit)) + minMatch; + } + } + else + { + byte* repMatch = dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState ? dmsBase + repIndex - dmsIndexDelta : dictBase + repIndex; + + assert(curr >= windowLow); + if (dictMode == ZSTD_dictMode_e.ZSTD_extDict && ((((repOffset - 1) < curr - windowLow) && (((uint)((dictLimit - 1) - repIndex) >= 3)))) && (ZSTD_readMINMATCH((void*)ip, minMatch) == ZSTD_readMINMATCH((void*)repMatch, minMatch))) + { + repLen = (uint)(ZSTD_count_2segments(ip + minMatch, repMatch + minMatch, iLimit, dictEnd, prefixStart)) + minMatch; + } + + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && ((((repOffset - 1) < curr - (dmsLowLimit + dmsIndexDelta)) && ((uint)((dictLimit - 1) - repIndex) >= 3))) && (ZSTD_readMINMATCH((void*)ip, minMatch) == ZSTD_readMINMATCH((void*)repMatch, minMatch))) + { + repLen = (uint)(ZSTD_count_2segments(ip + minMatch, repMatch + minMatch, iLimit, dmsEnd, prefixStart)) + minMatch; + } + } + + if (repLen > bestLength) + { + bestLength = repLen; + matches[mnum].off = repCode - ll0; + matches[mnum].len = (uint)(repLen); + mnum++; + if (((repLen > sufficient_len) || (ip + repLen == iLimit))) + { + return mnum; + } + } + } + } + + if ((mls == 3) && (bestLength < mls)) + { + uint matchIndex3 = ZSTD_insertAndFindFirstIndexHash3(ms, nextToUpdate3, ip); + + if (((matchIndex3 >= matchLow) && (curr - matchIndex3 < (uint)((1 << 18))))) + { + nuint mlen; + + if ((dictMode == ZSTD_dictMode_e.ZSTD_noDict) || (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) || (matchIndex3 >= dictLimit)) + { + byte* match = @base + matchIndex3; + + mlen = ZSTD_count(ip, match, iLimit); + } + else + { + byte* match = dictBase + matchIndex3; + + mlen = ZSTD_count_2segments(ip, match, iLimit, dictEnd, prefixStart); + } + + if (mlen >= mls) + { + bestLength = mlen; + assert(curr > matchIndex3); + assert(mnum == 0); + matches[0].off = (curr - matchIndex3) + (uint)((3 - 1)); + matches[0].len = (uint)(mlen); + mnum = 1; + if (((mlen > sufficient_len) || (ip + mlen == iLimit))) + { + ms->nextToUpdate = curr + 1; + return 1; + } + } + } + } + + hashTable[h] = curr; + while (nbCompares-- != 0 && (matchIndex >= matchLow)) + { + uint* nextPtr = bt + 2 * (matchIndex & btMask); + byte* match; + nuint matchLength = ((commonLengthSmaller) < (commonLengthLarger) ? (commonLengthSmaller) : (commonLengthLarger)); + + assert(curr > matchIndex); + if ((dictMode == ZSTD_dictMode_e.ZSTD_noDict) || (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) || (matchIndex + matchLength >= dictLimit)) + { + assert(matchIndex + matchLength >= dictLimit); + match = @base + matchIndex; + if (matchIndex >= dictLimit) + { + assert(memcmp((void*)match, (void*)ip, matchLength) == 0); + } + + matchLength += ZSTD_count(ip + matchLength, match + matchLength, iLimit); + } + else + { + match = dictBase + matchIndex; + assert(memcmp((void*)match, (void*)ip, matchLength) == 0); + matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iLimit, dictEnd, prefixStart); + if (matchIndex + matchLength >= dictLimit) + { + match = @base + matchIndex; + } + } + + if (matchLength > bestLength) + { + assert(matchEndIdx > matchIndex); + if (matchLength > matchEndIdx - matchIndex) + { + matchEndIdx = matchIndex + (uint)(matchLength); + } + + bestLength = matchLength; + matches[mnum].off = (curr - matchIndex) + (uint)((3 - 1)); + matches[mnum].len = (uint)(matchLength); + mnum++; + if (((matchLength > (uint)((1 << 12))) || (ip + matchLength == iLimit))) + { + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) + { + nbCompares = 0; + } + + break; + } + } + + if (match[matchLength] < ip[matchLength]) + { + *smallerPtr = matchIndex; + commonLengthSmaller = matchLength; + if (matchIndex <= btLow) + { + smallerPtr = &dummy32; + break; + } + + smallerPtr = nextPtr + 1; + matchIndex = nextPtr[1]; + } + else + { + *largerPtr = matchIndex; + commonLengthLarger = matchLength; + if (matchIndex <= btLow) + { + largerPtr = &dummy32; + break; + } + + largerPtr = nextPtr; + matchIndex = nextPtr[0]; + } + } + + *smallerPtr = *largerPtr = 0; + if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && nbCompares != 0) + { + nuint dmsH = ZSTD_hashPtr((void*)ip, dmsHashLog, mls); + uint dictMatchIndex = dms->hashTable[dmsH]; + uint* dmsBt = dms->chainTable; + + commonLengthSmaller = commonLengthLarger = 0; + while (nbCompares-- != 0 && (dictMatchIndex > dmsLowLimit)) + { + uint* nextPtr = dmsBt + 2 * (dictMatchIndex & dmsBtMask); + nuint matchLength = ((commonLengthSmaller) < (commonLengthLarger) ? (commonLengthSmaller) : (commonLengthLarger)); + byte* match = dmsBase + dictMatchIndex; + + matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iLimit, dmsEnd, prefixStart); + if (dictMatchIndex + matchLength >= dmsHighLimit) + { + match = @base + dictMatchIndex + dmsIndexDelta; + } + + if (matchLength > bestLength) + { + matchIndex = dictMatchIndex + dmsIndexDelta; + if (matchLength > matchEndIdx - matchIndex) + { + matchEndIdx = matchIndex + (uint)(matchLength); + } + + bestLength = matchLength; + matches[mnum].off = (curr - matchIndex) + (uint)((3 - 1)); + matches[mnum].len = (uint)(matchLength); + mnum++; + if (((matchLength > (uint)((1 << 12))) || (ip + matchLength == iLimit))) + { + break; + } + } + + if (dictMatchIndex <= dmsBtLow) + { + break; + } + + if (match[matchLength] < ip[matchLength]) + { + commonLengthSmaller = matchLength; + dictMatchIndex = nextPtr[1]; + } + else + { + commonLengthLarger = matchLength; + dictMatchIndex = nextPtr[0]; + } + } + } + + assert(matchEndIdx > curr + 8); + ms->nextToUpdate = matchEndIdx - 8; + return mnum; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint ZSTD_BtGetAllMatches(ZSTD_match_t* matches, ZSTD_matchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, ZSTD_dictMode_e dictMode, uint* rep, uint ll0, uint lengthToBeat) + { + ZSTD_compressionParameters* cParams = &ms->cParams; + uint matchLengthSearch = cParams->minMatch; + + if (ip < ms->window.@base + ms->nextToUpdate) + { + return 0; + } + + ZSTD_updateTree_internal(ms, ip, iHighLimit, matchLengthSearch, dictMode); + switch (matchLengthSearch) + { + case 3: + { + return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 3); + } + + default: + case 4: + { + return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 4); + } + + case 5: + { + return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 5); + } + + case 7: + case 6: + { + return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, 6); + } + } + } + + /* ZSTD_optLdm_skipRawSeqStoreBytes(): + * Moves forward in rawSeqStore by nbBytes, which will update the fields 'pos' and 'posInSequence'. + */ + private static void ZSTD_optLdm_skipRawSeqStoreBytes(rawSeqStore_t* rawSeqStore, nuint nbBytes) + { + uint currPos = (uint)(rawSeqStore->posInSequence + nbBytes); + + while (currPos != 0 && rawSeqStore->pos < rawSeqStore->size) + { + rawSeq currSeq = rawSeqStore->seq[rawSeqStore->pos]; + + if (currPos >= currSeq.litLength + currSeq.matchLength) + { + currPos -= currSeq.litLength + currSeq.matchLength; + rawSeqStore->pos++; + } + else + { + rawSeqStore->posInSequence = currPos; + break; + } + } + + if (currPos == 0 || rawSeqStore->pos == rawSeqStore->size) + { + rawSeqStore->posInSequence = 0; + } + } + + /* ZSTD_opt_getNextMatchAndUpdateSeqStore(): + * Calculates the beginning and end of the next match in the current block. + * Updates 'pos' and 'posInSequence' of the ldmSeqStore. + */ + private static void ZSTD_opt_getNextMatchAndUpdateSeqStore(ZSTD_optLdm_t* optLdm, uint currPosInBlock, uint blockBytesRemaining) + { + rawSeq currSeq; + uint currBlockEndPos; + uint literalsBytesRemaining; + uint matchBytesRemaining; + + if (optLdm->seqStore.size == 0 || optLdm->seqStore.pos >= optLdm->seqStore.size) + { + optLdm->startPosInBlock = 0xffffffff; + optLdm->endPosInBlock = 0xffffffff; + return; + } + + currSeq = optLdm->seqStore.seq[optLdm->seqStore.pos]; + assert(optLdm->seqStore.posInSequence <= currSeq.litLength + currSeq.matchLength); + currBlockEndPos = currPosInBlock + blockBytesRemaining; + literalsBytesRemaining = (optLdm->seqStore.posInSequence < currSeq.litLength) ? currSeq.litLength - (uint)(optLdm->seqStore.posInSequence) : 0; + matchBytesRemaining = (literalsBytesRemaining == 0) ? currSeq.matchLength - ((uint)(optLdm->seqStore.posInSequence) - currSeq.litLength) : currSeq.matchLength; + if (literalsBytesRemaining >= blockBytesRemaining) + { + optLdm->startPosInBlock = 0xffffffff; + optLdm->endPosInBlock = 0xffffffff; + ZSTD_optLdm_skipRawSeqStoreBytes(&optLdm->seqStore, blockBytesRemaining); + return; + } + + optLdm->startPosInBlock = currPosInBlock + literalsBytesRemaining; + optLdm->endPosInBlock = optLdm->startPosInBlock + matchBytesRemaining; + optLdm->offset = currSeq.offset; + if (optLdm->endPosInBlock > currBlockEndPos) + { + optLdm->endPosInBlock = currBlockEndPos; + ZSTD_optLdm_skipRawSeqStoreBytes(&optLdm->seqStore, currBlockEndPos - currPosInBlock); + } + else + { + ZSTD_optLdm_skipRawSeqStoreBytes(&optLdm->seqStore, literalsBytesRemaining + matchBytesRemaining); + } + } + + /* ZSTD_optLdm_maybeAddMatch(): + * Adds a match if it's long enough, based on it's 'matchStartPosInBlock' + * and 'matchEndPosInBlock', into 'matches'. Maintains the correct ordering of 'matches' + */ + private static void ZSTD_optLdm_maybeAddMatch(ZSTD_match_t* matches, uint* nbMatches, ZSTD_optLdm_t* optLdm, uint currPosInBlock) + { + uint posDiff = currPosInBlock - optLdm->startPosInBlock; + uint candidateMatchLength = optLdm->endPosInBlock - optLdm->startPosInBlock - posDiff; + uint candidateOffCode = optLdm->offset + (uint)((3 - 1)); + + if (currPosInBlock < optLdm->startPosInBlock || currPosInBlock >= optLdm->endPosInBlock || candidateMatchLength < 3) + { + return; + } + + if (*nbMatches == 0 || ((candidateMatchLength > matches[*nbMatches - 1].len) && *nbMatches < (uint)((1 << 12)))) + { + matches[*nbMatches].len = candidateMatchLength; + matches[*nbMatches].off = candidateOffCode; + (*nbMatches)++; + } + } + + /* ZSTD_optLdm_processMatchCandidate(): + * Wrapper function to update ldm seq store and call ldm functions as necessary. + */ + private static void ZSTD_optLdm_processMatchCandidate(ZSTD_optLdm_t* optLdm, ZSTD_match_t* matches, uint* nbMatches, uint currPosInBlock, uint remainingBytes) + { + if (optLdm->seqStore.size == 0 || optLdm->seqStore.pos >= optLdm->seqStore.size) + { + return; + } + + if (currPosInBlock >= optLdm->endPosInBlock) + { + if (currPosInBlock > optLdm->endPosInBlock) + { + uint posOvershoot = currPosInBlock - optLdm->endPosInBlock; + + ZSTD_optLdm_skipRawSeqStoreBytes(&optLdm->seqStore, posOvershoot); + } + + ZSTD_opt_getNextMatchAndUpdateSeqStore(optLdm, currPosInBlock, remainingBytes); + } + + ZSTD_optLdm_maybeAddMatch(matches, nbMatches, optLdm, currPosInBlock); + } + + /*-******************************* + * Optimal parser + *********************************/ + private static uint ZSTD_totalLen(ZSTD_optimal_t sol) + { + return sol.litlen + sol.mlen; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static nuint ZSTD_compressBlock_opt_generic(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize, int optLevel, ZSTD_dictMode_e dictMode) + { + optState_t* optStatePtr = &ms->opt; + byte* istart = (byte*)(src); + byte* ip = istart; + byte* anchor = istart; + byte* iend = istart + srcSize; + byte* ilimit = iend - 8; + byte* @base = ms->window.@base; + byte* prefixStart = @base + ms->window.dictLimit; + ZSTD_compressionParameters* cParams = &ms->cParams; + uint sufficient_len = ((cParams->targetLength) < (uint)(((1 << 12) - 1)) ? (cParams->targetLength) : ((1 << 12) - 1)); + uint minMatch = (uint)((cParams->minMatch == 3) ? 3 : 4); + uint nextToUpdate3 = ms->nextToUpdate; + ZSTD_optimal_t* opt = optStatePtr->priceTable; + ZSTD_match_t* matches = optStatePtr->matchTable; + ZSTD_optimal_t lastSequence; + var _ = &lastSequence; + ZSTD_optLdm_t optLdm; + + optLdm.seqStore = ms->ldmSeqStore != null ? *ms->ldmSeqStore : kNullRawSeqStore; + optLdm.endPosInBlock = optLdm.startPosInBlock = optLdm.offset = 0; + ZSTD_opt_getNextMatchAndUpdateSeqStore(&optLdm, (uint)(ip - istart), (uint)(iend - ip)); + assert(optLevel <= 2); + ZSTD_rescaleFreqs(optStatePtr, (byte*)(src), srcSize, optLevel); + ip += ((ip == prefixStart) ? 1 : 0); + while (ip < ilimit) + { + uint cur, last_pos = 0; + + + { + uint litlen = (uint)(ip - anchor); + uint ll0 = (litlen == 0 ? 1U : 0U); + uint nbMatches = ZSTD_BtGetAllMatches(matches, ms, &nextToUpdate3, ip, iend, dictMode, rep, ll0, minMatch); + + ZSTD_optLdm_processMatchCandidate(&optLdm, matches, &nbMatches, (uint)(ip - istart), (uint)(iend - ip)); + if (nbMatches == 0) + { + ip++; + continue; + } + + + { + uint i; + + for (i = 0; i < 3; i++) + { + opt[0].rep[i] = rep[i]; + } + } + + opt[0].mlen = 0; + opt[0].litlen = litlen; + opt[0].price = (int)(ZSTD_litLengthPrice(litlen, optStatePtr, optLevel)); + + { + uint maxML = matches[nbMatches - 1].len; + uint maxOffset = matches[nbMatches - 1].off; + + if (maxML > sufficient_len) + { + lastSequence.litlen = litlen; + lastSequence.mlen = maxML; + lastSequence.off = maxOffset; + cur = 0; + last_pos = ZSTD_totalLen(lastSequence); + goto _shortestPath; + } + } + + + { + uint literalsPrice = (uint)(opt[0].price) + ZSTD_litLengthPrice(0, optStatePtr, optLevel); + uint pos; + uint matchNb; + + for (pos = 1; pos < minMatch; pos++) + { + opt[pos].price = (1 << 30); + } + + for (matchNb = 0; matchNb < nbMatches; matchNb++) + { + uint offset = matches[matchNb].off; + uint end = matches[matchNb].len; + + for (; pos <= end; pos++) + { + uint matchPrice = ZSTD_getMatchPrice(offset, pos, optStatePtr, optLevel); + uint sequencePrice = literalsPrice + matchPrice; + + opt[pos].mlen = pos; + opt[pos].off = offset; + opt[pos].litlen = litlen; + opt[pos].price = (int)sequencePrice; + } + } + + last_pos = pos - 1; + } + } + + for (cur = 1; cur <= last_pos; cur++) + { + byte* inr = ip + cur; + + assert(cur < (uint)((1 << 12))); + + { + uint litlen = (opt[cur - 1].mlen == 0) ? opt[cur - 1].litlen + 1 : 1; + int price = (int)((uint)(opt[cur - 1].price) + ZSTD_rawLiteralsCost(ip + cur - 1, 1, optStatePtr, optLevel) + ZSTD_litLengthPrice(litlen, optStatePtr, optLevel) - ZSTD_litLengthPrice(litlen - 1, optStatePtr, optLevel)); + + assert(price < 1000000000); + if (price <= opt[cur].price) + { + opt[cur].mlen = 0; + opt[cur].off = 0; + opt[cur].litlen = litlen; + opt[cur].price = price; + } + else + { + } + } + + assert(cur >= opt[cur].mlen); + if (opt[cur].mlen != 0) + { + uint prev = cur - opt[cur].mlen; + repcodes_s newReps = ZSTD_updateRep(opt[prev].rep, opt[cur].off, ((opt[cur].litlen == 0) ? 1U : 0U)); + + memcpy((void*)((opt[cur].rep)), (void*)(&newReps), ((nuint)(sizeof(repcodes_s)))); + } + else + { + memcpy((void*)((opt[cur].rep)), (void*)((opt[cur - 1].rep)), ((nuint)(sizeof(repcodes_s)))); + } + + if (inr > ilimit) + { + continue; + } + + if (cur == last_pos) + { + break; + } + + if ((optLevel == 0) && (opt[cur + 1].price <= opt[cur].price + ((1 << 8) / 2))) + { + continue; + } + + + { + uint ll0 = (((opt[cur].mlen != 0)) ? 1U : 0U); + uint litlen = (opt[cur].mlen == 0) ? opt[cur].litlen : 0; + uint previousPrice = (uint)(opt[cur].price); + uint basePrice = previousPrice + ZSTD_litLengthPrice(0, optStatePtr, optLevel); + uint nbMatches = ZSTD_BtGetAllMatches(matches, ms, &nextToUpdate3, inr, iend, dictMode, opt[cur].rep, ll0, minMatch); + uint matchNb; + + ZSTD_optLdm_processMatchCandidate(&optLdm, matches, &nbMatches, (uint)(inr - istart), (uint)(iend - inr)); + if (nbMatches == 0) + { + continue; + } + + + { + uint maxML = matches[nbMatches - 1].len; + + if ((maxML > sufficient_len) || (cur + maxML >= (uint)((1 << 12)))) + { + lastSequence.mlen = maxML; + lastSequence.off = matches[nbMatches - 1].off; + lastSequence.litlen = litlen; + cur -= (opt[cur].mlen == 0) ? opt[cur].litlen : 0; + last_pos = cur + ZSTD_totalLen(lastSequence); + if (cur > (uint)((1 << 12))) + { + cur = 0; + } + + goto _shortestPath; + } + } + + for (matchNb = 0; matchNb < nbMatches; matchNb++) + { + uint offset = matches[matchNb].off; + uint lastML = matches[matchNb].len; + uint startML = (matchNb > 0) ? matches[matchNb - 1].len + 1 : minMatch; + uint mlen; + + for (mlen = lastML; mlen >= startML; mlen--) + { + uint pos = cur + mlen; + int price = (int)(basePrice + ZSTD_getMatchPrice(offset, mlen, optStatePtr, optLevel)); + + if ((pos > last_pos) || (price < opt[pos].price)) + { + while (last_pos < pos) + { + opt[last_pos + 1].price = (1 << 30); + last_pos++; + } + + opt[pos].mlen = mlen; + opt[pos].off = offset; + opt[pos].litlen = litlen; + opt[pos].price = price; + } + else + { + if (optLevel == 0) + { + break; + } + } + } + } + } + } + + lastSequence = opt[last_pos]; + cur = last_pos > ZSTD_totalLen(lastSequence) ? last_pos - ZSTD_totalLen(lastSequence) : 0; + assert(cur < (uint)((1 << 12))); + _shortestPath: + assert(opt[0].mlen == 0); + if (lastSequence.mlen != 0) + { + repcodes_s reps = ZSTD_updateRep(opt[cur].rep, lastSequence.off, ((lastSequence.litlen == 0) ? 1U : 0U)); + + memcpy((void*)(rep), (void*)(&reps), ((nuint)(sizeof(repcodes_s)))); + } + else + { + memcpy((void*)(rep), (void*)((opt[cur].rep)), ((nuint)(sizeof(repcodes_s)))); + } + + + { + uint storeEnd = cur + 1; + uint storeStart = storeEnd; + uint seqPos = cur; + + assert(storeEnd < (uint)((1 << 12))); + opt[storeEnd] = lastSequence; + while (seqPos > 0) + { + uint backDist = ZSTD_totalLen(opt[seqPos]); + + storeStart--; + opt[storeStart] = opt[seqPos]; + seqPos = (seqPos > backDist) ? seqPos - backDist : 0; + } + + + { + uint storePos; + + for (storePos = storeStart; storePos <= storeEnd; storePos++) + { + uint llen = opt[storePos].litlen; + uint mlen = opt[storePos].mlen; + uint offCode = opt[storePos].off; + uint advance = llen + mlen; + + if (mlen == 0) + { + assert(storePos == storeEnd); + ip = anchor + llen; + continue; + } + + assert(anchor + llen <= iend); + ZSTD_updateStats(optStatePtr, llen, anchor, offCode, mlen); + ZSTD_storeSeq(seqStore, llen, anchor, iend, offCode, mlen - 3); + anchor += advance; + ip = anchor; + } + } + + ZSTD_setBasePrices(optStatePtr, optLevel); + } + } + + return (nuint)(iend - anchor); + } + + public static nuint ZSTD_compressBlock_btopt(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0, ZSTD_dictMode_e.ZSTD_noDict); + } + + /* used in 2-pass strategy */ + private static uint ZSTD_upscaleStat(uint* table, uint lastEltIndex, int bonus) + { + uint s, sum = 0; + + assert(4 + bonus >= 0); + for (s = 0; s < lastEltIndex + 1; s++) + { + table[s] <<= 4 + bonus; + table[s]--; + sum += table[s]; + } + + return sum; + } + + /* used in 2-pass strategy */ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ZSTD_upscaleStats(optState_t* optPtr) + { + if ((ZSTD_compressedLiterals(optPtr)) != 0) + { + optPtr->litSum = ZSTD_upscaleStat(optPtr->litFreq, (uint)(((1 << 8) - 1)), 0); + } + + optPtr->litLengthSum = ZSTD_upscaleStat(optPtr->litLengthFreq, 35, 0); + optPtr->matchLengthSum = ZSTD_upscaleStat(optPtr->matchLengthFreq, 52, 0); + optPtr->offCodeSum = ZSTD_upscaleStat(optPtr->offCodeFreq, 31, 0); + } + + /* ZSTD_initStats_ultra(): + * make a first compression pass, just to seed stats with more accurate starting values. + * only works on first block, with no dictionary and no ldm. + * this function cannot error, hence its contract must be respected. + */ + private static void ZSTD_initStats_ultra(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + uint* tmpRep = stackalloc uint[3]; + + memcpy((void*)(tmpRep), (void*)(rep), ((nuint)(sizeof(uint) * 3))); + assert(ms->opt.litLengthSum == 0); + assert(seqStore->sequences == seqStore->sequencesStart); + assert(ms->window.dictLimit == ms->window.lowLimit); + assert(ms->window.dictLimit - ms->nextToUpdate <= 1); + ZSTD_compressBlock_opt_generic(ms, seqStore, tmpRep, src, srcSize, 2, ZSTD_dictMode_e.ZSTD_noDict); + ZSTD_resetSeqStore(seqStore); + ms->window.@base -= srcSize; + ms->window.dictLimit += (uint)(srcSize); + ms->window.lowLimit = ms->window.dictLimit; + ms->nextToUpdate = ms->window.dictLimit; + ZSTD_upscaleStats(&ms->opt); + } + + public static nuint ZSTD_compressBlock_btultra(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_btultra2(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + uint curr = (uint)((byte*)(src) - ms->window.@base); + + assert(srcSize <= (uint)((1 << 17))); + if ((ms->opt.litLengthSum == 0) && (seqStore->sequences == seqStore->sequencesStart) && (ms->window.dictLimit == ms->window.lowLimit) && (curr == ms->window.dictLimit) && (srcSize > 1024)) + { + ZSTD_initStats_ultra(ms, seqStore, rep, src, srcSize); + } + + return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2, ZSTD_dictMode_e.ZSTD_noDict); + } + + public static nuint ZSTD_compressBlock_btopt_dictMatchState(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_btultra_dictMatchState(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2, ZSTD_dictMode_e.ZSTD_dictMatchState); + } + + public static nuint ZSTD_compressBlock_btopt_extDict(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0, ZSTD_dictMode_e.ZSTD_extDict); + } + + public static nuint ZSTD_compressBlock_btultra_extDict(ZSTD_matchState_t* ms, seqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + { + return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2, ZSTD_dictMode_e.ZSTD_extDict); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/_wksps_e__Union.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/_wksps_e__Union.cs new file mode 100644 index 00000000..a90d3613 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/_wksps_e__Union.cs @@ -0,0 +1,15 @@ +using System; +using System.Runtime.InteropServices; + +namespace ZstdSharp.Unsafe +{ + [StructLayout(LayoutKind.Explicit)] + public partial struct _wksps_e__Union + { + [FieldOffset(0)] + public HUF_buildCTable_wksp_tables buildCTable_wksp; + + [FieldOffset(0)] + public HUF_WriteCTableWksp writeCTable_wksp; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/_workspace_e__Union.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/_workspace_e__Union.cs new file mode 100644 index 00000000..05170bd8 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/_workspace_e__Union.cs @@ -0,0 +1,15 @@ +using System; +using System.Runtime.InteropServices; + +namespace ZstdSharp.Unsafe +{ + [StructLayout(LayoutKind.Explicit)] + public unsafe partial struct _workspace_e__Union + { + [FieldOffset(0)] + public fixed uint hist_wksp[1024]; + + [FieldOffset(0)] + public fixed byte scratchBuffer[4096]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/algo_time_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/algo_time_t.cs new file mode 100644 index 00000000..7a7ad66d --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/algo_time_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct algo_time_t + { + public uint tableTime; + + public uint decode256Time; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/blockProperties_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/blockProperties_t.cs new file mode 100644 index 00000000..12b0b45c --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/blockProperties_t.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct blockProperties_t + { + public blockType_e blockType; + + public uint lastBlock; + + public uint origSize; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/blockType_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/blockType_e.cs new file mode 100644 index 00000000..ce5909ac --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/blockType_e.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum blockType_e + { + bt_raw, + bt_rle, + bt_compressed, + bt_reserved, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/decompressionAlgo.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/decompressionAlgo.cs new file mode 100644 index 00000000..d6b3e296 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/decompressionAlgo.cs @@ -0,0 +1,8 @@ +using System; +using System.Runtime.InteropServices; + +namespace ZstdSharp.Unsafe +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public unsafe delegate nuint decompressionAlgo(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize); +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/dictItem.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/dictItem.cs new file mode 100644 index 00000000..df052281 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/dictItem.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct dictItem + { + public uint pos; + + public uint length; + + public uint savings; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/fseWkspMax_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/fseWkspMax_t.cs new file mode 100644 index 00000000..20598588 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/fseWkspMax_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct fseWkspMax_t + { + public fixed uint CTable_max[2561]; + + public _workspace_e__Union workspace; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ldmEntry_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmEntry_t.cs new file mode 100644 index 00000000..d62ce44b --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmEntry_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ldmEntry_t + { + public uint offset; + + public uint checksum; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ldmMatchCandidate_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmMatchCandidate_t.cs new file mode 100644 index 00000000..d492d28b --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmMatchCandidate_t.cs @@ -0,0 +1,15 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ldmMatchCandidate_t + { + public byte* split; + + public uint hash; + + public uint checksum; + + public ldmEntry_t* bucket; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ldmParams_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmParams_t.cs new file mode 100644 index 00000000..63259eb2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmParams_t.cs @@ -0,0 +1,25 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ldmParams_t + { + /* 1 if enable long distance matching */ + public uint enableLdm; + + /* Log size of hashTable */ + public uint hashLog; + + /* Log bucket size for collision resolution, at most 8 */ + public uint bucketSizeLog; + + /* Minimum match length */ + public uint minMatchLength; + + /* Log number of entries to skip */ + public uint hashRateLog; + + /* Window log for the LDM */ + public uint windowLog; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ldmRollingHashState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmRollingHashState_t.cs new file mode 100644 index 00000000..cd5a60a5 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmRollingHashState_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct ldmRollingHashState_t + { + public ulong rolling; + + public ulong stopMask; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/ldmState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmState_t.cs new file mode 100644 index 00000000..6e86b3c4 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/ldmState_t.cs @@ -0,0 +1,251 @@ +using InlineIL; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static InlineIL.IL.Emit; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct ldmState_t + { + /* State for the window round buffer management */ + public ZSTD_window_t window; + + public ldmEntry_t* hashTable; + + public uint loadedDictEnd; + + /* Next position in bucket to insert entry */ + public byte* bucketOffsets; + + public _splitIndices_e__FixedBuffer splitIndices; + + public _matchCandidates_e__FixedBuffer matchCandidates; + + public unsafe partial struct _splitIndices_e__FixedBuffer + { + public nuint e0; + public nuint e1; + public nuint e2; + public nuint e3; + public nuint e4; + public nuint e5; + public nuint e6; + public nuint e7; + public nuint e8; + public nuint e9; + public nuint e10; + public nuint e11; + public nuint e12; + public nuint e13; + public nuint e14; + public nuint e15; + public nuint e16; + public nuint e17; + public nuint e18; + public nuint e19; + public nuint e20; + public nuint e21; + public nuint e22; + public nuint e23; + public nuint e24; + public nuint e25; + public nuint e26; + public nuint e27; + public nuint e28; + public nuint e29; + public nuint e30; + public nuint e31; + public nuint e32; + public nuint e33; + public nuint e34; + public nuint e35; + public nuint e36; + public nuint e37; + public nuint e38; + public nuint e39; + public nuint e40; + public nuint e41; + public nuint e42; + public nuint e43; + public nuint e44; + public nuint e45; + public nuint e46; + public nuint e47; + public nuint e48; + public nuint e49; + public nuint e50; + public nuint e51; + public nuint e52; + public nuint e53; + public nuint e54; + public nuint e55; + public nuint e56; + public nuint e57; + public nuint e58; + public nuint e59; + public nuint e60; + public nuint e61; + public nuint e62; + public nuint e63; + + public ref nuint this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref nuint this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref nuint this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator nuint*(in _splitIndices_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_splitIndices_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static nuint* operator +(in _splitIndices_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_splitIndices_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + + public unsafe partial struct _matchCandidates_e__FixedBuffer + { + public ldmMatchCandidate_t e0; + public ldmMatchCandidate_t e1; + public ldmMatchCandidate_t e2; + public ldmMatchCandidate_t e3; + public ldmMatchCandidate_t e4; + public ldmMatchCandidate_t e5; + public ldmMatchCandidate_t e6; + public ldmMatchCandidate_t e7; + public ldmMatchCandidate_t e8; + public ldmMatchCandidate_t e9; + public ldmMatchCandidate_t e10; + public ldmMatchCandidate_t e11; + public ldmMatchCandidate_t e12; + public ldmMatchCandidate_t e13; + public ldmMatchCandidate_t e14; + public ldmMatchCandidate_t e15; + public ldmMatchCandidate_t e16; + public ldmMatchCandidate_t e17; + public ldmMatchCandidate_t e18; + public ldmMatchCandidate_t e19; + public ldmMatchCandidate_t e20; + public ldmMatchCandidate_t e21; + public ldmMatchCandidate_t e22; + public ldmMatchCandidate_t e23; + public ldmMatchCandidate_t e24; + public ldmMatchCandidate_t e25; + public ldmMatchCandidate_t e26; + public ldmMatchCandidate_t e27; + public ldmMatchCandidate_t e28; + public ldmMatchCandidate_t e29; + public ldmMatchCandidate_t e30; + public ldmMatchCandidate_t e31; + public ldmMatchCandidate_t e32; + public ldmMatchCandidate_t e33; + public ldmMatchCandidate_t e34; + public ldmMatchCandidate_t e35; + public ldmMatchCandidate_t e36; + public ldmMatchCandidate_t e37; + public ldmMatchCandidate_t e38; + public ldmMatchCandidate_t e39; + public ldmMatchCandidate_t e40; + public ldmMatchCandidate_t e41; + public ldmMatchCandidate_t e42; + public ldmMatchCandidate_t e43; + public ldmMatchCandidate_t e44; + public ldmMatchCandidate_t e45; + public ldmMatchCandidate_t e46; + public ldmMatchCandidate_t e47; + public ldmMatchCandidate_t e48; + public ldmMatchCandidate_t e49; + public ldmMatchCandidate_t e50; + public ldmMatchCandidate_t e51; + public ldmMatchCandidate_t e52; + public ldmMatchCandidate_t e53; + public ldmMatchCandidate_t e54; + public ldmMatchCandidate_t e55; + public ldmMatchCandidate_t e56; + public ldmMatchCandidate_t e57; + public ldmMatchCandidate_t e58; + public ldmMatchCandidate_t e59; + public ldmMatchCandidate_t e60; + public ldmMatchCandidate_t e61; + public ldmMatchCandidate_t e62; + public ldmMatchCandidate_t e63; + + public ref ldmMatchCandidate_t this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref ldmMatchCandidate_t this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref ldmMatchCandidate_t this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator ldmMatchCandidate_t*(in _matchCandidates_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_matchCandidates_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static ldmMatchCandidate_t* operator +(in _matchCandidates_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_matchCandidates_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/nodeElt_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/nodeElt_s.cs new file mode 100644 index 00000000..d7182279 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/nodeElt_s.cs @@ -0,0 +1,15 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct nodeElt_s + { + public uint count; + + public ushort parent; + + public byte @byte; + + public byte nbBits; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/offsetCount_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/offsetCount_t.cs new file mode 100644 index 00000000..4d373a09 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/offsetCount_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct offsetCount_t + { + public uint offset; + + public uint count; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/optState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/optState_t.cs new file mode 100644 index 00000000..43a8fedd --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/optState_t.cs @@ -0,0 +1,57 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct optState_t + { + /* table of literals statistics, of size 256 */ + public uint* litFreq; + + /* table of litLength statistics, of size (MaxLL+1) */ + public uint* litLengthFreq; + + /* table of matchLength statistics, of size (MaxML+1) */ + public uint* matchLengthFreq; + + /* table of offCode statistics, of size (MaxOff+1) */ + public uint* offCodeFreq; + + /* list of found matches, of size ZSTD_OPT_NUM+1 */ + public ZSTD_match_t* matchTable; + + /* All positions tracked by optimal parser, of size ZSTD_OPT_NUM+1 */ + public ZSTD_optimal_t* priceTable; + + /* nb of literals */ + public uint litSum; + + /* nb of litLength codes */ + public uint litLengthSum; + + /* nb of matchLength codes */ + public uint matchLengthSum; + + /* nb of offset codes */ + public uint offCodeSum; + + /* to compare to log2(litfreq) */ + public uint litSumBasePrice; + + /* to compare to log2(llfreq) */ + public uint litLengthSumBasePrice; + + /* to compare to log2(mlfreq) */ + public uint matchLengthSumBasePrice; + + /* to compare to log2(offreq) */ + public uint offCodeSumBasePrice; + + /* prices can be determined dynamically, or follow a pre-defined cost structure */ + public ZSTD_OptPrice_e priceType; + + /* pre-calculated dictionary statistics */ + public ZSTD_entropyCTables_t* symbolCosts; + + public ZSTD_literalCompressionMode_e literalCompressionMode; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/rankPos.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/rankPos.cs new file mode 100644 index 00000000..e56e9a2f --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/rankPos.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct rankPos + { + public uint @base; + + public uint curr; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/rankValCol_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/rankValCol_t.cs new file mode 100644 index 00000000..7d6dfd7e --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/rankValCol_t.cs @@ -0,0 +1,21 @@ +using InlineIL; +using System; +using System.Runtime.CompilerServices; +using static InlineIL.IL.Emit; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct rankValCol_t + { + public fixed uint Body[13]; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator uint*(in rankValCol_t t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(rankValCol_t), nameof(Body))); + return IL.ReturnPointer(); + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/rawSeq.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/rawSeq.cs new file mode 100644 index 00000000..38ae21f4 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/rawSeq.cs @@ -0,0 +1,16 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct rawSeq + { + /* Offset of sequence */ + public uint offset; + + /* Length of literals prior to match */ + public uint litLength; + + /* Raw length of match */ + public uint matchLength; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/rawSeqStore_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/rawSeqStore_t.cs new file mode 100644 index 00000000..829df3c9 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/rawSeqStore_t.cs @@ -0,0 +1,23 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct rawSeqStore_t + { + /* The start of the sequences */ + public rawSeq* seq; + + /* The index in seq where reading stopped. pos <= size. */ + public nuint pos; + + /* The position within the sequence at seq[pos] where reading + stopped. posInSequence <= seq[pos].litLength + seq[pos].matchLength */ + public nuint posInSequence; + + /* The number of sequences. <= capacity. */ + public nuint size; + + /* The capacity starting from `seq` pointer */ + public nuint capacity; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/repcodes_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/repcodes_s.cs new file mode 100644 index 00000000..4a443cbc --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/repcodes_s.cs @@ -0,0 +1,9 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct repcodes_s + { + public fixed uint rep[3]; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/searchMax_f.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/searchMax_f.cs new file mode 100644 index 00000000..e335c778 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/searchMax_f.cs @@ -0,0 +1,8 @@ +using System; +using System.Runtime.InteropServices; + +namespace ZstdSharp.Unsafe +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public unsafe delegate nuint searchMax_f(ZSTD_matchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr); +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/searchMethod_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/searchMethod_e.cs new file mode 100644 index 00000000..ec89e7a7 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/searchMethod_e.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum searchMethod_e + { + search_hashChain = 0, + search_binaryTree = 1, + search_rowHash = 2, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/seqDef_s.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/seqDef_s.cs new file mode 100644 index 00000000..407922bc --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/seqDef_s.cs @@ -0,0 +1,17 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /*-******************************************* + * Private declarations + *********************************************/ + public partial struct seqDef_s + { + /* offset == rawOffset + ZSTD_REP_NUM, or equivalently, offCode + 1 */ + public uint offset; + + public ushort litLength; + + public ushort matchLength; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/seqState_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/seqState_t.cs new file mode 100644 index 00000000..fe5ed143 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/seqState_t.cs @@ -0,0 +1,73 @@ +using InlineIL; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static InlineIL.IL.Emit; + +namespace ZstdSharp.Unsafe +{ + public partial struct seqState_t + { + public BIT_DStream_t DStream; + + public ZSTD_fseState stateLL; + + public ZSTD_fseState stateOffb; + + public ZSTD_fseState stateML; + + public _prevOffset_e__FixedBuffer prevOffset; + + public unsafe partial struct _prevOffset_e__FixedBuffer + { + public nuint e0; + public nuint e1; + public nuint e2; + + public ref nuint this[int index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + public ref nuint this[uint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + index); + } + + public ref nuint this[nuint index] + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + get => ref *(this + (uint)index); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static implicit operator nuint*(in _prevOffset_e__FixedBuffer t) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_prevOffset_e__FixedBuffer), nameof(e0))); + return IL.ReturnPointer(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static nuint* operator +(in _prevOffset_e__FixedBuffer t, uint index) + { + Ldarg_0(); + Ldflda(new FieldRef(typeof(_prevOffset_e__FixedBuffer), nameof(e0))); + Ldarg_1(); + Conv_I(); + Sizeof(); + Conv_I(); + Mul(); + Add(); + return IL.ReturnPointer(); + } + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/seqStoreSplits.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/seqStoreSplits.cs new file mode 100644 index 00000000..d0f11f29 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/seqStoreSplits.cs @@ -0,0 +1,14 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + /* Struct to keep track of where we are in our recursive calls. */ + public unsafe partial struct seqStoreSplits + { + /* Array of split indices */ + public uint* splitLocations; + + /* The current index within splitLocations being worked on */ + public nuint idx; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/seqStore_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/seqStore_t.cs new file mode 100644 index 00000000..79376caa --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/seqStore_t.cs @@ -0,0 +1,36 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public unsafe partial struct seqStore_t + { + public seqDef_s* sequencesStart; + + /* ptr to end of sequences */ + public seqDef_s* sequences; + + public byte* litStart; + + /* ptr to end of literals */ + public byte* lit; + + public byte* llCode; + + public byte* mlCode; + + public byte* ofCode; + + public nuint maxNbSeq; + + public nuint maxNbLit; + + /* longLengthPos and longLengthType to allow us to represent either a single litLength or matchLength + * in the seqStore that has a value larger than U16 (if it exists). To do so, we increment + * the existing value of the litLength or matchLength by 0x10000. + */ + public ZSTD_longLengthType_e longLengthType; + + /* Index of the sequence to apply long length modification to */ + public uint longLengthPos; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/seq_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/seq_t.cs new file mode 100644 index 00000000..0e0daafd --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/seq_t.cs @@ -0,0 +1,13 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct seq_t + { + public nuint litLength; + + public nuint matchLength; + + public nuint offset; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/sortedSymbol_t.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/sortedSymbol_t.cs new file mode 100644 index 00000000..b14642f2 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/sortedSymbol_t.cs @@ -0,0 +1,11 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public partial struct sortedSymbol_t + { + public byte symbol; + + public byte weight; + } +} diff --git a/src/SharpCompress/Compressors/Zstd/Unsafe/symbolEncodingType_e.cs b/src/SharpCompress/Compressors/Zstd/Unsafe/symbolEncodingType_e.cs new file mode 100644 index 00000000..beaa7405 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/Unsafe/symbolEncodingType_e.cs @@ -0,0 +1,12 @@ +using System; + +namespace ZstdSharp.Unsafe +{ + public enum symbolEncodingType_e + { + set_basic, + set_rle, + set_compressed, + set_repeat, + } +} diff --git a/src/SharpCompress/Compressors/Zstd/UnsafeHelper.cs b/src/SharpCompress/Compressors/Zstd/UnsafeHelper.cs new file mode 100644 index 00000000..4c2d969a --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/UnsafeHelper.cs @@ -0,0 +1,206 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; +using static InlineIL.IL.Emit; +// ReSharper disable InconsistentNaming +// ReSharper disable IdentifierTypo + +[module: SkipLocalsInit] + +namespace ZstdSharp +{ + public static unsafe class UnsafeHelper + { + public static void* PoisonMemory(void* destination, int size) + { + memset(destination, 0xCC, size); + return destination; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void* malloc(uint size) + { +#if DEBUG + return PoisonMemory((void*)Marshal.AllocHGlobal((int)size), (int)size); +#else + return (void*) Marshal.AllocHGlobal((int) size); +#endif + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void* malloc(ulong size) + { +#if DEBUG + return PoisonMemory((void*)Marshal.AllocHGlobal((int)size), (int)size); +#else + return (void*)Marshal.AllocHGlobal((int)size); +#endif + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void* calloc(ulong num, ulong size) + { + int total = (int)(num * size); + var destination = (void*)Marshal.AllocHGlobal(total); + //Unsafe.InitBlockUnaligned(destination, 0, (uint)total); + //Ldloc(nameof(destination)); + memset(destination, 0, total); + return destination; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void free(void* ptr) + { + Marshal.FreeHGlobal((IntPtr)ptr); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static void* memcpy(void* destination, void* source, ulong size) + { + Ldarg(nameof(destination)); + Ldarg(nameof(source)); + Ldarg(nameof(size)); + Unaligned(1); + Cpblk(); + return destination; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static void* memcpy(void* destination, void* source, uint size) + { + Ldarg(nameof(destination)); + Ldarg(nameof(source)); + Ldarg(nameof(size)); + Unaligned(1); + Cpblk(); + return destination; + } + + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static void* memcpy(void* destination, void* source, int size) + { + Ldarg(nameof(destination)); + Ldarg(nameof(source)); + Ldarg(nameof(size)); + Unaligned(1); + Cpblk(); + return destination; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static void memset(void* memPtr, int val, uint size) + { + Ldarg(nameof(memPtr)); + Ldarg(nameof(val)); + Ldarg(nameof(size)); + Unaligned(1); + Initblk(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static void memset(void* memPtr, int val, int size) + { + Ldarg(nameof(memPtr)); + Ldarg(nameof(val)); + Ldarg(nameof(size)); + Unaligned(1); + Initblk(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static void memset(void* memPtr, int val, ulong size) + { + //Unsafe.InitBlockUnaligned(memPtr, (byte)val, (uint)size); + Ldarg(nameof(memPtr)); + Ldarg(nameof(val)); + Ldarg(nameof(size)); + Unaligned(1); + Initblk(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T* GetArrayPointer(T[] array) where T : unmanaged + { + var size = (uint)(sizeof(T) * array.Length); + var destination = (T*)malloc(size); + fixed (void* source = &array[0]) + { + memcpy(destination, source, size); + } + + return destination; + } + + [Conditional("DEBUG")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void assert(bool condition, string? message = null) + { + if (!condition) + throw new ArgumentException(message ?? "assert failed"); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void memmove(void* destination, void* source, ulong size) + { + Buffer.MemoryCopy(source, destination, size, size); + } + +#if NET + public static bool IsBmi2Supported => System.Runtime.Intrinsics.X86.Bmi2.IsSupported; +#else + public static bool IsBmi2Supported => false; +#endif + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static void Prefetch0(void* p) + { +#if NETCOREAPP3_0_OR_GREATER + if (System.Runtime.Intrinsics.X86.Sse.IsSupported) + { + System.Runtime.Intrinsics.X86.Sse.Prefetch0(p); + } +#endif + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [InlineMethod.Inline] + public static void Prefetch1(void* p) + { +#if NETCOREAPP3_0_OR_GREATER + if (System.Runtime.Intrinsics.X86.Sse.IsSupported) + { + System.Runtime.Intrinsics.X86.Sse.Prefetch1(p); + } +#endif + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int memcmp(void* buf1, void* buf2, ulong size) + { + var p1 = (byte*)buf1; + var p2 = (byte*)buf2; + + while (size > 0) + { + var diff = *p1++ - *p2++; + if (diff != 0) + { + return diff; + } + size--; + } + return 0; + } + } +} diff --git a/src/SharpCompress/Compressors/Zstd/ZstdException.cs b/src/SharpCompress/Compressors/Zstd/ZstdException.cs new file mode 100644 index 00000000..bb12d5a1 --- /dev/null +++ b/src/SharpCompress/Compressors/Zstd/ZstdException.cs @@ -0,0 +1,13 @@ +using System; +using ZstdSharp.Unsafe; + +namespace ZstdSharp +{ + public class ZstdException : Exception + { + public ZstdException(ZSTD_ErrorCode code, string message) : base(message) + => Code = code; + + public ZSTD_ErrorCode Code { get; } + } +} diff --git a/src/SharpCompress/FodyWeavers.xml b/src/SharpCompress/FodyWeavers.xml new file mode 100644 index 00000000..9a562ea9 --- /dev/null +++ b/src/SharpCompress/FodyWeavers.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/SharpCompress/FodyWeavers.xsd b/src/SharpCompress/FodyWeavers.xsd new file mode 100644 index 00000000..38e79916 --- /dev/null +++ b/src/SharpCompress/FodyWeavers.xsd @@ -0,0 +1,83 @@ + + + + + + + + + + + + Defines if sequence points should be generated for each emitted IL instruction. Default value: Debug + + + + + + Insert sequence points in Debug builds only (this is the default). + + + + + Insert sequence points in Release builds only. + + + + + Always insert sequence points. + + + + + Never insert sequence points. + + + + + + + + Defines how warnings should be handled. Default value: Warnings + + + + + + Emit build warnings (this is the default). + + + + + Do not emit warnings. + + + + + Treat warnings as errors. + + + + + + + + + + + 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. + + + + + A comma-separated list of error codes that can be safely ignored in assembly verification. + + + + + 'false' to turn off automatic generation of the XML Schema file. + + + + + \ No newline at end of file diff --git a/src/SharpCompress/SharpCompress.csproj b/src/SharpCompress/SharpCompress.csproj index 9dc1ee19..414789a9 100644 --- a/src/SharpCompress/SharpCompress.csproj +++ b/src/SharpCompress/SharpCompress.csproj @@ -28,6 +28,12 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + @@ -36,9 +42,11 @@ + + diff --git a/tests/SharpCompress.Test/SharpCompress.Test.csproj b/tests/SharpCompress.Test/SharpCompress.Test.csproj index de801011..ac9a74de 100644 --- a/tests/SharpCompress.Test/SharpCompress.Test.csproj +++ b/tests/SharpCompress.Test/SharpCompress.Test.csproj @@ -1,4 +1,4 @@ - + net5.0;net461 SharpCompress.Test @@ -7,6 +7,7 @@ true SharpCompress.Test true + 9 @@ -17,6 +18,7 @@ + diff --git a/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs b/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs index dd8f8943..8ec4ccbc 100644 --- a/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs @@ -61,7 +61,7 @@ namespace SharpCompress.Test.Streams } } - public void Compress(Stream input, Stream output, int compressionLevel) + private void Compress(Stream input, Stream output, int compressionLevel) { using (var zlibStream = new ZlibStream(new NonDisposingStream(output), CompressionMode.Compress, (CompressionLevel)compressionLevel)) { @@ -70,7 +70,7 @@ namespace SharpCompress.Test.Streams } } - public void Decompress(Stream input, Stream output) + private void Decompress(Stream input, Stream output) { using (var zlibStream = new ZlibStream(new NonDisposingStream(input), CompressionMode.Decompress)) { @@ -78,7 +78,7 @@ namespace SharpCompress.Test.Streams } } - byte[] GetBytes(BufferedStream stream) + private byte[] GetBytes(BufferedStream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, (int)stream.Length); diff --git a/tests/SharpCompress.Test/Zstd/ZstdNetSteamingTests.cs b/tests/SharpCompress.Test/Zstd/ZstdNetSteamingTests.cs new file mode 100644 index 00000000..f54f9a5d --- /dev/null +++ b/tests/SharpCompress.Test/Zstd/ZstdNetSteamingTests.cs @@ -0,0 +1,436 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Xunit; +using ZstdSharp.Unsafe; + +namespace ZstdSharp.Test +{ + public enum DataFill + { + Random, + Sequential + } + + internal static class DataGenerator + { + private static readonly Random Random = new(1234); + + public const int LargeBufferSize = 1024 * 1024; + public const int SmallBufferSize = 1024; + + public static MemoryStream GetSmallStream(DataFill dataFill) => GetStream(SmallBufferSize, dataFill); + public static MemoryStream GetLargeStream(DataFill dataFill) => GetStream(LargeBufferSize, dataFill); + public static MemoryStream GetStream(int length, DataFill dataFill) => new(GetBuffer(length, dataFill)); + + public static byte[] GetSmallBuffer(DataFill dataFill) => GetBuffer(SmallBufferSize, dataFill); + public static byte[] GetLargeBuffer(DataFill dataFill) => GetBuffer(LargeBufferSize, dataFill); + + public static byte[] GetBuffer(int length, DataFill dataFill) + { + var buffer = new byte[length]; + if (dataFill == DataFill.Random) + Random.NextBytes(buffer); + else + { + for (int i = 0; i < buffer.Length; i++) + buffer[i] = (byte) (i % 256); + } + + return buffer; + } + } + + public class ZstdNetSteamingTests + { + [Fact] + public void StreamingCompressionZeroAndOneByte() + { + var data = new byte[] {0, 0, 0, 1, 2, 3, 4, 0, 0, 0}; + + var tempStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(tempStream)) + { + compressionStream.Write(data, 0, 0); + compressionStream.Write(ReadOnlySpan.Empty); + compressionStream.WriteAsync(data, 0, 0).GetAwaiter().GetResult(); + compressionStream.WriteAsync(ReadOnlyMemory.Empty).GetAwaiter().GetResult(); + + compressionStream.Write(data, 3, 1); + compressionStream.Write(new ReadOnlySpan(data, 4, 1)); + compressionStream.Flush(); + compressionStream.WriteAsync(data, 5, 1).GetAwaiter().GetResult(); + compressionStream.WriteAsync(new ReadOnlyMemory(data, 6, 1)).GetAwaiter().GetResult(); + compressionStream.FlushAsync().GetAwaiter().GetResult(); + } + + tempStream.Seek(0, SeekOrigin.Begin); + + var result = new byte[data.Length]; + using (var decompressionStream = new DecompressionStream(tempStream)) + { + Assert.Equal(0, decompressionStream.Read(result, 0, 0)); + Assert.Equal(0, decompressionStream.Read(Span.Empty)); + Assert.Equal(0, decompressionStream.ReadAsync(result, 0, 0).GetAwaiter().GetResult()); + Assert.Equal(0, decompressionStream.ReadAsync(Memory.Empty).GetAwaiter().GetResult()); + + Assert.Equal(1, decompressionStream.Read(result, 3, 1)); + Assert.Equal(1, decompressionStream.Read(new Span(result, 4, 1))); + Assert.Equal(1, decompressionStream.ReadAsync(result, 5, 1).GetAwaiter().GetResult()); + Assert.Equal(1, decompressionStream.ReadAsync(new Memory(result, 6, 1)).GetAwaiter().GetResult()); + } + + Assert.True(data.SequenceEqual(result)); + } + + + [Theory] + [InlineData(new byte[0], 0, 0)] + [InlineData(new byte[] {1, 2, 3}, 1, 2)] + [InlineData(new byte[] {1, 2, 3}, 0, 2)] + [InlineData(new byte[] {1, 2, 3}, 1, 1)] + [InlineData(new byte[] {1, 2, 3}, 0, 3)] + public void StreamingCompressionSimpleWrite(byte[] data, int offset, int count) + { + var tempStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(tempStream)) + compressionStream.Write(data, offset, count); + + tempStream.Seek(0, SeekOrigin.Begin); + + var resultStream = new MemoryStream(); + using (var decompressionStream = new DecompressionStream(tempStream)) + decompressionStream.CopyTo(resultStream); + + var dataToCompress = new byte[count]; + Array.Copy(data, offset, dataToCompress, 0, count); + + Assert.True(dataToCompress.SequenceEqual(resultStream.ToArray())); + } + + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(5)] + [InlineData(9)] + [InlineData(10)] + public void StreamingDecompressionSimpleRead(int readCount) + { + var data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + var tempStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(tempStream)) + compressionStream.Write(data, 0, data.Length); + + tempStream.Seek(0, SeekOrigin.Begin); + + var buffer = new byte[data.Length]; + using (var decompressionStream = new DecompressionStream(tempStream)) + { + int bytesRead; + int totalBytesRead = 0; + while ((bytesRead = decompressionStream.Read(buffer, totalBytesRead, + Math.Min(readCount, buffer.Length - totalBytesRead))) > 0) + { + Assert.True(bytesRead <= readCount); + totalBytesRead += bytesRead; + } + + Assert.Equal(data.Length, totalBytesRead); + } + + Assert.True(data.SequenceEqual(buffer)); + } + + [Fact] + public void StreamingDecompressionTruncatedInput() + { + var dataStream = DataGenerator.GetLargeStream(DataFill.Sequential); + + var resultStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(resultStream)) + dataStream.CopyTo(compressionStream); + + // truncate resultStream + var truncatedStream = + new MemoryStream(resultStream.ToArray(), 0, Math.Min(32, (int) resultStream.Length / 3)); + + var exception = Record.Exception(() => + { + using var decompressionStream = new DecompressionStream(truncatedStream); + decompressionStream.CopyTo(resultStream); + }); + Assert.True(exception is EndOfStreamException); + } + + [Fact] + public void StreamingCompressionFlushDataFromInternalBuffers() + { + var testBuffer = new byte[1]; + + var tempStream = new MemoryStream(); + using var compressionStream = new CompressionStream(tempStream); + compressionStream.Write(testBuffer, 0, testBuffer.Length); + compressionStream.Flush(); + + Assert.True(tempStream.Length > 0); + tempStream.Seek(0, SeekOrigin.Begin); + + //NOTE: without ZSTD_endStream call on compression + var resultStream = new MemoryStream(); + using (var decompressionStream = new DecompressionStream(tempStream)) + decompressionStream.CopyTo(resultStream); + + Assert.True(testBuffer.SequenceEqual(resultStream.ToArray())); + } + + [Fact] + public void CompressionImprovesWithDictionary() + { + var dict = TrainDict(); + + var dataStream = DataGenerator.GetSmallStream(DataFill.Sequential); + + var normalResultStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(normalResultStream)) + dataStream.CopyTo(compressionStream); + + dataStream.Seek(0, SeekOrigin.Begin); + + var dictResultStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(dictResultStream)) + { + compressionStream.LoadDictionary(dict); + dataStream.CopyTo(compressionStream); + } + + Assert.True(normalResultStream.Length > dictResultStream.Length); + + dictResultStream.Seek(0, SeekOrigin.Begin); + + var resultStream = new MemoryStream(); + using (var decompressionStream = new DecompressionStream(dictResultStream)) + { + decompressionStream.LoadDictionary(dict); + decompressionStream.CopyTo(resultStream); + } + + Assert.True(dataStream.ToArray().SequenceEqual(resultStream.ToArray())); + } + + [Fact] + public void CompressionShrinksData() + { + var dataStream = DataGenerator.GetLargeStream(DataFill.Sequential); + + var resultStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(resultStream)) + dataStream.CopyTo(compressionStream); + + Assert.True(dataStream.Length > resultStream.Length); + } + + [Fact] + public void RoundTrip_BatchToStreaming() + { + var data = DataGenerator.GetLargeBuffer(DataFill.Sequential); + + byte[] compressed; + using (var compressor = new Compressor()) + compressed = compressor.Wrap(data).ToArray(); + + var resultStream = new MemoryStream(); + using (var decompressionStream = new DecompressionStream(new MemoryStream(compressed))) + decompressionStream.CopyTo(resultStream); + + Assert.True(data.SequenceEqual(resultStream.ToArray())); + } + + [Fact] + public void RoundTrip_StreamingToBatch() + { + var dataStream = DataGenerator.GetLargeStream(DataFill.Sequential); + + var tempStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(tempStream)) + dataStream.CopyTo(compressionStream); + + var resultBuffer = new byte[dataStream.Length]; + using (var decompressor = new Decompressor()) + Assert.Equal(dataStream.Length, decompressor.Unwrap(tempStream.ToArray(), resultBuffer, 0)); + + Assert.True(dataStream.ToArray().SequenceEqual(resultBuffer)); + } + + [Theory, CombinatorialData] + public void RoundTrip_StreamingToStreaming( + [CombinatorialValues(false, true)] bool useDict, [CombinatorialValues(false, true)] bool advanced, + [CombinatorialValues(1, 2, 7, 101, 1024, 65535, DataGenerator.LargeBufferSize, + DataGenerator.LargeBufferSize + 1)] + int zstdBufferSize, + [CombinatorialValues(1, 2, 7, 101, 1024, 65535, DataGenerator.LargeBufferSize, + DataGenerator.LargeBufferSize + 1)] + int copyBufferSize) + { + var dict = useDict ? TrainDict() : null; + var testStream = DataGenerator.GetLargeStream(DataFill.Sequential); + + const int offset = 1; + var buffer = new byte[copyBufferSize + offset + 1]; + + var tempStream = new MemoryStream(); + using (var compressionStream = + new CompressionStream(tempStream, Compressor.DefaultCompressionLevel, zstdBufferSize)) + { + compressionStream.LoadDictionary(dict); + if (advanced) + { + compressionStream.SetParameter(ZSTD_cParameter.ZSTD_c_windowLog, 11); + compressionStream.SetParameter(ZSTD_cParameter.ZSTD_c_checksumFlag, 1); + } + + int bytesRead; + while ((bytesRead = testStream.Read(buffer, offset, copyBufferSize)) > 0) + compressionStream.Write(buffer, offset, bytesRead); + } + + tempStream.Seek(0, SeekOrigin.Begin); + + var resultStream = new MemoryStream(); + using (var decompressionStream = new DecompressionStream(tempStream, zstdBufferSize)) + { + decompressionStream.LoadDictionary(dict); + if (advanced) + { + decompressionStream.SetParameter(ZSTD_dParameter.ZSTD_d_windowLogMax, 11); + } + + int bytesRead; + while ((bytesRead = decompressionStream.Read(buffer, offset, copyBufferSize)) > 0) + resultStream.Write(buffer, offset, bytesRead); + } + + Assert.True(testStream.ToArray().SequenceEqual(resultStream.ToArray())); + } + + [Theory, CombinatorialData] + public async Task RoundTrip_StreamingToStreamingAsync( + [CombinatorialValues(false, true)] bool useDict, [CombinatorialValues(false, true)] bool advanced, + [CombinatorialValues(1, 2, 7, 101, 1024, 65535, DataGenerator.LargeBufferSize, + DataGenerator.LargeBufferSize + 1)] + int zstdBufferSize, + [CombinatorialValues(1, 2, 7, 101, 1024, 65535, DataGenerator.LargeBufferSize, + DataGenerator.LargeBufferSize + 1)] + int copyBufferSize) + { + var dict = useDict ? TrainDict() : null; + var testStream = DataGenerator.GetLargeStream(DataFill.Sequential); + + const int offset = 1; + var buffer = new byte[copyBufferSize + offset + 1]; + + var tempStream = new MemoryStream(); + await using (var compressionStream = + new CompressionStream(tempStream, Compressor.DefaultCompressionLevel, zstdBufferSize)) + { + compressionStream.LoadDictionary(dict); + if (advanced) + { + compressionStream.SetParameter(ZSTD_cParameter.ZSTD_c_windowLog, 11); + compressionStream.SetParameter(ZSTD_cParameter.ZSTD_c_checksumFlag, 1); + } + + int bytesRead; + while ((bytesRead = await testStream.ReadAsync(buffer, offset, copyBufferSize)) > 0) + await compressionStream.WriteAsync(buffer, offset, bytesRead); + } + + tempStream.Seek(0, SeekOrigin.Begin); + + var resultStream = new MemoryStream(); + await using (var decompressionStream = new DecompressionStream(tempStream, zstdBufferSize)) + { + decompressionStream.LoadDictionary(dict); + if (advanced) + { + decompressionStream.SetParameter(ZSTD_dParameter.ZSTD_d_windowLogMax, 11); + } + + int bytesRead; + while ((bytesRead = await decompressionStream.ReadAsync(buffer, offset, copyBufferSize)) > 0) + await resultStream.WriteAsync(buffer, offset, bytesRead); + } + + Assert.True(testStream.ToArray().SequenceEqual(resultStream.ToArray())); + } + + [Theory(Skip = "stress"), CombinatorialData] + public void RoundTrip_StreamingToStreaming_Stress([CombinatorialValues(true, false)] bool useDict, + [CombinatorialValues(true, false)] bool async) + { + long i = 0; + var dict = useDict ? TrainDict() : null; + Enumerable.Range(0, 10000) + .AsParallel() + .WithDegreeOfParallelism(Environment.ProcessorCount * 4) + .ForAll(n => + { + var testStream = DataGenerator.GetSmallStream(DataFill.Sequential); + var cBuffer = new byte[1 + (int) (n % (testStream.Length * 11))]; + var dBuffer = new byte[1 + (int) (n % (testStream.Length * 13))]; + + var tempStream = new MemoryStream(); + using (var compressionStream = new CompressionStream(tempStream, Compressor.DefaultCompressionLevel, + 1 + (int) (n % (testStream.Length * 17)))) + { + compressionStream.LoadDictionary(dict); + int bytesRead; + int offset = n % cBuffer.Length; + while ((bytesRead = testStream.Read(cBuffer, offset, cBuffer.Length - offset)) > 0) + { + if (async) + compressionStream.WriteAsync(cBuffer, offset, bytesRead).GetAwaiter().GetResult(); + else + compressionStream.Write(cBuffer, offset, bytesRead); + if (Interlocked.Increment(ref i) % 100 == 0) + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); + } + } + + tempStream.Seek(0, SeekOrigin.Begin); + + var resultStream = new MemoryStream(); + using (var decompressionStream = + new DecompressionStream(tempStream, 1 + (int) (n % (testStream.Length * 19)))) + { + decompressionStream.LoadDictionary(dict); + int bytesRead; + int offset = n % dBuffer.Length; + while ((bytesRead = async + ? decompressionStream.ReadAsync(dBuffer, offset, dBuffer.Length - offset).GetAwaiter() + .GetResult() + : decompressionStream.Read(dBuffer, offset, dBuffer.Length - offset)) > 0) + { + resultStream.Write(dBuffer, offset, bytesRead); + if (Interlocked.Increment(ref i) % 100 == 0) + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); + } + } + + Assert.True(testStream.ToArray().SequenceEqual(resultStream.ToArray())); + }); + } + + private static byte[] TrainDict() + { + var trainingData = new byte[100][]; + for (int i = 0; i < trainingData.Length; i++) + trainingData[i] = DataGenerator.GetSmallBuffer(DataFill.Sequential); + return DictBuilder.TrainFromBuffer(trainingData); + } + } +} diff --git a/tests/SharpCompress.Test/Zstd/ZstdNetTests.cs b/tests/SharpCompress.Test/Zstd/ZstdNetTests.cs new file mode 100644 index 00000000..aa995240 --- /dev/null +++ b/tests/SharpCompress.Test/Zstd/ZstdNetTests.cs @@ -0,0 +1,646 @@ +using System; +using System.Linq; +using System.Text; +using System.Threading; +using Xunit; +using ZstdSharp.Unsafe; + +namespace ZstdSharp.Test +{ + public class ZstdNetTests + { + public enum CompressionLevel + { + Default = 0, + Min, + Max + } + + [Theory] + [InlineData(false, CompressionLevel.Min)] + [InlineData(true, CompressionLevel.Min)] + [InlineData(false, CompressionLevel.Default)] + [InlineData(true, CompressionLevel.Default)] + [InlineData(false, CompressionLevel.Max)] + [InlineData(true, CompressionLevel.Max)] + public void CompressAndDecompress_workCorrectly(bool useDictionary, CompressionLevel level) + { + var data = GenerateSample(); + + var dict = useDictionary ? BuildDictionary() : null; + var compressionLevel = level switch + { + CompressionLevel.Min => Compressor.MinCompressionLevel, + CompressionLevel.Max => Compressor.MaxCompressionLevel, + _ => Compressor.DefaultCompressionLevel + }; + + Assert.True(CompressAndDecompress(data, dict, compressionLevel).SequenceEqual(data)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_worksCorrectly_advanced(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + + Span compressed1, compressed2; + + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressor.SetParameter(ZSTD_cParameter.ZSTD_c_checksumFlag, 0); + compressed1 = compressor.Wrap(data); + } + + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressor.SetParameter(ZSTD_cParameter.ZSTD_c_checksumFlag, 1); + compressed2 = compressor.Wrap(data); + } + + Assert.Equal(compressed1.Length + 4, compressed2.Length); + + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + Assert.True(decompressor.Unwrap(compressed1).SequenceEqual(data)); + Assert.True(decompressor.Unwrap(compressed2).SequenceEqual(data)); + } + } + + [Fact] + public void DecompressWithDictionary_worksCorrectly_onDataCompressedWithoutIt() + { + var data = GenerateSample(); + Span compressed; + using (var compressor = new Compressor()) + compressed = compressor.Wrap(data); + + var dict = BuildDictionary(); + + Span decompressed; + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + decompressed = decompressor.Unwrap(compressed); + } + + Assert.True(decompressed.SequenceEqual(data)); + } + + [Fact] + public void DecompressWithoutDictionary_throwsZstdException_onDataCompressedWithIt() + { + var data = GenerateSample(); + var dict = BuildDictionary(); + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(data).ToArray(); + } + + using (var decompressor = new Decompressor()) + { + Assert.Throws(() => decompressor.Unwrap(compressed)); + } + } + + [Fact] + public void DecompressWithAnotherDictionary_throwsZstdException() + { + var data = GenerateSample(); + var oldDict = BuildDictionary(); + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(oldDict); + compressed = compressor.Wrap(data).ToArray(); + } + + var newDict = Encoding.ASCII.GetBytes("zstd supports raw-content dictionaries"); + + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(newDict); + Assert.Throws(() => decompressor.Unwrap(compressed)); + } + } + + [Fact] + public void Compress_reducesDataSize() + { + var data = GenerateSample(); + + Span compressed; + using (var compressor = new Compressor()) + compressed = compressor.Wrap(data); + + Assert.True(data.Length > compressed.Length); + } + + [Fact] + public void Compress_worksBetter_withDictionary() + { + var data = GenerateSample(); + + Span compressedWithoutDict, compressedWithDict; + using (var compressor = new Compressor()) + compressedWithoutDict = compressor.Wrap(data); + + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(BuildDictionary()); + compressedWithDict = compressor.Wrap(data); + } + + Assert.True(compressedWithoutDict.Length > compressedWithDict.Length); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Decompress_throwsZstdException_onInvalidData(bool useDictionary) + { + var data = GenerateSample(); // This isn't data in compressed format + var dict = useDictionary ? BuildDictionary() : null; + + using var decompressor = new Decompressor(); + decompressor.LoadDictionary(dict); + Assert.Throws(() => decompressor.Unwrap(data)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Decompress_throwsZstdException_onMalformedDecompressedSize(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(data).ToArray(); + } + + var frameHeader = compressed[4]; // Ensure that we malform decompressed size in the right place + if (useDictionary) + { + Assert.Equal(0x63, frameHeader); + compressed[9]--; + } + else + { + Assert.Equal(0x60, frameHeader); + compressed[5]--; + } + + // Thus, ZSTD_getDecompressedSize will return size that is one byte lesser than actual + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + Assert.Throws(() => decompressor.Unwrap(compressed)); + } + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Decompress_throwsArgumentOutOfRangeException_onTooBigData(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(data).ToArray(); + } + + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + var ex = Assert.Throws(() => decompressor.Unwrap(compressed, 20)); + Assert.Equal(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, ex.Code); + } + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Decompress_tryUnwrap_onTooBigData(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(data).ToArray(); + } + + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + var dest = new byte[20]; + Assert.False(decompressor.TryUnwrap(compressed, dest, out _)); + } + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Compress_canRead_fromArraySegment(bool useDictionary) + { + var data = GenerateSample(); + var segment = new ArraySegment(data, 2, data.Length - 5); + var dict = useDictionary ? BuildDictionary() : null; + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(segment).ToArray(); + } + + byte[] decompressed; + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + decompressed = decompressor.Unwrap(compressed).ToArray(); + } + + Assert.True(segment.SequenceEqual(decompressed)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_workCorrectly_spans(bool useDictionary) + { + var buffer = GenerateSample(); + + var data = new ReadOnlySpan(buffer, 1, buffer.Length - 1); + var dict = useDictionary ? BuildDictionary() : null; + + Span compressed = stackalloc byte[Compressor.GetCompressBound(data.Length)]; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + var size = compressor.Wrap(data, compressed); + compressed = compressed.Slice(0, size); + } + + Span decompressed = stackalloc byte[data.Length + 1]; + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + var size = decompressor.Unwrap(compressed, decompressed); + Assert.Equal(data.Length, size); + decompressed = decompressed.Slice(0, size); + } + + Assert.True(data.ToArray().SequenceEqual(decompressed.ToArray())); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Decompress_canRead_fromArraySegment(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(data).ToArray(); + } + + compressed = new byte[] { 1, 2 }.Concat(compressed).Concat(new byte[] { 4, 5, 6 }).ToArray(); + var segment = new ArraySegment(compressed, 2, compressed.Length - 5); + + byte[] decompressed; + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + decompressed = decompressor.Unwrap(segment).ToArray(); + } + + Assert.True(data.SequenceEqual(decompressed)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Compress_canWrite_toGivenBuffer(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + var compressed = new byte[1000]; + const int offset = 54; + + int compressedSize; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressedSize = compressor.Wrap(data, compressed, offset); + } + + byte[] decompressed; + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + decompressed = decompressor.Unwrap(compressed.Skip(offset).Take(compressedSize).ToArray()).ToArray(); + } + + Assert.True(data.SequenceEqual(decompressed)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Decompress_canWrite_toGivenBuffer(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(data).ToArray(); + } + + var decompressed = new byte[1000]; + const int offset = 54; + + int decompressedSize; + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + decompressedSize = decompressor.Unwrap(compressed, decompressed, offset); + } + + Assert.True(data.SequenceEqual(decompressed.Skip(offset).Take(decompressedSize))); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Compress_throwsDstSizeTooSmall_whenDestinationBufferIsTooSmall(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + var compressed = new byte[20]; + const int offset = 4; + + using var compressor = new Compressor(); + compressor.LoadDictionary(dict); + var ex = Assert.Throws(() => compressor.Wrap(data, compressed, offset)); + Assert.Equal(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, ex.Code); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Compress_tryWrap_whenDestinationBufferIsTooSmall(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + var compressed = new byte[20]; + const int offset = 4; + + using var compressor = new Compressor(); + compressor.LoadDictionary(dict); + Assert.False(compressor.TryWrap(data, compressed, offset, out _)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Decompress_throwsDstSizeTooSmall_whenDestinationBufferIsTooSmall(bool useDictionary) + { + var data = GenerateSample(); + var dict = useDictionary ? BuildDictionary() : null; + + byte[] compressed; + using (var compressor = new Compressor()) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(data).ToArray(); + } + + var decompressed = new byte[20]; + const int offset = 4; + + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + var ex = Assert.Throws(() => decompressor.Unwrap(compressed, decompressed, offset)); + Assert.Equal(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, ex.Code); + } + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_workCorrectly_onEmptyBuffer(bool useDictionary) + { + var data = new byte[0]; + var dict = useDictionary ? BuildDictionary() : null; + + Assert.True(CompressAndDecompress(data, dict).SequenceEqual(data)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_workCorrectly_onOneByteBuffer(bool useDictionary) + { + var data = new byte[] { 42 }; + var dict = useDictionary ? BuildDictionary() : null; + + Assert.True(CompressAndDecompress(data, dict).SequenceEqual(data)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_workCorrectly_onArraysOfDifferentSizes(bool useDictionary) + { + var dict = useDictionary ? BuildDictionary() : null; + using var compressor = new Compressor(); + using var decompressor = new Decompressor(); + compressor.LoadDictionary(dict); + decompressor.LoadDictionary(dict); + for (var i = 2; i < 100000; i += 3000) + { + var data = GenerateBuffer(i); + + var decompressed = decompressor.Unwrap(compressor.Wrap(data)).ToArray(); + + Assert.True(data.SequenceEqual(decompressed)); + } + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_workCorrectly_ifDifferentInstancesRunInDifferentThreads(bool useDictionary) + { + var dict = useDictionary ? BuildDictionary() : null; + Enumerable.Range(0, 100) + .AsParallel().WithDegreeOfParallelism(50) + .ForAll(_ => + { + using var compressor = new Compressor(); + using var decompressor = new Decompressor(); + compressor.LoadDictionary(dict); + decompressor.LoadDictionary(dict); + + for (var i = 2; i < 100000; i += 30000) + { + var data = GenerateBuffer(i); + + var decompressed = decompressor.Unwrap(compressor.Wrap(data)); + + Assert.True(decompressed.SequenceEqual(data)); + } + }); + } + + [Theory(Skip = "Explicit/stress")] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_workCorrectly_stress(bool useDictionary) + { + long i = 0L; + var data = GenerateBuffer(65536); + var dict = useDictionary ? BuildDictionary() : null; + Enumerable.Range(0, 10000) + .AsParallel().WithDegreeOfParallelism(100) + .ForAll(_ => + { + using var compressor = new Compressor(); + using var decompressor = new Decompressor(); + compressor.LoadDictionary(dict); + decompressor.LoadDictionary(dict); + var decompressed = decompressor.Unwrap(compressor.Wrap(data)); + if (Interlocked.Increment(ref i) % 100 == 0) + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); + Assert.True(decompressed.SequenceEqual(data)); + }); + } + + [Theory(Skip = "Explicit/memory consuming")] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_workCorrectly_2GB(bool useDictionary) + { + var data = new byte[MaxByteArrayLength]; + new Span(data, 100, 10000000).Fill(0xff); + + var dict = useDictionary ? BuildDictionary() : null; + + Assert.True(CompressAndDecompress(data, dict).SequenceEqual(data)); + + int size; + //NOTE: Calc max reliable compression data size + for (size = MaxByteArrayLength; Compressor.GetCompressBoundLong((ulong)size) > MaxByteArrayLength; size--) + { + } + + data = new byte[size]; + new Random(1337).NextBytes(data); //NOTE: Uncompressible data + + Assert.True(CompressAndDecompress(data, dict).SequenceEqual(data)); + } + + [Theory(Skip = "Explicit/memory consuming")] + [InlineData(false)] + [InlineData(true)] + public void CompressAndDecompress_throwsDstSizeTooSmall_Over2GB(bool useDictionary) + { + var data = new byte[MaxByteArrayLength]; + new Random(1337).NextBytes(data); //NOTE: Uncompressible data + + var dict = useDictionary ? BuildDictionary() : null; + + using var compressor = new Compressor(); + compressor.LoadDictionary(dict); + var ex = Assert.Throws(() => compressor.Wrap(data)); + Assert.Equal(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, ex.Code); + } + + [Fact(Skip = "Explicit/stress")] + public void TrainDictionaryParallel() + { + var buffer = Enumerable.Range(0, 100000).Select(i => unchecked((byte)(i * i))).ToArray(); + var samples = Enumerable.Range(0, 100) + .Select(i => buffer.Skip(i).Take(200 - i).ToArray()) + .ToArray(); + + var dict = DictBuilder.TrainFromBuffer(samples); + Assert.True(dict.Length > 0); + Assert.True(dict.Length <= DictBuilder.DefaultDictCapacity); + + Enumerable.Range(0, 100000) + .AsParallel().WithDegreeOfParallelism(Environment.ProcessorCount * 4) + .ForAll(_ => Assert.True(dict.SequenceEqual(DictBuilder.TrainFromBuffer(samples)))); + } + + private static byte[] BuildDictionary() + => DictBuilder.TrainFromBuffer(Enumerable.Range(0, 8).Select(_ => GenerateSample()).ToArray(), 1024); + + private static byte[] GenerateSample() + { + var random = new Random(1234); + return Enumerable.Range(0, 10) + .SelectMany(_ => + Encoding.ASCII.GetBytes( + $"['a': 'constant_field', 'b': '{random.Next()}', 'c': {random.Next()}, 'd': '{(random.Next(1) == 1 ? "almost" : "sometimes")} constant field']")) + .ToArray(); + } + + private static byte[] GenerateBuffer(int size) + { + return Enumerable.Range(0, size) + .Select(i => unchecked((byte) i)) + .ToArray(); + } + + private static Span CompressAndDecompress(byte[] data, byte[] dict, + int compressionLevel = Compressor.DefaultCompressionLevel) + { + Span compressed; + using (var compressor = new Compressor(compressionLevel)) + { + compressor.LoadDictionary(dict); + compressed = compressor.Wrap(data); + } + + Span decompressed; + using (var decompressor = new Decompressor()) + { + decompressor.LoadDictionary(dict); + decompressed = decompressor.Unwrap(compressed); + } + + return decompressed; + } + + private const int MaxByteArrayLength = 0x7FFFFFC7; + } +}