From 0a7ffd003b160759d8f4f96fbf45763aaf2c06c2 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 13 Oct 2025 16:42:22 +0100 Subject: [PATCH] ran formatting --- .../Compressors/ZStandard/BitOperations.cs | 124 +- .../ZStandard/CompressionStream.cs | 161 +- .../Compressors/ZStandard/Compressor.cs | 89 +- .../Compressors/ZStandard/Constants.cs | 12 +- .../ZStandard/DecompressionStream.cs | 91 +- .../Compressors/ZStandard/Decompressor.cs | 88 +- .../Compressors/ZStandard/JobThreadPool.cs | 8 +- .../Compressors/ZStandard/Pool.cs | 11 +- .../Compressors/ZStandard/SafeHandles.cs | 26 +- .../Compressors/ZStandard/ThrowHelper.cs | 12 +- .../ZStandard/Unsafe/Allocations.cs | 17 +- .../ZStandard/Unsafe/BIT_CStream_t.cs | 2 +- .../ZStandard/Unsafe/BIT_DStream_status.cs | 7 +- .../ZStandard/Unsafe/BIT_DStream_t.cs | 2 +- .../Compressors/ZStandard/Unsafe/Bits.cs | 12 +- .../Compressors/ZStandard/Unsafe/Bitstream.cs | 297 +- .../ZStandard/Unsafe/BlockSummary.cs | 2 +- .../ZStandard/Unsafe/COVER_best_s.cs | 2 +- .../ZStandard/Unsafe/COVER_ctx_t.cs | 2 +- .../ZStandard/Unsafe/COVER_dictSelection.cs | 2 +- .../ZStandard/Unsafe/COVER_epoch_info_t.cs | 2 +- .../ZStandard/Unsafe/COVER_map_pair_t_s.cs | 2 +- .../ZStandard/Unsafe/COVER_map_s.cs | 2 +- .../ZStandard/Unsafe/COVER_segment_t.cs | 2 +- .../Unsafe/COVER_tryParameters_data_s.cs | 2 +- .../Compressors/ZStandard/Unsafe/Clevels.cs | 951 +++- .../Compressors/ZStandard/Unsafe/Compiler.cs | 2 +- .../Compressors/ZStandard/Unsafe/Cover.cs | 119 +- .../ZStandard/Unsafe/DTableDesc.cs | 2 +- .../ZStandard/Unsafe/EStats_ress_t.cs | 4 +- .../ZStandard/Unsafe/EntropyCommon.cs | 177 +- .../ZStandard/Unsafe/ErrorPrivate.cs | 2 +- .../ZStandard/Unsafe/EstimatedBlockSize.cs | 2 +- .../ZStandard/Unsafe/FASTCOVER_accel_t.cs | 4 +- .../ZStandard/Unsafe/FASTCOVER_ctx_t.cs | 2 +- .../Unsafe/FASTCOVER_tryParameters_data_s.cs | 2 +- .../Compressors/ZStandard/Unsafe/FPStats.cs | 2 +- .../ZStandard/Unsafe/FSE_CState_t.cs | 2 +- .../ZStandard/Unsafe/FSE_DState_t.cs | 3 +- .../ZStandard/Unsafe/FSE_DTableHeader.cs | 2 +- .../ZStandard/Unsafe/FSE_DecompressWksp.cs | 2 +- .../ZStandard/Unsafe/FSE_decode_t.cs | 2 +- .../ZStandard/Unsafe/FSE_repeat.cs | 6 +- .../Unsafe/FSE_symbolCompressionTransform.cs | 2 +- .../Compressors/ZStandard/Unsafe/Fastcover.cs | 251 +- .../ZStandard/Unsafe/Fingerprint.cs | 2 +- .../Compressors/ZStandard/Unsafe/Fse.cs | 83 +- .../ZStandard/Unsafe/FseCompress.cs | 246 +- .../ZStandard/Unsafe/FseDecompress.cs | 291 +- .../ZStandard/Unsafe/HIST_checkInput_e.cs | 4 +- .../ZStandard/Unsafe/HUF_CStream_t.cs | 3 +- .../ZStandard/Unsafe/HUF_CTableHeader.cs | 2 +- .../Unsafe/HUF_CompressWeightsWksp.cs | 2 +- .../ZStandard/Unsafe/HUF_DEltX1.cs | 2 +- .../ZStandard/Unsafe/HUF_DEltX2.cs | 2 +- .../Unsafe/HUF_DecompressFastArgs.cs | 3 +- .../Unsafe/HUF_ReadDTableX1_Workspace.cs | 2 +- .../Unsafe/HUF_ReadDTableX2_Workspace.cs | 3 +- .../ZStandard/Unsafe/HUF_WriteCTableWksp.cs | 3 +- .../Unsafe/HUF_buildCTable_wksp_tables.cs | 3 +- .../ZStandard/Unsafe/HUF_compress_tables_t.cs | 3 +- .../ZStandard/Unsafe/HUF_flags_e.cs | 9 +- .../ZStandard/Unsafe/HUF_nbStreams_e.cs | 4 +- .../ZStandard/Unsafe/HUF_repeat.cs | 6 +- .../Compressors/ZStandard/Unsafe/Hist.cs | 99 +- .../ZStandard/Unsafe/HufCompress.cs | 772 ++- .../ZStandard/Unsafe/HufDecompress.cs | 770 ++- .../Compressors/ZStandard/Unsafe/Mem.cs | 10 +- .../ZStandard/Unsafe/RSyncState_t.cs | 2 +- .../Compressors/ZStandard/Unsafe/Range.cs | 3 +- .../ZStandard/Unsafe/RawSeqStore_t.cs | 15 +- .../ZStandard/Unsafe/RoundBuff_t.cs | 5 +- .../ZStandard/Unsafe/SeqCollector.cs | 2 +- .../Compressors/ZStandard/Unsafe/SeqDef_s.cs | 3 +- .../ZStandard/Unsafe/SeqStore_t.cs | 6 +- .../ZStandard/Unsafe/SerialState.cs | 5 +- .../ZStandard/Unsafe/SymbolEncodingType_e.cs | 4 +- .../Compressors/ZStandard/Unsafe/SyncPoint.cs | 3 +- .../ZStandard/Unsafe/XXH32_canonical_t.cs | 2 +- .../ZStandard/Unsafe/XXH32_state_s.cs | 7 +- .../ZStandard/Unsafe/XXH64_canonical_t.cs | 2 +- .../ZStandard/Unsafe/XXH64_state_s.cs | 7 +- .../ZStandard/Unsafe/XXH_alignment.cs | 5 +- .../ZStandard/Unsafe/XXH_errorcode.cs | 5 +- .../Compressors/ZStandard/Unsafe/Xxhash.cs | 98 +- .../ZStandard/Unsafe/ZDICT_cover_params_t.cs | 8 +- .../Unsafe/ZDICT_fastCover_params_t.cs | 10 +- .../ZStandard/Unsafe/ZDICT_legacy_params_t.cs | 2 +- .../ZStandard/Unsafe/ZDICT_params_t.cs | 4 +- .../ZStandard/Unsafe/ZSTDMT_CCtxPool.cs | 2 +- .../ZStandard/Unsafe/ZSTDMT_CCtx_s.cs | 3 +- .../ZStandard/Unsafe/ZSTDMT_bufferPool_s.cs | 2 +- .../ZStandard/Unsafe/ZSTDMT_jobDescription.cs | 20 +- .../Unsafe/ZSTD_BlockCompressor_f.cs | 10 +- .../ZStandard/Unsafe/ZSTD_BuildCTableWksp.cs | 2 +- .../ZStandard/Unsafe/ZSTD_BuildSeqStore_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_CCtx_params_s.cs | 19 +- .../ZStandard/Unsafe/ZSTD_CCtx_s.cs | 20 +- .../ZStandard/Unsafe/ZSTD_CDict_s.cs | 6 +- .../ZStandard/Unsafe/ZSTD_CParamMode_e.cs | 7 +- .../ZStandard/Unsafe/ZSTD_DCtx_s.cs | 18 +- .../ZStandard/Unsafe/ZSTD_DDictHashSet.cs | 2 +- .../ZStandard/Unsafe/ZSTD_DDict_s.cs | 2 +- .../ZStandard/Unsafe/ZSTD_DefaultPolicy_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_EndDirective.cs | 6 +- .../ZStandard/Unsafe/ZSTD_ErrorCode.cs | 6 +- .../ZStandard/Unsafe/ZSTD_MatchState_t.cs | 15 +- .../ZStandard/Unsafe/ZSTD_OffsetInfo.cs | 2 +- .../ZStandard/Unsafe/ZSTD_OptPrice_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_ResetDirective.cs | 4 +- .../ZStandard/Unsafe/ZSTD_Sequence.cs | 5 +- .../ZStandard/Unsafe/ZSTD_SequenceLength.cs | 2 +- .../ZStandard/Unsafe/ZSTD_SequencePosition.cs | 4 +- .../ZStandard/Unsafe/ZSTD_blockSplitCtx.cs | 2 +- .../ZStandard/Unsafe/ZSTD_blockState_t.cs | 2 +- .../ZStandard/Unsafe/ZSTD_bounds.cs | 2 +- .../ZStandard/Unsafe/ZSTD_bufferMode_e.cs | 5 +- .../Unsafe/ZSTD_buffered_policy_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_cParameter.cs | 25 +- .../ZStandard/Unsafe/ZSTD_cStreamStage.cs | 4 +- .../Unsafe/ZSTD_compResetPolicy_e.cs | 4 +- .../Unsafe/ZSTD_compressedBlockState_t.cs | 2 +- .../Unsafe/ZSTD_compressionParameters.cs | 19 +- .../Unsafe/ZSTD_compressionStage_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_customMem.cs | 3 +- .../ZStandard/Unsafe/ZSTD_cwksp.cs | 2 +- .../Unsafe/ZSTD_cwksp_alloc_phase_e.cs | 4 +- .../Unsafe/ZSTD_cwksp_static_alloc_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_dParameter.cs | 5 +- .../ZStandard/Unsafe/ZSTD_dStage.cs | 4 +- .../ZStandard/Unsafe/ZSTD_dStreamStage.cs | 4 +- .../ZStandard/Unsafe/ZSTD_dictAttachPref_e.cs | 7 +- .../Unsafe/ZSTD_dictContentType_e.cs | 6 +- .../ZStandard/Unsafe/ZSTD_dictLoadMethod_e.cs | 5 +- .../ZStandard/Unsafe/ZSTD_dictMode_e.cs | 4 +- .../Unsafe/ZSTD_dictTableLoadMethod_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_dictUses_e.cs | 6 +- .../Unsafe/ZSTD_entropyCTablesMetadata_t.cs | 2 +- .../ZStandard/Unsafe/ZSTD_entropyCTables_t.cs | 2 +- .../ZStandard/Unsafe/ZSTD_entropyDTables_t.cs | 6 +- .../Unsafe/ZSTD_forceIgnoreChecksum_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_format_e.cs | 5 +- .../ZStandard/Unsafe/ZSTD_frameHeader.cs | 5 +- .../ZStandard/Unsafe/ZSTD_frameParameters.cs | 4 +- .../ZStandard/Unsafe/ZSTD_frameProgression.cs | 7 +- .../ZStandard/Unsafe/ZSTD_frameSizeInfo.cs | 2 +- .../ZStandard/Unsafe/ZSTD_frameType_e.cs | 4 +- .../Unsafe/ZSTD_fseCTablesMetadata_t.cs | 3 +- .../ZStandard/Unsafe/ZSTD_fseCTables_t.cs | 2 +- .../ZStandard/Unsafe/ZSTD_fseState.cs | 2 +- .../ZStandard/Unsafe/ZSTD_getAllMatchesFn.cs | 13 +- .../Unsafe/ZSTD_hufCTablesMetadata_t.cs | 2 +- .../ZStandard/Unsafe/ZSTD_hufCTables_t.cs | 3 +- .../ZStandard/Unsafe/ZSTD_inBuffer_s.cs | 4 +- .../Unsafe/ZSTD_indexResetPolicy_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_litLocation_e.cs | 6 +- .../Unsafe/ZSTD_literalCompressionMode_e.cs | 6 +- .../ZStandard/Unsafe/ZSTD_localDict.cs | 2 +- .../ZStandard/Unsafe/ZSTD_longLengthType_e.cs | 6 +- .../ZStandard/Unsafe/ZSTD_longOffset_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_match_t.cs | 3 +- .../ZStandard/Unsafe/ZSTD_nextInputType_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_optLdm_t.cs | 5 +- .../ZStandard/Unsafe/ZSTD_optimal_t.cs | 6 +- .../ZStandard/Unsafe/ZSTD_outBuffer_s.cs | 4 +- .../ZStandard/Unsafe/ZSTD_overlap_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_paramSwitch_e.cs | 6 +- .../ZStandard/Unsafe/ZSTD_parameters.cs | 2 +- .../ZStandard/Unsafe/ZSTD_prefixDict_s.cs | 2 +- .../Unsafe/ZSTD_refMultipleDDicts_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_resetTarget_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_seqSymbol.cs | 3 +- .../ZStandard/Unsafe/ZSTD_seqSymbol_header.cs | 2 +- .../ZStandard/Unsafe/ZSTD_sequenceFormat_e.cs | 5 +- .../ZStandard/Unsafe/ZSTD_strategy.cs | 4 +- .../Unsafe/ZSTD_symbolEncodingTypeStats_t.cs | 3 +- .../Unsafe/ZSTD_tableFillPurpose_e.cs | 4 +- .../ZStandard/Unsafe/ZSTD_window_t.cs | 7 +- .../Compressors/ZStandard/Unsafe/Zdict.cs | 226 +- .../Compressors/ZStandard/Unsafe/Zstd.cs | 8 +- .../ZStandard/Unsafe/ZstdCommon.cs | 2 +- .../ZStandard/Unsafe/ZstdCompress.cs | 4965 ++++++++++++++--- .../ZStandard/Unsafe/ZstdCompressInternal.cs | 838 ++- .../ZStandard/Unsafe/ZstdCompressLiterals.cs | 155 +- .../ZStandard/Unsafe/ZstdCompressSequences.cs | 1236 ++-- .../Unsafe/ZstdCompressSuperblock.cs | 532 +- .../Compressors/ZStandard/Unsafe/ZstdCwksp.cs | 111 +- .../Compressors/ZStandard/Unsafe/ZstdDdict.cs | 108 +- .../ZStandard/Unsafe/ZstdDecompress.cs | 1230 +++- .../ZStandard/Unsafe/ZstdDecompressBlock.cs | 1855 ++++-- .../Unsafe/ZstdDecompressInternal.cs | 542 +- .../ZStandard/Unsafe/ZstdDoubleFast.cs | 506 +- .../Compressors/ZStandard/Unsafe/ZstdFast.cs | 453 +- .../ZStandard/Unsafe/ZstdInternal.cs | 752 ++- .../Compressors/ZStandard/Unsafe/ZstdLazy.cs | 3074 ++++++++-- .../Compressors/ZStandard/Unsafe/ZstdLdm.cs | 287 +- .../ZStandard/Unsafe/ZstdLdmGeartab.cs | 791 ++- .../Compressors/ZStandard/Unsafe/ZstdOpt.cs | 1332 ++++- .../ZStandard/Unsafe/ZstdPresplit.cs | 103 +- .../ZStandard/Unsafe/ZstdmtCompress.cs | 490 +- .../ZStandard/Unsafe/_wksps_e__Union.cs | 4 +- .../ZStandard/Unsafe/algo_time_t.cs | 3 +- .../ZStandard/Unsafe/base_directive_e.cs | 4 +- .../ZStandard/Unsafe/blockProperties_t.cs | 2 +- .../ZStandard/Unsafe/blockType_e.cs | 4 +- .../Compressors/ZStandard/Unsafe/buffer_s.cs | 3 +- .../Compressors/ZStandard/Unsafe/dictItem.cs | 2 +- .../Compressors/ZStandard/Unsafe/inBuff_t.cs | 2 +- .../ZStandard/Unsafe/ldmEntry_t.cs | 2 +- .../ZStandard/Unsafe/ldmMatchCandidate_t.cs | 2 +- .../ZStandard/Unsafe/ldmParams_t.cs | 7 +- .../ZStandard/Unsafe/ldmRollingHashState_t.cs | 2 +- .../ZStandard/Unsafe/ldmState_t.cs | 4 +- .../Compressors/ZStandard/Unsafe/nodeElt_s.cs | 2 +- .../ZStandard/Unsafe/offsetCount_t.cs | 2 +- .../ZStandard/Unsafe/optState_t.cs | 17 +- .../Compressors/ZStandard/Unsafe/rankPos.cs | 2 +- .../ZStandard/Unsafe/rankValCol_t.cs | 2 +- .../Compressors/ZStandard/Unsafe/rawSeq.cs | 4 +- .../ZStandard/Unsafe/repcodes_s.cs | 2 +- .../ZStandard/Unsafe/searchMethod_e.cs | 4 +- .../ZStandard/Unsafe/seqState_t.cs | 3 +- .../ZStandard/Unsafe/seqStoreSplits.cs | 3 +- .../Compressors/ZStandard/Unsafe/seq_t.cs | 2 +- .../ZStandard/Unsafe/sortedSymbol_t.cs | 2 +- .../ZStandard/Unsafe/streaming_operation.cs | 4 +- .../Compressors/ZStandard/UnsafeHelper.cs | 38 +- .../Compressors/ZStandard/ZstdException.cs | 6 +- 228 files changed, 20345 insertions(+), 4893 deletions(-) diff --git a/src/SharpCompress/Compressors/ZStandard/BitOperations.cs b/src/SharpCompress/Compressors/ZStandard/BitOperations.cs index d0563430..d35133b2 100644 --- a/src/SharpCompress/Compressors/ZStandard/BitOperations.cs +++ b/src/SharpCompress/Compressors/ZStandard/BitOperations.cs @@ -19,22 +19,82 @@ namespace System.Numerics 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 - }); + 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 - }); + 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. @@ -69,7 +129,8 @@ namespace System.Numerics // 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)]; + (int)((value * 0x07C4ACDDu) >> 27) + ]; } /// @@ -98,8 +159,7 @@ namespace System.Numerics /// /// The value. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int TrailingZeroCount(int value) - => TrailingZeroCount((uint)value); + public static int TrailingZeroCount(int value) => TrailingZeroCount((uint)value); /// /// Count the number of trailing zero bits in an integer value. @@ -118,7 +178,8 @@ namespace System.Numerics // 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 + (int)(((value & (uint)-(int)value) * 0x077CB531u) >> 27) + ]; // Multi-cast mitigates redundant conv.u8 } /// @@ -127,8 +188,7 @@ namespace System.Numerics /// /// The value. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int TrailingZeroCount(long value) - => TrailingZeroCount((ulong)value); + public static int TrailingZeroCount(long value) => TrailingZeroCount((ulong)value); /// /// Count the number of trailing zero bits in a mask. @@ -157,8 +217,8 @@ namespace System.Numerics /// 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)); + public static uint RotateLeft(uint value, int offset) => + (value << offset) | (value >> (32 - offset)); /// /// Rotates the specified value left by the specified number of bits. @@ -169,8 +229,8 @@ namespace System.Numerics /// 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)); + public static ulong RotateLeft(ulong value, int offset) => + (value << offset) | (value >> (64 - offset)); /// /// Rotates the specified value right by the specified number of bits. @@ -181,8 +241,8 @@ namespace System.Numerics /// 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)); + public static uint RotateRight(uint value, int offset) => + (value >> offset) | (value << (32 - offset)); /// /// Rotates the specified value right by the specified number of bits. @@ -193,8 +253,8 @@ namespace System.Numerics /// 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)); + public static ulong RotateRight(ulong value, int offset) => + (value >> offset) | (value << (64 - offset)); /// /// Count the number of leading zero bits in a mask. @@ -221,9 +281,11 @@ namespace System.Numerics value |= value >> 16; // uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check - return 31 ^ Log2DeBruijn[ - // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here - (int)((value * 0x07C4ACDDu) >> 27)]; + return 31 + ^ Log2DeBruijn[ + // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here + (int)((value * 0x07C4ACDDu) >> 27) + ]; } /// diff --git a/src/SharpCompress/Compressors/ZStandard/CompressionStream.cs b/src/SharpCompress/Compressors/ZStandard/CompressionStream.cs index b65cc752..c8546afc 100644 --- a/src/SharpCompress/Compressors/ZStandard/CompressionStream.cs +++ b/src/SharpCompress/Compressors/ZStandard/CompressionStream.cs @@ -16,14 +16,21 @@ namespace ZstdSharp private Compressor? compressor; private ZSTD_outBuffer_s output; - public CompressionStream(Stream stream, int level = Compressor.DefaultCompressionLevel, - int bufferSize = 0, bool leaveOpen = true) - : this(stream, new Compressor(level), bufferSize, false, leaveOpen) - { - } + public CompressionStream( + Stream stream, + int level = Compressor.DefaultCompressionLevel, + int bufferSize = 0, + bool leaveOpen = true + ) + : this(stream, new Compressor(level), bufferSize, false, leaveOpen) { } - public CompressionStream(Stream stream, Compressor compressor, int bufferSize = 0, - bool preserveCompressor = true, bool leaveOpen = true) + public CompressionStream( + Stream stream, + Compressor compressor, + int bufferSize = 0, + bool preserveCompressor = true, + bool leaveOpen = true + ) { if (stream == null) throw new ArgumentNullException(nameof(stream)); @@ -40,9 +47,11 @@ namespace ZstdSharp this.leaveOpen = leaveOpen; var outputBufferSize = - bufferSize > 0 ? bufferSize : (int) Methods.ZSTD_CStreamOutSize().EnsureZstdSuccess(); + bufferSize > 0 + ? bufferSize + : (int)Methods.ZSTD_CStreamOutSize().EnsureZstdSuccess(); outputBuffer = ArrayPool.Shared.Rent(outputBufferSize); - output = new ZSTD_outBuffer_s {pos = 0, size = (nuint) outputBufferSize}; + output = new ZSTD_outBuffer_s { pos = 0, size = (nuint)outputBufferSize }; } public void SetParameter(ZSTD_cParameter parameter, int value) @@ -120,97 +129,147 @@ namespace ZstdSharp } } - public override void Flush() - => FlushInternal(ZSTD_EndDirective.ZSTD_e_flush); + public override void Flush() => FlushInternal(ZSTD_EndDirective.ZSTD_e_flush); - public override async Task FlushAsync(CancellationToken cancellationToken) - => await FlushInternalAsync(ZSTD_EndDirective.ZSTD_e_flush, cancellationToken).ConfigureAwait(false); + public override async Task FlushAsync(CancellationToken cancellationToken) => + await FlushInternalAsync(ZSTD_EndDirective.ZSTD_e_flush, cancellationToken) + .ConfigureAwait(false); private void FlushInternal(ZSTD_EndDirective directive) => WriteInternal(null, directive); - private async Task FlushInternalAsync(ZSTD_EndDirective directive, - CancellationToken cancellationToken = default) => - await WriteInternalAsync(null, directive, cancellationToken).ConfigureAwait(false); + private async Task FlushInternalAsync( + ZSTD_EndDirective directive, + CancellationToken cancellationToken = default + ) => await WriteInternalAsync(null, directive, cancellationToken).ConfigureAwait(false); - public override void Write(byte[] buffer, int offset, int count) - => Write(new ReadOnlySpan(buffer, offset, count)); + public override void Write(byte[] buffer, int offset, int count) => + Write(new ReadOnlySpan(buffer, offset, count)); #if !NETSTANDARD2_0 && !NETFRAMEWORK - public override void Write(ReadOnlySpan buffer) - => WriteInternal(buffer, ZSTD_EndDirective.ZSTD_e_continue); + public override void Write(ReadOnlySpan buffer) => + WriteInternal(buffer, ZSTD_EndDirective.ZSTD_e_continue); #else - public void Write(ReadOnlySpan buffer) - => WriteInternal(buffer, ZSTD_EndDirective.ZSTD_e_continue); + public void Write(ReadOnlySpan buffer) => + WriteInternal(buffer, ZSTD_EndDirective.ZSTD_e_continue); #endif private void WriteInternal(ReadOnlySpan buffer, ZSTD_EndDirective directive) { EnsureNotDisposed(); - var input = new ZSTD_inBuffer_s {pos = 0, size = buffer != null ? (nuint) buffer.Length : 0}; + 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, directive); - var written = (int) output.pos; + var written = (int)output.pos; if (written > 0) innerStream.Write(outputBuffer, 0, written); - } while (directive == ZSTD_EndDirective.ZSTD_e_continue ? input.pos < input.size : remaining > 0); + } while ( + directive == ZSTD_EndDirective.ZSTD_e_continue + ? input.pos < input.size + : remaining > 0 + ); } #if !NETSTANDARD2_0 && !NETFRAMEWORK - private async ValueTask WriteInternalAsync(ReadOnlyMemory? buffer, ZSTD_EndDirective directive, - CancellationToken cancellationToken = default) + private async ValueTask WriteInternalAsync( + ReadOnlyMemory? buffer, + ZSTD_EndDirective directive, + CancellationToken cancellationToken = default + ) #else - private async Task WriteInternalAsync(ReadOnlyMemory? buffer, ZSTD_EndDirective directive, - CancellationToken cancellationToken = default) + private async Task WriteInternalAsync( + ReadOnlyMemory? buffer, + ZSTD_EndDirective directive, + CancellationToken cancellationToken = default + ) #endif { EnsureNotDisposed(); - var input = new ZSTD_inBuffer_s { pos = 0, size = buffer.HasValue ? (nuint)buffer.Value.Length : 0 }; + 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, directive); + remaining = CompressStream( + ref input, + buffer.HasValue ? buffer.Value.Span : null, + directive + ); - var written = (int) output.pos; + var written = (int)output.pos; if (written > 0) - await innerStream.WriteAsync(outputBuffer, 0, written, cancellationToken).ConfigureAwait(false); - } while (directive == ZSTD_EndDirective.ZSTD_e_continue ? input.pos < input.size : remaining > 0); + await innerStream + .WriteAsync(outputBuffer, 0, written, cancellationToken) + .ConfigureAwait(false); + } while ( + directive == ZSTD_EndDirective.ZSTD_e_continue + ? input.pos < input.size + : remaining > 0 + ); } #if !NETSTANDARD2_0 && !NETFRAMEWORK - public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - => WriteAsync(new ReadOnlyMemory(buffer, offset, count), cancellationToken).AsTask(); + public override Task WriteAsync( + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken + ) => + WriteAsync(new ReadOnlyMemory(buffer, offset, count), cancellationToken).AsTask(); - public override async ValueTask WriteAsync(ReadOnlyMemory buffer, - CancellationToken cancellationToken = default) - => await WriteInternalAsync(buffer, ZSTD_EndDirective.ZSTD_e_continue, cancellationToken).ConfigureAwait(false); + public override async ValueTask WriteAsync( + ReadOnlyMemory buffer, + CancellationToken cancellationToken = default + ) => + await WriteInternalAsync(buffer, ZSTD_EndDirective.ZSTD_e_continue, cancellationToken) + .ConfigureAwait(false); #else - public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - => WriteAsync(new ReadOnlyMemory(buffer, offset, count), cancellationToken); + public override Task WriteAsync( + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken + ) => WriteAsync(new ReadOnlyMemory(buffer, offset, count), cancellationToken); - public async Task WriteAsync(ReadOnlyMemory buffer, - CancellationToken cancellationToken = default) - => await WriteInternalAsync(buffer, ZSTD_EndDirective.ZSTD_e_continue, cancellationToken).ConfigureAwait(false); + public async Task WriteAsync( + ReadOnlyMemory buffer, + CancellationToken cancellationToken = default + ) => + await WriteInternalAsync(buffer, ZSTD_EndDirective.ZSTD_e_continue, cancellationToken) + .ConfigureAwait(false); #endif - internal unsafe nuint CompressStream(ref ZSTD_inBuffer_s input, ReadOnlySpan inputBuffer, - ZSTD_EndDirective directive) + 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.NotNull().CompressStream(ref input, ref output, directive).EnsureZstdSuccess(); + return compressor + .NotNull() + .CompressStream(ref input, ref output, directive) + .EnsureZstdSuccess(); } } @@ -226,9 +285,13 @@ namespace ZstdSharp set => throw new NotSupportedException(); } - public override long Seek(long offset, SeekOrigin origin) => 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(); + + public override int Read(byte[] buffer, int offset, int count) => + throw new NotSupportedException(); private void EnsureNotDisposed() { diff --git a/src/SharpCompress/Compressors/ZStandard/Compressor.cs b/src/SharpCompress/Compressors/ZStandard/Compressor.cs index 71cd6c39..470cd563 100644 --- a/src/SharpCompress/Compressors/ZStandard/Compressor.cs +++ b/src/SharpCompress/Compressors/ZStandard/Compressor.cs @@ -62,7 +62,9 @@ namespace ZstdSharp { using var cctx = handle.Acquire(); fixed (byte* dictPtr = dict) - Methods.ZSTD_CCtx_loadDictionary(cctx, dictPtr, (nuint)dict.Length).EnsureZstdSuccess(); + Methods + .ZSTD_CCtx_loadDictionary(cctx, dictPtr, (nuint)dict.Length) + .EnsureZstdSuccess(); } public Compressor(int level = DefaultCompressionLevel) @@ -71,11 +73,11 @@ namespace ZstdSharp Level = level; } - public static int GetCompressBound(int length) - => (int)Methods.ZSTD_compressBound((nuint)length); + public static int GetCompressBound(int length) => + (int)Methods.ZSTD_compressBound((nuint)length); - public static ulong GetCompressBoundLong(ulong length) - => Methods.ZSTD_compressBound((nuint)length); + public static ulong GetCompressBoundLong(ulong length) => + Methods.ZSTD_compressBound((nuint)length); public Span Wrap(ReadOnlySpan src) { @@ -84,8 +86,8 @@ namespace ZstdSharp 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(byte[] src, byte[] dest, int offset) => + Wrap(src, new Span(dest, offset, dest.Length - offset)); public int Wrap(ReadOnlySpan src, Span dest) { @@ -93,19 +95,37 @@ namespace ZstdSharp fixed (byte* destPtr = dest) { using var cctx = handle.Acquire(); - return (int)Methods.ZSTD_compress2(cctx, destPtr, (nuint)dest.Length, srcPtr, (nuint)src.Length) - .EnsureZstdSuccess(); + 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(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 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(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) { @@ -115,8 +135,13 @@ namespace ZstdSharp nuint returnValue; using (var cctx = handle.Acquire()) { - returnValue = - Methods.ZSTD_compress2(cctx, destPtr, (nuint)dest.Length, srcPtr, (nuint)src.Length); + returnValue = Methods.ZSTD_compress2( + cctx, + destPtr, + (nuint)dest.Length, + srcPtr, + (nuint)src.Length + ); } if (returnValue == unchecked(0 - (nuint)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)) @@ -131,11 +156,23 @@ namespace ZstdSharp } } - public bool TryWrap(ArraySegment src, ArraySegment dest, out int written) - => TryWrap((ReadOnlySpan)src, dest, out written); + 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); + 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 + ); public void Dispose() { @@ -143,13 +180,19 @@ namespace ZstdSharp GC.SuppressFinalize(this); } - internal nuint CompressStream(ref ZSTD_inBuffer_s input, ref ZSTD_outBuffer_s output, ZSTD_EndDirective directive) + 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) { using var cctx = handle.Acquire(); - return Methods.ZSTD_compressStream2(cctx, outputPtr, inputPtr, directive).EnsureZstdSuccess(); + return Methods + .ZSTD_compressStream2(cctx, outputPtr, inputPtr, directive) + .EnsureZstdSuccess(); } } diff --git a/src/SharpCompress/Compressors/ZStandard/Constants.cs b/src/SharpCompress/Compressors/ZStandard/Constants.cs index c7af1231..844946f7 100644 --- a/src/SharpCompress/Compressors/ZStandard/Constants.cs +++ b/src/SharpCompress/Compressors/ZStandard/Constants.cs @@ -1,9 +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; - } + 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/ZStandard/DecompressionStream.cs b/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs index 3f15227f..69c44935 100644 --- a/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs +++ b/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs @@ -20,13 +20,22 @@ namespace ZstdSharp private nuint lastDecompressResult = 0; private bool contextDrained = true; - public DecompressionStream(Stream stream, int bufferSize = 0, bool checkEndOfStream = true, bool leaveOpen = true) - : this(stream, new Decompressor(), bufferSize, checkEndOfStream, false, leaveOpen) - { - } + public DecompressionStream( + Stream stream, + int bufferSize = 0, + bool checkEndOfStream = true, + bool leaveOpen = true + ) + : this(stream, new Decompressor(), bufferSize, checkEndOfStream, false, leaveOpen) { } - public DecompressionStream(Stream stream, Decompressor decompressor, int bufferSize = 0, - bool checkEndOfStream = true, bool preserveDecompressor = true, bool leaveOpen = true) + public DecompressionStream( + Stream stream, + Decompressor decompressor, + int bufferSize = 0, + bool checkEndOfStream = true, + bool preserveDecompressor = true, + bool leaveOpen = true + ) { if (stream == null) throw new ArgumentNullException(nameof(stream)); @@ -43,9 +52,14 @@ namespace ZstdSharp this.leaveOpen = leaveOpen; this.checkEndOfStream = checkEndOfStream; - inputBufferSize = bufferSize > 0 ? bufferSize : (int) Methods.ZSTD_DStreamInSize().EnsureZstdSuccess(); + inputBufferSize = + bufferSize > 0 ? bufferSize : (int)Methods.ZSTD_DStreamInSize().EnsureZstdSuccess(); inputBuffer = ArrayPool.Shared.Rent(inputBufferSize); - input = new ZSTD_inBuffer_s {pos = (nuint) inputBufferSize, size = (nuint) inputBufferSize}; + input = new ZSTD_inBuffer_s + { + pos = (nuint)inputBufferSize, + size = (nuint)inputBufferSize, + }; } public void SetParameter(ZSTD_dParameter parameter, int value) @@ -90,8 +104,8 @@ namespace ZstdSharp } } - public override int Read(byte[] buffer, int offset, int count) - => Read(new Span(buffer, offset, count)); + 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) @@ -107,7 +121,7 @@ namespace ZstdSharp return 0; } - var output = new ZSTD_outBuffer_s {pos = 0, size = (nuint) buffer.Length}; + var output = new ZSTD_outBuffer_s { pos = 0, size = (nuint)buffer.Length }; while (true) { // If there is still input available, or there might be data buffered in the decompressor context, flush that out @@ -125,7 +139,7 @@ namespace ZstdSharp // If we have data to return, return it immediately, so we won't stall on Read if (output.pos > 0) { - return (int) output.pos; + return (int)output.pos; } } @@ -141,24 +155,36 @@ namespace ZstdSharp return 0; } - input.size = (nuint) bytesRead; + input.size = (nuint)bytesRead; input.pos = 0; } } - #if !NETSTANDARD2_0 && !NETFRAMEWORK - public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - => ReadAsync(new Memory(buffer, offset, count), cancellationToken).AsTask(); + public override Task ReadAsync( + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken + ) => ReadAsync(new Memory(buffer, offset, count), cancellationToken).AsTask(); - public override async ValueTask ReadAsync(Memory buffer, - CancellationToken cancellationToken = default) + public override async ValueTask ReadAsync( + Memory buffer, + CancellationToken cancellationToken = default + ) #else - public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - => ReadAsync(new Memory(buffer, offset, count), cancellationToken); - public async Task ReadAsync(Memory buffer, - CancellationToken cancellationToken = default) + public override Task ReadAsync( + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken + ) => ReadAsync(new Memory(buffer, offset, count), cancellationToken); + + public async Task ReadAsync( + Memory buffer, + CancellationToken cancellationToken = default + ) #endif { EnsureNotDisposed(); @@ -169,7 +195,7 @@ namespace ZstdSharp return 0; } - var output = new ZSTD_outBuffer_s { pos = 0, size = (nuint)buffer.Length}; + var output = new ZSTD_outBuffer_s { pos = 0, size = (nuint)buffer.Length }; while (true) { // If there is still input available, or there might be data buffered in the decompressor context, flush that out @@ -193,8 +219,13 @@ namespace ZstdSharp // Otherwise, read some more input int bytesRead; - if ((bytesRead = await innerStream.ReadAsync(inputBuffer, 0, inputBufferSize, cancellationToken) - .ConfigureAwait(false)) == 0) + if ( + ( + bytesRead = await innerStream + .ReadAsync(inputBuffer, 0, inputBufferSize, cancellationToken) + .ConfigureAwait(false) + ) == 0 + ) { if (checkEndOfStream && lastDecompressResult != 0) { @@ -204,7 +235,7 @@ namespace ZstdSharp return 0; } - input.size = (nuint) bytesRead; + input.size = (nuint)bytesRead; input.pos = 0; } } @@ -234,9 +265,13 @@ namespace ZstdSharp public override void Flush() => throw new NotSupportedException(); - public override long Seek(long offset, SeekOrigin origin) => 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(); + + public override void Write(byte[] buffer, int offset, int count) => + throw new NotSupportedException(); private void EnsureNotDisposed() { diff --git a/src/SharpCompress/Compressors/ZStandard/Decompressor.cs b/src/SharpCompress/Compressors/ZStandard/Decompressor.cs index 85b99e77..2e388bb2 100644 --- a/src/SharpCompress/Compressors/ZStandard/Decompressor.cs +++ b/src/SharpCompress/Compressors/ZStandard/Decompressor.cs @@ -36,38 +36,46 @@ namespace ZstdSharp { using var dctx = handle.Acquire(); fixed (byte* dictPtr = dict) - Methods.ZSTD_DCtx_loadDictionary(dctx, dictPtr, (nuint)dict.Length).EnsureZstdSuccess(); + 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(); + return Methods + .ZSTD_decompressBound(srcPtr, (nuint)src.Length) + .EnsureContentSizeOk(); } - public static ulong GetDecompressedSize(ArraySegment src) - => GetDecompressedSize((ReadOnlySpan)src); + 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 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}"); + 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}"); + 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(byte[] src, byte[] dest, int offset) => + Unwrap(src, new Span(dest, offset, dest.Length - offset)); public int Unwrap(ReadOnlySpan src, Span dest) { @@ -75,17 +83,34 @@ namespace ZstdSharp fixed (byte* destPtr = dest) { using var dctx = handle.Acquire(); - return (int)Methods - .ZSTD_decompressDCtx(dctx, destPtr, (nuint)dest.Length, srcPtr, (nuint)src.Length) - .EnsureZstdSuccess(); + 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 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(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) { @@ -95,8 +120,13 @@ namespace ZstdSharp nuint returnValue; using (var dctx = handle.Acquire()) { - returnValue = - Methods.ZSTD_decompressDCtx(dctx, destPtr, (nuint)dest.Length, srcPtr, (nuint)src.Length); + returnValue = Methods.ZSTD_decompressDCtx( + dctx, + destPtr, + (nuint)dest.Length, + srcPtr, + (nuint)src.Length + ); } if (returnValue == unchecked(0 - (nuint)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)) @@ -111,8 +141,20 @@ namespace ZstdSharp } } - 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); + 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 + ); public void Dispose() { diff --git a/src/SharpCompress/Compressors/ZStandard/JobThreadPool.cs b/src/SharpCompress/Compressors/ZStandard/JobThreadPool.cs index a51c3679..c9bd20f7 100644 --- a/src/SharpCompress/Compressors/ZStandard/JobThreadPool.cs +++ b/src/SharpCompress/Compressors/ZStandard/JobThreadPool.cs @@ -57,12 +57,8 @@ namespace ZstdSharp if (queue.TryTake(out var job, -1, cancellationToken)) ((delegate* managed)job.function)(job.opaque); } - catch (InvalidOperationException) - { - } - catch (OperationCanceledException) - { - } + catch (InvalidOperationException) { } + catch (OperationCanceledException) { } } } diff --git a/src/SharpCompress/Compressors/ZStandard/Pool.cs b/src/SharpCompress/Compressors/ZStandard/Pool.cs index 41c0ab57..0d681cbc 100644 --- a/src/SharpCompress/Compressors/ZStandard/Pool.cs +++ b/src/SharpCompress/Compressors/ZStandard/Pool.cs @@ -4,7 +4,8 @@ namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { - private static JobThreadPool GetThreadPool(void* ctx) => UnmanagedObject.Unwrap(ctx); + private static JobThreadPool GetThreadPool(void* ctx) => + UnmanagedObject.Unwrap(ctx); /* ZSTD_createThreadPool() : public access point */ public static void* ZSTD_createThreadPool(nuint numThreads) @@ -23,7 +24,11 @@ namespace ZstdSharp.Unsafe return POOL_create_advanced(numThreads, queueSize, ZSTD_defaultCMem); } - private static void* POOL_create_advanced(nuint numThreads, nuint queueSize, ZSTD_customMem customMem) + private static void* POOL_create_advanced( + nuint numThreads, + nuint queueSize, + ZSTD_customMem customMem + ) { var jobThreadPool = new JobThreadPool((int)numThreads, (int)queueSize); return UnmanagedObject.Wrap(jobThreadPool); @@ -114,4 +119,4 @@ namespace ZstdSharp.Unsafe return jobThreadPool.TryAdd(function, opaque) ? 1 : 0; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/SafeHandles.cs b/src/SharpCompress/Compressors/ZStandard/SafeHandles.cs index b470a434..7d46add3 100644 --- a/src/SharpCompress/Compressors/ZStandard/SafeHandles.cs +++ b/src/SharpCompress/Compressors/ZStandard/SafeHandles.cs @@ -20,9 +20,8 @@ namespace ZstdSharp /// Parameterless constructor is hidden. Use the static Create factory /// method to create a new safe handle instance. /// - protected SafeZstdHandle() : base(IntPtr.Zero, true) - { - } + protected SafeZstdHandle() + : base(IntPtr.Zero, true) { } public sealed override bool IsInvalid => handle == IntPtr.Zero; } @@ -33,9 +32,7 @@ namespace ZstdSharp internal sealed unsafe class SafeCctxHandle : SafeZstdHandle { /// - private SafeCctxHandle() - { - } + private SafeCctxHandle() { } /// /// Creates a new instance of . @@ -50,7 +47,10 @@ namespace ZstdSharp { var cctx = Methods.ZSTD_createCCtx(); if (cctx == null) - throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Failed to create cctx"); + throw new ZstdException( + ZSTD_ErrorCode.ZSTD_error_GENERIC, + "Failed to create cctx" + ); safeHandle.SetHandle((IntPtr)cctx); success = true; } @@ -85,9 +85,7 @@ namespace ZstdSharp internal sealed unsafe class SafeDctxHandle : SafeZstdHandle { /// - private SafeDctxHandle() - { - } + private SafeDctxHandle() { } /// /// Creates a new instance of . @@ -102,7 +100,10 @@ namespace ZstdSharp { var dctx = Methods.ZSTD_createDCtx(); if (dctx == null) - throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Failed to create dctx"); + throw new ZstdException( + ZSTD_ErrorCode.ZSTD_error_GENERIC, + "Failed to create dctx" + ); safeHandle.SetHandle((IntPtr)dctx); success = true; } @@ -140,7 +141,8 @@ namespace ZstdSharp /// Safe handle holders can be d to decrement the safe handle's /// reference count, and can be implicitly converted to pointers to . /// - internal unsafe ref struct SafeHandleHolder where T : unmanaged + internal unsafe ref struct SafeHandleHolder + where T : unmanaged { private readonly SafeHandle _handle; diff --git a/src/SharpCompress/Compressors/ZStandard/ThrowHelper.cs b/src/SharpCompress/Compressors/ZStandard/ThrowHelper.cs index 380e6aca..bf0ac0cb 100644 --- a/src/SharpCompress/Compressors/ZStandard/ThrowHelper.cs +++ b/src/SharpCompress/Compressors/ZStandard/ThrowHelper.cs @@ -26,10 +26,16 @@ namespace ZstdSharp 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"); + 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)"); + throw new ZstdException( + ZSTD_ErrorCode.ZSTD_error_GENERIC, + "Decompressed content size cannot be determined (e.g. invalid magic number, srcSize too small)" + ); return returnValue; } @@ -37,7 +43,7 @@ namespace ZstdSharp private static void ThrowException(nuint returnValue, string message) { var code = 0 - returnValue; - throw new ZstdException((ZSTD_ErrorCode) code, message); + throw new ZstdException((ZSTD_ErrorCode)code, message); } } } diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Allocations.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Allocations.cs index ea59312f..46e132e1 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Allocations.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Allocations.cs @@ -10,7 +10,10 @@ namespace ZstdSharp.Unsafe private static void* ZSTD_customMalloc(nuint size, ZSTD_customMem customMem) { if (customMem.customAlloc != null) - return ((delegate* managed)customMem.customAlloc)(customMem.opaque, size); + return ((delegate* managed)customMem.customAlloc)( + customMem.opaque, + size + ); return malloc(size); } @@ -21,7 +24,10 @@ namespace ZstdSharp.Unsafe { /* calloc implemented as malloc+memset; * not as efficient as calloc, but next best guess for custom malloc */ - void* ptr = ((delegate* managed)customMem.customAlloc)(customMem.opaque, size); + void* ptr = ((delegate* managed)customMem.customAlloc)( + customMem.opaque, + size + ); memset(ptr, 0, (uint)size); return ptr; } @@ -35,10 +41,13 @@ namespace ZstdSharp.Unsafe if (ptr != null) { if (customMem.customFree != null) - ((delegate* managed)customMem.customFree)(customMem.opaque, ptr); + ((delegate* managed)customMem.customFree)( + customMem.opaque, + ptr + ); else free(ptr); } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_CStream_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_CStream_t.cs index ee8d40ba..b3be314f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_CStream_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_CStream_t.cs @@ -12,4 +12,4 @@ namespace ZstdSharp.Unsafe public sbyte* ptr; public sbyte* endPtr; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_DStream_status.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_DStream_status.cs index 3fbf5713..aa2966ca 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_DStream_status.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_DStream_status.cs @@ -4,11 +4,14 @@ namespace ZstdSharp.Unsafe { /* fully refilled */ BIT_DStream_unfinished = 0, + /* still some bits left in bitstream */ BIT_DStream_endOfBuffer = 1, + /* bitstream entirely consumed, bit-exact */ BIT_DStream_completed = 2, + /* user requested more bits than present in bitstream */ - BIT_DStream_overflow = 3 + BIT_DStream_overflow = 3, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_DStream_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_DStream_t.cs index f47bc40b..76cc2361 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_DStream_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/BIT_DStream_t.cs @@ -11,4 +11,4 @@ namespace ZstdSharp.Unsafe public sbyte* start; public sbyte* limitPtr; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Bits.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Bits.cs index 94b6010c..14fdc46e 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Bits.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Bits.cs @@ -1,7 +1,7 @@ -using System.Runtime.CompilerServices; -using static ZstdSharp.UnsafeHelper; using System; using System.Numerics; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -41,10 +41,14 @@ namespace ZstdSharp.Unsafe assert(val != 0); if (BitConverter.IsLittleEndian) { - return MEM_64bits ? (uint)BitOperations.TrailingZeroCount(val) >> 3 : (uint)BitOperations.TrailingZeroCount((uint)val) >> 3; + return MEM_64bits + ? (uint)BitOperations.TrailingZeroCount(val) >> 3 + : (uint)BitOperations.TrailingZeroCount((uint)val) >> 3; } - return MEM_64bits ? (uint)BitOperations.LeadingZeroCount(val) >> 3 : (uint)BitOperations.LeadingZeroCount((uint)val) >> 3; + return MEM_64bits + ? (uint)BitOperations.LeadingZeroCount(val) >> 3 + : (uint)BitOperations.LeadingZeroCount((uint)val) >> 3; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Bitstream.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Bitstream.cs index 23f59ac3..41a5ea77 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Bitstream.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Bitstream.cs @@ -1,7 +1,7 @@ -using static ZstdSharp.UnsafeHelper; using System; -using System.Runtime.InteropServices; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; #if NETCOREAPP3_0_OR_GREATER using System.Runtime.Intrinsics.X86; #endif @@ -11,45 +11,86 @@ namespace ZstdSharp.Unsafe public static unsafe partial class Methods { #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_BIT_mask => 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 - }; - private static uint* BIT_mask => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_BIT_mask)); + private static ReadOnlySpan Span_BIT_mask => + 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, + }; + private static uint* BIT_mask => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_BIT_mask) + ); #else - private static readonly 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 }); + private static readonly 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, + } + ); #endif /*-************************************************************** * bitStream encoding @@ -59,7 +100,11 @@ namespace ZstdSharp.Unsafe * @return : 0 if success, * otherwise an error code (can be tested using ERR_isError()) */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint BIT_initCStream(ref BIT_CStream_t bitC, void* startPtr, nuint dstCapacity) + private static nuint BIT_initCStream( + ref BIT_CStream_t bitC, + void* startPtr, + nuint dstCapacity + ) { bitC.bitContainer = 0; bitC.bitPos = 0; @@ -94,7 +139,12 @@ namespace ZstdSharp.Unsafe * can add up to 31 bits into `bitC`. * Note : does not check for register overflow ! */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void BIT_addBits(ref nuint bitC_bitContainer, ref uint bitC_bitPos, nuint value, uint nbBits) + private static void BIT_addBits( + ref nuint bitC_bitContainer, + ref uint bitC_bitPos, + nuint value, + uint nbBits + ) { assert(nbBits < sizeof(uint) * 32 / sizeof(uint)); assert(nbBits + bitC_bitPos < (uint)(sizeof(nuint) * 8)); @@ -106,7 +156,12 @@ namespace ZstdSharp.Unsafe * works only if `value` is _clean_, * meaning all high bits above nbBits are 0 */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void BIT_addBitsFast(ref nuint bitC_bitContainer, ref uint bitC_bitPos, nuint value, uint nbBits) + private static void BIT_addBitsFast( + ref nuint bitC_bitContainer, + ref uint bitC_bitPos, + nuint value, + uint nbBits + ) { assert(value >> (int)nbBits == 0); assert(nbBits + bitC_bitPos < (uint)(sizeof(nuint) * 8)); @@ -118,7 +173,12 @@ namespace ZstdSharp.Unsafe * assumption : bitContainer has not overflowed * unsafe version; does not check buffer overflow */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void BIT_flushBitsFast(ref nuint bitC_bitContainer, ref uint bitC_bitPos, ref sbyte* bitC_ptr, sbyte* bitC_endPtr) + private static void BIT_flushBitsFast( + ref nuint bitC_bitContainer, + ref uint bitC_bitPos, + ref sbyte* bitC_ptr, + sbyte* bitC_endPtr + ) { nuint nbBytes = bitC_bitPos >> 3; assert(bitC_bitPos < (uint)(sizeof(nuint) * 8)); @@ -135,7 +195,12 @@ namespace ZstdSharp.Unsafe * note : does not signal buffer overflow. * overflow will be revealed later on using BIT_closeCStream() */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void BIT_flushBits(ref nuint bitC_bitContainer, ref uint bitC_bitPos, ref sbyte* bitC_ptr, sbyte* bitC_endPtr) + private static void BIT_flushBits( + ref nuint bitC_bitContainer, + ref uint bitC_bitPos, + ref sbyte* bitC_ptr, + sbyte* bitC_endPtr + ) { nuint nbBytes = bitC_bitPos >> 3; assert(bitC_bitPos < (uint)(sizeof(nuint) * 8)); @@ -152,7 +217,13 @@ namespace ZstdSharp.Unsafe * @return : size of CStream, in bytes, * or 0 if it could not fit into dstBuffer */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint BIT_closeCStream(ref nuint bitC_bitContainer, ref uint bitC_bitPos, sbyte* bitC_ptr, sbyte* bitC_endPtr, sbyte* bitC_startPtr) + private static nuint BIT_closeCStream( + ref nuint bitC_bitContainer, + ref uint bitC_bitPos, + sbyte* bitC_ptr, + sbyte* bitC_endPtr, + sbyte* bitC_startPtr + ) { BIT_addBitsFast(ref bitC_bitContainer, ref bitC_bitPos, 1, 1); BIT_flushBits(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); @@ -199,13 +270,16 @@ namespace ZstdSharp.Unsafe switch (srcSize) { case 7: - bitD->bitContainer += (nuint)((byte*)srcBuffer)[6] << sizeof(nuint) * 8 - 16; + bitD->bitContainer += + (nuint)((byte*)srcBuffer)[6] << sizeof(nuint) * 8 - 16; goto case 6; case 6: - bitD->bitContainer += (nuint)((byte*)srcBuffer)[5] << sizeof(nuint) * 8 - 24; + bitD->bitContainer += + (nuint)((byte*)srcBuffer)[5] << sizeof(nuint) * 8 - 24; goto case 5; case 5: - bitD->bitContainer += (nuint)((byte*)srcBuffer)[4] << sizeof(nuint) * 8 - 32; + bitD->bitContainer += + (nuint)((byte*)srcBuffer)[4] << sizeof(nuint) * 8 - 32; goto case 4; case 4: bitD->bitContainer += (nuint)((byte*)srcBuffer)[3] << 24; @@ -224,7 +298,9 @@ namespace ZstdSharp.Unsafe byte lastByte = ((byte*)srcBuffer)[srcSize - 1]; bitD->bitsConsumed = lastByte != 0 ? 8 - ZSTD_highbit32(lastByte) : 0; if (lastByte == 0) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } bitD->bitsConsumed += (uint)((nuint)sizeof(nuint) - srcSize) * 8; @@ -268,7 +344,11 @@ namespace ZstdSharp.Unsafe [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint BIT_lookBits(BIT_DStream_t* bitD, uint nbBits) { - return BIT_getMiddleBits(bitD->bitContainer, (uint)(sizeof(nuint) * 8) - bitD->bitsConsumed - nbBits, nbBits); + return BIT_getMiddleBits( + bitD->bitContainer, + (uint)(sizeof(nuint) * 8) - bitD->bitsConsumed - nbBits, + nbBits + ); } /*! BIT_lookBitsFast() : @@ -278,7 +358,9 @@ namespace ZstdSharp.Unsafe { uint regMask = (uint)(sizeof(nuint) * 8 - 1); assert(nbBits >= 1); - return bitD->bitContainer << (int)(bitD->bitsConsumed & regMask) >> (int)(regMask + 1 - nbBits & regMask); + return bitD->bitContainer + << (int)(bitD->bitsConsumed & regMask) + >> (int)(regMask + 1 - nbBits & regMask); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -341,21 +423,18 @@ namespace ZstdSharp.Unsafe } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_static_zeroFilled => new byte[] - { - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - }; - private static nuint* static_zeroFilled => (nuint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_static_zeroFilled)); + private static ReadOnlySpan Span_static_zeroFilled => + new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; + private static nuint* static_zeroFilled => + (nuint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_static_zeroFilled) + ); #else - private static readonly nuint* static_zeroFilled = (nuint*)GetArrayPointer(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }); + private static readonly nuint* static_zeroFilled = (nuint*)GetArrayPointer( + new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 } + ); #endif /*! BIT_reloadDStream() : * Refill `bitD` from buffer previously set in BIT_initDStream() . @@ -406,7 +485,10 @@ namespace ZstdSharp.Unsafe [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint BIT_endOfDStream(BIT_DStream_t* DStream) { - return DStream->ptr == DStream->start && DStream->bitsConsumed == (uint)(sizeof(nuint) * 8) ? 1U : 0U; + return + DStream->ptr == DStream->start && DStream->bitsConsumed == (uint)(sizeof(nuint) * 8) + ? 1U + : 0U; } /*-******************************************************** @@ -472,7 +554,9 @@ namespace ZstdSharp.Unsafe byte lastByte = ((byte*)srcBuffer)[srcSize - 1]; bitD.bitsConsumed = lastByte != 0 ? 8 - ZSTD_highbit32(lastByte) : 0; if (lastByte == 0) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } bitD.bitsConsumed += (uint)((nuint)sizeof(nuint) - srcSize) * 8; @@ -488,19 +572,33 @@ namespace ZstdSharp.Unsafe * On 64-bits, maxNbBits==56. * @return : value extracted */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint BIT_lookBits(nuint bitD_bitContainer, uint bitD_bitsConsumed, uint nbBits) + private static nuint BIT_lookBits( + nuint bitD_bitContainer, + uint bitD_bitsConsumed, + uint nbBits + ) { - return BIT_getMiddleBits(bitD_bitContainer, (uint)(sizeof(nuint) * 8) - bitD_bitsConsumed - nbBits, nbBits); + return BIT_getMiddleBits( + bitD_bitContainer, + (uint)(sizeof(nuint) * 8) - bitD_bitsConsumed - nbBits, + nbBits + ); } /*! BIT_lookBitsFast() : * unsafe version; only works if nbBits >= 1 */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint BIT_lookBitsFast(nuint bitD_bitContainer, uint bitD_bitsConsumed, uint nbBits) + private static nuint BIT_lookBitsFast( + nuint bitD_bitContainer, + uint bitD_bitsConsumed, + uint nbBits + ) { uint regMask = (uint)(sizeof(nuint) * 8 - 1); assert(nbBits >= 1); - return bitD_bitContainer << (int)(bitD_bitsConsumed & regMask) >> (int)(regMask + 1 - nbBits & regMask); + return bitD_bitContainer + << (int)(bitD_bitsConsumed & regMask) + >> (int)(regMask + 1 - nbBits & regMask); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -514,7 +612,11 @@ namespace ZstdSharp.Unsafe * Pay attention to not read more than nbBits contained into local register. * @return : extracted value. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint BIT_readBits(nuint bitD_bitContainer, ref uint bitD_bitsConsumed, uint nbBits) + private static nuint BIT_readBits( + nuint bitD_bitContainer, + ref uint bitD_bitsConsumed, + uint nbBits + ) { nuint value = BIT_lookBits(bitD_bitContainer, bitD_bitsConsumed, nbBits); BIT_skipBits(ref bitD_bitsConsumed, nbBits); @@ -524,7 +626,11 @@ namespace ZstdSharp.Unsafe /*! BIT_readBitsFast() : * unsafe version; only works if nbBits >= 1 */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint BIT_readBitsFast(nuint bitD_bitContainer, ref uint bitD_bitsConsumed, uint nbBits) + private static nuint BIT_readBitsFast( + nuint bitD_bitContainer, + ref uint bitD_bitsConsumed, + uint nbBits + ) { nuint value = BIT_lookBitsFast(bitD_bitContainer, bitD_bitsConsumed, nbBits); assert(nbBits >= 1); @@ -539,11 +645,22 @@ namespace ZstdSharp.Unsafe * point you must use BIT_reloadDStream() to reload. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static BIT_DStream_status BIT_reloadDStreamFast(ref nuint bitD_bitContainer, ref uint bitD_bitsConsumed, ref sbyte* bitD_ptr, sbyte* bitD_start, sbyte* bitD_limitPtr) + private static BIT_DStream_status BIT_reloadDStreamFast( + ref nuint bitD_bitContainer, + ref uint bitD_bitsConsumed, + ref sbyte* bitD_ptr, + sbyte* bitD_start, + sbyte* bitD_limitPtr + ) { if (bitD_ptr < bitD_limitPtr) return BIT_DStream_status.BIT_DStream_overflow; - return BIT_reloadDStream_internal(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start); + return BIT_reloadDStream_internal( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start + ); } /*! BIT_reloadDStream() : @@ -552,7 +669,13 @@ namespace ZstdSharp.Unsafe * @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(ref nuint bitD_bitContainer, ref uint bitD_bitsConsumed, ref sbyte* bitD_ptr, sbyte* bitD_start, sbyte* bitD_limitPtr) + private static BIT_DStream_status BIT_reloadDStream( + ref nuint bitD_bitContainer, + ref uint bitD_bitsConsumed, + ref sbyte* bitD_ptr, + sbyte* bitD_start, + sbyte* bitD_limitPtr + ) { if (bitD_bitsConsumed > (uint)(sizeof(nuint) * 8)) { @@ -563,7 +686,12 @@ namespace ZstdSharp.Unsafe assert(bitD_ptr >= bitD_start); if (bitD_ptr >= bitD_limitPtr) { - return BIT_reloadDStream_internal(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start); + return BIT_reloadDStream_internal( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start + ); } if (bitD_ptr == bitD_start) @@ -595,7 +723,12 @@ namespace ZstdSharp.Unsafe * 2. look window is valid after shifted down : bitD->ptr >= bitD->start */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static BIT_DStream_status BIT_reloadDStream_internal(ref nuint bitD_bitContainer, ref uint bitD_bitsConsumed, ref sbyte* bitD_ptr, sbyte* bitD_start) + private static BIT_DStream_status BIT_reloadDStream_internal( + ref nuint bitD_bitContainer, + ref uint bitD_bitsConsumed, + ref sbyte* bitD_ptr, + sbyte* bitD_start + ) { assert(bitD_bitsConsumed <= (uint)(sizeof(nuint) * 8)); bitD_ptr -= bitD_bitsConsumed >> 3; @@ -609,9 +742,15 @@ namespace ZstdSharp.Unsafe * @return : 1 if DStream has _exactly_ reached its end (all bits consumed). */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint BIT_endOfDStream(uint DStream_bitsConsumed, sbyte* DStream_ptr, sbyte* DStream_start) + private static uint BIT_endOfDStream( + uint DStream_bitsConsumed, + sbyte* DStream_ptr, + sbyte* DStream_start + ) { - return DStream_ptr == DStream_start && DStream_bitsConsumed == (uint)(sizeof(nuint) * 8) ? 1U : 0U; + return DStream_ptr == DStream_start && DStream_bitsConsumed == (uint)(sizeof(nuint) * 8) + ? 1U + : 0U; } } } diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/BlockSummary.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/BlockSummary.cs index 4a105a85..5905c452 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/BlockSummary.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/BlockSummary.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public nuint blockSize; public nuint litSize; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_best_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_best_s.cs index 629ee409..5c26ba0a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_best_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_best_s.cs @@ -18,4 +18,4 @@ namespace ZstdSharp.Unsafe public ZDICT_cover_params_t parameters; public nuint compressedSize; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_ctx_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_ctx_t.cs index 3e5fb2d6..cddd0f7b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_ctx_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_ctx_t.cs @@ -17,4 +17,4 @@ namespace ZstdSharp.Unsafe public uint* dmerAt; public uint d; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_dictSelection.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_dictSelection.cs index 21a03e74..c077af0c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_dictSelection.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_dictSelection.cs @@ -9,4 +9,4 @@ namespace ZstdSharp.Unsafe public nuint dictSize; public nuint totalCompressedSize; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_epoch_info_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_epoch_info_t.cs index 50b791c3..3d421061 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_epoch_info_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_epoch_info_t.cs @@ -8,4 +8,4 @@ namespace ZstdSharp.Unsafe public uint num; public uint size; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_map_pair_t_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_map_pair_t_s.cs index 473be376..121a8dde 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_map_pair_t_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_map_pair_t_s.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public uint key; public uint value; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_map_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_map_s.cs index 88188bec..1f872b01 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_map_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_map_s.cs @@ -7,4 +7,4 @@ namespace ZstdSharp.Unsafe public uint size; public uint sizeMask; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_segment_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_segment_t.cs index c217ce39..c83a9f61 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_segment_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_segment_t.cs @@ -9,4 +9,4 @@ namespace ZstdSharp.Unsafe public uint end; public uint score; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_tryParameters_data_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_tryParameters_data_s.cs index 43b10726..91208d00 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_tryParameters_data_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/COVER_tryParameters_data_s.cs @@ -10,4 +10,4 @@ namespace ZstdSharp.Unsafe public nuint dictBufferCapacity; public ZDICT_cover_params_t parameters; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Clevels.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Clevels.cs index 8b5664c3..35ca9cb8 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Clevels.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Clevels.cs @@ -2,112 +2,849 @@ namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { - private static readonly ZSTD_compressionParameters[][] ZSTD_defaultCParameters = new ZSTD_compressionParameters[4][] - { - new ZSTD_compressionParameters[23] + private static readonly ZSTD_compressionParameters[][] ZSTD_defaultCParameters = + new ZSTD_compressionParameters[4][] { - 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: 3, minMatch: 5, targetLength: 2, strategy: ZSTD_strategy.ZSTD_greedy), - new ZSTD_compressionParameters(windowLog: 21, chainLog: 18, hashLog: 19, searchLog: 3, minMatch: 5, targetLength: 4, strategy: ZSTD_strategy.ZSTD_lazy), - new ZSTD_compressionParameters(windowLog: 21, chainLog: 19, hashLog: 20, searchLog: 4, minMatch: 5, targetLength: 8, strategy: ZSTD_strategy.ZSTD_lazy), - 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: 5, minMatch: 5, targetLength: 16, strategy: ZSTD_strategy.ZSTD_lazy2), - new ZSTD_compressionParameters(windowLog: 22, chainLog: 21, hashLog: 22, searchLog: 6, minMatch: 5, targetLength: 16, strategy: ZSTD_strategy.ZSTD_lazy2), - new ZSTD_compressionParameters(windowLog: 22, chainLog: 22, hashLog: 23, searchLog: 6, minMatch: 5, targetLength: 32, strategy: ZSTD_strategy.ZSTD_lazy2), - new ZSTD_compressionParameters(windowLog: 22, chainLog: 22, hashLog: 22, searchLog: 4, 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: 3, minMatch: 5, targetLength: 2, strategy: ZSTD_strategy.ZSTD_greedy), - new ZSTD_compressionParameters(windowLog: 18, chainLog: 17, hashLog: 18, searchLog: 5, 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: 16, hashLog: 17, searchLog: 3, minMatch: 4, targetLength: 4, strategy: ZSTD_strategy.ZSTD_lazy), - new ZSTD_compressionParameters(windowLog: 17, chainLog: 16, hashLog: 17, searchLog: 3, minMatch: 4, targetLength: 8, strategy: ZSTD_strategy.ZSTD_lazy2), - new ZSTD_compressionParameters(windowLog: 17, chainLog: 16, hashLog: 17, searchLog: 4, minMatch: 4, targetLength: 8, strategy: ZSTD_strategy.ZSTD_lazy2), - new ZSTD_compressionParameters(windowLog: 17, chainLog: 16, hashLog: 17, searchLog: 5, minMatch: 4, targetLength: 8, strategy: ZSTD_strategy.ZSTD_lazy2), - new ZSTD_compressionParameters(windowLog: 17, chainLog: 16, 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) - } - }; + 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: 3, + minMatch: 5, + targetLength: 2, + strategy: ZSTD_strategy.ZSTD_greedy + ), + new ZSTD_compressionParameters( + windowLog: 21, + chainLog: 18, + hashLog: 19, + searchLog: 3, + minMatch: 5, + targetLength: 4, + strategy: ZSTD_strategy.ZSTD_lazy + ), + new ZSTD_compressionParameters( + windowLog: 21, + chainLog: 19, + hashLog: 20, + searchLog: 4, + minMatch: 5, + targetLength: 8, + strategy: ZSTD_strategy.ZSTD_lazy + ), + 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: 5, + minMatch: 5, + targetLength: 16, + strategy: ZSTD_strategy.ZSTD_lazy2 + ), + new ZSTD_compressionParameters( + windowLog: 22, + chainLog: 21, + hashLog: 22, + searchLog: 6, + minMatch: 5, + targetLength: 16, + strategy: ZSTD_strategy.ZSTD_lazy2 + ), + new ZSTD_compressionParameters( + windowLog: 22, + chainLog: 22, + hashLog: 23, + searchLog: 6, + minMatch: 5, + targetLength: 32, + strategy: ZSTD_strategy.ZSTD_lazy2 + ), + new ZSTD_compressionParameters( + windowLog: 22, + chainLog: 22, + hashLog: 22, + searchLog: 4, + 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: 3, + minMatch: 5, + targetLength: 2, + strategy: ZSTD_strategy.ZSTD_greedy + ), + new ZSTD_compressionParameters( + windowLog: 18, + chainLog: 17, + hashLog: 18, + searchLog: 5, + 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: 16, + hashLog: 17, + searchLog: 3, + minMatch: 4, + targetLength: 4, + strategy: ZSTD_strategy.ZSTD_lazy + ), + new ZSTD_compressionParameters( + windowLog: 17, + chainLog: 16, + hashLog: 17, + searchLog: 3, + minMatch: 4, + targetLength: 8, + strategy: ZSTD_strategy.ZSTD_lazy2 + ), + new ZSTD_compressionParameters( + windowLog: 17, + chainLog: 16, + hashLog: 17, + searchLog: 4, + minMatch: 4, + targetLength: 8, + strategy: ZSTD_strategy.ZSTD_lazy2 + ), + new ZSTD_compressionParameters( + windowLog: 17, + chainLog: 16, + hashLog: 17, + searchLog: 5, + minMatch: 4, + targetLength: 8, + strategy: ZSTD_strategy.ZSTD_lazy2 + ), + new ZSTD_compressionParameters( + windowLog: 17, + chainLog: 16, + 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 + ), + }, + }; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Compiler.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Compiler.cs index 27ab2cdf..ab5def3d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Compiler.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Compiler.cs @@ -59,4 +59,4 @@ namespace ZstdSharp.Unsafe return add > 0 ? ptr + add : ptr; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Cover.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Cover.cs index ca1d7aa8..e90943ac 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Cover.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Cover.cs @@ -5,6 +5,7 @@ namespace ZstdSharp.Unsafe public static unsafe partial class Methods { private static int g_displayLevel = 0; + /** * Returns the sum of the sample sizes. */ @@ -23,7 +24,11 @@ namespace ZstdSharp.Unsafe /** * Warns the user when their corpus is too small. */ - private static void COVER_warnOnSmallCorpus(nuint maxDictSize, nuint nbDmers, int displayLevel) + private static void COVER_warnOnSmallCorpus( + nuint maxDictSize, + nuint nbDmers, + int displayLevel + ) { double ratio = nbDmers / (double)maxDictSize; if (ratio >= 10) @@ -45,7 +50,12 @@ namespace ZstdSharp.Unsafe * @param passes The target number of passes over the dmer corpus. * More passes means a better dictionary. */ - private static COVER_epoch_info_t COVER_computeEpochs(uint maxDictSize, uint nbDmers, uint k, uint passes) + private static COVER_epoch_info_t COVER_computeEpochs( + uint maxDictSize, + uint nbDmers, + uint k, + uint passes + ) { uint minEpochSize = k * 10; COVER_epoch_info_t epochs; @@ -66,7 +76,16 @@ namespace ZstdSharp.Unsafe /** * Checks total compressed size of a dictionary */ - private static nuint COVER_checkTotalCompressedSize(ZDICT_cover_params_t parameters, nuint* samplesSizes, byte* samples, nuint* offsets, nuint nbTrainSamples, nuint nbSamples, byte* dict, nuint dictBufferCapacity) + private 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)); /* Pointers */ @@ -81,7 +100,8 @@ namespace ZstdSharp.Unsafe i = parameters.splitPoint < 1 ? nbTrainSamples : 0; for (; i < nbSamples; ++i) { - maxSampleSize = samplesSizes[i] > maxSampleSize ? samplesSizes[i] : maxSampleSize; + maxSampleSize = + samplesSizes[i] > maxSampleSize ? samplesSizes[i] : maxSampleSize; } dstCapacity = ZSTD_compressBound(maxSampleSize); @@ -99,7 +119,14 @@ namespace ZstdSharp.Unsafe i = parameters.splitPoint < 1 ? nbTrainSamples : 0; for (; i < nbSamples; ++i) { - nuint size = ZSTD_compress_usingCDict(cctx, dst, dstCapacity, samples + offsets[i], samplesSizes[i], cdict); + nuint size = ZSTD_compress_usingCDict( + cctx, + dst, + dstCapacity, + samples + offsets[i], + samplesSizes[i], + cdict + ); if (ERR_isError(size)) { totalCompressedSize = size; @@ -109,7 +136,7 @@ namespace ZstdSharp.Unsafe totalCompressedSize += size; } - _compressCleanup: + _compressCleanup: ZSTD_freeCCtx(cctx); ZSTD_freeCDict(cdict); if (dst != null) @@ -194,7 +221,11 @@ namespace ZstdSharp.Unsafe * Decrements liveJobs and signals any waiting threads if liveJobs == 0. * If this dictionary is the best so far save it and its parameters. */ - private static void COVER_best_finish(COVER_best_s* best, ZDICT_cover_params_t parameters, COVER_dictSelection selection) + private static void COVER_best_finish( + COVER_best_s* best, + ZDICT_cover_params_t parameters, + COVER_dictSelection selection + ) { void* dict = selection.dictContent; nuint compressedSize = selection.totalCompressedSize; @@ -221,7 +252,9 @@ namespace ZstdSharp.Unsafe best->dict = malloc(dictSize); if (best->dict == null) { - best->compressedSize = unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC)); + best->compressedSize = unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC) + ); best->dictSize = 0; SynchronizationWrapper.Pulse(&best->mutex); SynchronizationWrapper.Exit(&best->mutex); @@ -271,7 +304,9 @@ namespace ZstdSharp.Unsafe */ private static uint COVER_dictSelectionIsError(COVER_dictSelection selection) { - return ERR_isError(selection.totalCompressedSize) || selection.dictContent == null ? 1U : 0U; + return ERR_isError(selection.totalCompressedSize) || selection.dictContent == null + ? 1U + : 0U; } /** @@ -289,7 +324,19 @@ namespace ZstdSharp.Unsafe * smallest dictionary within a specified regression of the compressed size * from the largest dictionary. */ - private 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) + private 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; @@ -305,7 +352,16 @@ namespace ZstdSharp.Unsafe } memcpy(largestDictbuffer, customDictContent, (uint)dictContentSize); - dictContentSize = ZDICT_finalizeDictionary(largestDictbuffer, dictBufferCapacity, customDictContent, dictContentSize, samplesBuffer, samplesSizes, nbFinalizeSamples, @params.zParams); + dictContentSize = ZDICT_finalizeDictionary( + largestDictbuffer, + dictBufferCapacity, + customDictContent, + dictContentSize, + samplesBuffer, + samplesSizes, + nbFinalizeSamples, + @params.zParams + ); if (ZDICT_isError(dictContentSize)) { free(largestDictbuffer); @@ -313,7 +369,16 @@ namespace ZstdSharp.Unsafe return COVER_dictSelectionError(dictContentSize); } - totalCompressedSize = COVER_checkTotalCompressedSize(@params, samplesSizes, samplesBuffer, offsets, nbCheckSamples, nbSamples, largestDictbuffer, dictContentSize); + totalCompressedSize = COVER_checkTotalCompressedSize( + @params, + samplesSizes, + samplesBuffer, + offsets, + nbCheckSamples, + nbSamples, + largestDictbuffer, + dictContentSize + ); if (ERR_isError(totalCompressedSize)) { free(largestDictbuffer); @@ -333,7 +398,16 @@ namespace ZstdSharp.Unsafe while (dictContentSize < largestDict) { memcpy(candidateDictBuffer, largestDictbuffer, (uint)largestDict); - dictContentSize = ZDICT_finalizeDictionary(candidateDictBuffer, dictBufferCapacity, customDictContentEnd - dictContentSize, dictContentSize, samplesBuffer, samplesSizes, nbFinalizeSamples, @params.zParams); + dictContentSize = ZDICT_finalizeDictionary( + candidateDictBuffer, + dictBufferCapacity, + customDictContentEnd - dictContentSize, + dictContentSize, + samplesBuffer, + samplesSizes, + nbFinalizeSamples, + @params.zParams + ); if (ZDICT_isError(dictContentSize)) { free(largestDictbuffer); @@ -341,7 +415,16 @@ namespace ZstdSharp.Unsafe return COVER_dictSelectionError(dictContentSize); } - totalCompressedSize = COVER_checkTotalCompressedSize(@params, samplesSizes, samplesBuffer, offsets, nbCheckSamples, nbSamples, candidateDictBuffer, dictContentSize); + totalCompressedSize = COVER_checkTotalCompressedSize( + @params, + samplesSizes, + samplesBuffer, + offsets, + nbCheckSamples, + nbSamples, + candidateDictBuffer, + dictContentSize + ); if (ERR_isError(totalCompressedSize)) { free(largestDictbuffer); @@ -352,7 +435,11 @@ namespace ZstdSharp.Unsafe if (totalCompressedSize <= largestCompressed * regressionTolerance) { free(largestDictbuffer); - return setDictSelection(candidateDictBuffer, dictContentSize, totalCompressedSize); + return setDictSelection( + candidateDictBuffer, + dictContentSize, + totalCompressedSize + ); } dictContentSize *= 2; @@ -364,4 +451,4 @@ namespace ZstdSharp.Unsafe return setDictSelection(largestDictbuffer, dictContentSize, totalCompressedSize); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/DTableDesc.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/DTableDesc.cs index e3fca967..a0baad8d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/DTableDesc.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/DTableDesc.cs @@ -10,4 +10,4 @@ namespace ZstdSharp.Unsafe public byte tableLog; public byte reserved; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/EStats_ress_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/EStats_ress_t.cs index ecf8dc2c..f55e6f7f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/EStats_ress_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/EStats_ress_t.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /* dictionary */ public ZSTD_CDict_s* dict; + /* working context */ public ZSTD_CCtx_s* zc; + /* must be ZSTD_BLOCKSIZE_MAX allocated */ public void* workPlace; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/EntropyCommon.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/EntropyCommon.cs index 12c39f6b..38f1729f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/EntropyCommon.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/EntropyCommon.cs @@ -37,7 +37,13 @@ namespace ZstdSharp.Unsafe * FSE NCount encoding-decoding ****************************************************************/ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint FSE_readNCount_body(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize) + private static nuint FSE_readNCount_body( + short* normalizedCounter, + uint* maxSVPtr, + uint* tableLogPtr, + void* headerBuffer, + nuint hbSize + ) { byte* istart = (byte*)headerBuffer; byte* iend = istart + hbSize; @@ -57,11 +63,19 @@ namespace ZstdSharp.Unsafe memset(buffer, 0, sizeof(sbyte) * 8); memcpy(buffer, headerBuffer, (uint)hbSize); { - nuint countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr, buffer, sizeof(sbyte) * 8); + nuint countSize = FSE_readNCount( + normalizedCounter, + maxSVPtr, + tableLogPtr, + buffer, + sizeof(sbyte) * 8 + ); if (FSE_isError(countSize)) return countSize; if (countSize > hbSize) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); return countSize; } } @@ -198,17 +212,42 @@ namespace ZstdSharp.Unsafe } /* 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) + 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); + 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. */ - private static nuint FSE_readNCount_bmi2(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize, int bmi2) + private static nuint FSE_readNCount_bmi2( + short* normalizedCounter, + uint* maxSVPtr, + uint* tableLogPtr, + void* headerBuffer, + nuint hbSize, + int bmi2 + ) { - return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize); + return FSE_readNCount_body_default( + normalizedCounter, + maxSVPtr, + tableLogPtr, + headerBuffer, + hbSize + ); } /*! FSE_readNCount(): @@ -216,9 +255,22 @@ namespace ZstdSharp.Unsafe @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 */ - private static nuint FSE_readNCount(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize) + private static nuint FSE_readNCount( + short* normalizedCounter, + uint* maxSVPtr, + uint* tableLogPtr, + void* headerBuffer, + nuint hbSize + ) { - return FSE_readNCount_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize, 0); + return FSE_readNCount_bmi2( + normalizedCounter, + maxSVPtr, + tableLogPtr, + headerBuffer, + hbSize, + 0 + ); } /*! HUF_readStats() : @@ -228,14 +280,44 @@ namespace ZstdSharp.Unsafe @return : size read from `src` , or an error Code . Note : Needed by HUF_readCTable() and HUF_readDTableX?() . */ - private static nuint HUF_readStats(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize) + private static nuint HUF_readStats( + byte* huffWeight, + nuint hwSize, + uint* rankStats, + uint* nbSymbolsPtr, + uint* tableLogPtr, + void* src, + nuint srcSize + ) { uint* wksp = stackalloc uint[219]; - return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(uint) * 219, 0); + return HUF_readStats_wksp( + huffWeight, + hwSize, + rankStats, + nbSymbolsPtr, + tableLogPtr, + src, + srcSize, + wksp, + sizeof(uint) * 219, + 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) + 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; @@ -266,7 +348,16 @@ namespace ZstdSharp.Unsafe { if (iSize + 1 > srcSize) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)); - oSize = FSE_decompress_wksp_bmi2(huffWeight, hwSize - 1, ip + 1, iSize, 6, workSpace, wkspSize, bmi2); + oSize = FSE_decompress_wksp_bmi2( + huffWeight, + hwSize - 1, + ip + 1, + iSize, + 6, + workSpace, + wkspSize, + bmi2 + ); if (FSE_isError(oSize)) return oSize; } @@ -278,7 +369,9 @@ namespace ZstdSharp.Unsafe for (n = 0; n < oSize; n++) { if (huffWeight[n] > 12) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); rankStats[huffWeight[n]]++; weightTotal += (uint)(1 << huffWeight[n] >> 1); } @@ -297,7 +390,9 @@ namespace ZstdSharp.Unsafe uint verif = (uint)(1 << (int)ZSTD_highbit32(rest)); uint lastWeight = ZSTD_highbit32(rest) + 1; if (verif != rest) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); huffWeight[oSize] = (byte)lastWeight; rankStats[lastWeight]++; } @@ -310,14 +405,56 @@ namespace ZstdSharp.Unsafe } /* 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) + 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); + return HUF_readStats_body( + huffWeight, + hwSize, + rankStats, + nbSymbolsPtr, + tableLogPtr, + src, + srcSize, + workSpace, + wkspSize, + 0 + ); } - private static nuint HUF_readStats_wksp(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_readStats_wksp( + byte* huffWeight, + nuint hwSize, + uint* rankStats, + uint* nbSymbolsPtr, + uint* tableLogPtr, + void* src, + nuint srcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { - return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize); + return HUF_readStats_body_default( + huffWeight, + hwSize, + rankStats, + nbSymbolsPtr, + tableLogPtr, + src, + srcSize, + workSpace, + wkspSize + ); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ErrorPrivate.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ErrorPrivate.cs index a04be98f..7c4956a6 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ErrorPrivate.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ErrorPrivate.cs @@ -108,4 +108,4 @@ namespace ZstdSharp.Unsafe } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/EstimatedBlockSize.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/EstimatedBlockSize.cs index 5e77ffd5..6ea7f508 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/EstimatedBlockSize.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/EstimatedBlockSize.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public nuint estLitSize; public nuint estBlockSize; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_accel_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_accel_t.cs index 73a307fb..773e913f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_accel_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_accel_t.cs @@ -7,12 +7,14 @@ namespace ZstdSharp.Unsafe { /* Percentage of training samples used for ZDICT_finalizeDictionary */ public uint finalize; + /* Number of dmer skipped between each dmer counted in computeFrequency */ public uint skip; + public FASTCOVER_accel_t(uint finalize, uint skip) { this.finalize = finalize; this.skip = skip; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_ctx_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_ctx_t.cs index 718132ba..d5fa4f4e 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_ctx_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_ctx_t.cs @@ -17,4 +17,4 @@ namespace ZstdSharp.Unsafe public uint f; public FASTCOVER_accel_t accelParams; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_tryParameters_data_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_tryParameters_data_s.cs index c80a4625..45d67a0a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_tryParameters_data_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FASTCOVER_tryParameters_data_s.cs @@ -10,4 +10,4 @@ namespace ZstdSharp.Unsafe public nuint dictBufferCapacity; public ZDICT_cover_params_t parameters; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FPStats.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FPStats.cs index d97e8482..70ed5f8c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FPStats.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FPStats.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public Fingerprint pastEvents; public Fingerprint newEvents; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_CState_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_CState_t.cs index a45fabd1..b6bf1e42 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_CState_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_CState_t.cs @@ -14,4 +14,4 @@ namespace ZstdSharp.Unsafe public void* symbolTT; public uint stateLog; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DState_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DState_t.cs index 14d967ab..0ad9873c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DState_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DState_t.cs @@ -6,7 +6,8 @@ namespace ZstdSharp.Unsafe public unsafe struct FSE_DState_t { public nuint state; + /* precise table may vary, depending on U16 */ public void* table; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DTableHeader.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DTableHeader.cs index f4c63896..9fa98926 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DTableHeader.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DTableHeader.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public ushort tableLog; public ushort fastMode; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DecompressWksp.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DecompressWksp.cs index 1a3e0228..09e7c440 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DecompressWksp.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_DecompressWksp.cs @@ -4,4 +4,4 @@ namespace ZstdSharp.Unsafe { public fixed short ncount[256]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_decode_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_decode_t.cs index 49f2ab6d..49f07ea3 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_decode_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_decode_t.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public byte symbol; public byte nbBits; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_repeat.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_repeat.cs index 15743172..52bc103c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_repeat.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_repeat.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /**< Cannot use the previous table */ FSE_repeat_none, + /**< Can use the previous table but it must be checked */ FSE_repeat_check, + /**< Can use the previous table and it is assumed to be valid */ - FSE_repeat_valid + FSE_repeat_valid, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_symbolCompressionTransform.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_symbolCompressionTransform.cs index 5172daaa..55dd4834 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_symbolCompressionTransform.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FSE_symbolCompressionTransform.cs @@ -8,4 +8,4 @@ namespace ZstdSharp.Unsafe public int deltaFindState; public uint deltaNbBits; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Fastcover.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Fastcover.cs index 5d775344..03e4179b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Fastcover.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Fastcover.cs @@ -20,7 +20,24 @@ namespace ZstdSharp.Unsafe return ZSTD_hash8Ptr(p, f); } - private static readonly 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) }); + private static readonly 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 ***************************************/ @@ -35,7 +52,14 @@ namespace ZstdSharp.Unsafe * * 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) + private static COVER_segment_t FASTCOVER_selectSegment( + FASTCOVER_ctx_t* ctx, + uint* freqs, + uint begin, + uint end, + ZDICT_cover_params_t parameters, + ushort* segmentFreqs + ) { /* Constants */ uint k = parameters.k; @@ -47,7 +71,7 @@ namespace ZstdSharp.Unsafe { begin = 0, end = 0, - score = 0 + score = 0, }; COVER_segment_t activeSegment; activeSegment.begin = begin; @@ -67,7 +91,11 @@ namespace ZstdSharp.Unsafe if (activeSegment.end - activeSegment.begin == dmersInK + 1) { /* Get hash value of the dmer to be eliminated from active segment */ - nuint delIndex = FASTCOVER_hashPtrToIndex(ctx->samples + activeSegment.begin, f, d); + nuint delIndex = FASTCOVER_hashPtrToIndex( + ctx->samples + activeSegment.begin, + f, + d + ); segmentFreqs[delIndex] -= 1; if (segmentFreqs[delIndex] == 0) { @@ -103,7 +131,12 @@ namespace ZstdSharp.Unsafe return bestSegment; } - private static int FASTCOVER_checkParameters(ZDICT_cover_params_t parameters, nuint maxDictSize, uint f, uint accel) + private static int FASTCOVER_checkParameters( + ZDICT_cover_params_t parameters, + nuint maxDictSize, + uint f, + uint accel + ) { if (parameters.d == 0 || parameters.k == 0) { @@ -189,16 +222,32 @@ namespace ZstdSharp.Unsafe * 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) + 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); /* Split samples into testing and training sets */ uint nbTrainSamples = splitPoint < 1 ? (uint)(nbSamples * splitPoint) : nbSamples; uint nbTestSamples = splitPoint < 1 ? nbSamples - nbTrainSamples : nbSamples; - nuint trainingSamplesSize = splitPoint < 1 ? COVER_sum(samplesSizes, nbTrainSamples) : totalSamplesSize; - nuint testSamplesSize = splitPoint < 1 ? COVER_sum(samplesSizes + nbTrainSamples, nbTestSamples) : totalSamplesSize; - if (totalSamplesSize < (d > sizeof(ulong) ? d : sizeof(ulong)) || totalSamplesSize >= (sizeof(nuint) == 8 ? unchecked((uint)-1) : 1 * (1U << 30))) + nuint trainingSamplesSize = + splitPoint < 1 ? COVER_sum(samplesSizes, nbTrainSamples) : totalSamplesSize; + nuint testSamplesSize = + splitPoint < 1 + ? COVER_sum(samplesSizes + nbTrainSamples, nbTestSamples) + : totalSamplesSize; + if ( + totalSamplesSize < (d > sizeof(ulong) ? d : sizeof(ulong)) + || totalSamplesSize >= (sizeof(nuint) == 8 ? unchecked((uint)-1) : 1 * (1U << 30)) + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)); } @@ -224,7 +273,7 @@ namespace ZstdSharp.Unsafe d = d, f = f, accelParams = accelParams, - offsets = (nuint*)calloc(nbSamples + 1, (ulong)sizeof(nuint)) + offsets = (nuint*)calloc(nbSamples + 1, (ulong)sizeof(nuint)), }; if (ctx->offsets == null) { @@ -256,12 +305,24 @@ namespace ZstdSharp.Unsafe /** * 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) + 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; /* Divide the data into epochs. We will select one segment from each epoch. */ - COVER_epoch_info_t epochs = COVER_computeEpochs((uint)dictBufferCapacity, (uint)ctx->nbDmers, parameters.k, 1); + COVER_epoch_info_t epochs = COVER_computeEpochs( + (uint)dictBufferCapacity, + (uint)ctx->nbDmers, + parameters.k, + 1 + ); const nuint maxZeroScoreRun = 10; nuint zeroScoreRun = 0; nuint epoch; @@ -271,7 +332,14 @@ namespace ZstdSharp.Unsafe uint epochEnd = epochBegin + epochs.size; nuint segmentSize; /* Select a segment */ - COVER_segment_t segment = FASTCOVER_selectSegment(ctx, freqs, epochBegin, epochEnd, parameters, segmentFreqs); + COVER_segment_t segment = FASTCOVER_selectSegment( + ctx, + freqs, + epochBegin, + epochEnd, + parameters, + segmentFreqs + ); if (segment.score == 0) { if (++zeroScoreRun >= maxZeroScoreRun) @@ -283,7 +351,10 @@ namespace ZstdSharp.Unsafe } zeroScoreRun = 0; - segmentSize = segment.end - segment.begin + parameters.d - 1 < tail ? segment.end - segment.begin + parameters.d - 1 : tail; + segmentSize = + segment.end - segment.begin + parameters.d - 1 < tail + ? segment.end - segment.begin + parameters.d - 1 + : tail; if (segmentSize < parameters.d) { break; @@ -313,7 +384,9 @@ namespace ZstdSharp.Unsafe ushort* segmentFreqs = (ushort*)calloc((ulong)1 << (int)ctx->f, sizeof(ushort)); /* Allocate space for hash table, dict, and freqs */ byte* dict = (byte*)malloc(dictBufferCapacity); - COVER_dictSelection selection = COVER_dictSelectionError(unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC))); + COVER_dictSelection selection = COVER_dictSelectionError( + unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC)) + ); uint* freqs = (uint*)malloc(((ulong)1 << (int)ctx->f) * sizeof(uint)); if (segmentFreqs == null || dict == null || freqs == null) { @@ -322,16 +395,37 @@ namespace ZstdSharp.Unsafe memcpy(freqs, ctx->freqs, (uint)(((ulong)1 << (int)ctx->f) * sizeof(uint))); { - nuint tail = FASTCOVER_buildDictionary(ctx, freqs, 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); + nuint tail = FASTCOVER_buildDictionary( + ctx, + freqs, + 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: + _cleanup: free(dict); COVER_best_finish(data->best, parameters, selection); free(data); @@ -340,7 +434,10 @@ namespace ZstdSharp.Unsafe free(freqs); } - private static void FASTCOVER_convertToCoverParams(ZDICT_fastCover_params_t fastCoverParams, ZDICT_cover_params_t* coverParams) + private static void FASTCOVER_convertToCoverParams( + ZDICT_fastCover_params_t fastCoverParams, + ZDICT_cover_params_t* coverParams + ) { coverParams->k = fastCoverParams.k; coverParams->d = fastCoverParams.d; @@ -351,7 +448,12 @@ namespace ZstdSharp.Unsafe coverParams->shrinkDict = fastCoverParams.shrinkDict; } - private static void FASTCOVER_convertToFastCoverParams(ZDICT_cover_params_t coverParams, ZDICT_fastCover_params_t* fastCoverParams, uint f, uint accel) + 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; @@ -380,7 +482,14 @@ namespace ZstdSharp.Unsafe * 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) + 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; @@ -392,7 +501,14 @@ namespace ZstdSharp.Unsafe parameters.accel = parameters.accel == 0 ? 1 : parameters.accel; coverParams = new ZDICT_cover_params_t(); FASTCOVER_convertToCoverParams(parameters, &coverParams); - if (FASTCOVER_checkParameters(coverParams, dictBufferCapacity, parameters.f, parameters.accel) == 0) + if ( + FASTCOVER_checkParameters( + coverParams, + dictBufferCapacity, + parameters.f, + parameters.accel + ) == 0 + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); } @@ -409,7 +525,16 @@ namespace ZstdSharp.Unsafe accelParams = FASTCOVER_defaultAccelParameters[parameters.accel]; { - nuint initVal = FASTCOVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, coverParams.d, parameters.splitPoint, parameters.f, accelParams); + nuint initVal = FASTCOVER_ctx_init( + &ctx, + samplesBuffer, + samplesSizes, + nbSamples, + coverParams.d, + parameters.splitPoint, + parameters.f, + accelParams + ); if (ERR_isError(initVal)) { return initVal; @@ -419,13 +544,32 @@ namespace ZstdSharp.Unsafe COVER_warnOnSmallCorpus(dictBufferCapacity, ctx.nbDmers, g_displayLevel); { /* Initialize array to keep track of frequency of dmer within activeSegment */ - ushort* segmentFreqs = (ushort*)calloc((ulong)1 << (int)parameters.f, sizeof(ushort)); - nuint tail = FASTCOVER_buildDictionary(&ctx, ctx.freqs, dictBuffer, dictBufferCapacity, coverParams, segmentFreqs); - uint nbFinalizeSamples = (uint)(ctx.nbTrainSamples * ctx.accelParams.finalize / 100); - nuint dictionarySize = ZDICT_finalizeDictionary(dict, dictBufferCapacity, dict + tail, dictBufferCapacity - tail, samplesBuffer, samplesSizes, nbFinalizeSamples, coverParams.zParams); - if (!ERR_isError(dictionarySize)) - { - } + ushort* segmentFreqs = (ushort*)calloc( + (ulong)1 << (int)parameters.f, + sizeof(ushort) + ); + nuint tail = FASTCOVER_buildDictionary( + &ctx, + ctx.freqs, + dictBuffer, + dictBufferCapacity, + coverParams, + segmentFreqs + ); + uint nbFinalizeSamples = (uint)( + ctx.nbTrainSamples * ctx.accelParams.finalize / 100 + ); + nuint dictionarySize = ZDICT_finalizeDictionary( + dict, + dictBufferCapacity, + dict + tail, + dictBufferCapacity - tail, + samplesBuffer, + samplesSizes, + nbFinalizeSamples, + coverParams.zParams + ); + if (!ERR_isError(dictionarySize)) { } FASTCOVER_ctx_destroy(&ctx); free(segmentFreqs); @@ -451,7 +595,14 @@ namespace ZstdSharp.Unsafe * 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) + 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; @@ -520,7 +671,16 @@ namespace ZstdSharp.Unsafe /* Initialize the context for this value of d */ FASTCOVER_ctx_t ctx; { - nuint initVal = FASTCOVER_ctx_init(&ctx, samplesBuffer, samplesSizes, nbSamples, d, splitPoint, f, accelParams); + nuint initVal = FASTCOVER_ctx_init( + &ctx, + samplesBuffer, + samplesSizes, + nbSamples, + d, + splitPoint, + f, + accelParams + ); if (ERR_isError(initVal)) { COVER_best_destroy(&best); @@ -538,13 +698,17 @@ namespace ZstdSharp.Unsafe for (k = kMinK; k <= kMaxK; k += kStepSize) { /* Prepare the arguments */ - FASTCOVER_tryParameters_data_s* data = (FASTCOVER_tryParameters_data_s*)malloc((ulong)sizeof(FASTCOVER_tryParameters_data_s)); + FASTCOVER_tryParameters_data_s* data = (FASTCOVER_tryParameters_data_s*)malloc( + (ulong)sizeof(FASTCOVER_tryParameters_data_s) + ); if (data == null) { COVER_best_destroy(&best); FASTCOVER_ctx_destroy(&ctx); POOL_free(pool); - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); } data->ctx = &ctx; @@ -557,7 +721,14 @@ namespace ZstdSharp.Unsafe 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) + if ( + FASTCOVER_checkParameters( + data->parameters, + dictBufferCapacity, + data->ctx->f, + accel + ) == 0 + ) { free(data); continue; @@ -566,7 +737,11 @@ namespace ZstdSharp.Unsafe COVER_best_start(&best); if (pool != null) { - POOL_add(pool, (delegate* managed)(&FASTCOVER_tryParameters), data); + POOL_add( + pool, + (delegate* managed)(&FASTCOVER_tryParameters), + data + ); } else { @@ -598,4 +773,4 @@ namespace ZstdSharp.Unsafe } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Fingerprint.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Fingerprint.cs index 6ef7760f..90559d8f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Fingerprint.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Fingerprint.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public fixed uint events[1024]; public nuint nbEvents; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Fse.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Fse.cs index dc2174b9..f42b45a1 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Fse.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Fse.cs @@ -25,28 +25,52 @@ namespace ZstdSharp.Unsafe { FSE_initCState(ref statePtr, ct); { - FSE_symbolCompressionTransform symbolTT = ((FSE_symbolCompressionTransform*)statePtr.symbolTT)[symbol]; + FSE_symbolCompressionTransform symbolTT = ( + (FSE_symbolCompressionTransform*)statePtr.symbolTT + )[symbol]; ushort* stateTable = (ushort*)statePtr.stateTable; uint nbBitsOut = symbolTT.deltaNbBits + (1 << 15) >> 16; statePtr.value = (nint)((nbBitsOut << 16) - symbolTT.deltaNbBits); - statePtr.value = stateTable[(statePtr.value >> (int)nbBitsOut) + symbolTT.deltaFindState]; + statePtr.value = stateTable[ + (statePtr.value >> (int)nbBitsOut) + symbolTT.deltaFindState + ]; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void FSE_encodeSymbol(ref nuint bitC_bitContainer, ref uint bitC_bitPos, ref FSE_CState_t statePtr, uint symbol) + private static void FSE_encodeSymbol( + ref nuint bitC_bitContainer, + ref uint bitC_bitPos, + ref FSE_CState_t statePtr, + uint symbol + ) { - FSE_symbolCompressionTransform symbolTT = ((FSE_symbolCompressionTransform*)statePtr.symbolTT)[symbol]; + FSE_symbolCompressionTransform symbolTT = ( + (FSE_symbolCompressionTransform*)statePtr.symbolTT + )[symbol]; ushort* stateTable = (ushort*)statePtr.stateTable; uint nbBitsOut = (uint)statePtr.value + symbolTT.deltaNbBits >> 16; BIT_addBits(ref bitC_bitContainer, ref bitC_bitPos, (nuint)statePtr.value, nbBitsOut); - statePtr.value = stateTable[(statePtr.value >> (int)nbBitsOut) + symbolTT.deltaFindState]; + statePtr.value = stateTable[ + (statePtr.value >> (int)nbBitsOut) + symbolTT.deltaFindState + ]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void FSE_flushCState(ref nuint bitC_bitContainer, ref uint bitC_bitPos, ref sbyte* bitC_ptr, sbyte* bitC_endPtr, ref FSE_CState_t statePtr) + private static void FSE_flushCState( + ref nuint bitC_bitContainer, + ref uint bitC_bitPos, + ref sbyte* bitC_ptr, + sbyte* bitC_endPtr, + ref FSE_CState_t statePtr + ) { - BIT_addBits(ref bitC_bitContainer, ref bitC_bitPos, (nuint)statePtr.value, statePtr.stateLog); + BIT_addBits( + ref bitC_bitContainer, + ref bitC_bitPos, + (nuint)statePtr.value, + statePtr.stateLog + ); BIT_flushBits(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); } @@ -67,7 +91,12 @@ namespace ZstdSharp.Unsafe * 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) + 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; @@ -76,9 +105,11 @@ namespace ZstdSharp.Unsafe assert(accuracyLog < 31 - tableLog); { uint tableSize = (uint)(1 << (int)tableLog); - uint deltaFromThreshold = threshold - (symbolTT[symbolValue].deltaNbBits + tableSize); + uint deltaFromThreshold = + threshold - (symbolTT[symbolValue].deltaNbBits + tableSize); /* linear interpolation (very approximate) */ - uint normalizedDeltaFromThreshold = deltaFromThreshold << (int)accuracyLog >> (int)tableLog; + uint normalizedDeltaFromThreshold = + deltaFromThreshold << (int)accuracyLog >> (int)tableLog; uint bitMultiplier = (uint)(1 << (int)accuracyLog); assert(symbolTT[symbolValue].deltaNbBits + tableSize <= threshold); assert(normalizedDeltaFromThreshold <= bitMultiplier); @@ -87,12 +118,26 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void FSE_initDState(ref FSE_DState_t DStatePtr, ref BIT_DStream_t bitD, uint* dt) + private static void FSE_initDState( + ref FSE_DState_t DStatePtr, + ref BIT_DStream_t bitD, + uint* dt + ) { void* ptr = dt; FSE_DTableHeader* DTableH = (FSE_DTableHeader*)ptr; - DStatePtr.state = BIT_readBits(bitD.bitContainer, ref bitD.bitsConsumed, DTableH->tableLog); - BIT_reloadDStream(ref bitD.bitContainer, ref bitD.bitsConsumed, ref bitD.ptr, bitD.start, bitD.limitPtr); + DStatePtr.state = BIT_readBits( + bitD.bitContainer, + ref bitD.bitsConsumed, + DTableH->tableLog + ); + BIT_reloadDStream( + ref bitD.bitContainer, + ref bitD.bitsConsumed, + ref bitD.ptr, + bitD.start, + bitD.limitPtr + ); DStatePtr.table = dt + 1; } @@ -113,7 +158,11 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static byte FSE_decodeSymbol(ref FSE_DState_t DStatePtr, nuint bitD_bitContainer, ref uint bitD_bitsConsumed) + private static byte FSE_decodeSymbol( + ref FSE_DState_t DStatePtr, + nuint bitD_bitContainer, + ref uint bitD_bitsConsumed + ) { FSE_decode_t DInfo = ((FSE_decode_t*)DStatePtr.table)[DStatePtr.state]; uint nbBits = DInfo.nbBits; @@ -126,7 +175,11 @@ namespace ZstdSharp.Unsafe /*! FSE_decodeSymbolFast() : unsafe, only works if no symbol has a probability > 50% */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static byte FSE_decodeSymbolFast(ref FSE_DState_t DStatePtr, nuint bitD_bitContainer, ref uint bitD_bitsConsumed) + private static byte FSE_decodeSymbolFast( + ref FSE_DState_t DStatePtr, + nuint bitD_bitContainer, + ref uint bitD_bitsConsumed + ) { FSE_decode_t DInfo = ((FSE_decode_t*)DStatePtr.table)[DStatePtr.state]; uint nbBits = DInfo.nbBits; diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FseCompress.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FseCompress.cs index a218439f..2a7de3ab 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FseCompress.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FseCompress.cs @@ -1,6 +1,6 @@ -using static ZstdSharp.UnsafeHelper; using System; using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -11,7 +11,14 @@ namespace ZstdSharp.Unsafe * wkspSize should be sized to handle worst case situation, which is `1< wkspSize) + if ( + sizeof(uint) + * ( + (maxSymbolValue + 2 + (1UL << (int)tableLog)) / 2 + + sizeof(ulong) / sizeof(uint) + ) + > wkspSize + ) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge)); tableU16[-2] = (ushort)tableLog; tableU16[-1] = (ushort)maxSymbolValue; @@ -137,7 +151,8 @@ namespace ZstdSharp.Unsafe switch (normalizedCounter[s]) { case 0: - symbolTT[s].deltaNbBits = (tableLog + 1 << 16) - (uint)(1 << (int)tableLog); + symbolTT[s].deltaNbBits = + (tableLog + 1 << 16) - (uint)(1 << (int)tableLog); break; case -1: case 1: @@ -148,11 +163,15 @@ namespace ZstdSharp.Unsafe break; default: assert(normalizedCounter[s] > 1); + { - uint maxBitsOut = tableLog - ZSTD_highbit32((uint)normalizedCounter[s] - 1); + uint maxBitsOut = + tableLog - ZSTD_highbit32((uint)normalizedCounter[s] - 1); uint minStatePlus = (uint)normalizedCounter[s] << (int)maxBitsOut; symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus; - symbolTT[s].deltaFindState = (int)(total - (uint)normalizedCounter[s]); + symbolTT[s].deltaFindState = (int)( + total - (uint)normalizedCounter[s] + ); total += (uint)normalizedCounter[s]; } @@ -173,7 +192,14 @@ namespace ZstdSharp.Unsafe return maxSymbolValue != 0 ? maxHeaderSize : 512; } - private static nuint FSE_writeNCount_generic(void* header, nuint headerBufferSize, short* normalizedCounter, uint maxSymbolValue, uint tableLog, uint writeIsSafe) + 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; @@ -206,7 +232,9 @@ namespace ZstdSharp.Unsafe start += 24; bitStream += 0xFFFFU << bitCount; if (writeIsSafe == 0 && @out > oend - 2) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); @out[0] = (byte)bitStream; @out[1] = (byte)(bitStream >> 8); @out += 2; @@ -225,7 +253,9 @@ namespace ZstdSharp.Unsafe if (bitCount > 16) { if (writeIsSafe == 0 && @out > oend - 2) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); @out[0] = (byte)bitStream; @out[1] = (byte)(bitStream >> 8); @out += 2; @@ -282,15 +312,35 @@ namespace ZstdSharp.Unsafe Compactly save 'normalizedCounter' into 'buffer'. @return : size of the compressed table, or an errorCode, which can be tested using FSE_isError(). */ - private static nuint FSE_writeNCount(void* buffer, nuint bufferSize, short* normalizedCounter, uint maxSymbolValue, uint tableLog) + private static nuint FSE_writeNCount( + void* buffer, + nuint bufferSize, + short* normalizedCounter, + uint maxSymbolValue, + uint tableLog + ) { if (tableLog > 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); + return FSE_writeNCount_generic( + buffer, + bufferSize, + normalizedCounter, + maxSymbolValue, + tableLog, + 0 + ); + return FSE_writeNCount_generic( + buffer, + bufferSize, + normalizedCounter, + maxSymbolValue, + tableLog, + 1 + ); } /* provides the minimum logSize to safely represent a distribution */ @@ -306,7 +356,12 @@ namespace ZstdSharp.Unsafe /* ***************************************** * FSE advanced API ***************************************** */ - private static uint FSE_optimalTableLog_internal(uint maxTableLog, nuint srcSize, uint maxSymbolValue, uint minus) + private static uint FSE_optimalTableLog_internal( + uint maxTableLog, + nuint srcSize, + uint maxSymbolValue, + uint minus + ) { uint maxBitsSrc = ZSTD_highbit32((uint)(srcSize - 1)) - minus; uint tableLog = maxTableLog; @@ -329,14 +384,25 @@ namespace ZstdSharp.Unsafe 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') */ - private static uint FSE_optimalTableLog(uint maxTableLog, nuint srcSize, uint maxSymbolValue) + private 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) + private static nuint FSE_normalizeM2( + short* norm, + uint tableLog, + uint* count, + nuint total, + uint maxSymbolValue, + short lowProbCount + ) { const short NOT_YET_ASSIGNED = -2; uint s; @@ -397,7 +463,8 @@ namespace ZstdSharp.Unsafe /* all values are pretty poor; probably incompressible data (should have already been detected); find max, then give all remaining points to max */ - uint maxV = 0, maxC = 0; + uint maxV = 0, + maxC = 0; for (s = 0; s <= maxSymbolValue; s++) if (count[s] > maxC) { @@ -447,21 +514,18 @@ namespace ZstdSharp.Unsafe } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_rtbTable => new uint[8] - { - 0, - 473195, - 504333, - 520860, - 550000, - 700000, - 750000, - 830000 - }; - private static uint* rtbTable => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_rtbTable)); + private static ReadOnlySpan Span_rtbTable => + new uint[8] { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 }; + private static uint* rtbTable => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_rtbTable) + ); #else - private static readonly uint* rtbTable = GetArrayPointer(new uint[8] { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 }); + private static readonly uint* rtbTable = GetArrayPointer( + new uint[8] { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 } + ); #endif /*! FSE_normalizeCount(): normalize counts so that sum(count[]) == Power_of_2 (2^tableLog) @@ -474,7 +538,14 @@ namespace ZstdSharp.Unsafe 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() */ - private static nuint FSE_normalizeCount(short* normalizedCounter, uint tableLog, uint* count, nuint total, uint maxSymbolValue, uint useLowProbCount) + private static nuint FSE_normalizeCount( + short* normalizedCounter, + uint tableLog, + uint* count, + nuint total, + uint maxSymbolValue, + uint useLowProbCount + ) { if (tableLog == 0) tableLog = 13 - 2; @@ -516,7 +587,9 @@ namespace ZstdSharp.Unsafe if (proba < 8) { ulong restToBeat = vStep * rtbTable[proba]; - proba += (short)(count[s] * step - ((ulong)proba << (int)scale) > restToBeat ? 1 : 0); + proba += (short)( + count[s] * step - ((ulong)proba << (int)scale) > restToBeat ? 1 : 0 + ); } if (proba > largestP) @@ -533,7 +606,14 @@ namespace ZstdSharp.Unsafe if (-stillToDistribute >= normalizedCounter[largest] >> 1) { /* corner case, need another normalization method */ - nuint errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue, lowProbCount); + nuint errorCode = FSE_normalizeM2( + normalizedCounter, + tableLog, + count, + total, + maxSymbolValue, + lowProbCount + ); if (ERR_isError(errorCode)) return errorCode; } @@ -560,14 +640,22 @@ namespace ZstdSharp.Unsafe return 0; } - private static nuint FSE_compress_usingCTable_generic(void* dst, nuint dstSize, void* src, nuint srcSize, uint* ct, uint fast) + 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; System.Runtime.CompilerServices.Unsafe.SkipInit(out bitC); - FSE_CState_t CState1, CState2; + FSE_CState_t CState1, + CState2; System.Runtime.CompilerServices.Unsafe.SkipInit(out CState1); System.Runtime.CompilerServices.Unsafe.SkipInit(out CState2); if (srcSize <= 2) @@ -588,9 +676,19 @@ namespace ZstdSharp.Unsafe FSE_initCState2(ref CState2, ct, *--ip); FSE_encodeSymbol(ref bitC_bitContainer, ref bitC_bitPos, ref CState1, *--ip); if (fast != 0) - BIT_flushBitsFast(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); + BIT_flushBitsFast( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr + ); else - BIT_flushBits(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); + BIT_flushBits( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr + ); } else { @@ -604,9 +702,19 @@ namespace ZstdSharp.Unsafe FSE_encodeSymbol(ref bitC_bitContainer, ref bitC_bitPos, ref CState2, *--ip); FSE_encodeSymbol(ref bitC_bitContainer, ref bitC_bitPos, ref CState1, *--ip); if (fast != 0) - BIT_flushBitsFast(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); + BIT_flushBitsFast( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr + ); else - BIT_flushBits(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); + BIT_flushBits( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr + ); } while (ip > istart) @@ -614,9 +722,19 @@ namespace ZstdSharp.Unsafe FSE_encodeSymbol(ref bitC_bitContainer, ref bitC_bitPos, ref CState2, *--ip); if (sizeof(nuint) * 8 < (14 - 2) * 2 + 7) if (fast != 0) - BIT_flushBitsFast(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); + BIT_flushBitsFast( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr + ); else - BIT_flushBits(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); + BIT_flushBits( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr + ); FSE_encodeSymbol(ref bitC_bitContainer, ref bitC_bitPos, ref CState1, *--ip); if (sizeof(nuint) * 8 > (14 - 2) * 4 + 7) { @@ -625,14 +743,42 @@ namespace ZstdSharp.Unsafe } if (fast != 0) - BIT_flushBitsFast(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); + BIT_flushBitsFast( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr + ); else - BIT_flushBits(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr); + BIT_flushBits( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr + ); } - FSE_flushCState(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr, ref CState2); - FSE_flushCState(ref bitC_bitContainer, ref bitC_bitPos, ref bitC_ptr, bitC_endPtr, ref CState1); - return BIT_closeCStream(ref bitC_bitContainer, ref bitC_bitPos, bitC_ptr, bitC_endPtr, bitC.startPtr); + FSE_flushCState( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr, + ref CState2 + ); + FSE_flushCState( + ref bitC_bitContainer, + ref bitC_bitPos, + ref bitC_ptr, + bitC_endPtr, + ref CState1 + ); + return BIT_closeCStream( + ref bitC_bitContainer, + ref bitC_bitPos, + bitC_ptr, + bitC_endPtr, + bitC.startPtr + ); } /*! FSE_compress_usingCTable(): @@ -640,7 +786,13 @@ namespace ZstdSharp.Unsafe @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() */ - private static nuint FSE_compress_usingCTable(void* dst, nuint dstSize, void* src, nuint srcSize, uint* ct) + private 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) @@ -657,4 +809,4 @@ namespace ZstdSharp.Unsafe return 512 + (size + (size >> 7) + 4 + (nuint)sizeof(nuint)); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/FseDecompress.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/FseDecompress.cs index 91ff90d6..9dee183a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/FseDecompress.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/FseDecompress.cs @@ -1,11 +1,18 @@ -using static ZstdSharp.UnsafeHelper; using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { - private static nuint FSE_buildDTable_internal(uint* dt, short* normalizedCounter, uint maxSymbolValue, uint tableLog, void* workSpace, nuint wkspSize) + private static nuint FSE_buildDTable_internal( + uint* dt, + short* normalizedCounter, + uint maxSymbolValue, + uint tableLog, + void* workSpace, + nuint wkspSize + ) { /* because *dt is unsigned, 32-bits aligned on 32-bits */ void* tdPtr = dt + 1; @@ -94,7 +101,8 @@ namespace ZstdSharp.Unsafe { uint tableMask = tableSize - 1; uint step = (tableSize >> 1) + (tableSize >> 3) + 3; - uint s, position = 0; + uint s, + position = 0; for (s = 0; s < maxSV1; s++) { int i; @@ -118,23 +126,46 @@ namespace ZstdSharp.Unsafe byte symbol = tableDecode[u].symbol; uint nextState = symbolNext[symbol]++; tableDecode[u].nbBits = (byte)(tableLog - ZSTD_highbit32(nextState)); - tableDecode[u].newState = (ushort)((nextState << tableDecode[u].nbBits) - tableSize); + tableDecode[u].newState = (ushort)( + (nextState << tableDecode[u].nbBits) - tableSize + ); } } return 0; } - private static nuint FSE_buildDTable_wksp(uint* dt, short* normalizedCounter, uint maxSymbolValue, uint tableLog, void* workSpace, nuint wkspSize) + private 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); + return FSE_buildDTable_internal( + dt, + normalizedCounter, + maxSymbolValue, + tableLog, + workSpace, + wkspSize + ); } /*-******************************************************* * Decompression (Byte symbols) *********************************************************/ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint FSE_decompress_usingDTable_generic(void* dst, nuint maxDstSize, void* cSrc, nuint cSrcSize, uint* dt, uint fast) + 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; @@ -160,49 +191,144 @@ namespace ZstdSharp.Unsafe sbyte* bitD_ptr = bitD.ptr; sbyte* bitD_start = bitD.start; sbyte* bitD_limitPtr = bitD.limitPtr; - if (BIT_reloadDStream(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start, bitD_limitPtr) == BIT_DStream_status.BIT_DStream_overflow) + if ( + BIT_reloadDStream( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start, + bitD_limitPtr + ) == BIT_DStream_status.BIT_DStream_overflow + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); } - for (; BIT_reloadDStream(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start, bitD_limitPtr) == BIT_DStream_status.BIT_DStream_unfinished && op < olimit; op += 4) + for ( + ; + BIT_reloadDStream( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start, + bitD_limitPtr + ) == BIT_DStream_status.BIT_DStream_unfinished + && op < olimit; + op += 4 + ) { - op[0] = fast != 0 ? FSE_decodeSymbolFast(ref state1, bitD_bitContainer, ref bitD_bitsConsumed) : FSE_decodeSymbol(ref state1, bitD_bitContainer, ref bitD_bitsConsumed); + op[0] = + fast != 0 + ? FSE_decodeSymbolFast(ref state1, bitD_bitContainer, ref bitD_bitsConsumed) + : FSE_decodeSymbol(ref state1, bitD_bitContainer, ref bitD_bitsConsumed); if ((14 - 2) * 2 + 7 > sizeof(nuint) * 8) - BIT_reloadDStream(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start, bitD_limitPtr); - op[1] = fast != 0 ? FSE_decodeSymbolFast(ref state2, bitD_bitContainer, ref bitD_bitsConsumed) : FSE_decodeSymbol(ref state2, bitD_bitContainer, ref bitD_bitsConsumed); + BIT_reloadDStream( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start, + bitD_limitPtr + ); + op[1] = + fast != 0 + ? FSE_decodeSymbolFast(ref state2, bitD_bitContainer, ref bitD_bitsConsumed) + : FSE_decodeSymbol(ref state2, bitD_bitContainer, ref bitD_bitsConsumed); if ((14 - 2) * 4 + 7 > sizeof(nuint) * 8) { - if (BIT_reloadDStream(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start, bitD_limitPtr) > BIT_DStream_status.BIT_DStream_unfinished) + if ( + BIT_reloadDStream( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start, + bitD_limitPtr + ) > BIT_DStream_status.BIT_DStream_unfinished + ) { op += 2; break; } } - op[2] = fast != 0 ? FSE_decodeSymbolFast(ref state1, bitD_bitContainer, ref bitD_bitsConsumed) : FSE_decodeSymbol(ref state1, bitD_bitContainer, ref bitD_bitsConsumed); + op[2] = + fast != 0 + ? FSE_decodeSymbolFast(ref state1, bitD_bitContainer, ref bitD_bitsConsumed) + : FSE_decodeSymbol(ref state1, bitD_bitContainer, ref bitD_bitsConsumed); if ((14 - 2) * 2 + 7 > sizeof(nuint) * 8) - BIT_reloadDStream(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start, bitD_limitPtr); - op[3] = fast != 0 ? FSE_decodeSymbolFast(ref state2, bitD_bitContainer, ref bitD_bitsConsumed) : FSE_decodeSymbol(ref state2, bitD_bitContainer, ref bitD_bitsConsumed); + BIT_reloadDStream( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start, + bitD_limitPtr + ); + op[3] = + fast != 0 + ? FSE_decodeSymbolFast(ref state2, bitD_bitContainer, ref bitD_bitsConsumed) + : FSE_decodeSymbol(ref state2, bitD_bitContainer, ref bitD_bitsConsumed); } while (true) { if (op > omax - 2) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); - *op++ = fast != 0 ? FSE_decodeSymbolFast(ref state1, bitD_bitContainer, ref bitD_bitsConsumed) : FSE_decodeSymbol(ref state1, bitD_bitContainer, ref bitD_bitsConsumed); - if (BIT_reloadDStream(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start, bitD_limitPtr) == BIT_DStream_status.BIT_DStream_overflow) + *op++ = + fast != 0 + ? FSE_decodeSymbolFast(ref state1, bitD_bitContainer, ref bitD_bitsConsumed) + : FSE_decodeSymbol(ref state1, bitD_bitContainer, ref bitD_bitsConsumed); + if ( + BIT_reloadDStream( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start, + bitD_limitPtr + ) == BIT_DStream_status.BIT_DStream_overflow + ) { - *op++ = fast != 0 ? FSE_decodeSymbolFast(ref state2, bitD_bitContainer, ref bitD_bitsConsumed) : FSE_decodeSymbol(ref state2, bitD_bitContainer, ref bitD_bitsConsumed); + *op++ = + fast != 0 + ? FSE_decodeSymbolFast( + ref state2, + bitD_bitContainer, + ref bitD_bitsConsumed + ) + : FSE_decodeSymbol( + ref state2, + bitD_bitContainer, + ref bitD_bitsConsumed + ); break; } if (op > omax - 2) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); - *op++ = fast != 0 ? FSE_decodeSymbolFast(ref state2, bitD_bitContainer, ref bitD_bitsConsumed) : FSE_decodeSymbol(ref state2, bitD_bitContainer, ref bitD_bitsConsumed); - if (BIT_reloadDStream(ref bitD_bitContainer, ref bitD_bitsConsumed, ref bitD_ptr, bitD_start, bitD_limitPtr) == BIT_DStream_status.BIT_DStream_overflow) + *op++ = + fast != 0 + ? FSE_decodeSymbolFast(ref state2, bitD_bitContainer, ref bitD_bitsConsumed) + : FSE_decodeSymbol(ref state2, bitD_bitContainer, ref bitD_bitsConsumed); + if ( + BIT_reloadDStream( + ref bitD_bitContainer, + ref bitD_bitsConsumed, + ref bitD_ptr, + bitD_start, + bitD_limitPtr + ) == BIT_DStream_status.BIT_DStream_overflow + ) { - *op++ = fast != 0 ? FSE_decodeSymbolFast(ref state1, bitD_bitContainer, ref bitD_bitsConsumed) : FSE_decodeSymbol(ref state1, bitD_bitContainer, ref bitD_bitsConsumed); + *op++ = + fast != 0 + ? FSE_decodeSymbolFast( + ref state1, + bitD_bitContainer, + ref bitD_bitsConsumed + ) + : FSE_decodeSymbol( + ref state1, + bitD_bitContainer, + ref bitD_bitsConsumed + ); break; } } @@ -212,7 +338,16 @@ namespace ZstdSharp.Unsafe } [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) + 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; @@ -224,7 +359,14 @@ namespace ZstdSharp.Unsafe if (wkspSize < (nuint)sizeof(FSE_DecompressWksp)) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC)); { - nuint NCountLength = FSE_readNCount_bmi2(wksp->ncount, &maxSymbolValue, &tableLog, istart, cSrcSize, bmi2); + nuint NCountLength = FSE_readNCount_bmi2( + wksp->ncount, + &maxSymbolValue, + &tableLog, + istart, + cSrcSize, + bmi2 + ); if (ERR_isError(NCountLength)) return NCountLength; if (tableLog > maxLog) @@ -234,13 +376,42 @@ namespace ZstdSharp.Unsafe cSrcSize -= NCountLength; } - if (((ulong)(1 + (1 << (int)tableLog) + 1) + (sizeof(short) * (maxSymbolValue + 1) + (1UL << (int)tableLog) + 8 + sizeof(uint) - 1) / sizeof(uint) + (255 + 1) / 2 + 1) * sizeof(uint) > wkspSize) + if ( + ( + (ulong)(1 + (1 << (int)tableLog) + 1) + + ( + sizeof(short) * (maxSymbolValue + 1) + + (1UL << (int)tableLog) + + 8 + + sizeof(uint) + - 1 + ) / sizeof(uint) + + (255 + 1) / 2 + + 1 + ) * sizeof(uint) + > wkspSize + ) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge)); - assert((nuint)(sizeof(FSE_DecompressWksp) + (1 + (1 << (int)tableLog)) * sizeof(uint)) <= wkspSize); - workSpace = (byte*)workSpace + sizeof(FSE_DecompressWksp) + (1 + (1 << (int)tableLog)) * sizeof(uint); - wkspSize -= (nuint)(sizeof(FSE_DecompressWksp) + (1 + (1 << (int)tableLog)) * sizeof(uint)); + assert( + (nuint)(sizeof(FSE_DecompressWksp) + (1 + (1 << (int)tableLog)) * sizeof(uint)) + <= wkspSize + ); + workSpace = + (byte*)workSpace + + sizeof(FSE_DecompressWksp) + + (1 + (1 << (int)tableLog)) * sizeof(uint); + wkspSize -= (nuint)( + sizeof(FSE_DecompressWksp) + (1 + (1 << (int)tableLog)) * sizeof(uint) + ); { - nuint _var_err__ = FSE_buildDTable_internal(dtable, wksp->ncount, maxSymbolValue, tableLog, workSpace, wkspSize); + nuint _var_err__ = FSE_buildDTable_internal( + dtable, + wksp->ncount, + maxSymbolValue, + tableLog, + workSpace, + wkspSize + ); if (ERR_isError(_var_err__)) return _var_err__; } @@ -250,20 +421,68 @@ namespace ZstdSharp.Unsafe FSE_DTableHeader* DTableH = (FSE_DTableHeader*)ptr; uint fastMode = DTableH->fastMode; if (fastMode != 0) - return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, dtable, 1); - return FSE_decompress_usingDTable_generic(dst, dstCapacity, ip, cSrcSize, dtable, 0); + return FSE_decompress_usingDTable_generic( + dst, + dstCapacity, + ip, + cSrcSize, + dtable, + 1 + ); + return FSE_decompress_usingDTable_generic( + dst, + dstCapacity, + ip, + cSrcSize, + 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) + 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); + return FSE_decompress_wksp_body( + dst, + dstCapacity, + cSrc, + cSrcSize, + maxLog, + workSpace, + wkspSize, + 0 + ); } - private static nuint FSE_decompress_wksp_bmi2(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize, uint maxLog, void* workSpace, nuint wkspSize, int bmi2) + private static nuint FSE_decompress_wksp_bmi2( + void* dst, + nuint dstCapacity, + void* cSrc, + nuint cSrcSize, + uint maxLog, + void* workSpace, + nuint wkspSize, + int bmi2 + ) { - return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize); + return FSE_decompress_wksp_body_default( + dst, + dstCapacity, + cSrc, + cSrcSize, + maxLog, + workSpace, + wkspSize + ); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HIST_checkInput_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HIST_checkInput_e.cs index 4d6c1081..a36d7163 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HIST_checkInput_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HIST_checkInput_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum HIST_checkInput_e { trustInput, - checkMaxSymbolValue + checkMaxSymbolValue, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CStream_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CStream_t.cs index c28390d5..e00f59c8 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CStream_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CStream_t.cs @@ -7,6 +7,7 @@ namespace ZstdSharp.Unsafe public byte* startPtr; public byte* ptr; public byte* endPtr; + public unsafe struct _bitContainer_e__FixedBuffer { public nuint e0; @@ -19,4 +20,4 @@ namespace ZstdSharp.Unsafe public nuint e1; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CTableHeader.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CTableHeader.cs index b2d934c7..f89e380c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CTableHeader.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CTableHeader.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public byte maxSymbolValue; public fixed byte unused[6]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CompressWeightsWksp.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CompressWeightsWksp.cs index 0a1a294b..f3017cfa 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CompressWeightsWksp.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_CompressWeightsWksp.cs @@ -7,4 +7,4 @@ namespace ZstdSharp.Unsafe public fixed uint count[13]; public fixed short norm[13]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DEltX1.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DEltX1.cs index 6d907c47..1ce3290b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DEltX1.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DEltX1.cs @@ -9,4 +9,4 @@ namespace ZstdSharp.Unsafe public byte nbBits; public byte @byte; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DEltX2.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DEltX2.cs index 51accf23..534b8982 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DEltX2.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DEltX2.cs @@ -10,4 +10,4 @@ namespace ZstdSharp.Unsafe public byte nbBits; public byte length; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DecompressFastArgs.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DecompressFastArgs.cs index 0cebbd52..4add3e9d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DecompressFastArgs.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_DecompressFastArgs.cs @@ -22,6 +22,7 @@ namespace ZstdSharp.Unsafe public byte* ilowest; public byte* oend; public _iend_e__FixedBuffer iend; + public unsafe struct _ip_e__FixedBuffer { public byte* e0; @@ -46,4 +47,4 @@ namespace ZstdSharp.Unsafe public byte* e3; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_ReadDTableX1_Workspace.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_ReadDTableX1_Workspace.cs index e1377ed4..47e6cca5 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_ReadDTableX1_Workspace.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_ReadDTableX1_Workspace.cs @@ -8,4 +8,4 @@ namespace ZstdSharp.Unsafe public fixed byte symbols[256]; public fixed byte huffWeight[256]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_ReadDTableX2_Workspace.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_ReadDTableX2_Workspace.cs index 9bc3d35f..03dc19a9 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_ReadDTableX2_Workspace.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_ReadDTableX2_Workspace.cs @@ -10,6 +10,7 @@ namespace ZstdSharp.Unsafe public _sortedSymbol_e__FixedBuffer sortedSymbol; public fixed byte weightList[256]; public fixed uint calleeWksp[219]; + #if NET8_0_OR_GREATER [InlineArray(12)] public unsafe struct _rankVal_e__FixedBuffer @@ -304,4 +305,4 @@ namespace ZstdSharp.Unsafe } #endif } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_WriteCTableWksp.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_WriteCTableWksp.cs index 4e70d5dc..2c6f9c87 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_WriteCTableWksp.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_WriteCTableWksp.cs @@ -3,8 +3,9 @@ namespace ZstdSharp.Unsafe public unsafe struct HUF_WriteCTableWksp { public HUF_CompressWeightsWksp wksp; + /* precomputed conversion table */ public fixed byte bitsToWeight[13]; public fixed byte huffWeight[255]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_buildCTable_wksp_tables.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_buildCTable_wksp_tables.cs index c1394b01..f259c5b0 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_buildCTable_wksp_tables.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_buildCTable_wksp_tables.cs @@ -6,6 +6,7 @@ namespace ZstdSharp.Unsafe { public _huffNodeTbl_e__FixedBuffer huffNodeTbl; public _rankPosition_e__FixedBuffer rankPosition; + #if NET8_0_OR_GREATER [InlineArray(512)] public unsafe struct _huffNodeTbl_e__FixedBuffer @@ -736,4 +737,4 @@ namespace ZstdSharp.Unsafe } #endif } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_compress_tables_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_compress_tables_t.cs index a13ec931..14bf2714 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_compress_tables_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_compress_tables_t.cs @@ -7,6 +7,7 @@ namespace ZstdSharp.Unsafe public fixed uint count[256]; public _CTable_e__FixedBuffer CTable; public _wksps_e__Union wksps; + #if NET8_0_OR_GREATER [InlineArray(257)] public unsafe struct _CTable_e__FixedBuffer @@ -277,4 +278,4 @@ namespace ZstdSharp.Unsafe } #endif } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_flags_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_flags_e.cs index 9a890377..fe365c45 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_flags_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_flags_e.cs @@ -11,30 +11,35 @@ namespace ZstdSharp.Unsafe * Otherwise: Ignored. */ HUF_flags_bmi2 = 1 << 0, + /** * If set: Test possible table depths to find the one that produces the smallest header + encoded size. * If unset: Use heuristic to find the table depth. */ HUF_flags_optimalDepth = 1 << 1, + /** * If set: If the previous table can encode the input, always reuse the previous table. * If unset: If the previous table can encode the input, reuse the previous table if it results in a smaller output. */ HUF_flags_preferRepeat = 1 << 2, + /** * If set: Sample the input and check if the sample is uncompressible, if it is then don't attempt to compress. * If unset: Always histogram the entire input. */ HUF_flags_suspectUncompressible = 1 << 3, + /** * If set: Don't use assembly implementations * If unset: Allow using assembly implementations */ HUF_flags_disableAsm = 1 << 4, + /** * If set: Don't use the fast decoding loop, always use the fallback decoding loop. * If unset: Use the fast decoding loop when possible. */ - HUF_flags_disableFast = 1 << 5 + HUF_flags_disableFast = 1 << 5, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_nbStreams_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_nbStreams_e.cs index e4d5a86f..55a931ac 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_nbStreams_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_nbStreams_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum HUF_nbStreams_e { HUF_singleStream, - HUF_fourStreams + HUF_fourStreams, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_repeat.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_repeat.cs index 39ed82d8..2bae2cae 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_repeat.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HUF_repeat.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /**< Cannot use the previous table */ HUF_repeat_none, + /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */ HUF_repeat_check, + /**< Can use the previous table and it is assumed to be valid */ - HUF_repeat_valid + HUF_repeat_valid, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Hist.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Hist.cs index 7dce78ff..586b280d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Hist.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Hist.cs @@ -31,7 +31,12 @@ namespace ZstdSharp.Unsafe * @return : count of the most frequent symbol. * Note this function doesn't produce any error (i.e. it must succeed). */ - private static uint HIST_count_simple(uint* count, uint* maxSymbolValuePtr, void* src, nuint srcSize) + private static uint HIST_count_simple( + uint* count, + uint* maxSymbolValuePtr, + void* src, + nuint srcSize + ) { byte* ip = (byte*)src; byte* end = ip + srcSize; @@ -71,7 +76,14 @@ namespace ZstdSharp.Unsafe * `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) + 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; @@ -145,7 +157,9 @@ namespace ZstdSharp.Unsafe while (Counting1[maxSymbolValue] == 0) maxSymbolValue--; if (check != default && maxSymbolValue > *maxSymbolValuePtr) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxSymbolValue_tooSmall) + ); *maxSymbolValuePtr = maxSymbolValue; memmove(count, Counting1, countSize); } @@ -158,7 +172,14 @@ namespace ZstdSharp.Unsafe * `workSpace` is a writable buffer which must be 4-bytes aligned, * `workSpaceSize` must be >= HIST_WKSP_SIZE */ - private static nuint HIST_countFast_wksp(uint* count, uint* maxSymbolValuePtr, void* source, nuint sourceSize, void* workSpace, nuint workSpaceSize) + private 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); @@ -166,29 +187,69 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC)); if (workSpaceSize < 1024 * 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); + 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 */ - private static nuint HIST_count_wksp(uint* count, uint* maxSymbolValuePtr, void* source, nuint sourceSize, void* workSpace, nuint workSpaceSize) + private 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 * 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); + 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); + return HIST_countFast_wksp( + count, + maxSymbolValuePtr, + source, + sourceSize, + workSpace, + workSpaceSize + ); } /* fast variant (unsafe : won't check if src contains values beyond count[] limit) */ - private static nuint HIST_countFast(uint* count, uint* maxSymbolValuePtr, void* source, nuint sourceSize) + private 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, tmpCounters, sizeof(uint) * 1024); + return HIST_countFast_wksp( + count, + maxSymbolValuePtr, + source, + sourceSize, + tmpCounters, + sizeof(uint) * 1024 + ); } /*! HIST_count(): @@ -199,10 +260,22 @@ namespace ZstdSharp.Unsafe * or an error code, which can be tested using HIST_isError(). * note : if return == srcSize, there is only one symbol. */ - private static nuint HIST_count(uint* count, uint* maxSymbolValuePtr, void* src, nuint srcSize) + private 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, tmpCounters, sizeof(uint) * 1024); + return HIST_count_wksp( + count, + maxSymbolValuePtr, + src, + srcSize, + tmpCounters, + sizeof(uint) * 1024 + ); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HufCompress.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HufCompress.cs index a41274cd..15bfe260 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HufCompress.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HufCompress.cs @@ -1,11 +1,15 @@ -using static ZstdSharp.UnsafeHelper; using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { - private static void* HUF_alignUpWorkspace(void* workspace, nuint* workspaceSizePtr, nuint align) + private static void* HUF_alignUpWorkspace( + void* workspace, + nuint* workspaceSizePtr, + nuint align + ) { nuint mask = align - 1; nuint rem = (nuint)workspace & mask; @@ -27,21 +31,37 @@ namespace ZstdSharp.Unsafe } } - private static nuint HUF_compressWeights(void* dst, nuint dstSize, void* weightTable, nuint wtSize, void* workspace, nuint workspaceSize) + 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*)HUF_alignUpWorkspace(workspace, &workspaceSize, sizeof(uint)); + HUF_CompressWeightsWksp* wksp = (HUF_CompressWeightsWksp*)HUF_alignUpWorkspace( + workspace, + &workspaceSize, + sizeof(uint) + ); if (workspaceSize < (nuint)sizeof(HUF_CompressWeightsWksp)) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC)); if (wtSize <= 1) return 0; { /* never fails */ - uint maxCount = HIST_count_simple(wksp->count, &maxSymbolValue, weightTable, wtSize); + uint maxCount = HIST_count_simple( + wksp->count, + &maxSymbolValue, + weightTable, + wtSize + ); if (maxCount == wtSize) return 1; if (maxCount == 1) @@ -51,13 +71,26 @@ namespace ZstdSharp.Unsafe tableLog = FSE_optimalTableLog(tableLog, wtSize, maxSymbolValue); { /* useLowProbCount */ - nuint _var_err__ = FSE_normalizeCount(wksp->norm, tableLog, wksp->count, wtSize, maxSymbolValue, 0); + nuint _var_err__ = FSE_normalizeCount( + wksp->norm, + tableLog, + wksp->count, + wtSize, + maxSymbolValue, + 0 + ); if (ERR_isError(_var_err__)) return _var_err__; } { - nuint hSize = FSE_writeNCount(op, (nuint)(oend - op), wksp->norm, maxSymbolValue, tableLog); + nuint hSize = FSE_writeNCount( + op, + (nuint)(oend - op), + wksp->norm, + maxSymbolValue, + tableLog + ); if (ERR_isError(hSize)) return hSize; op += hSize; @@ -65,13 +98,26 @@ namespace ZstdSharp.Unsafe { /* Compress */ - nuint _var_err__ = FSE_buildCTable_wksp(wksp->CTable, wksp->norm, maxSymbolValue, tableLog, wksp->scratchBuffer, sizeof(uint) * 41); + nuint _var_err__ = FSE_buildCTable_wksp( + wksp->CTable, + wksp->norm, + maxSymbolValue, + tableLog, + wksp->scratchBuffer, + sizeof(uint) * 41 + ); if (ERR_isError(_var_err__)) return _var_err__; } { - nuint cSize = FSE_compress_usingCTable(op, (nuint)(oend - op), weightTable, wtSize, wksp->CTable); + nuint cSize = FSE_compress_usingCTable( + op, + (nuint)(oend - op), + weightTable, + wtSize, + wksp->CTable + ); if (ERR_isError(cSize)) return cSize; if (cSize == 0) @@ -145,12 +191,24 @@ namespace ZstdSharp.Unsafe memcpy(ctable, &header, (uint)sizeof(nuint)); } - private static nuint HUF_writeCTable_wksp(void* dst, nuint maxDstSize, nuint* CTable, uint maxSymbolValue, uint huffLog, void* workspace, nuint workspaceSize) + private static nuint HUF_writeCTable_wksp( + void* dst, + nuint maxDstSize, + nuint* CTable, + uint maxSymbolValue, + uint huffLog, + void* workspace, + nuint workspaceSize + ) { nuint* ct = CTable + 1; byte* op = (byte*)dst; uint n; - HUF_WriteCTableWksp* wksp = (HUF_WriteCTableWksp*)HUF_alignUpWorkspace(workspace, &workspaceSize, sizeof(uint)); + HUF_WriteCTableWksp* wksp = (HUF_WriteCTableWksp*)HUF_alignUpWorkspace( + workspace, + &workspaceSize, + sizeof(uint) + ); assert(HUF_readCTableHeader(CTable).maxSymbolValue == maxSymbolValue); assert(HUF_readCTableHeader(CTable).tableLog == huffLog); if (workspaceSize < (nuint)sizeof(HUF_WriteCTableWksp)) @@ -165,7 +223,14 @@ namespace ZstdSharp.Unsafe if (maxDstSize < 1) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); { - nuint hSize = HUF_compressWeights(op + 1, maxDstSize - 1, wksp->huffWeight, maxSymbolValue, &wksp->wksp, (nuint)sizeof(HUF_CompressWeightsWksp)); + nuint hSize = HUF_compressWeights( + op + 1, + maxDstSize - 1, + wksp->huffWeight, + maxSymbolValue, + &wksp->wksp, + (nuint)sizeof(HUF_CompressWeightsWksp) + ); if (ERR_isError(hSize)) return hSize; if (hSize > 1 && hSize < maxSymbolValue / 2) @@ -188,7 +253,13 @@ namespace ZstdSharp.Unsafe /** HUF_readCTable() : * Loading a CTable saved with HUF_writeCTable() */ - private static nuint HUF_readCTable(nuint* CTable, uint* maxSymbolValuePtr, void* src, nuint srcSize, uint* hasZeroWeights) + private static nuint HUF_readCTable( + nuint* CTable, + uint* maxSymbolValuePtr, + void* src, + nuint srcSize, + uint* hasZeroWeights + ) { /* init not required, even though some static analyzer may complain */ byte* huffWeight = stackalloc byte[256]; @@ -198,7 +269,15 @@ namespace ZstdSharp.Unsafe uint nbSymbols = 0; nuint* ct = CTable + 1; /* get symbol weights */ - nuint readSize = HUF_readStats(huffWeight, 255 + 1, rankVal, &nbSymbols, &tableLog, src, srcSize); + nuint readSize = HUF_readStats( + huffWeight, + 255 + 1, + rankVal, + &nbSymbols, + &tableLog, + src, + srcSize + ); if (ERR_isError(readSize)) return readSize; *hasZeroWeights = rankVal[0] > 0 ? 1U : 0U; @@ -209,7 +288,8 @@ namespace ZstdSharp.Unsafe *maxSymbolValuePtr = nbSymbols - 1; HUF_writeCTableHeader(CTable, tableLog, *maxSymbolValuePtr); { - uint n, nextRankStart = 0; + uint n, + nextRankStart = 0; for (n = 1; n <= tableLog; n++) { uint curr = nextRankStart; @@ -297,7 +377,11 @@ namespace ZstdSharp.Unsafe * respect targetNbBits. * @return The maximum number of bits of the Huffman tree after adjustment. */ - private static uint HUF_setMaxHeight(nodeElt_s* huffNode, uint lastNonNull, uint targetNbBits) + private static uint HUF_setMaxHeight( + nodeElt_s* huffNode, + uint lastNonNull, + uint targetNbBits + ) { uint largestBits = huffNode[lastNonNull].nbBits; if (largestBits <= targetNbBits) @@ -308,7 +392,9 @@ namespace ZstdSharp.Unsafe int n = (int)lastNonNull; while (huffNode[n].nbBits > targetNbBits) { - totalCost += (int)(baseCost - (uint)(1 << (int)(largestBits - huffNode[n].nbBits))); + totalCost += (int)( + baseCost - (uint)(1 << (int)(largestBits - huffNode[n].nbBits)) + ); huffNode[n].nbBits = (byte)targetNbBits; n--; } @@ -370,7 +456,10 @@ namespace ZstdSharp.Unsafe else { rankLast[nBitsToDecrease]--; - if (huffNode[rankLast[nBitsToDecrease]].nbBits != targetNbBits - nBitsToDecrease) + if ( + huffNode[rankLast[nBitsToDecrease]].nbBits + != targetNbBits - nBitsToDecrease + ) rankLast[nBitsToDecrease] = noSymbol; } } @@ -404,7 +493,9 @@ namespace ZstdSharp.Unsafe [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint HUF_getIndex(uint count) { - return count < 192 - 1 - 32 - 1 + ZSTD_highbit32(192 - 1 - 32 - 1) ? count : ZSTD_highbit32(count) + (192 - 1 - 32 - 1); + return count < 192 - 1 - 32 - 1 + ZSTD_highbit32(192 - 1 - 32 - 1) + ? count + : ZSTD_highbit32(count) + (192 - 1 - 32 - 1); } /* Helper swap function for HUF_quickSortPartition() */ @@ -513,7 +604,12 @@ namespace ZstdSharp.Unsafe * @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) + private static void HUF_sort( + nodeElt_s* huffNode, + uint* count, + uint maxSymbolValue, + rankPos* rankPosition + ) { uint n; uint maxSymbolValue1 = maxSymbolValue + 1; @@ -567,9 +663,11 @@ namespace ZstdSharp.Unsafe { nodeElt_s* huffNode0 = huffNode - 1; int nonNullRank; - int lowS, lowN; + int lowS, + lowN; int nodeNb = 255 + 1; - int n, nodeRoot; + int n, + nodeRoot; nonNullRank = (int)maxSymbolValue; while (huffNode[nonNullRank].count == 0) nonNullRank--; @@ -610,7 +708,13 @@ namespace ZstdSharp.Unsafe * @param maxSymbolValue The maximum symbol value. * @param maxNbBits The exact maximum number of bits used in the Huffman tree. */ - private static void HUF_buildCTableFromTree(nuint* CTable, nodeElt_s* huffNode, int nonNullRank, uint maxSymbolValue, uint maxNbBits) + private static void HUF_buildCTableFromTree( + nuint* CTable, + nodeElt_s* huffNode, + int nonNullRank, + uint maxSymbolValue, + uint maxNbBits + ) { nuint* ct = CTable + 1; /* fill result into ctable (val, nbBits) */ @@ -639,9 +743,21 @@ namespace ZstdSharp.Unsafe HUF_writeCTableHeader(CTable, maxNbBits, maxSymbolValue); } - private static nuint HUF_buildCTable_wksp(nuint* CTable, uint* count, uint maxSymbolValue, uint maxNbBits, void* workSpace, nuint wkspSize) + private static nuint HUF_buildCTable_wksp( + nuint* CTable, + uint* count, + uint maxSymbolValue, + uint maxNbBits, + void* workSpace, + nuint wkspSize + ) { - HUF_buildCTable_wksp_tables* wksp_tables = (HUF_buildCTable_wksp_tables*)HUF_alignUpWorkspace(workSpace, &wkspSize, sizeof(uint)); + HUF_buildCTable_wksp_tables* wksp_tables = + (HUF_buildCTable_wksp_tables*)HUF_alignUpWorkspace( + workSpace, + &wkspSize, + sizeof(uint) + ); nodeElt_s* huffNode0 = &wksp_tables->huffNodeTbl.e0; nodeElt_s* huffNode = huffNode0 + 1; int nonNullRank; @@ -661,7 +777,11 @@ namespace ZstdSharp.Unsafe return maxNbBits; } - private static nuint HUF_estimateCompressedSize(nuint* CTable, uint* count, uint maxSymbolValue) + private static nuint HUF_estimateCompressedSize( + nuint* CTable, + uint* count, + uint maxSymbolValue + ) { nuint* ct = CTable + 1; nuint nbBits = 0; @@ -700,13 +820,17 @@ namespace ZstdSharp.Unsafe * Initializes the bitstream. * @returns 0 or an error code. */ - private static nuint HUF_initCStream(ref HUF_CStream_t bitC, void* startPtr, nuint dstCapacity) + private static nuint HUF_initCStream( + ref HUF_CStream_t bitC, + void* startPtr, + nuint dstCapacity + ) { bitC = new HUF_CStream_t { startPtr = (byte*)startPtr, ptr = (byte*)startPtr, - endPtr = (byte*)startPtr + dstCapacity - sizeof(nuint) + endPtr = (byte*)startPtr + dstCapacity - sizeof(nuint), }; if (dstCapacity <= (nuint)sizeof(nuint)) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); @@ -724,7 +848,12 @@ namespace ZstdSharp.Unsafe * otherwise it must be 0. HUF_addBits() is faster when fast is set. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void HUF_addBits(ref nuint bitC_bitContainer_e0, ref nuint bitC_bitPos_e0, nuint elt, int kFast) + private static void HUF_addBits( + ref nuint bitC_bitContainer_e0, + ref nuint bitC_bitPos_e0, + nuint elt, + int kFast + ) { assert(HUF_getNbBits(elt) <= 12); bitC_bitContainer_e0 >>= (int)HUF_getNbBits(elt); @@ -745,7 +874,12 @@ namespace ZstdSharp.Unsafe * and zeros the bit container @ index 1. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void HUF_mergeIndex1(ref nuint bitC_bitContainer_e0, ref nuint bitC_bitPos_e0, ref nuint bitC_bitContainer_e1, ref nuint bitC_bitPos_e1) + private static void HUF_mergeIndex1( + ref nuint bitC_bitContainer_e0, + ref nuint bitC_bitPos_e0, + ref nuint bitC_bitContainer_e1, + ref nuint bitC_bitPos_e1 + ) { assert((bitC_bitPos_e1 & 0xFF) < (nuint)(sizeof(nuint) * 8)); bitC_bitContainer_e0 >>= (int)(bitC_bitPos_e1 & 0xFF); @@ -762,7 +896,13 @@ namespace ZstdSharp.Unsafe * the bit container will not overflow. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void HUF_flushBits(ref nuint bitC_bitContainer_e0, ref nuint bitC_bitPos_e0, ref byte* bitC_ptr, byte* bitC_endPtr, int kFast) + private static void HUF_flushBits( + ref nuint bitC_bitContainer_e0, + ref nuint bitC_bitPos_e0, + ref byte* bitC_ptr, + byte* bitC_endPtr, + int kFast + ) { /* The upper bits of bitPos are noisy, so we must mask by 0xFF. */ nuint nbBits = bitC_bitPos_e0 & 0xFF; @@ -797,7 +937,13 @@ namespace ZstdSharp.Unsafe private static nuint HUF_closeCStream(ref HUF_CStream_t bitC) { HUF_addBits(ref bitC.bitContainer.e0, ref bitC.bitPos.e0, HUF_endMark(), 0); - HUF_flushBits(ref bitC.bitContainer.e0, ref bitC.bitPos.e0, ref bitC.ptr, bitC.endPtr, 0); + HUF_flushBits( + ref bitC.bitContainer.e0, + ref bitC.bitPos.e0, + ref bitC.ptr, + bitC.endPtr, + 0 + ); { nuint nbBits = bitC.bitPos.e0 & 0xFF; if (bitC.ptr >= bitC.endPtr) @@ -807,13 +953,27 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void HUF_encodeSymbol(ref nuint bitCPtr_bitContainer_e0, ref nuint bitCPtr_bitPos_e0, uint symbol, nuint* CTable, int fast) + private static void HUF_encodeSymbol( + ref nuint bitCPtr_bitContainer_e0, + ref nuint bitCPtr_bitPos_e0, + uint symbol, + nuint* CTable, + int fast + ) { HUF_addBits(ref bitCPtr_bitContainer_e0, ref bitCPtr_bitPos_e0, CTable[symbol], fast); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void HUF_compress1X_usingCTable_internal_body_loop(ref HUF_CStream_t bitC, byte* ip, nuint srcSize, nuint* ct, int kUnroll, int kFastFlush, int kLastFast) + private static void HUF_compress1X_usingCTable_internal_body_loop( + ref HUF_CStream_t bitC, + byte* ip, + nuint srcSize, + nuint* ct, + int kUnroll, + int kFastFlush, + int kLastFast + ) { byte* bitC_ptr = bitC.ptr; byte* bitC_endPtr = bitC.endPtr; @@ -831,7 +991,13 @@ namespace ZstdSharp.Unsafe HUF_encodeSymbol(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ip[--n], ct, 0); } - HUF_flushBits(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ref bitC_ptr, bitC_endPtr, kFastFlush); + HUF_flushBits( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ref bitC_ptr, + bitC_endPtr, + kFastFlush + ); } assert(n % kUnroll == 0); @@ -840,11 +1006,29 @@ namespace ZstdSharp.Unsafe int u; for (u = 1; u < kUnroll; ++u) { - HUF_encodeSymbol(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ip[n - u], ct, 1); + HUF_encodeSymbol( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ip[n - u], + ct, + 1 + ); } - HUF_encodeSymbol(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ip[n - kUnroll], ct, kLastFast); - HUF_flushBits(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ref bitC_ptr, bitC_endPtr, kFastFlush); + HUF_encodeSymbol( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ip[n - kUnroll], + ct, + kLastFast + ); + HUF_flushBits( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ref bitC_ptr, + bitC_endPtr, + kFastFlush + ); n -= kUnroll; } @@ -855,20 +1039,61 @@ namespace ZstdSharp.Unsafe int u; for (u = 1; u < kUnroll; ++u) { - HUF_encodeSymbol(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ip[n - u], ct, 1); + HUF_encodeSymbol( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ip[n - u], + ct, + 1 + ); } - HUF_encodeSymbol(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ip[n - kUnroll], ct, kLastFast); - HUF_flushBits(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ref bitC_ptr, bitC_endPtr, kFastFlush); + HUF_encodeSymbol( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ip[n - kUnroll], + ct, + kLastFast + ); + HUF_flushBits( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ref bitC_ptr, + bitC_endPtr, + kFastFlush + ); HUF_zeroIndex1(ref bitC_bitContainer_e1, ref bitC_bitPos_e1); for (u = 1; u < kUnroll; ++u) { - HUF_encodeSymbol(ref bitC_bitContainer_e1, ref bitC_bitPos_e1, ip[n - kUnroll - u], ct, 1); + HUF_encodeSymbol( + ref bitC_bitContainer_e1, + ref bitC_bitPos_e1, + ip[n - kUnroll - u], + ct, + 1 + ); } - HUF_encodeSymbol(ref bitC_bitContainer_e1, ref bitC_bitPos_e1, ip[n - kUnroll - kUnroll], ct, kLastFast); - HUF_mergeIndex1(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ref bitC_bitContainer_e1, ref bitC_bitPos_e1); - HUF_flushBits(ref bitC_bitContainer_e0, ref bitC_bitPos_e0, ref bitC_ptr, bitC_endPtr, kFastFlush); + HUF_encodeSymbol( + ref bitC_bitContainer_e1, + ref bitC_bitPos_e1, + ip[n - kUnroll - kUnroll], + ct, + kLastFast + ); + HUF_mergeIndex1( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ref bitC_bitContainer_e1, + ref bitC_bitPos_e1 + ); + HUF_flushBits( + ref bitC_bitContainer_e0, + ref bitC_bitPos_e0, + ref bitC_ptr, + bitC_endPtr, + kFastFlush + ); } assert(n == 0); @@ -890,7 +1115,13 @@ namespace ZstdSharp.Unsafe return (srcSize * tableLog >> 3) + 8; } - private static nuint HUF_compress1X_usingCTable_internal_body(void* dst, nuint dstSize, void* src, nuint srcSize, nuint* CTable) + private static nuint HUF_compress1X_usingCTable_internal_body( + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + nuint* CTable + ) { uint tableLog = HUF_readCTableHeader(CTable).tableLog; nuint* ct = CTable + 1; @@ -909,7 +1140,15 @@ namespace ZstdSharp.Unsafe } if (dstSize < HUF_tightCompressBound(srcSize, tableLog) || tableLog > 11) - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, MEM_32bits ? 2 : 4, 0, 0); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + MEM_32bits ? 2 : 4, + 0, + 0 + ); else { if (MEM_32bits) @@ -917,16 +1156,40 @@ namespace ZstdSharp.Unsafe switch (tableLog) { case 11: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 2, 1, 0); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 2, + 1, + 0 + ); break; case 10: case 9: case 8: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 2, 1, 1); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 2, + 1, + 1 + ); break; case 7: default: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 3, 1, 1); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 3, + 1, + 1 + ); break; } } @@ -935,23 +1198,71 @@ namespace ZstdSharp.Unsafe switch (tableLog) { case 11: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 5, 1, 0); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 5, + 1, + 0 + ); break; case 10: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 5, 1, 1); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 5, + 1, + 1 + ); break; case 9: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 6, 1, 0); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 6, + 1, + 0 + ); break; case 8: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 7, 1, 0); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 7, + 1, + 0 + ); break; case 7: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 8, 1, 0); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 8, + 1, + 0 + ); break; case 6: default: - HUF_compress1X_usingCTable_internal_body_loop(ref bitC, ip, srcSize, ct, 9, 1, 1); + HUF_compress1X_usingCTable_internal_body_loop( + ref bitC, + ip, + srcSize, + ct, + 9, + 1, + 1 + ); break; } } @@ -961,7 +1272,14 @@ namespace ZstdSharp.Unsafe return HUF_closeCStream(ref bitC); } - private static nuint HUF_compress1X_usingCTable_internal(void* dst, nuint dstSize, void* src, nuint srcSize, nuint* CTable, int flags) + private static nuint HUF_compress1X_usingCTable_internal( + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + nuint* CTable, + int flags + ) { return HUF_compress1X_usingCTable_internal_body(dst, dstSize, src, srcSize, CTable); } @@ -969,12 +1287,26 @@ namespace ZstdSharp.Unsafe /* ====================== */ /* single stream variants */ /* ====================== */ - private static nuint HUF_compress1X_usingCTable(void* dst, nuint dstSize, void* src, nuint srcSize, nuint* CTable, int flags) + private static nuint HUF_compress1X_usingCTable( + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + nuint* CTable, + int flags + ) { return HUF_compress1X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, flags); } - private static nuint HUF_compress4X_usingCTable_internal(void* dst, nuint dstSize, void* src, nuint srcSize, nuint* CTable, int flags) + private static nuint HUF_compress4X_usingCTable_internal( + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + nuint* CTable, + int flags + ) { /* first 3 segments */ nuint segmentSize = (srcSize + 3) / 4; @@ -990,7 +1322,14 @@ namespace ZstdSharp.Unsafe op += 6; assert(op <= oend); { - nuint cSize = HUF_compress1X_usingCTable_internal(op, (nuint)(oend - op), ip, segmentSize, CTable, flags); + nuint cSize = HUF_compress1X_usingCTable_internal( + op, + (nuint)(oend - op), + ip, + segmentSize, + CTable, + flags + ); if (ERR_isError(cSize)) return cSize; if (cSize == 0 || cSize > 65535) @@ -1002,7 +1341,14 @@ namespace ZstdSharp.Unsafe ip += segmentSize; assert(op <= oend); { - nuint cSize = HUF_compress1X_usingCTable_internal(op, (nuint)(oend - op), ip, segmentSize, CTable, flags); + nuint cSize = HUF_compress1X_usingCTable_internal( + op, + (nuint)(oend - op), + ip, + segmentSize, + CTable, + flags + ); if (ERR_isError(cSize)) return cSize; if (cSize == 0 || cSize > 65535) @@ -1014,7 +1360,14 @@ namespace ZstdSharp.Unsafe ip += segmentSize; assert(op <= oend); { - nuint cSize = HUF_compress1X_usingCTable_internal(op, (nuint)(oend - op), ip, segmentSize, CTable, flags); + nuint cSize = HUF_compress1X_usingCTable_internal( + op, + (nuint)(oend - op), + ip, + segmentSize, + CTable, + flags + ); if (ERR_isError(cSize)) return cSize; if (cSize == 0 || cSize > 65535) @@ -1027,7 +1380,14 @@ namespace ZstdSharp.Unsafe assert(op <= oend); assert(ip <= iend); { - nuint cSize = HUF_compress1X_usingCTable_internal(op, (nuint)(oend - op), ip, (nuint)(iend - ip), CTable, flags); + nuint cSize = HUF_compress1X_usingCTable_internal( + op, + (nuint)(oend - op), + ip, + (nuint)(iend - ip), + CTable, + flags + ); if (ERR_isError(cSize)) return cSize; if (cSize == 0 || cSize > 65535) @@ -1038,14 +1398,47 @@ namespace ZstdSharp.Unsafe return (nuint)(op - ostart); } - private static nuint HUF_compress4X_usingCTable(void* dst, nuint dstSize, void* src, nuint srcSize, nuint* CTable, int flags) + private static nuint HUF_compress4X_usingCTable( + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + nuint* CTable, + int flags + ) { return HUF_compress4X_usingCTable_internal(dst, dstSize, src, srcSize, CTable, flags); } - private static nuint HUF_compressCTable_internal(byte* ostart, byte* op, byte* oend, void* src, nuint srcSize, HUF_nbStreams_e nbStreams, nuint* CTable, int flags) + private static nuint HUF_compressCTable_internal( + byte* ostart, + byte* op, + byte* oend, + void* src, + nuint srcSize, + HUF_nbStreams_e nbStreams, + nuint* CTable, + int flags + ) { - nuint cSize = nbStreams == HUF_nbStreams_e.HUF_singleStream ? HUF_compress1X_usingCTable_internal(op, (nuint)(oend - op), src, srcSize, CTable, flags) : HUF_compress4X_usingCTable_internal(op, (nuint)(oend - op), src, srcSize, CTable, flags); + nuint cSize = + nbStreams == HUF_nbStreams_e.HUF_singleStream + ? HUF_compress1X_usingCTable_internal( + op, + (nuint)(oend - op), + src, + srcSize, + CTable, + flags + ) + : HUF_compress4X_usingCTable_internal( + op, + (nuint)(oend - op), + src, + srcSize, + CTable, + flags + ); if (ERR_isError(cSize)) { return cSize; @@ -1096,7 +1489,16 @@ namespace ZstdSharp.Unsafe return minBitsSymbols; } - private static uint HUF_optimalTableLog(uint maxTableLog, nuint srcSize, uint maxSymbolValue, void* workSpace, nuint wkspSize, nuint* table, uint* count, int flags) + private static uint HUF_optimalTableLog( + uint maxTableLog, + nuint srcSize, + uint maxSymbolValue, + void* workSpace, + nuint wkspSize, + nuint* table, + uint* count, + int flags + ) { assert(srcSize > 1); assert(wkspSize >= (nuint)sizeof(HUF_buildCTable_wksp_tables)); @@ -1108,20 +1510,37 @@ namespace ZstdSharp.Unsafe { byte* dst = (byte*)workSpace + sizeof(HUF_WriteCTableWksp); nuint dstSize = wkspSize - (nuint)sizeof(HUF_WriteCTableWksp); - nuint hSize, newSize; + nuint hSize, + newSize; uint symbolCardinality = HUF_cardinality(count, maxSymbolValue); uint minTableLog = HUF_minTableLog(symbolCardinality); nuint optSize = unchecked((nuint)~0) - 1; - uint optLog = maxTableLog, optLogGuess; + uint optLog = maxTableLog, + optLogGuess; for (optLogGuess = minTableLog; optLogGuess <= maxTableLog; optLogGuess++) { { - nuint maxBits = HUF_buildCTable_wksp(table, count, maxSymbolValue, optLogGuess, workSpace, wkspSize); + nuint maxBits = HUF_buildCTable_wksp( + table, + count, + maxSymbolValue, + optLogGuess, + workSpace, + wkspSize + ); if (ERR_isError(maxBits)) continue; if (maxBits < optLogGuess && optLogGuess > minTableLog) break; - hSize = HUF_writeCTable_wksp(dst, dstSize, table, maxSymbolValue, (uint)maxBits, workSpace, wkspSize); + hSize = HUF_writeCTable_wksp( + dst, + dstSize, + table, + maxSymbolValue, + (uint)maxBits, + workSpace, + wkspSize + ); } if (ERR_isError(hSize)) @@ -1147,9 +1566,26 @@ namespace ZstdSharp.Unsafe /* HUF_compress_internal() : * `workSpace_align4` must be aligned on 4-bytes boundaries, * and occupies the same space as a table of HUF_WORKSPACE_SIZE_U64 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, nuint wkspSize, nuint* oldHufTable, HUF_repeat* repeat, int flags) + private static nuint HUF_compress_internal( + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + uint maxSymbolValue, + uint huffLog, + HUF_nbStreams_e nbStreams, + void* workSpace, + nuint wkspSize, + nuint* oldHufTable, + HUF_repeat* repeat, + int flags + ) { - HUF_compress_tables_t* table = (HUF_compress_tables_t*)HUF_alignUpWorkspace(workSpace, &wkspSize, sizeof(ulong)); + HUF_compress_tables_t* table = (HUF_compress_tables_t*)HUF_alignUpWorkspace( + workSpace, + &wkspSize, + sizeof(ulong) + ); byte* ostart = (byte*)dst; byte* oend = ostart + dstSize; byte* op = ostart; @@ -1169,17 +1605,38 @@ namespace ZstdSharp.Unsafe maxSymbolValue = 255; if (huffLog == 0) huffLog = 11; - if ((flags & (int)HUF_flags_e.HUF_flags_preferRepeat) != 0 && repeat != null && *repeat == HUF_repeat.HUF_repeat_valid) + if ( + (flags & (int)HUF_flags_e.HUF_flags_preferRepeat) != 0 + && repeat != null + && *repeat == HUF_repeat.HUF_repeat_valid + ) { - return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, oldHufTable, flags); + return HUF_compressCTable_internal( + ostart, + op, + oend, + src, + srcSize, + nbStreams, + oldHufTable, + flags + ); } - if ((flags & (int)HUF_flags_e.HUF_flags_suspectUncompressible) != 0 && srcSize >= 4096 * 10) + if ( + (flags & (int)HUF_flags_e.HUF_flags_suspectUncompressible) != 0 + && srcSize >= 4096 * 10 + ) { nuint largestTotal = 0; { uint maxSymbolValueBegin = maxSymbolValue; - nuint largestBegin = HIST_count_simple(table->count, &maxSymbolValueBegin, (byte*)src, 4096); + nuint largestBegin = HIST_count_simple( + table->count, + &maxSymbolValueBegin, + (byte*)src, + 4096 + ); if (ERR_isError(largestBegin)) return largestBegin; largestTotal += largestBegin; @@ -1187,7 +1644,12 @@ namespace ZstdSharp.Unsafe { uint maxSymbolValueEnd = maxSymbolValue; - nuint largestEnd = HIST_count_simple(table->count, &maxSymbolValueEnd, (byte*)src + srcSize - 4096, 4096); + nuint largestEnd = HIST_count_simple( + table->count, + &maxSymbolValueEnd, + (byte*)src + srcSize - 4096, + 4096 + ); if (ERR_isError(largestEnd)) return largestEnd; largestTotal += largestEnd; @@ -1198,7 +1660,14 @@ namespace ZstdSharp.Unsafe } { - nuint largest = HIST_count_wksp(table->count, &maxSymbolValue, (byte*)src, srcSize, table->wksps.hist_wksp, sizeof(uint) * 1024); + nuint largest = HIST_count_wksp( + table->count, + &maxSymbolValue, + (byte*)src, + srcSize, + table->wksps.hist_wksp, + sizeof(uint) * 1024 + ); if (ERR_isError(largest)) return largest; if (largest == srcSize) @@ -1211,19 +1680,52 @@ namespace ZstdSharp.Unsafe return 0; } - if (repeat != null && *repeat == HUF_repeat.HUF_repeat_check && HUF_validateCTable(oldHufTable, table->count, maxSymbolValue) == 0) + if ( + repeat != null + && *repeat == HUF_repeat.HUF_repeat_check + && HUF_validateCTable(oldHufTable, table->count, maxSymbolValue) == 0 + ) { *repeat = HUF_repeat.HUF_repeat_none; } - if ((flags & (int)HUF_flags_e.HUF_flags_preferRepeat) != 0 && repeat != null && *repeat != HUF_repeat.HUF_repeat_none) + if ( + (flags & (int)HUF_flags_e.HUF_flags_preferRepeat) != 0 + && repeat != null + && *repeat != HUF_repeat.HUF_repeat_none + ) { - return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, oldHufTable, flags); + return HUF_compressCTable_internal( + ostart, + op, + oend, + src, + srcSize, + nbStreams, + oldHufTable, + flags + ); } - huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue, &table->wksps, (nuint)sizeof(_wksps_e__Union), &table->CTable.e0, table->count, flags); + huffLog = HUF_optimalTableLog( + huffLog, + srcSize, + maxSymbolValue, + &table->wksps, + (nuint)sizeof(_wksps_e__Union), + &table->CTable.e0, + table->count, + flags + ); { - nuint maxBits = HUF_buildCTable_wksp(&table->CTable.e0, table->count, maxSymbolValue, huffLog, &table->wksps.buildCTable_wksp, (nuint)sizeof(HUF_buildCTable_wksp_tables)); + nuint maxBits = HUF_buildCTable_wksp( + &table->CTable.e0, + table->count, + maxSymbolValue, + huffLog, + &table->wksps.buildCTable_wksp, + (nuint)sizeof(HUF_buildCTable_wksp_tables) + ); { nuint _var_err__ = maxBits; if (ERR_isError(_var_err__)) @@ -1234,16 +1736,41 @@ namespace ZstdSharp.Unsafe } { - nuint hSize = HUF_writeCTable_wksp(op, dstSize, &table->CTable.e0, maxSymbolValue, huffLog, &table->wksps.writeCTable_wksp, (nuint)sizeof(HUF_WriteCTableWksp)); + nuint hSize = HUF_writeCTable_wksp( + op, + dstSize, + &table->CTable.e0, + maxSymbolValue, + huffLog, + &table->wksps.writeCTable_wksp, + (nuint)sizeof(HUF_WriteCTableWksp) + ); if (ERR_isError(hSize)) return hSize; if (repeat != null && *repeat != HUF_repeat.HUF_repeat_none) { - nuint oldSize = HUF_estimateCompressedSize(oldHufTable, table->count, maxSymbolValue); - nuint newSize = HUF_estimateCompressedSize(&table->CTable.e0, table->count, maxSymbolValue); + nuint oldSize = HUF_estimateCompressedSize( + oldHufTable, + table->count, + maxSymbolValue + ); + nuint newSize = HUF_estimateCompressedSize( + &table->CTable.e0, + table->count, + maxSymbolValue + ); if (oldSize <= hSize + newSize || hSize + 12 >= srcSize) { - return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, oldHufTable, flags); + return HUF_compressCTable_internal( + ostart, + op, + oend, + src, + srcSize, + nbStreams, + oldHufTable, + flags + ); } } @@ -1262,7 +1789,16 @@ namespace ZstdSharp.Unsafe memcpy(oldHufTable, &table->CTable.e0, sizeof(ulong) * 257); } - return HUF_compressCTable_internal(ostart, op, oend, src, srcSize, nbStreams, &table->CTable.e0, flags); + return HUF_compressCTable_internal( + ostart, + op, + oend, + src, + srcSize, + nbStreams, + &table->CTable.e0, + flags + ); } /** HUF_compress1X_repeat() : @@ -1271,18 +1807,68 @@ namespace ZstdSharp.Unsafe * 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. * If suspectUncompressible then some sampling checks will be run to potentially skip huffman coding */ - private static nuint HUF_compress1X_repeat(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog, void* workSpace, nuint wkspSize, nuint* hufTable, HUF_repeat* repeat, int flags) + private static nuint HUF_compress1X_repeat( + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + uint maxSymbolValue, + uint huffLog, + void* workSpace, + nuint wkspSize, + nuint* hufTable, + HUF_repeat* repeat, + int flags + ) { - return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, HUF_nbStreams_e.HUF_singleStream, workSpace, wkspSize, hufTable, repeat, flags); + return HUF_compress_internal( + dst, + dstSize, + src, + srcSize, + maxSymbolValue, + huffLog, + HUF_nbStreams_e.HUF_singleStream, + workSpace, + wkspSize, + hufTable, + repeat, + flags + ); } /* HUF_compress4X_repeat(): * compress input using 4 streams. * consider skipping quickly * reuse an existing huffman compression table */ - private static nuint HUF_compress4X_repeat(void* dst, nuint dstSize, void* src, nuint srcSize, uint maxSymbolValue, uint huffLog, void* workSpace, nuint wkspSize, nuint* hufTable, HUF_repeat* repeat, int flags) + private static nuint HUF_compress4X_repeat( + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + uint maxSymbolValue, + uint huffLog, + void* workSpace, + nuint wkspSize, + nuint* hufTable, + HUF_repeat* repeat, + int flags + ) { - return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, HUF_nbStreams_e.HUF_fourStreams, workSpace, wkspSize, hufTable, repeat, flags); + return HUF_compress_internal( + dst, + dstSize, + src, + srcSize, + maxSymbolValue, + huffLog, + HUF_nbStreams_e.HUF_fourStreams, + workSpace, + wkspSize, + hufTable, + repeat, + flags + ); } } } diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/HufDecompress.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/HufDecompress.cs index fc9492c4..e3a2048d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/HufDecompress.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/HufDecompress.cs @@ -1,6 +1,6 @@ -using static ZstdSharp.UnsafeHelper; using System; using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -29,7 +29,14 @@ namespace ZstdSharp.Unsafe * 0 if the fallback implementation should be used. * Or an error code on failure. */ - private static nuint HUF_DecompressFastArgs_init(HUF_DecompressFastArgs* args, void* dst, nuint dstSize, void* src, nuint srcSize, uint* DTable) + private static nuint HUF_DecompressFastArgs_init( + HUF_DecompressFastArgs* args, + void* dst, + nuint dstSize, + void* src, + nuint srcSize, + uint* DTable + ) { void* dt = DTable + 1; uint dtLog = HUF_getDTableDesc(DTable).tableLog; @@ -79,7 +86,12 @@ namespace ZstdSharp.Unsafe return 1; } - private static nuint HUF_initRemainingDStream(BIT_DStream_t* bit, HUF_DecompressFastArgs* args, int stream, byte* segmentEnd) + private static nuint HUF_initRemainingDStream( + BIT_DStream_t* bit, + HUF_DecompressFastArgs* args, + int stream, + byte* segmentEnd + ) { if ((&args->op.e0)[stream] > segmentEnd) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); @@ -121,7 +133,13 @@ namespace ZstdSharp.Unsafe * If tableLog > targetTableLog this is a no-op. * @returns New tableLog */ - private static uint HUF_rescaleStats(byte* huffWeight, uint* rankVal, uint nbSymbols, uint tableLog, uint targetTableLog) + private static uint HUF_rescaleStats( + byte* huffWeight, + uint* rankVal, + uint nbSymbols, + uint tableLog, + uint targetTableLog + ) { if (tableLog > targetTableLog) return tableLog; @@ -148,7 +166,14 @@ namespace ZstdSharp.Unsafe return targetTableLog; } - private static nuint HUF_readDTableX1_wksp(uint* DTable, void* src, nuint srcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_readDTableX1_wksp( + uint* DTable, + void* src, + nuint srcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { uint tableLog = 0; uint nbSymbols = 0; @@ -158,14 +183,31 @@ namespace ZstdSharp.Unsafe 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(wksp->huffWeight, 255 + 1, wksp->rankVal, &nbSymbols, &tableLog, src, srcSize, wksp->statsWksp, sizeof(uint) * 219, flags); + iSize = HUF_readStats_wksp( + wksp->huffWeight, + 255 + 1, + wksp->rankVal, + &nbSymbols, + &tableLog, + src, + srcSize, + wksp->statsWksp, + sizeof(uint) * 219, + flags + ); if (ERR_isError(iSize)) return iSize; { DTableDesc dtd = HUF_getDTableDesc(DTable); uint maxTableLog = (uint)(dtd.maxTableLog + 1); uint targetTableLog = maxTableLog < 11 ? maxTableLog : 11; - tableLog = HUF_rescaleStats(wksp->huffWeight, wksp->rankVal, nbSymbols, tableLog, targetTableLog); + tableLog = HUF_rescaleStats( + wksp->huffWeight, + wksp->rankVal, + nbSymbols, + tableLog, + targetTableLog + ); if (tableLog > (uint)(dtd.maxTableLog + 1)) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge)); dtd.tableType = 0; @@ -296,12 +338,21 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint HUF_decodeStreamX1(byte* p, BIT_DStream_t* bitDPtr, byte* pEnd, HUF_DEltX1* dt, uint dtLog) + private static nuint HUF_decodeStreamX1( + byte* p, + BIT_DStream_t* bitDPtr, + byte* pEnd, + HUF_DEltX1* dt, + uint dtLog + ) { byte* pStart = p; if (pEnd - p > 3) { - while (BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished && p < pEnd - 3) + while ( + BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished + && p < pEnd - 3 + ) { if (MEM_64bits) *p++ = HUF_decodeSymbolX1(bitDPtr, dt, dtLog); @@ -317,7 +368,10 @@ namespace ZstdSharp.Unsafe } if (MEM_32bits) - while (BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished && p < pEnd) + 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); @@ -325,7 +379,13 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint HUF_decompress1X1_usingDTable_internal_body(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + private static nuint HUF_decompress1X1_usingDTable_internal_body( + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable + ) { byte* op = (byte*)dst; byte* oend = ZSTD_maybeNullPtrAdd(op, (nint)dstSize); @@ -351,7 +411,13 @@ namespace ZstdSharp.Unsafe * @dstSize >= 6 */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint HUF_decompress4X1_usingDTable_internal_body(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + 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)); @@ -420,7 +486,7 @@ namespace ZstdSharp.Unsafe if ((nuint)(oend - op4) >= (nuint)sizeof(nuint)) { - for (; (endSignal & (uint)(op4 < olimit ? 1 : 0)) != 0;) + for (; (endSignal & (uint)(op4 < olimit ? 1 : 0)) != 0; ) { if (MEM_64bits) *op1++ = HUF_decodeSymbolX1(&bitD1, dt, dtLog); @@ -446,10 +512,26 @@ namespace ZstdSharp.Unsafe *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; + 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; } } @@ -464,25 +546,54 @@ namespace ZstdSharp.Unsafe 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); + 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 unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } return dstSize; } } - private static nuint HUF_decompress4X1_usingDTable_internal_default(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* 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); + return HUF_decompress4X1_usingDTable_internal_body( + dst, + dstSize, + cSrc, + cSrcSize, + DTable + ); } - private static void HUF_decompress4X1_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs* args) + private static void HUF_decompress4X1_usingDTable_internal_fast_c_loop( + HUF_DecompressFastArgs* args + ) { - ulong bits0, bits1, bits2, bits3; - byte* ip0, ip1, ip2, ip3; - byte* op0, op1, op2, op3; + ulong bits0, + bits1, + bits2, + bits3; + byte* ip0, + ip1, + ip2, + ip3; + byte* op0, + op1, + op2, + op3; ushort* dtable = (ushort*)args->dt; byte* oend = args->oend; byte* ilowest = args->ilowest; @@ -759,11 +870,10 @@ namespace ZstdSharp.Unsafe bits3 <<= nbBits; } } - } - while (op3 < olimit); + } while (op3 < olimit); } - _out: + _out: args->bits[0] = bits0; args->bits[1] = bits1; args->bits[2] = bits2; @@ -783,14 +893,28 @@ namespace ZstdSharp.Unsafe * 0 if the fallback implementation should be used * An error if an error occurred */ - private static nuint HUF_decompress4X1_usingDTable_internal_fast(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, void* loopFn) + private static nuint HUF_decompress4X1_usingDTable_internal_fast( + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable, + void* loopFn + ) { void* dt = DTable + 1; byte* ilowest = (byte*)cSrc; byte* oend = ZSTD_maybeNullPtrAdd((byte*)dst, (nint)dstSize); HUF_DecompressFastArgs args; { - nuint ret = HUF_DecompressFastArgs_init(&args, dst, dstSize, cSrc, cSrcSize, DTable); + nuint ret = HUF_DecompressFastArgs_init( + &args, + dst, + dstSize, + cSrc, + cSrcSize, + DTable + ); { nuint err_code = ret; if (ERR_isError(err_code)) @@ -832,9 +956,17 @@ namespace ZstdSharp.Unsafe } } - (&args.op.e0)[i] += HUF_decodeStreamX1((&args.op.e0)[i], &bit, segmentEnd, (HUF_DEltX1*)dt, 11); + (&args.op.e0)[i] += HUF_decodeStreamX1( + (&args.op.e0)[i], + &bit, + segmentEnd, + (HUF_DEltX1*)dt, + 11 + ); if ((&args.op.e0)[i] != segmentEnd) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } } @@ -842,26 +974,72 @@ namespace ZstdSharp.Unsafe return dstSize; } - private static nuint HUF_decompress1X1_usingDTable_internal(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, int flags) + private static nuint HUF_decompress1X1_usingDTable_internal( + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable, + int flags + ) { - return HUF_decompress1X1_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + return HUF_decompress1X1_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 flags) + private static nuint HUF_decompress4X1_usingDTable_internal( + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable, + int flags + ) { - void* fallbackFn = (delegate* managed)(&HUF_decompress4X1_usingDTable_internal_default); - void* loopFn = (delegate* managed)(&HUF_decompress4X1_usingDTable_internal_fast_c_loop); + void* fallbackFn = (delegate* managed)( + &HUF_decompress4X1_usingDTable_internal_default + ); + void* loopFn = (delegate* managed)( + &HUF_decompress4X1_usingDTable_internal_fast_c_loop + ); if ((flags & (int)HUF_flags_e.HUF_flags_disableFast) == 0) { - nuint ret = HUF_decompress4X1_usingDTable_internal_fast(dst, dstSize, cSrc, cSrcSize, DTable, loopFn); + nuint ret = HUF_decompress4X1_usingDTable_internal_fast( + dst, + dstSize, + cSrc, + cSrcSize, + DTable, + loopFn + ); if (ret != 0) return ret; } - return ((delegate* managed)fallbackFn)(dst, dstSize, cSrc, cSrcSize, DTable); + return ((delegate* managed)fallbackFn)( + dst, + dstSize, + cSrc, + cSrcSize, + DTable + ); } - private static nuint HUF_decompress4X1_DCtx_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_decompress4X1_DCtx_wksp( + uint* dctx, + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { byte* ip = (byte*)cSrc; nuint hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize, flags); @@ -928,7 +1106,15 @@ namespace ZstdSharp.Unsafe * @param level The level in the table. Must be 1 or 2. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void HUF_fillDTableX2ForWeight(HUF_DEltX2* DTableRank, sortedSymbol_t* begin, sortedSymbol_t* end, uint nbBits, uint tableLog, ushort baseSeq, int level) + private static void HUF_fillDTableX2ForWeight( + HUF_DEltX2* DTableRank, + sortedSymbol_t* begin, + sortedSymbol_t* end, + uint nbBits, + uint tableLog, + ushort baseSeq, + int level + ) { /* quiet static-analyzer */ uint length = 1U << (int)(tableLog - nbBits & 0x1F); @@ -997,7 +1183,18 @@ namespace ZstdSharp.Unsafe /* HUF_fillDTableX2Level2() : * `rankValOrigin` must be a table of at least (HUF_TABLELOG_MAX + 1) U32 */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, uint targetLog, uint consumedBits, uint* rankVal, int minWeight, int maxWeight1, sortedSymbol_t* sortedSymbols, uint* rankStart, uint nbBitsBaseline, ushort baseSeq) + private static void HUF_fillDTableX2Level2( + HUF_DEltX2* DTable, + uint targetLog, + uint consumedBits, + uint* rankVal, + int minWeight, + int maxWeight1, + sortedSymbol_t* sortedSymbols, + uint* rankStart, + uint nbBitsBaseline, + ushort baseSeq + ) { if (minWeight > 1) { @@ -1043,12 +1240,28 @@ namespace ZstdSharp.Unsafe int end = (int)rankStart[w + 1]; uint nbBits = nbBitsBaseline - (uint)w; uint totalBits = nbBits + consumedBits; - HUF_fillDTableX2ForWeight(DTable + rankVal[w], sortedSymbols + begin, sortedSymbols + end, totalBits, targetLog, baseSeq, 2); + HUF_fillDTableX2ForWeight( + DTable + rankVal[w], + sortedSymbols + begin, + sortedSymbols + end, + totalBits, + targetLog, + baseSeq, + 2 + ); } } } - private static void HUF_fillDTableX2(HUF_DEltX2* DTable, uint targetLog, sortedSymbol_t* sortedList, uint* rankStart, rankValCol_t* rankValOrigin, uint maxWeight, uint nbBitsBaseline) + private static void HUF_fillDTableX2( + HUF_DEltX2* DTable, + uint targetLog, + sortedSymbol_t* sortedList, + uint* rankStart, + rankValCol_t* rankValOrigin, + uint maxWeight, + uint nbBitsBaseline + ) { uint* rankVal = (uint*)&rankValOrigin[0]; /* note : targetLog >= srcLog, hence scaleLog <= 1 */ @@ -1073,20 +1286,48 @@ namespace ZstdSharp.Unsafe minWeight = 1; for (s = begin; s != end; ++s) { - HUF_fillDTableX2Level2(DTable + start, targetLog, nbBits, (uint*)&rankValOrigin[nbBits], minWeight, wEnd, sortedList, rankStart, nbBitsBaseline, sortedList[s].symbol); + HUF_fillDTableX2Level2( + DTable + start, + targetLog, + nbBits, + (uint*)&rankValOrigin[nbBits], + minWeight, + wEnd, + sortedList, + rankStart, + nbBitsBaseline, + sortedList[s].symbol + ); start += (int)length; } } else { - HUF_fillDTableX2ForWeight(DTable + rankVal[w], sortedList + begin, sortedList + end, nbBits, targetLog, 0, 1); + HUF_fillDTableX2ForWeight( + DTable + rankVal[w], + sortedList + begin, + sortedList + end, + nbBits, + targetLog, + 0, + 1 + ); } } } - private static nuint HUF_readDTableX2_wksp(uint* DTable, void* src, nuint srcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_readDTableX2_wksp( + uint* DTable, + void* src, + nuint srcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { - uint tableLog, maxW, nbSymbols; + uint tableLog, + maxW, + nbSymbols; DTableDesc dtd = HUF_getDTableDesc(DTable); uint maxTableLog = dtd.maxTableLog; nuint iSize; @@ -1102,19 +1343,29 @@ namespace ZstdSharp.Unsafe memset(wksp->rankStart0, 0, sizeof(uint) * 15); if (maxTableLog > 12) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge)); - iSize = HUF_readStats_wksp(wksp->weightList, 255 + 1, wksp->rankStats, &nbSymbols, &tableLog, src, srcSize, wksp->calleeWksp, sizeof(uint) * 219, flags); + iSize = HUF_readStats_wksp( + wksp->weightList, + 255 + 1, + wksp->rankStats, + &nbSymbols, + &tableLog, + src, + srcSize, + wksp->calleeWksp, + sizeof(uint) * 219, + flags + ); if (ERR_isError(iSize)) return iSize; if (tableLog > maxTableLog) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_tableLog_tooLarge)); if (tableLog <= 11 && maxTableLog > 11) maxTableLog = 11; - for (maxW = tableLog; wksp->rankStats[maxW] == 0; maxW--) - { - } + for (maxW = tableLog; wksp->rankStats[maxW] == 0; maxW--) { } { - uint w, nextRankStart = 0; + uint w, + nextRankStart = 0; for (w = 1; w < maxW + 1; w++) { uint curr = nextRankStart; @@ -1168,7 +1419,15 @@ namespace ZstdSharp.Unsafe } } - HUF_fillDTableX2(dt, maxTableLog, &wksp->sortedSymbol.e0, wksp->rankStart0, &wksp->rankVal.e0, maxW, tableLog + 1); + HUF_fillDTableX2( + dt, + maxTableLog, + &wksp->sortedSymbol.e0, + wksp->rankStart0, + &wksp->rankVal.e0, + maxW, + tableLog + 1 + ); dtd.tableLog = (byte)maxTableLog; dtd.tableType = 1; memcpy(DTable, &dtd, (uint)sizeof(DTableDesc)); @@ -1176,7 +1435,12 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint HUF_decodeSymbolX2(void* op, BIT_DStream_t* DStream, HUF_DEltX2* dt, uint dtLog) + private static uint HUF_decodeSymbolX2( + void* op, + BIT_DStream_t* DStream, + HUF_DEltX2* dt, + uint dtLog + ) { /* note : dtLog >= 1 */ nuint val = BIT_lookBitsFast(DStream, dtLog); @@ -1186,7 +1450,12 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint HUF_decodeLastSymbolX2(void* op, BIT_DStream_t* DStream, HUF_DEltX2* dt, uint dtLog) + private static uint HUF_decodeLastSymbolX2( + void* op, + BIT_DStream_t* DStream, + HUF_DEltX2* dt, + uint dtLog + ) { /* note : dtLog >= 1 */ nuint val = BIT_lookBitsFast(DStream, dtLog); @@ -1209,14 +1478,23 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint HUF_decodeStreamX2(byte* p, BIT_DStream_t* bitDPtr, byte* pEnd, HUF_DEltX2* dt, uint dtLog) + private static nuint HUF_decodeStreamX2( + byte* p, + BIT_DStream_t* bitDPtr, + byte* pEnd, + HUF_DEltX2* dt, + uint dtLog + ) { byte* pStart = p; if ((nuint)(pEnd - p) >= (nuint)sizeof(nuint)) { if (dtLog <= 11 && MEM_64bits) { - while (BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished && p < pEnd - 9) + while ( + BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished + && p < pEnd - 9 + ) { p += HUF_decodeSymbolX2(p, bitDPtr, dt, dtLog); p += HUF_decodeSymbolX2(p, bitDPtr, dt, dtLog); @@ -1227,7 +1505,10 @@ namespace ZstdSharp.Unsafe } else { - while (BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished && p < pEnd - (sizeof(nuint) - 1)) + while ( + BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished + && p < pEnd - (sizeof(nuint) - 1) + ) { if (MEM_64bits) p += HUF_decodeSymbolX2(p, bitDPtr, dt, dtLog); @@ -1245,7 +1526,10 @@ namespace ZstdSharp.Unsafe if ((nuint)(pEnd - p) >= 2) { - while (BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished && p <= pEnd - 2) + while ( + BIT_reloadDStream(bitDPtr) == BIT_DStream_status.BIT_DStream_unfinished + && p <= pEnd - 2 + ) p += HUF_decodeSymbolX2(p, bitDPtr, dt, dtLog); while (p <= pEnd - 2) p += HUF_decodeSymbolX2(p, bitDPtr, dt, dtLog); @@ -1257,7 +1541,13 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint HUF_decompress1X2_usingDTable_internal_body(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + private static nuint HUF_decompress1X2_usingDTable_internal_body( + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable + ) { BIT_DStream_t bitD; { @@ -1287,7 +1577,13 @@ namespace ZstdSharp.Unsafe * @dstSize >= 6 */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint HUF_decompress4X2_usingDTable_internal_body(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable) + 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)); @@ -1356,7 +1652,7 @@ namespace ZstdSharp.Unsafe if ((nuint)(oend - op4) >= (nuint)sizeof(nuint)) { - for (; (endSignal & (uint)(op4 < olimit ? 1 : 0)) != 0;) + for (; (endSignal & (uint)(op4 < olimit ? 1 : 0)) != 0; ) { if (MEM_64bits) op1 += HUF_decodeSymbolX2(op1, &bitD1, dt, dtLog); @@ -1370,8 +1666,16 @@ namespace ZstdSharp.Unsafe if (MEM_64bits) op2 += HUF_decodeSymbolX2(op2, &bitD2, dt, dtLog); op2 += HUF_decodeSymbolX2(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; + 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(op3, &bitD3, dt, dtLog); op3 += HUF_decodeSymbolX2(op3, &bitD3, dt, dtLog); @@ -1384,8 +1688,16 @@ namespace ZstdSharp.Unsafe if (MEM_64bits) op4 += HUF_decodeSymbolX2(op4, &bitD4, dt, dtLog); op4 += HUF_decodeSymbolX2(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; + endSignal &= + BIT_reloadDStreamFast(&bitD3) + == BIT_DStream_status.BIT_DStream_unfinished + ? 1U + : 0U; + endSignal &= + BIT_reloadDStreamFast(&bitD4) + == BIT_DStream_status.BIT_DStream_unfinished + ? 1U + : 0U; } } @@ -1400,26 +1712,58 @@ namespace ZstdSharp.Unsafe 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); + 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 unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } return dstSize; } } - private static nuint HUF_decompress4X2_usingDTable_internal_default(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* 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); + return HUF_decompress4X2_usingDTable_internal_body( + dst, + dstSize, + cSrc, + cSrcSize, + DTable + ); } - private static void HUF_decompress4X2_usingDTable_internal_fast_c_loop(HUF_DecompressFastArgs* args) + private static void HUF_decompress4X2_usingDTable_internal_fast_c_loop( + HUF_DecompressFastArgs* args + ) { - ulong bits0, bits1, bits2, bits3; - byte* ip0, ip1, ip2, ip3; - byte* op0, op1, op2, op3; - byte* oend0, oend1, oend2, oend3; + ulong bits0, + bits1, + bits2, + bits3; + byte* ip0, + ip1, + ip2, + ip3; + byte* op0, + op1, + op2, + op3; + byte* oend0, + oend1, + oend2, + oend3; HUF_DEltX2* dtable = (HUF_DEltX2*)args->dt; byte* ilowest = args->ilowest; bits0 = args->bits[0]; @@ -1750,11 +2094,10 @@ namespace ZstdSharp.Unsafe } } } - } - while (op3 < olimit); + } while (op3 < olimit); } - _out: + _out: args->bits[0] = bits0; args->bits[1] = bits1; args->bits[2] = bits2; @@ -1769,14 +2112,28 @@ namespace ZstdSharp.Unsafe args->op.e3 = op3; } - private static nuint HUF_decompress4X2_usingDTable_internal_fast(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, void* loopFn) + private static nuint HUF_decompress4X2_usingDTable_internal_fast( + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable, + void* loopFn + ) { void* dt = DTable + 1; byte* ilowest = (byte*)cSrc; byte* oend = ZSTD_maybeNullPtrAdd((byte*)dst, (nint)dstSize); HUF_DecompressFastArgs args; { - nuint ret = HUF_DecompressFastArgs_init(&args, dst, dstSize, cSrc, cSrcSize, DTable); + nuint ret = HUF_DecompressFastArgs_init( + &args, + dst, + dstSize, + cSrc, + cSrcSize, + DTable + ); { nuint err_code = ret; if (ERR_isError(err_code)) @@ -1817,35 +2174,89 @@ namespace ZstdSharp.Unsafe } } - (&args.op.e0)[i] += HUF_decodeStreamX2((&args.op.e0)[i], &bit, segmentEnd, (HUF_DEltX2*)dt, 11); + (&args.op.e0)[i] += HUF_decodeStreamX2( + (&args.op.e0)[i], + &bit, + segmentEnd, + (HUF_DEltX2*)dt, + 11 + ); if ((&args.op.e0)[i] != segmentEnd) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } } return dstSize; } - private static nuint HUF_decompress4X2_usingDTable_internal(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, int flags) + private static nuint HUF_decompress4X2_usingDTable_internal( + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable, + int flags + ) { - void* fallbackFn = (delegate* managed)(&HUF_decompress4X2_usingDTable_internal_default); - void* loopFn = (delegate* managed)(&HUF_decompress4X2_usingDTable_internal_fast_c_loop); + void* fallbackFn = (delegate* managed)( + &HUF_decompress4X2_usingDTable_internal_default + ); + void* loopFn = (delegate* managed)( + &HUF_decompress4X2_usingDTable_internal_fast_c_loop + ); if ((flags & (int)HUF_flags_e.HUF_flags_disableFast) == 0) { - nuint ret = HUF_decompress4X2_usingDTable_internal_fast(dst, dstSize, cSrc, cSrcSize, DTable, loopFn); + nuint ret = HUF_decompress4X2_usingDTable_internal_fast( + dst, + dstSize, + cSrc, + cSrcSize, + DTable, + loopFn + ); if (ret != 0) return ret; } - return ((delegate* managed)fallbackFn)(dst, dstSize, cSrc, cSrcSize, DTable); + return ((delegate* managed)fallbackFn)( + dst, + dstSize, + cSrc, + cSrcSize, + DTable + ); } - private static nuint HUF_decompress1X2_usingDTable_internal(void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, uint* DTable, int flags) + private static nuint HUF_decompress1X2_usingDTable_internal( + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable, + int flags + ) { - return HUF_decompress1X2_usingDTable_internal_body(dst, dstSize, cSrc, cSrcSize, DTable); + return HUF_decompress1X2_usingDTable_internal_body( + dst, + dstSize, + cSrc, + cSrcSize, + DTable + ); } - private static nuint HUF_decompress1X2_DCtx_wksp(uint* DCtx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_decompress1X2_DCtx_wksp( + uint* DCtx, + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { byte* ip = (byte*)cSrc; nuint hSize = HUF_readDTableX2_wksp(DCtx, cSrc, cSrcSize, workSpace, wkspSize, flags); @@ -1858,7 +2269,16 @@ namespace ZstdSharp.Unsafe return HUF_decompress1X2_usingDTable_internal(dst, dstSize, ip, cSrcSize, DCtx, flags); } - private static nuint HUF_decompress4X2_DCtx_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_decompress4X2_DCtx_wksp( + uint* dctx, + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { byte* ip = (byte*)cSrc; nuint hSize = HUF_readDTableX2_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize, flags); @@ -1876,84 +2296,85 @@ namespace ZstdSharp.Unsafe new algo_time_t[2] { new algo_time_t(tableTime: 0, decode256Time: 0), - new algo_time_t(tableTime: 1, decode256Time: 1) + new algo_time_t(tableTime: 1, decode256Time: 1), }, new algo_time_t[2] { new algo_time_t(tableTime: 0, decode256Time: 0), - new algo_time_t(tableTime: 1, decode256Time: 1) + new algo_time_t(tableTime: 1, decode256Time: 1), }, new algo_time_t[2] { new algo_time_t(tableTime: 150, decode256Time: 216), - new algo_time_t(tableTime: 381, decode256Time: 119) + new algo_time_t(tableTime: 381, decode256Time: 119), }, new algo_time_t[2] { new algo_time_t(tableTime: 170, decode256Time: 205), - new algo_time_t(tableTime: 514, decode256Time: 112) + new algo_time_t(tableTime: 514, decode256Time: 112), }, new algo_time_t[2] { new algo_time_t(tableTime: 177, decode256Time: 199), - new algo_time_t(tableTime: 539, decode256Time: 110) + new algo_time_t(tableTime: 539, decode256Time: 110), }, new algo_time_t[2] { new algo_time_t(tableTime: 197, decode256Time: 194), - new algo_time_t(tableTime: 644, decode256Time: 107) + new algo_time_t(tableTime: 644, decode256Time: 107), }, new algo_time_t[2] { new algo_time_t(tableTime: 221, decode256Time: 192), - new algo_time_t(tableTime: 735, decode256Time: 107) + new algo_time_t(tableTime: 735, decode256Time: 107), }, new algo_time_t[2] { new algo_time_t(tableTime: 256, decode256Time: 189), - new algo_time_t(tableTime: 881, decode256Time: 106) + new algo_time_t(tableTime: 881, decode256Time: 106), }, new algo_time_t[2] { new algo_time_t(tableTime: 359, decode256Time: 188), - new algo_time_t(tableTime: 1167, decode256Time: 109) + new algo_time_t(tableTime: 1167, decode256Time: 109), }, new algo_time_t[2] { new algo_time_t(tableTime: 582, decode256Time: 187), - new algo_time_t(tableTime: 1570, decode256Time: 114) + new algo_time_t(tableTime: 1570, decode256Time: 114), }, new algo_time_t[2] { new algo_time_t(tableTime: 688, decode256Time: 187), - new algo_time_t(tableTime: 1712, decode256Time: 122) + new algo_time_t(tableTime: 1712, decode256Time: 122), }, new algo_time_t[2] { new algo_time_t(tableTime: 825, decode256Time: 186), - new algo_time_t(tableTime: 1965, decode256Time: 136) + new algo_time_t(tableTime: 1965, decode256Time: 136), }, new algo_time_t[2] { new algo_time_t(tableTime: 976, decode256Time: 185), - new algo_time_t(tableTime: 2131, decode256Time: 150) + new algo_time_t(tableTime: 2131, decode256Time: 150), }, new algo_time_t[2] { new algo_time_t(tableTime: 1180, decode256Time: 186), - new algo_time_t(tableTime: 2070, decode256Time: 175) + new algo_time_t(tableTime: 2070, decode256Time: 175), }, new algo_time_t[2] { new algo_time_t(tableTime: 1377, decode256Time: 185), - new algo_time_t(tableTime: 1731, decode256Time: 202) + new algo_time_t(tableTime: 1731, decode256Time: 202), }, new algo_time_t[2] { new algo_time_t(tableTime: 1412, decode256Time: 185), - new algo_time_t(tableTime: 1695, decode256Time: 202) - } + new algo_time_t(tableTime: 1695, decode256Time: 202), + }, }; + /** HUF_selectDecoder() : * Tells which decoder is likely to decode faster, * based on a set of pre-computed metrics. @@ -1974,7 +2395,16 @@ namespace ZstdSharp.Unsafe } } - private static nuint HUF_decompress1X_DCtx_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_decompress1X_DCtx_wksp( + uint* dctx, + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { if (dstSize == 0) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); @@ -1994,20 +2424,72 @@ namespace ZstdSharp.Unsafe { uint algoNb = HUF_selectDecoder(dstSize, cSrcSize); - return algoNb != 0 ? HUF_decompress1X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags) : HUF_decompress1X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags); + return algoNb != 0 + ? HUF_decompress1X2_DCtx_wksp( + dctx, + dst, + dstSize, + cSrc, + cSrcSize, + workSpace, + wkspSize, + flags + ) + : HUF_decompress1X1_DCtx_wksp( + dctx, + dst, + dstSize, + cSrc, + cSrcSize, + workSpace, + wkspSize, + flags + ); } } /* BMI2 variants. * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0. */ - private static nuint HUF_decompress1X_usingDTable(void* dst, nuint maxDstSize, void* cSrc, nuint cSrcSize, uint* DTable, int flags) + private static nuint HUF_decompress1X_usingDTable( + void* dst, + nuint maxDstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable, + int flags + ) { DTableDesc dtd = HUF_getDTableDesc(DTable); - return dtd.tableType != 0 ? HUF_decompress1X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags) : HUF_decompress1X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags); + return dtd.tableType != 0 + ? HUF_decompress1X2_usingDTable_internal( + dst, + maxDstSize, + cSrc, + cSrcSize, + DTable, + flags + ) + : HUF_decompress1X1_usingDTable_internal( + dst, + maxDstSize, + cSrc, + cSrcSize, + DTable, + flags + ); } - private static nuint HUF_decompress1X1_DCtx_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_decompress1X1_DCtx_wksp( + uint* dctx, + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { byte* ip = (byte*)cSrc; nuint hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize, flags); @@ -2020,13 +2502,45 @@ namespace ZstdSharp.Unsafe return HUF_decompress1X1_usingDTable_internal(dst, dstSize, ip, cSrcSize, dctx, flags); } - private static nuint HUF_decompress4X_usingDTable(void* dst, nuint maxDstSize, void* cSrc, nuint cSrcSize, uint* DTable, int flags) + private static nuint HUF_decompress4X_usingDTable( + void* dst, + nuint maxDstSize, + void* cSrc, + nuint cSrcSize, + uint* DTable, + int flags + ) { DTableDesc dtd = HUF_getDTableDesc(DTable); - return dtd.tableType != 0 ? HUF_decompress4X2_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags) : HUF_decompress4X1_usingDTable_internal(dst, maxDstSize, cSrc, cSrcSize, DTable, flags); + return dtd.tableType != 0 + ? HUF_decompress4X2_usingDTable_internal( + dst, + maxDstSize, + cSrc, + cSrcSize, + DTable, + flags + ) + : HUF_decompress4X1_usingDTable_internal( + dst, + maxDstSize, + cSrc, + cSrcSize, + DTable, + flags + ); } - private static nuint HUF_decompress4X_hufOnly_wksp(uint* dctx, void* dst, nuint dstSize, void* cSrc, nuint cSrcSize, void* workSpace, nuint wkspSize, int flags) + private static nuint HUF_decompress4X_hufOnly_wksp( + uint* dctx, + void* dst, + nuint dstSize, + void* cSrc, + nuint cSrcSize, + void* workSpace, + nuint wkspSize, + int flags + ) { if (dstSize == 0) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); @@ -2034,7 +2548,27 @@ namespace ZstdSharp.Unsafe 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, flags) : HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize, workSpace, wkspSize, flags); + return algoNb != 0 + ? HUF_decompress4X2_DCtx_wksp( + dctx, + dst, + dstSize, + cSrc, + cSrcSize, + workSpace, + wkspSize, + flags + ) + : HUF_decompress4X1_DCtx_wksp( + dctx, + dst, + dstSize, + cSrc, + cSrcSize, + workSpace, + wkspSize, + flags + ); } } } diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Mem.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Mem.cs index 1f918fff..284a043a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Mem.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Mem.cs @@ -41,10 +41,12 @@ namespace ZstdSharp.Unsafe private static nuint MEM_readST(void* memPtr) => BclUnsafe.ReadUnaligned(memPtr); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void MEM_write16(void* memPtr, ushort value) => BclUnsafe.WriteUnaligned(memPtr, value); + private static void MEM_write16(void* memPtr, ushort value) => + BclUnsafe.WriteUnaligned(memPtr, value); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void MEM_write64(void* memPtr, ulong value) => BclUnsafe.WriteUnaligned(memPtr, value); + private static void MEM_write64(void* memPtr, ulong value) => + BclUnsafe.WriteUnaligned(memPtr, value); /*=== Little endian r/w ===*/ [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -125,8 +127,8 @@ namespace ZstdSharp.Unsafe [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint ReverseEndiannessNative(nuint val) => MEM_32bits - ? BinaryPrimitives.ReverseEndianness((uint) val) - : (nuint) BinaryPrimitives.ReverseEndianness(val); + ? BinaryPrimitives.ReverseEndianness((uint)val) + : (nuint)BinaryPrimitives.ReverseEndianness(val); #endif [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/RSyncState_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/RSyncState_t.cs index d2065d07..e82e241a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/RSyncState_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/RSyncState_t.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public ulong hitMask; public ulong primePower; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Range.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Range.cs index bc0c8d34..3829e85b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Range.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Range.cs @@ -5,10 +5,11 @@ namespace ZstdSharp.Unsafe { public void* start; public nuint size; + public Range(void* start, nuint size) { this.start = start; this.size = size; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/RawSeqStore_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/RawSeqStore_t.cs index 0e2334cd..15b5b2ca 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/RawSeqStore_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/RawSeqStore_t.cs @@ -4,16 +4,27 @@ namespace ZstdSharp.Unsafe { /* 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; - public RawSeqStore_t(rawSeq* seq, nuint pos, nuint posInSequence, nuint size, nuint capacity) + + public RawSeqStore_t( + rawSeq* seq, + nuint pos, + nuint posInSequence, + nuint size, + nuint capacity + ) { this.seq = seq; this.pos = pos; @@ -22,4 +33,4 @@ namespace ZstdSharp.Unsafe this.capacity = capacity; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/RoundBuff_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/RoundBuff_t.cs index 22631e10..90db6546 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/RoundBuff_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/RoundBuff_t.cs @@ -8,14 +8,17 @@ namespace ZstdSharp.Unsafe * sure it doesn't overlap with any pieces still in use. */ public byte* buffer; + /* The capacity of buffer. */ public nuint capacity; + /* The position of the current inBuff in the round * buffer. Updated past the end if the inBuff once * the inBuff is sent to the worker thread. * pos <= capacity. */ public nuint pos; + public RoundBuff_t(byte* buffer, nuint capacity, nuint pos) { this.buffer = buffer; @@ -23,4 +26,4 @@ namespace ZstdSharp.Unsafe this.pos = pos; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqCollector.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqCollector.cs index 883517c9..b39d020f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqCollector.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqCollector.cs @@ -7,4 +7,4 @@ namespace ZstdSharp.Unsafe public nuint seqIndex; public nuint maxSequences; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqDef_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqDef_s.cs index 660f3866..20b849dc 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqDef_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqDef_s.cs @@ -8,7 +8,8 @@ namespace ZstdSharp.Unsafe /* offBase == Offset + ZSTD_REP_NUM, or repcode 1,2,3 */ public uint offBase; public ushort litLength; + /* mlBase == matchLength - MINMATCH */ public ushort mlBase; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqStore_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqStore_t.cs index 4b661a55..d4d5cb97 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqStore_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/SeqStore_t.cs @@ -3,9 +3,11 @@ namespace ZstdSharp.Unsafe public unsafe 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; @@ -13,12 +15,14 @@ namespace ZstdSharp.Unsafe 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/SerialState.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/SerialState.cs index 4b39c6c4..c88b175c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/SerialState.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/SerialState.cs @@ -9,13 +9,16 @@ namespace ZstdSharp.Unsafe public ldmState_t ldmState; public XXH64_state_s xxhState; public uint nextJobID; + /* Protects ldmWindow. * Must be acquired after the main mutex when acquiring both. */ public void* ldmWindowMutex; + /* Signaled when ldmWindow is updated */ public void* ldmWindowCond; + /* A thread-safe copy of ldmState.window */ public ZSTD_window_t ldmWindow; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/SymbolEncodingType_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/SymbolEncodingType_e.cs index 7763eaf8..3fc6e04c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/SymbolEncodingType_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/SymbolEncodingType_e.cs @@ -5,6 +5,6 @@ namespace ZstdSharp.Unsafe set_basic, set_rle, set_compressed, - set_repeat + set_repeat, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/SyncPoint.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/SyncPoint.cs index bf93adfc..db9ada1d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/SyncPoint.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/SyncPoint.cs @@ -4,7 +4,8 @@ namespace ZstdSharp.Unsafe { /* The number of bytes to load from the input. */ public nuint toLoad; + /* Boolean declaring if we must flush because we found a synchronization point. */ public int flush; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH32_canonical_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH32_canonical_t.cs index fbb3572f..c636b30f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH32_canonical_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH32_canonical_t.cs @@ -8,4 +8,4 @@ namespace ZstdSharp.Unsafe /*!< Hash bytes, big endian */ public fixed byte digest[4]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH32_state_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH32_state_s.cs index 01c6229e..413ae77f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH32_state_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH32_state_s.cs @@ -16,15 +16,20 @@ namespace ZstdSharp.Unsafe { /*!< Total length hashed, modulo 2^32 */ public uint total_len_32; + /*!< Whether the hash is >= 16 (handles @ref total_len_32 overflow) */ public uint large_len; + /*!< Accumulator lanes */ public fixed uint v[4]; + /*!< Internal buffer for partial reads. Treated as unsigned char[16]. */ public fixed uint mem32[4]; + /*!< Amount of data in @ref mem32 */ public uint memsize; + /*!< Reserved field. Do not read nor write to it. */ public uint reserved; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH64_canonical_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH64_canonical_t.cs index fc5ffb46..1f18d31a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH64_canonical_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH64_canonical_t.cs @@ -7,4 +7,4 @@ namespace ZstdSharp.Unsafe { public fixed byte digest[8]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH64_state_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH64_state_s.cs index 3ba3dd4a..448af8ce 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH64_state_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH64_state_s.cs @@ -16,15 +16,20 @@ namespace ZstdSharp.Unsafe { /*!< Total length hashed. This is always 64-bit. */ public ulong total_len; + /*!< Accumulator lanes */ public fixed ulong v[4]; + /*!< Internal buffer for partial reads. Treated as unsigned char[32]. */ public fixed ulong mem64[4]; + /*!< Amount of data in @ref mem64 */ public uint memsize; + /*!< Reserved field, needed for padding anyways*/ public uint reserved32; + /*!< Reserved field. Do not read or write to it. */ public ulong reserved64; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH_alignment.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH_alignment.cs index e3b72151..1a14c917 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH_alignment.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH_alignment.cs @@ -8,7 +8,8 @@ namespace ZstdSharp.Unsafe { /*!< Aligned */ XXH_aligned, + /*!< Possibly unaligned */ - XXH_unaligned + XXH_unaligned, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH_errorcode.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH_errorcode.cs index bd3568df..f88ee636 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH_errorcode.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/XXH_errorcode.cs @@ -7,7 +7,8 @@ namespace ZstdSharp.Unsafe { /*!< OK */ XXH_OK = 0, + /*!< Error */ - XXH_ERROR + XXH_ERROR, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Xxhash.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Xxhash.cs index 081fca15..ee25063c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Xxhash.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Xxhash.cs @@ -1,8 +1,8 @@ -using static ZstdSharp.UnsafeHelper; using System; using System.Buffers.Binary; using System.Numerics; using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -39,13 +39,17 @@ namespace ZstdSharp.Unsafe [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint XXH_readLE32(void* ptr) { - return BitConverter.IsLittleEndian ? MEM_read32(ptr) : BinaryPrimitives.ReverseEndianness(MEM_read32(ptr)); + return BitConverter.IsLittleEndian + ? MEM_read32(ptr) + : BinaryPrimitives.ReverseEndianness(MEM_read32(ptr)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint XXH_readBE32(void* ptr) { - return BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(MEM_read32(ptr)) : MEM_read32(ptr); + return BitConverter.IsLittleEndian + ? BinaryPrimitives.ReverseEndianness(MEM_read32(ptr)) + : MEM_read32(ptr); } private static uint XXH_readLE32_align(void* ptr, XXH_alignment align) @@ -56,7 +60,9 @@ namespace ZstdSharp.Unsafe } else { - return BitConverter.IsLittleEndian ? *(uint*)ptr : BinaryPrimitives.ReverseEndianness(*(uint*)ptr); + return BitConverter.IsLittleEndian + ? *(uint*)ptr + : BinaryPrimitives.ReverseEndianness(*(uint*)ptr); } } @@ -158,7 +164,12 @@ namespace ZstdSharp.Unsafe * @param align Whether @p input is aligned. * @return The calculated hash. */ - private static uint XXH32_endian_align(byte* input, nuint len, uint seed, XXH_alignment align) + private static uint XXH32_endian_align( + byte* input, + nuint len, + uint seed, + XXH_alignment align + ) { uint h32; if (len >= 16) @@ -179,9 +190,12 @@ namespace ZstdSharp.Unsafe input += 4; v4 = XXH32_round(v4, XXH_readLE32_align(input, align)); input += 4; - } - while (input < limit); - h32 = BitOperations.RotateLeft(v1, 1) + BitOperations.RotateLeft(v2, 7) + BitOperations.RotateLeft(v3, 12) + BitOperations.RotateLeft(v4, 18); + } while (input < limit); + h32 = + BitOperations.RotateLeft(v1, 1) + + BitOperations.RotateLeft(v2, 7) + + BitOperations.RotateLeft(v3, 12) + + BitOperations.RotateLeft(v4, 18); } else { @@ -279,8 +293,7 @@ namespace ZstdSharp.Unsafe p += 4; state->v[3] = XXH32_round(state->v[3], XXH_readLE32(p)); p += 4; - } - while (p <= limit); + } while (p <= limit); } if (p < bEnd) @@ -299,7 +312,11 @@ namespace ZstdSharp.Unsafe uint h32; if (state->large_len != 0) { - h32 = BitOperations.RotateLeft(state->v[0], 1) + BitOperations.RotateLeft(state->v[1], 7) + BitOperations.RotateLeft(state->v[2], 12) + BitOperations.RotateLeft(state->v[3], 18); + h32 = + BitOperations.RotateLeft(state->v[0], 1) + + BitOperations.RotateLeft(state->v[1], 7) + + BitOperations.RotateLeft(state->v[2], 12) + + BitOperations.RotateLeft(state->v[3], 18); } else { @@ -307,7 +324,12 @@ namespace ZstdSharp.Unsafe } h32 += state->total_len_32; - return XXH32_finalize(h32, (byte*)state->mem32, state->memsize, XXH_alignment.XXH_aligned); + return XXH32_finalize( + h32, + (byte*)state->mem32, + state->memsize, + XXH_alignment.XXH_aligned + ); } /*! @ingroup XXH32_family */ @@ -328,13 +350,17 @@ namespace ZstdSharp.Unsafe [MethodImpl(MethodImplOptions.AggressiveInlining)] private static ulong XXH_readLE64(void* ptr) { - return BitConverter.IsLittleEndian ? MEM_read64(ptr) : BinaryPrimitives.ReverseEndianness(MEM_read64(ptr)); + return BitConverter.IsLittleEndian + ? MEM_read64(ptr) + : BinaryPrimitives.ReverseEndianness(MEM_read64(ptr)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static ulong XXH_readBE64(void* ptr) { - return BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(MEM_read64(ptr)) : MEM_read64(ptr); + return BitConverter.IsLittleEndian + ? BinaryPrimitives.ReverseEndianness(MEM_read64(ptr)) + : MEM_read64(ptr); } private static ulong XXH_readLE64_align(void* ptr, XXH_alignment align) @@ -342,7 +368,9 @@ namespace ZstdSharp.Unsafe if (align == XXH_alignment.XXH_unaligned) return XXH_readLE64(ptr); else - return BitConverter.IsLittleEndian ? *(ulong*)ptr : BinaryPrimitives.ReverseEndianness(*(ulong*)ptr); + return BitConverter.IsLittleEndian + ? *(ulong*)ptr + : BinaryPrimitives.ReverseEndianness(*(ulong*)ptr); } /*! @copydoc XXH32_round */ @@ -398,7 +426,9 @@ namespace ZstdSharp.Unsafe ulong k1 = XXH64_round(0, XXH_readLE64_align(ptr, align)); ptr += 8; hash ^= k1; - hash = BitOperations.RotateLeft(hash, 27) * 0x9E3779B185EBCA87UL + 0x85EBCA77C2B2AE63UL; + hash = + BitOperations.RotateLeft(hash, 27) * 0x9E3779B185EBCA87UL + + 0x85EBCA77C2B2AE63UL; len -= 8; } @@ -406,7 +436,9 @@ namespace ZstdSharp.Unsafe { hash ^= XXH_readLE32_align(ptr, align) * 0x9E3779B185EBCA87UL; ptr += 4; - hash = BitOperations.RotateLeft(hash, 23) * 0xC2B2AE3D27D4EB4FUL + 0x165667B19E3779F9UL; + hash = + BitOperations.RotateLeft(hash, 23) * 0xC2B2AE3D27D4EB4FUL + + 0x165667B19E3779F9UL; len -= 4; } @@ -428,7 +460,12 @@ namespace ZstdSharp.Unsafe * @param align Whether @p input is aligned. * @return The calculated hash. */ - private static ulong XXH64_endian_align(byte* input, nuint len, ulong seed, XXH_alignment align) + private static ulong XXH64_endian_align( + byte* input, + nuint len, + ulong seed, + XXH_alignment align + ) { ulong h64; if (len >= 32) @@ -449,9 +486,12 @@ namespace ZstdSharp.Unsafe input += 8; v4 = XXH64_round(v4, XXH_readLE64_align(input, align)); input += 8; - } - while (input < limit); - h64 = BitOperations.RotateLeft(v1, 1) + BitOperations.RotateLeft(v2, 7) + BitOperations.RotateLeft(v3, 12) + BitOperations.RotateLeft(v4, 18); + } while (input < 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); @@ -545,8 +585,7 @@ namespace ZstdSharp.Unsafe p += 8; state->v[3] = XXH64_round(state->v[3], XXH_readLE64(p)); p += 8; - } - while (p <= limit); + } while (p <= limit); } if (p < bEnd) @@ -565,7 +604,11 @@ namespace ZstdSharp.Unsafe ulong h64; if (state->total_len >= 32) { - h64 = BitOperations.RotateLeft(state->v[0], 1) + BitOperations.RotateLeft(state->v[1], 7) + BitOperations.RotateLeft(state->v[2], 12) + BitOperations.RotateLeft(state->v[3], 18); + h64 = + BitOperations.RotateLeft(state->v[0], 1) + + BitOperations.RotateLeft(state->v[1], 7) + + BitOperations.RotateLeft(state->v[2], 12) + + BitOperations.RotateLeft(state->v[3], 18); h64 = XXH64_mergeRound(h64, state->v[0]); h64 = XXH64_mergeRound(h64, state->v[1]); h64 = XXH64_mergeRound(h64, state->v[2]); @@ -577,7 +620,12 @@ namespace ZstdSharp.Unsafe } h64 += state->total_len; - return XXH64_finalize(h64, (byte*)state->mem64, (nuint)state->total_len, XXH_alignment.XXH_aligned); + return XXH64_finalize( + h64, + (byte*)state->mem64, + (nuint)state->total_len, + XXH_alignment.XXH_aligned + ); } /*! @ingroup XXH64_family */ diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_cover_params_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_cover_params_t.cs index 2b27c1a1..9913870c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_cover_params_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_cover_params_t.cs @@ -8,18 +8,24 @@ namespace ZstdSharp.Unsafe { /* 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_fastCover_params_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_fastCover_params_t.cs index eea3bb33..8fa765d8 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_fastCover_params_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_fastCover_params_t.cs @@ -4,22 +4,30 @@ namespace ZstdSharp.Unsafe { /* 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_legacy_params_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_legacy_params_t.cs index 466cf2bc..001db2b9 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_legacy_params_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_legacy_params_t.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public uint selectivityLevel; public ZDICT_params_t zParams; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_params_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_params_t.cs index 28668ac5..6609d628 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_params_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZDICT_params_t.cs @@ -4,8 +4,10 @@ namespace ZstdSharp.Unsafe { /**< 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 @@ -16,4 +18,4 @@ namespace ZstdSharp.Unsafe */ public uint dictID; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_CCtxPool.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_CCtxPool.cs index 691635a3..3694a0d7 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_CCtxPool.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_CCtxPool.cs @@ -10,4 +10,4 @@ namespace ZstdSharp.Unsafe public ZSTD_customMem cMem; public ZSTD_CCtx_s** cctxs; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_CCtx_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_CCtx_s.cs index 7185cb5a..3daf397f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_CCtx_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_CCtx_s.cs @@ -10,6 +10,7 @@ namespace ZstdSharp.Unsafe public ZSTD_CCtx_params_s @params; public nuint targetSectionSize; public nuint targetPrefixSize; + /* 1 => one job is already prepared, but pool has shortage of workers. Don't create a new job. */ public int jobReady; public InBuff_t inBuff; @@ -29,4 +30,4 @@ namespace ZstdSharp.Unsafe public ZSTD_CDict_s* cdict; public uint providedFactory; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_bufferPool_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_bufferPool_s.cs index a5333bfe..79d0aceb 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_bufferPool_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_bufferPool_s.cs @@ -9,4 +9,4 @@ namespace ZstdSharp.Unsafe public ZSTD_customMem cMem; public buffer_s* buffers; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_jobDescription.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_jobDescription.cs index b0fb7a6e..f138a4e9 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_jobDescription.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTDMT_jobDescription.cs @@ -4,41 +4,59 @@ namespace ZstdSharp.Unsafe { /* SHARED - set0 by mtctx, then modified by worker AND read by mtctx */ public nuint consumed; + /* SHARED - set0 by mtctx, then modified by worker AND read by mtctx, then set0 by mtctx */ public nuint cSize; + /* Thread-safe - used by mtctx and worker */ public void* job_mutex; + /* Thread-safe - used by mtctx and worker */ public void* job_cond; + /* Thread-safe - used by mtctx and (all) workers */ public ZSTDMT_CCtxPool* cctxPool; + /* Thread-safe - used by mtctx and (all) workers */ public ZSTDMT_bufferPool_s* bufPool; + /* Thread-safe - used by mtctx and (all) workers */ public ZSTDMT_bufferPool_s* seqPool; + /* Thread-safe - used by mtctx and (all) workers */ public SerialState* serial; + /* set by worker (or mtctx), then read by worker & mtctx, then modified by mtctx => no barrier */ public buffer_s dstBuff; + /* set by mtctx, then read by worker & mtctx => no barrier */ public Range prefix; + /* set by mtctx, then read by worker & mtctx => no barrier */ public Range src; + /* set by mtctx, then read by worker => no barrier */ public uint jobID; + /* set by mtctx, then read by worker => no barrier */ public uint firstJob; + /* set by mtctx, then read by worker => no barrier */ public uint lastJob; + /* set by mtctx, then read by worker => no barrier */ public ZSTD_CCtx_params_s @params; + /* set by mtctx, then read by worker => no barrier */ public ZSTD_CDict_s* cdict; + /* set by mtctx, then read by worker => no barrier */ public ulong fullFrameSize; + /* used only by mtctx */ public nuint dstFlushed; + /* used only by mtctx */ public uint frameChecksumNeeded; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BlockCompressor_f.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BlockCompressor_f.cs index 9d9a7e3b..f65e81c7 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BlockCompressor_f.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BlockCompressor_f.cs @@ -3,5 +3,11 @@ using System.Runtime.InteropServices; namespace ZstdSharp.Unsafe { [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public unsafe delegate nuint ZSTD_BlockCompressor_f(ZSTD_MatchState_t* bs, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize); -} \ No newline at end of file + public unsafe delegate nuint ZSTD_BlockCompressor_f( + ZSTD_MatchState_t* bs, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ); +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BuildCTableWksp.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BuildCTableWksp.cs index 98631bfa..1020f695 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BuildCTableWksp.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BuildCTableWksp.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public fixed short norm[53]; public fixed uint wksp[285]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BuildSeqStore_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BuildSeqStore_e.cs index 6e575561..3f0b7d17 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BuildSeqStore_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_BuildSeqStore_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_BuildSeqStore_e { ZSTDbss_compress, - ZSTDbss_noCompress + ZSTDbss_noCompress, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_CCtx_params_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_CCtx_params_s.cs index d29f527d..8281cc69 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_CCtx_params_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_CCtx_params_s.cs @@ -6,34 +6,43 @@ namespace ZstdSharp.Unsafe public ZSTD_compressionParameters cParams; public ZSTD_frameParameters fParams; public int compressionLevel; + /* force back-references to respect limit of * 1< dFast. * Special: value 0 means "use default hashLog". */ ZSTD_c_hashLog = 102, + /* Size of the multi-probe search table, as a power of 2. * Resulting memory usage is (1 << (chainLog+2)). * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX. @@ -38,11 +41,13 @@ namespace ZstdSharp.Unsafe * in which case it defines a secondary probe table. * Special: value 0 means "use default chainLog". */ ZSTD_c_chainLog = 103, + /* Number of search attempts, as a power of 2. * More attempts result in better and slower compression. * This parameter is useless for "fast" and "dFast" strategies. * Special: value 0 means "use default searchLog". */ ZSTD_c_searchLog = 104, + /* Minimum size of searched matches. * Note that Zstandard can still find matches of smaller size, * it just tweaks its search algorithm to look for this size and larger. @@ -52,6 +57,7 @@ namespace ZstdSharp.Unsafe * , for all strategies > fast, effective maximum is 6. * Special: value 0 means "use default minMatchLength". */ ZSTD_c_minMatch = 105, + /* Impact of this field depends on strategy. * For strategies btopt, btultra & btultra2: * Length of Match considered "good enough" to stop search. @@ -61,11 +67,13 @@ namespace ZstdSharp.Unsafe * Larger values make compression faster, and weaker. * Special: value 0 means "use default targetLength". */ ZSTD_c_targetLength = 106, + /* See ZSTD_strategy enum definition. * The higher the value of selected strategy, the more complex it is, * resulting in stronger and slower compression. * Special: value 0 means "use default strategy". */ ZSTD_c_strategy = 107, + /* v1.5.6+ * Attempts to fit compressed block size into approximately targetCBlockSize. * Bound by ZSTD_TARGETCBLOCKSIZE_MIN and ZSTD_TARGETCBLOCKSIZE_MAX. @@ -79,6 +87,7 @@ namespace ZstdSharp.Unsafe * due to massive performance regressions. */ ZSTD_c_targetCBlockSize = 130, + /* Enable long distance matching. * This parameter is designed to improve compression ratio * for large inputs, by finding large matches at long distance. @@ -88,6 +97,7 @@ namespace ZstdSharp.Unsafe * Note: will be enabled by default if ZSTD_c_windowLog >= 128 MB and * compression strategy >= ZSTD_btopt (== compression level 16+) */ ZSTD_c_enableLongDistanceMatching = 160, + /* Size of the table for long distance matching, as a power of 2. * Larger values increase memory usage and compression ratio, * but decrease compression speed. @@ -95,16 +105,19 @@ namespace ZstdSharp.Unsafe * default: windowlog - 7. * Special: value 0 means "automatically determine hashlog". */ ZSTD_c_ldmHashLog = 161, + /* Minimum match size for long distance matcher. * Larger/too small values usually decrease compression ratio. * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX. * Special: value 0 means "use default value" (default: 64). */ ZSTD_c_ldmMinMatch = 162, + /* Log size of each bucket in the LDM hash table for collision resolution. * Larger values improve collision resolution but decrease compression speed. * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX. * Special: value 0 means "use default value" (default: 3). */ ZSTD_c_ldmBucketSizeLog = 163, + /* Frequency of inserting/looking up entries into the LDM hash table. * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN). * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage. @@ -112,15 +125,19 @@ namespace ZstdSharp.Unsafe * Deviating far from default value will likely result in a compression ratio decrease. * Special: value 0 means "automatically determine hashRateLog". */ ZSTD_c_ldmHashRateLog = 164, + /* Content size will be written into frame header _whenever known_ (default:1) * Content size must be known at the beginning of compression. * This is automatically the case when using ZSTD_compress2(), * For streaming scenarios, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */ ZSTD_c_contentSizeFlag = 200, + /* A 32-bits checksum of content is written at end of frame (default:0) */ ZSTD_c_checksumFlag = 201, + /* When applicable, dictionary's ID is written into frame header (default:1) */ ZSTD_c_dictIDFlag = 202, + /* Select how many threads will be spawned to compress in parallel. * When nbWorkers >= 1, triggers asynchronous mode when invoking ZSTD_compressStream*() : * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller, @@ -131,12 +148,14 @@ namespace ZstdSharp.Unsafe * Default value is `0`, aka "single-threaded mode" : no worker is spawned, * compression is performed inside Caller's thread, and all invocations are blocking */ ZSTD_c_nbWorkers = 400, + /* Size of a compression job. This value is enforced only when nbWorkers >= 1. * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads. * 0 means default, which is dynamically determined based on compression parameters. * Job size must be a minimum of overlap size, or ZSTDMT_JOBSIZE_MIN (= 512 KB), whichever is largest. * The minimum size is automatically and transparently enforced. */ ZSTD_c_jobSize = 401, + /* Control the overlap size, as a fraction of window size. * The overlap size is an amount of data reloaded from previous job at the beginning of a new job. * It helps preserve compression ratio, while each job is compressed in parallel. @@ -150,6 +169,7 @@ namespace ZstdSharp.Unsafe * 9: full window; 8: w/2; 7: w/4; 6: w/8; 5:w/16; 4: w/32; 3:w/64; 2:w/128; 1:no overlap; 0:default * default value varies between 6 and 9, depending on strategy */ ZSTD_c_overlapLog = 402, + /* note : additional experimental parameters are also available * within the experimental section of the API. * At the time of this writing, they include : @@ -179,6 +199,7 @@ namespace ZstdSharp.Unsafe ZSTD_c_experimentalParam3 = 1000, ZSTD_c_experimentalParam4 = 1001, ZSTD_c_experimentalParam5 = 1002, + /* was ZSTD_c_experimentalParam6=1003; is now ZSTD_c_targetCBlockSize */ ZSTD_c_experimentalParam7 = 1004, ZSTD_c_experimentalParam8 = 1005, @@ -193,6 +214,6 @@ namespace ZstdSharp.Unsafe ZSTD_c_experimentalParam17 = 1014, ZSTD_c_experimentalParam18 = 1015, ZSTD_c_experimentalParam19 = 1016, - ZSTD_c_experimentalParam20 = 1017 + ZSTD_c_experimentalParam20 = 1017, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cStreamStage.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cStreamStage.cs index 770eaa91..443520a9 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cStreamStage.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cStreamStage.cs @@ -4,6 +4,6 @@ namespace ZstdSharp.Unsafe { zcss_init = 0, zcss_load, - zcss_flush + zcss_flush, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compResetPolicy_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compResetPolicy_e.cs index 0f474bc0..95c055d2 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compResetPolicy_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compResetPolicy_e.cs @@ -10,6 +10,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_compResetPolicy_e { ZSTDcrp_makeClean, - ZSTDcrp_leaveDirty + ZSTDcrp_leaveDirty, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressedBlockState_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressedBlockState_t.cs index 24a9e6f8..ab5dbe72 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressedBlockState_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressedBlockState_t.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public ZSTD_entropyCTables_t entropy; public fixed uint rep[3]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressionParameters.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressionParameters.cs index f27b7cf8..0fe0b5c5 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressionParameters.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressionParameters.cs @@ -4,19 +4,34 @@ namespace ZstdSharp.Unsafe { /**< 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; - public ZSTD_compressionParameters(uint windowLog, uint chainLog, uint hashLog, uint searchLog, uint minMatch, uint targetLength, ZSTD_strategy strategy) + + public ZSTD_compressionParameters( + uint windowLog, + uint chainLog, + uint hashLog, + uint searchLog, + uint minMatch, + uint targetLength, + ZSTD_strategy strategy + ) { this.windowLog = windowLog; this.chainLog = chainLog; @@ -27,4 +42,4 @@ namespace ZstdSharp.Unsafe this.strategy = strategy; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressionStage_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressionStage_e.cs index 79447b48..a6c0f7ec 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressionStage_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_compressionStage_e.cs @@ -8,6 +8,6 @@ namespace ZstdSharp.Unsafe ZSTDcs_created = 0, ZSTDcs_init, ZSTDcs_ongoing, - ZSTDcs_ending + ZSTDcs_ending, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_customMem.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_customMem.cs index e34bfd7b..311ac0a3 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_customMem.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_customMem.cs @@ -5,6 +5,7 @@ namespace ZstdSharp.Unsafe public void* customAlloc; public void* customFree; public void* opaque; + public ZSTD_customMem(void* customAlloc, void* customFree, void* opaque) { this.customAlloc = customAlloc; @@ -12,4 +13,4 @@ namespace ZstdSharp.Unsafe this.opaque = opaque; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp.cs index d60a7468..0e9e7ae2 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp.cs @@ -108,4 +108,4 @@ namespace ZstdSharp.Unsafe public ZSTD_cwksp_alloc_phase_e phase; public ZSTD_cwksp_static_alloc_e isStatic; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp_alloc_phase_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp_alloc_phase_e.cs index 86136383..f3f266f8 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp_alloc_phase_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp_alloc_phase_e.cs @@ -8,6 +8,6 @@ namespace ZstdSharp.Unsafe ZSTD_cwksp_alloc_objects, ZSTD_cwksp_alloc_aligned_init_once, ZSTD_cwksp_alloc_aligned, - ZSTD_cwksp_alloc_buffers + ZSTD_cwksp_alloc_buffers, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp_static_alloc_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp_static_alloc_e.cs index 08d03b7e..25e530df 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp_static_alloc_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_cwksp_static_alloc_e.cs @@ -8,6 +8,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_cwksp_static_alloc_e { ZSTD_cwksp_dynamic_alloc, - ZSTD_cwksp_static_alloc + ZSTD_cwksp_static_alloc, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dParameter.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dParameter.cs index ef9d206a..20096143 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dParameter.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dParameter.cs @@ -16,6 +16,7 @@ namespace ZstdSharp.Unsafe * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT). * Special: value 0 means "use default maximum windowLog". */ ZSTD_d_windowLogMax = 100, + /* note : additional experimental parameters are also available * within the experimental section of the API. * At the time of this writing, they include : @@ -33,6 +34,6 @@ namespace ZstdSharp.Unsafe ZSTD_d_experimentalParam3 = 1002, ZSTD_d_experimentalParam4 = 1003, ZSTD_d_experimentalParam5 = 1004, - ZSTD_d_experimentalParam6 = 1005 + ZSTD_d_experimentalParam6 = 1005, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dStage.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dStage.cs index 7374cd07..104155fe 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dStage.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dStage.cs @@ -9,6 +9,6 @@ namespace ZstdSharp.Unsafe ZSTDds_decompressLastBlock, ZSTDds_checkChecksum, ZSTDds_decodeSkippableHeader, - ZSTDds_skipFrame + ZSTDds_skipFrame, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dStreamStage.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dStreamStage.cs index eea32f24..fb40404f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dStreamStage.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dStreamStage.cs @@ -6,6 +6,6 @@ namespace ZstdSharp.Unsafe zdss_loadHeader, zdss_read, zdss_load, - zdss_flush + zdss_flush, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictAttachPref_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictAttachPref_e.cs index 59e4eb33..33983b06 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictAttachPref_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictAttachPref_e.cs @@ -4,11 +4,14 @@ namespace ZstdSharp.Unsafe { /* Use the default heuristic. */ ZSTD_dictDefaultAttach = 0, + /* Never copy the dictionary. */ ZSTD_dictForceAttach = 1, + /* Always copy the dictionary. */ ZSTD_dictForceCopy = 2, + /* Always reload the dictionary */ - ZSTD_dictForceLoad = 3 + ZSTD_dictForceLoad = 3, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictContentType_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictContentType_e.cs index 5ecb77db..1a486605 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictContentType_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictContentType_e.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */ ZSTD_dct_auto = 0, + /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */ ZSTD_dct_rawContent = 1, + /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */ - ZSTD_dct_fullDict = 2 + ZSTD_dct_fullDict = 2, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictLoadMethod_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictLoadMethod_e.cs index 68fa70a6..ea0d1ac2 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictLoadMethod_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictLoadMethod_e.cs @@ -4,7 +4,8 @@ namespace ZstdSharp.Unsafe { /**< Copy dictionary content internally */ ZSTD_dlm_byCopy = 0, + /**< Reference dictionary content -- the dictionary buffer must outlive its users. */ - ZSTD_dlm_byRef = 1 + ZSTD_dlm_byRef = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictMode_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictMode_e.cs index 1db92e43..70b51926 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictMode_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictMode_e.cs @@ -5,6 +5,6 @@ namespace ZstdSharp.Unsafe ZSTD_noDict = 0, ZSTD_extDict = 1, ZSTD_dictMatchState = 2, - ZSTD_dedicatedDictSearch = 3 + ZSTD_dedicatedDictSearch = 3, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictTableLoadMethod_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictTableLoadMethod_e.cs index daffd0b5..2fab404e 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictTableLoadMethod_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictTableLoadMethod_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_dictTableLoadMethod_e { ZSTD_dtlm_fast, - ZSTD_dtlm_full + ZSTD_dtlm_full, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictUses_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictUses_e.cs index 8cc95816..37893d7e 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictUses_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_dictUses_e.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /* Use the dictionary indefinitely */ ZSTD_use_indefinitely = -1, + /* Do not use the dictionary (if one exists free it) */ ZSTD_dont_use = 0, + /* Use the dictionary once and set to ZSTD_dont_use */ - ZSTD_use_once = 1 + ZSTD_use_once = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyCTablesMetadata_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyCTablesMetadata_t.cs index 7b5b682d..747e4ba4 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyCTablesMetadata_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyCTablesMetadata_t.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public ZSTD_hufCTablesMetadata_t hufMetadata; public ZSTD_fseCTablesMetadata_t fseMetadata; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyCTables_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyCTables_t.cs index 297f30bc..57171c80 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyCTables_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyCTables_t.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public ZSTD_hufCTables_t huf; public ZSTD_fseCTables_t fse; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyDTables_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyDTables_t.cs index 9f336d2f..638cbd7b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyDTables_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_entropyDTables_t.cs @@ -6,14 +6,18 @@ namespace ZstdSharp.Unsafe { /* 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]; + #if NET8_0_OR_GREATER [InlineArray(513)] public unsafe struct _LLTable_e__FixedBuffer @@ -1336,4 +1340,4 @@ namespace ZstdSharp.Unsafe } #endif } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_forceIgnoreChecksum_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_forceIgnoreChecksum_e.cs index e6639008..db2885ee 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_forceIgnoreChecksum_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_forceIgnoreChecksum_e.cs @@ -4,6 +4,6 @@ namespace ZstdSharp.Unsafe { /* Note: this enum controls ZSTD_d_forceIgnoreChecksum */ ZSTD_d_validateChecksum = 0, - ZSTD_d_ignoreChecksum = 1 + ZSTD_d_ignoreChecksum = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_format_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_format_e.cs index 3d317880..c36c2ac7 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_format_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_format_e.cs @@ -4,9 +4,10 @@ namespace ZstdSharp.Unsafe { /* zstd frame format, specified in zstd_compression_format.md (default) */ ZSTD_f_zstd1 = 0, + /* Variant of zstd frame format, without initial 4-bytes magic number. * Useful to save 4 bytes per generated frame. * Decoder cannot recognise automatically this format, requiring this instruction. */ - ZSTD_f_zstd1_magicless = 1 + ZSTD_f_zstd1_magicless = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameHeader.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameHeader.cs index 8baa840d..23fb31b1 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameHeader.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameHeader.cs @@ -4,16 +4,19 @@ namespace ZstdSharp.Unsafe { /* 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; + /* for ZSTD_skippableFrame, contains the skippable magic variant [0-15] */ public uint dictID; public uint checksumFlag; public uint _reserved1; public uint _reserved2; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameParameters.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameParameters.cs index 54c50937..8f03e457 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameParameters.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameParameters.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /**< 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameProgression.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameProgression.cs index 22b7fece..8e9e46b0 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameProgression.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameProgression.cs @@ -4,15 +4,20 @@ namespace ZstdSharp.Unsafe { /* 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameSizeInfo.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameSizeInfo.cs index 65eca157..8d60693f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameSizeInfo.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameSizeInfo.cs @@ -12,4 +12,4 @@ namespace ZstdSharp.Unsafe public nuint compressedSize; public ulong decompressedBound; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameType_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameType_e.cs index 9a451ee9..4cb02e71 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameType_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_frameType_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_frameType_e { ZSTD_frame, - ZSTD_skippableFrame + ZSTD_skippableFrame, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseCTablesMetadata_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseCTablesMetadata_t.cs index a051c501..0e89337b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseCTablesMetadata_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseCTablesMetadata_t.cs @@ -12,7 +12,8 @@ namespace ZstdSharp.Unsafe 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseCTables_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseCTables_t.cs index b8b43c91..b716d40e 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseCTables_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseCTables_t.cs @@ -9,4 +9,4 @@ namespace ZstdSharp.Unsafe public FSE_repeat matchlength_repeatMode; public FSE_repeat litlength_repeatMode; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseState.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseState.cs index 46fe512b..f77f7116 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseState.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_fseState.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public nuint state; public ZSTD_seqSymbol* table; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_getAllMatchesFn.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_getAllMatchesFn.cs index ab0bb109..b6e49f4e 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_getAllMatchesFn.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_getAllMatchesFn.cs @@ -3,5 +3,14 @@ using System.Runtime.InteropServices; namespace ZstdSharp.Unsafe { [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public unsafe delegate uint ZSTD_getAllMatchesFn(ZSTD_match_t* param0, ZSTD_MatchState_t* param1, uint* param2, byte* param3, byte* param4, uint* rep, uint ll0, uint lengthToBeat); -} \ No newline at end of file + public unsafe delegate uint ZSTD_getAllMatchesFn( + ZSTD_match_t* param0, + ZSTD_MatchState_t* param1, + uint* param2, + byte* param3, + byte* param4, + uint* rep, + uint ll0, + uint lengthToBeat + ); +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_hufCTablesMetadata_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_hufCTablesMetadata_t.cs index 7a2b7604..d4b5d4da 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_hufCTablesMetadata_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_hufCTablesMetadata_t.cs @@ -14,4 +14,4 @@ namespace ZstdSharp.Unsafe public fixed byte hufDesBuffer[128]; public nuint hufDesSize; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_hufCTables_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_hufCTables_t.cs index 00311fbb..a697fa63 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_hufCTables_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_hufCTables_t.cs @@ -6,6 +6,7 @@ namespace ZstdSharp.Unsafe { public _CTable_e__FixedBuffer CTable; public HUF_repeat repeatMode; + #if NET8_0_OR_GREATER [InlineArray(257)] public unsafe struct _CTable_e__FixedBuffer @@ -276,4 +277,4 @@ namespace ZstdSharp.Unsafe } #endif } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_inBuffer_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_inBuffer_s.cs index 660b1612..7a92d443 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_inBuffer_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_inBuffer_s.cs @@ -7,9 +7,11 @@ namespace ZstdSharp.Unsafe { /**< 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_indexResetPolicy_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_indexResetPolicy_e.cs index 3f225a7e..ed9ef92b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_indexResetPolicy_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_indexResetPolicy_e.cs @@ -8,6 +8,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_indexResetPolicy_e { ZSTDirp_continue, - ZSTDirp_reset + ZSTDirp_reset, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_litLocation_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_litLocation_e.cs index 8136cc51..69160e0b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_litLocation_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_litLocation_e.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /* Stored entirely within litExtraBuffer */ ZSTD_not_in_dst = 0, + /* Stored entirely within dst (in memory after current output write) */ ZSTD_in_dst = 1, + /* Split between litExtraBuffer and dst */ - ZSTD_split = 2 + ZSTD_split = 2, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_literalCompressionMode_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_literalCompressionMode_e.cs index 6382a0c8..63d35773 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_literalCompressionMode_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_literalCompressionMode_e.cs @@ -6,10 +6,12 @@ namespace ZstdSharp.Unsafe * Negative compression levels will be uncompressed, and positive compression * levels will be compressed. */ ZSTD_lcm_auto = 0, + /**< Always attempt Huffman compression. Uncompressed literals will still be * emitted if Huffman compression is not profitable. */ ZSTD_lcm_huffman = 1, + /**< Always emit uncompressed literals. */ - ZSTD_lcm_uncompressed = 2 + ZSTD_lcm_uncompressed = 2, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_localDict.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_localDict.cs index 9f8fa4a4..9cbcf7c7 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_localDict.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_localDict.cs @@ -8,4 +8,4 @@ namespace ZstdSharp.Unsafe public ZSTD_dictContentType_e dictContentType; public ZSTD_CDict_s* cdict; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_longLengthType_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_longLengthType_e.cs index a31f06b2..37c62011 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_longLengthType_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_longLengthType_e.cs @@ -5,9 +5,11 @@ namespace ZstdSharp.Unsafe { /* no longLengthType */ ZSTD_llt_none = 0, + /* represents a long literal */ ZSTD_llt_literalLength = 1, + /* represents a long match */ - ZSTD_llt_matchLength = 2 + ZSTD_llt_matchLength = 2, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_longOffset_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_longOffset_e.cs index 2c67c67b..3f67603d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_longOffset_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_longOffset_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_longOffset_e { ZSTD_lo_isRegularOffset, - ZSTD_lo_isLongOffset = 1 + ZSTD_lo_isLongOffset = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_match_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_match_t.cs index b8a5f3d3..5bf187cc 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_match_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_match_t.cs @@ -7,7 +7,8 @@ namespace ZstdSharp.Unsafe { /* Offset sumtype code for the match, using ZSTD_storeSeq() format */ public uint off; + /* Raw length of match */ public uint len; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_nextInputType_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_nextInputType_e.cs index af341a4f..ee85055b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_nextInputType_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_nextInputType_e.cs @@ -7,6 +7,6 @@ namespace ZstdSharp.Unsafe ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, - ZSTDnit_skippableFrame + ZSTDnit_skippableFrame, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_optLdm_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_optLdm_t.cs index adc1a38f..6ac86bf8 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_optLdm_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_optLdm_t.cs @@ -5,11 +5,14 @@ namespace ZstdSharp.Unsafe { /* 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_optimal_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_optimal_t.cs index e7d7f5cb..42661778 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_optimal_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_optimal_t.cs @@ -4,13 +4,17 @@ namespace ZstdSharp.Unsafe { /* price from beginning of segment to this position */ public int price; + /* offset of previous match */ public uint off; + /* length of previous match */ public uint mlen; + /* nb of literals since previous match */ public uint litlen; + /* offset history after previous match */ public fixed uint rep[3]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_outBuffer_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_outBuffer_s.cs index 6ecb86ad..9411454d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_outBuffer_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_outBuffer_s.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /**< 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_overlap_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_overlap_e.cs index 02941dd0..79bafe5b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_overlap_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_overlap_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_overlap_e { ZSTD_no_overlap, - ZSTD_overlap_src_before_dst + ZSTD_overlap_src_before_dst, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_paramSwitch_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_paramSwitch_e.cs index 99acee5c..c769bcbb 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_paramSwitch_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_paramSwitch_e.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /* Let the library automatically determine whether the feature shall be enabled */ ZSTD_ps_auto = 0, + /* Force-enable the feature */ ZSTD_ps_enable = 1, + /* Do not use the feature */ - ZSTD_ps_disable = 2 + ZSTD_ps_disable = 2, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_parameters.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_parameters.cs index cff10cd1..01441a69 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_parameters.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_parameters.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public ZSTD_compressionParameters cParams; public ZSTD_frameParameters fParams; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_prefixDict_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_prefixDict_s.cs index d3fc6d93..25eb0435 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_prefixDict_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_prefixDict_s.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public nuint dictSize; public ZSTD_dictContentType_e dictContentType; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_refMultipleDDicts_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_refMultipleDDicts_e.cs index 36ebf249..d8dd23e1 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_refMultipleDDicts_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_refMultipleDDicts_e.cs @@ -4,6 +4,6 @@ namespace ZstdSharp.Unsafe { /* Note: this enum controls ZSTD_d_refMultipleDDicts */ ZSTD_rmd_refSingleDDict = 0, - ZSTD_rmd_refMultipleDDicts = 1 + ZSTD_rmd_refMultipleDDicts = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_resetTarget_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_resetTarget_e.cs index 417ff5b6..ebb3d5d9 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_resetTarget_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_resetTarget_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_resetTarget_e { ZSTD_resetTarget_CDict, - ZSTD_resetTarget_CCtx + ZSTD_resetTarget_CCtx, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_seqSymbol.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_seqSymbol.cs index f78770e5..cf5d742a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_seqSymbol.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_seqSymbol.cs @@ -6,6 +6,7 @@ namespace ZstdSharp.Unsafe public byte nbAdditionalBits; public byte nbBits; public uint baseValue; + public ZSTD_seqSymbol(ushort nextState, byte nbAdditionalBits, byte nbBits, uint baseValue) { this.nextState = nextState; @@ -14,4 +15,4 @@ namespace ZstdSharp.Unsafe this.baseValue = baseValue; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_seqSymbol_header.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_seqSymbol_header.cs index 16925999..57348731 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_seqSymbol_header.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_seqSymbol_header.cs @@ -8,4 +8,4 @@ namespace ZstdSharp.Unsafe public uint fastMode; public uint tableLog; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_sequenceFormat_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_sequenceFormat_e.cs index ed8d9c8e..5fc47842 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_sequenceFormat_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_sequenceFormat_e.cs @@ -4,7 +4,8 @@ namespace ZstdSharp.Unsafe { /* ZSTD_Sequence[] has no block delimiters, just sequences */ ZSTD_sf_noBlockDelimiters = 0, + /* ZSTD_Sequence[] contains explicit block delimiters */ - ZSTD_sf_explicitBlockDelimiters = 1 + ZSTD_sf_explicitBlockDelimiters = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_strategy.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_strategy.cs index e4e5e4b5..e6d8fb08 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_strategy.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_strategy.cs @@ -11,6 +11,6 @@ namespace ZstdSharp.Unsafe ZSTD_btlazy2 = 6, ZSTD_btopt = 7, ZSTD_btultra = 8, - ZSTD_btultra2 = 9 + ZSTD_btultra2 = 9, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_symbolEncodingTypeStats_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_symbolEncodingTypeStats_t.cs index bcfc57b2..143226b1 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_symbolEncodingTypeStats_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_symbolEncodingTypeStats_t.cs @@ -9,8 +9,9 @@ namespace ZstdSharp.Unsafe 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; public int longOffsets; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_tableFillPurpose_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_tableFillPurpose_e.cs index 8bb66024..47a92f8f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_tableFillPurpose_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_tableFillPurpose_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum ZSTD_tableFillPurpose_e { ZSTD_tfp_forCCtx, - ZSTD_tfp_forCDict + ZSTD_tfp_forCDict, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_window_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_window_t.cs index 46325d7b..53d92b75 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_window_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZSTD_window_t.cs @@ -4,18 +4,23 @@ namespace ZstdSharp.Unsafe { /* 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Zdict.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Zdict.cs index ba6262bc..0b50173d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Zdict.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Zdict.cs @@ -17,9 +17,24 @@ namespace ZstdSharp.Unsafe 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) + 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 blockSizeMax = (nuint)( + 1 << 17 < 1 << (int)@params->cParams.windowLog + ? 1 << 17 + : 1 << (int)@params->cParams.windowLog + ); nuint cSize; if (srcSize > blockSizeMax) srcSize = blockSizeMax; @@ -125,7 +140,17 @@ namespace ZstdSharp.Unsafe 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) + 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]; /* no final ; */ @@ -143,11 +168,17 @@ namespace ZstdSharp.Unsafe { dict = null, zc = null, - workPlace = null + workPlace = null, }; ZSTD_parameters @params; - uint u, huffLog = 11, Offlog = 8, mlLog = 9, llLog = 9, total; - nuint pos = 0, errorCode; + 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)); @@ -155,7 +186,9 @@ namespace ZstdSharp.Unsafe uint* wksp = stackalloc uint[1216]; if (offcodeMax > 30) { - eSize = unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionaryCreation_failed)); + eSize = unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionaryCreation_failed) + ); goto _cleanup; } @@ -173,7 +206,14 @@ namespace ZstdSharp.Unsafe if (compressionLevel == 0) compressionLevel = 3; @params = ZSTD_getParams(compressionLevel, 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.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(1 << 17); if (esr.dict == null || esr.zc == null || esr.workPlace == null) @@ -184,19 +224,35 @@ namespace ZstdSharp.Unsafe for (u = 0; u < nbFiles; u++) { - ZDICT_countEStats(esr, &@params, countLit, offcodeCount, matchLengthCount, litLengthCount, repOffset, (sbyte*)srcBuffer + pos, fileSizes[u], notificationLevel); + ZDICT_countEStats( + esr, + &@params, + countLit, + offcodeCount, + matchLengthCount, + litLengthCount, + repOffset, + (sbyte*)srcBuffer + pos, + fileSizes[u], + notificationLevel + ); pos += fileSizes[u]; } if (notificationLevel >= 4) { - for (u = 0; u <= offcodeMax; u++) - { - } + for (u = 0; u <= offcodeMax; u++) { } } { - nuint maxNbBits = HUF_buildCTable_wksp(hufTable, countLit, 255, huffLog, wksp, sizeof(uint) * 1216); + nuint maxNbBits = HUF_buildCTable_wksp( + hufTable, + countLit, + 255, + huffLog, + wksp, + sizeof(uint) * 1216 + ); if (ERR_isError(maxNbBits)) { eSize = maxNbBits; @@ -206,7 +262,14 @@ namespace ZstdSharp.Unsafe if (maxNbBits == 8) { ZDICT_flatLit(countLit); - maxNbBits = HUF_buildCTable_wksp(hufTable, countLit, 255, huffLog, wksp, sizeof(uint) * 1216); + maxNbBits = HUF_buildCTable_wksp( + hufTable, + countLit, + 255, + huffLog, + wksp, + sizeof(uint) * 1216 + ); assert(maxNbBits == 9); } @@ -222,7 +285,14 @@ namespace ZstdSharp.Unsafe total = 0; for (u = 0; u <= offcodeMax; u++) total += offcodeCount[u]; - errorCode = FSE_normalizeCount(offcodeNCount, Offlog, offcodeCount, total, offcodeMax, 1); + errorCode = FSE_normalizeCount( + offcodeNCount, + Offlog, + offcodeCount, + total, + offcodeMax, + 1 + ); if (ERR_isError(errorCode)) { eSize = errorCode; @@ -233,7 +303,14 @@ namespace ZstdSharp.Unsafe total = 0; for (u = 0; u <= 52; u++) total += matchLengthCount[u]; - errorCode = FSE_normalizeCount(matchLengthNCount, mlLog, matchLengthCount, total, 52, 1); + errorCode = FSE_normalizeCount( + matchLengthNCount, + mlLog, + matchLengthCount, + total, + 52, + 1 + ); if (ERR_isError(errorCode)) { eSize = errorCode; @@ -253,7 +330,15 @@ namespace ZstdSharp.Unsafe llLog = (uint)errorCode; { - nuint hhSize = HUF_writeCTable_wksp(dstPtr, maxDstSize, hufTable, 255, huffLog, wksp, sizeof(uint) * 1216); + nuint hhSize = HUF_writeCTable_wksp( + dstPtr, + maxDstSize, + hufTable, + 255, + huffLog, + wksp, + sizeof(uint) * 1216 + ); if (ERR_isError(hhSize)) { eSize = hhSize; @@ -314,7 +399,7 @@ namespace ZstdSharp.Unsafe MEM_writeLE32(dstPtr + 4, repStartValue[1]); MEM_writeLE32(dstPtr + 8, repStartValue[2]); eSize += 12; - _cleanup: + _cleanup: ZSTD_freeCDict(esr.dict); ZSTD_freeCCtx(esr.zc); free(esr.workPlace); @@ -368,7 +453,16 @@ namespace ZstdSharp.Unsafe * * 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) + 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]; @@ -391,7 +485,17 @@ namespace ZstdSharp.Unsafe hSize = 8; { - nuint eSize = ZDICT_analyzeEntropy(header + hSize, 256 - hSize, compressionLevel, samplesBuffer, samplesSizes, nbSamples, customDictContent, dictContentSize, notificationLevel); + nuint eSize = ZDICT_analyzeEntropy( + header + hSize, + 256 - hSize, + compressionLevel, + samplesBuffer, + samplesSizes, + nbSamples, + customDictContent, + dictContentSize, + notificationLevel + ); if (ZDICT_isError(eSize)) return eSize; hSize += eSize; @@ -434,13 +538,31 @@ namespace ZstdSharp.Unsafe } } - private static nuint ZDICT_addEntropyTablesFromBuffer_advanced(void* dictBuffer, nuint dictContentSize, nuint dictBufferCapacity, void* samplesBuffer, nuint* samplesSizes, uint nbSamples, ZDICT_params_t @params) + 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((sbyte*)dictBuffer + hSize, dictBufferCapacity - hSize, compressionLevel, samplesBuffer, samplesSizes, nbSamples, (sbyte*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize, notificationLevel); + nuint eSize = ZDICT_analyzeEntropy( + (sbyte*)dictBuffer + hSize, + dictBufferCapacity - hSize, + compressionLevel, + samplesBuffer, + samplesSizes, + nbSamples, + (sbyte*)dictBuffer + dictBufferCapacity - dictContentSize, + dictContentSize, + notificationLevel + ); if (ZDICT_isError(eSize)) return eSize; hSize += eSize; @@ -448,15 +570,25 @@ namespace ZstdSharp.Unsafe MEM_writeLE32(dictBuffer, 0xEC30A437); { - ulong randomID = ZSTD_XXH64((sbyte*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize, 0); + ulong randomID = ZSTD_XXH64( + (sbyte*)dictBuffer + dictBufferCapacity - dictContentSize, + dictContentSize, + 0 + ); uint compliantID = (uint)(randomID % ((1U << 31) - 32768) + 32768); uint dictID = @params.dictID != 0 ? @params.dictID : compliantID; MEM_writeLE32((sbyte*)dictBuffer + 4, dictID); } if (hSize + dictContentSize < dictBufferCapacity) - memmove((sbyte*)dictBuffer + hSize, (sbyte*)dictBuffer + dictBufferCapacity - dictContentSize, dictContentSize); - return dictBufferCapacity < hSize + dictContentSize ? dictBufferCapacity : hSize + dictContentSize; + memmove( + (sbyte*)dictBuffer + hSize, + (sbyte*)dictBuffer + dictBufferCapacity - dictContentSize, + dictContentSize + ); + return dictBufferCapacity < hSize + dictContentSize + ? dictBufferCapacity + : hSize + dictContentSize; } /*! ZDICT_trainFromBuffer(): @@ -479,23 +611,47 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZDICT_trainFromBuffer( + void* dictBuffer, + nuint dictBufferCapacity, + void* samplesBuffer, + nuint* samplesSizes, + uint nbSamples + ) { ZDICT_fastCover_params_t @params; - @params = new ZDICT_fastCover_params_t - { - d = 8, - steps = 4 - }; + @params = new ZDICT_fastCover_params_t { d = 8, steps = 4 }; @params.zParams.compressionLevel = 3; - return ZDICT_optimizeTrainFromBuffer_fastCover(dictBuffer, dictBufferCapacity, samplesBuffer, samplesSizes, nbSamples, &@params); + 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) + public static nuint ZDICT_addEntropyTablesFromBuffer( + void* dictBuffer, + nuint dictContentSize, + nuint dictBufferCapacity, + void* samplesBuffer, + nuint* samplesSizes, + uint nbSamples + ) { ZDICT_params_t @params; @params = new ZDICT_params_t(); - return ZDICT_addEntropyTablesFromBuffer_advanced(dictBuffer, dictContentSize, dictBufferCapacity, samplesBuffer, samplesSizes, nbSamples, @params); + return ZDICT_addEntropyTablesFromBuffer_advanced( + dictBuffer, + dictContentSize, + dictBufferCapacity, + samplesBuffer, + samplesSizes, + nbSamples, + @params + ); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/Zstd.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/Zstd.cs index 875d50ba..f3a0972d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/Zstd.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/Zstd.cs @@ -2,6 +2,10 @@ namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { - private static readonly ZSTD_customMem ZSTD_defaultCMem = new ZSTD_customMem(customAlloc: null, customFree: null, opaque: null); + private static readonly ZSTD_customMem ZSTD_defaultCMem = new ZSTD_customMem( + customAlloc: null, + customFree: null, + opaque: null + ); } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCommon.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCommon.cs index 4cb1c744..92312bf3 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCommon.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCommon.cs @@ -46,4 +46,4 @@ namespace ZstdSharp.Unsafe return ERR_getErrorString(code); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompress.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompress.cs index e5eee65b..46b98f3a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompress.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompress.cs @@ -1,9 +1,9 @@ -using static ZstdSharp.UnsafeHelper; using System; -using System.Runtime.InteropServices; +using System.Diagnostics; using System.Numerics; using System.Runtime.CompilerServices; -using System.Diagnostics; +using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -22,7 +22,12 @@ namespace ZstdSharp.Unsafe */ public static nuint ZSTD_compressBound(nuint srcSize) { - nuint r = srcSize >= (sizeof(nuint) == 8 ? 0xFF00FF00FF00FF00UL : 0xFF00FF00U) ? 0 : srcSize + (srcSize >> 8) + (srcSize < 128 << 10 ? (128 << 10) - srcSize >> 11 : 0); + nuint r = + srcSize >= (sizeof(nuint) == 8 ? 0xFF00FF00FF00FF00UL : 0xFF00FF00U) + ? 0 + : srcSize + + (srcSize >> 8) + + (srcSize < 128 << 10 ? (128 << 10) - srcSize >> 11 : 0); if (r == 0) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)); return r; @@ -36,11 +41,7 @@ namespace ZstdSharp.Unsafe private static void ZSTD_initCCtx(ZSTD_CCtx_s* cctx, ZSTD_customMem memManager) { assert(cctx != null); - *cctx = new ZSTD_CCtx_s - { - customMem = memManager, - bmi2 = 0 - }; + *cctx = new ZSTD_CCtx_s { customMem = memManager, bmi2 = 0 }; { nuint err = ZSTD_CCtx_reset(cctx, ZSTD_ResetDirective.ZSTD_reset_parameters); assert(!ERR_isError(err)); @@ -49,10 +50,16 @@ namespace ZstdSharp.Unsafe public static ZSTD_CCtx_s* ZSTD_createCCtx_advanced(ZSTD_customMem customMem) { - if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + if ( + ((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) + != 0 + ) return null; { - ZSTD_CCtx_s* cctx = (ZSTD_CCtx_s*)ZSTD_customMalloc((nuint)sizeof(ZSTD_CCtx_s), customMem); + ZSTD_CCtx_s* cctx = (ZSTD_CCtx_s*)ZSTD_customMalloc( + (nuint)sizeof(ZSTD_CCtx_s), + customMem + ); if (cctx == null) return null; ZSTD_initCCtx(cctx, customMem); @@ -89,19 +96,50 @@ namespace ZstdSharp.Unsafe return null; if (((nuint)workspace & 7) != 0) return null; - ZSTD_cwksp_init(&ws, workspace, workspaceSize, ZSTD_cwksp_static_alloc_e.ZSTD_cwksp_static_alloc); + 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 null; *cctx = new ZSTD_CCtx_s(); ZSTD_cwksp_move(&cctx->workspace, &ws); cctx->staticSize = workspaceSize; - if (ZSTD_cwksp_check_available(&cctx->workspace, (nuint)(((8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) : 8208) + 2 * sizeof(ZSTD_compressedBlockState_t))) == 0) + if ( + ZSTD_cwksp_check_available( + &cctx->workspace, + (nuint)( + ( + (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 + ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) + : 8208 + ) + + 2 * sizeof(ZSTD_compressedBlockState_t) + ) + ) == 0 + ) return 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->tmpWorkspace = ZSTD_cwksp_reserve_object(&cctx->workspace, (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) : 8208); - cctx->tmpWkspSize = (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) : 8208; + 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->tmpWorkspace = ZSTD_cwksp_reserve_object( + &cctx->workspace, + (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 + ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) + : 8208 + ); + cctx->tmpWkspSize = + (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 + ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) + : 8208; cctx->bmi2 = 0; return cctx; } @@ -166,7 +204,10 @@ namespace ZstdSharp.Unsafe { if (cctx == null) return 0; - return (nuint)(cctx->workspace.workspace == cctx ? 0 : sizeof(ZSTD_CCtx_s)) + ZSTD_cwksp_sizeof(&cctx->workspace) + ZSTD_sizeof_localDict(cctx->localDict) + ZSTD_sizeof_mtctx(cctx); + return (nuint)(cctx->workspace.workspace == cctx ? 0 : 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) @@ -183,7 +224,9 @@ namespace ZstdSharp.Unsafe /* 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; + 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 @@ -192,11 +235,18 @@ namespace ZstdSharp.Unsafe private static int ZSTD_rowMatchFinderUsed(ZSTD_strategy strategy, ZSTD_paramSwitch_e mode) { assert(mode != ZSTD_paramSwitch_e.ZSTD_ps_auto); - return ZSTD_rowMatchFinderSupported(strategy) != 0 && mode == ZSTD_paramSwitch_e.ZSTD_ps_enable ? 1 : 0; + return + ZSTD_rowMatchFinderSupported(strategy) != 0 + && mode == ZSTD_paramSwitch_e.ZSTD_ps_enable + ? 1 + : 0; } /* Returns row matchfinder usage given an initial mode and cParams */ - private static ZSTD_paramSwitch_e ZSTD_resolveRowMatchFinderMode(ZSTD_paramSwitch_e mode, ZSTD_compressionParameters* cParams) + private static ZSTD_paramSwitch_e ZSTD_resolveRowMatchFinderMode( + ZSTD_paramSwitch_e mode, + ZSTD_compressionParameters* cParams + ) { if (mode != ZSTD_paramSwitch_e.ZSTD_ps_auto) return mode; @@ -209,29 +259,48 @@ namespace ZstdSharp.Unsafe } /* Returns block splitter usage (generally speaking, when using slower/stronger compression modes) */ - private static ZSTD_paramSwitch_e ZSTD_resolveBlockSplitterMode(ZSTD_paramSwitch_e mode, ZSTD_compressionParameters* cParams) + private static ZSTD_paramSwitch_e ZSTD_resolveBlockSplitterMode( + ZSTD_paramSwitch_e mode, + ZSTD_compressionParameters* cParams + ) { if (mode != ZSTD_paramSwitch_e.ZSTD_ps_auto) return mode; - return cParams->strategy >= ZSTD_strategy.ZSTD_btopt && cParams->windowLog >= 17 ? ZSTD_paramSwitch_e.ZSTD_ps_enable : ZSTD_paramSwitch_e.ZSTD_ps_disable; + return cParams->strategy >= ZSTD_strategy.ZSTD_btopt && cParams->windowLog >= 17 + ? ZSTD_paramSwitch_e.ZSTD_ps_enable + : ZSTD_paramSwitch_e.ZSTD_ps_disable; } /* Returns 1 if the arguments indicate that we should allocate a chainTable, 0 otherwise */ - private static int ZSTD_allocateChainTable(ZSTD_strategy strategy, ZSTD_paramSwitch_e useRowMatchFinder, uint forDDSDict) + private static int ZSTD_allocateChainTable( + ZSTD_strategy strategy, + ZSTD_paramSwitch_e useRowMatchFinder, + uint forDDSDict + ) { assert(useRowMatchFinder != ZSTD_paramSwitch_e.ZSTD_ps_auto); - return forDDSDict != 0 || strategy != ZSTD_strategy.ZSTD_fast && ZSTD_rowMatchFinderUsed(strategy, useRowMatchFinder) == 0 ? 1 : 0; + return + forDDSDict != 0 + || strategy != ZSTD_strategy.ZSTD_fast + && ZSTD_rowMatchFinderUsed(strategy, useRowMatchFinder) == 0 + ? 1 + : 0; } /* Returns ZSTD_ps_enable if compression parameters are such that we should * enable long distance matching (wlog >= 27, strategy >= btopt). * Returns ZSTD_ps_disable otherwise. */ - private static ZSTD_paramSwitch_e ZSTD_resolveEnableLdm(ZSTD_paramSwitch_e mode, ZSTD_compressionParameters* cParams) + private static ZSTD_paramSwitch_e ZSTD_resolveEnableLdm( + ZSTD_paramSwitch_e mode, + ZSTD_compressionParameters* cParams + ) { if (mode != ZSTD_paramSwitch_e.ZSTD_ps_auto) return mode; - return cParams->strategy >= ZSTD_strategy.ZSTD_btopt && cParams->windowLog >= 27 ? ZSTD_paramSwitch_e.ZSTD_ps_enable : ZSTD_paramSwitch_e.ZSTD_ps_disable; + return cParams->strategy >= ZSTD_strategy.ZSTD_btopt && cParams->windowLog >= 27 + ? ZSTD_paramSwitch_e.ZSTD_ps_enable + : ZSTD_paramSwitch_e.ZSTD_ps_disable; } private static int ZSTD_resolveExternalSequenceValidation(int mode) @@ -252,7 +321,10 @@ namespace ZstdSharp.Unsafe } } - private static ZSTD_paramSwitch_e ZSTD_resolveExternalRepcodeSearch(ZSTD_paramSwitch_e value, int cLevel) + private static ZSTD_paramSwitch_e ZSTD_resolveExternalRepcodeSearch( + ZSTD_paramSwitch_e value, + int cLevel + ) { if (value != ZSTD_paramSwitch_e.ZSTD_ps_auto) return value; @@ -270,15 +342,24 @@ namespace ZstdSharp.Unsafe * If so, the tags need to be removed in ZSTD_resetCCtx_byCopyingCDict. */ private static int ZSTD_CDictIndicesAreTagged(ZSTD_compressionParameters* cParams) { - return cParams->strategy == ZSTD_strategy.ZSTD_fast || cParams->strategy == ZSTD_strategy.ZSTD_dfast ? 1 : 0; + return + cParams->strategy == ZSTD_strategy.ZSTD_fast + || cParams->strategy == ZSTD_strategy.ZSTD_dfast + ? 1 + : 0; } - private static ZSTD_CCtx_params_s ZSTD_makeCCtxParamsFromCParams(ZSTD_compressionParameters cParams) + private static ZSTD_CCtx_params_s ZSTD_makeCCtxParamsFromCParams( + ZSTD_compressionParameters cParams + ) { ZSTD_CCtx_params_s cctxParams; ZSTD_CCtxParams_init(&cctxParams, 3); cctxParams.cParams = cParams; - cctxParams.ldmParams.enableLdm = ZSTD_resolveEnableLdm(cctxParams.ldmParams.enableLdm, &cParams); + cctxParams.ldmParams.enableLdm = ZSTD_resolveEnableLdm( + cctxParams.ldmParams.enableLdm, + &cParams + ); if (cctxParams.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable) { ZSTD_ldm_adjustParameters(&cctxParams.ldmParams, &cParams); @@ -286,11 +367,22 @@ namespace ZstdSharp.Unsafe assert(cctxParams.ldmParams.hashRateLog < 32); } - cctxParams.postBlockSplitter = ZSTD_resolveBlockSplitterMode(cctxParams.postBlockSplitter, &cParams); - cctxParams.useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams.useRowMatchFinder, &cParams); - cctxParams.validateSequences = ZSTD_resolveExternalSequenceValidation(cctxParams.validateSequences); + cctxParams.postBlockSplitter = ZSTD_resolveBlockSplitterMode( + cctxParams.postBlockSplitter, + &cParams + ); + cctxParams.useRowMatchFinder = ZSTD_resolveRowMatchFinderMode( + cctxParams.useRowMatchFinder, + &cParams + ); + cctxParams.validateSequences = ZSTD_resolveExternalSequenceValidation( + cctxParams.validateSequences + ); cctxParams.maxBlockSize = ZSTD_resolveMaxBlockSize(cctxParams.maxBlockSize); - cctxParams.searchForExternalRepcodes = ZSTD_resolveExternalRepcodeSearch(cctxParams.searchForExternalRepcodes, cctxParams.compressionLevel); + cctxParams.searchForExternalRepcodes = ZSTD_resolveExternalRepcodeSearch( + cctxParams.searchForExternalRepcodes, + cctxParams.compressionLevel + ); assert(ZSTD_checkCParams(cParams) == 0); return cctxParams; } @@ -298,9 +390,15 @@ namespace ZstdSharp.Unsafe 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) + if ( + ((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) + != 0 + ) return null; - @params = (ZSTD_CCtx_params_s*)ZSTD_customCalloc((nuint)sizeof(ZSTD_CCtx_params_s), customMem); + @params = (ZSTD_CCtx_params_s*)ZSTD_customCalloc( + (nuint)sizeof(ZSTD_CCtx_params_s), + customMem + ); if (@params == null) { return null; @@ -356,17 +454,17 @@ namespace ZstdSharp.Unsafe * 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) + 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)); } - *cctxParams = new ZSTD_CCtx_params_s - { - compressionLevel = compressionLevel - }; + *cctxParams = new ZSTD_CCtx_params_s { compressionLevel = compressionLevel }; cctxParams->fParams.contentSizeFlag = 1; return 0; } @@ -375,7 +473,11 @@ namespace ZstdSharp.Unsafe * Initializes `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) + private static void ZSTD_CCtxParams_init_internal( + ZSTD_CCtx_params_s* cctxParams, + ZSTD_parameters* @params, + int compressionLevel + ) { assert(ZSTD_checkCParams(@params->cParams) == 0); *cctxParams = new ZSTD_CCtx_params_s @@ -383,20 +485,37 @@ namespace ZstdSharp.Unsafe cParams = @params->cParams, fParams = @params->fParams, compressionLevel = compressionLevel, - useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams->useRowMatchFinder, &@params->cParams), - postBlockSplitter = ZSTD_resolveBlockSplitterMode(cctxParams->postBlockSplitter, &@params->cParams) + useRowMatchFinder = ZSTD_resolveRowMatchFinderMode( + cctxParams->useRowMatchFinder, + &@params->cParams + ), + postBlockSplitter = ZSTD_resolveBlockSplitterMode( + cctxParams->postBlockSplitter, + &@params->cParams + ), }; - cctxParams->ldmParams.enableLdm = ZSTD_resolveEnableLdm(cctxParams->ldmParams.enableLdm, &@params->cParams); - cctxParams->validateSequences = ZSTD_resolveExternalSequenceValidation(cctxParams->validateSequences); + cctxParams->ldmParams.enableLdm = ZSTD_resolveEnableLdm( + cctxParams->ldmParams.enableLdm, + &@params->cParams + ); + cctxParams->validateSequences = ZSTD_resolveExternalSequenceValidation( + cctxParams->validateSequences + ); cctxParams->maxBlockSize = ZSTD_resolveMaxBlockSize(cctxParams->maxBlockSize); - cctxParams->searchForExternalRepcodes = ZSTD_resolveExternalRepcodeSearch(cctxParams->searchForExternalRepcodes, compressionLevel); + cctxParams->searchForExternalRepcodes = ZSTD_resolveExternalRepcodeSearch( + cctxParams->searchForExternalRepcodes, + compressionLevel + ); } /*! 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) + public static nuint ZSTD_CCtxParams_init_advanced( + ZSTD_CCtx_params_s* cctxParams, + ZSTD_parameters @params + ) { if (cctxParams == null) { @@ -419,7 +538,10 @@ namespace ZstdSharp.Unsafe * Sets cctxParams' cParams and fParams from params, but otherwise leaves them alone. * @param params Validated zstd parameters. */ - private static void ZSTD_CCtxParams_setZstdParams(ZSTD_CCtx_params_s* cctxParams, ZSTD_parameters* @params) + private static void ZSTD_CCtxParams_setZstdParams( + ZSTD_CCtx_params_s* cctxParams, + ZSTD_parameters* @params + ) { assert(ZSTD_checkCParams(@params->cParams) == 0); cctxParams->cParams = @params->cParams; @@ -440,7 +562,7 @@ namespace ZstdSharp.Unsafe { error = 0, lowerBound = 0, - upperBound = 0 + upperBound = 0, }; switch (param) { @@ -454,7 +576,12 @@ namespace ZstdSharp.Unsafe return bounds; case ZSTD_cParameter.ZSTD_c_hashLog: bounds.lowerBound = 6; - bounds.upperBound = (sizeof(nuint) == 4 ? 30 : 31) < 30 ? sizeof(nuint) == 4 ? 30 : 31 : 30; + bounds.upperBound = + (sizeof(nuint) == 4 ? 30 : 31) < 30 + ? sizeof(nuint) == 4 + ? 30 + : 31 + : 30; return bounds; case ZSTD_cParameter.ZSTD_c_chainLog: bounds.lowerBound = 6; @@ -510,7 +637,12 @@ namespace ZstdSharp.Unsafe return bounds; case ZSTD_cParameter.ZSTD_c_ldmHashLog: bounds.lowerBound = 6; - bounds.upperBound = (sizeof(nuint) == 4 ? 30 : 31) < 30 ? sizeof(nuint) == 4 ? 30 : 31 : 30; + bounds.upperBound = + (sizeof(nuint) == 4 ? 30 : 31) < 30 + ? sizeof(nuint) == 4 + ? 30 + : 31 + : 30; return bounds; case ZSTD_cParameter.ZSTD_c_ldmMinMatch: bounds.lowerBound = 4; @@ -598,7 +730,9 @@ namespace ZstdSharp.Unsafe bounds.upperBound = (int)ZSTD_paramSwitch_e.ZSTD_ps_disable; return bounds; default: - bounds.error = unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + bounds.error = unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); return bounds; } } @@ -678,7 +812,11 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_CCtx_setParameter( + ZSTD_CCtx_s* cctx, + ZSTD_cParameter param, + int value + ) { if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) { @@ -697,7 +835,9 @@ namespace ZstdSharp.Unsafe case ZSTD_cParameter.ZSTD_c_nbWorkers: if (value != 0 && cctx->staticSize != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); } break; @@ -741,7 +881,9 @@ namespace ZstdSharp.Unsafe case ZSTD_cParameter.ZSTD_c_experimentalParam19: break; default: - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); } return ZSTD_CCtxParams_setParameter(&cctx->requestedParams, param, value); @@ -755,42 +897,53 @@ namespace ZstdSharp.Unsafe * @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) + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam2, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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)) { - nuint err_code = ZSTD_cParam_clampBounds(param, &value); - if (ERR_isError(err_code)) - { - return err_code; - } + return err_code; } - - if (value == 0) - CCtxParams->compressionLevel = 3; - else - CCtxParams->compressionLevel = value; - if (CCtxParams->compressionLevel >= 0) - return (nuint)CCtxParams->compressionLevel; - return 0; } + 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)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->cParams.windowLog = (uint)value; @@ -799,7 +952,9 @@ namespace ZstdSharp.Unsafe if (value != 0) if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_hashLog, value) == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->cParams.hashLog = (uint)value; @@ -808,7 +963,9 @@ namespace ZstdSharp.Unsafe if (value != 0) if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_chainLog, value) == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->cParams.chainLog = (uint)value; @@ -817,7 +974,9 @@ namespace ZstdSharp.Unsafe if (value != 0) if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_searchLog, value) == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->cParams.searchLog = (uint)value; @@ -826,7 +985,9 @@ namespace ZstdSharp.Unsafe if (value != 0) if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_minMatch, value) == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->cParams.minMatch = (uint)value; @@ -834,7 +995,9 @@ namespace ZstdSharp.Unsafe 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)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->cParams.targetLength = (uint)value; @@ -843,7 +1006,9 @@ namespace ZstdSharp.Unsafe if (value != 0) if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_strategy, value) == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->cParams.strategy = (ZSTD_strategy)value; @@ -861,29 +1026,43 @@ namespace ZstdSharp.Unsafe 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 + ) { - 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; + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } + CCtxParams->attachDictPref = pref; + return (nuint)CCtxParams->attachDictPref; + } + case ZSTD_cParameter.ZSTD_c_experimentalParam5: + { + ZSTD_paramSwitch_e lcm = (ZSTD_paramSwitch_e)value; + if ( + ZSTD_cParam_withinBounds( + ZSTD_cParameter.ZSTD_c_experimentalParam5, + (int)lcm + ) == 0 + ) { - ZSTD_paramSwitch_e lcm = (ZSTD_paramSwitch_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; + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } + CCtxParams->literalCompressionMode = lcm; + return (nuint)CCtxParams->literalCompressionMode; + } + case ZSTD_cParameter.ZSTD_c_nbWorkers: { nuint err_code = ZSTD_cParam_clampBounds(param, &value); @@ -898,6 +1077,7 @@ namespace ZstdSharp.Unsafe case ZSTD_cParameter.ZSTD_c_jobSize: if (value != 0 && value < 512 * (1 << 10)) value = 512 * (1 << 10); + { nuint err_code = ZSTD_cParam_clampBounds(param, &value); if (ERR_isError(err_code)) @@ -911,7 +1091,10 @@ namespace ZstdSharp.Unsafe return CCtxParams->jobSize; case ZSTD_cParameter.ZSTD_c_overlapLog: { - nuint err_code = ZSTD_cParam_clampBounds(ZSTD_cParameter.ZSTD_c_overlapLog, &value); + nuint err_code = ZSTD_cParam_clampBounds( + ZSTD_cParameter.ZSTD_c_overlapLog, + &value + ); if (ERR_isError(err_code)) { return err_code; @@ -922,7 +1105,10 @@ namespace ZstdSharp.Unsafe return (nuint)CCtxParams->overlapLog; case ZSTD_cParameter.ZSTD_c_experimentalParam1: { - nuint err_code = ZSTD_cParam_clampBounds(ZSTD_cParameter.ZSTD_c_overlapLog, &value); + nuint err_code = ZSTD_cParam_clampBounds( + ZSTD_cParameter.ZSTD_c_overlapLog, + &value + ); if (ERR_isError(err_code)) { return err_code; @@ -935,9 +1121,16 @@ namespace ZstdSharp.Unsafe CCtxParams->enableDedicatedDictSearch = value != 0 ? 1 : 0; return (nuint)CCtxParams->enableDedicatedDictSearch; case ZSTD_cParameter.ZSTD_c_enableLongDistanceMatching: - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_enableLongDistanceMatching, value) == 0) + if ( + ZSTD_cParam_withinBounds( + ZSTD_cParameter.ZSTD_c_enableLongDistanceMatching, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->ldmParams.enableLdm = (ZSTD_paramSwitch_e)value; @@ -946,34 +1139,50 @@ namespace ZstdSharp.Unsafe if (value != 0) if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_ldmHashLog, value) == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_ldmMinMatch, value) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_ldmBucketSizeLog, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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 != 0) - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_ldmHashRateLog, value) == 0) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_ldmHashRateLog, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->ldmParams.hashRateLog = (uint)value; @@ -982,9 +1191,14 @@ namespace ZstdSharp.Unsafe if (value != 0) { value = value > 1340 ? value : 1340; - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_targetCBlockSize, value) == 0) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_targetCBlockSize, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } } @@ -992,113 +1206,184 @@ namespace ZstdSharp.Unsafe return CCtxParams->targetCBlockSize; case ZSTD_cParameter.ZSTD_c_experimentalParam7: if (value != 0) - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam7, value) == 0) + if ( + ZSTD_cParam_withinBounds( + ZSTD_cParameter.ZSTD_c_experimentalParam7, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam9, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam10, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam11, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam12, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam13, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->postBlockSplitter = (ZSTD_paramSwitch_e)value; return (nuint)CCtxParams->postBlockSplitter; case ZSTD_cParameter.ZSTD_c_experimentalParam20: - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam20, value) == 0) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam20, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->preBlockSplitter_level = value; return (nuint)CCtxParams->preBlockSplitter_level; case ZSTD_cParameter.ZSTD_c_experimentalParam14: - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam14, value) == 0) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam14, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->useRowMatchFinder = (ZSTD_paramSwitch_e)value; return (nuint)CCtxParams->useRowMatchFinder; case ZSTD_cParameter.ZSTD_c_experimentalParam15: - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam15, value) == 0) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam15, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->deterministicRefPrefix = !(value == 0) ? 1 : 0; return (nuint)CCtxParams->deterministicRefPrefix; case ZSTD_cParameter.ZSTD_c_experimentalParam16: - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam16, value) == 0) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam16, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->prefetchCDictTables = (ZSTD_paramSwitch_e)value; return (nuint)CCtxParams->prefetchCDictTables; case ZSTD_cParameter.ZSTD_c_experimentalParam17: - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam17, value) == 0) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam17, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->enableMatchFinderFallback = value; return (nuint)CCtxParams->enableMatchFinderFallback; case ZSTD_cParameter.ZSTD_c_experimentalParam18: if (value != 0) - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam18, value) == 0) + if ( + ZSTD_cParam_withinBounds( + ZSTD_cParameter.ZSTD_c_experimentalParam18, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } assert(value >= 0); CCtxParams->maxBlockSize = (nuint)value; return CCtxParams->maxBlockSize; case ZSTD_cParameter.ZSTD_c_experimentalParam19: - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam19, value) == 0) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam19, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } CCtxParams->searchForExternalRepcodes = (ZSTD_paramSwitch_e)value; return (nuint)CCtxParams->searchForExternalRepcodes; default: - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); } } @@ -1107,7 +1392,11 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_CCtx_getParameter( + ZSTD_CCtx_s* cctx, + ZSTD_cParameter param, + int* value + ) { return ZSTD_CCtxParams_getParameter(&cctx->requestedParams, param, value); } @@ -1117,7 +1406,11 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_CCtxParams_getParameter( + ZSTD_CCtx_params_s* CCtxParams, + ZSTD_cParameter param, + int* value + ) { switch (param) { @@ -1240,7 +1533,9 @@ namespace ZstdSharp.Unsafe *value = (int)CCtxParams->searchForExternalRepcodes; break; default: - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); } return 0; @@ -1253,7 +1548,10 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_CCtx_setParametersUsingCCtxParams( + ZSTD_CCtx_s* cctx, + ZSTD_CCtx_params_s* @params + ) { if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) { @@ -1276,7 +1574,10 @@ namespace ZstdSharp.Unsafe * @return 0 on success, or an error code (can be checked with ZSTD_isError()). * On failure, no parameters are updated. */ - public static nuint ZSTD_CCtx_setCParams(ZSTD_CCtx_s* cctx, ZSTD_compressionParameters cparams) + public static nuint ZSTD_CCtx_setCParams( + ZSTD_CCtx_s* cctx, + ZSTD_compressionParameters cparams + ) { { /* only update if all parameters are valid */ @@ -1288,7 +1589,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_windowLog, (int)cparams.windowLog); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_windowLog, + (int)cparams.windowLog + ); if (ERR_isError(err_code)) { return err_code; @@ -1296,7 +1601,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_chainLog, (int)cparams.chainLog); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_chainLog, + (int)cparams.chainLog + ); if (ERR_isError(err_code)) { return err_code; @@ -1304,7 +1613,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_hashLog, (int)cparams.hashLog); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_hashLog, + (int)cparams.hashLog + ); if (ERR_isError(err_code)) { return err_code; @@ -1312,7 +1625,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_searchLog, (int)cparams.searchLog); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_searchLog, + (int)cparams.searchLog + ); if (ERR_isError(err_code)) { return err_code; @@ -1320,7 +1637,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_minMatch, (int)cparams.minMatch); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_minMatch, + (int)cparams.minMatch + ); if (ERR_isError(err_code)) { return err_code; @@ -1328,7 +1649,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_targetLength, (int)cparams.targetLength); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_targetLength, + (int)cparams.targetLength + ); if (ERR_isError(err_code)) { return err_code; @@ -1336,7 +1661,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_strategy, (int)cparams.strategy); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_strategy, + (int)cparams.strategy + ); if (ERR_isError(err_code)) { return err_code; @@ -1353,7 +1682,11 @@ namespace ZstdSharp.Unsafe public static nuint ZSTD_CCtx_setFParams(ZSTD_CCtx_s* cctx, ZSTD_frameParameters fparams) { { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_contentSizeFlag, fparams.contentSizeFlag != 0 ? 1 : 0); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_contentSizeFlag, + fparams.contentSizeFlag != 0 ? 1 : 0 + ); if (ERR_isError(err_code)) { return err_code; @@ -1361,7 +1694,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_checksumFlag, fparams.checksumFlag != 0 ? 1 : 0); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_checksumFlag, + fparams.checksumFlag != 0 ? 1 : 0 + ); if (ERR_isError(err_code)) { return err_code; @@ -1369,7 +1706,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_dictIDFlag, fparams.noDictIDFlag == 0 ? 1 : 0); + nuint err_code = ZSTD_CCtx_setParameter( + cctx, + ZSTD_cParameter.ZSTD_c_dictIDFlag, + fparams.noDictIDFlag == 0 ? 1 : 0 + ); if (ERR_isError(err_code)) { return err_code; @@ -1466,7 +1807,14 @@ namespace ZstdSharp.Unsafe 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); + 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)); @@ -1480,7 +1828,13 @@ namespace ZstdSharp.Unsafe * 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) + 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) { @@ -1522,9 +1876,19 @@ namespace ZstdSharp.Unsafe /*! 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) + 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); + 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+ @@ -1551,7 +1915,13 @@ namespace ZstdSharp.Unsafe */ 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); + 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+ @@ -1610,13 +1980,23 @@ namespace ZstdSharp.Unsafe * 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); + 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) + 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) { @@ -1638,13 +2018,19 @@ namespace ZstdSharp.Unsafe * 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) + 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 ( + reset == ZSTD_ResetDirective.ZSTD_reset_parameters + || reset == ZSTD_ResetDirective.ZSTD_reset_session_and_parameters + ) { if (cctx->streamStage != ZSTD_cStreamStage.zcss_init) { @@ -1663,12 +2049,18 @@ namespace ZstdSharp.Unsafe @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) + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_chainLog, (int)cParams.chainLog) + == 0 + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); } @@ -1678,22 +2070,36 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); } - if (ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_searchLog, (int)cParams.searchLog) == 0) + 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) + 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) + 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) + if ( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_strategy, (int)cParams.strategy) + == 0 + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); } @@ -1704,7 +2110,9 @@ namespace ZstdSharp.Unsafe /** ZSTD_clampCParams() : * make CParam values within valid range. * @return : valid CParams */ - private static ZSTD_compressionParameters ZSTD_clampCParams(ZSTD_compressionParameters cParams) + private static ZSTD_compressionParameters ZSTD_clampCParams( + ZSTD_compressionParameters cParams + ) { { ZSTD_bounds bounds = ZSTD_cParam_getBounds(ZSTD_cParameter.ZSTD_c_windowLog); @@ -1816,7 +2224,13 @@ namespace ZstdSharp.Unsafe * `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, ZSTD_paramSwitch_e useRowMatchFinder) + private static ZSTD_compressionParameters ZSTD_adjustCParams_internal( + ZSTD_compressionParameters cPar, + ulong srcSize, + nuint dictSize, + ZSTD_CParamMode_e mode, + ZSTD_paramSwitch_e useRowMatchFinder + ) { /* (1<<9) + 1 */ const ulong minSrcSize = 513; @@ -1860,7 +2274,10 @@ namespace ZstdSharp.Unsafe if (cPar.windowLog < 10) cPar.windowLog = 10; - if (mode == ZSTD_CParamMode_e.ZSTD_cpm_createCDict && ZSTD_CDictIndicesAreTagged(&cPar) != 0) + if ( + mode == ZSTD_CParamMode_e.ZSTD_cpm_createCDict + && ZSTD_CDictIndicesAreTagged(&cPar) != 0 + ) { const uint maxShortCacheHashLog = 32 - 8; if (cPar.hashLog > maxShortCacheHashLog) @@ -1879,7 +2296,10 @@ namespace ZstdSharp.Unsafe if (ZSTD_rowMatchFinderUsed(cPar.strategy, useRowMatchFinder) != 0) { /* Switch to 32-entry rows if searchLog is 5 (or more) */ - uint rowLog = cPar.searchLog <= 4 ? 4 : cPar.searchLog <= 6 ? cPar.searchLog : 6; + uint rowLog = + cPar.searchLog <= 4 ? 4 + : cPar.searchLog <= 6 ? cPar.searchLog + : 6; const uint maxRowHashLog = 32 - 8; uint maxHashLog = maxRowHashLog + rowLog; assert(cPar.hashLog >= rowLog); @@ -1898,15 +2318,28 @@ namespace ZstdSharp.Unsafe * `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) + 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, ZSTD_paramSwitch_e.ZSTD_ps_auto); + return ZSTD_adjustCParams_internal( + cPar, + srcSize, + dictSize, + ZSTD_CParamMode_e.ZSTD_cpm_unknown, + ZSTD_paramSwitch_e.ZSTD_ps_auto + ); } - private static void ZSTD_overrideCParams(ZSTD_compressionParameters* cParams, ZSTD_compressionParameters* overrides) + private static void ZSTD_overrideCParams( + ZSTD_compressionParameters* cParams, + ZSTD_compressionParameters* overrides + ) { if (overrides->windowLog != 0) cParams->windowLog = overrides->windowLog; @@ -1929,7 +2362,12 @@ namespace ZstdSharp.Unsafe * LDM and manually set compression parameters. * Note: srcSizeHint == 0 means 0! */ - private static ZSTD_compressionParameters ZSTD_getCParamsFromCCtxParams(ZSTD_CCtx_params_s* CCtxParams, ulong srcSizeHint, nuint dictSize, ZSTD_CParamMode_e mode) + private 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) @@ -1938,27 +2376,70 @@ namespace ZstdSharp.Unsafe srcSizeHint = (ulong)CCtxParams->srcSizeHint; } - cParams = ZSTD_getCParams_internal(CCtxParams->compressionLevel, srcSizeHint, dictSize, mode); + cParams = ZSTD_getCParams_internal( + CCtxParams->compressionLevel, + srcSizeHint, + dictSize, + mode + ); if (CCtxParams->ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable) cParams.windowLog = 27; ZSTD_overrideCParams(&cParams, &CCtxParams->cParams); assert(ZSTD_checkCParams(cParams) == 0); - return ZSTD_adjustCParams_internal(cParams, srcSizeHint, dictSize, mode, CCtxParams->useRowMatchFinder); + return ZSTD_adjustCParams_internal( + cParams, + srcSizeHint, + dictSize, + mode, + CCtxParams->useRowMatchFinder + ); } - private static nuint ZSTD_sizeof_matchState(ZSTD_compressionParameters* cParams, ZSTD_paramSwitch_e useRowMatchFinder, int enableDedicatedDictSearch, uint forCCtx) + private static nuint ZSTD_sizeof_matchState( + ZSTD_compressionParameters* cParams, + ZSTD_paramSwitch_e useRowMatchFinder, + int enableDedicatedDictSearch, + uint forCCtx + ) { /* chain table size should be 0 for fast or row-hash strategies */ - nuint chainSize = ZSTD_allocateChainTable(cParams->strategy, useRowMatchFinder, enableDedicatedDictSearch != 0 && forCCtx == 0 ? 1U : 0U) != 0 ? (nuint)1 << (int)cParams->chainLog : 0; + 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 = forCCtx != 0 && cParams->minMatch == 3 ? 17 < cParams->windowLog ? 17 : cParams->windowLog : 0; + uint hashLog3 = + forCCtx != 0 && cParams->minMatch == 3 + ? 17 < cParams->windowLog + ? 17 + : cParams->windowLog + : 0; nuint h3Size = hashLog3 != 0 ? (nuint)1 << (int)hashLog3 : 0; /* We don't use ZSTD_cwksp_alloc_size() here because the tables aren't * surrounded by redzones in ASAN. */ - nuint tableSpace = chainSize * sizeof(uint) + hSize * sizeof(uint) + h3Size * sizeof(uint); - nuint optPotentialSpace = ZSTD_cwksp_aligned64_alloc_size((52 + 1) * sizeof(uint)) + ZSTD_cwksp_aligned64_alloc_size((35 + 1) * sizeof(uint)) + ZSTD_cwksp_aligned64_alloc_size((31 + 1) * sizeof(uint)) + ZSTD_cwksp_aligned64_alloc_size((1 << 8) * sizeof(uint)) + ZSTD_cwksp_aligned64_alloc_size((nuint)(((1 << 12) + 3) * sizeof(ZSTD_match_t))) + ZSTD_cwksp_aligned64_alloc_size((nuint)(((1 << 12) + 3) * sizeof(ZSTD_optimal_t))); - nuint lazyAdditionalSpace = ZSTD_rowMatchFinderUsed(cParams->strategy, useRowMatchFinder) != 0 ? ZSTD_cwksp_aligned64_alloc_size(hSize) : 0; - nuint optSpace = forCCtx != 0 && cParams->strategy >= ZSTD_strategy.ZSTD_btopt ? optPotentialSpace : 0; + nuint tableSpace = + chainSize * sizeof(uint) + hSize * sizeof(uint) + h3Size * sizeof(uint); + nuint optPotentialSpace = + ZSTD_cwksp_aligned64_alloc_size((52 + 1) * sizeof(uint)) + + ZSTD_cwksp_aligned64_alloc_size((35 + 1) * sizeof(uint)) + + ZSTD_cwksp_aligned64_alloc_size((31 + 1) * sizeof(uint)) + + ZSTD_cwksp_aligned64_alloc_size((1 << 8) * sizeof(uint)) + + ZSTD_cwksp_aligned64_alloc_size((nuint)(((1 << 12) + 3) * sizeof(ZSTD_match_t))) + + ZSTD_cwksp_aligned64_alloc_size( + (nuint)(((1 << 12) + 3) * sizeof(ZSTD_optimal_t)) + ); + nuint lazyAdditionalSpace = + ZSTD_rowMatchFinderUsed(cParams->strategy, useRowMatchFinder) != 0 + ? ZSTD_cwksp_aligned64_alloc_size(hSize) + : 0; + nuint optSpace = + forCCtx != 0 && cParams->strategy >= ZSTD_strategy.ZSTD_btopt + ? optPotentialSpace + : 0; nuint slackSpace = ZSTD_cwksp_slack_space_required(); assert(useRowMatchFinder != ZSTD_paramSwitch_e.ZSTD_ps_auto); return tableSpace + optSpace + slackSpace + lazyAdditionalSpace; @@ -1972,37 +2453,98 @@ namespace ZstdSharp.Unsafe return blockSize / divider; } - private static nuint ZSTD_estimateCCtxSize_usingCCtxParams_internal(ZSTD_compressionParameters* cParams, ldmParams_t* ldmParams, int isStatic, ZSTD_paramSwitch_e useRowMatchFinder, nuint buffInSize, nuint buffOutSize, ulong pledgedSrcSize, int useSequenceProducer, nuint maxBlockSize) + private static nuint ZSTD_estimateCCtxSize_usingCCtxParams_internal( + ZSTD_compressionParameters* cParams, + ldmParams_t* ldmParams, + int isStatic, + ZSTD_paramSwitch_e useRowMatchFinder, + nuint buffInSize, + nuint buffOutSize, + ulong pledgedSrcSize, + int useSequenceProducer, + nuint maxBlockSize + ) { - nuint windowSize = (nuint)(1UL << (int)cParams->windowLog <= 1UL ? 1UL : 1UL << (int)cParams->windowLog <= pledgedSrcSize ? 1UL << (int)cParams->windowLog : pledgedSrcSize); - nuint blockSize = ZSTD_resolveMaxBlockSize(maxBlockSize) < windowSize ? ZSTD_resolveMaxBlockSize(maxBlockSize) : windowSize; + nuint windowSize = (nuint)( + 1UL << (int)cParams->windowLog <= 1UL ? 1UL + : 1UL << (int)cParams->windowLog <= pledgedSrcSize ? 1UL << (int)cParams->windowLog + : pledgedSrcSize + ); + nuint blockSize = + ZSTD_resolveMaxBlockSize(maxBlockSize) < windowSize + ? ZSTD_resolveMaxBlockSize(maxBlockSize) + : windowSize; nuint maxNbSeq = ZSTD_maxNbSeq(blockSize, cParams->minMatch, useSequenceProducer); - nuint tokenSpace = ZSTD_cwksp_alloc_size(32 + blockSize) + ZSTD_cwksp_aligned64_alloc_size(maxNbSeq * (nuint)sizeof(SeqDef_s)) + 3 * ZSTD_cwksp_alloc_size(maxNbSeq * sizeof(byte)); - nuint tmpWorkSpace = ZSTD_cwksp_alloc_size((8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) : 8208); - nuint blockStateSpace = 2 * ZSTD_cwksp_alloc_size((nuint)sizeof(ZSTD_compressedBlockState_t)); + nuint tokenSpace = + ZSTD_cwksp_alloc_size(32 + blockSize) + + ZSTD_cwksp_aligned64_alloc_size(maxNbSeq * (nuint)sizeof(SeqDef_s)) + + 3 * ZSTD_cwksp_alloc_size(maxNbSeq * sizeof(byte)); + nuint tmpWorkSpace = ZSTD_cwksp_alloc_size( + (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 + ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) + : 8208 + ); + nuint blockStateSpace = + 2 * ZSTD_cwksp_alloc_size((nuint)sizeof(ZSTD_compressedBlockState_t)); /* enableDedicatedDictSearch */ 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 == ZSTD_paramSwitch_e.ZSTD_ps_enable ? ZSTD_cwksp_aligned64_alloc_size(maxNbLdmSeq * (nuint)sizeof(rawSeq)) : 0; - nuint bufferSpace = ZSTD_cwksp_alloc_size(buffInSize) + ZSTD_cwksp_alloc_size(buffOutSize); + nuint ldmSeqSpace = + ldmParams->enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable + ? ZSTD_cwksp_aligned64_alloc_size(maxNbLdmSeq * (nuint)sizeof(rawSeq)) + : 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 maxNbExternalSeq = ZSTD_sequenceBound(blockSize); - nuint externalSeqSpace = useSequenceProducer != 0 ? ZSTD_cwksp_aligned64_alloc_size(maxNbExternalSeq * (nuint)sizeof(ZSTD_Sequence)) : 0; - nuint neededSpace = cctxSpace + tmpWorkSpace + blockStateSpace + ldmSpace + ldmSeqSpace + matchStateSize + tokenSpace + bufferSpace + externalSeqSpace; + nuint externalSeqSpace = + useSequenceProducer != 0 + ? ZSTD_cwksp_aligned64_alloc_size( + maxNbExternalSeq * (nuint)sizeof(ZSTD_Sequence) + ) + : 0; + nuint neededSpace = + cctxSpace + + tmpWorkSpace + + blockStateSpace + + ldmSpace + + ldmSeqSpace + + matchStateSize + + tokenSpace + + bufferSpace + + externalSeqSpace; 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_paramSwitch_e useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(@params->useRowMatchFinder, &cParams); + ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams( + @params, + unchecked(0UL - 1), + 0, + ZSTD_CParamMode_e.ZSTD_cpm_noAttachDict + ); + ZSTD_paramSwitch_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), ZSTD_hasExtSeqProd(@params), @params->maxBlockSize); + return ZSTD_estimateCCtxSize_usingCCtxParams_internal( + &cParams, + &@params->ldmParams, + 1, + useRowMatchFinder, + 0, + 0, + unchecked(0UL - 1), + ZSTD_hasExtSeqProd(@params), + @params->maxBlockSize + ); } public static nuint ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams) @@ -2026,18 +2568,26 @@ namespace ZstdSharp.Unsafe } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_srcSizeTiers => new ulong[4] - { - 16 * (1 << 10), - 128 * (1 << 10), - 256 * (1 << 10), - unchecked(0UL - 1) - }; - private static ulong* srcSizeTiers => (ulong*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_srcSizeTiers)); + private static ReadOnlySpan Span_srcSizeTiers => + new ulong[4] { 16 * (1 << 10), 128 * (1 << 10), 256 * (1 << 10), unchecked(0UL - 1) }; + private static ulong* srcSizeTiers => + (ulong*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_srcSizeTiers) + ); #else - private static readonly ulong* srcSizeTiers = GetArrayPointer(new ulong[4] { (ulong)(16 * (1 << 10)), (ulong)(128 * (1 << 10)), (ulong)(256 * (1 << 10)), (unchecked(0UL - 1)) }); + private static readonly ulong* srcSizeTiers = GetArrayPointer( + new ulong[4] + { + (ulong)(16 * (1 << 10)), + (ulong)(128 * (1 << 10)), + (ulong)(256 * (1 << 10)), + (unchecked(0UL - 1)), + } + ); #endif + private static nuint ZSTD_estimateCCtxSize_internal(int compressionLevel) { int tier = 0; @@ -2045,8 +2595,16 @@ namespace ZstdSharp.Unsafe for (; tier < 4; ++tier) { /* Choose the set of cParams for a given level across all srcSizes that give the largest cctxSize */ - 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; + 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; @@ -2083,7 +2641,11 @@ namespace ZstdSharp.Unsafe { int level; nuint memBudget = 0; - for (level = compressionLevel < 1 ? compressionLevel : 1; level <= compressionLevel; level++) + for ( + level = compressionLevel < 1 ? compressionLevel : 1; + level <= compressionLevel; + level++ + ) { /* Ensure monotonically increasing memory usage as compression level increases */ nuint newMB = ZSTD_estimateCCtxSize_internal(level); @@ -2102,16 +2664,46 @@ namespace ZstdSharp.Unsafe } { - ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams(@params, unchecked(0UL - 1), 0, ZSTD_CParamMode_e.ZSTD_cpm_noAttachDict); - nuint blockSize = ZSTD_resolveMaxBlockSize(@params->maxBlockSize) < (nuint)1 << (int)cParams.windowLog ? ZSTD_resolveMaxBlockSize(@params->maxBlockSize) : (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_paramSwitch_e useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(@params->useRowMatchFinder, &@params->cParams); - return ZSTD_estimateCCtxSize_usingCCtxParams_internal(&cParams, &@params->ldmParams, 1, useRowMatchFinder, inBuffSize, outBuffSize, unchecked(0UL - 1), ZSTD_hasExtSeqProd(@params), @params->maxBlockSize); + ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams( + @params, + unchecked(0UL - 1), + 0, + ZSTD_CParamMode_e.ZSTD_cpm_noAttachDict + ); + nuint blockSize = + ZSTD_resolveMaxBlockSize(@params->maxBlockSize) + < (nuint)1 << (int)cParams.windowLog + ? ZSTD_resolveMaxBlockSize(@params->maxBlockSize) + : (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_paramSwitch_e useRowMatchFinder = ZSTD_resolveRowMatchFinderMode( + @params->useRowMatchFinder, + &@params->cParams + ); + return ZSTD_estimateCCtxSize_usingCCtxParams_internal( + &cParams, + &@params->ldmParams, + 1, + useRowMatchFinder, + inBuffSize, + outBuffSize, + unchecked(0UL - 1), + ZSTD_hasExtSeqProd(@params), + @params->maxBlockSize + ); } } - public static nuint ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams) + public static nuint ZSTD_estimateCStreamSize_usingCParams( + ZSTD_compressionParameters cParams + ) { ZSTD_CCtx_params_s initialParams = ZSTD_makeCCtxParamsFromCParams(cParams); if (ZSTD_rowMatchFinderSupported(cParams.strategy) != 0) @@ -2133,7 +2725,12 @@ namespace ZstdSharp.Unsafe 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); + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal( + compressionLevel, + unchecked(0UL - 1), + 0, + ZSTD_CParamMode_e.ZSTD_cpm_noAttachDict + ); return ZSTD_estimateCStreamSize_usingCParams(cParams); } @@ -2161,7 +2758,11 @@ namespace ZstdSharp.Unsafe { int level; nuint memBudget = 0; - for (level = compressionLevel < 1 ? compressionLevel : 1; level <= compressionLevel; level++) + for ( + level = compressionLevel < 1 ? compressionLevel : 1; + level <= compressionLevel; + level++ + ) { nuint newMB = ZSTD_estimateCStreamSize_internal(level); if (newMB > memBudget) @@ -2214,7 +2815,10 @@ namespace ZstdSharp.Unsafe } [Conditional("DEBUG")] - private static void ZSTD_assertEqualCParams(ZSTD_compressionParameters cParams1, ZSTD_compressionParameters cParams2) + private static void ZSTD_assertEqualCParams( + ZSTD_compressionParameters cParams1, + ZSTD_compressionParameters cParams2 + ) { assert(cParams1.windowLog == cParams2.windowLog); assert(cParams1.chainLog == cParams2.chainLog); @@ -2265,12 +2869,35 @@ namespace ZstdSharp.Unsafe ms->hashSalt = ZSTD_bitmix(ms->hashSalt, 8) ^ ZSTD_bitmix(ms->hashSaltEntropy, 4); } - private static nuint ZSTD_reset_matchState(ZSTD_MatchState_t* ms, ZSTD_cwksp* ws, ZSTD_compressionParameters* cParams, ZSTD_paramSwitch_e useRowMatchFinder, ZSTD_compResetPolicy_e crp, ZSTD_indexResetPolicy_e forceResetIndex, ZSTD_resetTarget_e forWho) + private static nuint ZSTD_reset_matchState( + ZSTD_MatchState_t* ms, + ZSTD_cwksp* ws, + ZSTD_compressionParameters* cParams, + ZSTD_paramSwitch_e useRowMatchFinder, + ZSTD_compResetPolicy_e crp, + ZSTD_indexResetPolicy_e forceResetIndex, + ZSTD_resetTarget_e forWho + ) { /* disable chain table allocation for fast or row-based strategies */ - 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 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 = forWho == ZSTD_resetTarget_e.ZSTD_resetTarget_CCtx && cParams->minMatch == 3 ? 17 < cParams->windowLog ? 17 : cParams->windowLog : 0; + uint hashLog3 = + 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_paramSwitch_e.ZSTD_ps_auto); if (forceResetIndex == ZSTD_indexResetPolicy_e.ZSTDirp_reset) @@ -2314,20 +2941,41 @@ namespace ZstdSharp.Unsafe } { - uint rowLog = cParams->searchLog <= 4 ? 4 : cParams->searchLog <= 6 ? cParams->searchLog : 6; + uint rowLog = + cParams->searchLog <= 4 ? 4 + : cParams->searchLog <= 6 ? cParams->searchLog + : 6; assert(cParams->hashLog >= rowLog); ms->rowHashLog = cParams->hashLog - rowLog; } } - if (forWho == ZSTD_resetTarget_e.ZSTD_resetTarget_CCtx && cParams->strategy >= ZSTD_strategy.ZSTD_btopt) + if ( + forWho == ZSTD_resetTarget_e.ZSTD_resetTarget_CCtx + && cParams->strategy >= ZSTD_strategy.ZSTD_btopt + ) { ms->opt.litFreq = (uint*)ZSTD_cwksp_reserve_aligned64(ws, (1 << 8) * sizeof(uint)); - ms->opt.litLengthFreq = (uint*)ZSTD_cwksp_reserve_aligned64(ws, (35 + 1) * sizeof(uint)); - ms->opt.matchLengthFreq = (uint*)ZSTD_cwksp_reserve_aligned64(ws, (52 + 1) * sizeof(uint)); - ms->opt.offCodeFreq = (uint*)ZSTD_cwksp_reserve_aligned64(ws, (31 + 1) * sizeof(uint)); - ms->opt.matchTable = (ZSTD_match_t*)ZSTD_cwksp_reserve_aligned64(ws, (nuint)(((1 << 12) + 3) * sizeof(ZSTD_match_t))); - ms->opt.priceTable = (ZSTD_optimal_t*)ZSTD_cwksp_reserve_aligned64(ws, (nuint)(((1 << 12) + 3) * sizeof(ZSTD_optimal_t))); + ms->opt.litLengthFreq = (uint*)ZSTD_cwksp_reserve_aligned64( + ws, + (35 + 1) * sizeof(uint) + ); + ms->opt.matchLengthFreq = (uint*)ZSTD_cwksp_reserve_aligned64( + ws, + (52 + 1) * sizeof(uint) + ); + ms->opt.offCodeFreq = (uint*)ZSTD_cwksp_reserve_aligned64( + ws, + (31 + 1) * sizeof(uint) + ); + ms->opt.matchTable = (ZSTD_match_t*)ZSTD_cwksp_reserve_aligned64( + ws, + (nuint)(((1 << 12) + 3) * sizeof(ZSTD_match_t)) + ); + ms->opt.priceTable = (ZSTD_optimal_t*)ZSTD_cwksp_reserve_aligned64( + ws, + (nuint)(((1 << 12) + 3) * sizeof(ZSTD_optimal_t)) + ); } ms->cParams = *cParams; @@ -2341,7 +2989,11 @@ namespace ZstdSharp.Unsafe private static int ZSTD_indexTooCloseToMax(ZSTD_window_t w) { - return (nuint)(w.nextSrc - w.@base) > (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20)) - 16 * (1 << 20) ? 1 : 0; + return + (nuint)(w.nextSrc - w.@base) + > (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20)) - 16 * (1 << 20) + ? 1 + : 0; } /** ZSTD_dictTooBig(): @@ -2351,7 +3003,11 @@ namespace ZstdSharp.Unsafe */ private static int ZSTD_dictTooBig(nuint loadedDictSize) { - return loadedDictSize > unchecked((uint)-1) - (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20)) ? 1 : 0; + return + loadedDictSize + > unchecked((uint)-1) - (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20)) + ? 1 + : 0; } /*! ZSTD_resetCCtx_internal() : @@ -2360,7 +3016,14 @@ namespace ZstdSharp.Unsafe * 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) + 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))); @@ -2379,16 +3042,54 @@ namespace ZstdSharp.Unsafe } { - 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 = @params->maxBlockSize < windowSize ? @params->maxBlockSize : windowSize; - nuint maxNbSeq = ZSTD_maxNbSeq(blockSize, @params->cParams.minMatch, ZSTD_hasExtSeqProd(@params)); - 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 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 = + @params->maxBlockSize < windowSize ? @params->maxBlockSize : windowSize; + nuint maxNbSeq = ZSTD_maxNbSeq( + blockSize, + @params->cParams.minMatch, + ZSTD_hasExtSeqProd(@params) + ); + 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, ZSTD_hasExtSeqProd(@params), @params->maxBlockSize); + 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, + ZSTD_hasExtSeqProd(@params), + @params->maxBlockSize + ); { nuint err_code = neededSpace; if (ERR_isError(err_code)) @@ -2407,7 +3108,9 @@ namespace ZstdSharp.Unsafe { if (zc->staticSize != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); } needsIndexReset = ZSTD_indexResetPolicy_e.ZSTDirp_reset; @@ -2420,32 +3123,60 @@ namespace ZstdSharp.Unsafe } } - assert(ZSTD_cwksp_check_available(ws, (nuint)(2 * sizeof(ZSTD_compressedBlockState_t))) != 0); - zc->blockState.prevCBlock = (ZSTD_compressedBlockState_t*)ZSTD_cwksp_reserve_object(ws, (nuint)sizeof(ZSTD_compressedBlockState_t)); + assert( + ZSTD_cwksp_check_available( + ws, + (nuint)(2 * 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)); + 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)); + 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)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); } - zc->tmpWorkspace = ZSTD_cwksp_reserve_object(ws, (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) : 8208); + zc->tmpWorkspace = ZSTD_cwksp_reserve_object( + ws, + (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 + ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) + : 8208 + ); if (zc->tmpWorkspace == null) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); } - zc->tmpWkspSize = (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) : 8208; + zc->tmpWkspSize = + (8 << 10) + 512 + sizeof(uint) * (52 + 2) > 8208 + ? (8 << 10) + 512 + sizeof(uint) * (52 + 2) + : 8208; } } ZSTD_cwksp_clear(ws); zc->blockState.matchState.cParams = @params->cParams; - zc->blockState.matchState.prefetchCDictTables = @params->prefetchCDictTables == ZSTD_paramSwitch_e.ZSTD_ps_enable ? 1 : 0; + zc->blockState.matchState.prefetchCDictTables = + @params->prefetchCDictTables == ZSTD_paramSwitch_e.ZSTD_ps_enable ? 1 : 0; zc->pledgedSrcSizePlusOne = pledgedSrcSize + 1; zc->consumedSrcSize = 0; zc->producedCSize = 0; @@ -2458,21 +3189,38 @@ namespace ZstdSharp.Unsafe zc->dictContentSize = 0; ZSTD_reset_compressedBlockState(zc->blockState.prevCBlock); { - nuint err_code = ZSTD_reset_matchState(&zc->blockState.matchState, ws, &@params->cParams, @params->useRowMatchFinder, crp, needsIndexReset, ZSTD_resetTarget_e.ZSTD_resetTarget_CCtx); + 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)) { return err_code; } } - zc->seqStore.sequencesStart = (SeqDef_s*)ZSTD_cwksp_reserve_aligned64(ws, maxNbSeq * (nuint)sizeof(SeqDef_s)); + zc->seqStore.sequencesStart = (SeqDef_s*)ZSTD_cwksp_reserve_aligned64( + ws, + maxNbSeq * (nuint)sizeof(SeqDef_s) + ); if (@params->ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable) { /* TODO: avoid memset? */ nuint ldmHSize = (nuint)1 << (int)@params->ldmParams.hashLog; - zc->ldmState.hashTable = (ldmEntry_t*)ZSTD_cwksp_reserve_aligned64(ws, ldmHSize * (nuint)sizeof(ldmEntry_t)); + zc->ldmState.hashTable = (ldmEntry_t*)ZSTD_cwksp_reserve_aligned64( + ws, + ldmHSize * (nuint)sizeof(ldmEntry_t) + ); memset(zc->ldmState.hashTable, 0, (uint)(ldmHSize * (nuint)sizeof(ldmEntry_t))); - zc->ldmSequences = (rawSeq*)ZSTD_cwksp_reserve_aligned64(ws, maxNbLdmSeq * (nuint)sizeof(rawSeq)); + zc->ldmSequences = (rawSeq*)ZSTD_cwksp_reserve_aligned64( + ws, + maxNbLdmSeq * (nuint)sizeof(rawSeq) + ); zc->maxNbLdmSequences = maxNbLdmSeq; ZSTD_window_init(&zc->ldmState.window); zc->ldmState.loadedDictEnd = 0; @@ -2482,7 +3230,10 @@ namespace ZstdSharp.Unsafe { nuint maxNbExternalSeq = ZSTD_sequenceBound(blockSize); zc->extSeqBufCapacity = maxNbExternalSeq; - zc->extSeqBuf = (ZSTD_Sequence*)ZSTD_cwksp_reserve_aligned64(ws, maxNbExternalSeq * (nuint)sizeof(ZSTD_Sequence)); + zc->extSeqBuf = (ZSTD_Sequence*)ZSTD_cwksp_reserve_aligned64( + ws, + maxNbExternalSeq * (nuint)sizeof(ZSTD_Sequence) + ); } zc->seqStore.litStart = ZSTD_cwksp_reserve_buffer(ws, blockSize + 32); @@ -2495,7 +3246,9 @@ namespace ZstdSharp.Unsafe if (@params->ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable) { /* TODO: avoid memset? */ - nuint numBuckets = (nuint)1 << (int)(@params->ldmParams.hashLog - @params->ldmParams.bucketSizeLog); + nuint numBuckets = + (nuint)1 + << (int)(@params->ldmParams.hashLog - @params->ldmParams.bucketSizeLog); zc->ldmState.bucketOffsets = ZSTD_cwksp_reserve_buffer(ws, numBuckets); memset(zc->ldmState.bucketOffsets, 0, (uint)numBuckets); } @@ -2523,15 +3276,50 @@ namespace ZstdSharp.Unsafe assert(ZSTD_window_hasExtDict(cctx->blockState.matchState.window) == 0); } - private static readonly 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) + private static readonly 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; + 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) + 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; @@ -2542,11 +3330,24 @@ namespace ZstdSharp.Unsafe ZSTD_dedicatedDictSearch_revertCParams(&adjusted_cdict_cParams); } - @params.cParams = ZSTD_adjustCParams_internal(adjusted_cdict_cParams, pledgedSrcSize, cdict->dictContentSize, ZSTD_CParamMode_e.ZSTD_cpm_attachDict, @params.useRowMatchFinder); + @params.cParams = ZSTD_adjustCParams_internal( + adjusted_cdict_cParams, + pledgedSrcSize, + cdict->dictContentSize, + ZSTD_CParamMode_e.ZSTD_cpm_attachDict, + @params.useRowMatchFinder + ); @params.cParams.windowLog = windowLog; @params.useRowMatchFinder = cdict->useRowMatchFinder; { - nuint err_code = ZSTD_resetCCtx_internal(cctx, &@params, pledgedSrcSize, 0, ZSTD_compResetPolicy_e.ZSTDcrp_makeClean, zbuff); + nuint err_code = ZSTD_resetCCtx_internal( + cctx, + &@params, + pledgedSrcSize, + 0, + ZSTD_compResetPolicy_e.ZSTDcrp_makeClean, + zbuff + ); if (ERR_isError(err_code)) { return err_code; @@ -2557,28 +3358,43 @@ namespace ZstdSharp.Unsafe } { - uint cdictEnd = (uint)(cdict->matchState.window.nextSrc - cdict->matchState.window.@base); + uint cdictEnd = (uint)( + cdict->matchState.window.nextSrc - cdict->matchState.window.@base + ); uint cdictLen = cdictEnd - cdict->matchState.window.dictLimit; if (cdictLen != 0) { cctx->blockState.matchState.dictMatchState = &cdict->matchState; if (cctx->blockState.matchState.window.dictLimit < cdictEnd) { - cctx->blockState.matchState.window.nextSrc = cctx->blockState.matchState.window.@base + 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->blockState.matchState.loadedDictEnd = cctx->blockState + .matchState + .window + .dictLimit; } } cctx->dictID = cdict->dictID; cctx->dictContentSize = cdict->dictContentSize; - memcpy(cctx->blockState.prevCBlock, &cdict->cBlockState, (uint)sizeof(ZSTD_compressedBlockState_t)); + memcpy( + cctx->blockState.prevCBlock, + &cdict->cBlockState, + (uint)sizeof(ZSTD_compressedBlockState_t) + ); return 0; } - private static void ZSTD_copyCDictTableIntoCCtx(uint* dst, uint* src, nuint tableSize, ZSTD_compressionParameters* cParams) + private static void ZSTD_copyCDictTableIntoCCtx( + uint* dst, + uint* src, + nuint tableSize, + ZSTD_compressionParameters* cParams + ) { if (ZSTD_CDictIndicesAreTagged(cParams) != 0) { @@ -2598,7 +3414,13 @@ namespace ZstdSharp.Unsafe } } - 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) + 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); @@ -2609,7 +3431,14 @@ namespace ZstdSharp.Unsafe @params.cParams.windowLog = windowLog; @params.useRowMatchFinder = cdict->useRowMatchFinder; { - nuint err_code = ZSTD_resetCCtx_internal(cctx, &@params, pledgedSrcSize, 0, ZSTD_compResetPolicy_e.ZSTDcrp_leaveDirty, zbuff); + nuint err_code = ZSTD_resetCCtx_internal( + cctx, + &@params, + pledgedSrcSize, + 0, + ZSTD_compResetPolicy_e.ZSTDcrp_leaveDirty, + zbuff + ); if (ERR_isError(err_code)) { return err_code; @@ -2625,18 +3454,42 @@ namespace ZstdSharp.Unsafe assert(@params.useRowMatchFinder != ZSTD_paramSwitch_e.ZSTD_ps_auto); { /* DDS guaranteed disabled */ - nuint chainSize = ZSTD_allocateChainTable(cdict_cParams->strategy, cdict->useRowMatchFinder, 0) != 0 ? (nuint)1 << (int)cdict_cParams->chainLog : 0; + 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; - ZSTD_copyCDictTableIntoCCtx(cctx->blockState.matchState.hashTable, cdict->matchState.hashTable, hSize, cdict_cParams); - if (ZSTD_allocateChainTable(cctx->appliedParams.cParams.strategy, cctx->appliedParams.useRowMatchFinder, 0) != 0) + ZSTD_copyCDictTableIntoCCtx( + cctx->blockState.matchState.hashTable, + cdict->matchState.hashTable, + hSize, + cdict_cParams + ); + if ( + ZSTD_allocateChainTable( + cctx->appliedParams.cParams.strategy, + cctx->appliedParams.useRowMatchFinder, + 0 + ) != 0 + ) { - ZSTD_copyCDictTableIntoCCtx(cctx->blockState.matchState.chainTable, cdict->matchState.chainTable, chainSize, cdict_cParams); + ZSTD_copyCDictTableIntoCCtx( + cctx->blockState.matchState.chainTable, + cdict->matchState.chainTable, + chainSize, + cdict_cParams + ); } if (ZSTD_rowMatchFinderUsed(cdict_cParams->strategy, cdict->useRowMatchFinder) != 0) { nuint tagTableSize = hSize; - memcpy(cctx->blockState.matchState.tagTable, cdict->matchState.tagTable, (uint)tagTableSize); + memcpy( + cctx->blockState.matchState.tagTable, + cdict->matchState.tagTable, + (uint)tagTableSize + ); cctx->blockState.matchState.hashSalt = cdict->matchState.hashSalt; } } @@ -2660,18 +3513,34 @@ namespace ZstdSharp.Unsafe cctx->dictID = cdict->dictID; cctx->dictContentSize = cdict->dictContentSize; - memcpy(cctx->blockState.prevCBlock, &cdict->cBlockState, (uint)sizeof(ZSTD_compressedBlockState_t)); + memcpy( + cctx->blockState.prevCBlock, + &cdict->cBlockState, + (uint)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) + 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); + return ZSTD_resetCCtx_byAttachingCDict( + cctx, + cdict, + *@params, + pledgedSrcSize, + zbuff + ); } else { @@ -2686,7 +3555,13 @@ namespace ZstdSharp.Unsafe * 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) + 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) { @@ -2699,29 +3574,71 @@ namespace ZstdSharp.Unsafe @params.cParams = srcCCtx->appliedParams.cParams; assert(srcCCtx->appliedParams.useRowMatchFinder != ZSTD_paramSwitch_e.ZSTD_ps_auto); assert(srcCCtx->appliedParams.postBlockSplitter != ZSTD_paramSwitch_e.ZSTD_ps_auto); - assert(srcCCtx->appliedParams.ldmParams.enableLdm != ZSTD_paramSwitch_e.ZSTD_ps_auto); + assert( + srcCCtx->appliedParams.ldmParams.enableLdm != ZSTD_paramSwitch_e.ZSTD_ps_auto + ); @params.useRowMatchFinder = srcCCtx->appliedParams.useRowMatchFinder; @params.postBlockSplitter = srcCCtx->appliedParams.postBlockSplitter; @params.ldmParams = srcCCtx->appliedParams.ldmParams; @params.fParams = fParams; @params.maxBlockSize = srcCCtx->appliedParams.maxBlockSize; - 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_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 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; uint h3log = srcCCtx->blockState.matchState.hashLog3; nuint h3Size = h3log != 0 ? (nuint)1 << (int)h3log : 0; - memcpy(dstCCtx->blockState.matchState.hashTable, srcCCtx->blockState.matchState.hashTable, (uint)(hSize * sizeof(uint))); - memcpy(dstCCtx->blockState.matchState.chainTable, srcCCtx->blockState.matchState.chainTable, (uint)(chainSize * sizeof(uint))); - memcpy(dstCCtx->blockState.matchState.hashTable3, srcCCtx->blockState.matchState.hashTable3, (uint)(h3Size * sizeof(uint))); + memcpy( + dstCCtx->blockState.matchState.hashTable, + srcCCtx->blockState.matchState.hashTable, + (uint)(hSize * sizeof(uint)) + ); + memcpy( + dstCCtx->blockState.matchState.chainTable, + srcCCtx->blockState.matchState.chainTable, + (uint)(chainSize * sizeof(uint)) + ); + memcpy( + dstCCtx->blockState.matchState.hashTable3, + srcCCtx->blockState.matchState.hashTable3, + (uint)(h3Size * sizeof(uint)) + ); } ZSTD_cwksp_mark_tables_clean(&dstCCtx->workspace); @@ -2735,7 +3652,11 @@ namespace ZstdSharp.Unsafe dstCCtx->dictID = srcCCtx->dictID; dstCCtx->dictContentSize = srcCCtx->dictContentSize; - memcpy(dstCCtx->blockState.prevCBlock, srcCCtx->blockState.prevCBlock, (uint)sizeof(ZSTD_compressedBlockState_t)); + memcpy( + dstCCtx->blockState.prevCBlock, + srcCCtx->blockState.prevCBlock, + (uint)sizeof(ZSTD_compressedBlockState_t) + ); return 0; } @@ -2744,14 +3665,18 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_copyCCtx( + ZSTD_CCtx_s* dstCCtx, + ZSTD_CCtx_s* srcCCtx, + ulong pledgedSrcSize + ) { /*content*/ ZSTD_frameParameters fParams = new ZSTD_frameParameters { contentSizeFlag = 1, checksumFlag = 0, - noDictIDFlag = 0 + noDictIDFlag = 0, }; ZSTD_buffered_policy_e zbuff = srcCCtx->bufferedPolicy; if (pledgedSrcSize == 0) @@ -2767,7 +3692,12 @@ namespace ZstdSharp.Unsafe * 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) + private static void ZSTD_reduceTable_internal( + uint* table, + uint size, + uint reducerValue, + int preserveMark + ) { int nbRows = (int)size / 16; int cellNb = 0; @@ -2813,14 +3743,24 @@ namespace ZstdSharp.Unsafe /*! 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) + 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) + 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) @@ -2894,7 +3834,18 @@ namespace ZstdSharp.Unsafe * * 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) + 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; @@ -2914,13 +3865,57 @@ namespace ZstdSharp.Unsafe { uint max = 35; /* can't fail */ - nuint mostFrequent = HIST_countFast_wksp(countWorkspace, &max, llCodeTable, nbSeq, entropyWorkspace, entropyWkspSize); + nuint mostFrequent = HIST_countFast_wksp( + countWorkspace, + &max, + llCodeTable, + nbSeq, + entropyWorkspace, + entropyWkspSize + ); nextEntropy->litlength_repeatMode = prevEntropy->litlength_repeatMode; - stats.LLtype = (uint)ZSTD_selectEncodingType(&nextEntropy->litlength_repeatMode, countWorkspace, max, mostFrequent, nbSeq, 9, prevEntropy->litlengthCTable, 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)); + stats.LLtype = (uint)ZSTD_selectEncodingType( + &nextEntropy->litlength_repeatMode, + countWorkspace, + max, + mostFrequent, + nbSeq, + 9, + prevEntropy->litlengthCTable, + 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(op, (nuint)(oend - op), CTable_LitLength, 9, (SymbolEncodingType_e)stats.LLtype, countWorkspace, max, llCodeTable, nbSeq, LL_defaultNorm, LL_defaultNormLog, 35, prevEntropy->litlengthCTable, sizeof(uint) * 329, entropyWorkspace, entropyWkspSize); + nuint countSize = ZSTD_buildCTable( + op, + (nuint)(oend - op), + CTable_LitLength, + 9, + (SymbolEncodingType_e)stats.LLtype, + countWorkspace, + max, + llCodeTable, + nbSeq, + LL_defaultNorm, + LL_defaultNormLog, + 35, + prevEntropy->litlengthCTable, + sizeof(uint) * 329, + entropyWorkspace, + entropyWkspSize + ); if (ERR_isError(countSize)) { stats.size = countSize; @@ -2936,14 +3931,58 @@ namespace ZstdSharp.Unsafe { uint max = 31; - nuint mostFrequent = HIST_countFast_wksp(countWorkspace, &max, ofCodeTable, nbSeq, entropyWorkspace, entropyWkspSize); + nuint mostFrequent = HIST_countFast_wksp( + countWorkspace, + &max, + ofCodeTable, + nbSeq, + entropyWorkspace, + entropyWkspSize + ); /* We can only use the basic table if max <= DefaultMaxOff, otherwise the offsets are too large */ - ZSTD_DefaultPolicy_e defaultPolicy = max <= 28 ? ZSTD_DefaultPolicy_e.ZSTD_defaultAllowed : ZSTD_DefaultPolicy_e.ZSTD_defaultDisallowed; + 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, prevEntropy->offcodeCTable, OF_defaultNorm, OF_defaultNormLog, defaultPolicy, strategy); - assert(!(stats.Offtype < (uint)SymbolEncodingType_e.set_compressed && nextEntropy->offcode_repeatMode != FSE_repeat.FSE_repeat_none)); + stats.Offtype = (uint)ZSTD_selectEncodingType( + &nextEntropy->offcode_repeatMode, + countWorkspace, + max, + mostFrequent, + nbSeq, + 8, + prevEntropy->offcodeCTable, + 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(op, (nuint)(oend - op), CTable_OffsetBits, 8, (SymbolEncodingType_e)stats.Offtype, countWorkspace, max, ofCodeTable, nbSeq, OF_defaultNorm, OF_defaultNormLog, 28, prevEntropy->offcodeCTable, sizeof(uint) * 193, entropyWorkspace, entropyWkspSize); + nuint countSize = ZSTD_buildCTable( + op, + (nuint)(oend - op), + CTable_OffsetBits, + 8, + (SymbolEncodingType_e)stats.Offtype, + countWorkspace, + max, + ofCodeTable, + nbSeq, + OF_defaultNorm, + OF_defaultNormLog, + 28, + prevEntropy->offcodeCTable, + sizeof(uint) * 193, + entropyWorkspace, + entropyWkspSize + ); if (ERR_isError(countSize)) { stats.size = countSize; @@ -2959,12 +3998,53 @@ namespace ZstdSharp.Unsafe { uint max = 52; - nuint mostFrequent = HIST_countFast_wksp(countWorkspace, &max, mlCodeTable, nbSeq, entropyWorkspace, entropyWkspSize); + nuint mostFrequent = HIST_countFast_wksp( + countWorkspace, + &max, + mlCodeTable, + nbSeq, + entropyWorkspace, + entropyWkspSize + ); nextEntropy->matchlength_repeatMode = prevEntropy->matchlength_repeatMode; - stats.MLtype = (uint)ZSTD_selectEncodingType(&nextEntropy->matchlength_repeatMode, countWorkspace, max, mostFrequent, nbSeq, 9, prevEntropy->matchlengthCTable, 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)); + stats.MLtype = (uint)ZSTD_selectEncodingType( + &nextEntropy->matchlength_repeatMode, + countWorkspace, + max, + mostFrequent, + nbSeq, + 9, + prevEntropy->matchlengthCTable, + 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(op, (nuint)(oend - op), CTable_MatchLength, 9, (SymbolEncodingType_e)stats.MLtype, countWorkspace, max, mlCodeTable, nbSeq, ML_defaultNorm, ML_defaultNormLog, 52, prevEntropy->matchlengthCTable, sizeof(uint) * 363, entropyWorkspace, entropyWkspSize); + nuint countSize = ZSTD_buildCTable( + op, + (nuint)(oend - op), + CTable_MatchLength, + 9, + (SymbolEncodingType_e)stats.MLtype, + countWorkspace, + max, + mlCodeTable, + nbSeq, + ML_defaultNorm, + ML_defaultNormLog, + 52, + prevEntropy->matchlengthCTable, + sizeof(uint) * 363, + entropyWorkspace, + entropyWkspSize + ); if (ERR_isError(countSize)) { stats.size = countSize; @@ -2983,7 +4063,19 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_entropyCompressSeqStore_internal(void* dst, nuint dstCapacity, void* literals, nuint litSize, SeqStore_t* seqStorePtr, ZSTD_entropyCTables_t* prevEntropy, ZSTD_entropyCTables_t* nextEntropy, ZSTD_CCtx_params_s* cctxParams, void* entropyWorkspace, nuint entropyWkspSize, int bmi2) + private static nuint ZSTD_entropyCompressSeqStore_internal( + void* dst, + nuint dstCapacity, + void* literals, + nuint litSize, + SeqStore_t* seqStorePtr, + ZSTD_entropyCTables_t* prevEntropy, + ZSTD_entropyCTables_t* nextEntropy, + ZSTD_CCtx_params_s* cctxParams, + void* entropyWorkspace, + nuint entropyWkspSize, + int bmi2 + ) { ZSTD_strategy strategy = cctxParams->cParams.strategy; uint* count = (uint*)entropyWorkspace; @@ -3006,8 +4098,22 @@ namespace ZstdSharp.Unsafe { nuint numSequences = (nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart); /* Base suspicion of uncompressibility on ratio of literals to sequences */ - int suspectUncompressible = numSequences == 0 || litSize / numSequences >= 20 ? 1 : 0; - nuint cSize = ZSTD_compressLiterals(op, dstCapacity, literals, litSize, entropyWorkspace, entropyWkspSize, &prevEntropy->huf, &nextEntropy->huf, cctxParams->cParams.strategy, ZSTD_literalsCompressionIsDisabled(cctxParams), suspectUncompressible, bmi2); + int suspectUncompressible = + numSequences == 0 || litSize / numSequences >= 20 ? 1 : 0; + nuint cSize = ZSTD_compressLiterals( + op, + dstCapacity, + literals, + litSize, + entropyWorkspace, + entropyWkspSize, + &prevEntropy->huf, + &nextEntropy->huf, + cctxParams->cParams.strategy, + ZSTD_literalsCompressionIsDisabled(cctxParams), + suspectUncompressible, + bmi2 + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -3052,7 +4158,18 @@ namespace ZstdSharp.Unsafe { byte* seqHead = op++; /* build stats for sequences */ - ZSTD_symbolEncodingTypeStats_t stats = ZSTD_buildSequencesStatistics(seqStorePtr, nbSeq, &prevEntropy->fse, &nextEntropy->fse, op, oend, strategy, count, entropyWorkspace, entropyWkspSize); + ZSTD_symbolEncodingTypeStats_t 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)) @@ -3068,7 +4185,20 @@ namespace ZstdSharp.Unsafe } { - nuint bitstreamSize = ZSTD_encodeSequences(op, (nuint)(oend - op), CTable_MatchLength, mlCodeTable, CTable_OffsetBits, ofCodeTable, CTable_LitLength, llCodeTable, sequences, nbSeq, longOffsets, bmi2); + nuint bitstreamSize = ZSTD_encodeSequences( + 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)) @@ -3089,12 +4219,40 @@ namespace ZstdSharp.Unsafe return (nuint)(op - ostart); } - private static nuint ZSTD_entropyCompressSeqStore_wExtLitBuffer(void* dst, nuint dstCapacity, void* literals, nuint litSize, nuint blockSize, SeqStore_t* seqStorePtr, ZSTD_entropyCTables_t* prevEntropy, ZSTD_entropyCTables_t* nextEntropy, ZSTD_CCtx_params_s* cctxParams, void* entropyWorkspace, nuint entropyWkspSize, int bmi2) + private static nuint ZSTD_entropyCompressSeqStore_wExtLitBuffer( + void* dst, + nuint dstCapacity, + void* literals, + nuint litSize, + nuint blockSize, + SeqStore_t* seqStorePtr, + ZSTD_entropyCTables_t* prevEntropy, + ZSTD_entropyCTables_t* nextEntropy, + ZSTD_CCtx_params_s* cctxParams, + void* entropyWorkspace, + nuint entropyWkspSize, + int bmi2 + ) { - nuint cSize = ZSTD_entropyCompressSeqStore_internal(dst, dstCapacity, literals, litSize, seqStorePtr, prevEntropy, nextEntropy, cctxParams, entropyWorkspace, entropyWkspSize, bmi2); + nuint cSize = ZSTD_entropyCompressSeqStore_internal( + dst, + dstCapacity, + literals, + litSize, + seqStorePtr, + prevEntropy, + nextEntropy, + cctxParams, + entropyWorkspace, + entropyWkspSize, + bmi2 + ); if (cSize == 0) return 0; - if (cSize == unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)) && blockSize <= dstCapacity) + if ( + cSize == unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)) + && blockSize <= dstCapacity + ) { return 0; } @@ -3117,104 +4275,137 @@ namespace ZstdSharp.Unsafe return cSize; } - 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) + 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 + ) { - return ZSTD_entropyCompressSeqStore_wExtLitBuffer(dst, dstCapacity, seqStorePtr->litStart, (nuint)(seqStorePtr->lit - seqStorePtr->litStart), srcSize, seqStorePtr, prevEntropy, nextEntropy, cctxParams, entropyWorkspace, entropyWkspSize, bmi2); + return ZSTD_entropyCompressSeqStore_wExtLitBuffer( + dst, + dstCapacity, + seqStorePtr->litStart, + (nuint)(seqStorePtr->lit - seqStorePtr->litStart), + srcSize, + seqStorePtr, + prevEntropy, + nextEntropy, + cctxParams, + entropyWorkspace, + entropyWkspSize, + bmi2 + ); } - private static readonly ZSTD_BlockCompressor_f?[][] blockCompressor = new ZSTD_BlockCompressor_f?[4][] - { - new ZSTD_BlockCompressor_f[10] + private static readonly ZSTD_BlockCompressor_f?[][] blockCompressor = + new ZSTD_BlockCompressor_f?[4][] { - ZSTD_compressBlock_fast, - ZSTD_compressBlock_fast, - ZSTD_compressBlock_doubleFast, - ZSTD_compressBlock_greedy, - ZSTD_compressBlock_lazy, - ZSTD_compressBlock_lazy2, - ZSTD_compressBlock_btlazy2, - ZSTD_compressBlock_btopt, - ZSTD_compressBlock_btultra, - ZSTD_compressBlock_btultra2 - }, - new ZSTD_BlockCompressor_f[10] + new ZSTD_BlockCompressor_f[10] + { + ZSTD_compressBlock_fast, + ZSTD_compressBlock_fast, + ZSTD_compressBlock_doubleFast, + ZSTD_compressBlock_greedy, + ZSTD_compressBlock_lazy, + ZSTD_compressBlock_lazy2, + ZSTD_compressBlock_btlazy2, + ZSTD_compressBlock_btopt, + ZSTD_compressBlock_btultra, + ZSTD_compressBlock_btultra2, + }, + new ZSTD_BlockCompressor_f[10] + { + ZSTD_compressBlock_fast_extDict, + ZSTD_compressBlock_fast_extDict, + ZSTD_compressBlock_doubleFast_extDict, + ZSTD_compressBlock_greedy_extDict, + ZSTD_compressBlock_lazy_extDict, + ZSTD_compressBlock_lazy2_extDict, + ZSTD_compressBlock_btlazy2_extDict, + ZSTD_compressBlock_btopt_extDict, + ZSTD_compressBlock_btultra_extDict, + ZSTD_compressBlock_btultra_extDict, + }, + new ZSTD_BlockCompressor_f[10] + { + ZSTD_compressBlock_fast_dictMatchState, + ZSTD_compressBlock_fast_dictMatchState, + ZSTD_compressBlock_doubleFast_dictMatchState, + ZSTD_compressBlock_greedy_dictMatchState, + ZSTD_compressBlock_lazy_dictMatchState, + ZSTD_compressBlock_lazy2_dictMatchState, + ZSTD_compressBlock_btlazy2_dictMatchState, + ZSTD_compressBlock_btopt_dictMatchState, + ZSTD_compressBlock_btultra_dictMatchState, + ZSTD_compressBlock_btultra_dictMatchState, + }, + new ZSTD_BlockCompressor_f?[10] + { + null, + null, + null, + ZSTD_compressBlock_greedy_dedicatedDictSearch, + ZSTD_compressBlock_lazy_dedicatedDictSearch, + ZSTD_compressBlock_lazy2_dedicatedDictSearch, + null, + null, + null, + null, + }, + }; + private static readonly ZSTD_BlockCompressor_f[][] rowBasedBlockCompressors = + new ZSTD_BlockCompressor_f[4][] { - ZSTD_compressBlock_fast_extDict, - ZSTD_compressBlock_fast_extDict, - ZSTD_compressBlock_doubleFast_extDict, - ZSTD_compressBlock_greedy_extDict, - ZSTD_compressBlock_lazy_extDict, - ZSTD_compressBlock_lazy2_extDict, - ZSTD_compressBlock_btlazy2_extDict, - ZSTD_compressBlock_btopt_extDict, - ZSTD_compressBlock_btultra_extDict, - ZSTD_compressBlock_btultra_extDict - }, - new ZSTD_BlockCompressor_f[10] - { - ZSTD_compressBlock_fast_dictMatchState, - ZSTD_compressBlock_fast_dictMatchState, - ZSTD_compressBlock_doubleFast_dictMatchState, - ZSTD_compressBlock_greedy_dictMatchState, - ZSTD_compressBlock_lazy_dictMatchState, - ZSTD_compressBlock_lazy2_dictMatchState, - ZSTD_compressBlock_btlazy2_dictMatchState, - ZSTD_compressBlock_btopt_dictMatchState, - ZSTD_compressBlock_btultra_dictMatchState, - ZSTD_compressBlock_btultra_dictMatchState - }, - new ZSTD_BlockCompressor_f?[10] - { - null, - null, - null, - ZSTD_compressBlock_greedy_dedicatedDictSearch, - ZSTD_compressBlock_lazy_dedicatedDictSearch, - ZSTD_compressBlock_lazy2_dedicatedDictSearch, - null, - null, - null, - null - } - }; - private static readonly ZSTD_BlockCompressor_f[][] rowBasedBlockCompressors = new ZSTD_BlockCompressor_f[4][] - { - new ZSTD_BlockCompressor_f[3] - { - ZSTD_compressBlock_greedy_row, - ZSTD_compressBlock_lazy_row, - ZSTD_compressBlock_lazy2_row - }, - new ZSTD_BlockCompressor_f[3] - { - ZSTD_compressBlock_greedy_extDict_row, - ZSTD_compressBlock_lazy_extDict_row, - ZSTD_compressBlock_lazy2_extDict_row - }, - new ZSTD_BlockCompressor_f[3] - { - ZSTD_compressBlock_greedy_dictMatchState_row, - ZSTD_compressBlock_lazy_dictMatchState_row, - ZSTD_compressBlock_lazy2_dictMatchState_row - }, - new ZSTD_BlockCompressor_f[3] - { - ZSTD_compressBlock_greedy_dedicatedDictSearch_row, - ZSTD_compressBlock_lazy_dedicatedDictSearch_row, - ZSTD_compressBlock_lazy2_dedicatedDictSearch_row - } - }; + new ZSTD_BlockCompressor_f[3] + { + ZSTD_compressBlock_greedy_row, + ZSTD_compressBlock_lazy_row, + ZSTD_compressBlock_lazy2_row, + }, + new ZSTD_BlockCompressor_f[3] + { + ZSTD_compressBlock_greedy_extDict_row, + ZSTD_compressBlock_lazy_extDict_row, + ZSTD_compressBlock_lazy2_extDict_row, + }, + new ZSTD_BlockCompressor_f[3] + { + ZSTD_compressBlock_greedy_dictMatchState_row, + ZSTD_compressBlock_lazy_dictMatchState_row, + ZSTD_compressBlock_lazy2_dictMatchState_row, + }, + new ZSTD_BlockCompressor_f[3] + { + ZSTD_compressBlock_greedy_dedicatedDictSearch_row, + ZSTD_compressBlock_lazy_dedicatedDictSearch_row, + ZSTD_compressBlock_lazy2_dedicatedDictSearch_row, + }, + }; + /* ZSTD_selectBlockCompressor() : * Not static, but internal use only (used by long distance matcher) * assumption : strat is a valid strategy */ - private static ZSTD_BlockCompressor_f ZSTD_selectBlockCompressor(ZSTD_strategy strat, ZSTD_paramSwitch_e useRowMatchFinder, ZSTD_dictMode_e dictMode) + private static ZSTD_BlockCompressor_f ZSTD_selectBlockCompressor( + ZSTD_strategy strat, + ZSTD_paramSwitch_e useRowMatchFinder, + ZSTD_dictMode_e dictMode + ) { ZSTD_BlockCompressor_f? selectedCompressor; assert(ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_strategy, (int)strat) != 0); if (ZSTD_rowMatchFinderUsed(strat, useRowMatchFinder) != 0) { assert(useRowMatchFinder != ZSTD_paramSwitch_e.ZSTD_ps_auto); - selectedCompressor = rowBasedBlockCompressors[(int)dictMode][(int)strat - (int)ZSTD_strategy.ZSTD_greedy]; + selectedCompressor = rowBasedBlockCompressors[(int)dictMode][ + (int)strat - (int)ZSTD_strategy.ZSTD_greedy + ]; } else { @@ -3225,7 +4416,11 @@ namespace ZstdSharp.Unsafe return selectedCompressor.NotNull(); } - private static void ZSTD_storeLastLiterals(SeqStore_t* seqStorePtr, byte* anchor, nuint lastLLSize) + private static void ZSTD_storeLastLiterals( + SeqStore_t* seqStorePtr, + byte* anchor, + nuint lastLLSize + ) { memcpy(seqStorePtr->lit, anchor, (uint)lastLLSize); seqStorePtr->lit += lastLLSize; @@ -3244,7 +4439,12 @@ namespace ZstdSharp.Unsafe * - Appends a block delimiter to outSeqs if one is not already present. * See zstd.h for context regarding block delimiters. * Returns the number of sequences after post-processing, or an error code. */ - private static nuint ZSTD_postProcessSequenceProducerResult(ZSTD_Sequence* outSeqs, nuint nbExternalSeqs, nuint outSeqsCapacity, nuint srcSize) + private static nuint ZSTD_postProcessSequenceProducerResult( + ZSTD_Sequence* outSeqs, + nuint nbExternalSeqs, + nuint outSeqsCapacity, + nuint srcSize + ) { if (nbExternalSeqs > outSeqsCapacity) { @@ -3271,7 +4471,9 @@ namespace ZstdSharp.Unsafe if (nbExternalSeqs == outSeqsCapacity) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_sequenceProducer_failed)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_sequenceProducer_failed) + ); } outSeqs[nbExternalSeqs] = new ZSTD_Sequence(); @@ -3287,7 +4489,9 @@ namespace ZstdSharp.Unsafe * This function can be deleted and replaced by determine_blockSize after we resolve issue #3456. */ private static nuint ZSTD_fastSequenceLengthSum(ZSTD_Sequence* seqBuf, nuint seqBufSize) { - nuint matchLenSum, litLenSum, i; + nuint matchLenSum, + litLenSum, + i; matchLenSum = 0; litLenSum = 0; for (i = 0; i < seqBufSize; i++) @@ -3302,9 +4506,10 @@ namespace ZstdSharp.Unsafe /** * Function to validate sequences produced by a block compressor. */ - private static void ZSTD_validateSeqStore(SeqStore_t* seqStore, ZSTD_compressionParameters* cParams) - { - } + private static void ZSTD_validateSeqStore( + SeqStore_t* seqStore, + ZSTD_compressionParameters* cParams + ) { } private static nuint ZSTD_buildSeqStore(ZSTD_CCtx_s* zc, void* src, nuint srcSize) { @@ -3319,7 +4524,11 @@ namespace ZstdSharp.Unsafe } else { - ZSTD_ldm_skipSequences(&zc->externSeqStore, srcSize, zc->appliedParams.cParams.minMatch); + ZSTD_ldm_skipSequences( + &zc->externSeqStore, + srcSize, + zc->appliedParams.cParams.minMatch + ); } return (nuint)ZSTD_BuildSeqStore_e.ZSTDbss_noCompress; @@ -3338,7 +4547,13 @@ namespace ZstdSharp.Unsafe assert(istart - @base < unchecked((nint)(uint)-1)); #endif if (curr > ms->nextToUpdate + 384) - ms->nextToUpdate = curr - (192 < curr - ms->nextToUpdate - 384 ? 192 : curr - ms->nextToUpdate - 384); + ms->nextToUpdate = + curr + - ( + 192 < curr - ms->nextToUpdate - 384 + ? 192 + : curr - ms->nextToUpdate - 384 + ); } { @@ -3352,13 +4567,27 @@ namespace ZstdSharp.Unsafe if (zc->externSeqStore.pos < zc->externSeqStore.size) { - assert(zc->appliedParams.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_disable); + assert( + zc->appliedParams.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_disable + ); if (ZSTD_hasExtSeqProd(&zc->appliedParams) != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_combination_unsupported)); + return unchecked( + (nuint)( + -(int)ZSTD_ErrorCode.ZSTD_error_parameter_combination_unsupported + ) + ); } - lastLLSize = ZSTD_ldm_blockCompress(&zc->externSeqStore, ms, &zc->seqStore, zc->blockState.nextCBlock->rep, zc->appliedParams.useRowMatchFinder, src, srcSize); + 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 == ZSTD_paramSwitch_e.ZSTD_ps_enable) @@ -3366,21 +4595,39 @@ namespace ZstdSharp.Unsafe RawSeqStore_t ldmSeqStore = kNullRawSeqStore; if (ZSTD_hasExtSeqProd(&zc->appliedParams) != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_combination_unsupported)); + return unchecked( + (nuint)( + -(int)ZSTD_ErrorCode.ZSTD_error_parameter_combination_unsupported + ) + ); } ldmSeqStore.seq = zc->ldmSequences; ldmSeqStore.capacity = zc->maxNbLdmSequences; { /* Updates ldmSeqStore.size */ - nuint err_code = ZSTD_ldm_generateSequences(&zc->ldmState, &ldmSeqStore, &zc->appliedParams.ldmParams, src, srcSize); + nuint err_code = ZSTD_ldm_generateSequences( + &zc->ldmState, + &ldmSeqStore, + &zc->appliedParams.ldmParams, + src, + srcSize + ); if (ERR_isError(err_code)) { return err_code; } } - lastLLSize = ZSTD_ldm_blockCompress(&ldmSeqStore, ms, &zc->seqStore, zc->blockState.nextCBlock->rep, zc->appliedParams.useRowMatchFinder, src, srcSize); + lastLLSize = ZSTD_ldm_blockCompress( + &ldmSeqStore, + ms, + &zc->seqStore, + zc->blockState.nextCBlock->rep, + zc->appliedParams.useRowMatchFinder, + src, + srcSize + ); assert(ldmSeqStore.pos == ldmSeqStore.size); } else if (ZSTD_hasExtSeqProd(&zc->appliedParams) != 0) @@ -3389,24 +4636,67 @@ namespace ZstdSharp.Unsafe assert(zc->appliedParams.extSeqProdFunc != null); { uint windowSize = (uint)1 << (int)zc->appliedParams.cParams.windowLog; - nuint nbExternalSeqs = ((delegate* managed)zc->appliedParams.extSeqProdFunc)(zc->appliedParams.extSeqProdState, zc->extSeqBuf, zc->extSeqBufCapacity, src, srcSize, null, 0, zc->appliedParams.compressionLevel, windowSize); - nuint nbPostProcessedSeqs = ZSTD_postProcessSequenceProducerResult(zc->extSeqBuf, nbExternalSeqs, zc->extSeqBufCapacity, srcSize); + nuint nbExternalSeqs = ( + (delegate* managed< + void*, + ZSTD_Sequence*, + nuint, + void*, + nuint, + void*, + nuint, + int, + nuint, + nuint>) + zc->appliedParams.extSeqProdFunc + )( + zc->appliedParams.extSeqProdState, + zc->extSeqBuf, + zc->extSeqBufCapacity, + src, + srcSize, + null, + 0, + zc->appliedParams.compressionLevel, + windowSize + ); + nuint nbPostProcessedSeqs = ZSTD_postProcessSequenceProducerResult( + zc->extSeqBuf, + nbExternalSeqs, + zc->extSeqBufCapacity, + srcSize + ); if (!ERR_isError(nbPostProcessedSeqs)) { ZSTD_SequencePosition seqPos = new ZSTD_SequencePosition { idx = 0, posInSequence = 0, - posInSrc = 0 + posInSrc = 0, }; - nuint seqLenSum = ZSTD_fastSequenceLengthSum(zc->extSeqBuf, nbPostProcessedSeqs); + nuint seqLenSum = ZSTD_fastSequenceLengthSum( + zc->extSeqBuf, + nbPostProcessedSeqs + ); if (seqLenSum > srcSize) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)( + -(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid + ) + ); } { - nuint err_code = ZSTD_transferSequences_wBlockDelim(zc, &seqPos, zc->extSeqBuf, nbPostProcessedSeqs, src, srcSize, zc->appliedParams.searchForExternalRepcodes); + nuint err_code = ZSTD_transferSequences_wBlockDelim( + zc, + &seqPos, + zc->extSeqBuf, + nbPostProcessedSeqs, + src, + srcSize, + zc->appliedParams.searchForExternalRepcodes + ); if (ERR_isError(err_code)) { return err_code; @@ -3423,17 +4713,37 @@ namespace ZstdSharp.Unsafe } { - ZSTD_BlockCompressor_f blockCompressor = ZSTD_selectBlockCompressor(zc->appliedParams.cParams.strategy, zc->appliedParams.useRowMatchFinder, dictMode); + ZSTD_BlockCompressor_f blockCompressor = ZSTD_selectBlockCompressor( + zc->appliedParams.cParams.strategy, + zc->appliedParams.useRowMatchFinder, + dictMode + ); ms->ldmSeqStore = null; - lastLLSize = blockCompressor(ms, &zc->seqStore, zc->blockState.nextCBlock->rep, src, srcSize); + lastLLSize = blockCompressor( + ms, + &zc->seqStore, + zc->blockState.nextCBlock->rep, + src, + srcSize + ); } } } else { - ZSTD_BlockCompressor_f blockCompressor = ZSTD_selectBlockCompressor(zc->appliedParams.cParams.strategy, zc->appliedParams.useRowMatchFinder, dictMode); + ZSTD_BlockCompressor_f blockCompressor = ZSTD_selectBlockCompressor( + zc->appliedParams.cParams.strategy, + zc->appliedParams.useRowMatchFinder, + dictMode + ); ms->ldmSeqStore = null; - lastLLSize = blockCompressor(ms, &zc->seqStore, zc->blockState.nextCBlock->rep, src, srcSize); + lastLLSize = blockCompressor( + ms, + &zc->seqStore, + zc->blockState.nextCBlock->rep, + src, + srcSize + ); } { @@ -3446,12 +4756,19 @@ namespace ZstdSharp.Unsafe return (nuint)ZSTD_BuildSeqStore_e.ZSTDbss_compress; } - private static nuint ZSTD_copyBlockSequences(SeqCollector* seqCollector, SeqStore_t* seqStore, uint* prevRepcodes) + private static nuint ZSTD_copyBlockSequences( + SeqCollector* seqCollector, + SeqStore_t* seqStore, + uint* prevRepcodes + ) { SeqDef_s* inSeqs = seqStore->sequencesStart; nuint nbInSequences = (nuint)(seqStore->sequences - inSeqs); nuint nbInLiterals = (nuint)(seqStore->lit - seqStore->litStart); - ZSTD_Sequence* outSeqs = seqCollector->seqIndex == 0 ? seqCollector->seqStart : seqCollector->seqStart + seqCollector->seqIndex; + ZSTD_Sequence* outSeqs = + seqCollector->seqIndex == 0 + ? seqCollector->seqStart + : seqCollector->seqStart + seqCollector->seqIndex; nuint nbOutSequences = nbInSequences + 1; nuint nbOutLiterals = 0; repcodes_s repcodes; @@ -3571,7 +4888,13 @@ namespace ZstdSharp.Unsafe * ZSTD_sequenceBound(srcSize), or an error code that can be checked * with ZSTD_isError(). */ - public static nuint ZSTD_generateSequences(ZSTD_CCtx_s* zc, ZSTD_Sequence* outSeqs, nuint outSeqsSize, void* src, nuint srcSize) + public static nuint ZSTD_generateSequences( + ZSTD_CCtx_s* zc, + ZSTD_Sequence* outSeqs, + nuint outSeqsSize, + void* src, + nuint srcSize + ) { nuint dstCapacity = ZSTD_compressBound(srcSize); /* Make C90 happy. */ @@ -3580,7 +4903,11 @@ namespace ZstdSharp.Unsafe { int targetCBlockSize; { - nuint err_code = ZSTD_CCtx_getParameter(zc, ZSTD_cParameter.ZSTD_c_targetCBlockSize, &targetCBlockSize); + nuint err_code = ZSTD_CCtx_getParameter( + zc, + ZSTD_cParameter.ZSTD_c_targetCBlockSize, + &targetCBlockSize + ); if (ERR_isError(err_code)) { return err_code; @@ -3589,14 +4916,20 @@ namespace ZstdSharp.Unsafe if (targetCBlockSize != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); } } { int nbWorkers; { - nuint err_code = ZSTD_CCtx_getParameter(zc, ZSTD_cParameter.ZSTD_c_nbWorkers, &nbWorkers); + nuint err_code = ZSTD_CCtx_getParameter( + zc, + ZSTD_cParameter.ZSTD_c_nbWorkers, + &nbWorkers + ); if (ERR_isError(err_code)) { return err_code; @@ -3605,7 +4938,9 @@ namespace ZstdSharp.Unsafe if (nbWorkers != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); } } @@ -3723,7 +5058,10 @@ namespace ZstdSharp.Unsafe /* 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); + 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); } @@ -3734,7 +5072,17 @@ namespace ZstdSharp.Unsafe * Requires ENTROPY_WORKSPACE_SIZE workspace * @return : size of huffman description table, or an 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 literalsCompressionIsDisabled, void* workspace, nuint wkspSize, int hufFlags) + private static nuint ZSTD_buildBlockEntropyStats_literals( + void* src, + nuint srcSize, + ZSTD_hufCTables_t* prevHuf, + ZSTD_hufCTables_t* nextHuf, + ZSTD_hufCTablesMetadata_t* hufMetadata, + int literalsCompressionIsDisabled, + void* workspace, + nuint wkspSize, + int hufFlags + ) { byte* wkspStart = (byte*)workspace; byte* wkspEnd = wkspStart + wkspSize; @@ -3754,7 +5102,9 @@ namespace ZstdSharp.Unsafe } { - nuint minLitSize = (nuint)(prevHuf->repeatMode == HUF_repeat.HUF_repeat_valid ? 6 : 63); + nuint minLitSize = (nuint)( + prevHuf->repeatMode == HUF_repeat.HUF_repeat_valid ? 6 : 63 + ); if (srcSize <= minLitSize) { hufMetadata->hType = SymbolEncodingType_e.set_basic; @@ -3763,7 +5113,14 @@ namespace ZstdSharp.Unsafe } { - nuint largest = HIST_count_wksp(countWksp, &maxSymbolValue, (byte*)src, srcSize, workspace, wkspSize); + nuint largest = HIST_count_wksp( + countWksp, + &maxSymbolValue, + (byte*)src, + srcSize, + workspace, + wkspSize + ); { nuint err_code = largest; if (ERR_isError(err_code)) @@ -3785,16 +5142,35 @@ namespace ZstdSharp.Unsafe } } - if (repeat == HUF_repeat.HUF_repeat_check && HUF_validateCTable(&prevHuf->CTable.e0, countWksp, maxSymbolValue) == 0) + if ( + repeat == HUF_repeat.HUF_repeat_check + && HUF_validateCTable(&prevHuf->CTable.e0, countWksp, maxSymbolValue) == 0 + ) { repeat = HUF_repeat.HUF_repeat_none; } memset(&nextHuf->CTable.e0, 0, sizeof(ulong) * 257); - huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue, nodeWksp, nodeWkspSize, &nextHuf->CTable.e0, countWksp, hufFlags); + huffLog = HUF_optimalTableLog( + huffLog, + srcSize, + maxSymbolValue, + nodeWksp, + nodeWkspSize, + &nextHuf->CTable.e0, + countWksp, + hufFlags + ); assert(huffLog <= 11); { - nuint maxBits = HUF_buildCTable_wksp(&nextHuf->CTable.e0, countWksp, maxSymbolValue, huffLog, nodeWksp, nodeWkspSize); + nuint maxBits = HUF_buildCTable_wksp( + &nextHuf->CTable.e0, + countWksp, + maxSymbolValue, + huffLog, + nodeWksp, + nodeWkspSize + ); { nuint err_code = maxBits; if (ERR_isError(err_code)) @@ -3807,12 +5183,31 @@ namespace ZstdSharp.Unsafe } { - nuint newCSize = HUF_estimateCompressedSize(&nextHuf->CTable.e0, countWksp, maxSymbolValue); - nuint hSize = HUF_writeCTable_wksp(hufMetadata->hufDesBuffer, sizeof(byte) * 128, &nextHuf->CTable.e0, maxSymbolValue, huffLog, nodeWksp, nodeWkspSize); + nuint newCSize = HUF_estimateCompressedSize( + &nextHuf->CTable.e0, + countWksp, + maxSymbolValue + ); + nuint hSize = HUF_writeCTable_wksp( + hufMetadata->hufDesBuffer, + sizeof(byte) * 128, + &nextHuf->CTable.e0, + maxSymbolValue, + huffLog, + nodeWksp, + nodeWkspSize + ); if (repeat != HUF_repeat.HUF_repeat_none) { - nuint oldCSize = HUF_estimateCompressedSize(&prevHuf->CTable.e0, countWksp, maxSymbolValue); - if (oldCSize < srcSize && (oldCSize <= hSize + newCSize || hSize + 12 >= srcSize)) + nuint oldCSize = HUF_estimateCompressedSize( + &prevHuf->CTable.e0, + countWksp, + maxSymbolValue + ); + if ( + oldCSize < srcSize + && (oldCSize <= hSize + newCSize || hSize + 12 >= srcSize) + ) { memcpy(nextHuf, prevHuf, (uint)sizeof(ZSTD_hufCTables_t)); hufMetadata->hType = SymbolEncodingType_e.set_repeat; @@ -3837,7 +5232,9 @@ namespace ZstdSharp.Unsafe * 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) + private static ZSTD_symbolEncodingTypeStats_t ZSTD_buildDummySequencesStatistics( + ZSTD_fseCTables_t* nextEntropy + ) { ZSTD_symbolEncodingTypeStats_t stats = new ZSTD_symbolEncodingTypeStats_t { @@ -3846,7 +5243,7 @@ namespace ZstdSharp.Unsafe MLtype = (uint)SymbolEncodingType_e.set_basic, size = 0, lastCountSize = 0, - longOffsets = 0 + longOffsets = 0, }; nextEntropy->litlength_repeatMode = FSE_repeat.FSE_repeat_none; nextEntropy->offcode_repeatMode = FSE_repeat.FSE_repeat_none; @@ -3859,7 +5256,15 @@ namespace ZstdSharp.Unsafe * 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) + 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); @@ -3870,7 +5275,21 @@ namespace ZstdSharp.Unsafe uint* entropyWorkspace = countWorkspace + (52 + 1); nuint entropyWorkspaceSize = wkspSize - (52 + 1) * sizeof(uint); ZSTD_symbolEncodingTypeStats_t stats; - stats = nbSeq != 0 ? ZSTD_buildSequencesStatistics(seqStorePtr, nbSeq, prevEntropy, nextEntropy, op, oend, strategy, countWorkspace, entropyWorkspace, entropyWorkspaceSize) : ZSTD_buildDummySequencesStatistics(nextEntropy); + stats = + nbSeq != 0 + ? ZSTD_buildSequencesStatistics( + seqStorePtr, + nbSeq, + prevEntropy, + nextEntropy, + op, + oend, + strategy, + countWorkspace, + entropyWorkspace, + entropyWorkspaceSize + ) + : ZSTD_buildDummySequencesStatistics(nextEntropy); { nuint err_code = stats.size; if (ERR_isError(err_code)) @@ -3892,12 +5311,31 @@ namespace ZstdSharp.Unsafe * @return : 0 on success, or an error code * Note : also employed in superblock */ - private 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) + private 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); - int huf_useOptDepth = cctxParams->cParams.strategy >= ZSTD_strategy.ZSTD_btultra ? 1 : 0; + int huf_useOptDepth = + cctxParams->cParams.strategy >= ZSTD_strategy.ZSTD_btultra ? 1 : 0; int hufFlags = huf_useOptDepth != 0 ? (int)HUF_flags_e.HUF_flags_optimalDepth : 0; - entropyMetadata->hufMetadata.hufDesSize = ZSTD_buildBlockEntropyStats_literals(seqStorePtr->litStart, litSize, &prevEntropy->huf, &nextEntropy->huf, &entropyMetadata->hufMetadata, ZSTD_literalsCompressionIsDisabled(cctxParams), workspace, wkspSize, hufFlags); + entropyMetadata->hufMetadata.hufDesSize = ZSTD_buildBlockEntropyStats_literals( + seqStorePtr->litStart, + litSize, + &prevEntropy->huf, + &nextEntropy->huf, + &entropyMetadata->hufMetadata, + ZSTD_literalsCompressionIsDisabled(cctxParams), + workspace, + wkspSize, + hufFlags + ); { nuint err_code = entropyMetadata->hufMetadata.hufDesSize; if (ERR_isError(err_code)) @@ -3906,7 +5344,15 @@ namespace ZstdSharp.Unsafe } } - entropyMetadata->fseMetadata.fseTablesSize = ZSTD_buildBlockEntropyStats_sequences(seqStorePtr, &prevEntropy->fse, &nextEntropy->fse, cctxParams, &entropyMetadata->fseMetadata, workspace, wkspSize); + 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)) @@ -3919,23 +5365,47 @@ namespace ZstdSharp.Unsafe } /* 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) + 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 >= 1 * (1 << 10) ? 1 : 0) + (litSize >= 16 * (1 << 10) ? 1 : 0)); + nuint literalSectionHeaderSize = (nuint)( + 3 + (litSize >= 1 * (1 << 10) ? 1 : 0) + (litSize >= 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) + else if ( + hufMetadata->hType == SymbolEncodingType_e.set_compressed + || hufMetadata->hType == SymbolEncodingType_e.set_repeat + ) { - nuint largest = HIST_count_wksp(countWksp, &maxSymbolValue, literals, litSize, workspace, wkspSize); + nuint largest = HIST_count_wksp( + countWksp, + &maxSymbolValue, + literals, + litSize, + workspace, + wkspSize + ); if (ERR_isError(largest)) return litSize; { - nuint cLitSizeEstimate = HUF_estimateCompressedSize(&huf->CTable.e0, countWksp, maxSymbolValue); + nuint cLitSizeEstimate = HUF_estimateCompressedSize( + &huf->CTable.e0, + countWksp, + maxSymbolValue + ); if (writeEntropy != 0) cLitSizeEstimate += hufMetadata->hufDesSize; if (singleStream == 0) @@ -3949,7 +5419,19 @@ namespace ZstdSharp.Unsafe } /* 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, byte* additionalBits, short* defaultNorm, uint defaultNormLog, uint defaultMax, void* workspace, nuint wkspSize) + private static nuint ZSTD_estimateBlockSize_symbolType( + SymbolEncodingType_e type, + byte* codeTable, + nuint nbSeq, + uint maxCode, + uint* fseCTable, + byte* additionalBits, + short* defaultNorm, + uint defaultNormLog, + uint defaultMax, + void* workspace, + nuint wkspSize + ) { uint* countWksp = (uint*)workspace; byte* ctp = codeTable; @@ -3961,13 +5443,21 @@ namespace ZstdSharp.Unsafe if (type == SymbolEncodingType_e.set_basic) { assert(max <= defaultMax); - cSymbolTypeSizeEstimateInBits = ZSTD_crossEntropyCost(defaultNorm, defaultNormLog, countWksp, max); + 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) + else if ( + type == SymbolEncodingType_e.set_compressed + || type == SymbolEncodingType_e.set_repeat + ) { cSymbolTypeSizeEstimateInBits = ZSTD_fseBitCost(fseCTable, countWksp, max); } @@ -3990,24 +5480,103 @@ namespace ZstdSharp.Unsafe } /* 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) + 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 + ) { /* seqHead */ - nuint sequencesSectionHeaderSize = (nuint)(1 + 1 + (nbSeq >= 128 ? 1 : 0) + (nbSeq >= 0x7F00 ? 1 : 0)); + 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, fseTables->offcodeCTable, null, OF_defaultNorm, OF_defaultNormLog, 28, workspace, wkspSize); - cSeqSizeEstimate += ZSTD_estimateBlockSize_symbolType(fseMetadata->llType, llCodeTable, nbSeq, 35, fseTables->litlengthCTable, LL_bits, LL_defaultNorm, LL_defaultNormLog, 35, workspace, wkspSize); - cSeqSizeEstimate += ZSTD_estimateBlockSize_symbolType(fseMetadata->mlType, mlCodeTable, nbSeq, 52, fseTables->matchlengthCTable, ML_bits, ML_defaultNorm, ML_defaultNormLog, 52, workspace, wkspSize); + cSeqSizeEstimate += ZSTD_estimateBlockSize_symbolType( + fseMetadata->ofType, + ofCodeTable, + nbSeq, + 31, + fseTables->offcodeCTable, + null, + OF_defaultNorm, + OF_defaultNormLog, + 28, + workspace, + wkspSize + ); + cSeqSizeEstimate += ZSTD_estimateBlockSize_symbolType( + fseMetadata->llType, + llCodeTable, + nbSeq, + 35, + fseTables->litlengthCTable, + LL_bits, + LL_defaultNorm, + LL_defaultNormLog, + 35, + workspace, + wkspSize + ); + cSeqSizeEstimate += ZSTD_estimateBlockSize_symbolType( + fseMetadata->mlType, + mlCodeTable, + nbSeq, + 52, + fseTables->matchlengthCTable, + ML_bits, + 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) + 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); + 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; } @@ -4015,18 +5584,42 @@ namespace ZstdSharp.Unsafe * * @return: estimated compressed size of the seqStore, or a zstd error. */ - private static nuint ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize(SeqStore_t* seqStore, ZSTD_CCtx_s* zc) + private static nuint ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize( + SeqStore_t* seqStore, + ZSTD_CCtx_s* zc + ) { ZSTD_entropyCTablesMetadata_t* entropyMetadata = &zc->blockSplitCtx.entropyMetadata; { - nuint err_code = ZSTD_buildBlockEntropyStats(seqStore, &zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy, &zc->appliedParams, entropyMetadata, zc->tmpWorkspace, zc->tmpWkspSize); + nuint err_code = ZSTD_buildBlockEntropyStats( + seqStore, + &zc->blockState.prevCBlock->entropy, + &zc->blockState.nextCBlock->entropy, + &zc->appliedParams, + entropyMetadata, + zc->tmpWorkspace, + zc->tmpWkspSize + ); if (ERR_isError(err_code)) { 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, zc->tmpWorkspace, zc->tmpWkspSize, entropyMetadata->hufMetadata.hType == SymbolEncodingType_e.set_compressed ? 1 : 0, 1); + 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, + zc->tmpWorkspace, + zc->tmpWkspSize, + entropyMetadata->hufMetadata.hType == SymbolEncodingType_e.set_compressed ? 1 : 0, + 1 + ); } /* Returns literals bytes represented in a seqStore */ @@ -4039,7 +5632,10 @@ namespace ZstdSharp.Unsafe { SeqDef_s seq = seqStore->sequencesStart[i]; literalsBytes += seq.litLength; - if (i == seqStore->longLengthPos && seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_literalLength) + if ( + i == seqStore->longLengthPos + && seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_literalLength + ) { literalsBytes += 0x10000; } @@ -4058,7 +5654,10 @@ namespace ZstdSharp.Unsafe { SeqDef_s seq = seqStore->sequencesStart[i]; matchBytes += (nuint)(seq.mlBase + 3); - if (i == seqStore->longLengthPos && seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_matchLength) + if ( + i == seqStore->longLengthPos + && seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_matchLength + ) { matchBytes += 0x10000; } @@ -4070,7 +5669,12 @@ namespace ZstdSharp.Unsafe /* 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) + private static void ZSTD_deriveSeqStoreChunk( + SeqStore_t* resultSeqStore, + SeqStore_t* originalSeqStore, + nuint startIdx, + nuint endIdx + ) { *resultSeqStore = *originalSeqStore; if (startIdx > 0) @@ -4081,7 +5685,10 @@ namespace ZstdSharp.Unsafe if (originalSeqStore->longLengthType != ZSTD_longLengthType_e.ZSTD_llt_none) { - if (originalSeqStore->longLengthPos < startIdx || originalSeqStore->longLengthPos > endIdx) + if ( + originalSeqStore->longLengthPos < startIdx + || originalSeqStore->longLengthPos > endIdx + ) { resultSeqStore->longLengthType = ZSTD_longLengthType_e.ZSTD_llt_none; } @@ -4140,10 +5747,18 @@ namespace ZstdSharp.Unsafe * 1-3 : repcode 1-3 * 4+ : real_offset+3 */ - private static void ZSTD_seqStore_resolveOffCodes(repcodes_s* dRepcodes, repcodes_s* cRepcodes, SeqStore_t* seqStore, uint nbSeq) + private static void ZSTD_seqStore_resolveOffCodes( + repcodes_s* dRepcodes, + repcodes_s* cRepcodes, + SeqStore_t* seqStore, + uint nbSeq + ) { uint idx = 0; - uint longLitLenIdx = seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_literalLength ? seqStore->longLengthPos : nbSeq; + uint longLitLenIdx = + seqStore->longLengthType == ZSTD_longLengthType_e.ZSTD_llt_literalLength + ? seqStore->longLengthPos + : nbSeq; for (; idx < nbSeq; ++idx) { SeqDef_s* seq = seqStore->sequencesStart + idx; @@ -4171,7 +5786,18 @@ namespace ZstdSharp.Unsafe * * 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) + 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 + ) { const uint rleMaxLength = 25; byte* op = (byte*)dst; @@ -4181,13 +5807,29 @@ namespace ZstdSharp.Unsafe /* In case of an RLE or raw block, the simulated decompression repcode history must be reset */ repcodes_s dRepOriginal = *dRep; if (isPartition != 0) - ZSTD_seqStore_resolveOffCodes(dRep, cRep, seqStore, (uint)(seqStore->sequences - seqStore->sequencesStart)); + ZSTD_seqStore_resolveOffCodes( + dRep, + cRep, + seqStore, + (uint)(seqStore->sequences - seqStore->sequencesStart) + ); if (dstCapacity < ZSTD_blockHeaderSize) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); } - cSeqsSize = ZSTD_entropyCompressSeqStore(seqStore, &zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy, &zc->appliedParams, op + ZSTD_blockHeaderSize, dstCapacity - ZSTD_blockHeaderSize, srcSize, zc->tmpWorkspace, zc->tmpWkspSize, zc->bmi2); + cSeqsSize = ZSTD_entropyCompressSeqStore( + seqStore, + &zc->blockState.prevCBlock->entropy, + &zc->blockState.nextCBlock->entropy, + &zc->appliedParams, + op + ZSTD_blockHeaderSize, + dstCapacity - ZSTD_blockHeaderSize, + srcSize, + zc->tmpWorkspace, + zc->tmpWkspSize, + zc->bmi2 + ); { nuint err_code = cSeqsSize; if (ERR_isError(err_code)) @@ -4196,7 +5838,11 @@ namespace ZstdSharp.Unsafe } } - if (zc->isFirstBlock == 0 && cSeqsSize < rleMaxLength && ZSTD_isRLE((byte*)src, srcSize) != 0) + if ( + zc->isFirstBlock == 0 + && cSeqsSize < rleMaxLength + && ZSTD_isRLE((byte*)src, srcSize) != 0 + ) { cSeqsSize = 1; } @@ -4204,7 +5850,11 @@ namespace ZstdSharp.Unsafe if (zc->seqCollector.collectSequences != 0) { { - nuint err_code = ZSTD_copyBlockSequences(&zc->seqCollector, seqStore, dRepOriginal.rep); + nuint err_code = ZSTD_copyBlockSequences( + &zc->seqCollector, + seqStore, + dRepOriginal.rep + ); if (ERR_isError(err_code)) { return err_code; @@ -4248,8 +5898,12 @@ namespace ZstdSharp.Unsafe cSize = ZSTD_blockHeaderSize + cSeqsSize; } - 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 ( + 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; } @@ -4267,7 +5921,13 @@ namespace ZstdSharp.Unsafe * At ZSTD_MAX_NB_BLOCK_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) + private static void ZSTD_deriveBlockSplitsHelper( + seqStoreSplits* splits, + nuint startIdx, + nuint endIdx, + ZSTD_CCtx_s* zc, + SeqStore_t* origSeqStore + ) { SeqStore_t* fullSeqStoreChunk = &zc->blockSplitCtx.fullSeqStoreChunk; SeqStore_t* firstHalfSeqStore = &zc->blockSplitCtx.firstHalfSeqStore; @@ -4285,10 +5945,23 @@ namespace ZstdSharp.Unsafe 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) || ERR_isError(estimatedFirstHalfSize) || ERR_isError(estimatedSecondHalfSize)) + estimatedOriginalSize = ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize( + fullSeqStoreChunk, + zc + ); + estimatedFirstHalfSize = ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize( + firstHalfSeqStore, + zc + ); + estimatedSecondHalfSize = ZSTD_buildEntropyStatisticsAndEstimateSubBlockSize( + secondHalfSeqStore, + zc + ); + if ( + ERR_isError(estimatedOriginalSize) + || ERR_isError(estimatedFirstHalfSize) + || ERR_isError(estimatedSecondHalfSize) + ) { return; } @@ -4327,7 +6000,15 @@ namespace ZstdSharp.Unsafe * * 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) + 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; @@ -4360,7 +6041,18 @@ namespace ZstdSharp.Unsafe *nextSeqStore = new SeqStore_t(); if (numSplits == 0) { - nuint cSizeSingleBlock = ZSTD_compressSeqStore_singleBlock(zc, &zc->seqStore, &dRep, &cRep, op, dstCapacity, ip, blockSize, lastBlock, 0); + nuint cSizeSingleBlock = ZSTD_compressSeqStore_singleBlock( + zc, + &zc->seqStore, + &dRep, + &cRep, + op, + dstCapacity, + ip, + blockSize, + lastBlock, + 0 + ); { nuint err_code = cSizeSingleBlock; if (ERR_isError(err_code)) @@ -4380,7 +6072,9 @@ namespace ZstdSharp.Unsafe nuint cSizeChunk; uint lastPartition = i == numSplits ? 1U : 0U; uint lastBlockEntireSrc = 0; - nuint srcBytes = ZSTD_countSeqStoreLiteralsBytes(currSeqStore) + ZSTD_countSeqStoreMatchBytes(currSeqStore); + nuint srcBytes = + ZSTD_countSeqStoreLiteralsBytes(currSeqStore) + + ZSTD_countSeqStoreMatchBytes(currSeqStore); srcBytesTotal += srcBytes; if (lastPartition != 0) { @@ -4389,10 +6083,26 @@ namespace ZstdSharp.Unsafe } else { - ZSTD_deriveSeqStoreChunk(nextSeqStore, &zc->seqStore, partitions[i], partitions[i + 1]); + ZSTD_deriveSeqStoreChunk( + nextSeqStore, + &zc->seqStore, + partitions[i], + partitions[i + 1] + ); } - cSizeChunk = ZSTD_compressSeqStore_singleBlock(zc, currSeqStore, &dRep, &cRep, op, dstCapacity, ip, srcBytes, lastBlockEntireSrc, 1); + cSizeChunk = ZSTD_compressSeqStore_singleBlock( + zc, + currSeqStore, + &dRep, + &cRep, + op, + dstCapacity, + ip, + srcBytes, + lastBlockEntireSrc, + 1 + ); { nuint err_code = cSizeChunk; if (ERR_isError(err_code)) @@ -4413,7 +6123,14 @@ namespace ZstdSharp.Unsafe return cSize; } - private static nuint ZSTD_compressBlock_splitBlock(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint lastBlock) + private static nuint ZSTD_compressBlock_splitBlock( + ZSTD_CCtx_s* zc, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize, + uint lastBlock + ) { uint nbSeq; nuint cSize; @@ -4430,11 +6147,17 @@ namespace ZstdSharp.Unsafe 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; + 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 (zc->seqCollector.collectSequences != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_sequenceProducer_failed)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_sequenceProducer_failed) + ); } cSize = ZSTD_noCompressBlock(dst, dstCapacity, src, srcSize, lastBlock); @@ -4452,7 +6175,15 @@ namespace ZstdSharp.Unsafe nbSeq = (uint)(zc->seqStore.sequences - zc->seqStore.sequencesStart); } - cSize = ZSTD_compressBlock_splitBlock_internal(zc, dst, dstCapacity, src, srcSize, lastBlock, nbSeq); + cSize = ZSTD_compressBlock_splitBlock_internal( + zc, + dst, + dstCapacity, + src, + srcSize, + lastBlock, + nbSeq + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -4464,7 +6195,14 @@ namespace ZstdSharp.Unsafe return cSize; } - private static nuint ZSTD_compressBlock_internal(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint frame) + private static nuint ZSTD_compressBlock_internal( + ZSTD_CCtx_s* zc, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize, + uint frame + ) { /* This is an estimated upper bound for the length of an rle block. * This isn't the actual upper bound. @@ -4488,7 +6226,9 @@ namespace ZstdSharp.Unsafe { if (zc->seqCollector.collectSequences != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_sequenceProducer_failed)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_sequenceProducer_failed) + ); } cSize = 0; @@ -4499,7 +6239,11 @@ namespace ZstdSharp.Unsafe if (zc->seqCollector.collectSequences != 0) { { - nuint err_code = ZSTD_copyBlockSequences(&zc->seqCollector, ZSTD_getSeqStore(zc), zc->blockState.prevCBlock->rep); + nuint err_code = ZSTD_copyBlockSequences( + &zc->seqCollector, + ZSTD_getSeqStore(zc), + zc->blockState.prevCBlock->rep + ); if (ERR_isError(err_code)) { return err_code; @@ -4510,38 +6254,81 @@ namespace ZstdSharp.Unsafe return 0; } - cSize = ZSTD_entropyCompressSeqStore(&zc->seqStore, &zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy, &zc->appliedParams, dst, dstCapacity, srcSize, zc->tmpWorkspace, zc->tmpWkspSize, zc->bmi2); - if (frame != 0 && zc->isFirstBlock == 0 && cSize < rleMaxLength && ZSTD_isRLE(ip, srcSize) != 0) + cSize = ZSTD_entropyCompressSeqStore( + &zc->seqStore, + &zc->blockState.prevCBlock->entropy, + &zc->blockState.nextCBlock->entropy, + &zc->appliedParams, + dst, + dstCapacity, + srcSize, + zc->tmpWorkspace, + zc->tmpWkspSize, + zc->bmi2 + ); + if ( + frame != 0 + && zc->isFirstBlock == 0 + && cSize < rleMaxLength + && ZSTD_isRLE(ip, srcSize) != 0 + ) { cSize = 1; op[0] = ip[0]; } - @out: + @out: if (!ERR_isError(cSize) && 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; + 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) + 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) + 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 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 maxCSize = + srcSize - ZSTD_minGain(srcSize, zc->appliedParams.cParams.strategy); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -4562,7 +6349,14 @@ namespace ZstdSharp.Unsafe 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) + 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); @@ -4574,7 +6368,15 @@ namespace ZstdSharp.Unsafe } } - cSize = ZSTD_compressBlock_targetCBlockSize_body(zc, dst, dstCapacity, src, srcSize, bss, lastBlock); + cSize = ZSTD_compressBlock_targetCBlockSize_body( + zc, + dst, + dstCapacity, + src, + srcSize, + bss, + lastBlock + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -4583,16 +6385,35 @@ namespace ZstdSharp.Unsafe } } - 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 ( + 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) + 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) + 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); @@ -4608,25 +6429,29 @@ namespace ZstdSharp.Unsafe } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_splitLevels => new int[10] - { - 0, - 0, - 1, - 2, - 2, - 3, - 3, - 4, - 4, - 4 - }; - private static int* splitLevels => (int*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_splitLevels)); + private static ReadOnlySpan Span_splitLevels => + new int[10] { 0, 0, 1, 2, 2, 3, 3, 4, 4, 4 }; + private static int* splitLevels => + (int*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_splitLevels) + ); #else - private static readonly int* splitLevels = GetArrayPointer(new int[10] { 0, 0, 1, 2, 2, 3, 3, 4, 4, 4 }); + private static readonly int* splitLevels = GetArrayPointer( + new int[10] { 0, 0, 1, 2, 2, 3, 3, 4, 4, 4 } + ); #endif - private static nuint ZSTD_optimalBlockSize(ZSTD_CCtx_s* cctx, void* src, nuint srcSize, nuint blockSizeMax, int splitLevel, ZSTD_strategy strat, long savings) + + private static nuint ZSTD_optimalBlockSize( + ZSTD_CCtx_s* cctx, + void* src, + nuint srcSize, + nuint blockSizeMax, + int splitLevel, + ZSTD_strategy strat, + long savings + ) { if (srcSize < 128 * (1 << 10) || blockSizeMax < 128 * (1 << 10)) return srcSize < blockSizeMax ? srcSize : blockSizeMax; @@ -4648,7 +6473,13 @@ namespace ZstdSharp.Unsafe splitLevel -= 2; } - return ZSTD_splitBlock(src, blockSizeMax, splitLevel, cctx->tmpWorkspace, cctx->tmpWkspSize); + return ZSTD_splitBlock( + src, + blockSizeMax, + splitLevel, + cctx->tmpWorkspace, + cctx->tmpWkspSize + ); } /*! ZSTD_compress_frameChunk() : @@ -4658,7 +6489,14 @@ namespace ZstdSharp.Unsafe * 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) + private static nuint ZSTD_compress_frameChunk( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize, + uint lastFrameChunk + ) { nuint blockSizeMax = cctx->blockSizeMax; nuint remaining = srcSize; @@ -4673,7 +6511,15 @@ namespace ZstdSharp.Unsafe while (remaining != 0) { ZSTD_MatchState_t* ms = &cctx->blockState.matchState; - nuint blockSize = ZSTD_optimalBlockSize(cctx, ip, remaining, blockSizeMax, cctx->appliedParams.preBlockSplitter_level, cctx->appliedParams.cParams.strategy, savings); + nuint blockSize = ZSTD_optimalBlockSize( + cctx, + ip, + remaining, + blockSizeMax, + cctx->appliedParams.preBlockSplitter_level, + cctx->appliedParams.cParams.strategy, + savings + ); uint lastBlock = lastFrameChunk & (uint)(blockSize == remaining ? 1 : 0); assert(blockSize <= remaining); if (dstCapacity < ZSTD_blockHeaderSize + (nuint)(1 + 1) + 1) @@ -4681,16 +6527,41 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); } - ZSTD_overflowCorrectIfNeeded(ms, &cctx->workspace, &cctx->appliedParams, ip, ip + blockSize); - ZSTD_checkDictValidity(&ms->window, ip + blockSize, maxDist, &ms->loadedDictEnd, &ms->dictMatchState); - ZSTD_window_enforceMaxDist(&ms->window, ip, maxDist, &ms->loadedDictEnd, &ms->dictMatchState); + ZSTD_overflowCorrectIfNeeded( + ms, + &cctx->workspace, + &cctx->appliedParams, + ip, + ip + blockSize + ); + ZSTD_checkDictValidity( + &ms->window, + ip + blockSize, + maxDist, + &ms->loadedDictEnd, + &ms->dictMatchState + ); + ZSTD_window_enforceMaxDist( + &ms->window, + ip, + 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, op, dstCapacity, ip, blockSize, lastBlock); + cSize = ZSTD_compressBlock_targetCBlockSize( + cctx, + op, + dstCapacity, + ip, + blockSize, + lastBlock + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -4704,7 +6575,14 @@ namespace ZstdSharp.Unsafe } else if (ZSTD_blockSplitterEnabled(&cctx->appliedParams) != 0) { - cSize = ZSTD_compressBlock_splitBlock(cctx, op, dstCapacity, ip, blockSize, lastBlock); + cSize = ZSTD_compressBlock_splitBlock( + cctx, + op, + dstCapacity, + ip, + blockSize, + lastBlock + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -4717,7 +6595,14 @@ namespace ZstdSharp.Unsafe } else { - cSize = ZSTD_compressBlock_internal(cctx, op + ZSTD_blockHeaderSize, dstCapacity - ZSTD_blockHeaderSize, ip, blockSize, 1); + cSize = ZSTD_compressBlock_internal( + cctx, + op + ZSTD_blockHeaderSize, + dstCapacity - ZSTD_blockHeaderSize, + ip, + blockSize, + 1 + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -4739,7 +6624,14 @@ namespace ZstdSharp.Unsafe } 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); + 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); cSize += ZSTD_blockHeaderSize; } @@ -4761,21 +6653,40 @@ namespace ZstdSharp.Unsafe return (nuint)(op - ostart); } - private static nuint ZSTD_writeFrameHeader(void* dst, nuint dstCapacity, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize, uint dictID) + private static nuint ZSTD_writeFrameHeader( + void* dst, + nuint dstCapacity, + ZSTD_CCtx_params_s* @params, + ulong pledgedSrcSize, + uint dictID + ) { byte* op = (byte*)dst; /* 0-3 */ - uint dictIDSizeCodeLength = (uint)((dictID > 0 ? 1 : 0) + (dictID >= 256 ? 1 : 0) + (dictID >= 65536 ? 1 : 0)); + uint dictIDSizeCodeLength = (uint)( + (dictID > 0 ? 1 : 0) + (dictID >= 256 ? 1 : 0) + (dictID >= 65536 ? 1 : 0) + ); /* 0-3 */ uint dictIDSizeCode = @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; + 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 >= 65536 + 256 ? 1 : 0) + (pledgedSrcSize >= 0xFFFFFFFFU ? 1 : 0) : 0); - byte frameHeaderDescriptionByte = (byte)(dictIDSizeCode + (checksumFlag << 2) + (singleSegment << 5) + (fcsCode << 6)); + uint fcsCode = (uint)( + @params->fParams.contentSizeFlag != 0 + ? (pledgedSrcSize >= 256 ? 1 : 0) + + (pledgedSrcSize >= 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))); + assert( + !(@params->fParams.contentSizeFlag != 0 && pledgedSrcSize == unchecked(0UL - 1)) + ); if (dstCapacity < 18) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); @@ -4843,7 +6754,13 @@ namespace ZstdSharp.Unsafe * * 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) + public static nuint ZSTD_writeSkippableFrame( + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize, + uint magicVariant + ) { byte* op = (byte*)dst; if (dstCapacity < srcSize + 8) @@ -4896,10 +6813,17 @@ namespace ZstdSharp.Unsafe * NOTE: seqs are not verified! Invalid sequences can cause out-of-bounds memory * access and data corruption. */ - private static void ZSTD_referenceExternalSequences(ZSTD_CCtx_s* cctx, rawSeq* seq, nuint nbSeq) + private static void ZSTD_referenceExternalSequences( + ZSTD_CCtx_s* cctx, + rawSeq* seq, + nuint nbSeq + ) { assert(cctx->stage == ZSTD_compressionStage_e.ZSTDcs_init); - assert(nbSeq == 0 || cctx->appliedParams.ldmParams.enableLdm != ZSTD_paramSwitch_e.ZSTD_ps_enable); + assert( + nbSeq == 0 + || cctx->appliedParams.ldmParams.enableLdm != ZSTD_paramSwitch_e.ZSTD_ps_enable + ); cctx->externSeqStore.seq = seq; cctx->externSeqStore.size = nbSeq; cctx->externSeqStore.capacity = nbSeq; @@ -4907,7 +6831,15 @@ namespace ZstdSharp.Unsafe cctx->externSeqStore.posInSequence = 0; } - private static nuint ZSTD_compressContinue_internal(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint frame, uint lastFrameChunk) + 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; @@ -4918,7 +6850,13 @@ namespace ZstdSharp.Unsafe if (frame != 0 && cctx->stage == ZSTD_compressionStage_e.ZSTDcs_init) { - fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, &cctx->appliedParams, cctx->pledgedSrcSizePlusOne - 1, cctx->dictID); + fhSize = ZSTD_writeFrameHeader( + dst, + dstCapacity, + &cctx->appliedParams, + cctx->pledgedSrcSizePlusOne - 1, + cctx->dictID + ); { nuint err_code = fhSize; if (ERR_isError(err_code)) @@ -4948,11 +6886,27 @@ namespace ZstdSharp.Unsafe if (frame == 0) { - ZSTD_overflowCorrectIfNeeded(ms, &cctx->workspace, &cctx->appliedParams, src, (byte*)src + srcSize); + ZSTD_overflowCorrectIfNeeded( + ms, + &cctx->workspace, + &cctx->appliedParams, + src, + (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 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)) @@ -4963,7 +6917,12 @@ namespace ZstdSharp.Unsafe cctx->consumedSrcSize += srcSize; cctx->producedCSize += cSize + fhSize; - assert(!(cctx->appliedParams.fParams.contentSizeFlag != 0 && cctx->pledgedSrcSizePlusOne == 0)); + assert( + !( + cctx->appliedParams.fParams.contentSizeFlag != 0 + && cctx->pledgedSrcSizePlusOne == 0 + ) + ); if (cctx->pledgedSrcSizePlusOne != 0) { if (cctx->consumedSrcSize + 1 > cctx->pledgedSrcSizePlusOne) @@ -4976,13 +6935,25 @@ namespace ZstdSharp.Unsafe } } - private static nuint ZSTD_compressContinue_public(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + private static nuint ZSTD_compressContinue_public( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1, 0); } /* NOTE: Must just wrap ZSTD_compressContinue_public() */ - public static nuint ZSTD_compressContinue(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + public static nuint ZSTD_compressContinue( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { return ZSTD_compressContinue_public(cctx, dst, dstCapacity, src, srcSize); } @@ -4991,7 +6962,9 @@ namespace ZstdSharp.Unsafe { ZSTD_compressionParameters cParams = cctx->appliedParams.cParams; assert(ZSTD_checkCParams(cParams) == 0); - return cctx->appliedParams.maxBlockSize < (nuint)1 << (int)cParams.windowLog ? cctx->appliedParams.maxBlockSize : (nuint)1 << (int)cParams.windowLog; + return cctx->appliedParams.maxBlockSize < (nuint)1 << (int)cParams.windowLog + ? cctx->appliedParams.maxBlockSize + : (nuint)1 << (int)cParams.windowLog; } /* NOTE: Must just wrap ZSTD_getBlockSize_deprecated() */ @@ -5001,7 +6974,13 @@ namespace ZstdSharp.Unsafe } /* NOTE: Must just wrap ZSTD_compressBlock_deprecated() */ - private static nuint ZSTD_compressBlock_deprecated(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_deprecated( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { { nuint blockSizeMax = ZSTD_getBlockSize_deprecated(cctx); @@ -5015,7 +6994,13 @@ namespace ZstdSharp.Unsafe } /* NOTE: Must just wrap ZSTD_compressBlock_deprecated() */ - public static nuint ZSTD_compressBlock(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + public static nuint ZSTD_compressBlock( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_deprecated(cctx, dst, dstCapacity, src, srcSize); } @@ -5023,11 +7008,23 @@ namespace ZstdSharp.Unsafe /*! 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, ZSTD_tableFillPurpose_e tfp) + 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, + ZSTD_tableFillPurpose_e tfp + ) { byte* ip = (byte*)src; byte* iend = ip + srcSize; - int loadLdmDict = @params->ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable && ls != null ? 1 : 0; + int loadLdmDict = + @params->ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable && ls != null + ? 1 + : 0; ZSTD_assertEqualCParams(@params->cParams, ms->cParams); { /* Allow the dictionary to set indices up to exactly ZSTD_CURRENT_MAX. @@ -5044,7 +7041,8 @@ namespace ZstdSharp.Unsafe * When short cache is used, we need to truncate the dictionary * so that its indices don't overlap with the tag. */ const uint shortCacheMaxDictSize = (1U << 32 - 8) - 2; - maxDictSize = maxDictSize < shortCacheMaxDictSize ? maxDictSize : shortCacheMaxDictSize; + maxDictSize = + maxDictSize < shortCacheMaxDictSize ? maxDictSize : shortCacheMaxDictSize; assert(loadLdmDict == 0); } @@ -5056,7 +7054,10 @@ namespace ZstdSharp.Unsafe } } - if (srcSize > unchecked((uint)-1) - (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20))) + if ( + srcSize + > unchecked((uint)-1) - (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20)) + ) { assert(ZSTD_window_isEmpty(ms->window) != 0); #if DEBUG @@ -5074,7 +7075,19 @@ namespace ZstdSharp.Unsafe } { - uint maxDictSize = 1U << (int)((@params->cParams.hashLog + 3 > @params->cParams.chainLog + 1 ? @params->cParams.hashLog + 3 : @params->cParams.chainLog + 1) < 31 ? @params->cParams.hashLog + 3 > @params->cParams.chainLog + 1 ? @params->cParams.hashLog + 3 : @params->cParams.chainLog + 1 : 31); + uint maxDictSize = + 1U + << (int)( + ( + @params->cParams.hashLog + 3 > @params->cParams.chainLog + 1 + ? @params->cParams.hashLog + 3 + : @params->cParams.chainLog + 1 + ) < 31 + ? @params->cParams.hashLog + 3 > @params->cParams.chainLog + 1 + ? @params->cParams.hashLog + 3 + : @params->cParams.chainLog + 1 + : 31 + ); if (srcSize > maxDictSize) { ip = iend - maxDictSize; @@ -5142,7 +7155,11 @@ namespace ZstdSharp.Unsafe * 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) + private static FSE_repeat ZSTD_dictNCountRepeat( + short* normalizedCounter, + uint dictMaxSymbolValue, + uint maxSymbolValue + ) { uint s; if (dictMaxSymbolValue < maxSymbolValue) @@ -5166,7 +7183,12 @@ namespace ZstdSharp.Unsafe * return : size of dictionary header (size of magic number + dict ID + entropy tables) * assumptions : magic number supposed already checked * and dictSize >= 8 */ - private static nuint ZSTD_loadCEntropy(ZSTD_compressedBlockState_t* bs, void* workspace, void* dict, nuint dictSize) + private static nuint ZSTD_loadCEntropy( + ZSTD_compressedBlockState_t* bs, + void* workspace, + void* dict, + nuint dictSize + ) { short* offcodeNCount = stackalloc short[32]; uint offcodeMaxValue = 31; @@ -5178,7 +7200,13 @@ namespace ZstdSharp.Unsafe { uint maxSymbolValue = 255; uint hasZeroWeights = 1; - nuint hufHeaderSize = HUF_readCTable(&bs->entropy.huf.CTable.e0, &maxSymbolValue, dictPtr, (nuint)(dictEnd - dictPtr), &hasZeroWeights); + nuint hufHeaderSize = HUF_readCTable( + &bs->entropy.huf.CTable.e0, + &maxSymbolValue, + dictPtr, + (nuint)(dictEnd - dictPtr), + &hasZeroWeights + ); if (hasZeroWeights == 0 && maxSymbolValue == 255) bs->entropy.huf.repeatMode = HUF_repeat.HUF_repeat_valid; if (ERR_isError(hufHeaderSize)) @@ -5191,7 +7219,13 @@ namespace ZstdSharp.Unsafe { uint offcodeLog; - nuint offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (nuint)(dictEnd - dictPtr)); + nuint offcodeHeaderSize = FSE_readNCount( + offcodeNCount, + &offcodeMaxValue, + &offcodeLog, + dictPtr, + (nuint)(dictEnd - dictPtr) + ); if (ERR_isError(offcodeHeaderSize)) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); @@ -5202,7 +7236,18 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } - if (ERR_isError(FSE_buildCTable_wksp(bs->entropy.fse.offcodeCTable, offcodeNCount, 31, offcodeLog, workspace, (8 << 10) + 512))) + if ( + ERR_isError( + FSE_buildCTable_wksp( + bs->entropy.fse.offcodeCTable, + offcodeNCount, + 31, + offcodeLog, + workspace, + (8 << 10) + 512 + ) + ) + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } @@ -5212,8 +7257,15 @@ namespace ZstdSharp.Unsafe { short* matchlengthNCount = stackalloc short[53]; - uint matchlengthMaxValue = 52, matchlengthLog; - nuint matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (nuint)(dictEnd - dictPtr)); + uint matchlengthMaxValue = 52, + matchlengthLog; + nuint matchlengthHeaderSize = FSE_readNCount( + matchlengthNCount, + &matchlengthMaxValue, + &matchlengthLog, + dictPtr, + (nuint)(dictEnd - dictPtr) + ); if (ERR_isError(matchlengthHeaderSize)) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); @@ -5224,19 +7276,41 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } - if (ERR_isError(FSE_buildCTable_wksp(bs->entropy.fse.matchlengthCTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog, workspace, (8 << 10) + 512))) + if ( + ERR_isError( + FSE_buildCTable_wksp( + bs->entropy.fse.matchlengthCTable, + matchlengthNCount, + matchlengthMaxValue, + matchlengthLog, + workspace, + (8 << 10) + 512 + ) + ) + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } - bs->entropy.fse.matchlength_repeatMode = ZSTD_dictNCountRepeat(matchlengthNCount, matchlengthMaxValue, 52); + bs->entropy.fse.matchlength_repeatMode = ZSTD_dictNCountRepeat( + matchlengthNCount, + matchlengthMaxValue, + 52 + ); dictPtr += matchlengthHeaderSize; } { short* litlengthNCount = stackalloc short[36]; - uint litlengthMaxValue = 35, litlengthLog; - nuint litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (nuint)(dictEnd - dictPtr)); + uint litlengthMaxValue = 35, + litlengthLog; + nuint litlengthHeaderSize = FSE_readNCount( + litlengthNCount, + &litlengthMaxValue, + &litlengthLog, + dictPtr, + (nuint)(dictEnd - dictPtr) + ); if (ERR_isError(litlengthHeaderSize)) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); @@ -5247,12 +7321,27 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } - if (ERR_isError(FSE_buildCTable_wksp(bs->entropy.fse.litlengthCTable, litlengthNCount, litlengthMaxValue, litlengthLog, workspace, (8 << 10) + 512))) + if ( + ERR_isError( + FSE_buildCTable_wksp( + bs->entropy.fse.litlengthCTable, + litlengthNCount, + litlengthMaxValue, + litlengthLog, + workspace, + (8 << 10) + 512 + ) + ) + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } - bs->entropy.fse.litlength_repeatMode = ZSTD_dictNCountRepeat(litlengthNCount, litlengthMaxValue, 35); + bs->entropy.fse.litlength_repeatMode = ZSTD_dictNCountRepeat( + litlengthNCount, + litlengthMaxValue, + 35 + ); dictPtr += litlengthHeaderSize; } @@ -5275,19 +7364,27 @@ namespace ZstdSharp.Unsafe offcodeMax = ZSTD_highbit32(maxOffset); } - bs->entropy.fse.offcode_repeatMode = ZSTD_dictNCountRepeat(offcodeNCount, offcodeMaxValue, offcodeMax < 31 ? offcodeMax : 31); + bs->entropy.fse.offcode_repeatMode = ZSTD_dictNCountRepeat( + 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)); + 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 unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted) + ); } } } @@ -5305,7 +7402,17 @@ namespace ZstdSharp.Unsafe * 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, ZSTD_tableFillPurpose_e tfp, void* workspace) + 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, + ZSTD_tableFillPurpose_e tfp, + void* workspace + ) { byte* dictPtr = (byte*)dict; byte* dictEnd = dictPtr + dictSize; @@ -5327,7 +7434,16 @@ namespace ZstdSharp.Unsafe { nuint dictContentSize = (nuint)(dictEnd - dictPtr); { - nuint err_code = ZSTD_loadDictionaryContent(ms, null, ws, @params, dictPtr, dictContentSize, dtlm, tfp); + nuint err_code = ZSTD_loadDictionaryContent( + ms, + null, + ws, + @params, + dictPtr, + dictContentSize, + dtlm, + tfp + ); if (ERR_isError(err_code)) { return err_code; @@ -5340,7 +7456,19 @@ namespace ZstdSharp.Unsafe /** 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, ZSTD_tableFillPurpose_e tfp, void* workspace) + 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, + ZSTD_tableFillPurpose_e tfp, + void* workspace + ) { if (dict == null || dictSize < 8) { @@ -5359,7 +7487,16 @@ namespace ZstdSharp.Unsafe { if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_auto) { - return ZSTD_loadDictionaryContent(ms, ls, ws, @params, dict, dictSize, dtlm, tfp); + return ZSTD_loadDictionaryContent( + ms, + ls, + ws, + @params, + dict, + dictSize, + dtlm, + tfp + ); } if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_fullDict) @@ -5370,24 +7507,61 @@ namespace ZstdSharp.Unsafe assert(0 != 0); } - return ZSTD_loadZstdDictionary(bs, ms, ws, @params, dict, dictSize, dtlm, tfp, workspace); + return ZSTD_loadZstdDictionary( + bs, + ms, + ws, + @params, + dict, + dictSize, + dtlm, + tfp, + workspace + ); } /*! ZSTD_compressBegin_internal() : * Assumption : either @dict OR @cdict (or none) is non-NULL, never both * @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) + 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))); assert(!(dict != null && cdict != null)); - if (cdict != null && cdict->dictContentSize > 0 && (pledgedSrcSize < 128 * (1 << 10) || pledgedSrcSize < cdict->dictContentSize * 6UL || pledgedSrcSize == unchecked(0UL - 1) || cdict->compressionLevel == 0) && @params->attachDictPref != ZSTD_dictAttachPref_e.ZSTD_dictForceLoad) + if ( + cdict != null + && cdict->dictContentSize > 0 + && ( + pledgedSrcSize < 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); + nuint err_code = ZSTD_resetCCtx_internal( + cctx, + @params, + pledgedSrcSize, + dictContentSize, + ZSTD_compResetPolicy_e.ZSTDcrp_makeClean, + zbuff + ); if (ERR_isError(err_code)) { return err_code; @@ -5395,7 +7569,34 @@ namespace ZstdSharp.Unsafe } { - 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, ZSTD_tableFillPurpose_e.ZSTD_tfp_forCCtx, cctx->tmpWorkspace) : ZSTD_compress_insertDictionary(cctx->blockState.prevCBlock, &cctx->blockState.matchState, &cctx->ldmState, &cctx->workspace, &cctx->appliedParams, dict, dictSize, dictContentType, dtlm, ZSTD_tableFillPurpose_e.ZSTD_tfp_forCCtx, cctx->tmpWorkspace); + 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, + ZSTD_tableFillPurpose_e.ZSTD_tfp_forCCtx, + cctx->tmpWorkspace + ) + : ZSTD_compress_insertDictionary( + cctx->blockState.prevCBlock, + &cctx->blockState.matchState, + &cctx->ldmState, + &cctx->workspace, + &cctx->appliedParams, + dict, + dictSize, + dictContentType, + dtlm, + ZSTD_tableFillPurpose_e.ZSTD_tfp_forCCtx, + cctx->tmpWorkspace + ); { nuint err_code = dictID; if (ERR_isError(err_code)) @@ -5414,7 +7615,16 @@ namespace ZstdSharp.Unsafe /* ZSTD_compressBegin_advanced_internal() : * Private use only. To be called from zstdmt_compress.c. */ - private 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) + private 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 + ) { { /* compression parameters verification and optimization */ @@ -5425,30 +7635,84 @@ namespace ZstdSharp.Unsafe } } - return ZSTD_compressBegin_internal(cctx, dict, dictSize, dictContentType, dtlm, cdict, @params, pledgedSrcSize, ZSTD_buffered_policy_e.ZSTDb_not_buffered); + 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) + 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, null, &cctxParams, pledgedSrcSize); + return ZSTD_compressBegin_advanced_internal( + cctx, + dict, + dictSize, + ZSTD_dictContentType_e.ZSTD_dct_auto, + ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, + null, + &cctxParams, + pledgedSrcSize + ); } - private static nuint ZSTD_compressBegin_usingDict_deprecated(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize, int compressionLevel) + private static nuint ZSTD_compressBegin_usingDict_deprecated( + 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); + 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, null, &cctxParams, unchecked(0UL - 1), ZSTD_buffered_policy_e.ZSTDb_not_buffered); + return ZSTD_compressBegin_internal( + cctx, + dict, + dictSize, + ZSTD_dictContentType_e.ZSTD_dct_auto, + ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, + null, + &cctxParams, + unchecked(0UL - 1), + ZSTD_buffered_policy_e.ZSTDb_not_buffered + ); } - public static nuint ZSTD_compressBegin_usingDict(ZSTD_CCtx_s* cctx, void* dict, nuint dictSize, int compressionLevel) + public static nuint ZSTD_compressBegin_usingDict( + ZSTD_CCtx_s* cctx, + void* dict, + nuint dictSize, + int compressionLevel + ) { return ZSTD_compressBegin_usingDict_deprecated(cctx, dict, dictSize, compressionLevel); } @@ -5520,14 +7784,26 @@ namespace ZstdSharp.Unsafe /** ZSTD_CCtx_trace() : * Trace the end of a compression call. */ - private static void ZSTD_CCtx_trace(ZSTD_CCtx_s* cctx, nuint extraCSize) - { - } + private static void ZSTD_CCtx_trace(ZSTD_CCtx_s* cctx, nuint extraCSize) { } - private static nuint ZSTD_compressEnd_public(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + private static nuint ZSTD_compressEnd_public( + 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 cSize = ZSTD_compressContinue_internal( + cctx, + dst, + dstCapacity, + src, + srcSize, + 1, + 1 + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -5545,7 +7821,12 @@ namespace ZstdSharp.Unsafe } } - assert(!(cctx->appliedParams.fParams.contentSizeFlag != 0 && cctx->pledgedSrcSizePlusOne == 0)); + assert( + !( + cctx->appliedParams.fParams.contentSizeFlag != 0 + && cctx->pledgedSrcSizePlusOne == 0 + ) + ); if (cctx->pledgedSrcSizePlusOne != 0) { if (cctx->pledgedSrcSizePlusOne != cctx->consumedSrcSize + 1) @@ -5559,7 +7840,13 @@ namespace ZstdSharp.Unsafe } /* NOTE: Must just wrap ZSTD_compressEnd_public() */ - public static nuint ZSTD_compressEnd(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + public static nuint ZSTD_compressEnd( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { return ZSTD_compressEnd_public(cctx, dst, dstCapacity, src, srcSize); } @@ -5568,7 +7855,16 @@ namespace ZstdSharp.Unsafe * 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) + 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); @@ -5579,14 +7875,42 @@ namespace ZstdSharp.Unsafe } ZSTD_CCtxParams_init_internal(&cctx->simpleApiParams, &@params, 0); - return ZSTD_compress_advanced_internal(cctx, dst, dstCapacity, src, srcSize, dict, dictSize, &cctx->simpleApiParams); + return ZSTD_compress_advanced_internal( + cctx, + dst, + dstCapacity, + src, + srcSize, + dict, + dictSize, + &cctx->simpleApiParams + ); } /* Internal */ - private 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) + private 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, null, @params, srcSize, ZSTD_buffered_policy_e.ZSTDb_not_buffered); + nuint err_code = ZSTD_compressBegin_internal( + cctx, + dict, + dictSize, + ZSTD_dictContentType_e.ZSTD_dct_auto, + ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, + null, + @params, + srcSize, + ZSTD_buffered_policy_e.ZSTDb_not_buffered + ); if (ERR_isError(err_code)) { return err_code; @@ -5606,15 +7930,42 @@ namespace ZstdSharp.Unsafe * 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) + 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, srcSize, dict != null ? dictSize : 0, ZSTD_CParamMode_e.ZSTD_cpm_noAttachDict); + ZSTD_parameters @params = ZSTD_getParams_internal( + compressionLevel, + 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); + 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); + return ZSTD_compress_advanced_internal( + cctx, + dst, + dstCapacity, + src, + srcSize, + dict, + dictSize, + &cctx->simpleApiParams + ); } /*! ZSTD_compressCCtx() : @@ -5625,10 +7976,26 @@ namespace ZstdSharp.Unsafe * 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) + 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); + return ZSTD_compress_usingDict( + cctx, + dst, + dstCapacity, + src, + srcSize, + null, + 0, + compressionLevel + ); } /*************************************** @@ -5640,7 +8007,13 @@ namespace ZstdSharp.Unsafe * enough space to successfully compress the data. * @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) + public static nuint ZSTD_compress( + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize, + int compressionLevel + ) { nuint result; ZSTD_CCtx_s ctxBody; @@ -5652,9 +8025,25 @@ namespace ZstdSharp.Unsafe /*! 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) + 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((8 << 10) + 512) + ZSTD_sizeof_matchState(&cParams, ZSTD_resolveRowMatchFinderMode(ZSTD_paramSwitch_e.ZSTD_ps_auto, &cParams), 1, 0) + (dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef ? 0 : ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(dictSize, (nuint)sizeof(void*)))); + return ZSTD_cwksp_alloc_size((nuint)sizeof(ZSTD_CDict_s)) + + ZSTD_cwksp_alloc_size((8 << 10) + 512) + + ZSTD_sizeof_matchState( + &cParams, + ZSTD_resolveRowMatchFinderMode(ZSTD_paramSwitch_e.ZSTD_ps_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() : @@ -5664,29 +8053,53 @@ namespace ZstdSharp.Unsafe */ 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); + 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 (nuint)(cdict->workspace.workspace == cdict ? 0 : sizeof(ZSTD_CDict_s)) + ZSTD_cwksp_sizeof(&cdict->workspace); + return (nuint)(cdict->workspace.workspace == cdict ? 0 : 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) + 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) + 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*))); + 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)); @@ -5698,10 +8111,21 @@ namespace ZstdSharp.Unsafe cdict->dictContentSize = dictSize; cdict->dictContentType = dictContentType; - cdict->entropyWorkspace = (uint*)ZSTD_cwksp_reserve_object(&cdict->workspace, (8 << 10) + 512); + cdict->entropyWorkspace = (uint*)ZSTD_cwksp_reserve_object( + &cdict->workspace, + (8 << 10) + 512 + ); 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); + 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)) { return err_code; @@ -5712,7 +8136,19 @@ namespace ZstdSharp.Unsafe @params.compressionLevel = 3; @params.fParams.contentSizeFlag = 1; { - nuint dictID = ZSTD_compress_insertDictionary(&cdict->cBlockState, &cdict->matchState, null, &cdict->workspace, &@params, cdict->dictContent, cdict->dictContentSize, dictContentType, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_full, ZSTD_tableFillPurpose_e.ZSTD_tfp_forCDict, cdict->entropyWorkspace); + nuint dictID = ZSTD_compress_insertDictionary( + &cdict->cBlockState, + &cdict->matchState, + null, + &cdict->workspace, + &@params, + cdict->dictContent, + cdict->dictContentSize, + dictContentType, + ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_full, + ZSTD_tableFillPurpose_e.ZSTD_tfp_forCDict, + cdict->entropyWorkspace + ); { nuint err_code = dictID; if (ERR_isError(err_code)) @@ -5729,12 +8165,37 @@ namespace ZstdSharp.Unsafe return 0; } - private static ZSTD_CDict_s* ZSTD_createCDict_advanced_internal(nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_compressionParameters cParams, ZSTD_paramSwitch_e useRowMatchFinder, int enableDedicatedDictSearch, ZSTD_customMem customMem) + private static ZSTD_CDict_s* ZSTD_createCDict_advanced_internal( + nuint dictSize, + ZSTD_dictLoadMethod_e dictLoadMethod, + ZSTD_compressionParameters cParams, + ZSTD_paramSwitch_e useRowMatchFinder, + int enableDedicatedDictSearch, + ZSTD_customMem customMem + ) { - if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + if ( + ((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) + != 0 + ) return null; { - nuint workspaceSize = ZSTD_cwksp_alloc_size((nuint)sizeof(ZSTD_CDict_s)) + ZSTD_cwksp_alloc_size((8 << 10) + 512) + 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*)))); + nuint workspaceSize = + ZSTD_cwksp_alloc_size((nuint)sizeof(ZSTD_CDict_s)) + + ZSTD_cwksp_alloc_size((8 << 10) + 512) + + 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; @@ -5744,7 +8205,12 @@ namespace ZstdSharp.Unsafe return null; } - ZSTD_cwksp_init(&ws, workspace, workspaceSize, ZSTD_cwksp_static_alloc_e.ZSTD_cwksp_dynamic_alloc); + 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); @@ -5755,46 +8221,105 @@ namespace ZstdSharp.Unsafe } } - 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) + 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; cctxParams = new 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); + 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) + 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) + if ( + ((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) + != 0 + ) return null; if (cctxParams.enableDedicatedDictSearch != 0) { - cParams = ZSTD_dedicatedDictSearch_getCParams(cctxParams.compressionLevel, dictSize); + 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); + 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); + 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, cctxParams.enableDedicatedDictSearch, customMem); - if (cdict == null || ERR_isError(ZSTD_initCDict_internal(cdict, dict, dictSize, dictLoadMethod, dictContentType, cctxParams))) + cctxParams.useRowMatchFinder = ZSTD_resolveRowMatchFinderMode( + cctxParams.useRowMatchFinder, + &cParams + ); + cdict = ZSTD_createCDict_advanced_internal( + dictSize, + dictLoadMethod, + cctxParams.cParams, + cctxParams.useRowMatchFinder, + cctxParams.enableDedicatedDictSearch, + customMem + ); + if ( + cdict == null + || ERR_isError( + ZSTD_initCDict_internal( + cdict, + dict, + dictSize, + dictLoadMethod, + dictContentType, + cctxParams + ) + ) + ) { ZSTD_freeCDict(cdict); return null; @@ -5815,10 +8340,26 @@ namespace ZstdSharp.Unsafe * 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) + 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); + 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; @@ -5830,10 +8371,26 @@ namespace ZstdSharp.Unsafe * 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) + 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); + 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; @@ -5872,19 +8429,43 @@ namespace ZstdSharp.Unsafe * 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) + 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_paramSwitch_e useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(ZSTD_paramSwitch_e.ZSTD_ps_auto, &cParams); + ZSTD_paramSwitch_e useRowMatchFinder = ZSTD_resolveRowMatchFinderMode( + ZSTD_paramSwitch_e.ZSTD_ps_auto, + &cParams + ); /* enableDedicatedDictSearch */ 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((8 << 10) + 512) + matchStateSize; + 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((8 << 10) + 512) + + matchStateSize; ZSTD_CDict_s* cdict; ZSTD_CCtx_params_s @params; if (((nuint)workspace & 7) != 0) return null; { ZSTD_cwksp ws; - ZSTD_cwksp_init(&ws, workspace, workspaceSize, ZSTD_cwksp_static_alloc_e.ZSTD_cwksp_static_alloc); + 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 null; @@ -5898,7 +8479,18 @@ namespace ZstdSharp.Unsafe @params.useRowMatchFinder = useRowMatchFinder; cdict->useRowMatchFinder = useRowMatchFinder; cdict->compressionLevel = 0; - if (ERR_isError(ZSTD_initCDict_internal(cdict, dict, dictSize, dictLoadMethod, dictContentType, @params))) + if ( + ERR_isError( + ZSTD_initCDict_internal( + cdict, + dict, + dictSize, + dictLoadMethod, + dictContentType, + @params + ) + ) + ) return null; return cdict; } @@ -5925,7 +8517,12 @@ namespace ZstdSharp.Unsafe /* 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) + 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) @@ -5936,38 +8533,70 @@ namespace ZstdSharp.Unsafe { ZSTD_parameters @params; @params.fParams = fParams; - @params.cParams = pledgedSrcSize < 128 * (1 << 10) || pledgedSrcSize < cdict->dictContentSize * 6UL || pledgedSrcSize == unchecked(0UL - 1) || cdict->compressionLevel == 0 ? ZSTD_getCParamsFromCDict(cdict) : ZSTD_getCParams(cdict->compressionLevel, pledgedSrcSize, cdict->dictContentSize); + @params.cParams = + pledgedSrcSize < 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; + 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); + 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) + 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 */ - private static nuint ZSTD_compressBegin_usingCDict_deprecated(ZSTD_CCtx_s* cctx, ZSTD_CDict_s* cdict) + private static nuint ZSTD_compressBegin_usingCDict_deprecated( + ZSTD_CCtx_s* cctx, + ZSTD_CDict_s* cdict + ) { /*content*/ ZSTD_frameParameters fParams = new ZSTD_frameParameters { contentSizeFlag = 0, checksumFlag = 0, - noDictIDFlag = 0 + noDictIDFlag = 0, }; return ZSTD_compressBegin_usingCDict_internal(cctx, cdict, fParams, unchecked(0UL - 1)); } @@ -5980,11 +8609,24 @@ namespace ZstdSharp.Unsafe /*! 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) + 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 + ) { { /* will check if cdict != NULL */ - nuint err_code = ZSTD_compressBegin_usingCDict_internal(cctx, cdict, fParams, srcSize); + nuint err_code = ZSTD_compressBegin_usingCDict_internal( + cctx, + cdict, + fParams, + srcSize + ); if (ERR_isError(err_code)) { return err_code; @@ -5997,9 +8639,25 @@ namespace ZstdSharp.Unsafe /*! 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) + 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); + return ZSTD_compress_usingCDict_internal( + cctx, + dst, + dstCapacity, + src, + srcSize, + cdict, + fParams + ); } /*! ZSTD_compress_usingCDict() : @@ -6007,16 +8665,31 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_compress_usingCDict( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize, + ZSTD_CDict_s* cdict + ) { /*content*/ ZSTD_frameParameters fParams = new ZSTD_frameParameters { contentSizeFlag = 1, checksumFlag = 0, - noDictIDFlag = 0 + noDictIDFlag = 0, }; - return ZSTD_compress_usingCDict_internal(cctx, dst, dstCapacity, src, srcSize, cdict, fParams); + return ZSTD_compress_usingCDict_internal( + cctx, + dst, + dstCapacity, + src, + srcSize, + cdict, + fParams + ); } /* ****************************************************************** @@ -6053,7 +8726,11 @@ namespace ZstdSharp.Unsafe return ZSTD_compressBound(1 << 17) + ZSTD_blockHeaderSize + 4; } - private static ZSTD_CParamMode_e ZSTD_getCParamMode(ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize) + 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; @@ -6093,7 +8770,14 @@ namespace ZstdSharp.Unsafe * 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 */ - private static nuint ZSTD_initCStream_internal(ZSTD_CCtx_s* zcs, void* dict, nuint dictSize, ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s* @params, ulong pledgedSrcSize) + private 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); @@ -6137,7 +8821,12 @@ namespace ZstdSharp.Unsafe /* 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) + 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); @@ -6193,13 +8882,20 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_initCStream_advanced( + ZSTD_CCtx_s* zcs, + void* dict, + nuint dictSize, + ZSTD_parameters @params, + ulong pss + ) { /* for compatibility with older programs relying on this behavior. * Users should now specify ZSTD_CONTENTSIZE_UNKNOWN. * This line will be removed in the future. */ - ulong pledgedSrcSize = pss == 0 && @params.fParams.contentSizeFlag == 0 ? unchecked(0UL - 1) : 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)) @@ -6248,7 +8944,12 @@ namespace ZstdSharp.Unsafe * 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) + 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); @@ -6259,7 +8960,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(zcs, ZSTD_cParameter.ZSTD_c_compressionLevel, compressionLevel); + nuint err_code = ZSTD_CCtx_setParameter( + zcs, + ZSTD_cParameter.ZSTD_c_compressionLevel, + compressionLevel + ); if (ERR_isError(err_code)) { return err_code; @@ -6289,7 +8994,11 @@ namespace ZstdSharp.Unsafe * "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) + public static nuint ZSTD_initCStream_srcSize( + ZSTD_CCtx_s* zcs, + int compressionLevel, + ulong pss + ) { /* temporary : 0 interpreted as "unknown" during transition period. * Users willing to specify "unknown" **must** use ZSTD_CONTENTSIZE_UNKNOWN. @@ -6313,7 +9022,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(zcs, ZSTD_cParameter.ZSTD_c_compressionLevel, compressionLevel); + nuint err_code = ZSTD_CCtx_setParameter( + zcs, + ZSTD_cParameter.ZSTD_c_compressionLevel, + compressionLevel + ); if (ERR_isError(err_code)) { return err_code; @@ -6360,7 +9073,11 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_setParameter(zcs, ZSTD_cParameter.ZSTD_c_compressionLevel, compressionLevel); + nuint err_code = ZSTD_CCtx_setParameter( + zcs, + ZSTD_cParameter.ZSTD_c_compressionLevel, + compressionLevel + ); if (ERR_isError(err_code)) { return err_code; @@ -6390,7 +9107,12 @@ namespace ZstdSharp.Unsafe /** ZSTD_compressStream_generic(): * internal function for all *compressStream*() variants * @return : hint size for next input to complete ongoing block */ - private static nuint ZSTD_compressStream_generic(ZSTD_CCtx_s* zcs, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input, ZSTD_EndDirective flushMode) + private static nuint ZSTD_compressStream_generic( + ZSTD_CCtx_s* zcs, + ZSTD_outBuffer_s* output, + ZSTD_inBuffer_s* input, + ZSTD_EndDirective flushMode + ) { assert(input != null); sbyte* istart = (sbyte*)input->src; @@ -6445,10 +9167,24 @@ namespace ZstdSharp.Unsafe 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) + 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 + ) { /* shortcut to compression pass directly into output buffer */ - nuint cSize = ZSTD_compressEnd_public(zcs, op, (nuint)(oend - op), ip, (nuint)(iend - ip)); + nuint cSize = ZSTD_compressEnd_public( + zcs, + op, + (nuint)(oend - op), + ip, + (nuint)(iend - ip) + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -6468,17 +9204,28 @@ namespace ZstdSharp.Unsafe if (zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) { nuint toLoad = zcs->inBuffTarget - zcs->inBuffPos; - nuint loaded = ZSTD_limitCopy(zcs->inBuff + zcs->inBuffPos, toLoad, ip, (nuint)(iend - ip)); + nuint loaded = ZSTD_limitCopy( + zcs->inBuff + zcs->inBuffPos, + toLoad, + ip, + (nuint)(iend - ip) + ); zcs->inBuffPos += loaded; if (ip != null) ip += loaded; - if (flushMode == ZSTD_EndDirective.ZSTD_e_continue && zcs->inBuffPos < zcs->inBuffTarget) + 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) + if ( + flushMode == ZSTD_EndDirective.ZSTD_e_flush + && zcs->inBuffPos == zcs->inToCompress + ) { someMoreWork = 0; break; @@ -6486,8 +9233,13 @@ namespace ZstdSharp.Unsafe } else { - assert(zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable); - if (flushMode == ZSTD_EndDirective.ZSTD_e_continue && (nuint)(iend - ip) < zcs->blockSizeMax) + assert( + zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable + ); + if ( + flushMode == ZSTD_EndDirective.ZSTD_e_continue + && (nuint)(iend - ip) < zcs->blockSizeMax + ) { zcs->stableIn_notConsumed = (nuint)(iend - ip); ip = iend; @@ -6503,12 +9255,23 @@ namespace ZstdSharp.Unsafe } { - int inputBuffered = zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered ? 1 : 0; + 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->blockSizeMax ? (nuint)(iend - ip) : zcs->blockSizeMax; - if (oSize >= ZSTD_compressBound(iSize) || zcs->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable) + nuint iSize = + inputBuffered != 0 ? zcs->inBuffPos - zcs->inToCompress + : (nuint)(iend - ip) < zcs->blockSizeMax ? (nuint)(iend - ip) + : zcs->blockSizeMax; + if ( + oSize >= ZSTD_compressBound(iSize) + || zcs->appliedParams.outBufferMode + == ZSTD_bufferMode_e.ZSTD_bm_stable + ) cDst = op; else { @@ -6518,8 +9281,26 @@ namespace ZstdSharp.Unsafe if (inputBuffered != 0) { - uint lastBlock = flushMode == ZSTD_EndDirective.ZSTD_e_end && ip == iend ? 1U : 0U; - cSize = lastBlock != 0 ? ZSTD_compressEnd_public(zcs, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize) : ZSTD_compressContinue_public(zcs, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize); + uint lastBlock = + flushMode == ZSTD_EndDirective.ZSTD_e_end && ip == iend + ? 1U + : 0U; + cSize = + lastBlock != 0 + ? ZSTD_compressEnd_public( + zcs, + cDst, + oSize, + zcs->inBuff + zcs->inToCompress, + iSize + ) + : ZSTD_compressContinue_public( + zcs, + cDst, + oSize, + zcs->inBuff + zcs->inToCompress, + iSize + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -6544,8 +9325,14 @@ namespace ZstdSharp.Unsafe } else { - uint lastBlock = flushMode == ZSTD_EndDirective.ZSTD_e_end && ip + iSize == iend ? 1U : 0U; - cSize = lastBlock != 0 ? ZSTD_compressEnd_public(zcs, cDst, oSize, ip, iSize) : ZSTD_compressContinue_public(zcs, cDst, oSize, ip, iSize); + uint lastBlock = + flushMode == ZSTD_EndDirective.ZSTD_e_end && ip + iSize == iend + ? 1U + : 0U; + cSize = + lastBlock != 0 + ? ZSTD_compressEnd_public(zcs, cDst, oSize, ip, iSize) + : ZSTD_compressContinue_public(zcs, cDst, oSize, ip, iSize); if (ip != null) ip += iSize; { @@ -6569,7 +9356,10 @@ namespace ZstdSharp.Unsafe if (zcs->frameEnded != 0) { someMoreWork = 0; - ZSTD_CCtx_reset(zcs, ZSTD_ResetDirective.ZSTD_reset_session_only); + ZSTD_CCtx_reset( + zcs, + ZSTD_ResetDirective.ZSTD_reset_session_only + ); } break; @@ -6582,10 +9372,18 @@ namespace ZstdSharp.Unsafe goto case ZSTD_cStreamStage.zcss_flush; case ZSTD_cStreamStage.zcss_flush: - assert(zcs->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered); + assert( + zcs->appliedParams.outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered + ); + { nuint toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; - nuint flushed = ZSTD_limitCopy(op, (nuint)(oend - op), zcs->outBuff + zcs->outBuffFlushedSize, toFlush); + nuint flushed = ZSTD_limitCopy( + op, + (nuint)(oend - op), + zcs->outBuff + zcs->outBuffFlushedSize, + toFlush + ); if (flushed != 0) op += flushed; zcs->outBuffFlushedSize += flushed; @@ -6638,10 +9436,19 @@ namespace ZstdSharp.Unsafe * 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) + 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); + nuint err_code = ZSTD_compressStream2( + zcs, + output, + input, + ZSTD_EndDirective.ZSTD_e_continue + ); if (ERR_isError(err_code)) { return err_code; @@ -6654,7 +9461,11 @@ namespace ZstdSharp.Unsafe /* 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) + 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) { @@ -6670,20 +9481,29 @@ namespace ZstdSharp.Unsafe /* 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) + 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) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stabilityCondition_notRespected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stabilityCondition_notRespected) + ); } 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_stabilityCondition_notRespected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stabilityCondition_notRespected) + ); } return 0; @@ -6694,7 +9514,11 @@ namespace ZstdSharp.Unsafe * Otherwise, it's ignored. * @return: 0 on success, or a ZSTD_error code otherwise. */ - private static nuint ZSTD_CCtx_init_compressStream2(ZSTD_CCtx_s* cctx, ZSTD_EndDirective endOp, nuint inSize) + 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; @@ -6717,20 +9541,48 @@ namespace ZstdSharp.Unsafe if (endOp == ZSTD_EndDirective.ZSTD_e_end) cctx->pledgedSrcSizePlusOne = 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); + 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 + ); } - @params.postBlockSplitter = ZSTD_resolveBlockSplitterMode(@params.postBlockSplitter, &@params.cParams); - @params.ldmParams.enableLdm = ZSTD_resolveEnableLdm(@params.ldmParams.enableLdm, &@params.cParams); - @params.useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(@params.useRowMatchFinder, &@params.cParams); - @params.validateSequences = ZSTD_resolveExternalSequenceValidation(@params.validateSequences); + @params.postBlockSplitter = ZSTD_resolveBlockSplitterMode( + @params.postBlockSplitter, + &@params.cParams + ); + @params.ldmParams.enableLdm = ZSTD_resolveEnableLdm( + @params.ldmParams.enableLdm, + &@params.cParams + ); + @params.useRowMatchFinder = ZSTD_resolveRowMatchFinderMode( + @params.useRowMatchFinder, + &@params.cParams + ); + @params.validateSequences = ZSTD_resolveExternalSequenceValidation( + @params.validateSequences + ); @params.maxBlockSize = ZSTD_resolveMaxBlockSize(@params.maxBlockSize); - @params.searchForExternalRepcodes = ZSTD_resolveExternalRepcodeSearch(@params.searchForExternalRepcodes, @params.compressionLevel); + @params.searchForExternalRepcodes = ZSTD_resolveExternalRepcodeSearch( + @params.searchForExternalRepcodes, + @params.compressionLevel + ); if (ZSTD_hasExtSeqProd(&@params) != 0 && @params.nbWorkers >= 1) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_combination_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_combination_unsupported) + ); } if (cctx->pledgedSrcSizePlusOne - 1 <= 512 * (1 << 10)) @@ -6742,15 +9594,29 @@ namespace ZstdSharp.Unsafe { if (cctx->mtctx == null) { - cctx->mtctx = ZSTDMT_createCCtx_advanced((uint)@params.nbWorkers, cctx->customMem, cctx->pool); + cctx->mtctx = ZSTDMT_createCCtx_advanced( + (uint)@params.nbWorkers, + cctx->customMem, + cctx->pool + ); if (cctx->mtctx == null) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); } } { - nuint err_code = ZSTDMT_initCStream_internal(cctx->mtctx, prefixDict.dict, prefixDict.dictSize, prefixDict.dictContentType, cctx->cdict, @params, cctx->pledgedSrcSizePlusOne - 1); + nuint err_code = ZSTDMT_initCStream_internal( + cctx->mtctx, + prefixDict.dict, + prefixDict.dictSize, + prefixDict.dictContentType, + cctx->cdict, + @params, + cctx->pledgedSrcSizePlusOne - 1 + ); if (ERR_isError(err_code)) { return err_code; @@ -6758,7 +9624,8 @@ namespace ZstdSharp.Unsafe } cctx->dictID = cctx->cdict != null ? cctx->cdict->dictID : 0; - cctx->dictContentSize = cctx->cdict != null ? cctx->cdict->dictContentSize : prefixDict.dictSize; + cctx->dictContentSize = + cctx->cdict != null ? cctx->cdict->dictContentSize : prefixDict.dictSize; cctx->consumedSrcSize = 0; cctx->producedCSize = 0; cctx->streamStage = ZSTD_cStreamStage.zcss_load; @@ -6769,7 +9636,17 @@ namespace ZstdSharp.Unsafe ulong pledgedSrcSize = cctx->pledgedSrcSizePlusOne - 1; assert(!ERR_isError(ZSTD_checkCParams(@params.cParams))); { - 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); + 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)) { return err_code; @@ -6781,7 +9658,8 @@ namespace ZstdSharp.Unsafe cctx->inBuffPos = 0; if (cctx->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered) { - cctx->inBuffTarget = cctx->blockSizeMax + (nuint)(cctx->blockSizeMax == pledgedSrcSize ? 1 : 0); + cctx->inBuffTarget = + cctx->blockSizeMax + (nuint)(cctx->blockSizeMax == pledgedSrcSize ? 1 : 0); } else { @@ -6798,7 +9676,12 @@ namespace ZstdSharp.Unsafe /* @return provides a minimum amount of data remaining to be flushed from internal buffers */ - public static nuint ZSTD_compressStream2(ZSTD_CCtx_s* cctx, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input, ZSTD_EndDirective endOp) + 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) { @@ -6821,25 +9704,39 @@ namespace ZstdSharp.Unsafe /* no obligation to start from pos==0 */ nuint inputSize = input->size - input->pos; nuint totalInputSize = inputSize + cctx->stableIn_notConsumed; - if (cctx->requestedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable && endOp == ZSTD_EndDirective.ZSTD_e_continue && totalInputSize < 1 << 17) + if ( + cctx->requestedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable + && endOp == ZSTD_EndDirective.ZSTD_e_continue + && totalInputSize < 1 << 17 + ) { if (cctx->stableIn_notConsumed != 0) { if (input->src != cctx->expectedInBuffer.src) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stabilityCondition_notRespected)); + return unchecked( + (nuint)( + -(int)ZSTD_ErrorCode.ZSTD_error_stabilityCondition_notRespected + ) + ); } if (input->pos != cctx->expectedInBuffer.size) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_stabilityCondition_notRespected)); + return unchecked( + (nuint)( + -(int)ZSTD_ErrorCode.ZSTD_error_stabilityCondition_notRespected + ) + ); } } input->pos = input->size; cctx->expectedInBuffer = *input; cctx->stableIn_notConsumed += inputSize; - return (nuint)(cctx->requestedParams.format == ZSTD_format_e.ZSTD_f_zstd1 ? 6 : 2); + return (nuint)( + cctx->requestedParams.format == ZSTD_format_e.ZSTD_f_zstd1 ? 6 : 2 + ); } { @@ -6886,7 +9783,10 @@ namespace ZstdSharp.Unsafe flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp); cctx->consumedSrcSize += input->pos - ipos; cctx->producedCSize += output->pos - opos; - if (ERR_isError(flushMin) || endOp == ZSTD_EndDirective.ZSTD_e_end && flushMin == 0) + if ( + ERR_isError(flushMin) + || endOp == ZSTD_EndDirective.ZSTD_e_end && flushMin == 0 + ) { if (flushMin == 0) ZSTD_CCtx_trace(cctx, 0); @@ -6903,18 +9803,30 @@ namespace ZstdSharp.Unsafe if (endOp == ZSTD_EndDirective.ZSTD_e_continue) { - if (input->pos != ipos || output->pos != opos || input->pos == input->size || output->pos == output->size) + if ( + input->pos != ipos + || output->pos != opos + || input->pos == input->size + || output->pos == output->size + ) break; } else { - assert(endOp == ZSTD_EndDirective.ZSTD_e_flush || endOp == ZSTD_EndDirective.ZSTD_e_end); + assert( + endOp == ZSTD_EndDirective.ZSTD_e_flush + || endOp == ZSTD_EndDirective.ZSTD_e_end + ); if (flushMin == 0 || output->pos == output->size) break; } } - assert(endOp == ZSTD_EndDirective.ZSTD_e_continue || flushMin == 0 || output->pos == output->size); + assert( + endOp == ZSTD_EndDirective.ZSTD_e_continue + || flushMin == 0 + || output->pos == output->size + ); ZSTD_setBufferExpectations(cctx, output, input); return flushMin; } @@ -6937,7 +9849,16 @@ namespace ZstdSharp.Unsafe * 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) + 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; ZSTD_inBuffer_s input; @@ -6967,7 +9888,13 @@ namespace ZstdSharp.Unsafe * @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) + 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; @@ -6977,7 +9904,16 @@ namespace ZstdSharp.Unsafe { nuint oPos = 0; nuint iPos = 0; - nuint result = ZSTD_compressStream2_simpleArgs(cctx, dst, dstCapacity, &oPos, src, srcSize, &iPos, ZSTD_EndDirective.ZSTD_e_end); + 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; { @@ -7003,7 +9939,15 @@ namespace ZstdSharp.Unsafe * @offBase : must use the format required by ZSTD_storeSeq() * @returns a ZSTD error code if sequence is not valid */ - private static nuint ZSTD_validateSequence(uint offBase, uint matchLength, uint minMatch, nuint posInSrc, uint windowLog, nuint dictSize, int useSequenceProducer) + private static nuint ZSTD_validateSequence( + uint offBase, + uint matchLength, + uint minMatch, + nuint posInSrc, + uint windowLog, + nuint dictSize, + int useSequenceProducer + ) { uint windowSize = 1U << (int)windowLog; /* posInSrc represents the amount of data the decoder would decode up to this point. @@ -7017,13 +9961,17 @@ namespace ZstdSharp.Unsafe assert(offsetBound > 0); if (offBase > offsetBound + 3) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } } if (matchLength < matchLenLowerBound) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } return 0; @@ -7068,7 +10016,15 @@ namespace ZstdSharp.Unsafe * @blockSize must be == sum(sequence_lengths). * @returns @blockSize on success, and a ZSTD_error otherwise. */ - private static nuint ZSTD_transferSequences_wBlockDelim(ZSTD_CCtx_s* cctx, ZSTD_SequencePosition* seqPos, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* src, nuint blockSize, ZSTD_paramSwitch_e externalRepSearch) + private static nuint ZSTD_transferSequences_wBlockDelim( + ZSTD_CCtx_s* cctx, + ZSTD_SequencePosition* seqPos, + ZSTD_Sequence* inSeqs, + nuint inSeqsSize, + void* src, + nuint blockSize, + ZSTD_paramSwitch_e externalRepSearch + ) { uint idx = seqPos->idx; uint startIdx = idx; @@ -7090,7 +10046,11 @@ namespace ZstdSharp.Unsafe } memcpy(updatedRepcodes.rep, cctx->blockState.prevCBlock->rep, (uint)sizeof(repcodes_s)); - for (; idx < inSeqsSize && (inSeqs[idx].matchLength != 0 || inSeqs[idx].offset != 0); ++idx) + for ( + ; + idx < inSeqsSize && (inSeqs[idx].matchLength != 0 || inSeqs[idx].offset != 0); + ++idx + ) { uint litLength = inSeqs[idx].litLength; uint matchLength = inSeqs[idx].matchLength; @@ -7111,7 +10071,15 @@ namespace ZstdSharp.Unsafe { seqPos->posInSrc += litLength + matchLength; { - nuint err_code = ZSTD_validateSequence(offBase, matchLength, cctx->appliedParams.cParams.minMatch, seqPos->posInSrc, cctx->appliedParams.cParams.windowLog, dictSize, ZSTD_hasExtSeqProd(&cctx->appliedParams)); + nuint err_code = ZSTD_validateSequence( + offBase, + matchLength, + cctx->appliedParams.cParams.minMatch, + seqPos->posInSrc, + cctx->appliedParams.cParams.windowLog, + dictSize, + ZSTD_hasExtSeqProd(&cctx->appliedParams) + ); if (ERR_isError(err_code)) { return err_code; @@ -7121,7 +10089,9 @@ namespace ZstdSharp.Unsafe if (idx - seqPos->idx >= cctx->seqStore.maxNbSeq) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offBase, matchLength); @@ -7130,7 +10100,9 @@ namespace ZstdSharp.Unsafe if (idx == inSeqsSize) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } assert(externalRepSearch != ZSTD_paramSwitch_e.ZSTD_ps_auto); @@ -7171,7 +10143,9 @@ namespace ZstdSharp.Unsafe if (ip != iend) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } seqPos->idx = idx + 1; @@ -7189,7 +10163,15 @@ namespace ZstdSharp.Unsafe * @returns the number of bytes consumed from @src, necessarily <= @blockSize. * Otherwise, it may return a ZSTD error if something went wrong. */ - private static nuint ZSTD_transferSequences_noDelim(ZSTD_CCtx_s* cctx, ZSTD_SequencePosition* seqPos, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* src, nuint blockSize, ZSTD_paramSwitch_e externalRepSearch) + private static nuint ZSTD_transferSequences_noDelim( + ZSTD_CCtx_s* cctx, + ZSTD_SequencePosition* seqPos, + ZSTD_Sequence* inSeqs, + nuint inSeqsSize, + void* src, + nuint blockSize, + ZSTD_paramSwitch_e externalRepSearch + ) { uint idx = seqPos->idx; uint startPosInSequence = seqPos->posInSequence; @@ -7244,16 +10226,23 @@ namespace ZstdSharp.Unsafe if (endPosInSequence > litLength) { uint firstHalfMatchLength; - litLength = startPosInSequence >= litLength ? 0 : litLength - startPosInSequence; + litLength = + startPosInSequence >= litLength ? 0 : litLength - startPosInSequence; firstHalfMatchLength = endPosInSequence - startPosInSequence - litLength; - if (matchLength > blockSize && firstHalfMatchLength >= cctx->appliedParams.cParams.minMatch) + if ( + matchLength > blockSize + && firstHalfMatchLength >= cctx->appliedParams.cParams.minMatch + ) { /* Only ever split the match if it is larger than the block size */ - uint secondHalfMatchLength = currSeq.matchLength + currSeq.litLength - endPosInSequence; + 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; + endPosInSequence -= + cctx->appliedParams.cParams.minMatch - secondHalfMatchLength; + bytesAdjustment = + cctx->appliedParams.cParams.minMatch - secondHalfMatchLength; firstHalfMatchLength -= bytesAdjustment; } @@ -7283,7 +10272,15 @@ namespace ZstdSharp.Unsafe { seqPos->posInSrc += litLength + matchLength; { - nuint err_code = ZSTD_validateSequence(offBase, matchLength, cctx->appliedParams.cParams.minMatch, seqPos->posInSrc, cctx->appliedParams.cParams.windowLog, dictSize, ZSTD_hasExtSeqProd(&cctx->appliedParams)); + nuint err_code = ZSTD_validateSequence( + offBase, + matchLength, + cctx->appliedParams.cParams.minMatch, + seqPos->posInSrc, + cctx->appliedParams.cParams.windowLog, + dictSize, + ZSTD_hasExtSeqProd(&cctx->appliedParams) + ); if (ERR_isError(err_code)) { return err_code; @@ -7293,7 +10290,9 @@ namespace ZstdSharp.Unsafe if (idx - seqPos->idx >= cctx->seqStore.maxNbSeq) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offBase, matchLength); @@ -7302,7 +10301,10 @@ namespace ZstdSharp.Unsafe idx++; } - assert(idx == inSeqsSize || endPosInSequence <= inSeqs[idx].litLength + inSeqs[idx].matchLength); + assert( + idx == inSeqsSize + || endPosInSequence <= inSeqs[idx].litLength + inSeqs[idx].matchLength + ); seqPos->idx = idx; seqPos->posInSequence = endPosInSequence; memcpy(cctx->blockState.nextCBlock->rep, updatedRepcodes.rep, (uint)sizeof(repcodes_s)); @@ -7321,21 +10323,43 @@ namespace ZstdSharp.Unsafe private static void* ZSTD_selectSequenceCopier(ZSTD_sequenceFormat_e mode) { - assert(ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam11, (int)mode) != 0); + assert( + ZSTD_cParam_withinBounds(ZSTD_cParameter.ZSTD_c_experimentalParam11, (int)mode) != 0 + ); if (mode == ZSTD_sequenceFormat_e.ZSTD_sf_explicitBlockDelimiters) { - return (delegate* managed)(&ZSTD_transferSequences_wBlockDelim); + return (delegate* managed< + ZSTD_CCtx_s*, + ZSTD_SequencePosition*, + ZSTD_Sequence*, + nuint, + void*, + nuint, + ZSTD_paramSwitch_e, + nuint>)(&ZSTD_transferSequences_wBlockDelim); } assert(mode == ZSTD_sequenceFormat_e.ZSTD_sf_noBlockDelimiters); - return (delegate* managed)(&ZSTD_transferSequences_noDelim); + return (delegate* managed< + ZSTD_CCtx_s*, + ZSTD_SequencePosition*, + ZSTD_Sequence*, + nuint, + void*, + nuint, + ZSTD_paramSwitch_e, + nuint>)(&ZSTD_transferSequences_noDelim); } /* Discover the size of next block by searching for the delimiter. * Note that a block delimiter **must** exist in this mode, * otherwise it's an input error. * The block size retrieved will be later compared to ensure it remains within bounds */ - private static nuint blockSize_explicitDelimiter(ZSTD_Sequence* inSeqs, nuint inSeqsSize, ZSTD_SequencePosition seqPos) + private static nuint blockSize_explicitDelimiter( + ZSTD_Sequence* inSeqs, + nuint inSeqsSize, + ZSTD_SequencePosition seqPos + ) { int end = 0; nuint blockSize = 0; @@ -7348,7 +10372,9 @@ namespace ZstdSharp.Unsafe if (end != 0) { if (inSeqs[spos].matchLength != 0) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); break; } @@ -7356,11 +10382,20 @@ namespace ZstdSharp.Unsafe } if (end == 0) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); return blockSize; } - private static nuint determine_blockSize(ZSTD_sequenceFormat_e mode, nuint blockSize, nuint remaining, ZSTD_Sequence* inSeqs, nuint inSeqsSize, ZSTD_SequencePosition seqPos) + private static nuint determine_blockSize( + ZSTD_sequenceFormat_e mode, + nuint blockSize, + nuint remaining, + ZSTD_Sequence* inSeqs, + nuint inSeqsSize, + ZSTD_SequencePosition seqPos + ) { if (mode == ZSTD_sequenceFormat_e.ZSTD_sf_noBlockDelimiters) { @@ -7379,9 +10414,13 @@ namespace ZstdSharp.Unsafe } if (explicitBlockSize > blockSize) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); if (explicitBlockSize > remaining) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); return explicitBlockSize; } } @@ -7391,7 +10430,15 @@ namespace ZstdSharp.Unsafe * 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) + 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; nuint remaining = srcSize; @@ -7399,7 +10446,7 @@ namespace ZstdSharp.Unsafe { idx = 0, posInSequence = 0, - posInSrc = 0 + posInSrc = 0, }; byte* ip = (byte*)src; byte* op = (byte*)dst; @@ -7423,7 +10470,14 @@ namespace ZstdSharp.Unsafe { nuint compressedSeqsSize; nuint cBlockSize; - nuint blockSize = determine_blockSize(cctx->appliedParams.blockDelimiters, cctx->blockSizeMax, remaining, inSeqs, inSeqsSize, seqPos); + nuint blockSize = determine_blockSize( + cctx->appliedParams.blockDelimiters, + cctx->blockSizeMax, + remaining, + inSeqs, + inSeqsSize, + seqPos + ); uint lastBlock = blockSize == remaining ? 1U : 0U; { nuint err_code = blockSize; @@ -7435,7 +10489,25 @@ namespace ZstdSharp.Unsafe assert(blockSize <= remaining); ZSTD_resetSeqStore(&cctx->seqStore); - blockSize = ((delegate* managed)sequenceCopier)(cctx, &seqPos, inSeqs, inSeqsSize, ip, blockSize, cctx->appliedParams.searchForExternalRepcodes); + blockSize = ( + (delegate* managed< + ZSTD_CCtx_s*, + ZSTD_SequencePosition*, + ZSTD_Sequence*, + nuint, + void*, + nuint, + ZSTD_paramSwitch_e, + nuint>)sequenceCopier + )( + cctx, + &seqPos, + inSeqs, + inSeqsSize, + ip, + blockSize, + cctx->appliedParams.searchForExternalRepcodes + ); { nuint err_code = blockSize; if (ERR_isError(err_code)) @@ -7468,7 +10540,18 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); } - compressedSeqsSize = ZSTD_entropyCompressSeqStore(&cctx->seqStore, &cctx->blockState.prevCBlock->entropy, &cctx->blockState.nextCBlock->entropy, &cctx->appliedParams, op + ZSTD_blockHeaderSize, dstCapacity - ZSTD_blockHeaderSize, blockSize, cctx->tmpWorkspace, cctx->tmpWkspSize, cctx->bmi2); + compressedSeqsSize = ZSTD_entropyCompressSeqStore( + &cctx->seqStore, + &cctx->blockState.prevCBlock->entropy, + &cctx->blockState.nextCBlock->entropy, + &cctx->appliedParams, + op + ZSTD_blockHeaderSize, + dstCapacity - ZSTD_blockHeaderSize, + blockSize, + cctx->tmpWorkspace, + cctx->tmpWkspSize, + cctx->bmi2 + ); { nuint err_code = compressedSeqsSize; if (ERR_isError(err_code)) @@ -7477,7 +10560,11 @@ namespace ZstdSharp.Unsafe } } - if (cctx->isFirstBlock == 0 && ZSTD_maybeRLE(&cctx->seqStore) != 0 && ZSTD_isRLE(ip, blockSize) != 0) + if ( + cctx->isFirstBlock == 0 + && ZSTD_maybeRLE(&cctx->seqStore) != 0 + && ZSTD_isRLE(ip, blockSize) != 0 + ) { compressedSeqsSize = 1; } @@ -7508,9 +10595,16 @@ namespace ZstdSharp.Unsafe { 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); + 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(op, cBlockHeader); cBlockSize = ZSTD_blockHeaderSize + compressedSeqsSize; } @@ -7569,13 +10663,25 @@ namespace ZstdSharp.Unsafe * and cannot emit an RLE block that disagrees with the repcode history. * @return : final compressed size, or a ZSTD error code. */ - public static nuint ZSTD_compressSequences(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* src, nuint srcSize) + 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; assert(cctx != null); { - nuint err_code = ZSTD_CCtx_init_compressStream2(cctx, ZSTD_EndDirective.ZSTD_e_end, srcSize); + nuint err_code = ZSTD_CCtx_init_compressStream2( + cctx, + ZSTD_EndDirective.ZSTD_e_end, + srcSize + ); if (ERR_isError(err_code)) { return err_code; @@ -7583,7 +10689,13 @@ namespace ZstdSharp.Unsafe } { - nuint frameHeaderSize = ZSTD_writeFrameHeader(op, dstCapacity, &cctx->appliedParams, srcSize, cctx->dictID); + nuint frameHeaderSize = ZSTD_writeFrameHeader( + op, + dstCapacity, + &cctx->appliedParams, + srcSize, + cctx->dictID + ); op += frameHeaderSize; assert(frameHeaderSize <= dstCapacity); dstCapacity -= frameHeaderSize; @@ -7596,7 +10708,15 @@ namespace ZstdSharp.Unsafe } { - nuint cBlocksSize = ZSTD_compressSequences_internal(cctx, op, dstCapacity, inSeqs, inSeqsSize, src, srcSize); + nuint cBlocksSize = ZSTD_compressSequences_internal( + cctx, + op, + dstCapacity, + inSeqs, + inSeqsSize, + src, + srcSize + ); { nuint err_code = cBlocksSize; if (ERR_isError(err_code)) @@ -7625,7 +10745,11 @@ namespace ZstdSharp.Unsafe return cSize; } - private static nuint convertSequences_noRepcodes(SeqDef_s* dstSeqs, ZSTD_Sequence* inSeqs, nuint nbSequences) + private static nuint convertSequences_noRepcodes( + SeqDef_s* dstSeqs, + ZSTD_Sequence* inSeqs, + nuint nbSequences + ) { nuint longLen = 0; nuint n; @@ -7658,13 +10782,20 @@ namespace ZstdSharp.Unsafe * This is helpful to generate a lean main pipeline, improving performance. * It may be re-inserted later. */ - private static nuint ZSTD_convertBlockSequences(ZSTD_CCtx_s* cctx, ZSTD_Sequence* inSeqs, nuint nbSequences, int repcodeResolution) + private static nuint ZSTD_convertBlockSequences( + ZSTD_CCtx_s* cctx, + ZSTD_Sequence* inSeqs, + nuint nbSequences, + int repcodeResolution + ) { repcodes_s updatedRepcodes; nuint seqNb = 0; if (nbSequences >= cctx->seqStore.maxNbSeq) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } memcpy(updatedRepcodes.rep, cctx->blockState.prevCBlock->rep, (uint)sizeof(repcodes_s)); @@ -7673,7 +10804,11 @@ namespace ZstdSharp.Unsafe assert(inSeqs[nbSequences - 1].offset == 0); if (repcodeResolution == 0) { - nuint longl = convertSequences_noRepcodes(cctx->seqStore.sequencesStart, inSeqs, nbSequences - 1); + nuint longl = convertSequences_noRepcodes( + cctx->seqStore.sequencesStart, + inSeqs, + nbSequences - 1 + ); cctx->seqStore.sequences = cctx->seqStore.sequencesStart + nbSequences - 1; if (longl != 0) { @@ -7686,7 +10821,8 @@ namespace ZstdSharp.Unsafe else { assert(longl <= 2 * (nbSequences - 1)); - cctx->seqStore.longLengthType = ZSTD_longLengthType_e.ZSTD_llt_literalLength; + cctx->seqStore.longLengthType = + ZSTD_longLengthType_e.ZSTD_llt_literalLength; cctx->seqStore.longLengthPos = (uint)(longl - (nbSequences - 1) - 1); } } @@ -7698,7 +10834,11 @@ namespace ZstdSharp.Unsafe uint litLength = inSeqs[seqNb].litLength; uint matchLength = inSeqs[seqNb].matchLength; uint ll0 = litLength == 0 ? 1U : 0U; - uint offBase = ZSTD_finalizeOffBase(inSeqs[seqNb].offset, updatedRepcodes.rep, ll0); + uint offBase = ZSTD_finalizeOffBase( + inSeqs[seqNb].offset, + updatedRepcodes.rep, + ll0 + ); ZSTD_storeSeqOnly(&cctx->seqStore, litLength, offBase, matchLength); ZSTD_updateRep(updatedRepcodes.rep, offBase, ll0); } @@ -7755,7 +10895,9 @@ namespace ZstdSharp.Unsafe { BlockSummary bs; System.Runtime.CompilerServices.Unsafe.SkipInit(out bs); - bs.nbSequences = unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + bs.nbSequences = unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); return bs; } @@ -7768,16 +10910,32 @@ namespace ZstdSharp.Unsafe } } - private static nuint ZSTD_compressSequencesAndLiterals_internal(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, ZSTD_Sequence* inSeqs, nuint nbSequences, void* literals, nuint litSize, nuint srcSize) + private static nuint ZSTD_compressSequencesAndLiterals_internal( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + ZSTD_Sequence* inSeqs, + nuint nbSequences, + void* literals, + nuint litSize, + nuint srcSize + ) { nuint remaining = srcSize; nuint cSize = 0; byte* op = (byte*)dst; - int repcodeResolution = cctx->appliedParams.searchForExternalRepcodes == ZSTD_paramSwitch_e.ZSTD_ps_enable ? 1 : 0; - assert(cctx->appliedParams.searchForExternalRepcodes != ZSTD_paramSwitch_e.ZSTD_ps_auto); + int repcodeResolution = + cctx->appliedParams.searchForExternalRepcodes == ZSTD_paramSwitch_e.ZSTD_ps_enable + ? 1 + : 0; + assert( + cctx->appliedParams.searchForExternalRepcodes != ZSTD_paramSwitch_e.ZSTD_ps_auto + ); if (nbSequences == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } if (nbSequences == 1 && inSeqs[0].litLength == 0) @@ -7797,7 +10955,9 @@ namespace ZstdSharp.Unsafe while (nbSequences != 0) { - nuint compressedSeqsSize, cBlockSize, conversionStatus; + nuint compressedSeqsSize, + cBlockSize, + conversionStatus; BlockSummary block = ZSTD_get1BlockSummary(inSeqs, nbSequences); uint lastBlock = block.nbSequences == nbSequences ? 1U : 0U; { @@ -7811,11 +10971,18 @@ namespace ZstdSharp.Unsafe assert(block.nbSequences <= nbSequences); if (block.litSize > litSize) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } ZSTD_resetSeqStore(&cctx->seqStore); - conversionStatus = ZSTD_convertBlockSequences(cctx, inSeqs, block.nbSequences, repcodeResolution); + conversionStatus = ZSTD_convertBlockSequences( + cctx, + inSeqs, + block.nbSequences, + repcodeResolution + ); { nuint err_code = conversionStatus; if (ERR_isError(err_code)) @@ -7832,7 +10999,19 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); } - compressedSeqsSize = ZSTD_entropyCompressSeqStore_internal(op + ZSTD_blockHeaderSize, dstCapacity - ZSTD_blockHeaderSize, literals, block.litSize, &cctx->seqStore, &cctx->blockState.prevCBlock->entropy, &cctx->blockState.nextCBlock->entropy, &cctx->appliedParams, cctx->tmpWorkspace, cctx->tmpWkspSize, cctx->bmi2); + compressedSeqsSize = ZSTD_entropyCompressSeqStore_internal( + op + ZSTD_blockHeaderSize, + dstCapacity - ZSTD_blockHeaderSize, + literals, + block.litSize, + &cctx->seqStore, + &cctx->blockState.prevCBlock->entropy, + &cctx->blockState.nextCBlock->entropy, + &cctx->appliedParams, + cctx->tmpWorkspace, + cctx->tmpWkspSize, + cctx->bmi2 + ); { nuint err_code = compressedSeqsSize; if (ERR_isError(err_code)) @@ -7847,16 +11026,25 @@ namespace ZstdSharp.Unsafe literals = (sbyte*)literals + block.litSize; if (compressedSeqsSize == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_cannotProduce_uncompressedBlock)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_cannotProduce_uncompressedBlock) + ); } else { uint cBlockHeader; assert(compressedSeqsSize > 1); 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); + 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(op, cBlockHeader); cBlockSize = ZSTD_blockHeaderSize + compressedSeqsSize; } @@ -7874,12 +11062,16 @@ namespace ZstdSharp.Unsafe if (litSize != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } if (remaining != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_externalSequences_invalid) + ); } return cSize; @@ -7903,7 +11095,17 @@ namespace ZstdSharp.Unsafe * - @decompressedSize must be correct, and correspond to the sum of all Sequences. Any discrepancy will generate an error. * @return : final compressed size, or a ZSTD error code. */ - public static nuint ZSTD_compressSequencesAndLiterals(ZSTD_CCtx_s* cctx, void* dst, nuint dstCapacity, ZSTD_Sequence* inSeqs, nuint inSeqsSize, void* literals, nuint litSize, nuint litCapacity, nuint decompressedSize) + public static nuint ZSTD_compressSequencesAndLiterals( + ZSTD_CCtx_s* cctx, + void* dst, + nuint dstCapacity, + ZSTD_Sequence* inSeqs, + nuint inSeqsSize, + void* literals, + nuint litSize, + nuint litCapacity, + nuint decompressedSize + ) { byte* op = (byte*)dst; nuint cSize = 0; @@ -7914,16 +11116,25 @@ namespace ZstdSharp.Unsafe } { - nuint err_code = ZSTD_CCtx_init_compressStream2(cctx, ZSTD_EndDirective.ZSTD_e_end, decompressedSize); + nuint err_code = ZSTD_CCtx_init_compressStream2( + cctx, + ZSTD_EndDirective.ZSTD_e_end, + decompressedSize + ); if (ERR_isError(err_code)) { return err_code; } } - if (cctx->appliedParams.blockDelimiters == ZSTD_sequenceFormat_e.ZSTD_sf_noBlockDelimiters) + if ( + cctx->appliedParams.blockDelimiters + == ZSTD_sequenceFormat_e.ZSTD_sf_noBlockDelimiters + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported) + ); } if (cctx->appliedParams.validateSequences != 0) @@ -7933,11 +11144,19 @@ namespace ZstdSharp.Unsafe if (cctx->appliedParams.fParams.checksumFlag != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported) + ); } { - nuint frameHeaderSize = ZSTD_writeFrameHeader(op, dstCapacity, &cctx->appliedParams, decompressedSize, cctx->dictID); + nuint frameHeaderSize = ZSTD_writeFrameHeader( + op, + dstCapacity, + &cctx->appliedParams, + decompressedSize, + cctx->dictID + ); op += frameHeaderSize; assert(frameHeaderSize <= dstCapacity); dstCapacity -= frameHeaderSize; @@ -7945,7 +11164,16 @@ namespace ZstdSharp.Unsafe } { - nuint cBlocksSize = ZSTD_compressSequencesAndLiterals_internal(cctx, op, dstCapacity, inSeqs, inSeqsSize, literals, litSize, decompressedSize); + nuint cBlocksSize = ZSTD_compressSequencesAndLiterals_internal( + cctx, + op, + dstCapacity, + inSeqs, + inSeqsSize, + literals, + litSize, + decompressedSize + ); { nuint err_code = cBlocksSize; if (ERR_isError(err_code)) @@ -7969,9 +11197,10 @@ namespace ZstdSharp.Unsafe { src = null, size = 0, - pos = 0 + pos = 0, }; - int stableInput = zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable ? 1 : 0; + int stableInput = + zcs->appliedParams.inBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable ? 1 : 0; return stableInput != 0 ? zcs->expectedInBuffer : nullInput; } @@ -7988,7 +11217,12 @@ namespace ZstdSharp.Unsafe public static nuint ZSTD_endStream(ZSTD_CCtx_s* zcs, ZSTD_outBuffer_s* output) { ZSTD_inBuffer_s input = inBuffer_forEndFlush(zcs); - nuint remainingToFlush = ZSTD_compressStream2(zcs, output, &input, ZSTD_EndDirective.ZSTD_e_end); + nuint remainingToFlush = ZSTD_compressStream2( + zcs, + output, + &input, + ZSTD_EndDirective.ZSTD_e_end + ); { nuint err_code = remainingToFlush; if (ERR_isError(err_code)) @@ -8001,7 +11235,9 @@ namespace ZstdSharp.Unsafe return remainingToFlush; { nuint lastBlockSize = (nuint)(zcs->frameEnded != 0 ? 0 : 3); - nuint checksumSize = (nuint)(zcs->frameEnded != 0 ? 0 : zcs->appliedParams.fParams.checksumFlag * 4); + nuint checksumSize = (nuint)( + zcs->frameEnded != 0 ? 0 : zcs->appliedParams.fParams.checksumFlag * 4 + ); nuint toFlush = remainingToFlush + lastBlockSize + checksumSize; return toFlush; } @@ -8022,9 +11258,17 @@ namespace ZstdSharp.Unsafe return 3; } - private static ZSTD_compressionParameters ZSTD_dedicatedDictSearch_getCParams(int compressionLevel, nuint dictSize) + 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); + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal( + compressionLevel, + 0, + dictSize, + ZSTD_CParamMode_e.ZSTD_cpm_createCDict + ); switch (cParams.strategy) { case ZSTD_strategy.ZSTD_fast: @@ -8047,7 +11291,13 @@ namespace ZstdSharp.Unsafe 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; + return + cParams->strategy >= ZSTD_strategy.ZSTD_greedy + && cParams->strategy <= ZSTD_strategy.ZSTD_lazy2 + && cParams->hashLog > cParams->chainLog + && cParams->chainLog <= 24 + ? 1 + : 0; } /** @@ -8055,7 +11305,9 @@ namespace ZstdSharp.Unsafe * 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) + private static void ZSTD_dedicatedDictSearch_revertCParams( + ZSTD_compressionParameters* cParams + ) { switch (cParams->strategy) { @@ -8080,7 +11332,11 @@ namespace ZstdSharp.Unsafe } } - private static ulong ZSTD_getCParamRowSize(ulong srcSizeHint, nuint dictSize, ZSTD_CParamMode_e mode) + private static ulong ZSTD_getCParamRowSize( + ulong srcSizeHint, + nuint dictSize, + ZSTD_CParamMode_e mode + ) { switch (mode) { @@ -8099,7 +11355,9 @@ namespace ZstdSharp.Unsafe { int unknown = srcSizeHint == unchecked(0UL - 1) ? 1 : 0; nuint addedSize = (nuint)(unknown != 0 && dictSize > 0 ? 500 : 0); - return unknown != 0 && dictSize == 0 ? unchecked(0UL - 1) : srcSizeHint + dictSize + addedSize; + return unknown != 0 && dictSize == 0 + ? unchecked(0UL - 1) + : srcSizeHint + dictSize + addedSize; } } @@ -8108,10 +11366,19 @@ namespace ZstdSharp.Unsafe * 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) + 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 <= 256 * (1 << 10) ? 1 : 0) + (rSize <= 128 * (1 << 10) ? 1 : 0) + (rSize <= 16 * (1 << 10) ? 1 : 0)); + uint tableID = (uint)( + (rSize <= 256 * (1 << 10) ? 1 : 0) + + (rSize <= 128 * (1 << 10) ? 1 : 0) + + (rSize <= 16 * (1 << 10) ? 1 : 0) + ); int row; if (compressionLevel == 0) row = 3; @@ -8125,36 +11392,59 @@ namespace ZstdSharp.Unsafe ZSTD_compressionParameters cp = ZSTD_defaultCParameters[tableID][row]; if (compressionLevel < 0) { - int clampedCompressionLevel = ZSTD_minCLevel() > compressionLevel ? ZSTD_minCLevel() : compressionLevel; + int clampedCompressionLevel = + ZSTD_minCLevel() > compressionLevel ? ZSTD_minCLevel() : compressionLevel; cp.targetLength = (uint)-clampedCompressionLevel; } - return ZSTD_adjustCParams_internal(cp, srcSizeHint, dictSize, mode, ZSTD_paramSwitch_e.ZSTD_ps_auto); + return ZSTD_adjustCParams_internal( + cp, + srcSizeHint, + dictSize, + mode, + ZSTD_paramSwitch_e.ZSTD_ps_auto + ); } } /*! 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) + 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); + 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) + 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); - @params = new ZSTD_parameters - { - cParams = cParams - }; + ZSTD_compressionParameters cParams = ZSTD_getCParams_internal( + compressionLevel, + srcSizeHint, + dictSize, + mode + ); + @params = new ZSTD_parameters { cParams = cParams }; @params.fParams.contentSizeFlag = 1; return @params; } @@ -8163,11 +11453,20 @@ namespace ZstdSharp.Unsafe * 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) + 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); + return ZSTD_getParams_internal( + compressionLevel, + srcSizeHint, + dictSize, + ZSTD_CParamMode_e.ZSTD_cpm_unknown + ); } /*! ZSTD_registerSequenceProducer() : @@ -8189,10 +11488,18 @@ namespace ZstdSharp.Unsafe * * The user is strongly encouraged to read the full API documentation (above) before * calling this function. */ - public static void ZSTD_registerSequenceProducer(ZSTD_CCtx_s* zc, void* extSeqProdState, void* extSeqProdFunc) + public static void ZSTD_registerSequenceProducer( + ZSTD_CCtx_s* zc, + void* extSeqProdState, + void* extSeqProdFunc + ) { assert(zc != null); - ZSTD_CCtxParams_registerSequenceProducer(&zc->requestedParams, extSeqProdState, extSeqProdFunc); + ZSTD_CCtxParams_registerSequenceProducer( + &zc->requestedParams, + extSeqProdState, + extSeqProdFunc + ); } /*! ZSTD_CCtxParams_registerSequenceProducer() : @@ -8204,7 +11511,11 @@ namespace ZstdSharp.Unsafe * is required, then this function is for you. Otherwise, you probably don't need it. * * See tests/zstreamtest.c for example usage. */ - public static void ZSTD_CCtxParams_registerSequenceProducer(ZSTD_CCtx_params_s* @params, void* extSeqProdState, void* extSeqProdFunc) + public static void ZSTD_CCtxParams_registerSequenceProducer( + ZSTD_CCtx_params_s* @params, + void* extSeqProdState, + void* extSeqProdFunc + ) { assert(@params != null); if (extSeqProdFunc != null) diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressInternal.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressInternal.cs index 03da4907..c3026112 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressInternal.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressInternal.cs @@ -1,7 +1,7 @@ -using System.Runtime.CompilerServices; -using static ZstdSharp.UnsafeHelper; using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -12,7 +12,10 @@ namespace ZstdSharp.Unsafe * 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) + private static ZSTD_SequenceLength ZSTD_getSequenceLength( + SeqStore_t* seqStore, + SeqDef_s* seq + ) { ZSTD_SequenceLength seqLen; seqLen.litLength = seq->litLength; @@ -33,80 +36,160 @@ namespace ZstdSharp.Unsafe return seqLen; } - private static readonly RawSeqStore_t kNullRawSeqStore = new RawSeqStore_t(seq: null, pos: 0, posInSequence: 0, size: 0, capacity: 0); + private static readonly RawSeqStore_t kNullRawSeqStore = new RawSeqStore_t( + seq: null, + pos: 0, + posInSequence: 0, + size: 0, + capacity: 0 + ); #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_LL_Code => new byte[64] - { - 0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 16, - 17, - 17, - 18, - 18, - 19, - 19, - 20, - 20, - 20, - 20, - 21, - 21, - 21, - 21, - 22, - 22, - 22, - 22, - 22, - 22, - 22, - 22, - 23, - 23, - 23, - 23, - 23, - 23, - 23, - 23, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24, - 24 - }; - private static byte* LL_Code => (byte*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_LL_Code)); + private static ReadOnlySpan Span_LL_Code => + new byte[64] + { + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 16, + 17, + 17, + 18, + 18, + 19, + 19, + 20, + 20, + 20, + 20, + 21, + 21, + 21, + 21, + 22, + 22, + 22, + 22, + 22, + 22, + 22, + 22, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + }; + private static byte* LL_Code => + (byte*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_LL_Code) + ); #else - private static readonly 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 }); + private static readonly 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, + } + ); #endif + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ZSTD_LLcode(uint litLength) { @@ -115,141 +198,278 @@ namespace ZstdSharp.Unsafe } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_ML_Code => new byte[128] - { - 0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - 21, - 22, - 23, - 24, - 25, - 26, - 27, - 28, - 29, - 30, - 31, - 32, - 32, - 33, - 33, - 34, - 34, - 35, - 35, - 36, - 36, - 36, - 36, - 37, - 37, - 37, - 37, - 38, - 38, - 38, - 38, - 38, - 38, - 38, - 38, - 39, - 39, - 39, - 39, - 39, - 39, - 39, - 39, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 40, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 41, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42, - 42 - }; - private static byte* ML_Code => (byte*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_ML_Code)); + private static ReadOnlySpan Span_ML_Code => + new byte[128] + { + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 32, + 33, + 33, + 34, + 34, + 35, + 35, + 36, + 36, + 36, + 36, + 37, + 37, + 37, + 37, + 38, + 38, + 38, + 38, + 38, + 38, + 38, + 38, + 39, + 39, + 39, + 39, + 39, + 39, + 39, + 39, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 40, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 41, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + 42, + }; + private static byte* ML_Code => + (byte*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_ML_Code) + ); #else - private static readonly 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 }); + private static readonly 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, + } + ); #endif /* ZSTD_MLcode() : * note : mlBase = matchLength - MINMATCH; @@ -281,7 +501,12 @@ namespace ZstdSharp.Unsafe * @return index >= lowLimit ? candidate : backup, * tries to force branchless codegen. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static byte* ZSTD_selectAddr(uint index, uint lowLimit, byte* candidate, byte* backup) + private static byte* ZSTD_selectAddr( + uint index, + uint lowLimit, + byte* candidate, + byte* backup + ) { return index >= lowLimit ? candidate : backup; } @@ -290,9 +515,16 @@ namespace ZstdSharp.Unsafe * 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) + 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); + 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)); @@ -304,7 +536,13 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_rleCompressBlock(void* dst, nuint dstCapacity, byte src, nuint srcSize, uint lastBlock) + 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); @@ -343,7 +581,11 @@ namespace ZstdSharp.Unsafe assert(0 != 0); goto case ZSTD_paramSwitch_e.ZSTD_ps_auto; case ZSTD_paramSwitch_e.ZSTD_ps_auto: - return cctxParams->cParams.strategy == ZSTD_strategy.ZSTD_fast && cctxParams->cParams.targetLength > 0 ? 1 : 0; + return + cctxParams->cParams.strategy == ZSTD_strategy.ZSTD_fast + && cctxParams->cParams.targetLength > 0 + ? 1 + : 0; } } @@ -373,15 +615,25 @@ namespace ZstdSharp.Unsafe * @matchLength : must be >= MINMATCH */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_storeSeqOnly(SeqStore_t* seqStorePtr, nuint litLength, uint offBase, nuint matchLength) + private static void ZSTD_storeSeqOnly( + SeqStore_t* seqStorePtr, + nuint litLength, + uint offBase, + nuint matchLength + ) { - assert((nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq); + assert( + (nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart) + < seqStorePtr->maxNbSeq + ); assert(litLength <= 1 << 17); 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->longLengthPos = (uint)( + seqStorePtr->sequences - seqStorePtr->sequencesStart + ); } seqStorePtr->sequences[0].litLength = (ushort)litLength; @@ -394,7 +646,9 @@ namespace ZstdSharp.Unsafe { 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->longLengthPos = (uint)( + seqStorePtr->sequences - seqStorePtr->sequencesStart + ); } seqStorePtr->sequences[0].mlBase = (ushort)mlBase; @@ -410,11 +664,21 @@ namespace ZstdSharp.Unsafe * Allowed to over-read literals up to litLimit. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_storeSeq(SeqStore_t* seqStorePtr, nuint litLength, byte* literals, byte* litLimit, uint offBase, nuint matchLength) + private static void ZSTD_storeSeq( + SeqStore_t* seqStorePtr, + nuint litLength, + byte* literals, + byte* litLimit, + uint offBase, + nuint matchLength + ) { byte* litLimit_w = litLimit - 32; byte* litEnd = literals + litLength; - assert((nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart) < seqStorePtr->maxNbSeq); + assert( + (nuint)(seqStorePtr->sequences - seqStorePtr->sequencesStart) + < seqStorePtr->maxNbSeq + ); assert(seqStorePtr->maxNbLit <= 128 * (1 << 10)); assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + seqStorePtr->maxNbLit); assert(literals + litLength <= litLimit); @@ -423,7 +687,12 @@ namespace ZstdSharp.Unsafe ZSTD_copy16(seqStorePtr->lit, literals); if (litLength > 16) { - ZSTD_wildcopy(seqStorePtr->lit + 16, literals + 16, (nint)litLength - 16, ZSTD_overlap_e.ZSTD_no_overlap); + ZSTD_wildcopy( + seqStorePtr->lit + 16, + literals + 16, + (nint)litLength - 16, + ZSTD_overlap_e.ZSTD_no_overlap + ); } } else @@ -527,7 +796,13 @@ namespace ZstdSharp.Unsafe * 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) + 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); @@ -537,6 +812,7 @@ namespace ZstdSharp.Unsafe } private const uint prime3bytes = 506832829U; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ZSTD_hash3(uint u, uint h, uint s) { @@ -557,6 +833,7 @@ namespace ZstdSharp.Unsafe } private const uint prime4bytes = 2654435761U; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ZSTD_hash4(uint u, uint h, uint s) { @@ -577,6 +854,7 @@ namespace ZstdSharp.Unsafe } private const ulong prime5bytes = 889523592379UL; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint ZSTD_hash5(ulong u, uint h, ulong s) { @@ -597,6 +875,7 @@ namespace ZstdSharp.Unsafe } private const ulong prime6bytes = 227718039650203UL; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint ZSTD_hash6(ulong u, uint h, ulong s) { @@ -617,6 +896,7 @@ namespace ZstdSharp.Unsafe } private const ulong prime7bytes = 58295818150454627UL; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint ZSTD_hash7(ulong u, uint h, ulong s) { @@ -637,6 +917,7 @@ namespace ZstdSharp.Unsafe } private const ulong prime8bytes = 0xCF1BBCDCB7A56463UL; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static nuint ZSTD_hash8(ulong u, uint h, ulong s) { @@ -742,7 +1023,12 @@ namespace ZstdSharp.Unsafe * Rotate the rolling hash by one byte. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ulong ZSTD_rollingHash_rotate(ulong hash, byte toRemove, byte toAdd, ulong primePower) + private static ulong ZSTD_rollingHash_rotate( + ulong hash, + byte toRemove, + byte toAdd, + ulong primePower + ) { hash -= (ulong)(toRemove + 10) * primePower; hash *= prime8bytes; @@ -766,7 +1052,10 @@ namespace ZstdSharp.Unsafe [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint ZSTD_window_isEmpty(ZSTD_window_t window) { - return window.dictLimit == 2 && window.lowLimit == 2 && window.nextSrc - window.@base == 2 ? 1U : 0U; + return + window.dictLimit == 2 && window.lowLimit == 2 && window.nextSrc - window.@base == 2 + ? 1U + : 0U; } /** @@ -787,7 +1076,12 @@ namespace ZstdSharp.Unsafe [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; + 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; } /** @@ -796,18 +1090,28 @@ namespace ZstdSharp.Unsafe * 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) + 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) + 2; + uint minIndexToOverflowCorrect = + cycleSize + (maxDist > cycleSize ? maxDist : cycleSize) + 2; /* Adjust the min index to backoff the overflow correction frequency, * so we don't waste too much CPU in overflow correction. If this * computation overflows we don't really care, we just need to make * sure it is at least minIndexToOverflowCorrect. */ uint adjustment = window.nbOverflowCorrections + 1; - uint adjustedIndex = minIndexToOverflowCorrect * adjustment > minIndexToOverflowCorrect ? minIndexToOverflowCorrect * adjustment : minIndexToOverflowCorrect; + uint adjustedIndex = + minIndexToOverflowCorrect * adjustment > minIndexToOverflowCorrect + ? minIndexToOverflowCorrect * adjustment + : minIndexToOverflowCorrect; uint indexLargeEnough = curr > adjustedIndex ? 1U : 0U; /* Only overflow correct early if the dictionary is invalidated already, * so we don't hurt compression ratio. @@ -822,7 +1126,14 @@ namespace ZstdSharp.Unsafe * protection. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint ZSTD_window_needOverflowCorrection(ZSTD_window_t window, uint cycleLog, uint maxDist, uint loadedDictEnd, void* src, void* srcEnd) + 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 > (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20)) ? 1U : 0U; @@ -838,7 +1149,12 @@ namespace ZstdSharp.Unsafe * 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) + private static uint ZSTD_window_correctOverflow( + ZSTD_window_t* window, + uint cycleLog, + uint maxDist, + void* src + ) { /* preemptive overflow correction: * 1. correction is large enough: @@ -864,8 +1180,14 @@ namespace ZstdSharp.Unsafe uint curr = (uint)((byte*)src - window->@base); uint currentCycle = curr & cycleMask; /* Ensure newCurrent - maxDist >= ZSTD_WINDOW_START_INDEX. */ - uint currentCycleCorrection = currentCycle < 2 ? cycleSize > 2 ? cycleSize : 2 : 0; - uint newCurrent = currentCycle + currentCycleCorrection + (maxDist > cycleSize ? maxDist : cycleSize); + uint currentCycleCorrection = + currentCycle < 2 + ? cycleSize > 2 + ? cycleSize + : 2 + : 0; + uint newCurrent = + currentCycle + currentCycleCorrection + (maxDist > cycleSize ? maxDist : cycleSize); uint correction = curr - newCurrent; assert((maxDist & maxDist - 1) == 0); assert((curr & cycleMask) == (newCurrent & cycleMask)); @@ -926,7 +1248,13 @@ namespace ZstdSharp.Unsafe * 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) + 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; @@ -954,7 +1282,13 @@ namespace ZstdSharp.Unsafe * 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) + 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); @@ -971,16 +1305,17 @@ namespace ZstdSharp.Unsafe } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_stringToByte_20_00 => new byte[] - { - 32, - 0 - }; - private static byte* stringToByte_20_00 => (byte*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_stringToByte_20_00)); + private static ReadOnlySpan Span_stringToByte_20_00 => new byte[] { 32, 0 }; + private static byte* stringToByte_20_00 => + (byte*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_stringToByte_20_00) + ); #else private static readonly byte* stringToByte_20_00 = GetArrayPointer(new byte[] { 32, 0 }); #endif + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void ZSTD_window_init(ZSTD_window_t* window) { @@ -991,7 +1326,7 @@ namespace ZstdSharp.Unsafe dictLimit = 2, lowLimit = 2, nextSrc = stringToByte_20_00 + 2, - nbOverflowCorrections = 0 + nbOverflowCorrections = 0, }; } @@ -1003,7 +1338,12 @@ namespace ZstdSharp.Unsafe * 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) + private static uint ZSTD_window_update( + ZSTD_window_t* window, + void* src, + nuint srcSize, + int forceNonContiguous + ) { byte* ip = (byte*)src; uint contiguous = 1; @@ -1026,10 +1366,14 @@ namespace ZstdSharp.Unsafe } window->nextSrc = ip + srcSize; - if (ip + srcSize > window->dictBase + window->lowLimit && ip < window->dictBase + window->dictLimit) + if ( + ip + srcSize > window->dictBase + window->lowLimit + && ip < window->dictBase + window->dictLimit + ) { nuint highInputIdx = (nuint)(ip + srcSize - window->dictBase); - uint lowLimitMax = highInputIdx > window->dictLimit ? window->dictLimit : (uint)highInputIdx; + uint lowLimitMax = + highInputIdx > window->dictLimit ? window->dictLimit : (uint)highInputIdx; assert(highInputIdx < 0xffffffff); window->lowLimit = lowLimitMax; } @@ -1041,7 +1385,11 @@ namespace ZstdSharp.Unsafe * Returns the lowest allowed match index. It may either be in the ext-dict or the prefix. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint ZSTD_getLowestMatchIndex(ZSTD_MatchState_t* ms, uint curr, uint windowLog) + private static uint ZSTD_getLowestMatchIndex( + ZSTD_MatchState_t* ms, + uint curr, + uint windowLog + ) { uint maxDistance = 1U << (int)windowLog; uint lowestValid = ms->window.lowLimit; @@ -1059,7 +1407,11 @@ namespace ZstdSharp.Unsafe * 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) + private static uint ZSTD_getLowestPrefixIndex( + ZSTD_MatchState_t* ms, + uint curr, + uint windowLog + ) { uint maxDistance = 1U << (int)windowLog; uint lowestValid = ms->window.dictLimit; diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressLiterals.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressLiterals.cs index 3d50a52c..803f6749 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressLiterals.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressLiterals.cs @@ -7,7 +7,12 @@ namespace ZstdSharp.Unsafe /* ************************************************************** * Literals compression - special cases ****************************************************************/ - private static nuint ZSTD_noCompressLiterals(void* dst, nuint dstCapacity, void* src, nuint srcSize) + private 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)); @@ -22,10 +27,16 @@ namespace ZstdSharp.Unsafe ostart[0] = (byte)((uint)SymbolEncodingType_e.set_basic + (srcSize << 3)); break; case 2: - MEM_writeLE16(ostart, (ushort)((uint)SymbolEncodingType_e.set_basic + (1 << 2) + (srcSize << 4))); + MEM_writeLE16( + ostart, + (ushort)((uint)SymbolEncodingType_e.set_basic + (1 << 2) + (srcSize << 4)) + ); break; case 3: - MEM_writeLE32(ostart, (uint)((uint)SymbolEncodingType_e.set_basic + (3 << 2) + (srcSize << 4))); + MEM_writeLE32( + ostart, + (uint)((uint)SymbolEncodingType_e.set_basic + (3 << 2) + (srcSize << 4)) + ); break; default: assert(0 != 0); @@ -57,7 +68,12 @@ namespace ZstdSharp.Unsafe * Conditions : * - All bytes in @src are identical * - dstCapacity >= 4 */ - private static nuint ZSTD_compressRleLiteralsBlock(void* dst, nuint dstCapacity, void* src, nuint srcSize) + private 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)); @@ -69,10 +85,16 @@ namespace ZstdSharp.Unsafe ostart[0] = (byte)((uint)SymbolEncodingType_e.set_rle + (srcSize << 3)); break; case 2: - MEM_writeLE16(ostart, (ushort)((uint)SymbolEncodingType_e.set_rle + (1 << 2) + (srcSize << 4))); + MEM_writeLE16( + ostart, + (ushort)((uint)SymbolEncodingType_e.set_rle + (1 << 2) + (srcSize << 4)) + ); break; case 3: - MEM_writeLE32(ostart, (uint)((uint)SymbolEncodingType_e.set_rle + (3 << 2) + (srcSize << 4))); + MEM_writeLE32( + ostart, + (uint)((uint)SymbolEncodingType_e.set_rle + (3 << 2) + (srcSize << 4)) + ); break; default: assert(0 != 0); @@ -88,7 +110,10 @@ namespace ZstdSharp.Unsafe * for literal compression to even be attempted. * Minimum is made tighter as compression strategy increases. */ - private static nuint ZSTD_minLiteralsToCompress(ZSTD_strategy strategy, HUF_repeat huf_repeat) + private static nuint ZSTD_minLiteralsToCompress( + ZSTD_strategy strategy, + HUF_repeat huf_repeat + ) { assert((int)strategy >= 0); assert((int)strategy <= 9); @@ -104,9 +129,24 @@ namespace ZstdSharp.Unsafe * @entropyWorkspaceSize : must be >= HUF_WORKSPACE_SIZE * @suspectUncompressible: sampling checks, to potentially skip huffman coding */ - private static nuint ZSTD_compressLiterals(void* dst, nuint dstCapacity, void* src, nuint srcSize, void* entropyWorkspace, nuint entropyWorkspaceSize, ZSTD_hufCTables_t* prevHuf, ZSTD_hufCTables_t* nextHuf, ZSTD_strategy strategy, int disableLiteralCompression, int suspectUncompressible, int bmi2) + private static nuint ZSTD_compressLiterals( + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize, + void* entropyWorkspace, + nuint entropyWorkspaceSize, + ZSTD_hufCTables_t* prevHuf, + ZSTD_hufCTables_t* nextHuf, + ZSTD_strategy strategy, + int disableLiteralCompression, + int suspectUncompressible, + int bmi2 + ) { - nuint lhSize = (nuint)(3 + (srcSize >= 1 * (1 << 10) ? 1 : 0) + (srcSize >= 16 * (1 << 10) ? 1 : 0)); + nuint lhSize = (nuint)( + 3 + (srcSize >= 1 * (1 << 10) ? 1 : 0) + (srcSize >= 16 * (1 << 10) ? 1 : 0) + ); byte* ostart = (byte*)dst; uint singleStream = srcSize < 256 ? 1U : 0U; SymbolEncodingType_e hType = SymbolEncodingType_e.set_compressed; @@ -123,12 +163,82 @@ namespace ZstdSharp.Unsafe { HUF_repeat repeat = prevHuf->repeatMode; - int flags = 0 | (bmi2 != 0 ? (int)HUF_flags_e.HUF_flags_bmi2 : 0) | (strategy < ZSTD_strategy.ZSTD_lazy && srcSize <= 1024 ? (int)HUF_flags_e.HUF_flags_preferRepeat : 0) | (strategy >= ZSTD_strategy.ZSTD_btultra ? (int)HUF_flags_e.HUF_flags_optimalDepth : 0) | (suspectUncompressible != 0 ? (int)HUF_flags_e.HUF_flags_suspectUncompressible : 0); + int flags = + 0 + | (bmi2 != 0 ? (int)HUF_flags_e.HUF_flags_bmi2 : 0) + | ( + strategy < ZSTD_strategy.ZSTD_lazy && srcSize <= 1024 + ? (int)HUF_flags_e.HUF_flags_preferRepeat + : 0 + ) + | ( + strategy >= ZSTD_strategy.ZSTD_btultra + ? (int)HUF_flags_e.HUF_flags_optimalDepth + : 0 + ) + | ( + suspectUncompressible != 0 + ? (int)HUF_flags_e.HUF_flags_suspectUncompressible + : 0 + ); void* huf_compress; if (repeat == HUF_repeat.HUF_repeat_valid && lhSize == 3) singleStream = 1; - huf_compress = singleStream != 0 ? (delegate* managed)(&HUF_compress1X_repeat) : (delegate* managed)(&HUF_compress4X_repeat); - cLitSize = ((delegate* managed)huf_compress)(ostart + lhSize, dstCapacity - lhSize, src, srcSize, 255, 11, entropyWorkspace, entropyWorkspaceSize, &nextHuf->CTable.e0, &repeat, flags); + huf_compress = + singleStream != 0 + ? (delegate* managed< + void*, + nuint, + void*, + nuint, + uint, + uint, + void*, + nuint, + nuint*, + HUF_repeat*, + int, + nuint>)(&HUF_compress1X_repeat) + : (delegate* managed< + void*, + nuint, + void*, + nuint, + uint, + uint, + void*, + nuint, + nuint*, + HUF_repeat*, + int, + nuint>)(&HUF_compress4X_repeat); + cLitSize = ( + (delegate* managed< + void*, + nuint, + void*, + nuint, + uint, + uint, + void*, + nuint, + nuint*, + HUF_repeat*, + int, + nuint>)huf_compress + )( + ostart + lhSize, + dstCapacity - lhSize, + src, + srcSize, + 255, + 11, + entropyWorkspace, + entropyWorkspaceSize, + &nextHuf->CTable.e0, + &repeat, + flags + ); if (repeat != HUF_repeat.HUF_repeat_none) { hType = SymbolEncodingType_e.set_repeat; @@ -164,25 +274,38 @@ namespace ZstdSharp.Unsafe #if DEBUG if (singleStream == 0) assert(srcSize >= 6); + #endif { - uint lhc = (uint)hType + ((singleStream == 0 ? 1U : 0U) << 2) + ((uint)srcSize << 4) + ((uint)cLitSize << 14); + uint lhc = + (uint)hType + + ((singleStream == 0 ? 1U : 0U) << 2) + + ((uint)srcSize << 4) + + ((uint)cLitSize << 14); MEM_writeLE24(ostart, lhc); break; } case 4: assert(srcSize >= 6); + { - uint lhc = (uint)(hType + (2 << 2)) + ((uint)srcSize << 4) + ((uint)cLitSize << 18); + uint lhc = + (uint)(hType + (2 << 2)) + + ((uint)srcSize << 4) + + ((uint)cLitSize << 18); MEM_writeLE32(ostart, lhc); break; } case 5: assert(srcSize >= 6); + { - uint lhc = (uint)(hType + (3 << 2)) + ((uint)srcSize << 4) + ((uint)cLitSize << 22); + uint lhc = + (uint)(hType + (3 << 2)) + + ((uint)srcSize << 4) + + ((uint)cLitSize << 22); MEM_writeLE32(ostart, lhc); ostart[4] = (byte)(cLitSize >> 10); break; @@ -196,4 +319,4 @@ namespace ZstdSharp.Unsafe return lhSize + cLitSize; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressSequences.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressSequences.cs index 17578d25..c336ff44 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressSequences.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressSequences.cs @@ -1,277 +1,543 @@ -using static ZstdSharp.UnsafeHelper; using System; -using System.Runtime.InteropServices; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_kInverseProbabilityLog256 => 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* kInverseProbabilityLog256 => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_kInverseProbabilityLog256)); + private static ReadOnlySpan Span_kInverseProbabilityLog256 => + 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* kInverseProbabilityLog256 => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_kInverseProbabilityLog256) + ); #else - private static readonly 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 readonly 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, + } + ); #endif + private static uint ZSTD_getFSEMaxSymbolValue(uint* ctable) { void* ptr = ctable; @@ -299,7 +565,14 @@ namespace ZstdSharp.Unsafe short* norm = stackalloc short[53]; uint tableLog = FSE_optimalTableLog(FSELog, nbSeq, max); { - nuint err_code = FSE_normalizeCount(norm, tableLog, count, nbSeq, max, ZSTD_useLowProbCount(nbSeq)); + nuint err_code = FSE_normalizeCount( + norm, + tableLog, + count, + nbSeq, + max, + ZSTD_useLowProbCount(nbSeq) + ); if (ERR_isError(err_code)) { return err_code; @@ -369,7 +642,12 @@ namespace ZstdSharp.Unsafe * 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. */ - private static nuint ZSTD_crossEntropyCost(short* norm, uint accuracyLog, uint* count, uint max) + private static nuint ZSTD_crossEntropyCost( + short* norm, + uint accuracyLog, + uint* count, + uint max + ) { uint shift = 8 - accuracyLog; nuint cost = 0; @@ -387,7 +665,19 @@ namespace ZstdSharp.Unsafe return cost >> 8; } - private 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) + private 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) { @@ -408,7 +698,8 @@ namespace ZstdSharp.Unsafe nuint mult = (nuint)(10 - strategy); const nuint baseLog = 3; /* 28-36 for offset, 56-72 for lengths */ - nuint dynamicFse_nbSeq_min = ((nuint)1 << (int)defaultNormLog) * mult >> (int)baseLog; + 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) @@ -416,7 +707,10 @@ namespace ZstdSharp.Unsafe return SymbolEncodingType_e.set_repeat; } - if (nbSeq < dynamicFse_nbSeq_min || mostFrequent < nbSeq >> (int)(defaultNormLog - 1)) + if ( + nbSeq < dynamicFse_nbSeq_min + || mostFrequent < nbSeq >> (int)(defaultNormLog - 1) + ) { *repeatMode = FSE_repeat.FSE_repeat_none; return SymbolEncodingType_e.set_basic; @@ -425,20 +719,30 @@ namespace ZstdSharp.Unsafe } 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 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 DEBUG if (isDefaultAllowed != default) { assert(!ERR_isError(basicCost)); - assert(!(*repeatMode == FSE_repeat.FSE_repeat_valid && ERR_isError(repeatCost))); + assert( + !(*repeatMode == FSE_repeat.FSE_repeat_valid && ERR_isError(repeatCost)) + ); } #endif assert(!ERR_isError(NCountCost)); - assert(compressedCost < unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxCode))); + assert( + compressedCost < unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_maxCode)) + ); if (basicCost <= repeatCost && basicCost <= compressedCost) { assert(isDefaultAllowed != default); @@ -459,7 +763,24 @@ namespace ZstdSharp.Unsafe return SymbolEncodingType_e.set_compressed; } - private 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) + private 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; @@ -487,7 +808,14 @@ namespace ZstdSharp.Unsafe case SymbolEncodingType_e.set_basic: { /* note : could be pre-calculated */ - nuint err_code = FSE_buildCTable_wksp(nextCTable, defaultNorm, defaultMax, defaultNormLog, entropyWorkspace, entropyWorkspaceSize); + nuint err_code = FSE_buildCTable_wksp( + nextCTable, + defaultNorm, + defaultMax, + defaultNormLog, + entropyWorkspace, + entropyWorkspaceSize + ); if (ERR_isError(err_code)) { return err_code; @@ -496,49 +824,69 @@ namespace ZstdSharp.Unsafe 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) { - 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--; - } + count[codeTable[nbSeq - 1]]--; + nbSeq_1--; + } - assert(nbSeq_1 > 1); - assert(entropyWorkspaceSize >= (nuint)sizeof(ZSTD_BuildCTableWksp)); + assert(nbSeq_1 > 1); + assert(entropyWorkspaceSize >= (nuint)sizeof(ZSTD_BuildCTableWksp)); + { + nuint err_code = FSE_normalizeCount( + wksp->norm, + tableLog, + count, + nbSeq_1, + max, + ZSTD_useLowProbCount(nbSeq_1) + ); + if (ERR_isError(err_code)) { - nuint err_code = FSE_normalizeCount(wksp->norm, tableLog, count, nbSeq_1, max, ZSTD_useLowProbCount(nbSeq_1)); + return err_code; + } + } + + assert(oend >= op); + { + /* overflow protected */ + nuint NCountSize = FSE_writeNCount( + op, + (nuint)(oend - op), + wksp->norm, + max, + tableLog + ); + { + nuint err_code = NCountSize; if (ERR_isError(err_code)) { return err_code; } } - assert(oend >= op); { - /* overflow protected */ - nuint NCountSize = FSE_writeNCount(op, (nuint)(oend - op), wksp->norm, max, tableLog); + nuint err_code = FSE_buildCTable_wksp( + nextCTable, + wksp->norm, + max, + tableLog, + wksp->wksp, + sizeof(uint) * 285 + ); + if (ERR_isError(err_code)) { - nuint err_code = NCountSize; - if (ERR_isError(err_code)) - { - return err_code; - } + return err_code; } - - { - nuint err_code = FSE_buildCTable_wksp(nextCTable, wksp->norm, max, tableLog, wksp->wksp, sizeof(uint) * 285); - if (ERR_isError(err_code)) - { - return err_code; - } - } - - return NCountSize; } + + return NCountSize; } + } default: assert(0 != 0); @@ -546,7 +894,19 @@ namespace ZstdSharp.Unsafe } } - 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) + 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; System.Runtime.CompilerServices.Unsafe.SkipInit(out blockStream); @@ -568,30 +928,81 @@ namespace ZstdSharp.Unsafe FSE_initCState2(ref stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq - 1]); FSE_initCState2(ref stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq - 1]); FSE_initCState2(ref stateLitLength, CTable_LitLength, llCodeTable[nbSeq - 1]); - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[nbSeq - 1].litLength, LL_bits[llCodeTable[nbSeq - 1]]); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[nbSeq - 1].litLength, + LL_bits[llCodeTable[nbSeq - 1]] + ); if (MEM_32bits) - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[nbSeq - 1].mlBase, ML_bits[mlCodeTable[nbSeq - 1]]); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[nbSeq - 1].mlBase, + ML_bits[mlCodeTable[nbSeq - 1]] + ); if (MEM_32bits) - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); 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); + uint extraBits = + ofBits + - ( + ofBits < (uint)(MEM_32bits ? 25 : 57) - 1 + ? ofBits + : (uint)(MEM_32bits ? 25 : 57) - 1 + ); if (extraBits != 0) { - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[nbSeq - 1].offBase, extraBits); - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[nbSeq - 1].offBase, + extraBits + ); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); } - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[nbSeq - 1].offBase >> (int)extraBits, ofBits - extraBits); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[nbSeq - 1].offBase >> (int)extraBits, + ofBits - extraBits + ); } else { - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[nbSeq - 1].offBase, ofCodeTable[nbSeq - 1]); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[nbSeq - 1].offBase, + ofCodeTable[nbSeq - 1] + ); } - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); { nuint n; for (n = nbSeq - 2; n < nbSeq; n--) @@ -602,44 +1013,144 @@ namespace ZstdSharp.Unsafe uint llBits = LL_bits[llCode]; uint ofBits = ofCode; uint mlBits = ML_bits[mlCode]; - FSE_encodeSymbol(ref blockStream_bitContainer, ref blockStream_bitPos, ref stateOffsetBits, ofCode); - FSE_encodeSymbol(ref blockStream_bitContainer, ref blockStream_bitPos, ref stateMatchLength, mlCode); + FSE_encodeSymbol( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref stateOffsetBits, + ofCode + ); + FSE_encodeSymbol( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref stateMatchLength, + mlCode + ); if (MEM_32bits) - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); - FSE_encodeSymbol(ref blockStream_bitContainer, ref blockStream_bitPos, ref stateLitLength, llCode); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); + FSE_encodeSymbol( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref stateLitLength, + llCode + ); if (MEM_32bits || ofBits + mlBits + llBits >= 64 - 7 - (9 + 9 + 8)) - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[n].litLength, llBits); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[n].litLength, + llBits + ); if (MEM_32bits && llBits + mlBits > 24) - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[n].mlBase, mlBits); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[n].mlBase, + mlBits + ); if (MEM_32bits || ofBits + mlBits + llBits > 56) - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); if (longOffsets != 0) { - uint extraBits = ofBits - (ofBits < (uint)(MEM_32bits ? 25 : 57) - 1 ? ofBits : (uint)(MEM_32bits ? 25 : 57) - 1); + uint extraBits = + ofBits + - ( + ofBits < (uint)(MEM_32bits ? 25 : 57) - 1 + ? ofBits + : (uint)(MEM_32bits ? 25 : 57) - 1 + ); if (extraBits != 0) { - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[n].offBase, extraBits); - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[n].offBase, + extraBits + ); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); } - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[n].offBase >> (int)extraBits, ofBits - extraBits); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[n].offBase >> (int)extraBits, + ofBits - extraBits + ); } else { - BIT_addBits(ref blockStream_bitContainer, ref blockStream_bitPos, sequences[n].offBase, ofBits); + BIT_addBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + sequences[n].offBase, + ofBits + ); } - BIT_flushBits(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr); + BIT_flushBits( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr + ); } } - FSE_flushCState(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr, ref stateMatchLength); - FSE_flushCState(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr, ref stateOffsetBits); - FSE_flushCState(ref blockStream_bitContainer, ref blockStream_bitPos, ref blockStream_ptr, blockStream_endPtr, ref stateLitLength); + FSE_flushCState( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr, + ref stateMatchLength + ); + FSE_flushCState( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr, + ref stateOffsetBits + ); + FSE_flushCState( + ref blockStream_bitContainer, + ref blockStream_bitPos, + ref blockStream_ptr, + blockStream_endPtr, + ref stateLitLength + ); { - nuint streamSize = BIT_closeCStream(ref blockStream_bitContainer, ref blockStream_bitPos, blockStream_ptr, blockStream_endPtr, blockStream.startPtr); + nuint streamSize = BIT_closeCStream( + ref blockStream_bitContainer, + ref blockStream_bitPos, + blockStream_ptr, + blockStream_endPtr, + blockStream.startPtr + ); if (streamSize == 0) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); @@ -649,14 +1160,63 @@ namespace ZstdSharp.Unsafe } } - 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) + 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); + return ZSTD_encodeSequences_body( + dst, + dstCapacity, + CTable_MatchLength, + mlCodeTable, + CTable_OffsetBits, + ofCodeTable, + CTable_LitLength, + llCodeTable, + sequences, + nbSeq, + longOffsets + ); } - private 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) + private 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 + ) { - return ZSTD_encodeSequences_default(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 + ); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressSuperblock.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressSuperblock.cs index 3d5382c9..1f12375c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressSuperblock.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCompressSuperblock.cs @@ -23,15 +23,30 @@ namespace ZstdSharp.Unsafe * @return : compressed size of literals section of a sub-block * Or 0 if unable to compress. * Or error code */ - private static nuint ZSTD_compressSubBlock_literal(nuint* hufTable, ZSTD_hufCTablesMetadata_t* hufMetadata, byte* literals, nuint litSize, void* dst, nuint dstSize, int bmi2, int writeEntropy, int* entropyWritten) + private static nuint ZSTD_compressSubBlock_literal( + nuint* 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 >= 1 * (1 << 10) - header ? 1 : 0) + (litSize >= 16 * (1 << 10) - header ? 1 : 0)); + nuint lhSize = (nuint)( + 3 + + (litSize >= 1 * (1 << 10) - header ? 1 : 0) + + (litSize >= 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; + 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) @@ -44,7 +59,10 @@ namespace ZstdSharp.Unsafe } assert(litSize > 0); - assert(hufMetadata->hType == SymbolEncodingType_e.set_compressed || hufMetadata->hType == SymbolEncodingType_e.set_repeat); + assert( + hufMetadata->hType == SymbolEncodingType_e.set_compressed + || hufMetadata->hType == SymbolEncodingType_e.set_repeat + ); if (writeEntropy != 0 && hufMetadata->hType == SymbolEncodingType_e.set_compressed) { memcpy(op, hufMetadata->hufDesBuffer, (uint)hufMetadata->hufDesSize); @@ -54,7 +72,24 @@ namespace ZstdSharp.Unsafe { int flags = bmi2 != 0 ? (int)HUF_flags_e.HUF_flags_bmi2 : 0; - nuint cSize = singleStream != 0 ? HUF_compress1X_usingCTable(op, (nuint)(oend - op), literals, litSize, hufTable, flags) : HUF_compress4X_usingCTable(op, (nuint)(oend - op), literals, litSize, hufTable, flags); + nuint cSize = + singleStream != 0 + ? HUF_compress1X_usingCTable( + op, + (nuint)(oend - op), + literals, + litSize, + hufTable, + flags + ) + : HUF_compress4X_usingCTable( + op, + (nuint)(oend - op), + literals, + litSize, + hufTable, + flags + ); op += cSize; cLitSize += cSize; if (cSize == 0 || ERR_isError(cSize)) @@ -67,7 +102,14 @@ namespace ZstdSharp.Unsafe return ZSTD_noCompressLiterals(dst, dstSize, literals, litSize); } - if (lhSize < (nuint)(3 + (cLitSize >= 1 * (1 << 10) ? 1 : 0) + (cLitSize >= 16 * (1 << 10) ? 1 : 0))) + if ( + lhSize + < (nuint)( + 3 + + (cLitSize >= 1 * (1 << 10) ? 1 : 0) + + (cLitSize >= 16 * (1 << 10) ? 1 : 0) + ) + ) { assert(cLitSize > litSize); return ZSTD_noCompressLiterals(dst, dstSize, literals, litSize); @@ -77,26 +119,32 @@ namespace ZstdSharp.Unsafe switch (lhSize) { case 3: - { - uint lhc = (uint)hType + ((singleStream == 0 ? 1U : 0U) << 2) + ((uint)litSize << 4) + ((uint)cLitSize << 14); - MEM_writeLE24(ostart, lhc); - break; - } + { + uint lhc = + (uint)hType + + ((singleStream == 0 ? 1U : 0U) << 2) + + ((uint)litSize << 4) + + ((uint)cLitSize << 14); + MEM_writeLE24(ostart, lhc); + break; + } case 4: - { - uint lhc = (uint)(hType + (2 << 2)) + ((uint)litSize << 4) + ((uint)cLitSize << 18); - MEM_writeLE32(ostart, lhc); - break; - } + { + uint lhc = + (uint)(hType + (2 << 2)) + ((uint)litSize << 4) + ((uint)cLitSize << 18); + MEM_writeLE32(ostart, lhc); + break; + } case 5: - { - uint lhc = (uint)(hType + (3 << 2)) + ((uint)litSize << 4) + ((uint)cLitSize << 22); - MEM_writeLE32(ostart, lhc); - ostart[4] = (byte)(cLitSize >> 10); - break; - } + { + uint lhc = + (uint)(hType + (3 << 2)) + ((uint)litSize << 4) + ((uint)cLitSize << 22); + MEM_writeLE32(ostart, lhc); + ostart[4] = (byte)(cLitSize >> 10); + break; + } default: assert(0 != 0); @@ -107,7 +155,13 @@ namespace ZstdSharp.Unsafe return (nuint)(op - ostart); } - private static nuint ZSTD_seqDecompressedSize(SeqStore_t* seqStore, SeqDef_s* sequences, nuint nbSeqs, nuint litSize, int lastSubBlock) + private static nuint ZSTD_seqDecompressedSize( + SeqStore_t* seqStore, + SeqDef_s* sequences, + nuint nbSeqs, + nuint litSize, + int lastSubBlock + ) { nuint matchLengthSum = 0; nuint litLengthSum = 0; @@ -136,7 +190,21 @@ namespace ZstdSharp.Unsafe * @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) + 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; @@ -186,7 +254,20 @@ namespace ZstdSharp.Unsafe } { - nuint bitstreamSize = ZSTD_encodeSequences(op, (nuint)(oend - op), fseTables->matchlengthCTable, mlCode, fseTables->offcodeCTable, ofCode, fseTables->litlengthCTable, llCode, sequences, nbSeq, longOffsets, bmi2); + nuint bitstreamSize = ZSTD_encodeSequences( + op, + (nuint)(oend - op), + fseTables->matchlengthCTable, + mlCode, + fseTables->offcodeCTable, + ofCode, + fseTables->litlengthCTable, + llCode, + sequences, + nbSeq, + longOffsets, + bmi2 + ); { nuint err_code = bitstreamSize; if (ERR_isError(err_code)) @@ -196,7 +277,11 @@ namespace ZstdSharp.Unsafe } op += bitstreamSize; - if (writeEntropy != 0 && fseMetadata->lastCountSize != 0 && fseMetadata->lastCountSize + bitstreamSize < 4) + if ( + writeEntropy != 0 + && fseMetadata->lastCountSize != 0 + && fseMetadata->lastCountSize + bitstreamSize < 4 + ) { assert(fseMetadata->lastCountSize + bitstreamSize == 3); return 0; @@ -216,13 +301,42 @@ namespace ZstdSharp.Unsafe * 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) + 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(&entropy->huf.CTable.e0, &entropyMetadata->hufMetadata, literals, litSize, op, (nuint)(oend - op), bmi2, writeLitEntropy, litEntropyWritten); + nuint cLitSize = ZSTD_compressSubBlock_literal( + &entropy->huf.CTable.e0, + &entropyMetadata->hufMetadata, + literals, + litSize, + op, + (nuint)(oend - op), + bmi2, + writeLitEntropy, + litEntropyWritten + ); { nuint err_code = cLitSize; if (ERR_isError(err_code)) @@ -237,7 +351,21 @@ namespace ZstdSharp.Unsafe } { - nuint cSeqSize = ZSTD_compressSubBlock_sequences(&entropy->fse, &entropyMetadata->fseMetadata, sequences, nbSeq, llCode, mlCode, ofCode, cctxParams, op, (nuint)(oend - op), bmi2, writeSeqEntropy, seqEntropyWritten); + nuint cSeqSize = ZSTD_compressSubBlock_sequences( + &entropy->fse, + &entropyMetadata->fseMetadata, + sequences, + nbSeq, + llCode, + mlCode, + ofCode, + cctxParams, + op, + (nuint)(oend - op), + bmi2, + writeSeqEntropy, + seqEntropyWritten + ); { nuint err_code = cSeqSize; if (ERR_isError(err_code)) @@ -253,14 +381,23 @@ namespace ZstdSharp.Unsafe { nuint cSize = (nuint)(op - ostart) - ZSTD_blockHeaderSize; - uint cBlockHeader24 = lastBlock + ((uint)blockType_e.bt_compressed << 1) + (uint)(cSize << 3); + uint cBlockHeader24 = + lastBlock + ((uint)blockType_e.bt_compressed << 1) + (uint)(cSize << 3); MEM_writeLE24(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) + 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; @@ -270,13 +407,27 @@ namespace ZstdSharp.Unsafe 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) + else if ( + hufMetadata->hType == SymbolEncodingType_e.set_compressed + || hufMetadata->hType == SymbolEncodingType_e.set_repeat + ) { - nuint largest = HIST_count_wksp(countWksp, &maxSymbolValue, literals, litSize, workspace, wkspSize); + nuint largest = HIST_count_wksp( + countWksp, + &maxSymbolValue, + literals, + litSize, + workspace, + wkspSize + ); if (ERR_isError(largest)) return litSize; { - nuint cLitSizeEstimate = HUF_estimateCompressedSize(&huf->CTable.e0, countWksp, maxSymbolValue); + nuint cLitSizeEstimate = HUF_estimateCompressedSize( + &huf->CTable.e0, + countWksp, + maxSymbolValue + ); if (writeEntropy != 0) cLitSizeEstimate += hufMetadata->hufDesSize; return cLitSizeEstimate + literalSectionHeaderSize; @@ -287,7 +438,19 @@ namespace ZstdSharp.Unsafe return 0; } - private static nuint ZSTD_estimateSubBlockSize_symbolType(SymbolEncodingType_e type, byte* codeTable, uint maxCode, nuint nbSeq, uint* fseCTable, byte* additionalBits, short* defaultNorm, uint defaultNormLog, uint defaultMax, void* workspace, nuint wkspSize) + private static nuint ZSTD_estimateSubBlockSize_symbolType( + SymbolEncodingType_e type, + byte* codeTable, + uint maxCode, + nuint nbSeq, + uint* fseCTable, + byte* additionalBits, + short* defaultNorm, + uint defaultNormLog, + uint defaultMax, + void* workspace, + nuint wkspSize + ) { uint* countWksp = (uint*)workspace; byte* ctp = codeTable; @@ -299,13 +462,19 @@ namespace ZstdSharp.Unsafe 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)); + 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) + else if ( + type == SymbolEncodingType_e.set_compressed + || type == SymbolEncodingType_e.set_repeat + ) { cSymbolTypeSizeEstimateInBits = ZSTD_fseBitCost(fseCTable, countWksp, max); } @@ -324,44 +493,131 @@ namespace ZstdSharp.Unsafe 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) + 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 + ) { /* Use hard coded size of 3 bytes */ const nuint sequencesSectionHeaderSize = 3; nuint cSeqSizeEstimate = 0; if (nbSeq == 0) return sequencesSectionHeaderSize; - cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->ofType, ofCodeTable, 31, nbSeq, fseTables->offcodeCTable, null, OF_defaultNorm, OF_defaultNormLog, 28, workspace, wkspSize); - cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->llType, llCodeTable, 35, nbSeq, fseTables->litlengthCTable, LL_bits, LL_defaultNorm, LL_defaultNormLog, 35, workspace, wkspSize); - cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType(fseMetadata->mlType, mlCodeTable, 52, nbSeq, fseTables->matchlengthCTable, ML_bits, ML_defaultNorm, ML_defaultNormLog, 52, workspace, wkspSize); + cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType( + fseMetadata->ofType, + ofCodeTable, + 31, + nbSeq, + fseTables->offcodeCTable, + null, + OF_defaultNorm, + OF_defaultNormLog, + 28, + workspace, + wkspSize + ); + cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType( + fseMetadata->llType, + llCodeTable, + 35, + nbSeq, + fseTables->litlengthCTable, + LL_bits, + LL_defaultNorm, + LL_defaultNormLog, + 35, + workspace, + wkspSize + ); + cSeqSizeEstimate += ZSTD_estimateSubBlockSize_symbolType( + fseMetadata->mlType, + mlCodeTable, + 52, + nbSeq, + fseTables->matchlengthCTable, + ML_bits, + ML_defaultNorm, + ML_defaultNormLog, + 52, + workspace, + wkspSize + ); if (writeEntropy != 0) cSeqSizeEstimate += fseMetadata->fseTablesSize; return cSeqSizeEstimate + sequencesSectionHeaderSize; } - private static EstimatedBlockSize 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) + private static EstimatedBlockSize 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 + ) { EstimatedBlockSize ebs; - ebs.estLitSize = ZSTD_estimateSubBlockSize_literal(literals, litSize, &entropy->huf, &entropyMetadata->hufMetadata, workspace, wkspSize, writeLitEntropy); - ebs.estBlockSize = ZSTD_estimateSubBlockSize_sequences(ofCodeTable, llCodeTable, mlCodeTable, nbSeq, &entropy->fse, &entropyMetadata->fseMetadata, workspace, wkspSize, writeSeqEntropy); + ebs.estLitSize = ZSTD_estimateSubBlockSize_literal( + literals, + litSize, + &entropy->huf, + &entropyMetadata->hufMetadata, + workspace, + wkspSize, + writeLitEntropy + ); + ebs.estBlockSize = ZSTD_estimateSubBlockSize_sequences( + ofCodeTable, + llCodeTable, + mlCodeTable, + nbSeq, + &entropy->fse, + &entropyMetadata->fseMetadata, + workspace, + wkspSize, + writeSeqEntropy + ); ebs.estBlockSize += ebs.estLitSize + ZSTD_blockHeaderSize; return ebs; } private static int ZSTD_needSequenceEntropyTables(ZSTD_fseCTablesMetadata_t* fseMetadata) { - if (fseMetadata->llType == SymbolEncodingType_e.set_compressed || fseMetadata->llType == SymbolEncodingType_e.set_rle) + 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) + 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) + if ( + fseMetadata->ofType == SymbolEncodingType_e.set_compressed + || fseMetadata->ofType == SymbolEncodingType_e.set_rle + ) return 1; return 0; } private static nuint countLiterals(SeqStore_t* seqStore, SeqDef_s* sp, nuint seqCount) { - nuint n, total = 0; + nuint n, + total = 0; assert(sp != null); for (n = 0; n < seqCount; n++) { @@ -371,9 +627,18 @@ namespace ZstdSharp.Unsafe return total; } - private static nuint sizeBlockSequences(SeqDef_s* sp, nuint nbSeqs, nuint targetBudget, nuint avgLitCost, nuint avgSeqCost, int firstSubBlock) + private static nuint sizeBlockSequences( + SeqDef_s* sp, + nuint nbSeqs, + nuint targetBudget, + nuint avgLitCost, + nuint avgSeqCost, + int firstSubBlock + ) { - nuint n, budget = 0, inSize = 0; + nuint n, + budget = 0, + inSize = 0; /* generous estimate */ nuint headerSize = (nuint)firstSubBlock * 120 * 256; assert(firstSubBlock == 0 || firstSubBlock == 1); @@ -401,7 +666,21 @@ namespace ZstdSharp.Unsafe * Sub-blocks are all compressed, except the last one when beneficial. * @return : compressed size of the super block (which features 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) + 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; @@ -422,17 +701,37 @@ namespace ZstdSharp.Unsafe byte* ofCodePtr = seqStorePtr->ofCode; /* enforce minimum size, to reduce undesirable side effects */ const nuint minTarget = 1340; - nuint targetCBlockSize = minTarget > cctxParams->targetCBlockSize ? minTarget : cctxParams->targetCBlockSize; - int writeLitEntropy = entropyMetadata->hufMetadata.hType == SymbolEncodingType_e.set_compressed ? 1 : 0; + nuint targetCBlockSize = + minTarget > cctxParams->targetCBlockSize ? minTarget : cctxParams->targetCBlockSize; + int writeLitEntropy = + entropyMetadata->hufMetadata.hType == SymbolEncodingType_e.set_compressed ? 1 : 0; int writeSeqEntropy = 1; if (nbSeqs > 0) { - EstimatedBlockSize ebs = ZSTD_estimateSubBlockSize(lp, nbLiterals, ofCodePtr, llCodePtr, mlCodePtr, nbSeqs, &nextCBlock->entropy, entropyMetadata, workspace, wkspSize, writeLitEntropy, writeSeqEntropy); + EstimatedBlockSize ebs = ZSTD_estimateSubBlockSize( + lp, + nbLiterals, + ofCodePtr, + llCodePtr, + mlCodePtr, + nbSeqs, + &nextCBlock->entropy, + entropyMetadata, + workspace, + wkspSize, + writeLitEntropy, + writeSeqEntropy + ); /* quick estimation */ nuint avgLitCost = nbLiterals != 0 ? ebs.estLitSize * 256 / nbLiterals : 256; nuint avgSeqCost = (ebs.estBlockSize - ebs.estLitSize) * 256 / nbSeqs; - nuint nbSubBlocks = (ebs.estBlockSize + targetCBlockSize / 2) / targetCBlockSize > 1 ? (ebs.estBlockSize + targetCBlockSize / 2) / targetCBlockSize : 1; - nuint n, avgBlockBudget, blockBudgetSupp = 0; + nuint nbSubBlocks = + (ebs.estBlockSize + targetCBlockSize / 2) / targetCBlockSize > 1 + ? (ebs.estBlockSize + targetCBlockSize / 2) / targetCBlockSize + : 1; + nuint n, + avgBlockBudget, + blockBudgetSupp = 0; avgBlockBudget = ebs.estBlockSize * 256 / nbSubBlocks; if (ebs.estBlockSize > srcSize) return 0; @@ -440,7 +739,14 @@ namespace ZstdSharp.Unsafe for (n = 0; n < nbSubBlocks - 1; n++) { /* determine nb of sequences for current sub-block + nbLiterals from next sequence */ - nuint seqCount = sizeBlockSequences(sp, (nuint)(send - sp), avgBlockBudget + blockBudgetSupp, avgLitCost, avgSeqCost, n == 0 ? 1 : 0); + nuint seqCount = sizeBlockSequences( + sp, + (nuint)(send - sp), + avgBlockBudget + blockBudgetSupp, + avgLitCost, + avgSeqCost, + n == 0 ? 1 : 0 + ); assert(seqCount <= (nuint)(send - sp)); if (sp + seqCount == send) break; @@ -449,8 +755,33 @@ namespace ZstdSharp.Unsafe int litEntropyWritten = 0; int seqEntropyWritten = 0; nuint litSize = countLiterals(seqStorePtr, sp, seqCount); - nuint decompressedSize = ZSTD_seqDecompressedSize(seqStorePtr, sp, seqCount, litSize, 0); - nuint cSize = ZSTD_compressSubBlock(&nextCBlock->entropy, entropyMetadata, sp, seqCount, lp, litSize, llCodePtr, mlCodePtr, ofCodePtr, cctxParams, op, (nuint)(oend - op), bmi2, writeLitEntropy, writeSeqEntropy, &litEntropyWritten, &seqEntropyWritten, 0); + nuint decompressedSize = ZSTD_seqDecompressedSize( + seqStorePtr, + sp, + seqCount, + litSize, + 0 + ); + nuint cSize = ZSTD_compressSubBlock( + &nextCBlock->entropy, + entropyMetadata, + sp, + seqCount, + lp, + litSize, + llCodePtr, + mlCodePtr, + ofCodePtr, + cctxParams, + op, + (nuint)(oend - op), + bmi2, + writeLitEntropy, + writeSeqEntropy, + &litEntropyWritten, + &seqEntropyWritten, + 0 + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -490,8 +821,33 @@ namespace ZstdSharp.Unsafe int seqEntropyWritten = 0; nuint litSize = (nuint)(lend - lp); nuint seqCount = (nuint)(send - sp); - nuint decompressedSize = ZSTD_seqDecompressedSize(seqStorePtr, sp, seqCount, litSize, 1); - nuint cSize = ZSTD_compressSubBlock(&nextCBlock->entropy, entropyMetadata, sp, seqCount, lp, litSize, llCodePtr, mlCodePtr, ofCodePtr, cctxParams, op, (nuint)(oend - op), bmi2, writeLitEntropy, writeSeqEntropy, &litEntropyWritten, &seqEntropyWritten, lastBlock); + nuint decompressedSize = ZSTD_seqDecompressedSize( + seqStorePtr, + sp, + seqCount, + litSize, + 1 + ); + nuint cSize = ZSTD_compressSubBlock( + &nextCBlock->entropy, + entropyMetadata, + sp, + seqCount, + lp, + litSize, + llCodePtr, + mlCodePtr, + ofCodePtr, + cctxParams, + op, + (nuint)(oend - op), + bmi2, + writeLitEntropy, + writeSeqEntropy, + &litEntropyWritten, + &seqEntropyWritten, + lastBlock + ); { nuint err_code = cSize; if (ERR_isError(err_code)) @@ -525,10 +881,17 @@ namespace ZstdSharp.Unsafe if (writeLitEntropy != 0) { - memcpy(&nextCBlock->entropy.huf, &prevCBlock->entropy.huf, (uint)sizeof(ZSTD_hufCTables_t)); + memcpy( + &nextCBlock->entropy.huf, + &prevCBlock->entropy.huf, + (uint)sizeof(ZSTD_hufCTables_t) + ); } - if (writeSeqEntropy != 0 && ZSTD_needSequenceEntropyTables(&entropyMetadata->fseMetadata) != 0) + if ( + writeSeqEntropy != 0 + && ZSTD_needSequenceEntropyTables(&entropyMetadata->fseMetadata) != 0 + ) { return 0; } @@ -555,7 +918,11 @@ namespace ZstdSharp.Unsafe memcpy(&rep, prevCBlock->rep, (uint)sizeof(repcodes_s)); for (seq = sstart; seq < sp; ++seq) { - ZSTD_updateRep(rep.rep, seq->offBase, ZSTD_getSequenceLength(seqStorePtr, seq).litLength == 0 ? 1U : 0U); + ZSTD_updateRep( + rep.rep, + seq->offBase, + ZSTD_getSequenceLength(seqStorePtr, seq).litLength == 0 ? 1U : 0U + ); } memcpy(nextCBlock->rep, &rep, (uint)sizeof(repcodes_s)); @@ -568,18 +935,47 @@ namespace ZstdSharp.Unsafe /* 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. */ - private static nuint ZSTD_compressSuperBlock(ZSTD_CCtx_s* zc, void* dst, nuint dstCapacity, void* src, nuint srcSize, uint lastBlock) + private 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, zc->tmpWorkspace, zc->tmpWkspSize); + nuint err_code = ZSTD_buildBlockEntropyStats( + &zc->seqStore, + &zc->blockState.prevCBlock->entropy, + &zc->blockState.nextCBlock->entropy, + &zc->appliedParams, + &entropyMetadata, + zc->tmpWorkspace, + zc->tmpWkspSize + ); if (ERR_isError(err_code)) { 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, zc->tmpWorkspace, zc->tmpWkspSize); + return ZSTD_compressSubBlock_multi( + &zc->seqStore, + zc->blockState.prevCBlock, + zc->blockState.nextCBlock, + &entropyMetadata, + &zc->appliedParams, + dst, + dstCapacity, + src, + srcSize, + zc->bmi2, + lastBlock, + zc->tmpWorkspace, + zc->tmpWkspSize + ); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCwksp.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCwksp.cs index 8f6063a5..414e75db 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCwksp.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdCwksp.cs @@ -1,6 +1,6 @@ +using System.Diagnostics; using System.Runtime.CompilerServices; using static ZstdSharp.UnsafeHelper; -using System.Diagnostics; namespace ZstdSharp.Unsafe { @@ -143,12 +143,18 @@ namespace ZstdSharp.Unsafe * 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) + 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_aligned_init_once && phase >= ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned_init_once) + if ( + ws->phase < ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned_init_once + && phase >= ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned_init_once + ) { ws->tableValidEnd = ws->objectEnd; ws->initOnceStart = ZSTD_cwksp_initialAllocStart(ws); @@ -158,7 +164,9 @@ namespace ZstdSharp.Unsafe void* objectEnd = (byte*)alloc + bytesToAlign; if (objectEnd > ws->workspaceEnd) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); } ws->objectEnd = objectEnd; @@ -190,7 +198,11 @@ namespace ZstdSharp.Unsafe * 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) + 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)) || bytes == 0) @@ -208,7 +220,11 @@ namespace ZstdSharp.Unsafe [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); + return (byte*)ZSTD_cwksp_reserve_internal( + ws, + bytes, + ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_buffers + ); } /** @@ -224,11 +240,23 @@ namespace ZstdSharp.Unsafe private static void* ZSTD_cwksp_reserve_aligned_init_once(ZSTD_cwksp* ws, nuint bytes) { nuint alignedBytes = ZSTD_cwksp_align(bytes, 64); - void* ptr = ZSTD_cwksp_reserve_internal(ws, alignedBytes, ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned_init_once); + void* ptr = ZSTD_cwksp_reserve_internal( + ws, + alignedBytes, + ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned_init_once + ); assert(((nuint)ptr & 64 - 1) == 0); if (ptr != null && ptr < ws->initOnceStart) { - memset(ptr, 0, (uint)((nuint)((byte*)ws->initOnceStart - (byte*)ptr) < alignedBytes ? (nuint)((byte*)ws->initOnceStart - (byte*)ptr) : alignedBytes)); + memset( + ptr, + 0, + (uint)( + (nuint)((byte*)ws->initOnceStart - (byte*)ptr) < alignedBytes + ? (nuint)((byte*)ws->initOnceStart - (byte*)ptr) + : alignedBytes + ) + ); ws->initOnceStart = ptr; } @@ -241,7 +269,11 @@ namespace ZstdSharp.Unsafe [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void* ZSTD_cwksp_reserve_aligned64(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); + void* ptr = ZSTD_cwksp_reserve_internal( + ws, + ZSTD_cwksp_align(bytes, 64), + ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned + ); assert(((nuint)ptr & 64 - 1) == 0); return ptr; } @@ -254,7 +286,8 @@ namespace ZstdSharp.Unsafe [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_init_once; + ZSTD_cwksp_alloc_phase_e phase = + ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_aligned_init_once; void* alloc; void* end; void* top; @@ -297,7 +330,10 @@ namespace ZstdSharp.Unsafe assert((nuint)alloc % (nuint)sizeof(void*) == 0); assert(bytes % (nuint)sizeof(void*) == 0); ZSTD_cwksp_assert_internal_consistency(ws); - if (ws->phase != ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_objects || end > ws->workspaceEnd) + if ( + ws->phase != ZSTD_cwksp_alloc_phase_e.ZSTD_cwksp_alloc_objects + || end > ws->workspaceEnd + ) { ws->allocFailed = 1; return null; @@ -314,7 +350,11 @@ namespace ZstdSharp.Unsafe * Note : should happen only once, at workspace first initialization */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void* ZSTD_cwksp_reserve_object_aligned(ZSTD_cwksp* ws, nuint byteSize, nuint alignment) + private static void* ZSTD_cwksp_reserve_object_aligned( + ZSTD_cwksp* ws, + nuint byteSize, + nuint alignment + ) { nuint mask = alignment - 1; nuint surplus = alignment > (nuint)sizeof(void*) ? alignment - (nuint)sizeof(void*) : 0; @@ -359,7 +399,11 @@ namespace ZstdSharp.Unsafe assert(ws->tableValidEnd <= ws->allocStart); if (ws->tableValidEnd < ws->tableEnd) { - memset(ws->tableValidEnd, 0, (uint)(nuint)((byte*)ws->tableEnd - (byte*)ws->tableValidEnd)); + memset( + ws->tableValidEnd, + 0, + (uint)(nuint)((byte*)ws->tableEnd - (byte*)ws->tableValidEnd) + ); } ZSTD_cwksp_mark_tables_clean(ws); @@ -403,7 +447,8 @@ namespace ZstdSharp.Unsafe [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); + return (nuint)((byte*)ws->tableEnd - (byte*)ws->workspace) + + (nuint)((byte*)ws->workspaceEnd - (byte*)ws->allocStart); } /** @@ -412,7 +457,12 @@ namespace ZstdSharp.Unsafe * 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) + 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; @@ -436,7 +486,12 @@ namespace ZstdSharp.Unsafe 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); + ZSTD_cwksp_init( + ws, + workspace, + size, + ZSTD_cwksp_static_alloc_e.ZSTD_cwksp_dynamic_alloc + ); return 0; } @@ -470,9 +525,16 @@ namespace ZstdSharp.Unsafe * actual amount of space used. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int ZSTD_cwksp_estimated_space_within_bounds(ZSTD_cwksp* ws, nuint estimatedSpace) + private static int ZSTD_cwksp_estimated_space_within_bounds( + ZSTD_cwksp* ws, + nuint estimatedSpace + ) { - return estimatedSpace - ZSTD_cwksp_slack_space_required() <= ZSTD_cwksp_used(ws) && ZSTD_cwksp_used(ws) <= estimatedSpace ? 1 : 0; + return + estimatedSpace - ZSTD_cwksp_slack_space_required() <= ZSTD_cwksp_used(ws) + && ZSTD_cwksp_used(ws) <= estimatedSpace + ? 1 + : 0; } /*-************************************* @@ -499,11 +561,18 @@ namespace ZstdSharp.Unsafe [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; + 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) + private static void ZSTD_cwksp_bump_oversized_duration( + ZSTD_cwksp* ws, + nuint additionalNeededSpace + ) { if (ZSTD_cwksp_check_too_large(ws, additionalNeededSpace) != 0) { @@ -515,4 +584,4 @@ namespace ZstdSharp.Unsafe } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDdict.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDdict.cs index d7edbcd3..49d786a5 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDdict.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDdict.cs @@ -54,7 +54,10 @@ namespace ZstdSharp.Unsafe } } - private static nuint ZSTD_loadEntropy_intoDDict(ZSTD_DDict_s* ddict, ZSTD_dictContentType_e dictContentType) + private static nuint ZSTD_loadEntropy_intoDDict( + ZSTD_DDict_s* ddict, + ZSTD_dictContentType_e dictContentType + ) { ddict->dictID = 0; ddict->entropyPresent = 0; @@ -72,13 +75,17 @@ namespace ZstdSharp.Unsafe if (magic != 0xEC30A437) { if (dictContentType == ZSTD_dictContentType_e.ZSTD_dct_fullDict) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted) + ); return 0; } } ddict->dictID = MEM_readLE32((sbyte*)ddict->dictContent + 4); - if (ERR_isError(ZSTD_loadDEntropy(&ddict->entropy, ddict->dictContent, ddict->dictSize))) + if ( + ERR_isError(ZSTD_loadDEntropy(&ddict->entropy, ddict->dictContent, ddict->dictSize)) + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } @@ -87,9 +94,19 @@ namespace ZstdSharp.Unsafe return 0; } - private static nuint ZSTD_initDDict_internal(ZSTD_DDict_s* ddict, void* dict, nuint dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType) + 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) + if ( + dictLoadMethod == ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef + || dict == null + || dictSize == 0 + ) { ddict->dictBuffer = null; ddict->dictContent = dict; @@ -120,17 +137,35 @@ namespace ZstdSharp.Unsafe 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) + 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) + if ( + ((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) + != 0 + ) return null; { - ZSTD_DDict_s* ddict = (ZSTD_DDict_s*)ZSTD_customMalloc((nuint)sizeof(ZSTD_DDict_s), customMem); + ZSTD_DDict_s* ddict = (ZSTD_DDict_s*)ZSTD_customMalloc( + (nuint)sizeof(ZSTD_DDict_s), + customMem + ); if (ddict == null) return null; ddict->cMem = customMem; { - nuint initResult = ZSTD_initDDict_internal(ddict, dict, dictSize, dictLoadMethod, dictContentType); + nuint initResult = ZSTD_initDDict_internal( + ddict, + dict, + dictSize, + dictLoadMethod, + dictContentType + ); if (ERR_isError(initResult)) { ZSTD_freeDDict(ddict); @@ -152,9 +187,15 @@ namespace ZstdSharp.Unsafe { customAlloc = null, customFree = null, - opaque = null + opaque = null, }; - return ZSTD_createDDict_advanced(dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy, ZSTD_dictContentType_e.ZSTD_dct_auto, allocator); + return ZSTD_createDDict_advanced( + dict, + dictSize, + ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy, + ZSTD_dictContentType_e.ZSTD_dct_auto, + allocator + ); } /*! ZSTD_createDDict_byReference() : @@ -167,14 +208,29 @@ namespace ZstdSharp.Unsafe { customAlloc = null, customFree = null, - opaque = null + opaque = null, }; - return ZSTD_createDDict_advanced(dictBuffer, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, ZSTD_dictContentType_e.ZSTD_dct_auto, allocator); + 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) + 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); + 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); @@ -188,7 +244,17 @@ namespace ZstdSharp.Unsafe dict = ddict + 1; } - if (ERR_isError(ZSTD_initDDict_internal(ddict, dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, dictContentType))) + if ( + ERR_isError( + ZSTD_initDDict_internal( + ddict, + dict, + dictSize, + ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, + dictContentType + ) + ) + ) return null; return ddict; } @@ -211,9 +277,13 @@ namespace ZstdSharp.Unsafe /*! 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) + 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); + 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) @@ -234,4 +304,4 @@ namespace ZstdSharp.Unsafe return ddict->dictID; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompress.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompress.cs index 65454a56..737aae94 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompress.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompress.cs @@ -17,7 +17,10 @@ namespace ZstdSharp.Unsafe * 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) + 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); @@ -48,10 +51,16 @@ namespace ZstdSharp.Unsafe * 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) + 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** 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; @@ -82,7 +91,10 @@ namespace ZstdSharp.Unsafe /* 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) + 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; @@ -109,10 +121,16 @@ namespace ZstdSharp.Unsafe */ private static ZSTD_DDictHashSet* ZSTD_createDDictHashSet(ZSTD_customMem customMem) { - ZSTD_DDictHashSet* ret = (ZSTD_DDictHashSet*)ZSTD_customMalloc((nuint)sizeof(ZSTD_DDictHashSet), customMem); + ZSTD_DDictHashSet* ret = (ZSTD_DDictHashSet*)ZSTD_customMalloc( + (nuint)sizeof(ZSTD_DDictHashSet), + customMem + ); if (ret == null) return null; - ret->ddictPtrTable = (ZSTD_DDict_s**)ZSTD_customCalloc((nuint)(64 * sizeof(ZSTD_DDict_s*)), customMem); + ret->ddictPtrTable = (ZSTD_DDict_s**)ZSTD_customCalloc( + (nuint)(64 * sizeof(ZSTD_DDict_s*)), + customMem + ); if (ret->ddictPtrTable == null) { ZSTD_customFree(ret, customMem); @@ -127,7 +145,10 @@ namespace ZstdSharp.Unsafe /* 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) + private static void ZSTD_freeDDictHashSet( + ZSTD_DDictHashSet* hashSet, + ZSTD_customMem customMem + ) { if (hashSet != null && hashSet->ddictPtrTable != null) { @@ -143,7 +164,11 @@ namespace ZstdSharp.Unsafe /* 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) + private static nuint ZSTD_DDictHashSet_addDDict( + ZSTD_DDictHashSet* hashSet, + ZSTD_DDict_s* ddict, + ZSTD_customMem customMem + ) { if (hashSet->ddictPtrCount * 4 / hashSet->ddictPtrTableSize * 3 != 0) { @@ -172,7 +197,10 @@ namespace ZstdSharp.Unsafe { if (dctx == null) return 0; - return (nuint)sizeof(ZSTD_DCtx_s) + ZSTD_sizeof_DDict(dctx->ddictLocal) + dctx->inBuffSize + dctx->outBuffSize; + return (nuint)sizeof(ZSTD_DCtx_s) + + ZSTD_sizeof_DDict(dctx->ddictLocal) + + dctx->inBuffSize + + dctx->outBuffSize; } public static nuint ZSTD_estimateDCtxSize() @@ -183,7 +211,10 @@ namespace ZstdSharp.Unsafe 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); + assert( + format == ZSTD_format_e.ZSTD_f_zstd1 + || format == ZSTD_format_e.ZSTD_f_zstd1_magicless + ); return startingInputLength; } @@ -233,10 +264,16 @@ namespace ZstdSharp.Unsafe private static ZSTD_DCtx_s* ZSTD_createDCtx_internal(ZSTD_customMem customMem) { - if (((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) != 0) + if ( + ((customMem.customAlloc == null ? 1 : 0) ^ (customMem.customFree == null ? 1 : 0)) + != 0 + ) return null; { - ZSTD_DCtx_s* dctx = (ZSTD_DCtx_s*)ZSTD_customMalloc((nuint)sizeof(ZSTD_DCtx_s), customMem); + ZSTD_DCtx_s* dctx = (ZSTD_DCtx_s*)ZSTD_customMalloc( + (nuint)sizeof(ZSTD_DCtx_s), + customMem + ); if (dctx == null) return null; dctx->customMem = customMem; @@ -308,7 +345,10 @@ namespace ZstdSharp.Unsafe assert(dctx->refMultipleDDicts != default && dctx->ddictSet != null); if (dctx->ddict != null) { - ZSTD_DDict_s* frameDDict = ZSTD_DDictHashSet_getDDict(dctx->ddictSet, dctx->fParams.dictID); + ZSTD_DDict_s* frameDDict = ZSTD_DDictHashSet_getDDict( + dctx->ddictSet, + dctx->fParams.dictID + ); if (frameDDict != null) { ZSTD_clearDict(dctx); @@ -361,7 +401,11 @@ namespace ZstdSharp.Unsafe * 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) + private static nuint ZSTD_frameHeaderSize_internal( + void* src, + nuint srcSize, + ZSTD_format_e format + ) { nuint minInputSize = ZSTD_startingInputLength(format); if (srcSize < minInputSize) @@ -374,7 +418,11 @@ namespace ZstdSharp.Unsafe uint dictID = (uint)(fhd & 3); uint singleSegment = (uint)(fhd >> 5 & 1); uint fcsId = (uint)(fhd >> 6); - return minInputSize + (nuint)(singleSegment == 0 ? 1 : 0) + ZSTD_did_fieldSize[dictID] + ZSTD_fcs_fieldSize[fcsId] + (nuint)(singleSegment != 0 && fcsId == 0 ? 1 : 0); + return minInputSize + + (nuint)(singleSegment == 0 ? 1 : 0) + + ZSTD_did_fieldSize[dictID] + + ZSTD_fcs_fieldSize[fcsId] + + (nuint)(singleSegment != 0 && fcsId == 0 ? 1 : 0); } } @@ -393,7 +441,12 @@ namespace ZstdSharp.Unsafe * @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) + 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); @@ -424,7 +477,9 @@ namespace ZstdSharp.Unsafe memcpy(hbuf, src, (uint)toCopy); if ((MEM_readLE32(hbuf) & 0xFFFFFFF0) != 0x184D2A50) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_prefix_unknown)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_prefix_unknown) + ); } } } @@ -444,7 +499,7 @@ namespace ZstdSharp.Unsafe frameType = ZSTD_frameType_e.ZSTD_skippableFrame, dictID = MEM_readLE32(src) - 0x184D2A50, headerSize = 8, - frameContentSize = MEM_readLE32((sbyte*)src + 4) + frameContentSize = MEM_readLE32((sbyte*)src + 4), }; return 0; } @@ -471,7 +526,9 @@ namespace ZstdSharp.Unsafe ulong frameContentSize = unchecked(0UL - 1); if ((fhdByte & 0x08) != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported) + ); } if (singleSegment == 0) @@ -480,7 +537,9 @@ namespace ZstdSharp.Unsafe uint windowLog = (uint)((wlByte >> 3) + 10); if (windowLog > (uint)(sizeof(nuint) == 4 ? 30 : 31)) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge) + ); } windowSize = 1UL << (int)windowLog; @@ -584,7 +643,9 @@ namespace ZstdSharp.Unsafe sizeU32 = MEM_readLE32((byte*)src + 4); if (sizeU32 + 8 < sizeU32) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported) + ); } { @@ -609,7 +670,13 @@ namespace ZstdSharp.Unsafe * * @return : number of bytes written or a ZSTD error. */ - public static nuint ZSTD_readSkippableFrame(void* dst, nuint dstCapacity, uint* magicVariant, void* src, nuint srcSize) + public static nuint ZSTD_readSkippableFrame( + void* dst, + nuint dstCapacity, + uint* magicVariant, + void* src, + nuint srcSize + ) { if (srcSize < 8) { @@ -622,7 +689,9 @@ namespace ZstdSharp.Unsafe nuint skippableContentSize = skippableFrameSize - 8; if (ZSTD_isSkippableFrame(src, srcSize) == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_unsupported) + ); } if (skippableFrameSize < 8 || skippableFrameSize > srcSize) @@ -709,7 +778,12 @@ namespace ZstdSharp.Unsafe * @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); + nuint result = ZSTD_getFrameHeader_advanced( + &dctx->fParams, + src, + headerSize, + dctx->format + ); if (ERR_isError(result)) return result; if (result > 0) @@ -717,7 +791,10 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)); } - if (dctx->refMultipleDDicts == ZSTD_refMultipleDDicts_e.ZSTD_rmd_refMultipleDDicts && dctx->ddictSet != null) + if ( + dctx->refMultipleDDicts == ZSTD_refMultipleDDicts_e.ZSTD_rmd_refMultipleDDicts + && dctx->ddictSet != null + ) { ZSTD_DCtx_selectFrameDDict(dctx); } @@ -727,7 +804,9 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_wrong)); } - dctx->validateChecksum = (uint)(dctx->fParams.checksumFlag != 0 && dctx->forceIgnoreChecksum == default ? 1 : 0); + dctx->validateChecksum = (uint)( + dctx->fParams.checksumFlag != 0 && dctx->forceIgnoreChecksum == default ? 1 : 0 + ); if (dctx->validateChecksum != 0) ZSTD_XXH64_reset(&dctx->xxhState, 0); dctx->processedCSize += headerSize; @@ -743,14 +822,25 @@ namespace ZstdSharp.Unsafe return frameSizeInfo; } - private static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(void* src, nuint srcSize, ZSTD_format_e format) + private static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo( + void* src, + nuint srcSize, + ZSTD_format_e format + ) { ZSTD_frameSizeInfo frameSizeInfo; frameSizeInfo = new ZSTD_frameSizeInfo(); - if (format == ZSTD_format_e.ZSTD_f_zstd1 && srcSize >= 8 && (MEM_readLE32(src) & 0xFFFFFFF0) == 0x184D2A50) + if ( + format == ZSTD_format_e.ZSTD_f_zstd1 + && srcSize >= 8 + && (MEM_readLE32(src) & 0xFFFFFFF0) == 0x184D2A50 + ) { frameSizeInfo.compressedSize = readSkippableFrameSize(src, srcSize); - assert(ERR_isError(frameSizeInfo.compressedSize) || frameSizeInfo.compressedSize <= srcSize); + assert( + ERR_isError(frameSizeInfo.compressedSize) + || frameSizeInfo.compressedSize <= srcSize + ); return frameSizeInfo; } else @@ -765,7 +855,9 @@ namespace ZstdSharp.Unsafe if (ERR_isError(ret)) return ZSTD_errorFrameSizeInfo(ret); if (ret > 0) - return ZSTD_errorFrameSizeInfo(unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + return ZSTD_errorFrameSizeInfo( + unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)) + ); } ip += zfh.headerSize; @@ -777,7 +869,9 @@ namespace ZstdSharp.Unsafe if (ERR_isError(cBlockSize)) return ZSTD_errorFrameSizeInfo(cBlockSize); if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) - return ZSTD_errorFrameSizeInfo(unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + return ZSTD_errorFrameSizeInfo( + unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)) + ); ip += ZSTD_blockHeaderSize + cBlockSize; remainingSize -= ZSTD_blockHeaderSize + cBlockSize; nbBlocks++; @@ -788,18 +882,27 @@ namespace ZstdSharp.Unsafe if (zfh.checksumFlag != 0) { if (remainingSize < 4) - return ZSTD_errorFrameSizeInfo(unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong))); + return ZSTD_errorFrameSizeInfo( + unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)) + ); ip += 4; } frameSizeInfo.nbBlocks = nbBlocks; frameSizeInfo.compressedSize = (nuint)(ip - ipstart); - frameSizeInfo.decompressedBound = zfh.frameContentSize != unchecked(0UL - 1) ? zfh.frameContentSize : (ulong)nbBlocks * zfh.blockSizeMax; + frameSizeInfo.decompressedBound = + zfh.frameContentSize != unchecked(0UL - 1) + ? zfh.frameContentSize + : (ulong)nbBlocks * zfh.blockSizeMax; return frameSizeInfo; } } - private static nuint ZSTD_findFrameCompressedSize_advanced(void* src, nuint srcSize, ZSTD_format_e format) + private static nuint ZSTD_findFrameCompressedSize_advanced( + void* src, + nuint srcSize, + ZSTD_format_e format + ) { ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, format); return frameSizeInfo.compressedSize; @@ -824,7 +927,11 @@ namespace ZstdSharp.Unsafe ulong bound = 0; while (srcSize > 0) { - ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_format_e.ZSTD_f_zstd1); + ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo( + src, + srcSize, + ZSTD_format_e.ZSTD_f_zstd1 + ); nuint compressedSize = frameSizeInfo.compressedSize; ulong decompressedBound = frameSizeInfo.decompressedBound; if (ERR_isError(compressedSize) || decompressedBound == unchecked(0UL - 2)) @@ -867,7 +974,11 @@ namespace ZstdSharp.Unsafe uint maxBlockSize = 0; while (srcSize > 0) { - ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize, ZSTD_format_e.ZSTD_f_zstd1); + ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo( + src, + srcSize, + ZSTD_format_e.ZSTD_f_zstd1 + ); nuint compressedSize = frameSizeInfo.compressedSize; ulong decompressedBound = frameSizeInfo.decompressedBound; ZSTD_frameHeader zfh; @@ -886,7 +997,8 @@ namespace ZstdSharp.Unsafe margin += zfh.headerSize; margin += (nuint)(zfh.checksumFlag != 0 ? 4 : 0); margin += 3 * frameSizeInfo.nbBlocks; - maxBlockSize = maxBlockSize > zfh.blockSizeMax ? maxBlockSize : zfh.blockSizeMax; + maxBlockSize = + maxBlockSize > zfh.blockSizeMax ? maxBlockSize : zfh.blockSizeMax; } else { @@ -912,7 +1024,12 @@ namespace ZstdSharp.Unsafe return blockSize; } - private static nuint ZSTD_copyRawBlock(void* dst, nuint dstCapacity, void* src, nuint srcSize) + private static nuint ZSTD_copyRawBlock( + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { if (srcSize > dstCapacity) { @@ -948,15 +1065,24 @@ namespace ZstdSharp.Unsafe return regenSize; } - private static void ZSTD_DCtx_trace_end(ZSTD_DCtx_s* dctx, ulong uncompressedSize, ulong compressedSize, int streaming) - { - } + private static void ZSTD_DCtx_trace_end( + ZSTD_DCtx_s* dctx, + ulong uncompressedSize, + ulong compressedSize, + int 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) + private static nuint ZSTD_decompressFrame( + ZSTD_DCtx_s* dctx, + void* dst, + nuint dstCapacity, + void** srcPtr, + nuint* srcSizePtr + ) { byte* istart = (byte*)*srcPtr; byte* ip = istart; @@ -964,13 +1090,20 @@ namespace ZstdSharp.Unsafe byte* oend = dstCapacity != 0 ? ostart + dstCapacity : ostart; byte* op = ostart; nuint remainingSrcSize = *srcSizePtr; - if (remainingSrcSize < (nuint)(dctx->format == ZSTD_format_e.ZSTD_f_zstd1 ? 6 : 2) + ZSTD_blockHeaderSize) + if ( + remainingSrcSize + < (nuint)(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(ip, (nuint)(dctx->format == ZSTD_format_e.ZSTD_f_zstd1 ? 5 : 1), dctx->format); + nuint frameHeaderSize = ZSTD_frameHeaderSize_internal( + ip, + (nuint)(dctx->format == ZSTD_format_e.ZSTD_f_zstd1 ? 5 : 1), + dctx->format + ); if (ERR_isError(frameHeaderSize)) return frameHeaderSize; if (remainingSrcSize < frameHeaderSize + ZSTD_blockHeaderSize) @@ -991,7 +1124,10 @@ namespace ZstdSharp.Unsafe } if (dctx->maxBlockSizeParam != 0) - dctx->fParams.blockSizeMax = dctx->fParams.blockSizeMax < (uint)dctx->maxBlockSizeParam ? dctx->fParams.blockSizeMax : (uint)dctx->maxBlockSizeParam; + dctx->fParams.blockSizeMax = + dctx->fParams.blockSizeMax < (uint)dctx->maxBlockSizeParam + ? dctx->fParams.blockSizeMax + : (uint)dctx->maxBlockSizeParam; while (true) { byte* oBlockEnd = oend; @@ -1016,17 +1152,31 @@ namespace ZstdSharp.Unsafe { case blockType_e.bt_compressed: assert(dctx->isFrameDecompression == 1); - decodedSize = ZSTD_decompressBlock_internal(dctx, op, (nuint)(oBlockEnd - op), ip, cBlockSize, streaming_operation.not_streaming); + decodedSize = ZSTD_decompressBlock_internal( + dctx, + op, + (nuint)(oBlockEnd - op), + ip, + cBlockSize, + streaming_operation.not_streaming + ); break; case blockType_e.bt_raw: decodedSize = ZSTD_copyRawBlock(op, (nuint)(oend - op), ip, cBlockSize); break; case blockType_e.bt_rle: - decodedSize = ZSTD_setRleBlock(op, (nuint)(oBlockEnd - op), *ip, blockProperties.origSize); + decodedSize = ZSTD_setRleBlock( + op, + (nuint)(oBlockEnd - op), + *ip, + blockProperties.origSize + ); break; case blockType_e.bt_reserved: default: - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } { @@ -1090,7 +1240,16 @@ namespace ZstdSharp.Unsafe 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) + 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; @@ -1148,7 +1307,10 @@ namespace ZstdSharp.Unsafe 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) + if ( + ZSTD_getErrorCode(res) == ZSTD_ErrorCode.ZSTD_error_prefix_unknown + && moreThan1Frame == 1 + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_srcSize_wrong)); } @@ -1178,9 +1340,26 @@ namespace ZstdSharp.Unsafe * 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) + 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, null); + return ZSTD_decompressMultiFrame( + dctx, + dst, + dstCapacity, + src, + srcSize, + dict, + dictSize, + null + ); } private static ZSTD_DDict_s* ZSTD_getDDict(ZSTD_DCtx_s* dctx) @@ -1206,9 +1385,22 @@ namespace ZstdSharp.Unsafe * requires an allocated ZSTD_DCtx. * Compatible with sticky parameters (see below). */ - public static nuint ZSTD_decompressDCtx(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + 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)); + return ZSTD_decompress_usingDDict( + dctx, + dst, + dstCapacity, + src, + srcSize, + ZSTD_getDDict(dctx) + ); } /*! ZSTD_decompress() : @@ -1253,13 +1445,23 @@ namespace ZstdSharp.Unsafe * * @param inputSize - The total amount of input that the caller currently has. */ - private static nuint ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx_s* dctx, nuint inputSize) + private static nuint ZSTD_nextSrcSizeToDecompressWithInputSize( + ZSTD_DCtx_s* dctx, + nuint inputSize + ) { - if (!(dctx->stage == ZSTD_dStage.ZSTDds_decompressBlock || dctx->stage == ZSTD_dStage.ZSTDds_decompressLastBlock)) + 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 ? 1 : inputSize <= dctx->expected ? inputSize : dctx->expected; + return inputSize <= 1 ? 1 + : inputSize <= dctx->expected ? inputSize + : dctx->expected; } public static ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx_s* dctx) @@ -1295,7 +1497,13 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_decompressContinue( + ZSTD_DCtx_s* dctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { if (srcSize != ZSTD_nextSrcSizeToDecompressWithInputSize(dctx, srcSize)) { @@ -1330,8 +1538,13 @@ namespace ZstdSharp.Unsafe case ZSTD_dStage.ZSTDds_decodeFrameHeader: assert(src != null); memcpy(dctx->headerBuffer + (dctx->headerSize - srcSize), src, (uint)srcSize); + { - nuint err_code = ZSTD_decodeFrameHeader(dctx, dctx->headerBuffer, dctx->headerSize); + nuint err_code = ZSTD_decodeFrameHeader( + dctx, + dctx->headerBuffer, + dctx->headerSize + ); if (ERR_isError(err_code)) { return err_code; @@ -1342,133 +1555,156 @@ namespace ZstdSharp.Unsafe 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)) + return cBlockSize; + if (cBlockSize > dctx->fParams.blockSizeMax) { - blockProperties_t bp; - nuint cBlockSize = ZSTD_getcBlockSize(src, ZSTD_blockHeaderSize, &bp); - if (ERR_isError(cBlockSize)) - 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 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; } - case ZSTD_dStage.ZSTDds_decompressLastBlock: - case ZSTD_dStage.ZSTDds_decompressBlock: + if (bp.lastBlock != 0) { - nuint rSize; - switch (dctx->bType) + if (dctx->fParams.checksumFlag != 0) { - case blockType_e.bt_compressed: - assert(dctx->isFrameDecompression == 1); - rSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, streaming_operation.is_streaming); - 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)) - { - 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)) - { - return err_code; - } - } - - if (rSize > dctx->fParams.blockSizeMax) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); - } - - dctx->decodedSize += rSize; - if (dctx->validateChecksum != 0) - ZSTD_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; - } + dctx->expected = 4; + dctx->stage = ZSTD_dStage.ZSTDds_checkChecksum; } else { - dctx->stage = ZSTD_dStage.ZSTDds_decodeBlockHeader; - dctx->expected = ZSTD_blockHeaderSize; + 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: + assert(dctx->isFrameDecompression == 1); + rSize = ZSTD_decompressBlock_internal( + dctx, + dst, + dstCapacity, + src, + srcSize, + streaming_operation.is_streaming + ); + 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)) + { + 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)) + { + return err_code; + } + } + + if (rSize > dctx->fParams.blockSizeMax) + { + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); + } + + dctx->decodedSize += rSize; + if (dctx->validateChecksum != 0) + ZSTD_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) { @@ -1476,7 +1712,9 @@ namespace ZstdSharp.Unsafe uint check32 = MEM_readLE32(src); if (check32 != h32) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_checksum_wrong)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_checksum_wrong) + ); } } @@ -1507,7 +1745,8 @@ namespace ZstdSharp.Unsafe 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->virtualStart = + (sbyte*)dict - ((sbyte*)dctx->previousDstEnd - (sbyte*)dctx->prefixStart); dctx->prefixStart = dict; dctx->previousDstEnd = (sbyte*)dict + dictSize; return 0; @@ -1516,7 +1755,11 @@ namespace ZstdSharp.Unsafe /*! ZSTD_loadDEntropy() : * dict : must point at beginning of a valid zstd dictionary. * @return : size of entropy tables read */ - private static nuint ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy, void* dict, nuint dictSize) + private static nuint ZSTD_loadDEntropy( + ZSTD_entropyDTables_t* entropy, + void* dict, + nuint dictSize + ) { byte* dictPtr = (byte*)dict; byte* dictEnd = dictPtr + dictSize; @@ -1530,8 +1773,19 @@ namespace ZstdSharp.Unsafe { /* use fse tables as temporary workspace; implies fse tables are grouped together */ void* workspace = &entropy->LLTable; - nuint workspaceSize = (nuint)(sizeof(ZSTD_seqSymbol) * 513 + sizeof(ZSTD_seqSymbol) * 257 + sizeof(ZSTD_seqSymbol) * 513); - nuint hSize = HUF_readDTableX2_wksp(entropy->hufTable, dictPtr, (nuint)(dictEnd - dictPtr), workspace, workspaceSize, 0); + nuint workspaceSize = (nuint)( + sizeof(ZSTD_seqSymbol) * 513 + + sizeof(ZSTD_seqSymbol) * 257 + + sizeof(ZSTD_seqSymbol) * 513 + ); + nuint hSize = HUF_readDTableX2_wksp( + entropy->hufTable, + dictPtr, + (nuint)(dictEnd - dictPtr), + workspace, + workspaceSize, + 0 + ); if (ERR_isError(hSize)) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); @@ -1542,8 +1796,15 @@ namespace ZstdSharp.Unsafe { short* offcodeNCount = stackalloc short[32]; - uint offcodeMaxValue = 31, offcodeLog; - nuint offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, (nuint)(dictEnd - dictPtr)); + uint offcodeMaxValue = 31, + offcodeLog; + nuint offcodeHeaderSize = FSE_readNCount( + offcodeNCount, + &offcodeMaxValue, + &offcodeLog, + dictPtr, + (nuint)(dictEnd - dictPtr) + ); if (ERR_isError(offcodeHeaderSize)) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); @@ -1559,14 +1820,31 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } - ZSTD_buildFSETable(&entropy->OFTable.e0, offcodeNCount, offcodeMaxValue, OF_base, OF_bits, offcodeLog, entropy->workspace, sizeof(uint) * 157, 0); + ZSTD_buildFSETable( + &entropy->OFTable.e0, + offcodeNCount, + offcodeMaxValue, + OF_base, + OF_bits, + offcodeLog, + entropy->workspace, + sizeof(uint) * 157, + 0 + ); dictPtr += offcodeHeaderSize; } { short* matchlengthNCount = stackalloc short[53]; - uint matchlengthMaxValue = 52, matchlengthLog; - nuint matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, (nuint)(dictEnd - dictPtr)); + uint matchlengthMaxValue = 52, + matchlengthLog; + nuint matchlengthHeaderSize = FSE_readNCount( + matchlengthNCount, + &matchlengthMaxValue, + &matchlengthLog, + dictPtr, + (nuint)(dictEnd - dictPtr) + ); if (ERR_isError(matchlengthHeaderSize)) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); @@ -1582,14 +1860,31 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } - ZSTD_buildFSETable(&entropy->MLTable.e0, matchlengthNCount, matchlengthMaxValue, ML_base, ML_bits, matchlengthLog, entropy->workspace, sizeof(uint) * 157, 0); + ZSTD_buildFSETable( + &entropy->MLTable.e0, + matchlengthNCount, + matchlengthMaxValue, + ML_base, + ML_bits, + matchlengthLog, + entropy->workspace, + sizeof(uint) * 157, + 0 + ); dictPtr += matchlengthHeaderSize; } { short* litlengthNCount = stackalloc short[36]; - uint litlengthMaxValue = 35, litlengthLog; - nuint litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, (nuint)(dictEnd - dictPtr)); + uint litlengthMaxValue = 35, + litlengthLog; + nuint litlengthHeaderSize = FSE_readNCount( + litlengthNCount, + &litlengthMaxValue, + &litlengthLog, + dictPtr, + (nuint)(dictEnd - dictPtr) + ); if (ERR_isError(litlengthHeaderSize)) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); @@ -1605,7 +1900,17 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); } - ZSTD_buildFSETable(&entropy->LLTable.e0, litlengthNCount, litlengthMaxValue, LL_base, LL_bits, litlengthLog, entropy->workspace, sizeof(uint) * 157, 0); + ZSTD_buildFSETable( + &entropy->LLTable.e0, + litlengthNCount, + litlengthMaxValue, + LL_base, + LL_bits, + litlengthLog, + entropy->workspace, + sizeof(uint) * 157, + 0 + ); dictPtr += litlengthHeaderSize; } @@ -1623,7 +1928,9 @@ namespace ZstdSharp.Unsafe dictPtr += 4; if (rep == 0 || rep > dictContentSize) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted) + ); } entropy->rep[i] = rep; @@ -1633,7 +1940,11 @@ namespace ZstdSharp.Unsafe return (nuint)(dictPtr - (byte*)dict); } - private static nuint ZSTD_decompress_insertDictionary(ZSTD_DCtx_s* dctx, void* dict, nuint dictSize) + private static nuint ZSTD_decompress_insertDictionary( + ZSTD_DCtx_s* dctx, + void* dict, + nuint dictSize + ) { if (dictSize < 8) return ZSTD_refDictContent(dctx, dict, dictSize); @@ -1685,7 +1996,11 @@ namespace ZstdSharp.Unsafe return 0; } - public static nuint ZSTD_decompressBegin_usingDict(ZSTD_DCtx_s* dctx, void* dict, nuint dictSize) + public static nuint ZSTD_decompressBegin_usingDict( + ZSTD_DCtx_s* dctx, + void* dict, + nuint dictSize + ) { { nuint err_code = ZSTD_decompressBegin(dctx); @@ -1770,7 +2085,7 @@ namespace ZstdSharp.Unsafe dictID = 0, checksumFlag = 0, _reserved1 = 0, - _reserved2 = 0 + _reserved2 = 0, }; nuint hError = ZSTD_getFrameHeader(&zfp, src, srcSize); if (ERR_isError(hError)) @@ -1781,7 +2096,14 @@ namespace ZstdSharp.Unsafe /*! 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) + 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); } @@ -1825,7 +2147,13 @@ namespace ZstdSharp.Unsafe * 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) + 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) { @@ -1835,7 +2163,13 @@ namespace ZstdSharp.Unsafe ZSTD_clearDict(dctx); if (dict != null && dictSize != 0) { - dctx->ddictLocal = ZSTD_createDDict_advanced(dict, dictSize, dictLoadMethod, dictContentType, dctx->customMem); + 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)); @@ -1853,9 +2187,19 @@ namespace ZstdSharp.Unsafe * 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) + 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); + 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+ @@ -1875,16 +2219,33 @@ namespace ZstdSharp.Unsafe */ 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); + 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) + 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); + nuint err_code = ZSTD_DCtx_loadDictionary_advanced( + dctx, + prefix, + prefixSize, + ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, + dictContentType + ); if (ERR_isError(err_code)) { return err_code; @@ -1913,7 +2274,12 @@ namespace ZstdSharp.Unsafe */ 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); + return ZSTD_DCtx_refPrefix_advanced( + dctx, + prefix, + prefixSize, + ZSTD_dictContentType_e.ZSTD_dct_rawContent + ); } /* ZSTD_initDStream_usingDict() : @@ -2038,13 +2404,19 @@ namespace ZstdSharp.Unsafe dctx->ddictSet = ZSTD_createDDictHashSet(dctx->customMem); if (dctx->ddictSet == null) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + 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); + nuint err_code = ZSTD_DDictHashSet_addDDict( + dctx->ddictSet, + ddict, + dctx->customMem + ); if (ERR_isError(err_code)) { return err_code; @@ -2091,7 +2463,11 @@ namespace ZstdSharp.Unsafe * @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); + return ZSTD_DCtx_setParameter( + dctx, + ZSTD_dParameter.ZSTD_d_experimentalParam1, + (int)format + ); } /*! ZSTD_dParam_getBounds() : @@ -2107,7 +2483,7 @@ namespace ZstdSharp.Unsafe { error = 0, lowerBound = 0, - upperBound = 0 + upperBound = 0, }; switch (dParam) { @@ -2143,7 +2519,9 @@ namespace ZstdSharp.Unsafe break; } - bounds.error = unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + bounds.error = unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); return bounds; } @@ -2167,7 +2545,11 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_DCtx_getParameter( + ZSTD_DCtx_s* dctx, + ZSTD_dParameter param, + int* value + ) { switch (param) { @@ -2206,7 +2588,11 @@ namespace ZstdSharp.Unsafe * 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) + public static nuint ZSTD_DCtx_setParameter( + ZSTD_DCtx_s* dctx, + ZSTD_dParameter dParam, + int value + ) { if (dctx->streamStage != ZSTD_dStreamStage.zdss_init) { @@ -2218,10 +2604,16 @@ namespace ZstdSharp.Unsafe case ZSTD_dParameter.ZSTD_d_windowLogMax: if (value == 0) value = 27; + { - if (ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_windowLogMax, value) == 0) + if ( + ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_windowLogMax, value) + == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } } @@ -2229,9 +2621,16 @@ namespace ZstdSharp.Unsafe return 0; case ZSTD_dParameter.ZSTD_d_experimentalParam1: { - if (ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam1, value) == 0) + if ( + ZSTD_dParam_withinBounds( + ZSTD_dParameter.ZSTD_d_experimentalParam1, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } } @@ -2239,9 +2638,16 @@ namespace ZstdSharp.Unsafe return 0; case ZSTD_dParameter.ZSTD_d_experimentalParam2: { - if (ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam2, value) == 0) + if ( + ZSTD_dParam_withinBounds( + ZSTD_dParameter.ZSTD_d_experimentalParam2, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } } @@ -2249,9 +2655,16 @@ namespace ZstdSharp.Unsafe return 0; case ZSTD_dParameter.ZSTD_d_experimentalParam3: { - if (ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam3, value) == 0) + if ( + ZSTD_dParam_withinBounds( + ZSTD_dParameter.ZSTD_d_experimentalParam3, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } } @@ -2259,24 +2672,40 @@ namespace ZstdSharp.Unsafe return 0; case ZSTD_dParameter.ZSTD_d_experimentalParam4: { - if (ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam4, value) == 0) + if ( + ZSTD_dParam_withinBounds( + ZSTD_dParameter.ZSTD_d_experimentalParam4, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } } if (dctx->staticSize != 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_unsupported) + ); } dctx->refMultipleDDicts = (ZSTD_refMultipleDDicts_e)value; return 0; case ZSTD_dParameter.ZSTD_d_experimentalParam5: { - if (ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam5, value) == 0) + if ( + ZSTD_dParam_withinBounds( + ZSTD_dParameter.ZSTD_d_experimentalParam5, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } } @@ -2285,9 +2714,16 @@ namespace ZstdSharp.Unsafe case ZSTD_dParameter.ZSTD_d_experimentalParam6: if (value != 0) { - if (ZSTD_dParam_withinBounds(ZSTD_dParameter.ZSTD_d_experimentalParam6, value) == 0) + if ( + ZSTD_dParam_withinBounds( + ZSTD_dParameter.ZSTD_d_experimentalParam6, + value + ) == 0 + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_parameter_outOfBound) + ); } } @@ -2308,14 +2744,20 @@ namespace ZstdSharp.Unsafe */ 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) + 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; dctx->isFrameDecompression = 1; } - if (reset == ZSTD_ResetDirective.ZSTD_reset_parameters || reset == ZSTD_ResetDirective.ZSTD_reset_session_and_parameters) + if ( + reset == ZSTD_ResetDirective.ZSTD_reset_parameters + || reset == ZSTD_ResetDirective.ZSTD_reset_session_and_parameters + ) { if (dctx->streamStage != ZSTD_dStreamStage.zdss_init) { @@ -2334,9 +2776,16 @@ namespace ZstdSharp.Unsafe return ZSTD_sizeof_DCtx(dctx); } - private static nuint ZSTD_decodingBufferSize_internal(ulong windowSize, ulong frameContentSize, nuint blockSizeMax) + private static nuint ZSTD_decodingBufferSize_internal( + ulong windowSize, + ulong frameContentSize, + nuint blockSizeMax + ) { - nuint blockSize = (nuint)(windowSize < 1 << 17 ? windowSize : 1 << 17) < blockSizeMax ? (nuint)(windowSize < 1 << 17 ? windowSize : 1 << 17) : blockSizeMax; + nuint blockSize = + (nuint)(windowSize < 1 << 17 ? windowSize : 1 << 17) < blockSizeMax + ? (nuint)(windowSize < 1 << 17 ? windowSize : 1 << 17) + : blockSizeMax; /* We need blockSize + WILDCOPY_OVERLENGTH worth of buffer so that if a block * ends at windowSize + WILDCOPY_OVERLENGTH + 1 bytes, we can start writing * the block at the beginning of the output buffer, and maintain a full window. @@ -2349,7 +2798,9 @@ namespace ZstdSharp.Unsafe nuint minRBSize = (nuint)neededSize; if (minRBSize != neededSize) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge) + ); } return minRBSize; @@ -2385,19 +2836,31 @@ namespace ZstdSharp.Unsafe if (zfh.windowSize > windowSizeMax) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge)); + 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) + private static int ZSTD_DCtx_isOverflow( + ZSTD_DCtx_s* zds, + nuint neededInBuffSize, + nuint neededOutBuffSize + ) { - return zds->inBuffSize + zds->outBuffSize >= (neededInBuffSize + neededOutBuffSize) * 3 ? 1 : 0; + return zds->inBuffSize + zds->outBuffSize >= (neededInBuffSize + neededOutBuffSize) * 3 + ? 1 + : 0; } - private static void ZSTD_DCtx_updateOversizedDuration(ZSTD_DCtx_s* zds, nuint neededInBuffSize, nuint neededOutBuffSize) + private static void ZSTD_DCtx_updateOversizedDuration( + ZSTD_DCtx_s* zds, + nuint neededInBuffSize, + nuint neededOutBuffSize + ) { if (ZSTD_DCtx_isOverflow(zds, neededInBuffSize, neededOutBuffSize) != 0) zds->oversizedDuration++; @@ -2418,7 +2881,11 @@ namespace ZstdSharp.Unsafe return 0; if (zds->streamStage == ZSTD_dStreamStage.zdss_init) return 0; - if (expect.dst == output->dst && expect.pos == output->pos && expect.size == output->size) + 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)); } @@ -2428,13 +2895,25 @@ namespace ZstdSharp.Unsafe * 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) + 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, zds->outBuff + zds->outStart, dstSize, src, srcSize); + nuint decodedSize = ZSTD_decompressContinue( + zds, + zds->outBuff + zds->outStart, + dstSize, + src, + srcSize + ); { nuint err_code = decodedSize; if (ERR_isError(err_code)) @@ -2497,7 +2976,11 @@ namespace ZstdSharp.Unsafe * which can be done explicitly (`ZSTD_DCtx_reset()`), * or is implied for operations starting some new decompression job (`ZSTD_initDStream`, `ZSTD_decompressDCtx()`, `ZSTD_decompress_usingDict()`) */ - public static nuint ZSTD_decompressStream(ZSTD_DCtx_s* zds, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input) + 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; @@ -2539,7 +3022,12 @@ namespace ZstdSharp.Unsafe goto case ZSTD_dStreamStage.zdss_loadHeader; case ZSTD_dStreamStage.zdss_loadHeader: { - nuint hSize = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format); + nuint hSize = ZSTD_getFrameHeader_advanced( + &zds->fParams, + zds->headerBuffer, + zds->lhSize, + zds->format + ); if (zds->refMultipleDDicts != default && zds->ddictSet != null) { ZSTD_DCtx_selectFrameDDict(zds); @@ -2560,21 +3048,42 @@ namespace ZstdSharp.Unsafe { if (remainingInput > 0) { - memcpy(zds->headerBuffer + zds->lhSize, ip, (uint)remainingInput); + memcpy( + zds->headerBuffer + zds->lhSize, + ip, + (uint)remainingInput + ); zds->lhSize += remainingInput; } input->pos = input->size; { /* check first few bytes */ - nuint err_code = ZSTD_getFrameHeader_advanced(&zds->fParams, zds->headerBuffer, zds->lhSize, zds->format); + nuint err_code = ZSTD_getFrameHeader_advanced( + &zds->fParams, + zds->headerBuffer, + zds->lhSize, + zds->format + ); if (ERR_isError(err_code)) { return err_code; } } - 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; + 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); @@ -2585,13 +3094,28 @@ namespace ZstdSharp.Unsafe } } - if (zds->fParams.frameContentSize != unchecked(0UL - 1) && zds->fParams.frameType != ZSTD_frameType_e.ZSTD_skippableFrame && (nuint)(oend - op) >= zds->fParams.frameContentSize) + if ( + zds->fParams.frameContentSize != unchecked(0UL - 1) + && zds->fParams.frameType != ZSTD_frameType_e.ZSTD_skippableFrame + && (nuint)(oend - op) >= zds->fParams.frameContentSize + ) { - nuint cSize = ZSTD_findFrameCompressedSize_advanced(istart, (nuint)(iend - istart), zds->format); + nuint cSize = ZSTD_findFrameCompressedSize_advanced( + istart, + (nuint)(iend - istart), + zds->format + ); if (cSize <= (nuint)(iend - istart)) { /* shortcut : using single-pass mode */ - nuint decompressedSize = ZSTD_decompress_usingDDict(zds, op, (nuint)(oend - op), istart, cSize, ZSTD_getDDict(zds)); + nuint decompressedSize = ZSTD_decompress_usingDDict( + zds, + op, + (nuint)(oend - op), + istart, + cSize, + ZSTD_getDDict(zds) + ); if (ERR_isError(decompressedSize)) return decompressedSize; assert(istart != null); @@ -2604,20 +3128,33 @@ namespace ZstdSharp.Unsafe } } - if (zds->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable && zds->fParams.frameType != ZSTD_frameType_e.ZSTD_skippableFrame && zds->fParams.frameContentSize != unchecked(0UL - 1) && (nuint)(oend - op) < zds->fParams.frameContentSize) + if ( + zds->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_stable + && zds->fParams.frameType != ZSTD_frameType_e.ZSTD_skippableFrame + && zds->fParams.frameContentSize != unchecked(0UL - 1) + && (nuint)(oend - op) < zds->fParams.frameContentSize + ) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); } { - nuint err_code = ZSTD_decompressBegin_usingDDict(zds, ZSTD_getDDict(zds)); + nuint err_code = ZSTD_decompressBegin_usingDDict( + zds, + ZSTD_getDDict(zds) + ); if (ERR_isError(err_code)) { return err_code; } } - if (zds->format == ZSTD_format_e.ZSTD_f_zstd1 && (MEM_readLE32(zds->headerBuffer) & 0xFFFFFFF0) == 0x184D2A50) + if ( + zds->format == ZSTD_format_e.ZSTD_f_zstd1 + && (MEM_readLE32(zds->headerBuffer) & 0xFFFFFFF0) == 0x184D2A50 + ) { zds->expected = MEM_readLE32(zds->headerBuffer + 4); zds->stage = ZSTD_dStage.ZSTDds_skipFrame; @@ -2625,7 +3162,11 @@ namespace ZstdSharp.Unsafe else { { - nuint err_code = ZSTD_decodeFrameHeader(zds, zds->headerBuffer, zds->lhSize); + nuint err_code = ZSTD_decodeFrameHeader( + zds, + zds->headerBuffer, + zds->lhSize + ); if (ERR_isError(err_code)) { return err_code; @@ -2636,21 +3177,46 @@ namespace ZstdSharp.Unsafe zds->stage = ZSTD_dStage.ZSTDds_decodeBlockHeader; } - zds->fParams.windowSize = zds->fParams.windowSize > 1U << 10 ? zds->fParams.windowSize : 1U << 10; + 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)); + return unchecked( + (nuint)( + -(int)ZSTD_ErrorCode.ZSTD_error_frameParameter_windowTooLarge + ) + ); } if (zds->maxBlockSizeParam != 0) - zds->fParams.blockSizeMax = zds->fParams.blockSizeMax < (uint)zds->maxBlockSizeParam ? zds->fParams.blockSizeMax : (uint)zds->maxBlockSizeParam; + zds->fParams.blockSizeMax = + zds->fParams.blockSizeMax < (uint)zds->maxBlockSizeParam + ? zds->fParams.blockSizeMax + : (uint)zds->maxBlockSizeParam; + { /* frame checksum */ - nuint neededInBuffSize = zds->fParams.blockSizeMax > 4 ? zds->fParams.blockSizeMax : 4; - nuint neededOutBuffSize = zds->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered ? ZSTD_decodingBufferSize_internal(zds->fParams.windowSize, zds->fParams.frameContentSize, zds->fParams.blockSizeMax) : 0; - ZSTD_DCtx_updateOversizedDuration(zds, neededInBuffSize, neededOutBuffSize); + nuint neededInBuffSize = + zds->fParams.blockSizeMax > 4 ? zds->fParams.blockSizeMax : 4; + nuint neededOutBuffSize = + zds->outBufferMode == ZSTD_bufferMode_e.ZSTD_bm_buffered + ? ZSTD_decodingBufferSize_internal( + zds->fParams.windowSize, + zds->fParams.frameContentSize, + zds->fParams.blockSizeMax + ) + : 0; + ZSTD_DCtx_updateOversizedDuration( + zds, + neededInBuffSize, + neededOutBuffSize + ); { - int tooSmall = zds->inBuffSize < neededInBuffSize || zds->outBuffSize < neededOutBuffSize ? 1 : 0; + int tooSmall = + zds->inBuffSize < neededInBuffSize + || zds->outBuffSize < neededOutBuffSize + ? 1 + : 0; int tooLarge = ZSTD_DCtx_isOversizedTooLong(zds); if (tooSmall != 0 || tooLarge != 0) { @@ -2658,9 +3224,17 @@ namespace ZstdSharp.Unsafe if (zds->staticSize != 0) { assert(zds->staticSize >= (nuint)sizeof(ZSTD_DCtx_s)); - if (bufferSize > 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)); + return unchecked( + (nuint)( + -(int) + ZSTD_ErrorCode.ZSTD_error_memory_allocation + ) + ); } } else @@ -2668,10 +3242,18 @@ namespace ZstdSharp.Unsafe ZSTD_customFree(zds->inBuff, zds->customMem); zds->inBuffSize = 0; zds->outBuffSize = 0; - zds->inBuff = (sbyte*)ZSTD_customMalloc(bufferSize, zds->customMem); + zds->inBuff = (sbyte*)ZSTD_customMalloc( + bufferSize, + zds->customMem + ); if (zds->inBuff == null) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + return unchecked( + (nuint)( + -(int) + ZSTD_ErrorCode.ZSTD_error_memory_allocation + ) + ); } } @@ -2686,7 +3268,10 @@ namespace ZstdSharp.Unsafe goto case ZSTD_dStreamStage.zdss_read; case ZSTD_dStreamStage.zdss_read: { - nuint neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize(zds, (nuint)(iend - ip)); + nuint neededInSize = ZSTD_nextSrcSizeToDecompressWithInputSize( + zds, + (nuint)(iend - ip) + ); if (neededInSize == 0) { zds->streamStage = ZSTD_dStreamStage.zdss_init; @@ -2697,7 +3282,13 @@ namespace ZstdSharp.Unsafe if ((nuint)(iend - ip) >= neededInSize) { { - nuint err_code = ZSTD_decompressContinueStream(zds, &op, oend, ip, neededInSize); + nuint err_code = ZSTD_decompressContinueStream( + zds, + &op, + oend, + ip, + neededInSize + ); if (ERR_isError(err_code)) { return err_code; @@ -2719,60 +3310,87 @@ namespace ZstdSharp.Unsafe 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) { - 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) { - 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(zds->inBuff + zds->inPos, toLoad, ip, (nuint)(iend - ip)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } - if (loadedSize != 0) - { - ip += loadedSize; - zds->inPos += loadedSize; - } + loadedSize = ZSTD_limitCopy( + zds->inBuff + zds->inPos, + toLoad, + ip, + (nuint)(iend - ip) + ); + } - if (loadedSize < toLoad) - { - someMoreWork = 0; - break; - } - - zds->inPos = 0; - { - nuint err_code = ZSTD_decompressContinueStream(zds, &op, oend, zds->inBuff, neededInSize); - if (ERR_isError(err_code)) - { - return err_code; - } - } + if (loadedSize != 0) + { + ip += loadedSize; + zds->inPos += loadedSize; + } + if (loadedSize < toLoad) + { + someMoreWork = 0; break; } + zds->inPos = 0; + { + nuint err_code = ZSTD_decompressContinueStream( + zds, + &op, + oend, + zds->inBuff, + neededInSize + ); + if (ERR_isError(err_code)) + { + return err_code; + } + } + + break; + } + case ZSTD_dStreamStage.zdss_flush: { nuint toFlushSize = zds->outEnd - zds->outStart; - nuint flushedSize = ZSTD_limitCopy(op, (nuint)(oend - op), zds->outBuff + zds->outStart, toFlushSize); + nuint flushedSize = ZSTD_limitCopy( + op, + (nuint)(oend - op), + zds->outBuff + zds->outStart, + toFlushSize + ); op = op != null ? op + flushedSize : op; 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) + if ( + zds->outBuffSize < zds->fParams.frameContentSize + && zds->outStart + zds->fParams.blockSizeMax > zds->outBuffSize + ) { zds->outStart = zds->outEnd = 0; } @@ -2799,12 +3417,16 @@ namespace ZstdSharp.Unsafe { if (op == oend) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_noForwardProgress_destFull)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_noForwardProgress_destFull) + ); } if (ip == iend) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_noForwardProgress_inputEmpty)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_noForwardProgress_inputEmpty) + ); } assert(0 != 0); @@ -2844,7 +3466,11 @@ namespace ZstdSharp.Unsafe return 1; } - nextSrcSizeHint += ZSTD_blockHeaderSize * (nuint)(ZSTD_nextInputType(zds) == ZSTD_nextInputType_e.ZSTDnit_block ? 1 : 0); + nextSrcSizeHint += + ZSTD_blockHeaderSize + * (nuint)( + ZSTD_nextInputType(zds) == ZSTD_nextInputType_e.ZSTDnit_block ? 1 : 0 + ); assert(zds->inPos <= nextSrcSizeHint); nextSrcSizeHint -= zds->inPos; return nextSrcSizeHint; @@ -2857,7 +3483,15 @@ namespace ZstdSharp.Unsafe * 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) + 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; ZSTD_inBuffer_s input; @@ -2875,4 +3509,4 @@ namespace ZstdSharp.Unsafe } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompressBlock.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompressBlock.cs index a3ef9e25..a91c728a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompressBlock.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompressBlock.cs @@ -1,7 +1,7 @@ -using static ZstdSharp.UnsafeHelper; -using System.Runtime.CompilerServices; using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -20,7 +20,8 @@ namespace ZstdSharp.Unsafe ***************************************************************/ private static nuint ZSTD_blockSizeMax(ZSTD_DCtx_s* dctx) { - nuint blockSizeMax = dctx->isFrameDecompression != 0 ? dctx->fParams.blockSizeMax : 1 << 17; + nuint blockSizeMax = + dctx->isFrameDecompression != 0 ? dctx->fParams.blockSizeMax : 1 << 17; assert(blockSizeMax <= 1 << 17); return blockSizeMax; } @@ -52,13 +53,26 @@ namespace ZstdSharp.Unsafe } /* Allocate buffer for literals, either overlapping current dst, or split between dst and litExtraBuffer, or stored entirely within litExtraBuffer */ - private static void ZSTD_allocateLiteralsBuffer(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, nuint litSize, streaming_operation streaming, nuint expectedWriteSize, uint splitImmediately) + private static void ZSTD_allocateLiteralsBuffer( + ZSTD_DCtx_s* dctx, + void* dst, + nuint dstCapacity, + nuint litSize, + streaming_operation streaming, + nuint expectedWriteSize, + uint splitImmediately + ) { nuint blockSizeMax = ZSTD_blockSizeMax(dctx); assert(litSize <= blockSizeMax); - assert(dctx->isFrameDecompression != 0 || streaming == streaming_operation.not_streaming); + assert( + dctx->isFrameDecompression != 0 || streaming == streaming_operation.not_streaming + ); assert(expectedWriteSize <= blockSizeMax); - if (streaming == streaming_operation.not_streaming && dstCapacity > blockSizeMax + 32 + litSize + 32) + if ( + streaming == streaming_operation.not_streaming + && dstCapacity > blockSizeMax + 32 + litSize + 32 + ) { dctx->litBuffer = (byte*)dst + blockSizeMax + 32; dctx->litBufferEnd = dctx->litBuffer + litSize; @@ -97,7 +111,14 @@ namespace ZstdSharp.Unsafe * * @return : nb of bytes read from src (< srcSize ) * note : symbol not declared but exposed for fullbench */ - private static nuint ZSTD_decodeLiteralsBlock(ZSTD_DCtx_s* dctx, void* src, nuint srcSize, void* dst, nuint dstCapacity, streaming_operation streaming) + private static nuint ZSTD_decodeLiteralsBlock( + ZSTD_DCtx_s* dctx, + void* src, + nuint srcSize, + void* dst, + nuint dstCapacity, + streaming_operation streaming + ) { if (srcSize < 1 + 1) { @@ -113,24 +134,42 @@ namespace ZstdSharp.Unsafe case SymbolEncodingType_e.set_repeat: if (dctx->litEntropy == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dictionary_corrupted)); + 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)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } { - nuint lhSize, litSize, litCSize; + nuint lhSize, + litSize, + litCSize; uint singleStream = 0; uint lhlCode = (uint)(istart[0] >> 2 & 3); uint lhc = MEM_readLE32(istart); nuint hufSuccess; - nuint expectedWriteSize = blockSizeMax < dstCapacity ? blockSizeMax : dstCapacity; - int flags = 0 | (ZSTD_DCtx_get_bmi2(dctx) != 0 ? (int)HUF_flags_e.HUF_flags_bmi2 : 0) | (dctx->disableHufAsm != 0 ? (int)HUF_flags_e.HUF_flags_disableAsm : 0); + nuint expectedWriteSize = + blockSizeMax < dstCapacity ? blockSizeMax : dstCapacity; + int flags = + 0 + | ( + ZSTD_DCtx_get_bmi2(dctx) != 0 + ? (int)HUF_flags_e.HUF_flags_bmi2 + : 0 + ) + | ( + dctx->disableHufAsm != 0 + ? (int)HUF_flags_e.HUF_flags_disableAsm + : 0 + ); switch (lhlCode) { case 0: @@ -155,31 +194,51 @@ namespace ZstdSharp.Unsafe if (litSize > 0 && dst == null) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); } if (litSize > blockSizeMax) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } if (singleStream == 0) if (litSize < 6) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_literals_headerWrong)); + return unchecked( + (nuint)( + -(int)ZSTD_ErrorCode.ZSTD_error_literals_headerWrong + ) + ); } if (litCSize + lhSize > srcSize) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } if (expectedWriteSize < litSize) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); } - ZSTD_allocateLiteralsBuffer(dctx, dst, dstCapacity, litSize, streaming, expectedWriteSize, 0); + ZSTD_allocateLiteralsBuffer( + dctx, + dst, + dstCapacity, + litSize, + streaming, + expectedWriteSize, + 0 + ); if (dctx->ddictIsCold != 0 && litSize > 768) { sbyte* _ptr = (sbyte*)dctx->HUFptr; @@ -200,31 +259,71 @@ namespace ZstdSharp.Unsafe { if (singleStream != 0) { - hufSuccess = HUF_decompress1X_usingDTable(dctx->litBuffer, litSize, istart + lhSize, litCSize, dctx->HUFptr, flags); + hufSuccess = HUF_decompress1X_usingDTable( + dctx->litBuffer, + litSize, + istart + lhSize, + litCSize, + dctx->HUFptr, + flags + ); } else { assert(litSize >= 6); - hufSuccess = HUF_decompress4X_usingDTable(dctx->litBuffer, litSize, istart + lhSize, litCSize, dctx->HUFptr, flags); + hufSuccess = HUF_decompress4X_usingDTable( + dctx->litBuffer, + litSize, + istart + lhSize, + litCSize, + dctx->HUFptr, + flags + ); } } else { if (singleStream != 0) { - hufSuccess = HUF_decompress1X1_DCtx_wksp(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart + lhSize, litCSize, dctx->workspace, sizeof(uint) * 640, flags); + hufSuccess = HUF_decompress1X1_DCtx_wksp( + dctx->entropy.hufTable, + dctx->litBuffer, + litSize, + istart + lhSize, + litCSize, + dctx->workspace, + sizeof(uint) * 640, + flags + ); } else { - hufSuccess = HUF_decompress4X_hufOnly_wksp(dctx->entropy.hufTable, dctx->litBuffer, litSize, istart + lhSize, litCSize, dctx->workspace, sizeof(uint) * 640, flags); + hufSuccess = HUF_decompress4X_hufOnly_wksp( + dctx->entropy.hufTable, + dctx->litBuffer, + litSize, + istart + lhSize, + litCSize, + dctx->workspace, + sizeof(uint) * 640, + flags + ); } } if (dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split) { assert(litSize > 1 << 16); - memcpy(dctx->litExtraBuffer, dctx->litBufferEnd - (1 << 16), 1 << 16); - memmove(dctx->litBuffer + (1 << 16) - 32, dctx->litBuffer, litSize - (1 << 16)); + memcpy( + dctx->litExtraBuffer, + dctx->litBufferEnd - (1 << 16), + 1 << 16 + ); + memmove( + dctx->litBuffer + (1 << 16) - 32, + dctx->litBuffer, + litSize - (1 << 16) + ); dctx->litBuffer += (1 << 16) - 32; dctx->litBufferEnd -= 32; assert(dctx->litBufferEnd <= (byte*)dst + blockSizeMax); @@ -232,7 +331,9 @@ namespace ZstdSharp.Unsafe if (ERR_isError(hufSuccess)) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } dctx->litPtr = dctx->litBuffer; @@ -244,159 +345,462 @@ namespace ZstdSharp.Unsafe } case SymbolEncodingType_e.set_basic: + { + nuint litSize, + lhSize; + uint lhlCode = (uint)(istart[0] >> 2 & 3); + nuint expectedWriteSize = + blockSizeMax < dstCapacity ? blockSizeMax : dstCapacity; + switch (lhlCode) { - nuint litSize, lhSize; - uint lhlCode = (uint)(istart[0] >> 2 & 3); - nuint expectedWriteSize = blockSizeMax < dstCapacity ? blockSizeMax : dstCapacity; - switch (lhlCode) - { - case 0: - case 2: - default: - lhSize = 1; - litSize = (nuint)(istart[0] >> 3); - break; - case 1: - lhSize = 2; - litSize = (nuint)(MEM_readLE16(istart) >> 4); - break; - case 3: - lhSize = 3; - if (srcSize < 3) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); - } - - litSize = MEM_readLE24(istart) >> 4; - break; - } - - if (litSize > 0 && dst == null) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); - } - - if (litSize > blockSizeMax) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); - } - - if (expectedWriteSize < litSize) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); - } - - ZSTD_allocateLiteralsBuffer(dctx, dst, dstCapacity, litSize, streaming, expectedWriteSize, 1); - if (lhSize + litSize + 32 > srcSize) - { - if (litSize + lhSize > srcSize) + case 0: + case 2: + default: + lhSize = 1; + litSize = (nuint)(istart[0] >> 3); + break; + case 1: + lhSize = 2; + litSize = (nuint)(MEM_readLE16(istart) >> 4); + break; + case 3: + lhSize = 3; + if (srcSize < 3) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } - if (dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split) - { - memcpy(dctx->litBuffer, istart + lhSize, (uint)(litSize - (1 << 16))); - memcpy(dctx->litExtraBuffer, istart + lhSize + litSize - (1 << 16), 1 << 16); - } - else - { - memcpy(dctx->litBuffer, istart + lhSize, (uint)litSize); - } - - dctx->litPtr = dctx->litBuffer; - dctx->litSize = litSize; - return lhSize + litSize; - } - - dctx->litPtr = istart + lhSize; - dctx->litSize = litSize; - dctx->litBufferEnd = dctx->litPtr + litSize; - dctx->litBufferLocation = ZSTD_litLocation_e.ZSTD_not_in_dst; - return lhSize + litSize; + litSize = MEM_readLE24(istart) >> 4; + break; } - case SymbolEncodingType_e.set_rle: + if (litSize > 0 && dst == null) { - uint lhlCode = (uint)(istart[0] >> 2 & 3); - nuint litSize, lhSize; - nuint expectedWriteSize = blockSizeMax < dstCapacity ? blockSizeMax : dstCapacity; - switch (lhlCode) + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); + } + + if (litSize > blockSizeMax) + { + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); + } + + if (expectedWriteSize < litSize) + { + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); + } + + ZSTD_allocateLiteralsBuffer( + dctx, + dst, + dstCapacity, + litSize, + streaming, + expectedWriteSize, + 1 + ); + if (lhSize + litSize + 32 > srcSize) + { + if (litSize + lhSize > srcSize) { - case 0: - case 2: - default: - lhSize = 1; - litSize = (nuint)(istart[0] >> 3); - break; - case 1: - lhSize = 2; - if (srcSize < 3) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); - } - - litSize = (nuint)(MEM_readLE16(istart) >> 4); - break; - case 3: - lhSize = 3; - if (srcSize < 4) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); - } - - litSize = MEM_readLE24(istart) >> 4; - break; + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } - if (litSize > 0 && dst == null) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); - } - - if (litSize > blockSizeMax) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); - } - - if (expectedWriteSize < litSize) - { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); - } - - ZSTD_allocateLiteralsBuffer(dctx, dst, dstCapacity, litSize, streaming, expectedWriteSize, 1); if (dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split) { - memset(dctx->litBuffer, istart[lhSize], (uint)(litSize - (1 << 16))); - memset(dctx->litExtraBuffer, istart[lhSize], 1 << 16); + memcpy( + dctx->litBuffer, + istart + lhSize, + (uint)(litSize - (1 << 16)) + ); + memcpy( + dctx->litExtraBuffer, + istart + lhSize + litSize - (1 << 16), + 1 << 16 + ); } else { - memset(dctx->litBuffer, istart[lhSize], (uint)litSize); + memcpy(dctx->litBuffer, istart + lhSize, (uint)litSize); } dctx->litPtr = dctx->litBuffer; dctx->litSize = litSize; - return lhSize + 1; + return lhSize + litSize; } + dctx->litPtr = istart + lhSize; + dctx->litSize = litSize; + dctx->litBufferEnd = dctx->litPtr + litSize; + dctx->litBufferLocation = ZSTD_litLocation_e.ZSTD_not_in_dst; + return lhSize + litSize; + } + + case SymbolEncodingType_e.set_rle: + { + uint lhlCode = (uint)(istart[0] >> 2 & 3); + nuint litSize, + lhSize; + nuint expectedWriteSize = + blockSizeMax < dstCapacity ? blockSizeMax : dstCapacity; + switch (lhlCode) + { + case 0: + case 2: + default: + lhSize = 1; + litSize = (nuint)(istart[0] >> 3); + break; + case 1: + lhSize = 2; + if (srcSize < 3) + { + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); + } + + litSize = (nuint)(MEM_readLE16(istart) >> 4); + break; + case 3: + lhSize = 3; + if (srcSize < 4) + { + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); + } + + litSize = MEM_readLE24(istart) >> 4; + break; + } + + if (litSize > 0 && dst == null) + { + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); + } + + if (litSize > blockSizeMax) + { + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); + } + + if (expectedWriteSize < litSize) + { + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); + } + + ZSTD_allocateLiteralsBuffer( + dctx, + dst, + dstCapacity, + litSize, + streaming, + expectedWriteSize, + 1 + ); + if (dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split) + { + memset(dctx->litBuffer, istart[lhSize], (uint)(litSize - (1 << 16))); + memset(dctx->litExtraBuffer, istart[lhSize], 1 << 16); + } + else + { + memset(dctx->litBuffer, istart[lhSize], (uint)litSize); + } + + dctx->litPtr = dctx->litBuffer; + dctx->litSize = litSize; + return lhSize + 1; + } + default: - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } } } /* Hidden declaration for fullbench */ - private static nuint ZSTD_decodeLiteralsBlock_wrapper(ZSTD_DCtx_s* dctx, void* src, nuint srcSize, void* dst, nuint dstCapacity) + private static nuint ZSTD_decodeLiteralsBlock_wrapper( + ZSTD_DCtx_s* dctx, + void* src, + nuint srcSize, + void* dst, + nuint dstCapacity + ) { dctx->isFrameDecompression = 0; - return ZSTD_decodeLiteralsBlock(dctx, src, srcSize, dst, dstCapacity, streaming_operation.not_streaming); + return ZSTD_decodeLiteralsBlock( + dctx, + src, + srcSize, + dst, + dstCapacity, + streaming_operation.not_streaming + ); } - private static readonly 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) }); - private static readonly 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) }); - private static readonly 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, byte nbAddBits) + private static readonly 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), + } + ); + private static readonly 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 + ), + } + ); + private static readonly 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, + byte nbAddBits + ) { void* ptr = dt; ZSTD_seqSymbol_header* DTableH = (ZSTD_seqSymbol_header*)ptr; @@ -415,7 +819,16 @@ namespace ZstdSharp.Unsafe * 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, byte* nbAdditionalBits, uint tableLog, void* wksp, nuint wkspSize) + private static void ZSTD_buildFSETable_body( + ZSTD_seqSymbol* dt, + short* normalizedCounter, + uint maxSymbolValue, + uint* baseValue, + byte* nbAdditionalBits, + uint tableLog, + void* wksp, + nuint wkspSize + ) { ZSTD_seqSymbol* tableDecode = dt + 1; uint maxSV1 = maxSymbolValue + 1; @@ -502,7 +915,8 @@ namespace ZstdSharp.Unsafe { uint tableMask = tableSize - 1; uint step = (tableSize >> 1) + (tableSize >> 3) + 3; - uint s, position = 0; + uint s, + position = 0; for (s = 0; s < maxSV1; s++) { int i; @@ -526,7 +940,9 @@ namespace ZstdSharp.Unsafe uint symbol = tableDecode[u].baseValue; uint nextState = symbolNext[symbol]++; tableDecode[u].nbBits = (byte)(tableLog - ZSTD_highbit32(nextState)); - tableDecode[u].nextState = (ushort)((nextState << tableDecode[u].nbBits) - tableSize); + tableDecode[u].nextState = (ushort)( + (nextState << tableDecode[u].nbBits) - tableSize + ); assert(nbAdditionalBits[symbol] < 255); tableDecode[u].nbAdditionalBits = nbAdditionalBits[symbol]; tableDecode[u].baseValue = baseValue[symbol]; @@ -535,9 +951,27 @@ namespace ZstdSharp.Unsafe } /* Avoids the FORCE_INLINE of the _body() function. */ - private static void ZSTD_buildFSETable_body_default(ZSTD_seqSymbol* dt, short* normalizedCounter, uint maxSymbolValue, uint* baseValue, byte* nbAdditionalBits, uint tableLog, void* wksp, nuint wkspSize) + private static void ZSTD_buildFSETable_body_default( + ZSTD_seqSymbol* dt, + short* normalizedCounter, + uint maxSymbolValue, + uint* baseValue, + byte* nbAdditionalBits, + uint tableLog, + void* wksp, + nuint wkspSize + ) { - ZSTD_buildFSETable_body(dt, normalizedCounter, maxSymbolValue, baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); + ZSTD_buildFSETable_body( + dt, + normalizedCounter, + maxSymbolValue, + baseValue, + nbAdditionalBits, + tableLog, + wksp, + wkspSize + ); } /* ZSTD_buildFSETable() : @@ -549,15 +983,51 @@ namespace ZstdSharp.Unsafe * defined in zstd_decompress_internal.h. * Internal use only. */ - private static void ZSTD_buildFSETable(ZSTD_seqSymbol* dt, short* normalizedCounter, uint maxSymbolValue, uint* baseValue, byte* nbAdditionalBits, uint tableLog, void* wksp, nuint wkspSize, int bmi2) + private static void ZSTD_buildFSETable( + ZSTD_seqSymbol* dt, + short* normalizedCounter, + uint maxSymbolValue, + uint* baseValue, + byte* nbAdditionalBits, + uint tableLog, + void* wksp, + nuint wkspSize, + int bmi2 + ) { - ZSTD_buildFSETable_body_default(dt, normalizedCounter, maxSymbolValue, baseValue, nbAdditionalBits, tableLog, wksp, wkspSize); + 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, byte* nbAdditionalBits, ZSTD_seqSymbol* defaultTable, uint flagRepeatTable, int ddictIsCold, int nbSeq, uint* wksp, nuint wkspSize, int bmi2) + private static nuint ZSTD_buildSeqTable( + ZSTD_seqSymbol* DTableSpace, + ZSTD_seqSymbol** DTablePtr, + SymbolEncodingType_e type, + uint max, + uint maxLog, + void* src, + nuint srcSize, + uint* baseValue, + byte* nbAdditionalBits, + ZSTD_seqSymbol* defaultTable, + uint flagRepeatTable, + int ddictIsCold, + int nbSeq, + uint* wksp, + nuint wkspSize, + int bmi2 + ) { switch (type) { @@ -569,7 +1039,9 @@ namespace ZstdSharp.Unsafe if (*(byte*)src > max) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } { @@ -587,7 +1059,9 @@ namespace ZstdSharp.Unsafe case SymbolEncodingType_e.set_repeat: if (flagRepeatTable == 0) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } if (ddictIsCold != 0 && nbSeq > 24) @@ -612,25 +1086,39 @@ namespace ZstdSharp.Unsafe return 0; case SymbolEncodingType_e.set_compressed: + { + uint tableLog; + short* norm = stackalloc short[53]; + nuint headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize); + if (ERR_isError(headerSize)) { - uint tableLog; - short* norm = stackalloc short[53]; - nuint headerSize = FSE_readNCount(norm, &max, &tableLog, src, srcSize); - if (ERR_isError(headerSize)) - { - 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, norm, max, baseValue, nbAdditionalBits, tableLog, wksp, wkspSize, bmi2); - *DTablePtr = DTableSpace; - return headerSize; + 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, + norm, + max, + baseValue, + nbAdditionalBits, + tableLog, + wksp, + wkspSize, + bmi2 + ); + *DTablePtr = DTableSpace; + return headerSize; + } + default: assert(0 != 0); return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_GENERIC)); @@ -640,7 +1128,12 @@ namespace ZstdSharp.Unsafe /*! ZSTD_decodeSeqHeaders() : * decode sequence header from src */ /* Used by: zstd_decompress_block, fullbench */ - private static nuint ZSTD_decodeSeqHeaders(ZSTD_DCtx_s* dctx, int* nbSeqPtr, void* src, nuint srcSize) + private static nuint ZSTD_decodeSeqHeaders( + ZSTD_DCtx_s* dctx, + int* nbSeqPtr, + void* src, + nuint srcSize + ) { byte* istart = (byte*)src; byte* iend = istart + srcSize; @@ -702,30 +1195,87 @@ namespace ZstdSharp.Unsafe SymbolEncodingType_e MLtype = (SymbolEncodingType_e)(*ip >> 2 & 3); ip++; { - nuint llhSize = ZSTD_buildSeqTable(&dctx->entropy.LLTable.e0, &dctx->LLTptr, LLtype, 35, 9, ip, (nuint)(iend - ip), LL_base, LL_bits, LL_defaultDTable, dctx->fseEntropy, dctx->ddictIsCold, nbSeq, dctx->workspace, sizeof(uint) * 640, ZSTD_DCtx_get_bmi2(dctx)); + nuint llhSize = ZSTD_buildSeqTable( + &dctx->entropy.LLTable.e0, + &dctx->LLTptr, + LLtype, + 35, + 9, + ip, + (nuint)(iend - ip), + LL_base, + LL_bits, + LL_defaultDTable, + dctx->fseEntropy, + dctx->ddictIsCold, + nbSeq, + dctx->workspace, + sizeof(uint) * 640, + ZSTD_DCtx_get_bmi2(dctx) + ); if (ERR_isError(llhSize)) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } ip += llhSize; } { - nuint ofhSize = ZSTD_buildSeqTable(&dctx->entropy.OFTable.e0, &dctx->OFTptr, OFtype, 31, 8, ip, (nuint)(iend - ip), OF_base, OF_bits, OF_defaultDTable, dctx->fseEntropy, dctx->ddictIsCold, nbSeq, dctx->workspace, sizeof(uint) * 640, ZSTD_DCtx_get_bmi2(dctx)); + nuint ofhSize = ZSTD_buildSeqTable( + &dctx->entropy.OFTable.e0, + &dctx->OFTptr, + OFtype, + 31, + 8, + ip, + (nuint)(iend - ip), + OF_base, + OF_bits, + OF_defaultDTable, + dctx->fseEntropy, + dctx->ddictIsCold, + nbSeq, + dctx->workspace, + sizeof(uint) * 640, + ZSTD_DCtx_get_bmi2(dctx) + ); if (ERR_isError(ofhSize)) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } ip += ofhSize; } { - nuint mlhSize = ZSTD_buildSeqTable(&dctx->entropy.MLTable.e0, &dctx->MLTptr, MLtype, 52, 9, ip, (nuint)(iend - ip), ML_base, ML_bits, ML_defaultDTable, dctx->fseEntropy, dctx->ddictIsCold, nbSeq, dctx->workspace, sizeof(uint) * 640, ZSTD_DCtx_get_bmi2(dctx)); + nuint mlhSize = ZSTD_buildSeqTable( + &dctx->entropy.MLTable.e0, + &dctx->MLTptr, + MLtype, + 52, + 9, + ip, + (nuint)(iend - ip), + ML_base, + ML_bits, + ML_defaultDTable, + dctx->fseEntropy, + dctx->ddictIsCold, + nbSeq, + dctx->workspace, + sizeof(uint) * 640, + ZSTD_DCtx_get_bmi2(dctx) + ); if (ERR_isError(mlhSize)) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); } ip += mlhSize; @@ -736,38 +1286,30 @@ namespace ZstdSharp.Unsafe } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_dec32table => new uint[8] - { - 0, - 1, - 2, - 1, - 4, - 4, - 4, - 4 - }; - private static uint* dec32table => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_dec32table)); + private static ReadOnlySpan Span_dec32table => new uint[8] { 0, 1, 2, 1, 4, 4, 4, 4 }; + private static uint* dec32table => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_dec32table) + ); #else - private static readonly uint* dec32table = GetArrayPointer(new uint[8] { 0, 1, 2, 1, 4, 4, 4, 4 }); + private static readonly uint* dec32table = GetArrayPointer( + new uint[8] { 0, 1, 2, 1, 4, 4, 4, 4 } + ); #endif #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_dec64table => new int[8] - { - 8, - 8, - 8, - 7, - 8, - 9, - 10, - 11 - }; - private static int* dec64table => (int*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_dec64table)); + private static ReadOnlySpan Span_dec64table => new int[8] { 8, 8, 8, 7, 8, 9, 10, 11 }; + private static int* dec64table => + (int*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_dec64table) + ); #else - private static readonly int* dec64table = GetArrayPointer(new int[8] { 8, 8, 8, 7, 8, 9, 10, 11 }); + private static readonly int* dec64table = GetArrayPointer( + new int[8] { 8, 8, 8, 7, 8, 9, 10, 11 } + ); #endif /*! ZSTD_overlapCopy8() : * Copies 8 bytes from ip to op and updates op and ip where ip <= op. @@ -812,11 +1354,21 @@ namespace ZstdSharp.Unsafe * - 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) + 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); + 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) @@ -884,7 +1436,16 @@ namespace ZstdSharp.Unsafe * 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) + 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; @@ -903,7 +1464,13 @@ namespace ZstdSharp.Unsafe assert(op < op + sequenceLength); assert(oLitEnd < op + sequenceLength); - ZSTD_safecopy(op, oend_w, *litPtr, (nint)sequence.litLength, ZSTD_overlap_e.ZSTD_no_overlap); + 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)) @@ -929,14 +1496,30 @@ namespace ZstdSharp.Unsafe } } - ZSTD_safecopy(op, oend_w, match, (nint)sequence.matchLength, ZSTD_overlap_e.ZSTD_overlap_src_before_dst); + ZSTD_safecopy( + op, + oend_w, + match, + (nint)sequence.matchLength, + ZSTD_overlap_e.ZSTD_overlap_src_before_dst + ); return sequenceLength; } /* ZSTD_execSequenceEndSplitLitBuffer(): * This version is intended to be used during instances where the litBuffer is still split. It is kept separate to avoid performance impact for the good case. */ - private static nuint ZSTD_execSequenceEndSplitLitBuffer(byte* op, byte* oend, byte* oend_w, seq_t sequence, byte** litPtr, byte* litLimit, byte* prefixStart, byte* virtualStart, byte* dictEnd) + private static nuint ZSTD_execSequenceEndSplitLitBuffer( + byte* op, + byte* oend, + byte* oend_w, + seq_t sequence, + byte** litPtr, + byte* litLimit, + byte* prefixStart, + byte* virtualStart, + byte* dictEnd + ) { byte* oLitEnd = op + sequence.litLength; nuint sequenceLength = sequence.litLength + sequence.matchLength; @@ -985,12 +1568,27 @@ namespace ZstdSharp.Unsafe } } - ZSTD_safecopy(op, oend_w, match, (nint)sequence.matchLength, ZSTD_overlap_e.ZSTD_overlap_src_before_dst); + 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) + private static nuint ZSTD_execSequence( + byte* op, + byte* oend, + seq_t sequence, + byte** litPtr, + byte* litLimit, + byte* prefixStart, + byte* virtualStart, + byte* dictEnd + ) { var sequence_litLength = sequence.litLength; var sequence_matchLength = sequence.matchLength; @@ -1005,8 +1603,26 @@ namespace ZstdSharp.Unsafe 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, new seq_t { litLength = sequence_litLength, matchLength = sequence_matchLength, offset = sequence_offset }, litPtr, litLimit, prefixStart, virtualStart, dictEnd); + if ( + iLitEnd > litLimit + || oMatchEnd > oend_w + || MEM_32bits && (nuint)(oend - op) < sequenceLength + 32 + ) + return ZSTD_execSequenceEnd( + op, + oend, + new seq_t + { + litLength = sequence_litLength, + matchLength = sequence_matchLength, + offset = sequence_offset, + }, + litPtr, + litLimit, + prefixStart, + virtualStart, + dictEnd + ); assert(op <= oLitEnd); assert(oLitEnd < oMatchEnd); assert(oMatchEnd <= oend); @@ -1017,7 +1633,12 @@ namespace ZstdSharp.Unsafe ZSTD_copy16(op, *litPtr); if (sequence_litLength > 16) { - ZSTD_wildcopy(op + 16, *litPtr + 16, (nint)(sequence_litLength - 16), ZSTD_overlap_e.ZSTD_no_overlap); + ZSTD_wildcopy( + op + 16, + *litPtr + 16, + (nint)(sequence_litLength - 16), + ZSTD_overlap_e.ZSTD_no_overlap + ); } op = oLitEnd; @@ -1051,7 +1672,12 @@ namespace ZstdSharp.Unsafe assert(sequence_matchLength >= 1); if (sequence_offset >= 16) { - ZSTD_wildcopy(op, match, (nint)sequence_matchLength, ZSTD_overlap_e.ZSTD_no_overlap); + ZSTD_wildcopy( + op, + match, + (nint)sequence_matchLength, + ZSTD_overlap_e.ZSTD_no_overlap + ); return sequenceLength; } @@ -1060,14 +1686,29 @@ namespace ZstdSharp.Unsafe if (sequence_matchLength > 8) { assert(op < oMatchEnd); - ZSTD_wildcopy(op, match, (nint)sequence_matchLength - 8, ZSTD_overlap_e.ZSTD_overlap_src_before_dst); + ZSTD_wildcopy( + op, + match, + (nint)sequence_matchLength - 8, + ZSTD_overlap_e.ZSTD_overlap_src_before_dst + ); } return sequenceLength; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_execSequenceSplitLitBuffer(byte* op, byte* oend, byte* oend_w, seq_t sequence, byte** litPtr, byte* litLimit, byte* prefixStart, byte* virtualStart, byte* dictEnd) + private static nuint ZSTD_execSequenceSplitLitBuffer( + byte* op, + byte* oend, + byte* oend_w, + seq_t sequence, + byte** litPtr, + byte* litLimit, + byte* prefixStart, + byte* virtualStart, + byte* dictEnd + ) { byte* oLitEnd = op + sequence.litLength; nuint sequenceLength = sequence.litLength + sequence.matchLength; @@ -1077,8 +1718,22 @@ namespace ZstdSharp.Unsafe 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_execSequenceEndSplitLitBuffer(op, oend, oend_w, sequence, litPtr, litLimit, prefixStart, virtualStart, dictEnd); + if ( + iLitEnd > litLimit + || oMatchEnd > oend_w + || MEM_32bits && (nuint)(oend - op) < sequenceLength + 32 + ) + return ZSTD_execSequenceEndSplitLitBuffer( + op, + oend, + oend_w, + sequence, + litPtr, + litLimit, + prefixStart, + virtualStart, + dictEnd + ); assert(op <= oLitEnd); assert(oLitEnd < oMatchEnd); assert(oMatchEnd <= oend); @@ -1089,7 +1744,12 @@ namespace ZstdSharp.Unsafe ZSTD_copy16(op, *litPtr); if (sequence.litLength > 16) { - ZSTD_wildcopy(op + 16, *litPtr + 16, (nint)(sequence.litLength - 16), ZSTD_overlap_e.ZSTD_no_overlap); + ZSTD_wildcopy( + op + 16, + *litPtr + 16, + (nint)(sequence.litLength - 16), + ZSTD_overlap_e.ZSTD_no_overlap + ); } op = oLitEnd; @@ -1123,7 +1783,12 @@ namespace ZstdSharp.Unsafe assert(sequence.matchLength >= 1); if (sequence.offset >= 16) { - ZSTD_wildcopy(op, match, (nint)sequence.matchLength, ZSTD_overlap_e.ZSTD_no_overlap); + ZSTD_wildcopy( + op, + match, + (nint)sequence.matchLength, + ZSTD_overlap_e.ZSTD_no_overlap + ); return sequenceLength; } @@ -1132,13 +1797,22 @@ namespace ZstdSharp.Unsafe if (sequence.matchLength > 8) { assert(op < oMatchEnd); - ZSTD_wildcopy(op, match, (nint)sequence.matchLength - 8, ZSTD_overlap_e.ZSTD_overlap_src_before_dst); + ZSTD_wildcopy( + op, + 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) + private static void ZSTD_initFseState( + ZSTD_fseState* DStatePtr, + BIT_DStream_t* bitD, + ZSTD_seqSymbol* dt + ) { void* ptr = dt; ZSTD_seqSymbol_header* DTableH = (ZSTD_seqSymbol_header*)ptr; @@ -1148,7 +1822,12 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_updateFseStateWithDInfo(ZSTD_fseState* DStatePtr, BIT_DStream_t* bitD, ushort nextState, uint nbBits) + private static void ZSTD_updateFseStateWithDInfo( + ZSTD_fseState* DStatePtr, + BIT_DStream_t* bitD, + ushort nextState, + uint nbBits + ) { nuint lowBits = BIT_readBits(bitD, nbBits); DStatePtr->state = nextState + lowBits; @@ -1161,7 +1840,11 @@ namespace ZstdSharp.Unsafe * @return : Sequence (litL + matchL + offset) */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static seq_t ZSTD_decodeSequence(seqState_t* seqState, ZSTD_longOffset_e longOffsets, int isLastSeq) + private static seq_t ZSTD_decodeSequence( + seqState_t* seqState, + ZSTD_longOffset_e longOffsets, + int isLastSeq + ) { seq_t seq; ZSTD_seqSymbol* llDInfo = seqState->stateLL.table + seqState->stateLL.state; @@ -1194,7 +1877,12 @@ namespace ZstdSharp.Unsafe * avoids branches, and avoids accidentally reading 0 bits. */ const uint extraBits = 30 - 25; - offset = ofBase + (BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) << (int)extraBits); + offset = + ofBase + + ( + BIT_readBitsFast(&seqState->DStream, ofBits - extraBits) + << (int)extraBits + ); BIT_reloadDStream(&seqState->DStream); offset += BIT_readBitsFast(&seqState->DStream, extraBits); } @@ -1222,7 +1910,10 @@ namespace ZstdSharp.Unsafe { offset = ofBase + ll0 + BIT_readBitsFast(&seqState->DStream, 1); { - nuint temp = offset == 3 ? seqState->prevOffset.e0 - 1 : (&seqState->prevOffset.e0)[offset]; + nuint temp = + offset == 3 + ? seqState->prevOffset.e0 - 1 + : (&seqState->prevOffset.e0)[offset]; temp -= temp == 0 ? 1U : 0U; if (offset != 1) seqState->prevOffset.e2 = seqState->prevOffset.e1; @@ -1247,11 +1938,26 @@ namespace ZstdSharp.Unsafe BIT_reloadDStream(&seqState->DStream); if (isLastSeq == 0) { - ZSTD_updateFseStateWithDInfo(&seqState->stateLL, &seqState->DStream, llNext, llnbBits); - ZSTD_updateFseStateWithDInfo(&seqState->stateML, &seqState->DStream, mlNext, mlnbBits); + ZSTD_updateFseStateWithDInfo( + &seqState->stateLL, + &seqState->DStream, + llNext, + llnbBits + ); + ZSTD_updateFseStateWithDInfo( + &seqState->stateML, + &seqState->DStream, + mlNext, + mlnbBits + ); if (MEM_32bits) BIT_reloadDStream(&seqState->DStream); - ZSTD_updateFseStateWithDInfo(&seqState->stateOffb, &seqState->DStream, ofNext, ofnbBits); + ZSTD_updateFseStateWithDInfo( + &seqState->stateOffb, + &seqState->DStream, + ofNext, + ofnbBits + ); BIT_reloadDStream(&seqState->DStream); } } @@ -1260,7 +1966,15 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_decompressSequences_bodySplitLitBuffer(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset) + private static nuint ZSTD_decompressSequences_bodySplitLitBuffer( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { byte* ip = (byte*)seqStart; byte* iend = ip + seqSize; @@ -1297,7 +2011,7 @@ namespace ZstdSharp.Unsafe { litLength = 0, matchLength = 0, - offset = 0 + offset = 0, }; for (; nbSeq != 0; nbSeq--) { @@ -1305,7 +2019,17 @@ namespace ZstdSharp.Unsafe if (litPtr + sequence.litLength > dctx->litBufferEnd) break; { - nuint oneSeqSize = ZSTD_execSequenceSplitLitBuffer(op, oend, litPtr + sequence.litLength - 32, sequence, &litPtr, litBufferEnd, prefixStart, vBase, dictEnd); + nuint oneSeqSize = ZSTD_execSequenceSplitLitBuffer( + op, + oend, + litPtr + sequence.litLength - 32, + sequence, + &litPtr, + litBufferEnd, + prefixStart, + vBase, + dictEnd + ); if (ERR_isError(oneSeqSize)) return oneSeqSize; op += oneSeqSize; @@ -1319,7 +2043,9 @@ namespace ZstdSharp.Unsafe { if (leftoverLit > (nuint)(oend - op)) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); } ZSTD_safecopyDstBeforeSrc(op, litPtr, (nint)leftoverLit); @@ -1331,7 +2057,16 @@ namespace ZstdSharp.Unsafe litBufferEnd = dctx->litExtraBuffer + (1 << 16); dctx->litBufferLocation = ZSTD_litLocation_e.ZSTD_not_in_dst; { - nuint oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litBufferEnd, prefixStart, vBase, dictEnd); + nuint oneSeqSize = ZSTD_execSequence( + op, + oend, + sequence, + &litPtr, + litBufferEnd, + prefixStart, + vBase, + dictEnd + ); if (ERR_isError(oneSeqSize)) return oneSeqSize; op += oneSeqSize; @@ -1345,8 +2080,21 @@ namespace ZstdSharp.Unsafe { for (; nbSeq != 0; nbSeq--) { - seq_t sequence = ZSTD_decodeSequence(&seqState, isLongOffset, nbSeq == 1 ? 1 : 0); - nuint oneSeqSize = ZSTD_execSequence(op, oend, sequence, &litPtr, litBufferEnd, prefixStart, vBase, dictEnd); + seq_t sequence = ZSTD_decodeSequence( + &seqState, + isLongOffset, + nbSeq == 1 ? 1 : 0 + ); + nuint oneSeqSize = ZSTD_execSequence( + op, + oend, + sequence, + &litPtr, + litBufferEnd, + prefixStart, + vBase, + dictEnd + ); if (ERR_isError(oneSeqSize)) return oneSeqSize; op += oneSeqSize; @@ -1408,14 +2156,25 @@ namespace ZstdSharp.Unsafe } [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) + private static nuint ZSTD_decompressSequences_body( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { // HACK, force nbSeq to stack (better register usage) System.Threading.Thread.VolatileRead(ref nbSeq); byte* ip = (byte*)seqStart; byte* iend = ip + seqSize; byte* ostart = (byte*)dst; - byte* oend = dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_not_in_dst ? ZSTD_maybeNullPtrAdd(ostart, (nint)maxDstSize) : dctx->litBuffer; + byte* oend = + dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_not_in_dst + ? ZSTD_maybeNullPtrAdd(ostart, (nint)maxDstSize) + : dctx->litBuffer; byte* op = ostart; byte* litPtr = dctx->litPtr; byte* litEnd = litPtr + dctx->litSize; @@ -1430,7 +2189,10 @@ namespace ZstdSharp.Unsafe { uint i; for (i = 0; i < 3; i++) - System.Runtime.CompilerServices.Unsafe.Add(ref seqState.prevOffset.e0, (int)i) = dctx->entropy.rep[i]; + System.Runtime.CompilerServices.Unsafe.Add( + ref seqState.prevOffset.e0, + (int)i + ) = dctx->entropy.rep[i]; } if (ERR_isError(BIT_initDStream(ref seqState.DStream, ip, (nuint)(iend - ip)))) @@ -1482,15 +2244,45 @@ namespace ZstdSharp.Unsafe * avoids branches, and avoids accidentally reading 0 bits. */ const uint extraBits = 30 - 25; - offset = ofBase + (BIT_readBitsFast(seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ofBits - extraBits) << (int)extraBits); - BIT_reloadDStream(ref seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ref seqState_DStream_ptr, seqState_DStream_start, seqState_DStream_limitPtr); - offset += BIT_readBitsFast(seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, extraBits); + offset = + ofBase + + ( + BIT_readBitsFast( + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ofBits - extraBits + ) << (int)extraBits + ); + BIT_reloadDStream( + ref seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ref seqState_DStream_ptr, + seqState_DStream_start, + seqState_DStream_limitPtr + ); + offset += BIT_readBitsFast( + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + extraBits + ); } else { - offset = ofBase + BIT_readBitsFast(seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ofBits); + offset = + ofBase + + BIT_readBitsFast( + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ofBits + ); if (MEM_32bits) - BIT_reloadDStream(ref seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ref seqState_DStream_ptr, seqState_DStream_start, seqState_DStream_limitPtr); + BIT_reloadDStream( + ref seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ref seqState_DStream_ptr, + seqState_DStream_start, + seqState_DStream_limitPtr + ); } seqState.prevOffset.e2 = seqState.prevOffset.e1; @@ -1502,15 +2294,35 @@ namespace ZstdSharp.Unsafe uint ll0 = llDInfo->baseValue == 0 ? 1U : 0U; if (ofBits == 0) { - offset = System.Runtime.CompilerServices.Unsafe.Add(ref seqState.prevOffset.e0, (int)ll0); - seqState.prevOffset.e1 = System.Runtime.CompilerServices.Unsafe.Add(ref seqState.prevOffset.e0, ll0 == 0 ? 1 : 0); + offset = System.Runtime.CompilerServices.Unsafe.Add( + ref seqState.prevOffset.e0, + (int)ll0 + ); + seqState.prevOffset.e1 = + System.Runtime.CompilerServices.Unsafe.Add( + ref seqState.prevOffset.e0, + ll0 == 0 ? 1 : 0 + ); seqState.prevOffset.e0 = offset; } else { - offset = ofBase + ll0 + BIT_readBitsFast(seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, 1); + offset = + ofBase + + ll0 + + BIT_readBitsFast( + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + 1 + ); { - nuint temp = offset == 3 ? seqState.prevOffset.e0 - 1 : System.Runtime.CompilerServices.Unsafe.Add(ref seqState.prevOffset.e0, (int)offset); + nuint temp = + offset == 3 + ? seqState.prevOffset.e0 - 1 + : System.Runtime.CompilerServices.Unsafe.Add( + ref seqState.prevOffset.e0, + (int)offset + ); temp -= temp == 0 ? 1U : 0U; if (offset != 1) seqState.prevOffset.e2 = seqState.prevOffset.e1; @@ -1524,23 +2336,79 @@ namespace ZstdSharp.Unsafe } if (mlBits > 0) - sequence_matchLength += BIT_readBitsFast(seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, mlBits); + sequence_matchLength += BIT_readBitsFast( + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + mlBits + ); if (MEM_32bits && mlBits + llBits >= 25 - (30 - 25)) - BIT_reloadDStream(ref seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ref seqState_DStream_ptr, seqState_DStream_start, seqState_DStream_limitPtr); + BIT_reloadDStream( + ref seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ref seqState_DStream_ptr, + seqState_DStream_start, + seqState_DStream_limitPtr + ); if (MEM_64bits && totalBits >= 57 - (9 + 9 + 8)) - BIT_reloadDStream(ref seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ref seqState_DStream_ptr, seqState_DStream_start, seqState_DStream_limitPtr); + BIT_reloadDStream( + ref seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ref seqState_DStream_ptr, + seqState_DStream_start, + seqState_DStream_limitPtr + ); if (llBits > 0) - sequence_litLength += BIT_readBitsFast(seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, llBits); + sequence_litLength += BIT_readBitsFast( + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + llBits + ); if (MEM_32bits) - BIT_reloadDStream(ref seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ref seqState_DStream_ptr, seqState_DStream_start, seqState_DStream_limitPtr); + BIT_reloadDStream( + ref seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ref seqState_DStream_ptr, + seqState_DStream_start, + seqState_DStream_limitPtr + ); if ((nbSeq == 1 ? 1 : 0) == 0) { - ZSTD_updateFseStateWithDInfo(ref seqState.stateLL, seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, llNext, llnbBits); - ZSTD_updateFseStateWithDInfo(ref seqState.stateML, seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, mlNext, mlnbBits); + ZSTD_updateFseStateWithDInfo( + ref seqState.stateLL, + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + llNext, + llnbBits + ); + ZSTD_updateFseStateWithDInfo( + ref seqState.stateML, + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + mlNext, + mlnbBits + ); if (MEM_32bits) - BIT_reloadDStream(ref seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ref seqState_DStream_ptr, seqState_DStream_start, seqState_DStream_limitPtr); - ZSTD_updateFseStateWithDInfo(ref seqState.stateOffb, seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ofNext, ofnbBits); - BIT_reloadDStream(ref seqState_DStream_bitContainer, ref seqState_DStream_bitsConsumed, ref seqState_DStream_ptr, seqState_DStream_start, seqState_DStream_limitPtr); + BIT_reloadDStream( + ref seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ref seqState_DStream_ptr, + seqState_DStream_start, + seqState_DStream_limitPtr + ); + ZSTD_updateFseStateWithDInfo( + ref seqState.stateOffb, + seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ofNext, + ofnbBits + ); + BIT_reloadDStream( + ref seqState_DStream_bitContainer, + ref seqState_DStream_bitsConsumed, + ref seqState_DStream_ptr, + seqState_DStream_start, + seqState_DStream_limitPtr + ); } } @@ -1556,9 +2424,27 @@ namespace ZstdSharp.Unsafe byte* match = oLitEnd - sequence_offset; assert(op != null); assert(oend_w < oend); - if (iLitEnd > litEnd || oMatchEnd > oend_w || MEM_32bits && (nuint)(oend - op) < oneSeqSize + 32) + if ( + iLitEnd > litEnd + || oMatchEnd > oend_w + || MEM_32bits && (nuint)(oend - op) < oneSeqSize + 32 + ) { - oneSeqSize = ZSTD_execSequenceEnd(op, oend, new seq_t { litLength = sequence_litLength, matchLength = sequence_matchLength, offset = sequence_offset }, &litPtr, litEnd, prefixStart, vBase, dictEnd); + oneSeqSize = ZSTD_execSequenceEnd( + op, + oend, + new seq_t + { + litLength = sequence_litLength, + matchLength = sequence_matchLength, + offset = sequence_offset, + }, + &litPtr, + litEnd, + prefixStart, + vBase, + dictEnd + ); goto returnOneSeqSize; } @@ -1572,7 +2458,12 @@ namespace ZstdSharp.Unsafe ZSTD_copy16(op, litPtr); if (sequence_litLength > 16) { - ZSTD_wildcopy(op + 16, litPtr + 16, (nint)(sequence_litLength - 16), ZSTD_overlap_e.ZSTD_no_overlap); + ZSTD_wildcopy( + op + 16, + litPtr + 16, + (nint)(sequence_litLength - 16), + ZSTD_overlap_e.ZSTD_no_overlap + ); } byte* opInner = oLitEnd; @@ -1581,7 +2472,9 @@ namespace ZstdSharp.Unsafe { if (sequence_offset > (nuint)(oLitEnd - vBase)) { - oneSeqSize = unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); + oneSeqSize = unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected) + ); goto returnOneSeqSize; } @@ -1607,7 +2500,12 @@ namespace ZstdSharp.Unsafe assert(sequence_matchLength >= 1); if (sequence_offset >= 16) { - ZSTD_wildcopy(opInner, match, (nint)sequence_matchLength, ZSTD_overlap_e.ZSTD_no_overlap); + ZSTD_wildcopy( + opInner, + match, + (nint)sequence_matchLength, + ZSTD_overlap_e.ZSTD_no_overlap + ); goto returnOneSeqSize; } @@ -1616,10 +2514,15 @@ namespace ZstdSharp.Unsafe if (sequence_matchLength > 8) { assert(opInner < oMatchEnd); - ZSTD_wildcopy(opInner, match, (nint)sequence_matchLength - 8, ZSTD_overlap_e.ZSTD_overlap_src_before_dst); + ZSTD_wildcopy( + opInner, + match, + (nint)sequence_matchLength - 8, + ZSTD_overlap_e.ZSTD_overlap_src_before_dst + ); } - returnOneSeqSize: + returnOneSeqSize: ; } @@ -1629,7 +2532,13 @@ namespace ZstdSharp.Unsafe } assert(nbSeq == 0); - if (BIT_endOfDStream(seqState_DStream_bitsConsumed, seqState_DStream_ptr, seqState_DStream_start) == 0) + if ( + BIT_endOfDStream( + seqState_DStream_bitsConsumed, + seqState_DStream_ptr, + seqState_DStream_start + ) == 0 + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_corruption_detected)); } @@ -1637,7 +2546,11 @@ namespace ZstdSharp.Unsafe { uint i; for (i = 0; i < 3; i++) - dctx->entropy.rep[i] = (uint)System.Runtime.CompilerServices.Unsafe.Add(ref seqState.prevOffset.e0, (int)i); + dctx->entropy.rep[i] = (uint) + System.Runtime.CompilerServices.Unsafe.Add( + ref seqState.prevOffset.e0, + (int)i + ); } } @@ -1658,25 +2571,65 @@ namespace ZstdSharp.Unsafe 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) + private static nuint ZSTD_decompressSequences_default( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { - return ZSTD_decompressSequences_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); + return ZSTD_decompressSequences_body( + dctx, + dst, + maxDstSize, + seqStart, + seqSize, + nbSeq, + isLongOffset + ); } - private static nuint ZSTD_decompressSequencesSplitLitBuffer_default(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset) + private static nuint ZSTD_decompressSequencesSplitLitBuffer_default( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { - return ZSTD_decompressSequences_bodySplitLitBuffer(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); + return ZSTD_decompressSequences_bodySplitLitBuffer( + dctx, + dst, + maxDstSize, + seqStart, + seqSize, + nbSeq, + isLongOffset + ); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_prefetchMatch(nuint prefetchPos, seq_t sequence, byte* prefixStart, byte* dictEnd) + private static nuint ZSTD_prefetchMatch( + nuint prefetchPos, + seq_t sequence, + byte* prefixStart, + byte* dictEnd + ) { prefetchPos += sequence.litLength; { byte* matchBase = sequence.offset > prefetchPos ? dictEnd : prefixStart; /* note : this operation can overflow when seq.offset is really too large, which can only happen when input is corrupted. * No consequence though : memory address is only used for prefetching, not for dereferencing */ - byte* match = ZSTD_wrappedPtrSub(ZSTD_wrappedPtrAdd(matchBase, (nint)prefetchPos), (nint)sequence.offset); + byte* match = ZSTD_wrappedPtrSub( + ZSTD_wrappedPtrAdd(matchBase, (nint)prefetchPos), + (nint)sequence.offset + ); #if NETCOREAPP3_0_OR_GREATER if (System.Runtime.Intrinsics.X86.Sse.IsSupported) { @@ -1694,12 +2647,23 @@ namespace ZstdSharp.Unsafe * 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) + private static nuint ZSTD_decompressSequencesLong_body( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { byte* ip = (byte*)seqStart; byte* iend = ip + seqSize; byte* ostart = (byte*)dst; - byte* oend = dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_in_dst ? dctx->litBuffer : ZSTD_maybeNullPtrAdd(ostart, (nint)maxDstSize); + byte* oend = + dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_in_dst + ? dctx->litBuffer + : ZSTD_maybeNullPtrAdd(ostart, (nint)maxDstSize); byte* op = ostart; byte* litPtr = dctx->litPtr; byte* litBufferEnd = dctx->litBufferEnd; @@ -1733,15 +2697,26 @@ namespace ZstdSharp.Unsafe ZSTD_initFseState(&seqState.stateML, &seqState.DStream, dctx->MLTptr); for (seqNb = 0; seqNb < seqAdvance; seqNb++) { - seq_t sequence = ZSTD_decodeSequence(&seqState, isLongOffset, seqNb == nbSeq - 1 ? 1 : 0); + seq_t sequence = ZSTD_decodeSequence( + &seqState, + isLongOffset, + seqNb == nbSeq - 1 ? 1 : 0 + ); prefetchPos = ZSTD_prefetchMatch(prefetchPos, sequence, prefixStart, dictEnd); sequences[seqNb] = sequence; } for (; seqNb < nbSeq; seqNb++) { - seq_t sequence = ZSTD_decodeSequence(&seqState, isLongOffset, seqNb == nbSeq - 1 ? 1 : 0); - if (dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split && litPtr + sequences[seqNb - 8 & 8 - 1].litLength > dctx->litBufferEnd) + seq_t sequence = ZSTD_decodeSequence( + &seqState, + isLongOffset, + seqNb == nbSeq - 1 ? 1 : 0 + ); + if ( + dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split + && litPtr + sequences[seqNb - 8 & 8 - 1].litLength > dctx->litBufferEnd + ) { /* lit buffer is reaching split point, empty out the first buffer and transition to litExtraBuffer */ nuint leftoverLit = (nuint)(dctx->litBufferEnd - litPtr); @@ -1749,7 +2724,9 @@ namespace ZstdSharp.Unsafe { if (leftoverLit > (nuint)(oend - op)) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); } ZSTD_safecopyDstBeforeSrc(op, litPtr, (nint)leftoverLit); @@ -1761,10 +2738,24 @@ namespace ZstdSharp.Unsafe litBufferEnd = dctx->litExtraBuffer + (1 << 16); dctx->litBufferLocation = ZSTD_litLocation_e.ZSTD_not_in_dst; { - nuint oneSeqSize = ZSTD_execSequence(op, oend, sequences[seqNb - 8 & 8 - 1], &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd); + nuint oneSeqSize = ZSTD_execSequence( + op, + oend, + sequences[seqNb - 8 & 8 - 1], + &litPtr, + litBufferEnd, + prefixStart, + dictStart, + dictEnd + ); if (ERR_isError(oneSeqSize)) return oneSeqSize; - prefetchPos = ZSTD_prefetchMatch(prefetchPos, sequence, prefixStart, dictEnd); + prefetchPos = ZSTD_prefetchMatch( + prefetchPos, + sequence, + prefixStart, + dictEnd + ); sequences[seqNb & 8 - 1] = sequence; op += oneSeqSize; } @@ -1772,10 +2763,37 @@ namespace ZstdSharp.Unsafe else { /* lit buffer is either wholly contained in first or second split, or not split at all*/ - nuint oneSeqSize = dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split ? ZSTD_execSequenceSplitLitBuffer(op, oend, litPtr + sequences[seqNb - 8 & 8 - 1].litLength - 32, sequences[seqNb - 8 & 8 - 1], &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd) : ZSTD_execSequence(op, oend, sequences[seqNb - 8 & 8 - 1], &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd); + nuint oneSeqSize = + dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split + ? ZSTD_execSequenceSplitLitBuffer( + op, + oend, + litPtr + sequences[seqNb - 8 & 8 - 1].litLength - 32, + sequences[seqNb - 8 & 8 - 1], + &litPtr, + litBufferEnd, + prefixStart, + dictStart, + dictEnd + ) + : ZSTD_execSequence( + op, + oend, + sequences[seqNb - 8 & 8 - 1], + &litPtr, + litBufferEnd, + prefixStart, + dictStart, + dictEnd + ); if (ERR_isError(oneSeqSize)) return oneSeqSize; - prefetchPos = ZSTD_prefetchMatch(prefetchPos, sequence, prefixStart, dictEnd); + prefetchPos = ZSTD_prefetchMatch( + prefetchPos, + sequence, + prefixStart, + dictEnd + ); sequences[seqNb & 8 - 1] = sequence; op += oneSeqSize; } @@ -1790,14 +2808,19 @@ namespace ZstdSharp.Unsafe for (; seqNb < nbSeq; seqNb++) { seq_t* sequence = &sequences[seqNb & 8 - 1]; - if (dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split && litPtr + sequence->litLength > dctx->litBufferEnd) + if ( + dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split + && litPtr + sequence->litLength > dctx->litBufferEnd + ) { nuint leftoverLit = (nuint)(dctx->litBufferEnd - litPtr); if (leftoverLit != 0) { if (leftoverLit > (nuint)(oend - op)) { - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); } ZSTD_safecopyDstBeforeSrc(op, litPtr, (nint)leftoverLit); @@ -1809,7 +2832,16 @@ namespace ZstdSharp.Unsafe litBufferEnd = dctx->litExtraBuffer + (1 << 16); dctx->litBufferLocation = ZSTD_litLocation_e.ZSTD_not_in_dst; { - nuint oneSeqSize = ZSTD_execSequence(op, oend, *sequence, &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd); + nuint oneSeqSize = ZSTD_execSequence( + op, + oend, + *sequence, + &litPtr, + litBufferEnd, + prefixStart, + dictStart, + dictEnd + ); if (ERR_isError(oneSeqSize)) return oneSeqSize; op += oneSeqSize; @@ -1817,7 +2849,29 @@ namespace ZstdSharp.Unsafe } else { - nuint oneSeqSize = dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split ? ZSTD_execSequenceSplitLitBuffer(op, oend, litPtr + sequence->litLength - 32, *sequence, &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd) : ZSTD_execSequence(op, oend, *sequence, &litPtr, litBufferEnd, prefixStart, dictStart, dictEnd); + nuint oneSeqSize = + dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split + ? ZSTD_execSequenceSplitLitBuffer( + op, + oend, + litPtr + sequence->litLength - 32, + *sequence, + &litPtr, + litBufferEnd, + prefixStart, + dictStart, + dictEnd + ) + : ZSTD_execSequence( + op, + oend, + *sequence, + &litPtr, + litBufferEnd, + prefixStart, + dictStart, + dictEnd + ); if (ERR_isError(oneSeqSize)) return oneSeqSize; op += oneSeqSize; @@ -1866,19 +2920,67 @@ namespace ZstdSharp.Unsafe 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) + private static nuint ZSTD_decompressSequencesLong_default( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { - return ZSTD_decompressSequencesLong_body(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); + return ZSTD_decompressSequencesLong_body( + dctx, + dst, + maxDstSize, + seqStart, + seqSize, + nbSeq, + isLongOffset + ); } - private static nuint ZSTD_decompressSequences(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset) + private static nuint ZSTD_decompressSequences( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { - return ZSTD_decompressSequences_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); + return ZSTD_decompressSequences_default( + dctx, + dst, + maxDstSize, + seqStart, + seqSize, + nbSeq, + isLongOffset + ); } - private static nuint ZSTD_decompressSequencesSplitLitBuffer(ZSTD_DCtx_s* dctx, void* dst, nuint maxDstSize, void* seqStart, nuint seqSize, int nbSeq, ZSTD_longOffset_e isLongOffset) + private static nuint ZSTD_decompressSequencesSplitLitBuffer( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { - return ZSTD_decompressSequencesSplitLitBuffer_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); + return ZSTD_decompressSequencesSplitLitBuffer_default( + dctx, + dst, + maxDstSize, + seqStart, + seqSize, + nbSeq, + isLongOffset + ); } /* ZSTD_decompressSequencesLong() : @@ -1886,9 +2988,25 @@ namespace ZstdSharp.Unsafe * 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) + private static nuint ZSTD_decompressSequencesLong( + ZSTD_DCtx_s* dctx, + void* dst, + nuint maxDstSize, + void* seqStart, + nuint seqSize, + int nbSeq, + ZSTD_longOffset_e isLongOffset + ) { - return ZSTD_decompressSequencesLong_default(dctx, dst, maxDstSize, seqStart, seqSize, nbSeq, isLongOffset); + return ZSTD_decompressSequencesLong_default( + dctx, + dst, + maxDstSize, + seqStart, + seqSize, + nbSeq, + isLongOffset + ); } /** @@ -1912,7 +3030,7 @@ namespace ZstdSharp.Unsafe ZSTD_OffsetInfo info = new ZSTD_OffsetInfo { longOffsetShare = 0, - maxNbAdditionalBits = 0 + maxNbAdditionalBits = 0, }; if (nbSeq != 0) { @@ -1924,7 +3042,10 @@ namespace ZstdSharp.Unsafe assert(max <= 1 << 8); for (u = 0; u < max; u++) { - info.maxNbAdditionalBits = info.maxNbAdditionalBits > table[u].nbAdditionalBits ? info.maxNbAdditionalBits : table[u].nbAdditionalBits; + info.maxNbAdditionalBits = + info.maxNbAdditionalBits > table[u].nbAdditionalBits + ? info.maxNbAdditionalBits + : table[u].nbAdditionalBits; if (table[u].nbAdditionalBits > 22) info.longOffsetShare += 1; } @@ -1966,7 +3087,14 @@ namespace ZstdSharp.Unsafe * @return : decompressed block size, * or an error code (which can be tested using ZSTD_isError()) */ - private static nuint ZSTD_decompressBlock_internal(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize, streaming_operation streaming) + private static nuint ZSTD_decompressBlock_internal( + ZSTD_DCtx_s* dctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize, + streaming_operation streaming + ) { byte* ip = (byte*)src; if (srcSize > ZSTD_blockSizeMax(dctx)) @@ -1975,7 +3103,14 @@ namespace ZstdSharp.Unsafe } { - nuint litCSize = ZSTD_decodeLiteralsBlock(dctx, src, srcSize, dst, dstCapacity, streaming); + nuint litCSize = ZSTD_decodeLiteralsBlock( + dctx, + src, + srcSize, + dst, + dstCapacity, + streaming + ); if (ERR_isError(litCSize)) return litCSize; ip += litCSize; @@ -1986,8 +3121,12 @@ namespace ZstdSharp.Unsafe /* Compute the maximum block size, which must also work when !frame and fParams are unset. * Additionally, take the min with dstCapacity to ensure that the totalHistorySize fits in a size_t. */ - nuint blockSizeMax = dstCapacity < ZSTD_blockSizeMax(dctx) ? dstCapacity : ZSTD_blockSizeMax(dctx); - nuint totalHistorySize = ZSTD_totalHistorySize(ZSTD_maybeNullPtrAdd((byte*)dst, (nint)blockSizeMax), (byte*)dctx->virtualStart); + nuint blockSizeMax = + dstCapacity < ZSTD_blockSizeMax(dctx) ? dstCapacity : ZSTD_blockSizeMax(dctx); + nuint totalHistorySize = ZSTD_totalHistorySize( + ZSTD_maybeNullPtrAdd((byte*)dst, (nint)blockSizeMax), + (byte*)dctx->virtualStart + ); /* isLongOffset must be true if there are long offsets. * Offsets are long if they are larger than ZSTD_maxShortOffset(). * We don't expect that to be the case in 64-bit mode. @@ -1999,7 +3138,9 @@ namespace ZstdSharp.Unsafe * If isLongOffsets is true, then we will later check our decoding table to see * if it is even possible to generate long offsets. */ - ZSTD_longOffset_e isLongOffset = (ZSTD_longOffset_e)(MEM_32bits && totalHistorySize > ZSTD_maxShortOffset() ? 1 : 0); + ZSTD_longOffset_e isLongOffset = (ZSTD_longOffset_e)( + MEM_32bits && totalHistorySize > ZSTD_maxShortOffset() ? 1 : 0 + ); int usePrefetchDecoder = dctx->ddictIsCold; int nbSeq; nuint seqHSize = ZSTD_decodeSeqHeaders(dctx, &nbSeq, ip, srcSize); @@ -2012,15 +3153,25 @@ namespace ZstdSharp.Unsafe return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); } - if (MEM_64bits && sizeof(nuint) == sizeof(void*) && unchecked((nuint)(-1)) - (nuint)dst < 1 << 20) + if ( + MEM_64bits + && sizeof(nuint) == sizeof(void*) + && unchecked((nuint)(-1)) - (nuint)dst < 1 << 20 + ) { return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); } - if (isLongOffset != default || usePrefetchDecoder == 0 && totalHistorySize > 1U << 24 && nbSeq > 8) + if ( + isLongOffset != default + || usePrefetchDecoder == 0 && totalHistorySize > 1U << 24 && nbSeq > 8 + ) { ZSTD_OffsetInfo info = ZSTD_getOffsetInfo(dctx->OFTptr, nbSeq); - if (isLongOffset != default && info.maxNbAdditionalBits <= (uint)(MEM_32bits ? 25 : 57)) + if ( + isLongOffset != default + && info.maxNbAdditionalBits <= (uint)(MEM_32bits ? 25 : 57) + ) { isLongOffset = ZSTD_longOffset_e.ZSTD_lo_isRegularOffset; } @@ -2036,13 +3187,37 @@ namespace ZstdSharp.Unsafe dctx->ddictIsCold = 0; if (usePrefetchDecoder != 0) { - return ZSTD_decompressSequencesLong(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset); + return ZSTD_decompressSequencesLong( + dctx, + dst, + dstCapacity, + ip, + srcSize, + nbSeq, + isLongOffset + ); } if (dctx->litBufferLocation == ZSTD_litLocation_e.ZSTD_split) - return ZSTD_decompressSequencesSplitLitBuffer(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset); + return ZSTD_decompressSequencesSplitLitBuffer( + dctx, + dst, + dstCapacity, + ip, + srcSize, + nbSeq, + isLongOffset + ); else - return ZSTD_decompressSequences(dctx, dst, dstCapacity, ip, srcSize, nbSeq, isLongOffset); + return ZSTD_decompressSequences( + dctx, + dst, + dstCapacity, + ip, + srcSize, + nbSeq, + isLongOffset + ); } } @@ -2056,19 +3231,33 @@ namespace ZstdSharp.Unsafe if (dst != dctx->previousDstEnd && dstSize > 0) { dctx->dictEnd = dctx->previousDstEnd; - dctx->virtualStart = (sbyte*)dst - ((sbyte*)dctx->previousDstEnd - (sbyte*)dctx->prefixStart); + dctx->virtualStart = + (sbyte*)dst - ((sbyte*)dctx->previousDstEnd - (sbyte*)dctx->prefixStart); dctx->prefixStart = dst; dctx->previousDstEnd = dst; } } /* Internal definition of ZSTD_decompressBlock() to avoid deprecation warnings. */ - private static nuint ZSTD_decompressBlock_deprecated(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + private static nuint ZSTD_decompressBlock_deprecated( + ZSTD_DCtx_s* dctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { nuint dSize; dctx->isFrameDecompression = 0; ZSTD_checkContinuity(dctx, dst, dstCapacity); - dSize = ZSTD_decompressBlock_internal(dctx, dst, dstCapacity, src, srcSize, streaming_operation.not_streaming); + dSize = ZSTD_decompressBlock_internal( + dctx, + dst, + dstCapacity, + src, + srcSize, + streaming_operation.not_streaming + ); { nuint err_code = dSize; if (ERR_isError(err_code)) @@ -2082,22 +3271,48 @@ namespace ZstdSharp.Unsafe } /* NOTE: Must just wrap ZSTD_decompressBlock_deprecated() */ - public static nuint ZSTD_decompressBlock(ZSTD_DCtx_s* dctx, void* dst, nuint dstCapacity, void* src, nuint srcSize) + public static nuint ZSTD_decompressBlock( + ZSTD_DCtx_s* dctx, + void* dst, + nuint dstCapacity, + void* src, + nuint srcSize + ) { return ZSTD_decompressBlock_deprecated(dctx, dst, dstCapacity, src, srcSize); } - private static void ZSTD_initFseState(ref ZSTD_fseState DStatePtr, ref BIT_DStream_t bitD, ZSTD_seqSymbol* dt) + private static void ZSTD_initFseState( + ref ZSTD_fseState DStatePtr, + ref BIT_DStream_t bitD, + ZSTD_seqSymbol* dt + ) { void* ptr = dt; ZSTD_seqSymbol_header* DTableH = (ZSTD_seqSymbol_header*)ptr; - DStatePtr.state = BIT_readBits(bitD.bitContainer, ref bitD.bitsConsumed, DTableH->tableLog); - BIT_reloadDStream(ref bitD.bitContainer, ref bitD.bitsConsumed, ref bitD.ptr, bitD.start, bitD.limitPtr); + DStatePtr.state = BIT_readBits( + bitD.bitContainer, + ref bitD.bitsConsumed, + DTableH->tableLog + ); + BIT_reloadDStream( + ref bitD.bitContainer, + ref bitD.bitsConsumed, + ref bitD.ptr, + bitD.start, + bitD.limitPtr + ); DStatePtr.table = dt + 1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_updateFseStateWithDInfo(ref ZSTD_fseState DStatePtr, nuint bitD_bitContainer, ref uint bitD_bitsConsumed, ushort nextState, uint nbBits) + private static void ZSTD_updateFseStateWithDInfo( + ref ZSTD_fseState DStatePtr, + nuint bitD_bitContainer, + ref uint bitD_bitsConsumed, + ushort nextState, + uint nbBits + ) { nuint lowBits = BIT_readBits(bitD_bitContainer, ref bitD_bitsConsumed, nbBits); DStatePtr.state = nextState + lowBits; diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompressInternal.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompressInternal.cs index 5b37eaa4..c0fb99b4 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompressInternal.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDecompressInternal.cs @@ -1,205 +1,395 @@ -using static ZstdSharp.UnsafeHelper; using System; -using System.Runtime.InteropServices; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_LL_base => 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 - }; - private static uint* LL_base => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_LL_base)); + private static ReadOnlySpan Span_LL_base => + 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, + }; + private static uint* LL_base => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_LL_base) + ); #else - private static readonly 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 }); + private static readonly 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, + } + ); #endif #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_OF_base => 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 - }; - private static uint* OF_base => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_OF_base)); + private static ReadOnlySpan Span_OF_base => + 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, + }; + private static uint* OF_base => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_OF_base) + ); #else - private static readonly 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 }); + private static readonly 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, + } + ); #endif #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_OF_bits => new byte[32] - { - 0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - 21, - 22, - 23, - 24, - 25, - 26, - 27, - 28, - 29, - 30, - 31 - }; - private static byte* OF_bits => (byte*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_OF_bits)); + private static ReadOnlySpan Span_OF_bits => + new byte[32] + { + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + }; + private static byte* OF_bits => + (byte*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_OF_bits) + ); #else - private static readonly byte* OF_bits = GetArrayPointer(new byte[32] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }); + private static readonly byte* OF_bits = GetArrayPointer( + new byte[32] + { + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + } + ); #endif #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_ML_base => 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 - }; - private static uint* ML_base => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_ML_base)); + private static ReadOnlySpan Span_ML_base => + 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, + }; + private static uint* ML_base => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_ML_base) + ); #else - private static readonly 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 }); + private static readonly 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, + } + ); #endif + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int ZSTD_DCtx_get_bmi2(ZSTD_DCtx_s* dctx) { return 0; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDoubleFast.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDoubleFast.cs index ec35208f..f7b916f8 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDoubleFast.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdDoubleFast.cs @@ -1,13 +1,17 @@ -using System.Runtime.CompilerServices; -using static ZstdSharp.UnsafeHelper; using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { - private static void ZSTD_fillDoubleHashTableForCDict(ZSTD_MatchState_t* ms, void* end, ZSTD_dictTableLoadMethod_e dtlm) + private static void ZSTD_fillDoubleHashTableForCDict( + ZSTD_MatchState_t* ms, + void* end, + ZSTD_dictTableLoadMethod_e dtlm + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashLarge = ms->hashTable; @@ -43,7 +47,11 @@ namespace ZstdSharp.Unsafe } } - private static void ZSTD_fillDoubleHashTableForCCtx(ZSTD_MatchState_t* ms, void* end, ZSTD_dictTableLoadMethod_e dtlm) + private static void ZSTD_fillDoubleHashTableForCCtx( + ZSTD_MatchState_t* ms, + void* end, + ZSTD_dictTableLoadMethod_e dtlm + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashLarge = ms->hashTable; @@ -73,7 +81,12 @@ namespace ZstdSharp.Unsafe } } - private static void ZSTD_fillDoubleHashTable(ZSTD_MatchState_t* ms, void* end, ZSTD_dictTableLoadMethod_e dtlm, ZSTD_tableFillPurpose_e tfp) + private static void ZSTD_fillDoubleHashTable( + ZSTD_MatchState_t* ms, + void* end, + ZSTD_dictTableLoadMethod_e dtlm, + ZSTD_tableFillPurpose_e tfp + ) { if (tfp == ZSTD_tableFillPurpose_e.ZSTD_tfp_forCDict) { @@ -86,26 +99,29 @@ namespace ZstdSharp.Unsafe } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_dummy => new byte[10] - { - 0x12, - 0x34, - 0x56, - 0x78, - 0x9a, - 0xbc, - 0xde, - 0xf0, - 0xe2, - 0xb4 - }; - private static byte* dummy => (byte*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_dummy)); + private static ReadOnlySpan Span_dummy => + new byte[10] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xe2, 0xb4 }; + private static byte* dummy => + (byte*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_dummy) + ); #else - private static readonly byte* dummy = GetArrayPointer(new byte[10] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xe2, 0xb4 }); + private static readonly byte* dummy = GetArrayPointer( + new byte[10] { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xe2, 0xb4 } + ); #endif + [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_compressBlock_doubleFast_noDict_generic(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls) + private static nuint ZSTD_compressBlock_doubleFast_noDict_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; @@ -121,8 +137,10 @@ namespace ZstdSharp.Unsafe byte* prefixLowest = @base + prefixLowestIndex; byte* iend = istart + srcSize; byte* ilimit = iend - 8; - uint offset_1 = rep[0], offset_2 = rep[1]; - uint offsetSaved1 = 0, offsetSaved2 = 0; + uint offset_1 = rep[0], + offset_2 = rep[1]; + uint offsetSaved1 = 0, + offsetSaved2 = 0; nuint mLength; uint offset; uint curr; @@ -202,7 +220,12 @@ namespace ZstdSharp.Unsafe hl1 = ZSTD_hashPtr(ip1, hBitsL, 8); { - byte* matchl0_safe = ZSTD_selectAddr(idxl0, prefixLowestIndex, matchl0, &dummy[0]); + byte* matchl0_safe = ZSTD_selectAddr( + idxl0, + prefixLowestIndex, + matchl0, + &dummy[0] + ); if (MEM_read64(matchl0_safe) == MEM_read64(ip) && matchl0_safe == matchl0) { mLength = ZSTD_count(ip + 8, matchl0 + 8, iend) + 8; @@ -245,14 +268,13 @@ namespace ZstdSharp.Unsafe hl0 = hl1; idxl0 = idxl1; matchl0 = matchl1; - } - while (ip1 <= ilimit); - _cleanup: + } while (ip1 <= ilimit); + _cleanup: offsetSaved2 = offsetSaved1 != 0 && offset_1 != 0 ? offsetSaved1 : offsetSaved2; rep[0] = offset_1 != 0 ? offset_1 : offsetSaved1; rep[1] = offset_2 != 0 ? offset_2 : offsetSaved2; return (nuint)(iend - anchor); - _search_next_long: + _search_next_long: mLength = ZSTD_count(ip + 4, matchs0 + 4, iend) + 4; offset = (uint)(ip - matchs0); if (idxl1 > prefixLowestIndex && MEM_read64(matchl1) == MEM_read64(ip1)) @@ -274,7 +296,7 @@ namespace ZstdSharp.Unsafe mLength++; } - _match_found: + _match_found: offset_2 = offset_1; offset_1 = offset; if (step < 4) @@ -284,7 +306,7 @@ namespace ZstdSharp.Unsafe assert(offset > 0); ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + 3, mLength); - _match_stored: + _match_stored: ip += mLength; anchor = ip; if (ip <= ilimit) @@ -297,7 +319,9 @@ namespace ZstdSharp.Unsafe hashSmall[ZSTD_hashPtr(ip - 1, hBitsS, mls)] = (uint)(ip - 1 - @base); } - while (ip <= ilimit && offset_2 > 0 && MEM_read32(ip) == MEM_read32(ip - offset_2)) + while ( + ip <= ilimit && offset_2 > 0 && MEM_read32(ip) == MEM_read32(ip - offset_2) + ) { /* store sequence */ nuint rLength = ZSTD_count(ip + 4, ip + 4 - offset_2, iend) + 4; @@ -319,7 +343,14 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_generic(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls) + private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_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; @@ -336,7 +367,8 @@ namespace ZstdSharp.Unsafe byte* prefixLowest = @base + prefixLowestIndex; byte* iend = istart + srcSize; byte* ilimit = iend - 8; - uint offset_1 = rep[0], offset_2 = rep[1]; + uint offset_1 = rep[0], + offset_2 = rep[1]; ZSTD_MatchState_t* dms = ms->dictMatchState; ZSTD_compressionParameters* dictCParams = &dms->cParams; uint* dictHashLong = dms->hashTable; @@ -406,12 +438,25 @@ namespace ZstdSharp.Unsafe byte* matchLong = @base + matchIndexL; byte* match = @base + matchIndexS; uint repIndex = curr + 1 - offset_1; - byte* repMatch = repIndex < prefixLowestIndex ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; + byte* repMatch = + repIndex < prefixLowestIndex + ? dictBase + (repIndex - dictIndexDelta) + : @base + repIndex; hashLong[h2] = hashSmall[h] = curr; - if (ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 && MEM_read32(repMatch) == MEM_read32(ip + 1)) + if ( + ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 + && MEM_read32(repMatch) == MEM_read32(ip + 1) + ) { byte* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; - mLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixLowest) + 4; + mLength = + ZSTD_count_2segments( + ip + 1 + 4, + repMatch + 4, + iend, + repMatchEnd, + prefixLowest + ) + 4; ip++; assert(1 >= 1); assert(1 <= 3); @@ -440,7 +485,14 @@ namespace ZstdSharp.Unsafe assert(dictMatchL < dictEnd); if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) { - mLength = ZSTD_count_2segments(ip + 8, dictMatchL + 8, iend, dictEnd, prefixLowest) + 8; + mLength = + ZSTD_count_2segments( + ip + 8, + dictMatchL + 8, + iend, + dictEnd, + prefixLowest + ) + 8; offset = curr - dictMatchIndexL - dictIndexDelta; while (ip > anchor && dictMatchL > dictStart && ip[-1] == dictMatchL[-1]) { @@ -474,16 +526,22 @@ namespace ZstdSharp.Unsafe ip += (ip - anchor >> 8) + 1; continue; - _search_next_long: + _search_next_long: { nuint hl3 = ZSTD_hashPtr(ip + 1, hBitsL, 8); nuint dictHashAndTagL3 = ZSTD_hashPtr(ip + 1, dictHBitsL, 8); uint matchIndexL3 = hashLong[hl3]; uint dictMatchIndexAndTagL3 = dictHashLong[dictHashAndTagL3 >> 8]; - int dictTagsMatchL3 = ZSTD_comparePackedTags(dictMatchIndexAndTagL3, dictHashAndTagL3); + int dictTagsMatchL3 = ZSTD_comparePackedTags( + dictMatchIndexAndTagL3, + dictHashAndTagL3 + ); byte* matchL3 = @base + matchIndexL3; hashLong[hl3] = curr + 1; - if (matchIndexL3 >= prefixLowestIndex && MEM_read64(matchL3) == MEM_read64(ip + 1)) + if ( + matchIndexL3 >= prefixLowestIndex + && MEM_read64(matchL3) == MEM_read64(ip + 1) + ) { mLength = ZSTD_count(ip + 9, matchL3 + 8, iend) + 8; ip++; @@ -503,12 +561,24 @@ namespace ZstdSharp.Unsafe uint dictMatchIndexL3 = dictMatchIndexAndTagL3 >> 8; byte* dictMatchL3 = dictBase + dictMatchIndexL3; assert(dictMatchL3 < dictEnd); - if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip + 1)) + if ( + dictMatchL3 > dictStart + && MEM_read64(dictMatchL3) == MEM_read64(ip + 1) + ) { - mLength = ZSTD_count_2segments(ip + 1 + 8, dictMatchL3 + 8, iend, dictEnd, prefixLowest) + 8; + mLength = + ZSTD_count_2segments( + ip + 1 + 8, + dictMatchL3 + 8, + iend, + dictEnd, + prefixLowest + ) + 8; ip++; offset = curr + 1 - dictMatchIndexL3 - dictIndexDelta; - while (ip > anchor && dictMatchL3 > dictStart && ip[-1] == dictMatchL3[-1]) + while ( + ip > anchor && dictMatchL3 > dictStart && ip[-1] == dictMatchL3[-1] + ) { ip--; dictMatchL3--; @@ -522,7 +592,8 @@ namespace ZstdSharp.Unsafe if (matchIndexS < prefixLowestIndex) { - mLength = ZSTD_count_2segments(ip + 4, match + 4, iend, dictEnd, prefixLowest) + 4; + mLength = + ZSTD_count_2segments(ip + 4, match + 4, iend, dictEnd, prefixLowest) + 4; offset = curr - matchIndexS; while (ip > anchor && match > dictStart && ip[-1] == match[-1]) { @@ -543,12 +614,12 @@ namespace ZstdSharp.Unsafe } } - _match_found: + _match_found: offset_2 = offset_1; offset_1 = offset; assert(offset > 0); ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + 3, mLength); - _match_stored: + _match_stored: ip += mLength; anchor = ip; if (ip <= ilimit) @@ -565,11 +636,24 @@ namespace ZstdSharp.Unsafe { uint current2 = (uint)(ip - @base); uint repIndex2 = current2 - offset_2; - byte* repMatch2 = repIndex2 < prefixLowestIndex ? dictBase + repIndex2 - dictIndexDelta : @base + repIndex2; - if (ZSTD_index_overlap_check(prefixLowestIndex, repIndex2) != 0 && MEM_read32(repMatch2) == MEM_read32(ip)) + byte* repMatch2 = + repIndex2 < prefixLowestIndex + ? dictBase + repIndex2 - dictIndexDelta + : @base + repIndex2; + if ( + ZSTD_index_overlap_check(prefixLowestIndex, repIndex2) != 0 + && MEM_read32(repMatch2) == MEM_read32(ip) + ) { byte* repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend; - nuint repLength2 = ZSTD_count_2segments(ip + 4, repMatch2 + 4, iend, repEnd2, prefixLowest) + 4; + nuint repLength2 = + ZSTD_count_2segments( + ip + 4, + repMatch2 + 4, + iend, + repEnd2, + prefixLowest + ) + 4; /* swap offset_2 <=> offset_1 */ uint tmpOffset = offset_2; offset_2 = offset_1; @@ -594,47 +678,129 @@ namespace ZstdSharp.Unsafe return (nuint)(iend - anchor); } - private static nuint ZSTD_compressBlock_doubleFast_noDict_4(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_noDict_4( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_doubleFast_noDict_generic(ms, seqStore, rep, src, srcSize, 4); } - private static nuint ZSTD_compressBlock_doubleFast_noDict_5(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_noDict_5( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_doubleFast_noDict_generic(ms, seqStore, rep, src, srcSize, 5); } - private static nuint ZSTD_compressBlock_doubleFast_noDict_6(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_noDict_6( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_doubleFast_noDict_generic(ms, seqStore, rep, src, srcSize, 6); } - private static nuint ZSTD_compressBlock_doubleFast_noDict_7(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_noDict_7( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_doubleFast_noDict_generic(ms, seqStore, rep, src, srcSize, 7); } - private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_4(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_4( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_doubleFast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4); + return ZSTD_compressBlock_doubleFast_dictMatchState_generic( + ms, + seqStore, + rep, + src, + srcSize, + 4 + ); } - private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_5(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_5( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_doubleFast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5); + return ZSTD_compressBlock_doubleFast_dictMatchState_generic( + ms, + seqStore, + rep, + src, + srcSize, + 5 + ); } - private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_6(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_6( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_doubleFast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6); + return ZSTD_compressBlock_doubleFast_dictMatchState_generic( + ms, + seqStore, + rep, + src, + srcSize, + 6 + ); } - private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_7(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_dictMatchState_7( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_doubleFast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7); + return ZSTD_compressBlock_doubleFast_dictMatchState_generic( + ms, + seqStore, + rep, + src, + srcSize, + 7 + ); } - private static nuint ZSTD_compressBlock_doubleFast(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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) @@ -651,24 +817,61 @@ namespace ZstdSharp.Unsafe } } - private static nuint ZSTD_compressBlock_doubleFast_dictMatchState(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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_dictMatchState_4(ms, seqStore, rep, src, srcSize); + return ZSTD_compressBlock_doubleFast_dictMatchState_4( + ms, + seqStore, + rep, + src, + srcSize + ); case 5: - return ZSTD_compressBlock_doubleFast_dictMatchState_5(ms, seqStore, rep, src, srcSize); + return ZSTD_compressBlock_doubleFast_dictMatchState_5( + ms, + seqStore, + rep, + src, + srcSize + ); case 6: - return ZSTD_compressBlock_doubleFast_dictMatchState_6(ms, seqStore, rep, src, srcSize); + return ZSTD_compressBlock_doubleFast_dictMatchState_6( + ms, + seqStore, + rep, + src, + srcSize + ); case 7: - return ZSTD_compressBlock_doubleFast_dictMatchState_7(ms, seqStore, rep, src, srcSize); + return ZSTD_compressBlock_doubleFast_dictMatchState_7( + ms, + seqStore, + rep, + src, + srcSize + ); } } - private static nuint ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls) + 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; @@ -690,7 +893,8 @@ namespace ZstdSharp.Unsafe byte* dictBase = ms->window.dictBase; byte* dictStart = dictBase + dictStartIndex; byte* dictEnd = dictBase + prefixStartIndex; - uint offset_1 = rep[0], offset_2 = rep[1]; + uint offset_1 = rep[0], + offset_2 = rep[1]; if (prefixStartIndex == dictStartIndex) return ZSTD_compressBlock_doubleFast(ms, seqStore, rep, src, srcSize); while (ip < ilimit) @@ -710,10 +914,23 @@ namespace ZstdSharp.Unsafe byte* repMatch = repBase + repIndex; nuint mLength; hashSmall[hSmall] = hashLong[hLong] = curr; - if ((ZSTD_index_overlap_check(prefixStartIndex, repIndex) & (offset_1 <= curr + 1 - dictStartIndex ? 1 : 0)) != 0 && MEM_read32(repMatch) == MEM_read32(ip + 1)) + if ( + ( + ZSTD_index_overlap_check(prefixStartIndex, repIndex) + & (offset_1 <= curr + 1 - dictStartIndex ? 1 : 0) + ) != 0 + && MEM_read32(repMatch) == MEM_read32(ip + 1) + ) { byte* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; - mLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixStart) + 4; + mLength = + ZSTD_count_2segments( + ip + 1 + 4, + repMatch + 4, + iend, + repMatchEnd, + prefixStart + ) + 4; ip++; assert(1 >= 1); assert(1 <= 3); @@ -724,9 +941,12 @@ namespace ZstdSharp.Unsafe if (matchLongIndex > dictStartIndex && MEM_read64(matchLong) == MEM_read64(ip)) { byte* matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend; - byte* lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart; + byte* lowMatchPtr = + matchLongIndex < prefixStartIndex ? dictStart : prefixStart; uint offset; - mLength = ZSTD_count_2segments(ip + 8, matchLong + 8, iend, matchEnd, prefixStart) + 8; + mLength = + ZSTD_count_2segments(ip + 8, matchLong + 8, iend, matchEnd, prefixStart) + + 8; offset = curr - matchLongIndex; while (ip > anchor && matchLong > lowMatchPtr && ip[-1] == matchLong[-1]) { @@ -738,7 +958,14 @@ namespace ZstdSharp.Unsafe offset_2 = offset_1; offset_1 = offset; assert(offset > 0); - ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + 3, mLength); + ZSTD_storeSeq( + seqStore, + (nuint)(ip - anchor), + anchor, + iend, + offset + 3, + mLength + ); } else if (matchIndex > dictStartIndex && MEM_read32(match) == MEM_read32(ip)) { @@ -748,11 +975,22 @@ namespace ZstdSharp.Unsafe byte* match3 = match3Base + matchIndex3; uint offset; hashLong[h3] = curr + 1; - if (matchIndex3 > dictStartIndex && MEM_read64(match3) == MEM_read64(ip + 1)) + if ( + matchIndex3 > dictStartIndex + && MEM_read64(match3) == MEM_read64(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; + 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]) @@ -765,8 +1003,11 @@ namespace ZstdSharp.Unsafe 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; + 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]) { @@ -779,7 +1020,14 @@ namespace ZstdSharp.Unsafe offset_2 = offset_1; offset_1 = offset; assert(offset > 0); - ZSTD_storeSeq(seqStore, (nuint)(ip - anchor), anchor, iend, offset + 3, mLength); + ZSTD_storeSeq( + seqStore, + (nuint)(ip - anchor), + anchor, + iend, + offset + 3, + mLength + ); } else { @@ -804,11 +1052,25 @@ namespace ZstdSharp.Unsafe { uint current2 = (uint)(ip - @base); uint repIndex2 = current2 - offset_2; - byte* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : @base + repIndex2; - if ((ZSTD_index_overlap_check(prefixStartIndex, repIndex2) & (offset_2 <= current2 - dictStartIndex ? 1 : 0)) != 0 && MEM_read32(repMatch2) == MEM_read32(ip)) + byte* repMatch2 = + repIndex2 < prefixStartIndex ? dictBase + repIndex2 : @base + repIndex2; + if ( + ( + ZSTD_index_overlap_check(prefixStartIndex, repIndex2) + & (offset_2 <= current2 - dictStartIndex ? 1 : 0) + ) != 0 + && MEM_read32(repMatch2) == MEM_read32(ip) + ) { byte* repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; - nuint repLength2 = ZSTD_count_2segments(ip + 4, repMatch2 + 4, iend, repEnd2, prefixStart) + 4; + nuint repLength2 = + ZSTD_count_2segments( + ip + 4, + repMatch2 + 4, + iend, + repEnd2, + prefixStart + ) + 4; /* swap offset_2 <=> offset_1 */ uint tmpOffset = offset_2; offset_2 = offset_1; @@ -833,27 +1095,85 @@ namespace ZstdSharp.Unsafe return (nuint)(iend - anchor); } - private static nuint ZSTD_compressBlock_doubleFast_extDict_4(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_extDict_4( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4); + return ZSTD_compressBlock_doubleFast_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + 4 + ); } - private static nuint ZSTD_compressBlock_doubleFast_extDict_5(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_extDict_5( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5); + return ZSTD_compressBlock_doubleFast_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + 5 + ); } - private static nuint ZSTD_compressBlock_doubleFast_extDict_6(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_extDict_6( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6); + return ZSTD_compressBlock_doubleFast_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + 6 + ); } - private static nuint ZSTD_compressBlock_doubleFast_extDict_7(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_doubleFast_extDict_7( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7); + return ZSTD_compressBlock_doubleFast_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + 7 + ); } - private static nuint ZSTD_compressBlock_doubleFast_extDict(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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) @@ -870,4 +1190,4 @@ namespace ZstdSharp.Unsafe } } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdFast.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdFast.cs index e4a03f89..49aa8a7d 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdFast.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdFast.cs @@ -1,11 +1,15 @@ -using static ZstdSharp.UnsafeHelper; using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { - private static void ZSTD_fillHashTableForCDict(ZSTD_MatchState_t* ms, void* end, ZSTD_dictTableLoadMethod_e dtlm) + private static void ZSTD_fillHashTableForCDict( + ZSTD_MatchState_t* ms, + void* end, + ZSTD_dictTableLoadMethod_e dtlm + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashTable = ms->hashTable; @@ -40,7 +44,11 @@ namespace ZstdSharp.Unsafe } } - private static void ZSTD_fillHashTableForCCtx(ZSTD_MatchState_t* ms, void* end, ZSTD_dictTableLoadMethod_e dtlm) + private static void ZSTD_fillHashTableForCCtx( + ZSTD_MatchState_t* ms, + void* end, + ZSTD_dictTableLoadMethod_e dtlm + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashTable = ms->hashTable; @@ -72,7 +80,12 @@ namespace ZstdSharp.Unsafe } } - private static void ZSTD_fillHashTable(ZSTD_MatchState_t* ms, void* end, ZSTD_dictTableLoadMethod_e dtlm, ZSTD_tableFillPurpose_e tfp) + private static void ZSTD_fillHashTable( + ZSTD_MatchState_t* ms, + void* end, + ZSTD_dictTableLoadMethod_e dtlm, + ZSTD_tableFillPurpose_e tfp + ) { if (tfp == ZSTD_tableFillPurpose_e.ZSTD_tfp_forCDict) { @@ -85,7 +98,12 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int ZSTD_match4Found_cmov(byte* currentPtr, byte* matchAddress, uint matchIdx, uint idxLowLimit) + private static int ZSTD_match4Found_cmov( + byte* currentPtr, + byte* matchAddress, + uint matchIdx, + uint idxLowLimit + ) { /* currentIdx >= lowLimit is a (somewhat) unpredictable branch. * However expression below compiles into conditional move. @@ -97,7 +115,12 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int ZSTD_match4Found_branch(byte* currentPtr, byte* matchAddress, uint matchIdx, uint idxLowLimit) + private static int ZSTD_match4Found_branch( + byte* currentPtr, + byte* matchAddress, + uint matchIdx, + uint idxLowLimit + ) { /* using a branch instead of a cmov, * because it's faster in scenarios where matchIdx >= idxLowLimit is generally true, @@ -162,7 +185,15 @@ namespace ZstdSharp.Unsafe * This is also the work we do at the beginning to enter the loop initially. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_compressBlock_fast_noDict_generic(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls, int useCmov) + private static nuint ZSTD_compressBlock_fast_noDict_generic( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize, + uint mls, + int useCmov + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashTable = ms->hashTable; @@ -184,7 +215,8 @@ namespace ZstdSharp.Unsafe uint current0; uint rep_offset1 = rep[0]; uint rep_offset2 = rep[1]; - uint offsetSaved1 = 0, offsetSaved2 = 0; + uint offsetSaved1 = 0, + offsetSaved2 = 0; /* hash for ip0 */ nuint hash0; /* hash for ip1 */ @@ -201,7 +233,10 @@ namespace ZstdSharp.Unsafe nuint step; byte* nextStep; const nuint kStepIncr = 1 << 8 - 1; - void* matchFound = useCmov != 0 ? (delegate* managed)(&ZSTD_match4Found_cmov) : (delegate* managed)(&ZSTD_match4Found_branch); + void* matchFound = + useCmov != 0 + ? (delegate* managed)(&ZSTD_match4Found_cmov) + : (delegate* managed)(&ZSTD_match4Found_branch); ip0 += ip0 == prefixStart ? 1 : 0; { uint curr = (uint)(ip0 - @base); @@ -220,7 +255,7 @@ namespace ZstdSharp.Unsafe } } - _start: + _start: step = stepSize; nextStep = ip0 + kStepIncr; ip1 = ip0 + 1; @@ -255,7 +290,14 @@ namespace ZstdSharp.Unsafe goto _match; } - if (((delegate* managed)matchFound)(ip0, @base + matchIdx, matchIdx, prefixStartIndex) != 0) + if ( + ((delegate* managed)matchFound)( + ip0, + @base + matchIdx, + matchIdx, + prefixStartIndex + ) != 0 + ) { hashTable[hash1] = (uint)(ip1 - @base); goto _offset; @@ -269,7 +311,14 @@ namespace ZstdSharp.Unsafe ip2 = ip3; current0 = (uint)(ip0 - @base); hashTable[hash0] = current0; - if (((delegate* managed)matchFound)(ip0, @base + matchIdx, matchIdx, prefixStartIndex) != 0) + if ( + ((delegate* managed)matchFound)( + ip0, + @base + matchIdx, + matchIdx, + prefixStartIndex + ) != 0 + ) { if (step <= 4) { @@ -299,14 +348,13 @@ namespace ZstdSharp.Unsafe nextStep += kStepIncr; } - } - while (ip3 < ilimit); - _cleanup: + } while (ip3 < ilimit); + _cleanup: offsetSaved2 = offsetSaved1 != 0 && rep_offset1 != 0 ? offsetSaved1 : offsetSaved2; rep[0] = rep_offset1 != 0 ? rep_offset1 : offsetSaved1; rep[1] = rep_offset2 != 0 ? rep_offset2 : offsetSaved2; return (nuint)(iend - anchor); - _offset: + _offset: match0 = @base + matchIdx; rep_offset2 = rep_offset1; rep_offset1 = (uint)(ip0 - match0); @@ -320,7 +368,7 @@ namespace ZstdSharp.Unsafe mLength++; } - _match: + _match: mLength += ZSTD_count(ip0 + mLength, match0 + mLength, iend); ZSTD_storeSeq(seqStore, (nuint)(ip0 - anchor), anchor, iend, offcode, mLength); ip0 += mLength; @@ -357,47 +405,101 @@ namespace ZstdSharp.Unsafe goto _start; } - private static nuint ZSTD_compressBlock_fast_noDict_4_1(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_noDict_4_1( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_noDict_generic(ms, seqStore, rep, src, srcSize, 4, 1); } - private static nuint ZSTD_compressBlock_fast_noDict_5_1(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_noDict_5_1( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_noDict_generic(ms, seqStore, rep, src, srcSize, 5, 1); } - private static nuint ZSTD_compressBlock_fast_noDict_6_1(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_noDict_6_1( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_noDict_generic(ms, seqStore, rep, src, srcSize, 6, 1); } - private static nuint ZSTD_compressBlock_fast_noDict_7_1(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_noDict_7_1( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_noDict_generic(ms, seqStore, rep, src, srcSize, 7, 1); } - private static nuint ZSTD_compressBlock_fast_noDict_4_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_noDict_4_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_noDict_generic(ms, seqStore, rep, src, srcSize, 4, 0); } - private static nuint ZSTD_compressBlock_fast_noDict_5_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_noDict_5_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_noDict_generic(ms, seqStore, rep, src, srcSize, 5, 0); } - private static nuint ZSTD_compressBlock_fast_noDict_6_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_noDict_6_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_noDict_generic(ms, seqStore, rep, src, srcSize, 6, 0); } - private static nuint ZSTD_compressBlock_fast_noDict_7_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_noDict_7_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_noDict_generic(ms, seqStore, rep, src, srcSize, 7, 0); } - private static nuint ZSTD_compressBlock_fast(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { uint mml = ms->cParams.minMatch; /* use cmov when "candidate in range" branch is likely unpredictable */ @@ -436,7 +538,15 @@ namespace ZstdSharp.Unsafe } [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, uint hasStep) + private static nuint ZSTD_compressBlock_fast_dictMatchState_generic( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize, + uint mls, + uint hasStep + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashTable = ms->hashTable; @@ -453,7 +563,8 @@ namespace ZstdSharp.Unsafe byte* prefixStart = @base + prefixStartIndex; byte* iend = istart + srcSize; byte* ilimit = iend - 8; - uint offset_1 = rep[0], offset_2 = rep[1]; + uint offset_1 = rep[0], + offset_2 = rep[1]; ZSTD_MatchState_t* dms = ms->dictMatchState; ZSTD_compressionParameters* dictCParams = &dms->cParams; uint* dictHashTable = dms->hashTable; @@ -509,14 +620,27 @@ namespace ZstdSharp.Unsafe { byte* match = @base + matchIndex; uint repIndex = curr + 1 - offset_1; - byte* repMatch = repIndex < prefixStartIndex ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; + byte* repMatch = + repIndex < prefixStartIndex + ? dictBase + (repIndex - dictIndexDelta) + : @base + repIndex; nuint hash1 = ZSTD_hashPtr(ip1, hlog, mls); nuint dictHashAndTag1 = ZSTD_hashPtr(ip1, dictHBits, mls); hashTable[hash0] = curr; - if (ZSTD_index_overlap_check(prefixStartIndex, repIndex) != 0 && MEM_read32(repMatch) == MEM_read32(ip0 + 1)) + if ( + ZSTD_index_overlap_check(prefixStartIndex, repIndex) != 0 + && MEM_read32(repMatch) == MEM_read32(ip0 + 1) + ) { byte* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; - mLength = ZSTD_count_2segments(ip0 + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixStart) + 4; + mLength = + ZSTD_count_2segments( + ip0 + 1 + 4, + repMatch + 4, + iend, + repMatchEnd, + prefixStart + ) + 4; ip0++; assert(1 >= 1); assert(1 <= 3); @@ -529,13 +653,27 @@ namespace ZstdSharp.Unsafe /* Found a possible dict match */ uint dictMatchIndex = dictMatchIndexAndTag >> 8; byte* dictMatch = dictBase + dictMatchIndex; - if (dictMatchIndex > dictStartIndex && MEM_read32(dictMatch) == MEM_read32(ip0)) + if ( + dictMatchIndex > dictStartIndex + && MEM_read32(dictMatch) == MEM_read32(ip0) + ) { if (matchIndex <= prefixStartIndex) { uint offset = curr - dictMatchIndex - dictIndexDelta; - mLength = ZSTD_count_2segments(ip0 + 4, dictMatch + 4, iend, dictEnd, prefixStart) + 4; - while (ip0 > anchor && dictMatch > dictStart && ip0[-1] == dictMatch[-1]) + mLength = + ZSTD_count_2segments( + ip0 + 4, + dictMatch + 4, + iend, + dictEnd, + prefixStart + ) + 4; + while ( + ip0 > anchor + && dictMatch > dictStart + && ip0[-1] == dictMatch[-1] + ) { ip0--; dictMatch--; @@ -545,7 +683,14 @@ namespace ZstdSharp.Unsafe offset_2 = offset_1; offset_1 = offset; assert(offset > 0); - ZSTD_storeSeq(seqStore, (nuint)(ip0 - anchor), anchor, iend, offset + 3, mLength); + ZSTD_storeSeq( + seqStore, + (nuint)(ip0 - anchor), + anchor, + iend, + offset + 3, + mLength + ); break; } } @@ -566,7 +711,14 @@ namespace ZstdSharp.Unsafe offset_2 = offset_1; offset_1 = offset; assert(offset > 0); - ZSTD_storeSeq(seqStore, (nuint)(ip0 - anchor), anchor, iend, offset + 3, mLength); + ZSTD_storeSeq( + seqStore, + (nuint)(ip0 - anchor), + anchor, + iend, + offset + 3, + mLength + ); break; } @@ -599,11 +751,24 @@ namespace ZstdSharp.Unsafe { uint current2 = (uint)(ip0 - @base); uint repIndex2 = current2 - offset_2; - byte* repMatch2 = repIndex2 < prefixStartIndex ? dictBase - dictIndexDelta + repIndex2 : @base + repIndex2; - if (ZSTD_index_overlap_check(prefixStartIndex, repIndex2) != 0 && MEM_read32(repMatch2) == MEM_read32(ip0)) + byte* repMatch2 = + repIndex2 < prefixStartIndex + ? dictBase - dictIndexDelta + repIndex2 + : @base + repIndex2; + if ( + ZSTD_index_overlap_check(prefixStartIndex, repIndex2) != 0 + && MEM_read32(repMatch2) == MEM_read32(ip0) + ) { byte* repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; - nuint repLength2 = ZSTD_count_2segments(ip0 + 4, repMatch2 + 4, iend, repEnd2, prefixStart) + 4; + nuint repLength2 = + ZSTD_count_2segments( + ip0 + 4, + repMatch2 + 4, + iend, + repEnd2, + prefixStart + ) + 4; /* swap offset_2 <=> offset_1 */ uint tmpOffset = offset_2; offset_2 = offset_1; @@ -625,33 +790,95 @@ namespace ZstdSharp.Unsafe ip1 = ip0 + stepSize; } - _cleanup: + _cleanup: rep[0] = offset_1; rep[1] = offset_2; return (nuint)(iend - anchor); } - private static nuint ZSTD_compressBlock_fast_dictMatchState_4_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_dictMatchState_4_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4, 0); + return ZSTD_compressBlock_fast_dictMatchState_generic( + ms, + seqStore, + rep, + src, + srcSize, + 4, + 0 + ); } - private static nuint ZSTD_compressBlock_fast_dictMatchState_5_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_dictMatchState_5_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5, 0); + return ZSTD_compressBlock_fast_dictMatchState_generic( + ms, + seqStore, + rep, + src, + srcSize, + 5, + 0 + ); } - private static nuint ZSTD_compressBlock_fast_dictMatchState_6_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_dictMatchState_6_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6, 0); + return ZSTD_compressBlock_fast_dictMatchState_generic( + ms, + seqStore, + rep, + src, + srcSize, + 6, + 0 + ); } - private static nuint ZSTD_compressBlock_fast_dictMatchState_7_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_dictMatchState_7_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7, 0); + return ZSTD_compressBlock_fast_dictMatchState_generic( + ms, + seqStore, + rep, + src, + srcSize, + 7, + 0 + ); } - private static nuint ZSTD_compressBlock_fast_dictMatchState(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); @@ -659,17 +886,49 @@ namespace ZstdSharp.Unsafe { default: case 4: - return ZSTD_compressBlock_fast_dictMatchState_4_0(ms, seqStore, rep, src, srcSize); + return ZSTD_compressBlock_fast_dictMatchState_4_0( + ms, + seqStore, + rep, + src, + srcSize + ); case 5: - return ZSTD_compressBlock_fast_dictMatchState_5_0(ms, seqStore, rep, src, srcSize); + return ZSTD_compressBlock_fast_dictMatchState_5_0( + ms, + seqStore, + rep, + src, + srcSize + ); case 6: - return ZSTD_compressBlock_fast_dictMatchState_6_0(ms, seqStore, rep, src, srcSize); + return ZSTD_compressBlock_fast_dictMatchState_6_0( + ms, + seqStore, + rep, + src, + srcSize + ); case 7: - return ZSTD_compressBlock_fast_dictMatchState_7_0(ms, seqStore, rep, src, srcSize); + return ZSTD_compressBlock_fast_dictMatchState_7_0( + ms, + seqStore, + rep, + src, + srcSize + ); } } - private static nuint ZSTD_compressBlock_fast_extDict_generic(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize, uint mls, uint hasStep) + private static nuint ZSTD_compressBlock_fast_extDict_generic( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize, + uint mls, + uint hasStep + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashTable = ms->hashTable; @@ -690,8 +949,10 @@ namespace ZstdSharp.Unsafe byte* dictEnd = dictBase + prefixStartIndex; byte* iend = istart + srcSize; byte* ilimit = iend - 8; - uint offset_1 = rep[0], offset_2 = rep[1]; - uint offsetSaved1 = 0, offsetSaved2 = 0; + uint offset_1 = rep[0], + offset_2 = rep[1]; + uint offsetSaved1 = 0, + offsetSaved2 = 0; byte* ip0 = istart; byte* ip1; byte* ip2; @@ -731,7 +992,7 @@ namespace ZstdSharp.Unsafe } } - _start: + _start: step = stepSize; nextStep = ip0 + kStepIncr; ip1 = ip0 + 1; @@ -782,7 +1043,8 @@ namespace ZstdSharp.Unsafe } { - uint mval = idx >= dictStartIndex ? MEM_read32(idxBase + idx) : MEM_read32(ip0) ^ 1; + uint mval = + idx >= dictStartIndex ? MEM_read32(idxBase + idx) : MEM_read32(ip0) ^ 1; if (MEM_read32(ip0) == mval) { goto _offset; @@ -799,7 +1061,8 @@ namespace ZstdSharp.Unsafe current0 = (uint)(ip0 - @base); hashTable[hash0] = current0; { - uint mval = idx >= dictStartIndex ? MEM_read32(idxBase + idx) : MEM_read32(ip0) ^ 1; + uint mval = + idx >= dictStartIndex ? MEM_read32(idxBase + idx) : MEM_read32(ip0) ^ 1; if (MEM_read32(ip0) == mval) { goto _offset; @@ -827,14 +1090,13 @@ namespace ZstdSharp.Unsafe nextStep += kStepIncr; } - } - while (ip3 < ilimit); - _cleanup: + } while (ip3 < ilimit); + _cleanup: offsetSaved2 = offsetSaved1 != 0 && offset_1 != 0 ? offsetSaved1 : offsetSaved2; rep[0] = offset_1 != 0 ? offset_1 : offsetSaved1; rep[1] = offset_2 != 0 ? offset_2 : offsetSaved2; return (nuint)(iend - anchor); - _offset: + _offset: { uint offset = current0 - idx; byte* lowMatchPtr = idx < prefixStartIndex ? dictStart : prefixStart; @@ -853,9 +1115,15 @@ namespace ZstdSharp.Unsafe } } - _match: + _match: assert(matchEnd != null); - mLength += ZSTD_count_2segments(ip0 + mLength, match0 + mLength, iend, matchEnd, prefixStart); + mLength += ZSTD_count_2segments( + ip0 + mLength, + match0 + mLength, + iend, + matchEnd, + prefixStart + ); ZSTD_storeSeq(seqStore, (nuint)(ip0 - anchor), anchor, iend, offcode, mLength); ip0 += mLength; anchor = ip0; @@ -872,11 +1140,20 @@ namespace ZstdSharp.Unsafe while (ip0 <= ilimit) { uint repIndex2 = (uint)(ip0 - @base) - offset_2; - byte* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : @base + repIndex2; - if ((ZSTD_index_overlap_check(prefixStartIndex, repIndex2) & (offset_2 > 0 ? 1 : 0)) != 0 && MEM_read32(repMatch2) == MEM_read32(ip0)) + byte* repMatch2 = + repIndex2 < prefixStartIndex ? dictBase + repIndex2 : @base + repIndex2; + if ( + ( + ZSTD_index_overlap_check(prefixStartIndex, repIndex2) + & (offset_2 > 0 ? 1 : 0) + ) != 0 + && MEM_read32(repMatch2) == MEM_read32(ip0) + ) { byte* repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; - nuint repLength2 = ZSTD_count_2segments(ip0 + 4, repMatch2 + 4, iend, repEnd2, prefixStart) + 4; + nuint repLength2 = + ZSTD_count_2segments(ip0 + 4, repMatch2 + 4, iend, repEnd2, prefixStart) + + 4; { /* swap offset_2 <=> offset_1 */ uint tmpOffset = offset_2; @@ -900,27 +1177,57 @@ namespace ZstdSharp.Unsafe goto _start; } - private static nuint ZSTD_compressBlock_fast_extDict_4_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_extDict_4_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4, 0); } - private static nuint ZSTD_compressBlock_fast_extDict_5_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_extDict_5_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5, 0); } - private static nuint ZSTD_compressBlock_fast_extDict_6_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_extDict_6_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6, 0); } - private static nuint ZSTD_compressBlock_fast_extDict_7_0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_extDict_7_0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7, 0); } - private static nuint ZSTD_compressBlock_fast_extDict(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_fast_extDict( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { uint mls = ms->cParams.minMatch; assert(ms->dictMatchState == null); diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdInternal.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdInternal.cs index 2ad2c628..e6ccfb0c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdInternal.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdInternal.cs @@ -1,7 +1,7 @@ -using static ZstdSharp.UnsafeHelper; using System; -using System.Runtime.InteropServices; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; #if NETCOREAPP3_0_OR_GREATER using System.Runtime.Intrinsics.X86; #endif @@ -14,275 +14,531 @@ namespace ZstdSharp.Unsafe public static unsafe partial class Methods { #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_repStartValue => new uint[3] - { - 1, - 4, - 8 - }; - private static uint* repStartValue => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_repStartValue)); + private static ReadOnlySpan Span_repStartValue => new uint[3] { 1, 4, 8 }; + private static uint* repStartValue => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_repStartValue) + ); #else private static readonly uint* repStartValue = GetArrayPointer(new uint[3] { 1, 4, 8 }); #endif - private static readonly nuint* ZSTD_fcs_fieldSize = GetArrayPointer(new nuint[4] { 0, 2, 4, 8 }); - private static readonly nuint* ZSTD_did_fieldSize = GetArrayPointer(new nuint[4] { 0, 1, 2, 4 }); + private static readonly nuint* ZSTD_fcs_fieldSize = GetArrayPointer( + new nuint[4] { 0, 2, 4, 8 } + ); + private static readonly nuint* ZSTD_did_fieldSize = GetArrayPointer( + new nuint[4] { 0, 1, 2, 4 } + ); private const uint ZSTD_blockHeaderSize = 3; #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_LL_bits => new byte[36] - { - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 1, - 1, - 2, - 2, - 3, - 3, - 4, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16 - }; - private static byte* LL_bits => (byte*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_LL_bits)); + private static ReadOnlySpan Span_LL_bits => + new byte[36] + { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 2, + 2, + 3, + 3, + 4, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + }; + private static byte* LL_bits => + (byte*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_LL_bits) + ); #else - private static readonly byte* LL_bits = GetArrayPointer(new byte[36] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }); + private static readonly byte* LL_bits = GetArrayPointer( + new byte[36] + { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 2, + 2, + 3, + 3, + 4, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + } + ); #endif #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_LL_defaultNorm => new short[36] - { - 4, - 3, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 1, - 1, - 1, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 2, - 3, - 2, - 1, - 1, - 1, - 1, - 1, - -1, - -1, - -1, - -1 - }; - private static short* LL_defaultNorm => (short*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_LL_defaultNorm)); + private static ReadOnlySpan Span_LL_defaultNorm => + new short[36] + { + 4, + 3, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 1, + 1, + 1, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 3, + 2, + 1, + 1, + 1, + 1, + 1, + -1, + -1, + -1, + -1, + }; + private static short* LL_defaultNorm => + (short*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_LL_defaultNorm) + ); #else - private static readonly 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, (short)(-1), (short)(-1), (short)(-1), (short)(-1) }); + private static readonly 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, + (short)(-1), + (short)(-1), + (short)(-1), + (short)(-1), + } + ); #endif private const uint LL_defaultNormLog = 6; #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_ML_bits => new byte[53] - { - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 1, - 1, - 2, - 2, - 3, - 3, - 4, - 4, - 5, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16 - }; - private static byte* ML_bits => (byte*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_ML_bits)); + private static ReadOnlySpan Span_ML_bits => + new byte[53] + { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 2, + 2, + 3, + 3, + 4, + 4, + 5, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + }; + private static byte* ML_bits => + (byte*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_ML_bits) + ); #else - private static readonly byte* ML_bits = GetArrayPointer(new byte[53] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }); + private static readonly byte* ML_bits = GetArrayPointer( + new byte[53] + { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 2, + 2, + 3, + 3, + 4, + 4, + 5, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + } + ); #endif #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_ML_defaultNorm => new short[53] - { - 1, - 4, - 3, - 2, - 2, - 2, - 2, - 2, - 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - }; - private static short* ML_defaultNorm => (short*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_ML_defaultNorm)); + private static ReadOnlySpan Span_ML_defaultNorm => + new short[53] + { + 1, + 4, + 3, + 2, + 2, + 2, + 2, + 2, + 2, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + }; + private static short* ML_defaultNorm => + (short*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_ML_defaultNorm) + ); #else - private static readonly 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, (short)(-1), (short)(-1), (short)(-1), (short)(-1), (short)(-1), (short)(-1), (short)(-1) }); + private static readonly 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, + (short)(-1), + (short)(-1), + (short)(-1), + (short)(-1), + (short)(-1), + (short)(-1), + (short)(-1), + } + ); #endif private const uint ML_defaultNormLog = 6; #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_OF_defaultNorm => new short[29] - { - 1, - 1, - 1, - 1, - 1, - 1, - 2, - 2, - 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - -1, - -1, - -1, - -1, - -1 - }; - private static short* OF_defaultNorm => (short*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_OF_defaultNorm)); + private static ReadOnlySpan Span_OF_defaultNorm => + new short[29] + { + 1, + 1, + 1, + 1, + 1, + 1, + 2, + 2, + 2, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + -1, + -1, + -1, + -1, + -1, + }; + private static short* OF_defaultNorm => + (short*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_OF_defaultNorm) + ); #else - private static readonly 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, (short)(-1), (short)(-1), (short)(-1), (short)(-1), (short)(-1) }); + private static readonly 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, + (short)(-1), + (short)(-1), + (short)(-1), + (short)(-1), + (short)(-1), + } + ); #endif private const uint OF_defaultNormLog = 5; + /*-******************************************* * Shared functions to include for inlining *********************************************/ @@ -314,7 +570,9 @@ namespace ZstdSharp.Unsafe #endif { var v1 = System.Runtime.CompilerServices.Unsafe.ReadUnaligned((ulong*)src); - var v2 = System.Runtime.CompilerServices.Unsafe.ReadUnaligned((ulong*)src + 1); + var v2 = System.Runtime.CompilerServices.Unsafe.ReadUnaligned( + (ulong*)src + 1 + ); System.Runtime.CompilerServices.Unsafe.WriteUnaligned((ulong*)dst, v1); System.Runtime.CompilerServices.Unsafe.WriteUnaligned((ulong*)dst + 1, v2); } @@ -341,8 +599,7 @@ namespace ZstdSharp.Unsafe ZSTD_copy8(op, ip); op += 8; ip += 8; - } - while (op < oend); + } while (op < oend); } else { @@ -365,8 +622,7 @@ namespace ZstdSharp.Unsafe op += 16; ip += 16; } - } - while (op < oend); + } while (op < oend); } } diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLazy.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLazy.cs index c537716e..b5a3d777 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLazy.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLazy.cs @@ -1,7 +1,7 @@ -using static ZstdSharp.UnsafeHelper; -using System.Runtime.CompilerServices; -using System.Numerics; using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; #if NETCOREAPP3_0_OR_GREATER using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; @@ -49,13 +49,21 @@ namespace ZstdSharp.Unsafe * sort one already inserted but unsorted position * assumption : curr >= btlow == (curr - btmask) * doesn't fail */ - private static void ZSTD_insertDUBT1(ZSTD_MatchState_t* ms, uint curr, byte* inputEnd, uint nbCompares, uint btLow, ZSTD_dictMode_e dictMode) + 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; + nuint commonLengthSmaller = 0, + commonLengthLarger = 0; byte* @base = ms->window.@base; byte* dictBase = ms->window.dictBase; uint dictLimit = ms->window.dictLimit; @@ -79,11 +87,22 @@ namespace ZstdSharp.Unsafe { uint* nextPtr = bt + 2 * (matchIndex & btMask); /* guaranteed minimum nb of common bytes */ - nuint matchLength = commonLengthSmaller < commonLengthLarger ? commonLengthSmaller : commonLengthLarger; + nuint matchLength = + commonLengthSmaller < commonLengthLarger + ? commonLengthSmaller + : commonLengthLarger; assert(matchIndex < curr); - if (dictMode != ZSTD_dictMode_e.ZSTD_extDict || matchIndex + matchLength >= dictLimit || curr < dictLimit) + 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; + 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); @@ -91,7 +110,13 @@ namespace ZstdSharp.Unsafe else { match = dictBase + matchIndex; - matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart); + matchLength += ZSTD_count_2segments( + ip + matchLength, + match + matchLength, + iend, + dictEnd, + prefixStart + ); if (matchIndex + matchLength >= dictLimit) match = @base + matchIndex; } @@ -132,7 +157,16 @@ namespace ZstdSharp.Unsafe *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) + 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; @@ -151,22 +185,39 @@ namespace ZstdSharp.Unsafe 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; + uint btLow = + btMask >= dictHighLimit - dictLowLimit ? dictLowLimit : dictHighLimit - btMask; + nuint commonLengthSmaller = 0, + commonLengthLarger = 0; assert(dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState); for (; nbCompares != 0 && dictMatchIndex > dictLowLimit; --nbCompares) { uint* nextPtr = dictBt + 2 * (dictMatchIndex & btMask); /* guaranteed minimum nb of common bytes */ - nuint matchLength = commonLengthSmaller < commonLengthLarger ? commonLengthSmaller : commonLengthLarger; + nuint matchLength = + commonLengthSmaller < commonLengthLarger + ? commonLengthSmaller + : commonLengthLarger; byte* match = dictBase + dictMatchIndex; - matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart); + 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))) + if ( + 4 * (int)(matchLength - bestLength) + > (int)( + ZSTD_highbit32(curr - matchIndex + 1) + - ZSTD_highbit32((uint)offsetPtr[0] + 1) + ) + ) { bestLength = matchLength; assert(curr - matchIndex > 0); @@ -210,7 +261,14 @@ namespace ZstdSharp.Unsafe return bestLength; } - private static nuint ZSTD_DUBT_findBestMatch(ZSTD_MatchState_t* ms, byte* ip, byte* iend, nuint* offBasePtr, uint mls, ZSTD_dictMode_e dictMode) + private static nuint ZSTD_DUBT_findBestMatch( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iend, + nuint* offBasePtr, + uint mls, + ZSTD_dictMode_e dictMode + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashTable = ms->hashTable; @@ -258,7 +316,8 @@ namespace ZstdSharp.Unsafe } { - nuint commonLengthSmaller = 0, commonLengthLarger = 0; + nuint commonLengthSmaller = 0, + commonLengthLarger = 0; byte* dictBase = ms->window.dictBase; uint dictLimit = ms->window.dictLimit; byte* dictEnd = dictBase + dictLimit; @@ -275,9 +334,15 @@ namespace ZstdSharp.Unsafe { uint* nextPtr = bt + 2 * (matchIndex & btMask); /* guaranteed minimum nb of common bytes */ - nuint matchLength = commonLengthSmaller < commonLengthLarger ? commonLengthSmaller : commonLengthLarger; + nuint matchLength = + commonLengthSmaller < commonLengthLarger + ? commonLengthSmaller + : commonLengthLarger; byte* match; - if (dictMode != ZSTD_dictMode_e.ZSTD_extDict || matchIndex + matchLength >= dictLimit) + if ( + dictMode != ZSTD_dictMode_e.ZSTD_extDict + || matchIndex + matchLength >= dictLimit + ) { match = @base + matchIndex; matchLength += ZSTD_count(ip + matchLength, match + matchLength, iend); @@ -285,7 +350,13 @@ namespace ZstdSharp.Unsafe else { match = dictBase + matchIndex; - matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart); + matchLength += ZSTD_count_2segments( + ip + matchLength, + match + matchLength, + iend, + dictEnd, + prefixStart + ); if (matchIndex + matchLength >= dictLimit) match = @base + matchIndex; } @@ -294,7 +365,13 @@ namespace ZstdSharp.Unsafe { if (matchLength > matchEndIdx - matchIndex) matchEndIdx = matchIndex + (uint)matchLength; - if (4 * (int)(matchLength - bestLength) > (int)(ZSTD_highbit32(curr - matchIndex + 1) - ZSTD_highbit32((uint)*offBasePtr))) + if ( + 4 * (int)(matchLength - bestLength) + > (int)( + ZSTD_highbit32(curr - matchIndex + 1) + - ZSTD_highbit32((uint)*offBasePtr) + ) + ) { bestLength = matchLength; assert(curr - matchIndex > 0); @@ -344,7 +421,16 @@ namespace ZstdSharp.Unsafe assert(nbCompares <= 1U << (sizeof(nuint) == 4 ? 30 : 31) - 1); if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState && nbCompares != 0) { - bestLength = ZSTD_DUBT_findBetterDictMatch(ms, ip, iend, offBasePtr, bestLength, nbCompares, mls, dictMode); + bestLength = ZSTD_DUBT_findBetterDictMatch( + ms, + ip, + iend, + offBasePtr, + bestLength, + nbCompares, + mls, + dictMode + ); } assert(matchEndIdx > curr + 8); @@ -361,7 +447,14 @@ namespace ZstdSharp.Unsafe /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_BtFindBestMatch(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr, uint mls, ZSTD_dictMode_e dictMode) + private static nuint ZSTD_BtFindBestMatch( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr, + uint mls, + ZSTD_dictMode_e dictMode + ) { if (ip < ms->window.@base + ms->nextToUpdate) return 0; @@ -372,7 +465,10 @@ namespace ZstdSharp.Unsafe /*********************************** * Dedicated dict search ***********************************/ - private static void ZSTD_dedicatedDictSearch_lazy_loadDictionary(ZSTD_MatchState_t* ms, byte* ip) + private static void ZSTD_dedicatedDictSearch_lazy_loadDictionary( + ZSTD_MatchState_t* ms, + byte* ip + ) { byte* @base = ms->window.@base; uint target = (uint)(ip - @base); @@ -430,7 +526,7 @@ namespace ZstdSharp.Unsafe if (count == cacheSize) { - for (count = 0; count < chainLimit;) + for (count = 0; count < chainLimit; ) { if (i < minChain) { @@ -468,7 +564,7 @@ namespace ZstdSharp.Unsafe assert(chainPos <= chainSize); } - for (hashIdx = (uint)(1 << (int)hashLog); hashIdx != 0;) + for (hashIdx = (uint)(1 << (int)hashLog); hashIdx != 0; ) { uint bucketIdx = --hashIdx << 2; uint chainPackedPointer = tmpHashTable[hashIdx]; @@ -497,7 +593,18 @@ namespace ZstdSharp.Unsafe * 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) + 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; @@ -544,7 +651,8 @@ namespace ZstdSharp.Unsafe assert(match + 4 <= ddsEnd); if (MEM_read32(match) == MEM_read32(ip)) { - currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, ddsEnd, prefixStart) + 4; + currentMl = + ZSTD_count_2segments(ip + 4, match + 4, iLimit, ddsEnd, prefixStart) + 4; } if (currentMl > ml) @@ -586,7 +694,9 @@ namespace ZstdSharp.Unsafe assert(match + 4 <= ddsEnd); if (MEM_read32(match) == MEM_read32(ip)) { - currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, ddsEnd, prefixStart) + 4; + currentMl = + ZSTD_count_2segments(ip + 4, match + 4, iLimit, ddsEnd, prefixStart) + + 4; } if (currentMl > ml) @@ -606,7 +716,13 @@ namespace ZstdSharp.Unsafe /* 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 lazySkipping) + private static uint ZSTD_insertAndFindFirstIndex_internal( + ZSTD_MatchState_t* ms, + ZSTD_compressionParameters* cParams, + byte* ip, + uint mls, + uint lazySkipping + ) { uint* hashTable = ms->hashTable; uint hashLog = cParams->hashLog; @@ -637,7 +753,14 @@ namespace ZstdSharp.Unsafe /* inlining is important to hardwire a hot branch (template emulation) */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_HcFindBestMatch(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr, uint mls, ZSTD_dictMode_e dictMode) + private static nuint ZSTD_HcFindBestMatch( + 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; @@ -651,15 +774,20 @@ namespace ZstdSharp.Unsafe 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 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 = 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(ip, ddsHashLog, mls) << 2 : 0; + uint ddsHashLog = + dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch ? dms->cParams.hashLog - 2 : 0; + nuint ddsIdx = + dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ? ZSTD_hashPtr(ip, ddsHashLog, mls) << 2 + : 0; uint matchIndex; if (dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) { @@ -672,7 +800,13 @@ namespace ZstdSharp.Unsafe #endif } - matchIndex = ZSTD_insertAndFindFirstIndex_internal(ms, cParams, ip, mls, (uint)ms->lazySkipping); + matchIndex = ZSTD_insertAndFindFirstIndex_internal( + ms, + cParams, + ip, + mls, + (uint)ms->lazySkipping + ); for (; matchIndex >= lowLimit && nbAttempts > 0; nbAttempts--) { nuint currentMl = 0; @@ -688,7 +822,9 @@ namespace ZstdSharp.Unsafe byte* match = dictBase + matchIndex; assert(match + 4 <= dictEnd); if (MEM_read32(match) == MEM_read32(ip)) - currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, dictEnd, prefixStart) + 4; + currentMl = + ZSTD_count_2segments(ip + 4, match + 4, iLimit, dictEnd, prefixStart) + + 4; } if (currentMl > ml) @@ -708,7 +844,18 @@ namespace ZstdSharp.Unsafe assert(nbAttempts <= 1U << (sizeof(nuint) == 4 ? 30 : 31) - 1); if (dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) { - ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts, dms, ip, iLimit, prefixStart, curr, dictLimit, ddsIdx); + ml = ZSTD_dedicatedDictSearch_lazy_search( + offsetPtr, + ml, + nbAttempts, + dms, + ip, + iLimit, + prefixStart, + curr, + dictLimit, + ddsIdx + ); } else if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) { @@ -728,7 +875,9 @@ namespace ZstdSharp.Unsafe byte* match = dmsBase + matchIndex; assert(match + 4 <= dmsEnd); if (MEM_read32(match) == MEM_read32(ip)) - currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, dmsEnd, prefixStart) + 4; + currentMl = + ZSTD_count_2segments(ip + 4, match + 4, iLimit, dmsEnd, prefixStart) + + 4; if (currentMl > ml) { ml = currentMl; @@ -786,7 +935,12 @@ namespace ZstdSharp.Unsafe * Performs prefetching for the hashTable and tagTable at a given row. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_row_prefetch(uint* hashTable, byte* tagTable, uint relRow, uint rowLog) + private static void ZSTD_row_prefetch( + uint* hashTable, + byte* tagTable, + uint relRow, + uint rowLog + ) { #if NETCOREAPP3_0_OR_GREATER if (Sse.IsSupported) @@ -832,7 +986,14 @@ namespace ZstdSharp.Unsafe * but not beyond iLimit. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_row_fillHashCache(ZSTD_MatchState_t* ms, byte* @base, uint rowLog, uint mls, uint idx, byte* 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; byte* tagTable = ms->tagTable; @@ -853,7 +1014,17 @@ namespace ZstdSharp.Unsafe * base + idx + ZSTD_ROW_HASH_CACHE_SIZE. Also prefetches the appropriate rows from hashTable and tagTable. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint ZSTD_row_nextCachedHash(uint* cache, uint* hashTable, byte* tagTable, byte* @base, uint idx, uint hashLog, uint rowLog, uint mls, ulong hashSalt) + private static uint ZSTD_row_nextCachedHash( + uint* cache, + uint* hashTable, + byte* tagTable, + byte* @base, + uint idx, + uint hashLog, + uint rowLog, + uint mls, + ulong hashSalt + ) { uint newHash = (uint)ZSTD_hashPtrSalted(@base + idx + 8, hashLog + 8, mls, hashSalt); uint row = newHash >> 8 << (int)rowLog; @@ -869,7 +1040,15 @@ namespace ZstdSharp.Unsafe * Updates the hash table with positions starting from updateStartIdx until updateEndIdx. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_row_update_internalImpl(ZSTD_MatchState_t* ms, uint updateStartIdx, uint updateEndIdx, uint mls, uint rowLog, uint rowMask, uint useCache) + private static void ZSTD_row_update_internalImpl( + ZSTD_MatchState_t* ms, + uint updateStartIdx, + uint updateEndIdx, + uint mls, + uint rowLog, + uint rowMask, + uint useCache + ) { uint* hashTable = ms->hashTable; byte* tagTable = ms->tagTable; @@ -877,12 +1056,38 @@ namespace ZstdSharp.Unsafe byte* @base = ms->window.@base; for (; updateStartIdx < updateEndIdx; ++updateStartIdx) { - uint hash = useCache != 0 ? ZSTD_row_nextCachedHash(ms->hashCache, hashTable, tagTable, @base, updateStartIdx, hashLog, rowLog, mls, ms->hashSalt) : (uint)ZSTD_hashPtrSalted(@base + updateStartIdx, hashLog + 8, mls, ms->hashSalt); + uint hash = + useCache != 0 + ? ZSTD_row_nextCachedHash( + ms->hashCache, + hashTable, + tagTable, + @base, + updateStartIdx, + hashLog, + rowLog, + mls, + ms->hashSalt + ) + : (uint)ZSTD_hashPtrSalted( + @base + updateStartIdx, + hashLog + 8, + mls, + ms->hashSalt + ); uint relRow = hash >> 8 << (int)rowLog; uint* row = hashTable + relRow; byte* tagRow = tagTable + relRow; uint pos = ZSTD_row_nextIndex(tagRow, rowMask); - assert(hash == ZSTD_hashPtrSalted(@base + updateStartIdx, hashLog + 8, mls, ms->hashSalt)); + assert( + hash + == ZSTD_hashPtrSalted( + @base + updateStartIdx, + hashLog + 8, + mls, + ms->hashSalt + ) + ); tagRow[pos] = (byte)(hash & (1U << 8) - 1); row[pos] = updateStartIdx; } @@ -893,7 +1098,14 @@ namespace ZstdSharp.Unsafe * Skips sections of long matches as is necessary. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_row_update_internal(ZSTD_MatchState_t* ms, byte* ip, uint mls, uint rowLog, uint rowMask, uint useCache) + private static void ZSTD_row_update_internal( + ZSTD_MatchState_t* ms, + byte* ip, + uint mls, + uint rowLog, + uint rowMask, + uint useCache + ) { uint idx = ms->nextToUpdate; byte* @base = ms->window.@base; @@ -923,7 +1135,10 @@ namespace ZstdSharp.Unsafe */ private static void ZSTD_row_update(ZSTD_MatchState_t* ms, byte* ip) { - uint rowLog = ms->cParams.searchLog <= 4 ? 4 : ms->cParams.searchLog <= 6 ? ms->cParams.searchLog : 6; + uint rowLog = + ms->cParams.searchLog <= 4 ? 4 + : ms->cParams.searchLog <= 6 ? ms->cParams.searchLog + : 6; uint rowMask = (1U << (int)rowLog) - 1; /* mls caps out at 6 */ uint mls = ms->cParams.minMatch < 6 ? ms->cParams.minMatch : 6; @@ -996,7 +1211,13 @@ namespace ZstdSharp.Unsafe Vector128 chunk3 = Sse2.LoadVector128(src + 16 * 3); Vector128 equalMask3 = Sse2.CompareEqual(chunk3, comparisonMask); int matches3 = Sse2.MoveMask(equalMask3); - return BitOperations.RotateRight((ulong)matches3 << 48 | (ulong)matches2 << 32 | (ulong)matches1 << 16 | (uint)matches0, (int)head); + return BitOperations.RotateRight( + (ulong)matches3 << 48 + | (ulong)matches2 << 32 + | (ulong)matches1 << 16 + | (uint)matches0, + (int)head + ); } } #endif @@ -1008,7 +1229,12 @@ namespace ZstdSharp.Unsafe * must rotate the "matches" bitfield to match up with the actual layout of the * entries within the hashTable */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ulong ZSTD_row_getMatchMask(byte* tagRow, byte tag, uint headGrouped, uint rowEntries) + private static ulong ZSTD_row_getMatchMask( + byte* tagRow, + byte tag, + uint headGrouped, + uint rowEntries + ) { byte* src = tagRow; assert(rowEntries == 16 || rowEntries == 32 || rowEntries == 64); @@ -1032,10 +1258,13 @@ namespace ZstdSharp.Unsafe * 0x88 = 0b10001000. */ Vector128 chunk = AdvSimd.LoadVector128(src); - Vector128 equalMask = AdvSimd.CompareEqual(chunk, AdvSimd.DuplicateToVector128(tag)).As(); + Vector128 equalMask = AdvSimd + .CompareEqual(chunk, AdvSimd.DuplicateToVector128(tag)) + .As(); Vector64 res = AdvSimd.ShiftRightLogicalNarrowingLower(equalMask, 4); ulong matches = res.As().GetElement(0); - return BitOperations.RotateRight(matches, (int)headGrouped) & 0x8888888888888888; + return BitOperations.RotateRight(matches, (int)headGrouped) + & 0x8888888888888888; } else if (rowEntries == 32) { @@ -1045,13 +1274,21 @@ namespace ZstdSharp.Unsafe /* Same idea as with rowEntries == 16 but doing AND with * 0x55 = 0b01010101. */ - (Vector128 chunk0, Vector128 chunk1) = AdvSimd.Arm64.Load2xVector128AndUnzip((ushort*)src); + (Vector128 chunk0, Vector128 chunk1) = + AdvSimd.Arm64.Load2xVector128AndUnzip((ushort*)src); Vector128 dup = AdvSimd.DuplicateToVector128(tag); - Vector64 t0 = AdvSimd.ShiftRightLogicalNarrowingLower(AdvSimd.CompareEqual(chunk0.As(), dup).As(), 6); - Vector64 t1 = AdvSimd.ShiftRightLogicalNarrowingLower(AdvSimd.CompareEqual(chunk1.As(), dup).As(), 6); + Vector64 t0 = AdvSimd.ShiftRightLogicalNarrowingLower( + AdvSimd.CompareEqual(chunk0.As(), dup).As(), + 6 + ); + Vector64 t1 = AdvSimd.ShiftRightLogicalNarrowingLower( + AdvSimd.CompareEqual(chunk1.As(), dup).As(), + 6 + ); Vector64 res = AdvSimd.ShiftLeftAndInsert(t0, t1, 4); ulong matches = res.As().GetElement(0); - return BitOperations.RotateRight(matches, (int)headGrouped) & 0x5555555555555555; + return BitOperations.RotateRight(matches, (int)headGrouped) + & 0x5555555555555555; } #endif } @@ -1060,7 +1297,12 @@ namespace ZstdSharp.Unsafe #if NET9_0_OR_GREATER if (AdvSimd.Arm64.IsSupported) { - (Vector128 chunk0, Vector128 chunk1, Vector128 chunk2, Vector128 chunk3) = AdvSimd.Arm64.Load4xVector128AndUnzip(src); + ( + Vector128 chunk0, + Vector128 chunk1, + Vector128 chunk2, + Vector128 chunk3 + ) = AdvSimd.Arm64.Load4xVector128AndUnzip(src); Vector128 dup = AdvSimd.DuplicateToVector128(tag); Vector128 cmp0 = AdvSimd.CompareEqual(chunk0, dup); Vector128 cmp1 = AdvSimd.CompareEqual(chunk1, dup); @@ -1071,9 +1313,12 @@ namespace ZstdSharp.Unsafe Vector128 t1 = AdvSimd.ShiftRightAndInsert(cmp3, cmp2, 1); Vector128 t2 = AdvSimd.ShiftRightAndInsert(t1, t0, 2); Vector128 t3 = AdvSimd.ShiftRightAndInsert(t2, t2, 4); - Vector64 t4 = AdvSimd.ShiftRightLogicalNarrowingLower(t3.As(), 4); + Vector64 t4 = AdvSimd.ShiftRightLogicalNarrowingLower( + t3.As(), + 4 + ); ulong matches = t4.As().GetElement(0); - return BitOperations.RotateRight(matches, (int) headGrouped); + return BitOperations.RotateRight(matches, (int)headGrouped); } #endif } @@ -1101,8 +1346,7 @@ namespace ZstdSharp.Unsafe matches <<= (int)chunkSize; matches |= chunk * extractMagic >> (int)shiftAmount; i -= (int)chunkSize; - } - while (i >= 0); + } while (i >= 0); } else { @@ -1116,8 +1360,7 @@ namespace ZstdSharp.Unsafe matches <<= (int)chunkSize; matches |= (chunk >> 7) * extractMagic >> (int)shiftAmount; i -= (int)chunkSize; - } - while (i >= 0); + } while (i >= 0); } matches = ~matches; @@ -1152,7 +1395,15 @@ namespace ZstdSharp.Unsafe * - Insert the tag into the equivalent row and position in the tagTable. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_RowFindBestMatch(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr, uint mls, ZSTD_dictMode_e dictMode, uint rowLog) + private static nuint ZSTD_RowFindBestMatch( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr, + uint mls, + ZSTD_dictMode_e dictMode, + uint rowLog + ) { uint* hashTable = ms->hashTable; byte* tagTable = ms->tagTable; @@ -1167,7 +1418,8 @@ namespace ZstdSharp.Unsafe 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 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; @@ -1201,7 +1453,8 @@ namespace ZstdSharp.Unsafe #endif } - ddsExtraAttempts = cParams->searchLog > rowLog ? 1U << (int)(cParams->searchLog - rowLog) : 0; + ddsExtraAttempts = + cParams->searchLog > rowLog ? 1U << (int)(cParams->searchLog - rowLog) : 0; } if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) @@ -1220,7 +1473,17 @@ namespace ZstdSharp.Unsafe if (ms->lazySkipping == 0) { ZSTD_row_update_internal(ms, ip, mls, rowLog, rowMask, 1); - hash = ZSTD_row_nextCachedHash(hashCache, hashTable, tagTable, @base, curr, hashLog, rowLog, mls, hashSalt); + hash = ZSTD_row_nextCachedHash( + hashCache, + hashTable, + tagTable, + @base, + curr, + hashLog, + rowLog, + mls, + hashSalt + ); } else { @@ -1241,7 +1504,8 @@ namespace ZstdSharp.Unsafe ulong matches = ZSTD_row_getMatchMask(tagRow, (byte)tag, headGrouped, rowEntries); for (; matches > 0 && nbAttempts > 0; matches &= matches - 1) { - uint matchPos = (headGrouped + ZSTD_VecMask_next(matches)) / groupWidth & rowMask; + uint matchPos = + (headGrouped + ZSTD_VecMask_next(matches)) / groupWidth & rowMask; uint matchIndex = row[matchPos]; if (matchPos == 0) continue; @@ -1295,7 +1559,14 @@ namespace ZstdSharp.Unsafe byte* match = dictBase + matchIndex; assert(match + 4 <= dictEnd); if (MEM_read32(match) == MEM_read32(ip)) - currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, dictEnd, prefixStart) + 4; + currentMl = + ZSTD_count_2segments( + ip + 4, + match + 4, + iLimit, + dictEnd, + prefixStart + ) + 4; } if (currentMl > ml) @@ -1312,7 +1583,18 @@ namespace ZstdSharp.Unsafe assert(nbAttempts <= 1U << (sizeof(nuint) == 4 ? 30 : 31) - 1); if (dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch) { - ml = ZSTD_dedicatedDictSearch_lazy_search(offsetPtr, ml, nbAttempts + ddsExtraAttempts, dms, ip, iLimit, prefixStart, curr, dictLimit, ddsIdx); + ml = ZSTD_dedicatedDictSearch_lazy_search( + offsetPtr, + ml, + nbAttempts + ddsExtraAttempts, + dms, + ip, + iLimit, + prefixStart, + curr, + dictLimit, + ddsIdx + ); } else if (dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState) { @@ -1327,10 +1609,16 @@ namespace ZstdSharp.Unsafe uint* matchBuffer = stackalloc uint[64]; nuint numMatches = 0; nuint currMatch = 0; - ulong matches = ZSTD_row_getMatchMask(dmsTagRow, (byte)dmsTag, headGrouped, rowEntries); + ulong matches = ZSTD_row_getMatchMask( + dmsTagRow, + (byte)dmsTag, + headGrouped, + rowEntries + ); for (; matches > 0 && nbAttempts > 0; matches &= matches - 1) { - uint matchPos = (headGrouped + ZSTD_VecMask_next(matches)) / groupWidth & rowMask; + uint matchPos = + (headGrouped + ZSTD_VecMask_next(matches)) / groupWidth & rowMask; uint matchIndex = dmsRow[matchPos]; if (matchPos == 0) continue; @@ -1357,7 +1645,14 @@ namespace ZstdSharp.Unsafe byte* match = dmsBase + matchIndex; assert(match + 4 <= dmsEnd); if (MEM_read32(match) == MEM_read32(ip)) - currentMl = ZSTD_count_2segments(ip + 4, match + 4, iLimit, dmsEnd, prefixStart) + 4; + currentMl = + ZSTD_count_2segments( + ip + 4, + match + 4, + iLimit, + dmsEnd, + prefixStart + ) + 4; } if (currentMl > ml) @@ -1377,402 +1672,1671 @@ namespace ZstdSharp.Unsafe } /* Generate row search fns for each combination of (dictMode, mls, rowLog) */ - private static nuint ZSTD_RowFindBestMatch_noDict_4_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_4_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_noDict, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_noDict, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_noDict_4_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_4_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_noDict, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_noDict, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_noDict_4_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_4_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_noDict, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_noDict, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_noDict_5_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_5_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_noDict, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_noDict, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_noDict_5_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_5_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_noDict, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_noDict, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_noDict_5_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_5_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_noDict, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_noDict, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_noDict_6_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_6_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_noDict, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_noDict, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_noDict_6_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_6_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_noDict, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_noDict, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_noDict_6_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_noDict_6_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_noDict, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_noDict, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_4_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_4_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_extDict, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_extDict, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_4_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_4_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_extDict, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_extDict, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_4_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_4_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_extDict, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_extDict, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_5_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_5_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_extDict, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_extDict, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_5_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_5_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_extDict, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_extDict, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_5_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_5_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_extDict, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_extDict, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_6_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_6_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_extDict, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_extDict, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_6_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_6_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_extDict, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_extDict, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_extDict_6_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_extDict_6_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_extDict, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_extDict, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_4_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_4_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_dictMatchState, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_4_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_4_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_dictMatchState, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_4_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_4_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_dictMatchState, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_5_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_5_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_dictMatchState, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_5_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_5_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_dictMatchState, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_5_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_5_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_dictMatchState, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_6_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_6_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_dictMatchState, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_6_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_6_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_dictMatchState, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_dictMatchState_6_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dictMatchState_6_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_dictMatchState, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_4_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_4_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_4_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_4_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_4_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_4_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_5_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_5_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_5_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_5_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_5_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_5_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 6 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_6_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_6_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 4); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 4 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 4 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_6_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_6_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 5); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 5 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 5 + ); } - private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_6_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_RowFindBestMatch_dedicatedDictSearch_6_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - assert((4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 : 6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) == 6); - return ZSTD_RowFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + assert( + ( + 4 > (6 < ms->cParams.searchLog ? 6 : ms->cParams.searchLog) ? 4 + : 6 < ms->cParams.searchLog ? 6 + : ms->cParams.searchLog + ) == 6 + ); + return ZSTD_RowFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch, + 6 + ); } /* Generate binary Tree search fns for each combination of (dictMode, mls) */ - private static nuint ZSTD_BtFindBestMatch_noDict_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_noDict_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 4, ZSTD_dictMode_e.ZSTD_noDict); } - private static nuint ZSTD_BtFindBestMatch_noDict_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_noDict_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 5, ZSTD_dictMode_e.ZSTD_noDict); } - private static nuint ZSTD_BtFindBestMatch_noDict_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_noDict_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 6, ZSTD_dictMode_e.ZSTD_noDict); } - private static nuint ZSTD_BtFindBestMatch_extDict_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_extDict_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 4, ZSTD_dictMode_e.ZSTD_extDict); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 4, + ZSTD_dictMode_e.ZSTD_extDict + ); } - private static nuint ZSTD_BtFindBestMatch_extDict_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_extDict_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 5, ZSTD_dictMode_e.ZSTD_extDict); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 5, + ZSTD_dictMode_e.ZSTD_extDict + ); } - private static nuint ZSTD_BtFindBestMatch_extDict_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_extDict_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 6, ZSTD_dictMode_e.ZSTD_extDict); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 6, + ZSTD_dictMode_e.ZSTD_extDict + ); } - private static nuint ZSTD_BtFindBestMatch_dictMatchState_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_dictMatchState_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 4, ZSTD_dictMode_e.ZSTD_dictMatchState); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 4, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_BtFindBestMatch_dictMatchState_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_dictMatchState_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 5, ZSTD_dictMode_e.ZSTD_dictMatchState); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 5, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_BtFindBestMatch_dictMatchState_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_dictMatchState_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 6, ZSTD_dictMode_e.ZSTD_dictMatchState); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 6, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_BtFindBestMatch_dedicatedDictSearch_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_dedicatedDictSearch_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 4, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 4, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_BtFindBestMatch_dedicatedDictSearch_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_dedicatedDictSearch_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 5, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 5, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_BtFindBestMatch_dedicatedDictSearch_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offBasePtr) + private static nuint ZSTD_BtFindBestMatch_dedicatedDictSearch_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offBasePtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - return ZSTD_BtFindBestMatch(ms, ip, iLimit, offBasePtr, 6, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + return ZSTD_BtFindBestMatch( + ms, + ip, + iLimit, + offBasePtr, + 6, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } /* Generate hash chain search fns for each combination of (dictMode, mls) */ - private static nuint ZSTD_HcFindBestMatch_noDict_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_noDict_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_noDict); } - private static nuint ZSTD_HcFindBestMatch_noDict_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_noDict_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_noDict); } - private static nuint ZSTD_HcFindBestMatch_noDict_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_noDict_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_noDict); } - private static nuint ZSTD_HcFindBestMatch_extDict_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_extDict_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_extDict); } - private static nuint ZSTD_HcFindBestMatch_extDict_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_extDict_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_extDict); } - private static nuint ZSTD_HcFindBestMatch_extDict_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_extDict_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_extDict); } - private static nuint ZSTD_HcFindBestMatch_dictMatchState_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_dictMatchState_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_dictMatchState); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + return ZSTD_HcFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_HcFindBestMatch_dictMatchState_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_dictMatchState_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_dictMatchState); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + return ZSTD_HcFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_HcFindBestMatch_dictMatchState_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_dictMatchState_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_dictMatchState); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + return ZSTD_HcFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_HcFindBestMatch_dedicatedDictSearch_4(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_dedicatedDictSearch_4( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 4); - return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 4, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 4 + ); + return ZSTD_HcFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 4, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_HcFindBestMatch_dedicatedDictSearch_5(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_dedicatedDictSearch_5( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 5); - return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 5, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 5 + ); + return ZSTD_HcFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 5, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_HcFindBestMatch_dedicatedDictSearch_6(ZSTD_MatchState_t* ms, byte* ip, byte* iLimit, nuint* offsetPtr) + private static nuint ZSTD_HcFindBestMatch_dedicatedDictSearch_6( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iLimit, + nuint* offsetPtr + ) { - assert((4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 : 6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) == 6); - return ZSTD_HcFindBestMatch(ms, ip, iLimit, offsetPtr, 6, ZSTD_dictMode_e.ZSTD_dedicatedDictSearch); + assert( + ( + 4 > (6 < ms->cParams.minMatch ? 6 : ms->cParams.minMatch) ? 4 + : 6 < ms->cParams.minMatch ? 6 + : ms->cParams.minMatch + ) == 6 + ); + return ZSTD_HcFindBestMatch( + ms, + ip, + iLimit, + offsetPtr, + 6, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } /** @@ -1800,7 +3364,16 @@ namespace ZstdSharp.Unsafe * If a match is found its offset is stored in @p offsetPtr. */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static nuint ZSTD_searchMax(ZSTD_MatchState_t* ms, byte* ip, byte* iend, nuint* offsetPtr, uint mls, uint rowLog, searchMethod_e searchMethod, ZSTD_dictMode_e dictMode) + private static nuint ZSTD_searchMax( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iend, + nuint* offsetPtr, + uint mls, + uint rowLog, + searchMethod_e searchMethod, + ZSTD_dictMode_e dictMode + ) { if (dictMode == ZSTD_dictMode_e.ZSTD_noDict) { @@ -1810,32 +3383,42 @@ namespace ZstdSharp.Unsafe { if (rowLog == 4) return ZSTD_RowFindBestMatch_noDict_4_4(ms, ip, iend, offsetPtr); - return rowLog == 5 ? ZSTD_RowFindBestMatch_noDict_4_5(ms, ip, iend, offsetPtr) : ZSTD_RowFindBestMatch_noDict_4_6(ms, ip, iend, offsetPtr); + return rowLog == 5 + ? ZSTD_RowFindBestMatch_noDict_4_5(ms, ip, iend, offsetPtr) + : ZSTD_RowFindBestMatch_noDict_4_6(ms, ip, iend, offsetPtr); } if (mls == 5) { if (rowLog == 4) return ZSTD_RowFindBestMatch_noDict_5_4(ms, ip, iend, offsetPtr); - return rowLog == 5 ? ZSTD_RowFindBestMatch_noDict_5_5(ms, ip, iend, offsetPtr) : ZSTD_RowFindBestMatch_noDict_5_6(ms, ip, iend, offsetPtr); + return rowLog == 5 + ? ZSTD_RowFindBestMatch_noDict_5_5(ms, ip, iend, offsetPtr) + : ZSTD_RowFindBestMatch_noDict_5_6(ms, ip, iend, offsetPtr); } if (rowLog == 4) return ZSTD_RowFindBestMatch_noDict_6_4(ms, ip, iend, offsetPtr); - return rowLog == 5 ? ZSTD_RowFindBestMatch_noDict_6_5(ms, ip, iend, offsetPtr) : ZSTD_RowFindBestMatch_noDict_6_6(ms, ip, iend, offsetPtr); + return rowLog == 5 + ? ZSTD_RowFindBestMatch_noDict_6_5(ms, ip, iend, offsetPtr) + : ZSTD_RowFindBestMatch_noDict_6_6(ms, ip, iend, offsetPtr); } if (searchMethod == searchMethod_e.search_hashChain) { if (mls == 4) return ZSTD_HcFindBestMatch_noDict_4(ms, ip, iend, offsetPtr); - return mls == 5 ? ZSTD_HcFindBestMatch_noDict_5(ms, ip, iend, offsetPtr) : ZSTD_HcFindBestMatch_noDict_6(ms, ip, iend, offsetPtr); + return mls == 5 + ? ZSTD_HcFindBestMatch_noDict_5(ms, ip, iend, offsetPtr) + : ZSTD_HcFindBestMatch_noDict_6(ms, ip, iend, offsetPtr); } // searchMethod_e.search_binaryTree if (mls == 4) return ZSTD_BtFindBestMatch_noDict_4(ms, ip, iend, offsetPtr); - return mls == 5 ? ZSTD_BtFindBestMatch_noDict_5(ms, ip, iend, offsetPtr) : ZSTD_BtFindBestMatch_noDict_6(ms, ip, iend, offsetPtr); + return mls == 5 + ? ZSTD_BtFindBestMatch_noDict_5(ms, ip, iend, offsetPtr) + : ZSTD_BtFindBestMatch_noDict_6(ms, ip, iend, offsetPtr); } if (dictMode == ZSTD_dictMode_e.ZSTD_extDict) @@ -1894,27 +3477,57 @@ namespace ZstdSharp.Unsafe if (mls == 4) { if (rowLog == 4) - return ZSTD_RowFindBestMatch_dictMatchState_4_4(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dictMatchState_4_4( + ms, + ip, + iend, + offsetPtr + ); if (rowLog == 5) - return ZSTD_RowFindBestMatch_dictMatchState_4_5(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dictMatchState_4_5( + ms, + ip, + iend, + offsetPtr + ); return ZSTD_RowFindBestMatch_dictMatchState_4_6(ms, ip, iend, offsetPtr); } if (mls == 5) { if (rowLog == 4) - return ZSTD_RowFindBestMatch_dictMatchState_5_4(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dictMatchState_5_4( + ms, + ip, + iend, + offsetPtr + ); if (rowLog == 5) - return ZSTD_RowFindBestMatch_dictMatchState_5_5(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dictMatchState_5_5( + ms, + ip, + iend, + offsetPtr + ); return ZSTD_RowFindBestMatch_dictMatchState_5_6(ms, ip, iend, offsetPtr); } if (mls == 6) { if (rowLog == 4) - return ZSTD_RowFindBestMatch_dictMatchState_6_4(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dictMatchState_6_4( + ms, + ip, + iend, + offsetPtr + ); if (rowLog == 5) - return ZSTD_RowFindBestMatch_dictMatchState_6_5(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dictMatchState_6_5( + ms, + ip, + iend, + offsetPtr + ); return ZSTD_RowFindBestMatch_dictMatchState_6_6(ms, ip, iend, offsetPtr); } } @@ -1941,27 +3554,57 @@ namespace ZstdSharp.Unsafe if (mls == 4) { if (rowLog == 4) - return ZSTD_RowFindBestMatch_dedicatedDictSearch_4_4(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dedicatedDictSearch_4_4( + ms, + ip, + iend, + offsetPtr + ); if (rowLog == 5) - return ZSTD_RowFindBestMatch_dedicatedDictSearch_4_5(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dedicatedDictSearch_4_5( + ms, + ip, + iend, + offsetPtr + ); return ZSTD_RowFindBestMatch_dedicatedDictSearch_4_6(ms, ip, iend, offsetPtr); } if (mls == 5) { if (rowLog == 4) - return ZSTD_RowFindBestMatch_dedicatedDictSearch_5_4(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dedicatedDictSearch_5_4( + ms, + ip, + iend, + offsetPtr + ); if (rowLog == 5) - return ZSTD_RowFindBestMatch_dedicatedDictSearch_5_5(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dedicatedDictSearch_5_5( + ms, + ip, + iend, + offsetPtr + ); return ZSTD_RowFindBestMatch_dedicatedDictSearch_5_6(ms, ip, iend, offsetPtr); } if (mls == 6) { if (rowLog == 4) - return ZSTD_RowFindBestMatch_dedicatedDictSearch_6_4(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dedicatedDictSearch_6_4( + ms, + ip, + iend, + offsetPtr + ); if (rowLog == 5) - return ZSTD_RowFindBestMatch_dedicatedDictSearch_6_5(ms, ip, iend, offsetPtr); + return ZSTD_RowFindBestMatch_dedicatedDictSearch_6_5( + ms, + ip, + iend, + offsetPtr + ); return ZSTD_RowFindBestMatch_dedicatedDictSearch_6_6(ms, ip, iend, offsetPtr); } } @@ -1987,7 +3630,16 @@ namespace ZstdSharp.Unsafe * Common parser - lazy strategy *********************************/ [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) + 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; @@ -1997,10 +3649,18 @@ namespace ZstdSharp.Unsafe byte* @base = ms->window.@base; uint prefixLowestIndex = ms->window.dictLimit; byte* prefixLowest = @base + prefixLowestIndex; - uint mls = ms->cParams.minMatch <= 4 ? 4 : ms->cParams.minMatch <= 6 ? ms->cParams.minMatch : 6; - uint rowLog = ms->cParams.searchLog <= 4 ? 4 : ms->cParams.searchLog <= 6 ? ms->cParams.searchLog : 6; - uint offset_1 = rep[0], offset_2 = rep[1]; - uint offsetSaved1 = 0, offsetSaved2 = 0; + uint mls = + ms->cParams.minMatch <= 4 ? 4 + : ms->cParams.minMatch <= 6 ? ms->cParams.minMatch + : 6; + uint rowLog = + ms->cParams.searchLog <= 4 ? 4 + : ms->cParams.searchLog <= 6 ? ms->cParams.searchLog + : 6; + uint offset_1 = rep[0], + offset_2 = rep[1]; + uint offsetSaved1 = 0, + offsetSaved2 = 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; @@ -2054,17 +3714,38 @@ namespace ZstdSharp.Unsafe 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 (ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 && MEM_read32(repMatch) == MEM_read32(ip + 1)) + byte* repMatch = + ( + dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState + || dictMode == ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ) + && repIndex < prefixLowestIndex + ? dictBase + (repIndex - dictIndexDelta) + : @base + repIndex; + if ( + ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 + && MEM_read32(repMatch) == MEM_read32(ip + 1) + ) { byte* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; - matchLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repMatchEnd, prefixLowest) + 4; + 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(ip + 1 - offset_1) == MEM_read32(ip + 1)) + if ( + dictMode == ZSTD_dictMode_e.ZSTD_noDict + && offset_1 > 0 + && MEM_read32(ip + 1 - offset_1) == MEM_read32(ip + 1) + ) { matchLength = ZSTD_count(ip + 1 + 4, ip + 1 + 4 - offset_1, iend) + 4; if (depth == 0) @@ -2073,7 +3754,16 @@ namespace ZstdSharp.Unsafe { nuint offbaseFound = 999999999; - nuint ml2 = ZSTD_searchMax(ms, ip, iend, &offbaseFound, mls, rowLog, searchMethod, dictMode); + nuint ml2 = ZSTD_searchMax( + ms, + ip, + iend, + &offbaseFound, + mls, + rowLog, + searchMethod, + dictMode + ); if (ml2 > matchLength) { matchLength = ml2; @@ -2095,7 +3785,12 @@ namespace ZstdSharp.Unsafe while (ip < ilimit) { ip++; - if (dictMode == ZSTD_dictMode_e.ZSTD_noDict && offBase != 0 && offset_1 > 0 && MEM_read32(ip) == MEM_read32(ip - offset_1)) + if ( + dictMode == ZSTD_dictMode_e.ZSTD_noDict + && offBase != 0 + && offset_1 > 0 + && MEM_read32(ip) == MEM_read32(ip - offset_1) + ) { nuint mlRep = ZSTD_count(ip + 4, ip + 4 - offset_1, iend) + 4; int gain2 = (int)(mlRep * 3); @@ -2113,13 +3808,28 @@ namespace ZstdSharp.Unsafe if (isDxS != 0) { uint repIndex = (uint)(ip - @base) - offset_1; - byte* repMatch = repIndex < prefixLowestIndex ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; - if (ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 && MEM_read32(repMatch) == MEM_read32(ip)) + byte* repMatch = + repIndex < prefixLowestIndex + ? dictBase + (repIndex - dictIndexDelta) + : @base + repIndex; + if ( + ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 + && MEM_read32(repMatch) == MEM_read32(ip) + ) { byte* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; - nuint mlRep = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repMatchEnd, prefixLowest) + 4; + 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)offBase) + 1); + int gain1 = (int)( + matchLength * 3 - ZSTD_highbit32((uint)offBase) + 1 + ); if (mlRep >= 4 && gain2 > gain1) { matchLength = mlRep; @@ -2133,7 +3843,16 @@ namespace ZstdSharp.Unsafe { nuint ofbCandidate = 999999999; - nuint ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, dictMode); + nuint ml2 = ZSTD_searchMax( + ms, + ip, + iend, + &ofbCandidate, + mls, + rowLog, + searchMethod, + dictMode + ); /* raw approx */ int gain2 = (int)(ml2 * 4 - ZSTD_highbit32((uint)ofbCandidate)); int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)offBase) + 4); @@ -2149,11 +3868,18 @@ namespace ZstdSharp.Unsafe if (depth == 2 && ip < ilimit) { ip++; - if (dictMode == ZSTD_dictMode_e.ZSTD_noDict && offBase != 0 && offset_1 > 0 && MEM_read32(ip) == MEM_read32(ip - offset_1)) + if ( + dictMode == ZSTD_dictMode_e.ZSTD_noDict + && offBase != 0 + && offset_1 > 0 + && MEM_read32(ip) == MEM_read32(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)offBase) + 1); + int gain1 = (int)( + matchLength * 4 - ZSTD_highbit32((uint)offBase) + 1 + ); if (mlRep >= 4 && gain2 > gain1) { matchLength = mlRep; @@ -2167,13 +3893,29 @@ namespace ZstdSharp.Unsafe if (isDxS != 0) { uint repIndex = (uint)(ip - @base) - offset_1; - byte* repMatch = repIndex < prefixLowestIndex ? dictBase + (repIndex - dictIndexDelta) : @base + repIndex; - if (ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 && MEM_read32(repMatch) == MEM_read32(ip)) + byte* repMatch = + repIndex < prefixLowestIndex + ? dictBase + (repIndex - dictIndexDelta) + : @base + repIndex; + if ( + ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 + && MEM_read32(repMatch) == MEM_read32(ip) + ) { - byte* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend; - nuint mlRep = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repMatchEnd, prefixLowest) + 4; + 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)offBase) + 1); + int gain1 = (int)( + matchLength * 4 - ZSTD_highbit32((uint)offBase) + 1 + ); if (mlRep >= 4 && gain2 > gain1) { matchLength = mlRep; @@ -2187,10 +3929,21 @@ namespace ZstdSharp.Unsafe { nuint ofbCandidate = 999999999; - nuint ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, dictMode); + nuint ml2 = ZSTD_searchMax( + ms, + ip, + iend, + &ofbCandidate, + mls, + rowLog, + searchMethod, + dictMode + ); /* raw approx */ int gain2 = (int)(ml2 * 4 - ZSTD_highbit32((uint)ofbCandidate)); - int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)offBase) + 7); + int gain1 = (int)( + matchLength * 4 - ZSTD_highbit32((uint)offBase) + 7 + ); if (ml2 >= 4 && gain2 > gain1) { matchLength = ml2; @@ -2210,7 +3963,11 @@ namespace ZstdSharp.Unsafe { assert(offBase > 3); assert(offBase > 3); - while (start > anchor && start - (offBase - 3) > prefixLowest && start[-1] == (start - (offBase - 3))[-1]) + while ( + start > anchor + && start - (offBase - 3) > prefixLowest + && start[-1] == (start - (offBase - 3))[-1] + ) { start--; matchLength++; @@ -2221,7 +3978,10 @@ namespace ZstdSharp.Unsafe { assert(offBase > 3); uint matchIndex = (uint)((nuint)(start - @base) - (offBase - 3)); - byte* match = matchIndex < prefixLowestIndex ? dictBase + matchIndex - dictIndexDelta : @base + matchIndex; + byte* match = + matchIndex < prefixLowestIndex + ? dictBase + matchIndex - dictIndexDelta + : @base + matchIndex; byte* mStart = matchIndex < prefixLowestIndex ? dictLowest : prefixLowest; while (start > anchor && match > mStart && start[-1] == match[-1]) { @@ -2236,7 +3996,7 @@ namespace ZstdSharp.Unsafe offset_1 = (uint)(offBase - 3); } - _storeSequence: + _storeSequence: { nuint litLength = (nuint)(start - anchor); ZSTD_storeSeq(seqStore, litLength, anchor, iend, (uint)offBase, matchLength); @@ -2259,11 +4019,24 @@ namespace ZstdSharp.Unsafe { uint current2 = (uint)(ip - @base); uint repIndex = current2 - offset_2; - byte* repMatch = repIndex < prefixLowestIndex ? dictBase - dictIndexDelta + repIndex : @base + repIndex; - if (ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 && MEM_read32(repMatch) == MEM_read32(ip)) + byte* repMatch = + repIndex < prefixLowestIndex + ? dictBase - dictIndexDelta + repIndex + : @base + repIndex; + if ( + ZSTD_index_overlap_check(prefixLowestIndex, repIndex) != 0 + && MEM_read32(repMatch) == MEM_read32(ip) + ) { byte* repEnd2 = repIndex < prefixLowestIndex ? dictEnd : iend; - matchLength = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repEnd2, prefixLowest) + 4; + matchLength = + ZSTD_count_2segments( + ip + 4, + repMatch + 4, + iend, + repEnd2, + prefixLowest + ) + 4; offBase = offset_2; offset_2 = offset_1; offset_1 = (uint)offBase; @@ -2281,7 +4054,9 @@ namespace ZstdSharp.Unsafe if (dictMode == ZSTD_dictMode_e.ZSTD_noDict) { - while (ip <= ilimit && offset_2 > 0 && MEM_read32(ip) == MEM_read32(ip - offset_2)) + while ( + ip <= ilimit && offset_2 > 0 && MEM_read32(ip) == MEM_read32(ip - offset_2) + ) { matchLength = ZSTD_count(ip + 4, ip + 4 - offset_2, iend) + 4; offBase = offset_2; @@ -2303,108 +4078,416 @@ namespace ZstdSharp.Unsafe return (nuint)(iend - anchor); } - private static nuint ZSTD_compressBlock_greedy(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 0, + ZSTD_dictMode_e.ZSTD_noDict + ); } - private static nuint ZSTD_compressBlock_greedy_dictMatchState(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 0, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_compressBlock_greedy_dedicatedDictSearch(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 0, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_compressBlock_greedy_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 0, + ZSTD_dictMode_e.ZSTD_noDict + ); } - private static nuint ZSTD_compressBlock_greedy_dictMatchState_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 0, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_compressBlock_greedy_dedicatedDictSearch_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 0, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_compressBlock_lazy(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 1, + ZSTD_dictMode_e.ZSTD_noDict + ); } - private static nuint ZSTD_compressBlock_lazy_dictMatchState(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 1, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_compressBlock_lazy_dedicatedDictSearch(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 1, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_compressBlock_lazy_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 1, + ZSTD_dictMode_e.ZSTD_noDict + ); } - private static nuint ZSTD_compressBlock_lazy_dictMatchState_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 1, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_compressBlock_lazy_dedicatedDictSearch_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 1, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_compressBlock_lazy2(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 2, + ZSTD_dictMode_e.ZSTD_noDict + ); } - private static nuint ZSTD_compressBlock_lazy2_dictMatchState(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 2, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_compressBlock_lazy2_dedicatedDictSearch(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 2, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_compressBlock_lazy2_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 2, + ZSTD_dictMode_e.ZSTD_noDict + ); } - private static nuint ZSTD_compressBlock_lazy2_dictMatchState_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 2, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_compressBlock_lazy2_dedicatedDictSearch_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 2, + ZSTD_dictMode_e.ZSTD_dedicatedDictSearch + ); } - private static nuint ZSTD_compressBlock_btlazy2(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_binaryTree, + 2, + ZSTD_dictMode_e.ZSTD_noDict + ); } - private static nuint ZSTD_compressBlock_btlazy2_dictMatchState(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_binaryTree, + 2, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } [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) + 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; @@ -2418,9 +4501,16 @@ namespace ZstdSharp.Unsafe byte* dictEnd = dictBase + dictLimit; byte* dictStart = dictBase + ms->window.lowLimit; uint windowLog = ms->cParams.windowLog; - uint mls = ms->cParams.minMatch <= 4 ? 4 : ms->cParams.minMatch <= 6 ? ms->cParams.minMatch : 6; - uint rowLog = ms->cParams.searchLog <= 4 ? 4 : ms->cParams.searchLog <= 6 ? ms->cParams.searchLog : 6; - uint offset_1 = rep[0], offset_2 = rep[1]; + uint mls = + ms->cParams.minMatch <= 4 ? 4 + : ms->cParams.minMatch <= 6 ? ms->cParams.minMatch + : 6; + uint rowLog = + ms->cParams.searchLog <= 4 ? 4 + : ms->cParams.searchLog <= 6 ? ms->cParams.searchLog + : 6; + uint offset_1 = rep[0], + offset_2 = rep[1]; ms->lazySkipping = 0; ip += ip == prefixStart ? 1 : 0; if (searchMethod == searchMethod_e.search_rowHash) @@ -2441,12 +4531,24 @@ namespace ZstdSharp.Unsafe uint repIndex = curr + 1 - offset_1; byte* repBase = repIndex < dictLimit ? dictBase : @base; byte* repMatch = repBase + repIndex; - if ((ZSTD_index_overlap_check(dictLimit, repIndex) & (offset_1 <= curr + 1 - windowLow ? 1 : 0)) != 0) + if ( + ( + ZSTD_index_overlap_check(dictLimit, repIndex) + & (offset_1 <= curr + 1 - windowLow ? 1 : 0) + ) != 0 + ) if (MEM_read32(ip + 1) == MEM_read32(repMatch)) { /* repcode detected we should take it */ byte* repEnd = repIndex < dictLimit ? dictEnd : iend; - matchLength = ZSTD_count_2segments(ip + 1 + 4, repMatch + 4, iend, repEnd, prefixStart) + 4; + matchLength = + ZSTD_count_2segments( + ip + 1 + 4, + repMatch + 4, + iend, + repEnd, + prefixStart + ) + 4; if (depth == 0) goto _storeSequence; } @@ -2454,7 +4556,16 @@ namespace ZstdSharp.Unsafe { nuint ofbCandidate = 999999999; - nuint ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_dictMode_e.ZSTD_extDict); + nuint ml2 = ZSTD_searchMax( + ms, + ip, + iend, + &ofbCandidate, + mls, + rowLog, + searchMethod, + ZSTD_dictMode_e.ZSTD_extDict + ); if (ml2 > matchLength) { matchLength = ml2; @@ -2482,14 +4593,28 @@ namespace ZstdSharp.Unsafe uint repIndex = curr - offset_1; byte* repBase = repIndex < dictLimit ? dictBase : @base; byte* repMatch = repBase + repIndex; - if ((ZSTD_index_overlap_check(dictLimit, repIndex) & (offset_1 <= curr - windowLow ? 1 : 0)) != 0) + if ( + ( + ZSTD_index_overlap_check(dictLimit, repIndex) + & (offset_1 <= curr - windowLow ? 1 : 0) + ) != 0 + ) if (MEM_read32(ip) == MEM_read32(repMatch)) { /* repcode detected */ byte* repEnd = repIndex < dictLimit ? dictEnd : iend; - nuint repLength = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repEnd, prefixStart) + 4; + 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)offBase) + 1); + int gain1 = (int)( + matchLength * 3 - ZSTD_highbit32((uint)offBase) + 1 + ); if (repLength >= 4 && gain2 > gain1) { matchLength = repLength; @@ -2503,7 +4628,16 @@ namespace ZstdSharp.Unsafe { nuint ofbCandidate = 999999999; - nuint ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_dictMode_e.ZSTD_extDict); + nuint ml2 = ZSTD_searchMax( + ms, + ip, + iend, + &ofbCandidate, + mls, + rowLog, + searchMethod, + ZSTD_dictMode_e.ZSTD_extDict + ); /* raw approx */ int gain2 = (int)(ml2 * 4 - ZSTD_highbit32((uint)ofbCandidate)); int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)offBase) + 4); @@ -2526,14 +4660,28 @@ namespace ZstdSharp.Unsafe uint repIndex = curr - offset_1; byte* repBase = repIndex < dictLimit ? dictBase : @base; byte* repMatch = repBase + repIndex; - if ((ZSTD_index_overlap_check(dictLimit, repIndex) & (offset_1 <= curr - windowLow ? 1 : 0)) != 0) + if ( + ( + ZSTD_index_overlap_check(dictLimit, repIndex) + & (offset_1 <= curr - windowLow ? 1 : 0) + ) != 0 + ) if (MEM_read32(ip) == MEM_read32(repMatch)) { /* repcode detected */ byte* repEnd = repIndex < dictLimit ? dictEnd : iend; - nuint repLength = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repEnd, prefixStart) + 4; + 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)offBase) + 1); + int gain1 = (int)( + matchLength * 4 - ZSTD_highbit32((uint)offBase) + 1 + ); if (repLength >= 4 && gain2 > gain1) { matchLength = repLength; @@ -2547,10 +4695,21 @@ namespace ZstdSharp.Unsafe { nuint ofbCandidate = 999999999; - nuint ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, ZSTD_dictMode_e.ZSTD_extDict); + nuint ml2 = ZSTD_searchMax( + ms, + ip, + iend, + &ofbCandidate, + mls, + rowLog, + searchMethod, + ZSTD_dictMode_e.ZSTD_extDict + ); /* raw approx */ int gain2 = (int)(ml2 * 4 - ZSTD_highbit32((uint)ofbCandidate)); - int gain1 = (int)(matchLength * 4 - ZSTD_highbit32((uint)offBase) + 7); + int gain1 = (int)( + matchLength * 4 - ZSTD_highbit32((uint)offBase) + 7 + ); if (ml2 >= 4 && gain2 > gain1) { matchLength = ml2; @@ -2568,7 +4727,8 @@ namespace ZstdSharp.Unsafe { assert(offBase > 3); uint matchIndex = (uint)((nuint)(start - @base) - (offBase - 3)); - byte* match = matchIndex < dictLimit ? dictBase + matchIndex : @base + matchIndex; + byte* match = + matchIndex < dictLimit ? dictBase + matchIndex : @base + matchIndex; byte* mStart = matchIndex < dictLimit ? dictStart : prefixStart; while (start > anchor && match > mStart && start[-1] == match[-1]) { @@ -2582,7 +4742,7 @@ namespace ZstdSharp.Unsafe offset_1 = (uint)(offBase - 3); } - _storeSequence: + _storeSequence: { nuint litLength = (nuint)(start - anchor); ZSTD_storeSeq(seqStore, litLength, anchor, iend, (uint)offBase, matchLength); @@ -2606,12 +4766,24 @@ namespace ZstdSharp.Unsafe uint repIndex = repCurrent - offset_2; byte* repBase = repIndex < dictLimit ? dictBase : @base; byte* repMatch = repBase + repIndex; - if ((ZSTD_index_overlap_check(dictLimit, repIndex) & (offset_2 <= repCurrent - windowLow ? 1 : 0)) != 0) + if ( + ( + ZSTD_index_overlap_check(dictLimit, repIndex) + & (offset_2 <= repCurrent - windowLow ? 1 : 0) + ) != 0 + ) if (MEM_read32(ip) == MEM_read32(repMatch)) { /* repcode detected we should take it */ byte* repEnd = repIndex < dictLimit ? dictEnd : iend; - matchLength = ZSTD_count_2segments(ip + 4, repMatch + 4, iend, repEnd, prefixStart) + 4; + matchLength = + ZSTD_count_2segments( + ip + 4, + repMatch + 4, + iend, + repEnd, + prefixStart + ) + 4; offBase = offset_2; offset_2 = offset_1; offset_1 = (uint)offBase; @@ -2632,39 +4804,137 @@ namespace ZstdSharp.Unsafe return (nuint)(iend - anchor); } - private static nuint ZSTD_compressBlock_greedy_extDict(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 0 + ); } - private static nuint ZSTD_compressBlock_greedy_extDict_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 0 + ); } - private static nuint ZSTD_compressBlock_lazy_extDict(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 1 + ); } - private static nuint ZSTD_compressBlock_lazy_extDict_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 1 + ); } - private static nuint ZSTD_compressBlock_lazy2_extDict(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_hashChain, + 2 + ); } - private static nuint ZSTD_compressBlock_lazy2_extDict_row(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_rowHash, + 2 + ); } - private static nuint ZSTD_compressBlock_btlazy2_extDict(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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); + return ZSTD_compressBlock_lazy_extDict_generic( + ms, + seqStore, + rep, + src, + srcSize, + searchMethod_e.search_binaryTree, + 2 + ); } } } diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLdm.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLdm.cs index 53a55943..407ec879 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLdm.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLdm.cs @@ -15,7 +15,8 @@ namespace ZstdSharp.Unsafe state->rolling = ~(uint)0; if (hashRateLog > 0 && hashRateLog <= maxBitsInMask) { - state->stopMask = ((ulong)1 << (int)hashRateLog) - 1 << (int)(maxBitsInMask - hashRateLog); + state->stopMask = + ((ulong)1 << (int)hashRateLog) - 1 << (int)(maxBitsInMask - hashRateLog); } else { @@ -28,7 +29,11 @@ namespace ZstdSharp.Unsafe * 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) + private static void ZSTD_ldm_gear_reset( + ldmRollingHashState_t* state, + byte* data, + nuint minMatchLength + ) { ulong hash = state->rolling; nuint n = 0; @@ -71,10 +76,17 @@ namespace ZstdSharp.Unsafe * * 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) + private static nuint ZSTD_ldm_gear_feed( + ldmRollingHashState_t* state, + byte* data, + nuint size, + nuint* splits, + uint* numSplits + ) { nuint n; - ulong hash, mask; + ulong hash, + mask; hash = state->rolling; mask = state->stopMask; n = 0; @@ -142,7 +154,7 @@ namespace ZstdSharp.Unsafe } } - done: + done: state->rolling = hash; return n; } @@ -156,14 +168,26 @@ namespace ZstdSharp.Unsafe * * Ensures that the minMatchLength >= targetLength during optimal parsing. */ - private static void ZSTD_ldm_adjustParameters(ldmParams_t* @params, ZSTD_compressionParameters* cParams) + private static void ZSTD_ldm_adjustParameters( + ldmParams_t* @params, + ZSTD_compressionParameters* cParams + ) { @params->windowLog = cParams->windowLog; if (@params->hashRateLog == 0) { if (@params->hashLog > 0) { - assert(@params->hashLog <= (uint)((sizeof(nuint) == 4 ? 30 : 31) < 30 ? sizeof(nuint) == 4 ? 30 : 31 : 30)); + assert( + @params->hashLog + <= (uint)( + (sizeof(nuint) == 4 ? 30 : 31) < 30 + ? sizeof(nuint) == 4 + ? 30 + : 31 + : 30 + ) + ); if (@params->windowLog > @params->hashLog) { @params->hashRateLog = @params->windowLog - @params->hashLog; @@ -178,7 +202,24 @@ namespace ZstdSharp.Unsafe if (@params->hashLog == 0) { - @params->hashLog = @params->windowLog - @params->hashRateLog <= 6 ? 6 : @params->windowLog - @params->hashRateLog <= (uint)((sizeof(nuint) == 4 ? 30 : 31) < 30 ? sizeof(nuint) == 4 ? 30 : 31 : 30) ? @params->windowLog - @params->hashRateLog : (uint)((sizeof(nuint) == 4 ? 30 : 31) < 30 ? sizeof(nuint) == 4 ? 30 : 31 : 30); + @params->hashLog = + @params->windowLog - @params->hashRateLog <= 6 ? 6 + : @params->windowLog - @params->hashRateLog + <= (uint)( + (sizeof(nuint) == 4 ? 30 : 31) < 30 + ? sizeof(nuint) == 4 + ? 30 + : 31 + : 30 + ) + ? @params->windowLog - @params->hashRateLog + : (uint)( + (sizeof(nuint) == 4 ? 30 : 31) < 30 + ? sizeof(nuint) == 4 + ? 30 + : 31 + : 30 + ); } if (@params->minMatchLength == 0) @@ -191,10 +232,16 @@ namespace ZstdSharp.Unsafe if (@params->bucketSizeLog == 0) { assert(1 <= (int)cParams->strategy && (int)cParams->strategy <= 9); - @params->bucketSizeLog = (uint)cParams->strategy <= 4 ? 4 : (uint)cParams->strategy <= 8 ? (uint)cParams->strategy : 8; + @params->bucketSizeLog = + (uint)cParams->strategy <= 4 ? 4 + : (uint)cParams->strategy <= 8 ? (uint)cParams->strategy + : 8; } - @params->bucketSizeLog = @params->bucketSizeLog < @params->hashLog ? @params->bucketSizeLog : @params->hashLog; + @params->bucketSizeLog = + @params->bucketSizeLog < @params->hashLog + ? @params->bucketSizeLog + : @params->hashLog; } /** ZSTD_ldm_getTableSize() : @@ -204,9 +251,12 @@ namespace ZstdSharp.Unsafe private 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 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)sizeof(ldmEntry_t)); + nuint totalSize = + ZSTD_cwksp_alloc_size(ldmBucketSize) + + ZSTD_cwksp_alloc_size(ldmHSize * (nuint)sizeof(ldmEntry_t)); return @params.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable ? totalSize : 0; } @@ -216,19 +266,30 @@ namespace ZstdSharp.Unsafe */ private static nuint ZSTD_ldm_getMaxNbSeq(ldmParams_t @params, nuint maxChunkSize) { - return @params.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable ? maxChunkSize / @params.minMatchLength : 0; + return @params.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable + ? 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, uint bucketSizeLog) + private static ldmEntry_t* ZSTD_ldm_getBucket( + ldmState_t* ldmState, + nuint hash, + uint bucketSizeLog + ) { return ldmState->hashTable + (hash << (int)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, uint bucketSizeLog) + private static void ZSTD_ldm_insertEntry( + ldmState_t* ldmState, + nuint hash, + ldmEntry_t entry, + uint bucketSizeLog + ) { byte* pOffset = ldmState->bucketOffsets + hash; uint offset = *pOffset; @@ -240,7 +301,12 @@ namespace ZstdSharp.Unsafe * 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) + 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]) @@ -258,7 +324,14 @@ namespace ZstdSharp.Unsafe * 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) + 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) @@ -266,7 +339,12 @@ namespace ZstdSharp.Unsafe return matchLength; } - matchLength += ZSTD_ldm_countBackwardsMatch(pIn - matchLength, pAnchor, pExtDictEnd, pExtDictStart); + matchLength += ZSTD_ldm_countBackwardsMatch( + pIn - matchLength, + pAnchor, + pExtDictEnd, + pExtDictStart + ); return matchLength; } @@ -283,10 +361,20 @@ namespace ZstdSharp.Unsafe switch (ms->cParams.strategy) { case ZSTD_strategy.ZSTD_fast: - ZSTD_fillHashTable(ms, iend, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, ZSTD_tableFillPurpose_e.ZSTD_tfp_forCCtx); + ZSTD_fillHashTable( + ms, + iend, + ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, + ZSTD_tableFillPurpose_e.ZSTD_tfp_forCCtx + ); break; case ZSTD_strategy.ZSTD_dfast: - ZSTD_fillDoubleHashTable(ms, iend, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, ZSTD_tableFillPurpose_e.ZSTD_tfp_forCCtx); + ZSTD_fillDoubleHashTable( + ms, + iend, + ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, + ZSTD_tableFillPurpose_e.ZSTD_tfp_forCCtx + ); break; case ZSTD_strategy.ZSTD_greedy: case ZSTD_strategy.ZSTD_lazy: @@ -304,7 +392,12 @@ namespace ZstdSharp.Unsafe return 0; } - private static void ZSTD_ldm_fillHashTable(ldmState_t* ldmState, byte* ip, byte* iend, ldmParams_t* @params) + private static void ZSTD_ldm_fillHashTable( + ldmState_t* ldmState, + byte* ip, + byte* iend, + ldmParams_t* @params + ) { uint minMatchLength = @params->minMatchLength; uint bucketSizeLog = @params->bucketSizeLog; @@ -349,11 +442,19 @@ namespace ZstdSharp.Unsafe uint curr = (uint)(anchor - ms->window.@base); if (curr > ms->nextToUpdate + 1024) { - ms->nextToUpdate = curr - (512 < curr - ms->nextToUpdate - 1024 ? 512 : curr - ms->nextToUpdate - 1024); + ms->nextToUpdate = + curr + - (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) + private static nuint ZSTD_ldm_generateSequences_internal( + ldmState_t* ldmState, + RawSeqStore_t* rawSeqStore, + ldmParams_t* @params, + void* src, + nuint srcSize + ) { /* LDM parameters */ int extDict = (int)ZSTD_window_hasExtDict(ldmState->window); @@ -391,7 +492,13 @@ namespace ZstdSharp.Unsafe nuint hashed; uint n; numSplits = 0; - hashed = ZSTD_ldm_gear_feed(&hashState, ip, (nuint)(ilimit - ip), splits, &numSplits); + hashed = ZSTD_ldm_gear_feed( + &hashState, + ip, + (nuint)(ilimit - ip), + splits, + &numSplits + ); for (n = 0; n < numSplits; n++) { byte* split = ip + splits[n] - minMatchLength; @@ -400,7 +507,11 @@ namespace ZstdSharp.Unsafe candidates[n].split = split; candidates[n].hash = hash; candidates[n].checksum = (uint)(xxhash >> 32); - candidates[n].bucket = ZSTD_ldm_getBucket(ldmState, hash, @params->bucketSizeLog); + candidates[n].bucket = ZSTD_ldm_getBucket( + ldmState, + hash, + @params->bucketSizeLog + ); #if NETCOREAPP3_0_OR_GREATER if (System.Runtime.Intrinsics.X86.Sse.IsSupported) { @@ -411,7 +522,10 @@ namespace ZstdSharp.Unsafe for (n = 0; n < numSplits; n++) { - nuint forwardMatchLength = 0, backwardMatchLength = 0, bestMatchLength = 0, mLength; + nuint forwardMatchLength = 0, + backwardMatchLength = 0, + bestMatchLength = 0, + mLength; uint offset; byte* split = candidates[n].split; uint checksum = candidates[n].checksum; @@ -430,7 +544,9 @@ namespace ZstdSharp.Unsafe for (cur = bucket; cur < bucket + entsPerBucket; cur++) { - nuint curForwardMatchLength, curBackwardMatchLength, curTotalMatchLength; + nuint curForwardMatchLength, + curBackwardMatchLength, + curTotalMatchLength; if (cur->checksum != checksum || cur->offset <= lowestIndex) { continue; @@ -442,13 +558,26 @@ namespace ZstdSharp.Unsafe 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); + curForwardMatchLength = ZSTD_count_2segments( + split, + pMatch, + iend, + matchEnd, + lowPrefixPtr + ); if (curForwardMatchLength < minMatchLength) { continue; } - curBackwardMatchLength = ZSTD_ldm_countBackwardsMatch_2segments(split, anchor, pMatch, lowMatchPtr, dictStart, dictEnd); + curBackwardMatchLength = ZSTD_ldm_countBackwardsMatch_2segments( + split, + anchor, + pMatch, + lowMatchPtr, + dictStart, + dictEnd + ); } else { @@ -459,7 +588,12 @@ namespace ZstdSharp.Unsafe continue; } - curBackwardMatchLength = ZSTD_ldm_countBackwardsMatch(split, anchor, pMatch, lowPrefixPtr); + curBackwardMatchLength = ZSTD_ldm_countBackwardsMatch( + split, + anchor, + pMatch, + lowPrefixPtr + ); } curTotalMatchLength = curForwardMatchLength + curBackwardMatchLength; @@ -483,7 +617,9 @@ namespace ZstdSharp.Unsafe { rawSeq* seq = rawSeqStore->seq + rawSeqStore->size; if (rawSeqStore->size == rawSeqStore->capacity) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall) + ); seq->litLength = (uint)(split - backwardMatchLength - anchor); seq->matchLength = (uint)mLength; seq->offset = offset; @@ -534,16 +670,26 @@ namespace ZstdSharp.Unsafe * NOTE: This function returns an error if it runs out of space to store * sequences. */ - private static nuint ZSTD_ldm_generateSequences(ldmState_t* ldmState, RawSeqStore_t* sequences, ldmParams_t* @params, void* src, nuint srcSize) + private 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; const nuint kMaxChunkSize = 1 << 20; - nuint nbChunks = srcSize / kMaxChunkSize + (nuint)(srcSize % kMaxChunkSize != 0 ? 1 : 0); + nuint nbChunks = + srcSize / kMaxChunkSize + (nuint)(srcSize % kMaxChunkSize != 0 ? 1 : 0); nuint chunk; nuint leftoverSize = 0; - assert(unchecked((uint)-1) - (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20)) >= kMaxChunkSize); + assert( + unchecked((uint)-1) - (MEM_64bits ? 3500U * (1 << 20) : 2000U * (1 << 20)) + >= kMaxChunkSize + ); assert(ldmState->window.nextSrc >= (byte*)src + srcSize); assert(sequences->pos <= sequences->size); assert(sequences->size <= sequences->capacity); @@ -556,16 +702,42 @@ namespace ZstdSharp.Unsafe nuint newLeftoverSize; nuint prevSize = sequences->size; assert(chunkStart < iend); - if (ZSTD_window_needOverflowCorrection(ldmState->window, 0, maxDist, ldmState->loadedDictEnd, chunkStart, chunkEnd) != 0) + if ( + ZSTD_window_needOverflowCorrection( + ldmState->window, + 0, + maxDist, + ldmState->loadedDictEnd, + chunkStart, + chunkEnd + ) != 0 + ) { uint ldmHSize = 1U << (int)@params->hashLog; - uint correction = ZSTD_window_correctOverflow(&ldmState->window, 0, maxDist, chunkStart); + uint correction = ZSTD_window_correctOverflow( + &ldmState->window, + 0, + maxDist, + chunkStart + ); ZSTD_ldm_reduceTable(ldmState->hashTable, ldmHSize, correction); ldmState->loadedDictEnd = 0; } - ZSTD_window_enforceMaxDist(&ldmState->window, chunkEnd, maxDist, &ldmState->loadedDictEnd, null); - newLeftoverSize = ZSTD_ldm_generateSequences_internal(ldmState, sequences, @params, chunkStart, chunkSize); + ZSTD_window_enforceMaxDist( + &ldmState->window, + chunkEnd, + maxDist, + &ldmState->loadedDictEnd, + null + ); + newLeftoverSize = ZSTD_ldm_generateSequences_internal( + ldmState, + sequences, + @params, + chunkStart, + chunkSize + ); if (ERR_isError(newLeftoverSize)) return newLeftoverSize; if (prevSize < sequences->size) @@ -590,7 +762,11 @@ namespace ZstdSharp.Unsafe * Avoids emitting matches less than `minMatch` bytes. * Must be called for data that is not passed to ZSTD_ldm_blockCompress(). */ - private static void ZSTD_ldm_skipSequences(RawSeqStore_t* rawSeqStore, nuint srcSize, uint minMatch) + private static void ZSTD_ldm_skipSequences( + RawSeqStore_t* rawSeqStore, + nuint srcSize, + uint minMatch + ) { while (srcSize > 0 && rawSeqStore->pos < rawSeqStore->size) { @@ -632,7 +808,11 @@ namespace ZstdSharp.Unsafe * 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) + private static rawSeq maybeSplitSequence( + RawSeqStore_t* rawSeqStore, + uint remaining, + uint minMatch + ) { rawSeq sequence = rawSeqStore->seq[rawSeqStore->pos]; assert(sequence.offset > 0); @@ -706,11 +886,23 @@ namespace ZstdSharp.Unsafe * two. We handle that case correctly, and update `rawSeqStore` appropriately. * NOTE: This function does not return any errors. */ - private static nuint ZSTD_ldm_blockCompress(RawSeqStore_t* rawSeqStore, ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, ZSTD_paramSwitch_e useRowMatchFinder, void* src, nuint srcSize) + private static nuint ZSTD_ldm_blockCompress( + RawSeqStore_t* rawSeqStore, + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + ZSTD_paramSwitch_e useRowMatchFinder, + void* src, + nuint srcSize + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint minMatch = cParams->minMatch; - ZSTD_BlockCompressor_f blockCompressor = ZSTD_selectBlockCompressor(cParams->strategy, useRowMatchFinder, ZSTD_matchState_dictMode(ms)); + ZSTD_BlockCompressor_f blockCompressor = ZSTD_selectBlockCompressor( + cParams->strategy, + useRowMatchFinder, + ZSTD_matchState_dictMode(ms) + ); /* Input bounds */ byte* istart = (byte*)src; byte* iend = istart + srcSize; @@ -744,7 +936,14 @@ namespace ZstdSharp.Unsafe rep[i] = rep[i - 1]; rep[0] = sequence.offset; assert(sequence.offset > 0); - ZSTD_storeSeq(seqStore, newLitLength, ip - newLitLength, iend, sequence.offset + 3, sequence.matchLength); + ZSTD_storeSeq( + seqStore, + newLitLength, + ip - newLitLength, + iend, + sequence.offset + 3, + sequence.matchLength + ); ip += sequence.matchLength; } } @@ -754,4 +953,4 @@ namespace ZstdSharp.Unsafe return blockCompressor(ms, seqStore, rep, ip, (nuint)(iend - ip)); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLdmGeartab.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLdmGeartab.cs index 7027cb6f..5cf4c7a4 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLdmGeartab.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdLdmGeartab.cs @@ -1,275 +1,540 @@ -using static ZstdSharp.UnsafeHelper; using System; using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_ZSTD_ldm_gearTab => 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 - }; - private static ulong* ZSTD_ldm_gearTab => (ulong*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_ZSTD_ldm_gearTab)); + private static ReadOnlySpan Span_ZSTD_ldm_gearTab => + 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, + }; + private static ulong* ZSTD_ldm_gearTab => + (ulong*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_ZSTD_ldm_gearTab) + ); #else - private static readonly 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 }); + private static readonly 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, + } + ); #endif } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdOpt.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdOpt.cs index c001858d..d5ebe36a 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdOpt.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdOpt.cs @@ -1,7 +1,7 @@ -using System.Runtime.CompilerServices; -using static ZstdSharp.UnsafeHelper; using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -41,10 +41,22 @@ namespace ZstdSharp.Unsafe 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); + 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); } private static uint sum_u32(uint* table, nuint nbElts) @@ -59,13 +71,23 @@ namespace ZstdSharp.Unsafe return total; } - private static uint ZSTD_downscaleStats(uint* table, uint lastEltIndex, uint shift, base_directive_e base1) + private static uint ZSTD_downscaleStats( + uint* table, + uint lastEltIndex, + uint shift, + base_directive_e base1 + ) { - uint s, sum = 0; + uint s, + sum = 0; assert(shift < 30); for (s = 0; s < lastEltIndex + 1; s++) { - uint @base = (uint)(base1 != default ? 1 : table[s] > 0 ? 1 : 0); + uint @base = (uint)( + base1 != default ? 1 + : table[s] > 0 ? 1 + : 0 + ); uint newStat = @base + (table[s] >> (int)shift); sum += newStat; table[s] = newStat; @@ -84,94 +106,185 @@ namespace ZstdSharp.Unsafe assert(logTarget < 30); if (factor <= 1) return prevsum; - return ZSTD_downscaleStats(table, lastEltIndex, ZSTD_highbit32(factor), base_directive_e.base_1guaranteed); + return ZSTD_downscaleStats( + table, + lastEltIndex, + ZSTD_highbit32(factor), + base_directive_e.base_1guaranteed + ); } #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_baseLLfreqs => new uint[36] - { - 4, - 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 - }; - private static uint* baseLLfreqs => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_baseLLfreqs)); + private static ReadOnlySpan Span_baseLLfreqs => + new uint[36] + { + 4, + 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, + }; + private static uint* baseLLfreqs => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_baseLLfreqs) + ); #else - private static readonly uint* baseLLfreqs = GetArrayPointer(new uint[36] { 4, 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 }); + private static readonly uint* baseLLfreqs = GetArrayPointer( + new uint[36] + { + 4, + 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, + } + ); #endif #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_baseOFCfreqs => new uint[32] - { - 6, - 2, - 1, - 1, - 2, - 3, - 4, - 4, - 4, - 3, - 2, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1 - }; - private static uint* baseOFCfreqs => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_baseOFCfreqs)); + private static ReadOnlySpan Span_baseOFCfreqs => + new uint[32] + { + 6, + 2, + 1, + 1, + 2, + 3, + 4, + 4, + 4, + 3, + 2, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + }; + private static uint* baseOFCfreqs => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_baseOFCfreqs) + ); #else - private static readonly uint* baseOFCfreqs = GetArrayPointer(new uint[32] { 6, 2, 1, 1, 2, 3, 4, 4, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); + private static readonly uint* baseOFCfreqs = GetArrayPointer( + new uint[32] + { + 6, + 2, + 1, + 1, + 2, + 3, + 4, + 4, + 4, + 3, + 2, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + } + ); #endif /* ZSTD_rescaleFreqs() : * if first block (detected by optPtr->litLengthSum == 0) : init statistics @@ -180,7 +293,12 @@ namespace ZstdSharp.Unsafe * using src for literals stats, and baseline stats for sequence 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) + 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; @@ -205,9 +323,14 @@ namespace ZstdSharp.Unsafe { /* scale to 2K */ const uint scaleLog = 11; - uint bitCost = HUF_getNbBitsFromCTable(&optPtr->symbolCosts->huf.CTable.e0, lit); + uint bitCost = HUF_getNbBitsFromCTable( + &optPtr->symbolCosts->huf.CTable.e0, + lit + ); assert(bitCost <= scaleLog); - optPtr->litFreq[lit] = (uint)(bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1); + optPtr->litFreq[lit] = (uint)( + bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1 + ); optPtr->litSum += optPtr->litFreq[lit]; } } @@ -223,7 +346,9 @@ namespace ZstdSharp.Unsafe const 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->litLengthFreq[ll] = (uint)( + bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1 + ); optPtr->litLengthSum += optPtr->litLengthFreq[ll]; } } @@ -238,7 +363,9 @@ namespace ZstdSharp.Unsafe const 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->matchLengthFreq[ml] = (uint)( + bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1 + ); optPtr->matchLengthSum += optPtr->matchLengthFreq[ml]; } } @@ -253,7 +380,9 @@ namespace ZstdSharp.Unsafe const 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->offCodeFreq[of] = (uint)( + bitCost != 0 ? 1 << (int)(scaleLog - bitCost) : 1 + ); optPtr->offCodeSum += optPtr->offCodeFreq[of]; } } @@ -266,7 +395,12 @@ namespace ZstdSharp.Unsafe /* base initial cost of literals on direct frequency within src */ uint lit = (1 << 8) - 1; HIST_count_simple(optPtr->litFreq, &lit, src, srcSize); - optPtr->litSum = ZSTD_downscaleStats(optPtr->litFreq, (1 << 8) - 1, 8, base_directive_e.base_0possible); + optPtr->litSum = ZSTD_downscaleStats( + optPtr->litFreq, + (1 << 8) - 1, + 8, + base_directive_e.base_0possible + ); } { @@ -302,7 +436,12 @@ namespace ZstdSharp.Unsafe /* 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) + private static uint ZSTD_rawLiteralsCost( + byte* literals, + uint litLength, + optState_t* optPtr, + int optLevel + ) { if (litLength == 0) return 0; @@ -317,7 +456,10 @@ namespace ZstdSharp.Unsafe assert(optPtr->litSumBasePrice >= 1 << 8); for (u = 0; u < litLength; u++) { - uint litPrice = optLevel != 0 ? ZSTD_fracWeight(optPtr->litFreq[literals[u]]) : ZSTD_bitWeight(optPtr->litFreq[literals[u]]); + uint litPrice = + optLevel != 0 + ? ZSTD_fracWeight(optPtr->litFreq[literals[u]]) + : ZSTD_bitWeight(optPtr->litFreq[literals[u]]); if (litPrice > litPriceMax) litPrice = litPriceMax; price -= litPrice; @@ -338,7 +480,13 @@ namespace ZstdSharp.Unsafe return (1 << 8) + ZSTD_litLengthPrice((1 << 17) - 1, optPtr, optLevel); { uint llCode = ZSTD_LLcode(litLength); - return (uint)(LL_bits[llCode] * (1 << 8)) + optPtr->litLengthSumBasePrice - (optLevel != 0 ? ZSTD_fracWeight(optPtr->litLengthFreq[llCode]) : ZSTD_bitWeight(optPtr->litLengthFreq[llCode])); + return (uint)(LL_bits[llCode] * (1 << 8)) + + optPtr->litLengthSumBasePrice + - ( + optLevel != 0 + ? ZSTD_fracWeight(optPtr->litLengthFreq[llCode]) + : ZSTD_bitWeight(optPtr->litLengthFreq[llCode]) + ); } } @@ -349,20 +497,44 @@ namespace ZstdSharp.Unsafe * @optLevel: when <2, favors small offset for decompression speed (improved cache efficiency) */ [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint ZSTD_getMatchPrice(uint offBase, uint matchLength, optState_t* optPtr, int optLevel) + private static uint ZSTD_getMatchPrice( + uint offBase, + uint matchLength, + optState_t* optPtr, + int optLevel + ) { uint price; uint offCode = ZSTD_highbit32(offBase); 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) * (1 << 8); - price = offCode * (1 << 8) + (optPtr->offCodeSumBasePrice - (optLevel != 0 ? ZSTD_fracWeight(optPtr->offCodeFreq[offCode]) : ZSTD_bitWeight(optPtr->offCodeFreq[offCode]))); + return (optLevel != 0 ? ZSTD_fracWeight(mlBase) : ZSTD_bitWeight(mlBase)) + + (16 + offCode) * (1 << 8); + price = + offCode * (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 * (1 << 8); { uint mlCode = ZSTD_MLcode(mlBase); - price += (uint)(ML_bits[mlCode] * (1 << 8)) + (optPtr->matchLengthSumBasePrice - (optLevel != 0 ? ZSTD_fracWeight(optPtr->matchLengthFreq[mlCode]) : ZSTD_bitWeight(optPtr->matchLengthFreq[mlCode]))); + price += + (uint)(ML_bits[mlCode] * (1 << 8)) + + ( + optPtr->matchLengthSumBasePrice + - ( + optLevel != 0 + ? ZSTD_fracWeight(optPtr->matchLengthFreq[mlCode]) + : ZSTD_bitWeight(optPtr->matchLengthFreq[mlCode]) + ) + ); } price += (1 << 8) / 5; @@ -371,7 +543,13 @@ namespace ZstdSharp.Unsafe /* ZSTD_updateStats() : * assumption : literals + litLength <= iend */ - private static void ZSTD_updateStats(optState_t* optPtr, uint litLength, byte* literals, uint offBase, uint matchLength) + private static void ZSTD_updateStats( + optState_t* optPtr, + uint litLength, + byte* literals, + uint offBase, + uint matchLength + ) { if (ZSTD_compressedLiterals(optPtr) != 0) { @@ -423,7 +601,11 @@ namespace ZstdSharp.Unsafe /* 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) + private static uint ZSTD_insertAndFindFirstIndexHash3( + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip + ) { uint* hashTable3 = ms->hashTable3; uint hashLog3 = ms->hashLog3; @@ -449,7 +631,14 @@ namespace ZstdSharp.Unsafe * @param ip assumed <= iend-8 . * @param target The target of ZSTD_updateTree_internal() - we are filling to this position * @return : nb of positions added */ - private static uint ZSTD_insertBt1(ZSTD_MatchState_t* ms, byte* ip, byte* iend, uint target, uint mls, int extDict) + private static uint ZSTD_insertBt1( + ZSTD_MatchState_t* ms, + byte* ip, + byte* iend, + uint target, + uint mls, + int extDict + ) { ZSTD_compressionParameters* cParams = &ms->cParams; uint* hashTable = ms->hashTable; @@ -459,7 +648,8 @@ namespace ZstdSharp.Unsafe uint btLog = cParams->chainLog - 1; uint btMask = (uint)((1 << (int)btLog) - 1); uint matchIndex = hashTable[h]; - nuint commonLengthSmaller = 0, commonLengthLarger = 0; + nuint commonLengthSmaller = 0, + commonLengthLarger = 0; byte* @base = ms->window.@base; byte* dictBase = ms->window.dictBase; uint dictLimit = ms->window.dictLimit; @@ -487,7 +677,10 @@ namespace ZstdSharp.Unsafe { uint* nextPtr = bt + 2 * (matchIndex & btMask); /* guaranteed minimum nb of common bytes */ - nuint matchLength = commonLengthSmaller < commonLengthLarger ? commonLengthSmaller : commonLengthLarger; + nuint matchLength = + commonLengthSmaller < commonLengthLarger + ? commonLengthSmaller + : commonLengthLarger; assert(matchIndex < curr); if (extDict == 0 || matchIndex + matchLength >= dictLimit) { @@ -498,7 +691,13 @@ namespace ZstdSharp.Unsafe else { match = dictBase + matchIndex; - matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iend, dictEnd, prefixStart); + matchLength += ZSTD_count_2segments( + ip + matchLength, + match + matchLength, + iend, + dictEnd, + prefixStart + ); if (matchIndex + matchLength >= dictLimit) match = @base + matchIndex; } @@ -554,14 +753,27 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void ZSTD_updateTree_internal(ZSTD_MatchState_t* ms, byte* ip, byte* iend, uint mls, ZSTD_dictMode_e dictMode) + 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, target, mls, dictMode == ZSTD_dictMode_e.ZSTD_extDict ? 1 : 0); + uint forward = ZSTD_insertBt1( + ms, + @base + idx, + iend, + target, + mls, + dictMode == ZSTD_dictMode_e.ZSTD_extDict ? 1 : 0 + ); assert(idx < idx + forward); idx += forward; } @@ -574,14 +786,32 @@ namespace ZstdSharp.Unsafe /* used in ZSTD_loadDictionaryContent() */ private 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); + 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) + 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 < (1 << 12) - 1 ? cParams->targetLength : (1 << 12) - 1; + uint sufficient_len = + cParams->targetLength < (1 << 12) - 1 ? cParams->targetLength : (1 << 12) - 1; byte* @base = ms->window.@base; uint curr = (uint)(ip - @base); uint hashLog = cParams->hashLog; @@ -592,7 +822,8 @@ namespace ZstdSharp.Unsafe uint* bt = ms->chainTable; uint btLog = cParams->chainLog - 1; uint btMask = (1U << (int)btLog) - 1; - nuint commonLengthSmaller = 0, commonLengthLarger = 0; + nuint commonLengthSmaller = 0, + commonLengthLarger = 0; byte* dictBase = ms->window.dictBase; uint dictLimit = ms->window.dictLimit; byte* dictEnd = dictBase + dictLimit; @@ -608,17 +839,31 @@ namespace ZstdSharp.Unsafe 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; + 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); { @@ -632,23 +877,62 @@ namespace ZstdSharp.Unsafe assert(curr >= dictLimit); if (repOffset - 1 < curr - dictLimit) { - if (repIndex >= windowLow && ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(ip - repOffset, minMatch)) + if ( + repIndex >= windowLow + && ZSTD_readMINMATCH(ip, minMatch) + == ZSTD_readMINMATCH(ip - repOffset, minMatch) + ) { - repLen = (uint)ZSTD_count(ip + minMatch, ip + minMatch - repOffset, iLimit) + 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; + 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 ? 1 : 0) & ZSTD_index_overlap_check(dictLimit, repIndex)) != 0 && ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch)) + if ( + dictMode == ZSTD_dictMode_e.ZSTD_extDict + && ( + (repOffset - 1 < curr - windowLow ? 1 : 0) + & ZSTD_index_overlap_check(dictLimit, repIndex) + ) != 0 + && ZSTD_readMINMATCH(ip, minMatch) + == ZSTD_readMINMATCH(repMatch, minMatch) + ) { - repLen = (uint)ZSTD_count_2segments(ip + minMatch, repMatch + minMatch, iLimit, dictEnd, prefixStart) + 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) ? 1 : 0) & ZSTD_index_overlap_check(dictLimit, repIndex)) != 0 && ZSTD_readMINMATCH(ip, minMatch) == ZSTD_readMINMATCH(repMatch, minMatch)) + if ( + dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState + && ( + (repOffset - 1 < curr - (dmsLowLimit + dmsIndexDelta) ? 1 : 0) + & ZSTD_index_overlap_check(dictLimit, repIndex) + ) != 0 + && ZSTD_readMINMATCH(ip, minMatch) + == ZSTD_readMINMATCH(repMatch, minMatch) + ) { - repLen = (uint)ZSTD_count_2segments(ip + minMatch, repMatch + minMatch, iLimit, dmsEnd, prefixStart) + minMatch; + repLen = + (uint)ZSTD_count_2segments( + ip + minMatch, + repMatch + minMatch, + iLimit, + dmsEnd, + prefixStart + ) + minMatch; } } @@ -674,7 +958,11 @@ namespace ZstdSharp.Unsafe if (matchIndex3 >= matchLow && curr - matchIndex3 < 1 << 18) { nuint mlen; - if (dictMode == ZSTD_dictMode_e.ZSTD_noDict || dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState || matchIndex3 >= dictLimit) + 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); @@ -709,9 +997,16 @@ namespace ZstdSharp.Unsafe uint* nextPtr = bt + 2 * (matchIndex & btMask); byte* match; /* guaranteed minimum nb of common bytes */ - nuint matchLength = commonLengthSmaller < commonLengthLarger ? commonLengthSmaller : commonLengthLarger; + 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) + if ( + dictMode == ZSTD_dictMode_e.ZSTD_noDict + || dictMode == ZSTD_dictMode_e.ZSTD_dictMatchState + || matchIndex + matchLength >= dictLimit + ) { assert(matchIndex + matchLength >= dictLimit); match = @base + matchIndex; @@ -725,7 +1020,13 @@ namespace ZstdSharp.Unsafe { match = dictBase + matchIndex; assert(memcmp(match, ip, matchLength) == 0); - matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iLimit, dictEnd, prefixStart); + matchLength += ZSTD_count_2segments( + ip + matchLength, + match + matchLength, + iLimit, + dictEnd, + prefixStart + ); if (matchIndex + matchLength >= dictLimit) match = @base + matchIndex; } @@ -788,9 +1089,18 @@ namespace ZstdSharp.Unsafe { uint* nextPtr = dmsBt + 2 * (dictMatchIndex & dmsBtMask); /* guaranteed minimum nb of common bytes */ - nuint matchLength = commonLengthSmaller < commonLengthLarger ? commonLengthSmaller : commonLengthLarger; + nuint matchLength = + commonLengthSmaller < commonLengthLarger + ? commonLengthSmaller + : commonLengthLarger; byte* match = dmsBase + dictMatchIndex; - matchLength += ZSTD_count_2segments(ip + matchLength, match + matchLength, iLimit, dmsEnd, prefixStart); + matchLength += ZSTD_count_2segments( + ip + matchLength, + match + matchLength, + iLimit, + dmsEnd, + prefixStart + ); if (dictMatchIndex + matchLength >= dmsHighLimit) match = @base + dictMatchIndex + dmsIndexDelta; if (matchLength > bestLength) @@ -833,102 +1143,378 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static uint ZSTD_btGetAllMatches_internal(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat, ZSTD_dictMode_e dictMode, uint mls) + private static uint ZSTD_btGetAllMatches_internal( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat, + ZSTD_dictMode_e dictMode, + uint mls + ) { - assert((ms->cParams.minMatch <= 3 ? 3 : ms->cParams.minMatch <= 6 ? ms->cParams.minMatch : 6) == mls); + assert( + ( + ms->cParams.minMatch <= 3 ? 3 + : ms->cParams.minMatch <= 6 ? ms->cParams.minMatch + : 6 + ) == mls + ); if (ip < ms->window.@base + ms->nextToUpdate) return 0; ZSTD_updateTree_internal(ms, ip, iHighLimit, mls, dictMode); - return ZSTD_insertBtAndGetAllMatches(matches, ms, nextToUpdate3, ip, iHighLimit, dictMode, rep, ll0, lengthToBeat, mls); + return ZSTD_insertBtAndGetAllMatches( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + dictMode, + rep, + ll0, + lengthToBeat, + mls + ); } - private static uint ZSTD_btGetAllMatches_noDict_3(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_noDict_3( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_noDict, 3); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_noDict, + 3 + ); } - private static uint ZSTD_btGetAllMatches_noDict_4(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_noDict_4( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_noDict, 4); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_noDict, + 4 + ); } - private static uint ZSTD_btGetAllMatches_noDict_5(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_noDict_5( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_noDict, 5); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_noDict, + 5 + ); } - private static uint ZSTD_btGetAllMatches_noDict_6(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_noDict_6( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_noDict, 6); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_noDict, + 6 + ); } - private static uint ZSTD_btGetAllMatches_extDict_3(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_extDict_3( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_extDict, 3); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_extDict, + 3 + ); } - private static uint ZSTD_btGetAllMatches_extDict_4(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_extDict_4( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_extDict, 4); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_extDict, + 4 + ); } - private static uint ZSTD_btGetAllMatches_extDict_5(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_extDict_5( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_extDict, 5); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_extDict, + 5 + ); } - private static uint ZSTD_btGetAllMatches_extDict_6(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_extDict_6( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_extDict, 6); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_extDict, + 6 + ); } - private static uint ZSTD_btGetAllMatches_dictMatchState_3(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_dictMatchState_3( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_dictMatchState, 3); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 3 + ); } - private static uint ZSTD_btGetAllMatches_dictMatchState_4(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_dictMatchState_4( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_dictMatchState, 4); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 4 + ); } - private static uint ZSTD_btGetAllMatches_dictMatchState_5(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_dictMatchState_5( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_dictMatchState, 5); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 5 + ); } - private static uint ZSTD_btGetAllMatches_dictMatchState_6(ZSTD_match_t* matches, ZSTD_MatchState_t* ms, uint* nextToUpdate3, byte* ip, byte* iHighLimit, uint* rep, uint ll0, uint lengthToBeat) + private static uint ZSTD_btGetAllMatches_dictMatchState_6( + ZSTD_match_t* matches, + ZSTD_MatchState_t* ms, + uint* nextToUpdate3, + byte* ip, + byte* iHighLimit, + uint* rep, + uint ll0, + uint lengthToBeat + ) { - return ZSTD_btGetAllMatches_internal(matches, ms, nextToUpdate3, ip, iHighLimit, rep, ll0, lengthToBeat, ZSTD_dictMode_e.ZSTD_dictMatchState, 6); + return ZSTD_btGetAllMatches_internal( + matches, + ms, + nextToUpdate3, + ip, + iHighLimit, + rep, + ll0, + lengthToBeat, + ZSTD_dictMode_e.ZSTD_dictMatchState, + 6 + ); } - private static readonly ZSTD_getAllMatchesFn[][] getAllMatchesFns = new ZSTD_getAllMatchesFn[3][] - { - new ZSTD_getAllMatchesFn[4] + private static readonly ZSTD_getAllMatchesFn[][] getAllMatchesFns = + new ZSTD_getAllMatchesFn[3][] { - ZSTD_btGetAllMatches_noDict_3, - ZSTD_btGetAllMatches_noDict_4, - ZSTD_btGetAllMatches_noDict_5, - ZSTD_btGetAllMatches_noDict_6 - }, - new ZSTD_getAllMatchesFn[4] - { - ZSTD_btGetAllMatches_extDict_3, - ZSTD_btGetAllMatches_extDict_4, - ZSTD_btGetAllMatches_extDict_5, - ZSTD_btGetAllMatches_extDict_6 - }, - new ZSTD_getAllMatchesFn[4] - { - ZSTD_btGetAllMatches_dictMatchState_3, - ZSTD_btGetAllMatches_dictMatchState_4, - ZSTD_btGetAllMatches_dictMatchState_5, - ZSTD_btGetAllMatches_dictMatchState_6 - } - }; - private static ZSTD_getAllMatchesFn ZSTD_selectBtGetAllMatches(ZSTD_MatchState_t* ms, ZSTD_dictMode_e dictMode) + new ZSTD_getAllMatchesFn[4] + { + ZSTD_btGetAllMatches_noDict_3, + ZSTD_btGetAllMatches_noDict_4, + ZSTD_btGetAllMatches_noDict_5, + ZSTD_btGetAllMatches_noDict_6, + }, + new ZSTD_getAllMatchesFn[4] + { + ZSTD_btGetAllMatches_extDict_3, + ZSTD_btGetAllMatches_extDict_4, + ZSTD_btGetAllMatches_extDict_5, + ZSTD_btGetAllMatches_extDict_6, + }, + new ZSTD_getAllMatchesFn[4] + { + ZSTD_btGetAllMatches_dictMatchState_3, + ZSTD_btGetAllMatches_dictMatchState_4, + ZSTD_btGetAllMatches_dictMatchState_5, + ZSTD_btGetAllMatches_dictMatchState_6, + }, + }; + + private static ZSTD_getAllMatchesFn ZSTD_selectBtGetAllMatches( + ZSTD_MatchState_t* ms, + ZSTD_dictMode_e dictMode + ) { - uint mls = ms->cParams.minMatch <= 3 ? 3 : ms->cParams.minMatch <= 6 ? ms->cParams.minMatch : 6; + uint mls = + ms->cParams.minMatch <= 3 ? 3 + : ms->cParams.minMatch <= 6 ? ms->cParams.minMatch + : 6; assert((uint)dictMode < 3); assert(mls - 3 < 4); return getAllMatchesFns[(int)dictMode][mls - 3]; @@ -938,7 +1524,10 @@ namespace ZstdSharp.Unsafe * 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) + private static void ZSTD_optLdm_skipRawSeqStoreBytes( + RawSeqStore_t* rawSeqStore, + nuint nbBytes + ) { uint currPos = (uint)(rawSeqStore->posInSequence + nbBytes); while (currPos != 0 && rawSeqStore->pos < rawSeqStore->size) @@ -966,7 +1555,11 @@ namespace ZstdSharp.Unsafe * 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) + private static void ZSTD_opt_getNextMatchAndUpdateSeqStore( + ZSTD_optLdm_t* optLdm, + uint currPosInBlock, + uint blockBytesRemaining + ) { rawSeq currSeq; uint currBlockEndPos; @@ -982,8 +1575,15 @@ namespace ZstdSharp.Unsafe 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; + 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; @@ -998,11 +1598,17 @@ namespace ZstdSharp.Unsafe if (optLdm->endPosInBlock > currBlockEndPos) { optLdm->endPosInBlock = currBlockEndPos; - ZSTD_optLdm_skipRawSeqStoreBytes(&optLdm->seqStore, currBlockEndPos - currPosInBlock); + ZSTD_optLdm_skipRawSeqStoreBytes( + &optLdm->seqStore, + currBlockEndPos - currPosInBlock + ); } else { - ZSTD_optLdm_skipRawSeqStoreBytes(&optLdm->seqStore, literalsBytesRemaining + matchBytesRemaining); + ZSTD_optLdm_skipRawSeqStoreBytes( + &optLdm->seqStore, + literalsBytesRemaining + matchBytesRemaining + ); } } @@ -1011,17 +1617,30 @@ namespace ZstdSharp.Unsafe * 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 minMatch) + private static void ZSTD_optLdm_maybeAddMatch( + ZSTD_match_t* matches, + uint* nbMatches, + ZSTD_optLdm_t* optLdm, + uint currPosInBlock, + uint minMatch + ) { uint posDiff = currPosInBlock - optLdm->startPosInBlock; /* Note: ZSTD_match_t actually contains offBase and matchLength (before subtracting MINMATCH) */ uint candidateMatchLength = optLdm->endPosInBlock - optLdm->startPosInBlock - posDiff; - if (currPosInBlock < optLdm->startPosInBlock || currPosInBlock >= optLdm->endPosInBlock || candidateMatchLength < minMatch) + if ( + currPosInBlock < optLdm->startPosInBlock + || currPosInBlock >= optLdm->endPosInBlock + || candidateMatchLength < minMatch + ) { return; } - if (*nbMatches == 0 || candidateMatchLength > matches[*nbMatches - 1].len && *nbMatches < 1 << 12) + if ( + *nbMatches == 0 + || candidateMatchLength > matches[*nbMatches - 1].len && *nbMatches < 1 << 12 + ) { assert(optLdm->offset > 0); uint candidateOffBase = optLdm->offset + 3; @@ -1034,7 +1653,14 @@ namespace ZstdSharp.Unsafe /* 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, uint minMatch) + private static void ZSTD_optLdm_processMatchCandidate( + ZSTD_optLdm_t* optLdm, + ZSTD_match_t* matches, + uint* nbMatches, + uint currPosInBlock, + uint remainingBytes, + uint minMatch + ) { if (optLdm->seqStore.size == 0 || optLdm->seqStore.pos >= optLdm->seqStore.size) { @@ -1060,7 +1686,15 @@ namespace ZstdSharp.Unsafe } [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) + 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; @@ -1072,7 +1706,8 @@ namespace ZstdSharp.Unsafe byte* prefixStart = @base + ms->window.dictLimit; ZSTD_compressionParameters* cParams = &ms->cParams; ZSTD_getAllMatchesFn getAllMatches = ZSTD_selectBtGetAllMatches(ms, dictMode); - uint sufficient_len = cParams->targetLength < (1 << 12) - 1 ? cParams->targetLength : (1 << 12) - 1; + uint sufficient_len = + cParams->targetLength < (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; @@ -1088,12 +1723,29 @@ namespace ZstdSharp.Unsafe ip += ip == prefixStart ? 1 : 0; while (ip < ilimit) { - uint cur, last_pos = 0; + uint cur, + last_pos = 0; { uint litlen = (uint)(ip - anchor); uint ll0 = litlen == 0 ? 1U : 0U; - uint nbMatches = getAllMatches(matches, ms, &nextToUpdate3, ip, iend, rep, ll0, minMatch); - ZSTD_optLdm_processMatchCandidate(&optLdm, matches, &nbMatches, (uint)(ip - istart), (uint)(iend - ip), minMatch); + uint nbMatches = getAllMatches( + matches, + ms, + &nextToUpdate3, + ip, + iend, + rep, + ll0, + minMatch + ); + ZSTD_optLdm_processMatchCandidate( + &optLdm, + matches, + &nbMatches, + (uint)(ip - istart), + (uint)(iend - ip), + minMatch + ); if (nbMatches == 0) { ip++; @@ -1135,12 +1787,19 @@ namespace ZstdSharp.Unsafe uint end = matches[matchNb].len; for (; pos <= end; pos++) { - int matchPrice = (int)ZSTD_getMatchPrice(offBase, pos, optStatePtr, optLevel); + int matchPrice = (int)ZSTD_getMatchPrice( + offBase, + pos, + optStatePtr, + optLevel + ); int sequencePrice = opt[0].price + matchPrice; opt[pos].mlen = pos; opt[pos].off = offBase; opt[pos].litlen = 0; - opt[pos].price = sequencePrice + (int)ZSTD_litLengthPrice(0, optStatePtr, optLevel); + opt[pos].price = + sequencePrice + + (int)ZSTD_litLengthPrice(0, optStatePtr, optLevel); } } @@ -1155,7 +1814,13 @@ namespace ZstdSharp.Unsafe assert(cur <= 1 << 12); { uint litlen = opt[cur - 1].litlen + 1; - int price = opt[cur - 1].price + (int)ZSTD_rawLiteralsCost(ip + cur - 1, 1, optStatePtr, optLevel) + ((int)ZSTD_litLengthPrice(litlen, optStatePtr, optLevel) - (int)ZSTD_litLengthPrice(litlen - 1, optStatePtr, optLevel)); + int price = + opt[cur - 1].price + + (int)ZSTD_rawLiteralsCost(ip + cur - 1, 1, optStatePtr, optLevel) + + ( + (int)ZSTD_litLengthPrice(litlen, optStatePtr, optLevel) + - (int)ZSTD_litLengthPrice(litlen - 1, optStatePtr, optLevel) + ); assert(price < 1000000000); if (price <= opt[cur].price) { @@ -1163,16 +1828,46 @@ namespace ZstdSharp.Unsafe opt[cur] = opt[cur - 1]; opt[cur].litlen = litlen; opt[cur].price = price; - if (optLevel >= 1 && prevMatch.litlen == 0 && (int)ZSTD_litLengthPrice(1, optStatePtr, optLevel) - (int)ZSTD_litLengthPrice(1 - 1, optStatePtr, optLevel) < 0 && ip + cur < iend) + if ( + optLevel >= 1 + && prevMatch.litlen == 0 + && (int)ZSTD_litLengthPrice(1, optStatePtr, optLevel) + - (int)ZSTD_litLengthPrice(1 - 1, optStatePtr, optLevel) + < 0 + && ip + cur < iend + ) { /* check next position, in case it would be cheaper */ - int with1literal = prevMatch.price + (int)ZSTD_rawLiteralsCost(ip + cur, 1, optStatePtr, optLevel) + ((int)ZSTD_litLengthPrice(1, optStatePtr, optLevel) - (int)ZSTD_litLengthPrice(1 - 1, optStatePtr, optLevel)); - int withMoreLiterals = price + (int)ZSTD_rawLiteralsCost(ip + cur, 1, optStatePtr, optLevel) + ((int)ZSTD_litLengthPrice(litlen + 1, optStatePtr, optLevel) - (int)ZSTD_litLengthPrice(litlen + 1 - 1, optStatePtr, optLevel)); - if (with1literal < withMoreLiterals && with1literal < opt[cur + 1].price) + int with1literal = + prevMatch.price + + (int)ZSTD_rawLiteralsCost(ip + cur, 1, optStatePtr, optLevel) + + ( + (int)ZSTD_litLengthPrice(1, optStatePtr, optLevel) + - (int)ZSTD_litLengthPrice(1 - 1, optStatePtr, optLevel) + ); + int withMoreLiterals = + price + + (int)ZSTD_rawLiteralsCost(ip + cur, 1, optStatePtr, optLevel) + + ( + (int)ZSTD_litLengthPrice(litlen + 1, optStatePtr, optLevel) + - (int)ZSTD_litLengthPrice( + litlen + 1 - 1, + optStatePtr, + optLevel + ) + ); + if ( + with1literal < withMoreLiterals + && with1literal < opt[cur + 1].price + ) { /* update offset history - before it disappears */ uint prev = cur - prevMatch.mlen; - repcodes_s newReps = ZSTD_newRep(opt[prev].rep, prevMatch.off, opt[prev].litlen == 0 ? 1U : 0U); + repcodes_s newReps = ZSTD_newRep( + opt[prev].rep, + prevMatch.off, + opt[prev].litlen == 0 ? 1U : 0U + ); assert(cur >= prevMatch.mlen); opt[cur + 1] = prevMatch; memcpy(opt[cur + 1].rep, &newReps, (uint)sizeof(repcodes_s)); @@ -1190,7 +1885,11 @@ namespace ZstdSharp.Unsafe { /* just finished a match => alter offset history */ uint prev = cur - opt[cur].mlen; - repcodes_s newReps = ZSTD_newRep(opt[prev].rep, opt[cur].off, opt[prev].litlen == 0 ? 1U : 0U); + repcodes_s newReps = ZSTD_newRep( + opt[prev].rep, + opt[cur].off, + opt[prev].litlen == 0 ? 1U : 0U + ); memcpy(opt[cur].rep, &newReps, (uint)sizeof(repcodes_s)); } @@ -1207,10 +1906,27 @@ namespace ZstdSharp.Unsafe { uint ll0 = opt[cur].litlen == 0 ? 1U : 0U; int previousPrice = opt[cur].price; - int basePrice = previousPrice + (int)ZSTD_litLengthPrice(0, optStatePtr, optLevel); - uint nbMatches = getAllMatches(matches, ms, &nextToUpdate3, inr, iend, opt[cur].rep, ll0, minMatch); + int basePrice = + previousPrice + (int)ZSTD_litLengthPrice(0, optStatePtr, optLevel); + uint nbMatches = getAllMatches( + matches, + ms, + &nextToUpdate3, + inr, + iend, + opt[cur].rep, + ll0, + minMatch + ); uint matchNb; - ZSTD_optLdm_processMatchCandidate(&optLdm, matches, &nbMatches, (uint)(inr - istart), (uint)(iend - inr), minMatch); + ZSTD_optLdm_processMatchCandidate( + &optLdm, + matches, + &nbMatches, + (uint)(inr - istart), + (uint)(iend - inr), + minMatch + ); if (nbMatches == 0) { continue; @@ -1218,7 +1934,11 @@ namespace ZstdSharp.Unsafe { uint longestML = matches[nbMatches - 1].len; - if (longestML > sufficient_len || cur + longestML >= 1 << 12 || ip + cur + longestML >= iend) + if ( + longestML > sufficient_len + || cur + longestML >= 1 << 12 + || ip + cur + longestML >= iend + ) { lastStretch.mlen = longestML; lastStretch.off = matches[nbMatches - 1].off; @@ -1237,7 +1957,9 @@ namespace ZstdSharp.Unsafe for (mlen = lastML; mlen >= startML; mlen--) { uint pos = cur + mlen; - int price = basePrice + (int)ZSTD_getMatchPrice(offset, mlen, optStatePtr, optLevel); + int price = + basePrice + + (int)ZSTD_getMatchPrice(offset, mlen, optStatePtr, optLevel); if (pos > last_pos || price < opt[pos].price) { while (last_pos < pos) @@ -1267,7 +1989,7 @@ namespace ZstdSharp.Unsafe lastStretch = opt[last_pos]; assert(cur >= lastStretch.mlen); cur = last_pos - lastStretch.mlen; - _shortestPath: + _shortestPath: assert(opt[0].mlen == 0); assert(last_pos >= lastStretch.mlen); assert(cur == last_pos - lastStretch.mlen); @@ -1282,7 +2004,11 @@ namespace ZstdSharp.Unsafe if (lastStretch.litlen == 0) { /* finishing on a match : update offset history */ - repcodes_s reps = ZSTD_newRep(opt[cur].rep, lastStretch.off, opt[cur].litlen == 0 ? 1U : 0U); + repcodes_s reps = ZSTD_newRep( + opt[cur].rep, + lastStretch.off, + opt[cur].litlen == 0 ? 1U : 0U + ); memcpy(rep, &reps, (uint)sizeof(repcodes_s)); } else @@ -1355,19 +2081,46 @@ namespace ZstdSharp.Unsafe return (nuint)(iend - anchor); } - private static nuint ZSTD_compressBlock_opt0(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize, ZSTD_dictMode_e dictMode) + private static nuint ZSTD_compressBlock_opt0( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize, + ZSTD_dictMode_e dictMode + ) { return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 0, dictMode); } - private static nuint ZSTD_compressBlock_opt2(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize, ZSTD_dictMode_e dictMode) + private static nuint ZSTD_compressBlock_opt2( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize, + ZSTD_dictMode_e dictMode + ) { return ZSTD_compressBlock_opt_generic(ms, seqStore, rep, src, srcSize, 2, dictMode); } - private static nuint ZSTD_compressBlock_btopt(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_btopt( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_dictMode_e.ZSTD_noDict); + return ZSTD_compressBlock_opt0( + ms, + seqStore, + rep, + src, + srcSize, + ZSTD_dictMode_e.ZSTD_noDict + ); } /* ZSTD_initStats_ultra(): @@ -1375,7 +2128,13 @@ namespace ZstdSharp.Unsafe * only works on first block, with no dictionary and no ldm. * this function cannot error out, its narrow contract must be respected. */ - private static void ZSTD_initStats_ultra(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static void ZSTD_initStats_ultra( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { /* updated rep codes will sink here */ uint* tmpRep = stackalloc uint[3]; @@ -1384,7 +2143,14 @@ namespace ZstdSharp.Unsafe assert(seqStore->sequences == seqStore->sequencesStart); assert(ms->window.dictLimit == ms->window.lowLimit); assert(ms->window.dictLimit - ms->nextToUpdate <= 1); - ZSTD_compressBlock_opt2(ms, seqStore, tmpRep, src, srcSize, ZSTD_dictMode_e.ZSTD_noDict); + ZSTD_compressBlock_opt2( + ms, + seqStore, + tmpRep, + src, + srcSize, + ZSTD_dictMode_e.ZSTD_noDict + ); ZSTD_resetSeqStore(seqStore); ms->window.@base -= srcSize; ms->window.dictLimit += (uint)srcSize; @@ -1392,44 +2158,128 @@ namespace ZstdSharp.Unsafe ms->nextToUpdate = ms->window.dictLimit; } - private static nuint ZSTD_compressBlock_btultra(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_btultra( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_dictMode_e.ZSTD_noDict); + return ZSTD_compressBlock_opt2( + ms, + seqStore, + rep, + src, + srcSize, + ZSTD_dictMode_e.ZSTD_noDict + ); } /* note : no btultra2 variant for extDict nor dictMatchState, * because btultra2 is not meant to work with dictionaries * and is only specific for the first block (no prefix) */ - private static nuint ZSTD_compressBlock_btultra2(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private 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 <= 1 << 17); - if (ms->opt.litLengthSum == 0 && seqStore->sequences == seqStore->sequencesStart && ms->window.dictLimit == ms->window.lowLimit && curr == ms->window.dictLimit && srcSize > 8) + if ( + ms->opt.litLengthSum == 0 + && seqStore->sequences == seqStore->sequencesStart + && ms->window.dictLimit == ms->window.lowLimit + && curr == ms->window.dictLimit + && srcSize > 8 + ) { ZSTD_initStats_ultra(ms, seqStore, rep, src, srcSize); } - return ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_dictMode_e.ZSTD_noDict); + return ZSTD_compressBlock_opt2( + ms, + seqStore, + rep, + src, + srcSize, + ZSTD_dictMode_e.ZSTD_noDict + ); } - private static nuint ZSTD_compressBlock_btopt_dictMatchState(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_btopt_dictMatchState( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_dictMode_e.ZSTD_dictMatchState); + return ZSTD_compressBlock_opt0( + ms, + seqStore, + rep, + src, + srcSize, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_compressBlock_btopt_extDict(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_btopt_extDict( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_opt0(ms, seqStore, rep, src, srcSize, ZSTD_dictMode_e.ZSTD_extDict); + return ZSTD_compressBlock_opt0( + ms, + seqStore, + rep, + src, + srcSize, + ZSTD_dictMode_e.ZSTD_extDict + ); } - private static nuint ZSTD_compressBlock_btultra_dictMatchState(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_btultra_dictMatchState( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_dictMode_e.ZSTD_dictMatchState); + return ZSTD_compressBlock_opt2( + ms, + seqStore, + rep, + src, + srcSize, + ZSTD_dictMode_e.ZSTD_dictMatchState + ); } - private static nuint ZSTD_compressBlock_btultra_extDict(ZSTD_MatchState_t* ms, SeqStore_t* seqStore, uint* rep, void* src, nuint srcSize) + private static nuint ZSTD_compressBlock_btultra_extDict( + ZSTD_MatchState_t* ms, + SeqStore_t* seqStore, + uint* rep, + void* src, + nuint srcSize + ) { - return ZSTD_compressBlock_opt2(ms, seqStore, rep, src, srcSize, ZSTD_dictMode_e.ZSTD_extDict); + return ZSTD_compressBlock_opt2( + ms, + seqStore, + rep, + src, + srcSize, + ZSTD_dictMode_e.ZSTD_extDict + ); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdPresplit.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdPresplit.cs index 22a13a59..96ba2eab 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdPresplit.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdPresplit.cs @@ -1,7 +1,7 @@ -using System.Runtime.CompilerServices; -using static ZstdSharp.UnsafeHelper; using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { @@ -26,7 +26,13 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void addEvents_generic(Fingerprint* fp, void* src, nuint srcSize, nuint samplingRate, uint hashLog) + private static void addEvents_generic( + Fingerprint* fp, + void* src, + nuint srcSize, + nuint samplingRate, + uint hashLog + ) { sbyte* p = (sbyte*)src; nuint limit = srcSize - 2 + 1; @@ -41,7 +47,13 @@ namespace ZstdSharp.Unsafe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void recordFingerprint_generic(Fingerprint* fp, void* src, nuint srcSize, nuint samplingRate, uint hashLog) + private static void recordFingerprint_generic( + Fingerprint* fp, + void* src, + nuint srcSize, + nuint samplingRate, + uint hashLog + ) { memset(fp, 0, (uint)(sizeof(uint) * ((nuint)1 << (int)hashLog))); fp->nbEvents = 0; @@ -80,7 +92,9 @@ namespace ZstdSharp.Unsafe assert(hashLog <= 10); for (n = 0; n < (nuint)1 << (int)hashLog; n++) { - distance += abs64(fp1->events[n] * (long)fp2->nbEvents - fp2->events[n] * (long)fp1->nbEvents); + distance += abs64( + fp1->events[n] * (long)fp2->nbEvents - fp2->events[n] * (long)fp1->nbEvents + ); } return distance; @@ -89,7 +103,12 @@ namespace ZstdSharp.Unsafe /* Compare newEvents with pastEvents * return 1 when considered "too different" */ - private static int compareFingerprints(Fingerprint* @ref, Fingerprint* newfp, int penalty, uint hashLog) + private static int compareFingerprints( + Fingerprint* @ref, + Fingerprint* newfp, + int penalty, + uint hashLog + ) { assert(@ref->nbEvents > 0); assert(newfp->nbEvents > 0); @@ -138,25 +157,30 @@ namespace ZstdSharp.Unsafe private static readonly void*[] records_fs = new void*[4] { - (delegate* managed )(&ZSTD_recordFingerprint_43), - (delegate* managed )(&ZSTD_recordFingerprint_11), - (delegate* managed )(&ZSTD_recordFingerprint_5), - (delegate* managed )(&ZSTD_recordFingerprint_1) + (delegate* managed)(&ZSTD_recordFingerprint_43), + (delegate* managed)(&ZSTD_recordFingerprint_11), + (delegate* managed)(&ZSTD_recordFingerprint_5), + (delegate* managed)(&ZSTD_recordFingerprint_1), }; #if NET7_0_OR_GREATER - private static ReadOnlySpan Span_hashParams => new uint[4] - { - 8, - 9, - 10, - 10 - }; - private static uint* hashParams => (uint*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref MemoryMarshal.GetReference(Span_hashParams)); + private static ReadOnlySpan Span_hashParams => new uint[4] { 8, 9, 10, 10 }; + private static uint* hashParams => + (uint*) + System.Runtime.CompilerServices.Unsafe.AsPointer( + ref MemoryMarshal.GetReference(Span_hashParams) + ); #else private static readonly uint* hashParams = GetArrayPointer(new uint[4] { 8, 9, 10, 10 }); #endif - private static nuint ZSTD_splitBlock_byChunks(void* blockStart, nuint blockSize, int level, void* workspace, nuint wkspSize) + + private static nuint ZSTD_splitBlock_byChunks( + void* blockStart, + nuint blockSize, + int level, + void* workspace, + nuint wkspSize + ) { assert(0 <= level && level <= 3); void* record_f = records_fs[level]; @@ -169,11 +193,26 @@ namespace ZstdSharp.Unsafe assert((nuint)workspace % (nuint)Math.Max(sizeof(uint), sizeof(ulong)) == 0); assert(wkspSize >= (nuint)sizeof(FPStats)); initStats(fpstats); - ((delegate* managed)record_f)(&fpstats->pastEvents, p, 8 << 10); + ((delegate* managed)record_f)( + &fpstats->pastEvents, + p, + 8 << 10 + ); for (pos = 8 << 10; pos <= blockSize - (8 << 10); pos += 8 << 10) { - ((delegate* managed)record_f)(&fpstats->newEvents, p + pos, 8 << 10); - if (compareFingerprints(&fpstats->pastEvents, &fpstats->newEvents, penalty, hashParams[level]) != 0) + ((delegate* managed)record_f)( + &fpstats->newEvents, + p + pos, + 8 << 10 + ); + if ( + compareFingerprints( + &fpstats->pastEvents, + &fpstats->newEvents, + penalty, + hashParams[level] + ) != 0 + ) { return pos; } @@ -198,10 +237,16 @@ namespace ZstdSharp.Unsafe * More accurate splitting saves more, but speed impact is also more perceptible. * For better accuracy, use more elaborate variant *_byChunks. */ - private static nuint ZSTD_splitBlock_fromBorders(void* blockStart, nuint blockSize, void* workspace, nuint wkspSize) + private static nuint ZSTD_splitBlock_fromBorders( + void* blockStart, + nuint blockSize, + void* workspace, + nuint wkspSize + ) { FPStats* fpstats = (FPStats*)workspace; - Fingerprint* middleEvents = (Fingerprint*)(void*)((sbyte*)workspace + 512 * sizeof(uint)); + Fingerprint* middleEvents = (Fingerprint*) + (void*)((sbyte*)workspace + 512 * sizeof(uint)); assert(blockSize == 128 << 10); assert(workspace != null); assert((nuint)workspace % (nuint)Math.Max(sizeof(uint), sizeof(ulong)) == 0); @@ -235,7 +280,13 @@ namespace ZstdSharp.Unsafe * While this could be extended to smaller sizes in the future, * it is not yet clear if this would be useful. TBD. */ - private static nuint ZSTD_splitBlock(void* blockStart, nuint blockSize, int level, void* workspace, nuint wkspSize) + private static nuint ZSTD_splitBlock( + void* blockStart, + nuint blockSize, + int level, + void* workspace, + nuint wkspSize + ) { assert(0 <= level && level <= 4); if (level == 0) @@ -243,4 +294,4 @@ namespace ZstdSharp.Unsafe return ZSTD_splitBlock_byChunks(blockStart, blockSize, level - 1, workspace, wkspSize); } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdmtCompress.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdmtCompress.cs index b379f7eb..bcf2da7b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdmtCompress.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ZstdmtCompress.cs @@ -1,11 +1,12 @@ -using static ZstdSharp.UnsafeHelper; using System.Runtime.CompilerServices; +using static ZstdSharp.UnsafeHelper; namespace ZstdSharp.Unsafe { public static unsafe partial class Methods { private static readonly buffer_s g_nullBuffer = new buffer_s(start: null, capacity: 0); + private static void ZSTDMT_freeBufferPool(ZSTDMT_bufferPool_s* bufPool) { if (bufPool == null) @@ -25,13 +26,22 @@ namespace ZstdSharp.Unsafe ZSTD_customFree(bufPool, bufPool->cMem); } - private static ZSTDMT_bufferPool_s* ZSTDMT_createBufferPool(uint maxNbBuffers, ZSTD_customMem cMem) + private static ZSTDMT_bufferPool_s* ZSTDMT_createBufferPool( + uint maxNbBuffers, + ZSTD_customMem cMem + ) { - ZSTDMT_bufferPool_s* bufPool = (ZSTDMT_bufferPool_s*)ZSTD_customCalloc((nuint)sizeof(ZSTDMT_bufferPool_s), cMem); + ZSTDMT_bufferPool_s* bufPool = (ZSTDMT_bufferPool_s*)ZSTD_customCalloc( + (nuint)sizeof(ZSTDMT_bufferPool_s), + cMem + ); if (bufPool == null) return null; SynchronizationWrapper.Init(&bufPool->poolMutex); - bufPool->buffers = (buffer_s*)ZSTD_customCalloc(maxNbBuffers * (uint)sizeof(buffer_s), cMem); + bufPool->buffers = (buffer_s*)ZSTD_customCalloc( + maxNbBuffers * (uint)sizeof(buffer_s), + cMem + ); if (bufPool->buffers == null) { ZSTDMT_freeBufferPool(bufPool); @@ -70,7 +80,10 @@ namespace ZstdSharp.Unsafe SynchronizationWrapper.Exit(&bufPool->poolMutex); } - private static ZSTDMT_bufferPool_s* ZSTDMT_expandBufferPool(ZSTDMT_bufferPool_s* srcBufPool, uint maxNbBuffers) + private static ZSTDMT_bufferPool_s* ZSTDMT_expandBufferPool( + ZSTDMT_bufferPool_s* srcBufPool, + uint maxNbBuffers + ) { if (srcBufPool == null) return null; @@ -180,7 +193,10 @@ namespace ZstdSharp.Unsafe ZSTDMT_setBufferSize(seqPool, nbSeq * (nuint)sizeof(rawSeq)); } - private static ZSTDMT_bufferPool_s* ZSTDMT_createSeqPool(uint nbWorkers, ZSTD_customMem cMem) + private static ZSTDMT_bufferPool_s* ZSTDMT_createSeqPool( + uint nbWorkers, + ZSTD_customMem cMem + ) { ZSTDMT_bufferPool_s* seqPool = ZSTDMT_createBufferPool(nbWorkers, cMem); if (seqPool == null) @@ -194,7 +210,10 @@ namespace ZstdSharp.Unsafe ZSTDMT_freeBufferPool(seqPool); } - private static ZSTDMT_bufferPool_s* ZSTDMT_expandSeqPool(ZSTDMT_bufferPool_s* pool, uint nbWorkers) + private static ZSTDMT_bufferPool_s* ZSTDMT_expandSeqPool( + ZSTDMT_bufferPool_s* pool, + uint nbWorkers + ) { return ZSTDMT_expandBufferPool(pool, nbWorkers); } @@ -220,13 +239,19 @@ namespace ZstdSharp.Unsafe * implies nbWorkers >= 1 , checked by caller ZSTDMT_createCCtx() */ private static ZSTDMT_CCtxPool* ZSTDMT_createCCtxPool(int nbWorkers, ZSTD_customMem cMem) { - ZSTDMT_CCtxPool* cctxPool = (ZSTDMT_CCtxPool*)ZSTD_customCalloc((nuint)sizeof(ZSTDMT_CCtxPool), cMem); + ZSTDMT_CCtxPool* cctxPool = (ZSTDMT_CCtxPool*)ZSTD_customCalloc( + (nuint)sizeof(ZSTDMT_CCtxPool), + cMem + ); assert(nbWorkers > 0); if (cctxPool == null) return null; SynchronizationWrapper.Init(&cctxPool->poolMutex); cctxPool->totalCCtx = nbWorkers; - cctxPool->cctxs = (ZSTD_CCtx_s**)ZSTD_customCalloc((nuint)(nbWorkers * sizeof(ZSTD_CCtx_s*)), cMem); + cctxPool->cctxs = (ZSTD_CCtx_s**)ZSTD_customCalloc( + (nuint)(nbWorkers * sizeof(ZSTD_CCtx_s*)), + cMem + ); if (cctxPool->cctxs == null) { ZSTDMT_freeCCtxPool(cctxPool); @@ -245,7 +270,10 @@ namespace ZstdSharp.Unsafe return cctxPool; } - private static ZSTDMT_CCtxPool* ZSTDMT_expandCCtxPool(ZSTDMT_CCtxPool* srcPool, int nbWorkers) + private static ZSTDMT_CCtxPool* ZSTDMT_expandCCtxPool( + ZSTDMT_CCtxPool* srcPool, + int nbWorkers + ) { if (srcPool == null) return null; @@ -311,7 +339,15 @@ namespace ZstdSharp.Unsafe SynchronizationWrapper.Exit(&pool->poolMutex); } - private static int ZSTDMT_serialState_reset(SerialState* serialState, ZSTDMT_bufferPool_s* seqPool, ZSTD_CCtx_params_s @params, nuint jobSize, void* dict, nuint dictSize, ZSTD_dictContentType_e dictContentType) + private static int ZSTDMT_serialState_reset( + SerialState* serialState, + ZSTDMT_bufferPool_s* seqPool, + ZSTD_CCtx_params_s @params, + nuint jobSize, + void* dict, + nuint dictSize, + ZSTD_dictContentType_e dictContentType + ) { if (@params.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable) { @@ -333,23 +369,37 @@ namespace ZstdSharp.Unsafe uint hashLog = @params.ldmParams.hashLog; nuint hashSize = ((nuint)1 << (int)hashLog) * (nuint)sizeof(ldmEntry_t); uint bucketLog = @params.ldmParams.hashLog - @params.ldmParams.bucketSizeLog; - uint prevBucketLog = serialState->@params.ldmParams.hashLog - serialState->@params.ldmParams.bucketSizeLog; + uint prevBucketLog = + serialState->@params.ldmParams.hashLog + - serialState->@params.ldmParams.bucketSizeLog; nuint numBuckets = (nuint)1 << (int)bucketLog; ZSTDMT_setNbSeq(seqPool, ZSTD_ldm_getMaxNbSeq(@params.ldmParams, jobSize)); ZSTD_window_init(&serialState->ldmState.window); - if (serialState->ldmState.hashTable == null || serialState->@params.ldmParams.hashLog < hashLog) + if ( + serialState->ldmState.hashTable == null + || serialState->@params.ldmParams.hashLog < hashLog + ) { ZSTD_customFree(serialState->ldmState.hashTable, cMem); - serialState->ldmState.hashTable = (ldmEntry_t*)ZSTD_customMalloc(hashSize, cMem); + serialState->ldmState.hashTable = (ldmEntry_t*)ZSTD_customMalloc( + hashSize, + cMem + ); } if (serialState->ldmState.bucketOffsets == null || prevBucketLog < bucketLog) { ZSTD_customFree(serialState->ldmState.bucketOffsets, cMem); - serialState->ldmState.bucketOffsets = (byte*)ZSTD_customMalloc(numBuckets, cMem); + serialState->ldmState.bucketOffsets = (byte*)ZSTD_customMalloc( + numBuckets, + cMem + ); } - if (serialState->ldmState.hashTable == null || serialState->ldmState.bucketOffsets == null) + if ( + serialState->ldmState.hashTable == null + || serialState->ldmState.bucketOffsets == null + ) return 1; memset(serialState->ldmState.hashTable, 0, (uint)hashSize); memset(serialState->ldmState.bucketOffsets, 0, (uint)numBuckets); @@ -360,8 +410,16 @@ namespace ZstdSharp.Unsafe { byte* dictEnd = (byte*)dict + dictSize; ZSTD_window_update(&serialState->ldmState.window, dict, dictSize, 0); - ZSTD_ldm_fillHashTable(&serialState->ldmState, (byte*)dict, dictEnd, &@params.ldmParams); - serialState->ldmState.loadedDictEnd = @params.forceWindow != 0 ? 0 : (uint)(dictEnd - serialState->ldmState.window.@base); + ZSTD_ldm_fillHashTable( + &serialState->ldmState, + (byte*)dict, + dictEnd, + &@params.ldmParams + ); + serialState->ldmState.loadedDictEnd = + @params.forceWindow != 0 + ? 0 + : (uint)(dictEnd - serialState->ldmState.window.@base); } } @@ -395,7 +453,12 @@ namespace ZstdSharp.Unsafe ZSTD_customFree(serialState->ldmState.bucketOffsets, cMem); } - private static void ZSTDMT_serialState_genSequences(SerialState* serialState, RawSeqStore_t* seqStore, Range src, uint jobID) + private static void ZSTDMT_serialState_genSequences( + SerialState* serialState, + RawSeqStore_t* seqStore, + Range src, + uint jobID + ) { SynchronizationWrapper.Enter(&serialState->mutex); while (serialState->nextJobID < jobID) @@ -408,10 +471,21 @@ namespace ZstdSharp.Unsafe if (serialState->@params.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable) { nuint error; - assert(seqStore->seq != null && seqStore->pos == 0 && seqStore->size == 0 && seqStore->capacity > 0); + assert( + seqStore->seq != null + && seqStore->pos == 0 + && seqStore->size == 0 + && seqStore->capacity > 0 + ); assert(src.size <= serialState->@params.jobSize); ZSTD_window_update(&serialState->ldmState.window, src.start, src.size, 0); - error = ZSTD_ldm_generateSequences(&serialState->ldmState, seqStore, &serialState->@params.ldmParams, src.start, src.size); + error = ZSTD_ldm_generateSequences( + &serialState->ldmState, + seqStore, + &serialState->@params.ldmParams, + src.start, + src.size + ); assert(!ERR_isError(error)); SynchronizationWrapper.Enter(&serialState->ldmWindowMutex); serialState->ldmWindow = serialState->ldmState.window; @@ -428,17 +502,27 @@ namespace ZstdSharp.Unsafe SynchronizationWrapper.Exit(&serialState->mutex); } - private static void ZSTDMT_serialState_applySequences(SerialState* serialState, ZSTD_CCtx_s* jobCCtx, RawSeqStore_t* seqStore) + private static void ZSTDMT_serialState_applySequences( + SerialState* serialState, + ZSTD_CCtx_s* jobCCtx, + RawSeqStore_t* seqStore + ) { if (seqStore->size > 0) { - assert(serialState->@params.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable); + assert( + serialState->@params.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable + ); assert(jobCCtx != null); ZSTD_referenceExternalSequences(jobCCtx, seqStore->seq, seqStore->size); } } - private static void ZSTDMT_serialState_ensureFinished(SerialState* serialState, uint jobID, nuint cSize) + private static void ZSTDMT_serialState_ensureFinished( + SerialState* serialState, + uint jobID, + nuint cSize + ) { SynchronizationWrapper.Enter(&serialState->mutex); if (serialState->nextJobID <= jobID) @@ -456,6 +540,7 @@ namespace ZstdSharp.Unsafe } private static readonly Range kNullRange = new Range(start: null, size: 0); + /* ZSTDMT_compressionJob() is a POOL_function type */ private static void ZSTDMT_compressionJob(void* jobDescription) { @@ -480,7 +565,9 @@ namespace ZstdSharp.Unsafe if (dstBuff.start == null) { SynchronizationWrapper.Enter(&job->job_mutex); - job->cSize = unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + job->cSize = unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); SynchronizationWrapper.Exit(&job->job_mutex); goto _endJob; } @@ -488,7 +575,10 @@ namespace ZstdSharp.Unsafe job->dstBuff = dstBuff; } - if (jobParams.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable && rawSeqStore.seq == null) + if ( + jobParams.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable + && rawSeqStore.seq == null + ) { SynchronizationWrapper.Enter(&job->job_mutex); job->cSize = unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); @@ -503,7 +593,16 @@ namespace ZstdSharp.Unsafe ZSTDMT_serialState_genSequences(job->serial, &rawSeqStore, job->src, job->jobID); if (job->cdict != null) { - nuint initError = ZSTD_compressBegin_advanced_internal(cctx, null, 0, ZSTD_dictContentType_e.ZSTD_dct_auto, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, job->cdict, &jobParams, job->fullFrameSize); + nuint initError = ZSTD_compressBegin_advanced_internal( + cctx, + null, + 0, + ZSTD_dictContentType_e.ZSTD_dct_auto, + ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, + job->cdict, + &jobParams, + job->fullFrameSize + ); assert(job->firstJob != 0); if (ERR_isError(initError)) { @@ -517,7 +616,11 @@ namespace ZstdSharp.Unsafe { ulong pledgedSrcSize = job->firstJob != 0 ? job->fullFrameSize : job->src.size; { - nuint forceWindowError = ZSTD_CCtxParams_setParameter(&jobParams, ZSTD_cParameter.ZSTD_c_experimentalParam3, job->firstJob == 0 ? 1 : 0); + nuint forceWindowError = ZSTD_CCtxParams_setParameter( + &jobParams, + ZSTD_cParameter.ZSTD_c_experimentalParam3, + job->firstJob == 0 ? 1 : 0 + ); if (ERR_isError(forceWindowError)) { SynchronizationWrapper.Enter(&job->job_mutex); @@ -529,7 +632,11 @@ namespace ZstdSharp.Unsafe if (job->firstJob == 0) { - nuint err = ZSTD_CCtxParams_setParameter(&jobParams, ZSTD_cParameter.ZSTD_c_experimentalParam15, 0); + nuint err = ZSTD_CCtxParams_setParameter( + &jobParams, + ZSTD_cParameter.ZSTD_c_experimentalParam15, + 0 + ); if (ERR_isError(err)) { SynchronizationWrapper.Enter(&job->job_mutex); @@ -540,7 +647,16 @@ namespace ZstdSharp.Unsafe } { - nuint initError = ZSTD_compressBegin_advanced_internal(cctx, job->prefix.start, job->prefix.size, ZSTD_dictContentType_e.ZSTD_dct_rawContent, ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, null, &jobParams, pledgedSrcSize); + nuint initError = ZSTD_compressBegin_advanced_internal( + cctx, + job->prefix.start, + job->prefix.size, + ZSTD_dictContentType_e.ZSTD_dct_rawContent, + ZSTD_dictTableLoadMethod_e.ZSTD_dtlm_fast, + null, + &jobParams, + pledgedSrcSize + ); if (ERR_isError(initError)) { SynchronizationWrapper.Enter(&job->job_mutex); @@ -554,7 +670,13 @@ namespace ZstdSharp.Unsafe ZSTDMT_serialState_applySequences(job->serial, cctx, &rawSeqStore); if (job->firstJob == 0) { - nuint hSize = ZSTD_compressContinue_public(cctx, dstBuff.start, dstBuff.capacity, job->src.start, 0); + nuint hSize = ZSTD_compressContinue_public( + cctx, + dstBuff.start, + dstBuff.capacity, + job->src.start, + 0 + ); if (ERR_isError(hSize)) { SynchronizationWrapper.Enter(&job->job_mutex); @@ -581,7 +703,13 @@ namespace ZstdSharp.Unsafe assert(job->cSize == 0); for (chunkNb = 1; chunkNb < nbChunks; chunkNb++) { - nuint cSize = ZSTD_compressContinue_public(cctx, op, (nuint)(oend - op), ip, chunkSize); + nuint cSize = ZSTD_compressContinue_public( + cctx, + op, + (nuint)(oend - op), + ip, + chunkSize + ); if (ERR_isError(cSize)) { SynchronizationWrapper.Enter(&job->job_mutex); @@ -605,8 +733,26 @@ namespace ZstdSharp.Unsafe if (((uint)(nbChunks > 0 ? 1 : 0) | job->lastJob) != 0) { nuint lastBlockSize1 = job->src.size & chunkSize - 1; - nuint lastBlockSize = lastBlockSize1 == 0 && job->src.size >= chunkSize ? chunkSize : lastBlockSize1; - nuint cSize = job->lastJob != 0 ? ZSTD_compressEnd_public(cctx, op, (nuint)(oend - op), ip, lastBlockSize) : ZSTD_compressContinue_public(cctx, op, (nuint)(oend - op), ip, lastBlockSize); + nuint lastBlockSize = + lastBlockSize1 == 0 && job->src.size >= chunkSize + ? chunkSize + : lastBlockSize1; + nuint cSize = + job->lastJob != 0 + ? ZSTD_compressEnd_public( + cctx, + op, + (nuint)(oend - op), + ip, + lastBlockSize + ) + : ZSTD_compressContinue_public( + cctx, + op, + (nuint)(oend - op), + ip, + lastBlockSize + ); if (ERR_isError(cSize)) { SynchronizationWrapper.Enter(&job->job_mutex); @@ -627,7 +773,7 @@ namespace ZstdSharp.Unsafe #endif ZSTD_CCtx_trace(cctx, 0); - _endJob: + _endJob: ZSTDMT_serialState_ensureFinished(job->serial, job->jobID, job->cSize); ZSTDMT_releaseSeq(job->seqPool, rawSeqStore); ZSTDMT_releaseCCtx(job->cctxPool, cctx); @@ -640,8 +786,17 @@ namespace ZstdSharp.Unsafe SynchronizationWrapper.Exit(&job->job_mutex); } - private static readonly RoundBuff_t kNullRoundBuff = new RoundBuff_t(buffer: null, capacity: 0, pos: 0); - private static void ZSTDMT_freeJobsTable(ZSTDMT_jobDescription* jobTable, uint nbJobs, ZSTD_customMem cMem) + private static readonly RoundBuff_t kNullRoundBuff = new RoundBuff_t( + buffer: null, + capacity: 0, + pos: 0 + ); + + private static void ZSTDMT_freeJobsTable( + ZSTDMT_jobDescription* jobTable, + uint nbJobs, + ZSTD_customMem cMem + ) { uint jobNb; if (jobTable == null) @@ -657,12 +812,18 @@ namespace ZstdSharp.Unsafe /* ZSTDMT_allocJobsTable() * allocate and init a job table. * update *nbJobsPtr to next power of 2 value, as size of table */ - private static ZSTDMT_jobDescription* ZSTDMT_createJobsTable(uint* nbJobsPtr, ZSTD_customMem cMem) + private static ZSTDMT_jobDescription* ZSTDMT_createJobsTable( + uint* nbJobsPtr, + ZSTD_customMem cMem + ) { uint nbJobsLog2 = ZSTD_highbit32(*nbJobsPtr) + 1; uint nbJobs = (uint)(1 << (int)nbJobsLog2); uint jobNb; - ZSTDMT_jobDescription* jobTable = (ZSTDMT_jobDescription*)ZSTD_customCalloc(nbJobs * (uint)sizeof(ZSTDMT_jobDescription), cMem); + ZSTDMT_jobDescription* jobTable = (ZSTDMT_jobDescription*)ZSTD_customCalloc( + nbJobs * (uint)sizeof(ZSTDMT_jobDescription), + cMem + ); int initError = 0; if (jobTable == null) return null; @@ -702,20 +863,34 @@ namespace ZstdSharp.Unsafe /* ZSTDMT_CCtxParam_setNbWorkers(): * Internal use only */ - private static nuint ZSTDMT_CCtxParam_setNbWorkers(ZSTD_CCtx_params_s* @params, uint nbWorkers) + private static nuint ZSTDMT_CCtxParam_setNbWorkers( + ZSTD_CCtx_params_s* @params, + uint nbWorkers + ) { - return ZSTD_CCtxParams_setParameter(@params, ZSTD_cParameter.ZSTD_c_nbWorkers, (int)nbWorkers); + return ZSTD_CCtxParams_setParameter( + @params, + ZSTD_cParameter.ZSTD_c_nbWorkers, + (int)nbWorkers + ); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static ZSTDMT_CCtx_s* ZSTDMT_createCCtx_advanced_internal(uint nbWorkers, ZSTD_customMem cMem, void* pool) + private static ZSTDMT_CCtx_s* ZSTDMT_createCCtx_advanced_internal( + uint nbWorkers, + ZSTD_customMem cMem, + void* pool + ) { ZSTDMT_CCtx_s* mtctx; uint nbJobs = nbWorkers + 2; int initError; if (nbWorkers < 1) return null; - nbWorkers = nbWorkers < (uint)(sizeof(void*) == 4 ? 64 : 256) ? nbWorkers : (uint)(sizeof(void*) == 4 ? 64 : 256); + nbWorkers = + nbWorkers < (uint)(sizeof(void*) == 4 ? 64 : 256) + ? nbWorkers + : (uint)(sizeof(void*) == 4 ? 64 : 256); if (((cMem.customAlloc != null ? 1 : 0) ^ (cMem.customFree != null ? 1 : 0)) != 0) return null; mtctx = (ZSTDMT_CCtx_s*)ZSTD_customCalloc((nuint)sizeof(ZSTDMT_CCtx_s), cMem); @@ -744,7 +919,19 @@ namespace ZstdSharp.Unsafe mtctx->seqPool = ZSTDMT_createSeqPool(nbWorkers, cMem); initError = ZSTDMT_serialState_init(&mtctx->serial); mtctx->roundBuff = kNullRoundBuff; - if (((mtctx->factory == null || mtctx->jobs == null || mtctx->bufPool == null || mtctx->cctxPool == null || mtctx->seqPool == null ? 1 : 0) | initError) != 0) + if ( + ( + ( + mtctx->factory == null + || mtctx->jobs == null + || mtctx->bufPool == null + || mtctx->cctxPool == null + || mtctx->seqPool == null + ? 1 + : 0 + ) | initError + ) != 0 + ) { ZSTDMT_freeCCtx(mtctx); return null; @@ -754,7 +941,11 @@ namespace ZstdSharp.Unsafe } /* Requires ZSTD_MULTITHREAD to be defined during compilation, otherwise it will return NULL. */ - private static ZSTDMT_CCtx_s* ZSTDMT_createCCtx_advanced(uint nbWorkers, ZSTD_customMem cMem, void* pool) + private static ZSTDMT_CCtx_s* ZSTDMT_createCCtx_advanced( + uint nbWorkers, + ZSTD_customMem cMem, + void* pool + ) { return ZSTDMT_createCCtx_advanced_internal(nbWorkers, cMem, pool); } @@ -773,7 +964,7 @@ namespace ZstdSharp.Unsafe mtctx->jobs[jobID] = new ZSTDMT_jobDescription { job_mutex = mutex, - job_cond = cond + job_cond = cond, }; } @@ -821,7 +1012,14 @@ namespace ZstdSharp.Unsafe { if (mtctx == null) return 0; - return (nuint)sizeof(ZSTDMT_CCtx_s) + POOL_sizeof(mtctx->factory) + ZSTDMT_sizeof_bufferPool(mtctx->bufPool) + (mtctx->jobIDMask + 1) * (uint)sizeof(ZSTDMT_jobDescription) + ZSTDMT_sizeof_CCtxPool(mtctx->cctxPool) + ZSTDMT_sizeof_seqPool(mtctx->seqPool) + ZSTD_sizeof_CDict(mtctx->cdictLocal) + mtctx->roundBuff.capacity; + return (nuint)sizeof(ZSTDMT_CCtx_s) + + POOL_sizeof(mtctx->factory) + + ZSTDMT_sizeof_bufferPool(mtctx->bufPool) + + (mtctx->jobIDMask + 1) * (uint)sizeof(ZSTDMT_jobDescription) + + ZSTDMT_sizeof_CCtxPool(mtctx->cctxPool) + + ZSTDMT_sizeof_seqPool(mtctx->seqPool) + + ZSTD_sizeof_CDict(mtctx->cdictLocal) + + mtctx->roundBuff.capacity; } /* ZSTDMT_resize() : @@ -854,14 +1052,22 @@ namespace ZstdSharp.Unsafe /*! ZSTDMT_updateCParams_whileCompressing() : * Updates a selected set of compression parameters, remaining compatible with currently active frame. * New parameters will be applied to next compression job. */ - private static void ZSTDMT_updateCParams_whileCompressing(ZSTDMT_CCtx_s* mtctx, ZSTD_CCtx_params_s* cctxParams) + private static void ZSTDMT_updateCParams_whileCompressing( + ZSTDMT_CCtx_s* mtctx, + ZSTD_CCtx_params_s* cctxParams + ) { /* Do not modify windowLog while compressing */ uint saved_wlog = mtctx->@params.cParams.windowLog; int compressionLevel = cctxParams->compressionLevel; mtctx->@params.compressionLevel = compressionLevel; { - ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams(cctxParams, unchecked(0UL - 1), 0, ZSTD_CParamMode_e.ZSTD_cpm_noAttachDict); + ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams( + cctxParams, + unchecked(0UL - 1), + 0, + ZSTD_CParamMode_e.ZSTD_cpm_noAttachDict + ); cParams.windowLog = saved_wlog; mtctx->@params.cParams = cParams; } @@ -953,7 +1159,10 @@ namespace ZstdSharp.Unsafe uint jobLog; if (@params->ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable) { - jobLog = 21 > ZSTD_cycleLog(@params->cParams.chainLog, @params->cParams.strategy) + 3 ? 21 : ZSTD_cycleLog(@params->cParams.chainLog, @params->cParams.strategy) + 3; + jobLog = + 21 > ZSTD_cycleLog(@params->cParams.chainLog, @params->cParams.strategy) + 3 + ? 21 + : ZSTD_cycleLog(@params->cParams.chainLog, @params->cParams.strategy) + 3; } else { @@ -997,11 +1206,19 @@ namespace ZstdSharp.Unsafe private static nuint ZSTDMT_computeOverlapSize(ZSTD_CCtx_params_s* @params) { int overlapRLog = 9 - ZSTDMT_overlapLog(@params->overlapLog, @params->cParams.strategy); - int ovLog = (int)(overlapRLog >= 8 ? 0 : @params->cParams.windowLog - (uint)overlapRLog); + int ovLog = (int)( + overlapRLog >= 8 ? 0 : @params->cParams.windowLog - (uint)overlapRLog + ); assert(0 <= overlapRLog && overlapRLog <= 8); if (@params->ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable) { - ovLog = (int)((@params->cParams.windowLog < ZSTDMT_computeTargetJobLog(@params) - 2 ? @params->cParams.windowLog : ZSTDMT_computeTargetJobLog(@params) - 2) - (uint)overlapRLog); + ovLog = (int)( + ( + @params->cParams.windowLog < ZSTDMT_computeTargetJobLog(@params) - 2 + ? @params->cParams.windowLog + : ZSTDMT_computeTargetJobLog(@params) - 2 + ) - (uint)overlapRLog + ); } assert(0 <= ovLog && ovLog <= (sizeof(nuint) == 4 ? 30 : 31)); @@ -1011,7 +1228,15 @@ namespace ZstdSharp.Unsafe /* ====================================== */ /* ======= Streaming API ======= */ /* ====================================== */ - private static nuint ZSTDMT_initCStream_internal(ZSTDMT_CCtx_s* mtctx, void* dict, nuint dictSize, ZSTD_dictContentType_e dictContentType, ZSTD_CDict_s* cdict, ZSTD_CCtx_params_s @params, ulong pledgedSrcSize) + private static nuint ZSTDMT_initCStream_internal( + ZSTDMT_CCtx_s* mtctx, + void* dict, + nuint dictSize, + ZSTD_dictContentType_e dictContentType, + ZSTD_CDict_s* cdict, + ZSTD_CCtx_params_s @params, + ulong pledgedSrcSize + ) { assert(!ERR_isError(ZSTD_checkCParams(@params.cParams))); assert(!(dict != null && cdict != null)); @@ -1041,7 +1266,14 @@ namespace ZstdSharp.Unsafe ZSTD_freeCDict(mtctx->cdictLocal); if (dict != null) { - mtctx->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy, dictContentType, @params.cParams, mtctx->cMem); + mtctx->cdictLocal = ZSTD_createCDict_advanced( + dict, + dictSize, + ZSTD_dictLoadMethod_e.ZSTD_dlm_byCopy, + dictContentType, + @params.cParams, + mtctx->cMem + ); mtctx->cdict = mtctx->cdictLocal; if (mtctx->cdictLocal == null) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); @@ -1056,10 +1288,14 @@ namespace ZstdSharp.Unsafe mtctx->targetSectionSize = @params.jobSize; if (mtctx->targetSectionSize == 0) { - mtctx->targetSectionSize = (nuint)(1UL << (int)ZSTDMT_computeTargetJobLog(&@params)); + mtctx->targetSectionSize = (nuint)( + 1UL << (int)ZSTDMT_computeTargetJobLog(&@params) + ); } - assert(mtctx->targetSectionSize <= (nuint)(MEM_32bits ? 512 * (1 << 20) : 1024 * (1 << 20))); + assert( + mtctx->targetSectionSize <= (nuint)(MEM_32bits ? 512 * (1 << 20) : 1024 * (1 << 20)) + ); if (@params.rsyncable != 0) { /* Aim for the targetsectionSize as the average job size. */ @@ -1077,7 +1313,10 @@ namespace ZstdSharp.Unsafe ZSTDMT_setBufferSize(mtctx->bufPool, ZSTD_compressBound(mtctx->targetSectionSize)); { /* If ldm is enabled we need windowSize space. */ - nuint windowSize = mtctx->@params.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable ? 1U << (int)mtctx->@params.cParams.windowLog : 0; + nuint windowSize = + mtctx->@params.ldmParams.enableLdm == ZSTD_paramSwitch_e.ZSTD_ps_enable + ? 1U << (int)mtctx->@params.cParams.windowLog + : 0; /* Two buffers of slack, plus extra space for the overlap * This is the minimum slack that LDM works with. One extra because * flush might waste up to targetSectionSize-1 bytes. Another extra @@ -1087,9 +1326,12 @@ namespace ZstdSharp.Unsafe nuint nbSlackBuffers = (nuint)(2 + (mtctx->targetPrefixSize > 0 ? 1 : 0)); nuint slackSize = mtctx->targetSectionSize * nbSlackBuffers; /* Compute the total size, and always have enough slack */ - nuint nbWorkers = (nuint)(mtctx->@params.nbWorkers > 1 ? mtctx->@params.nbWorkers : 1); + nuint nbWorkers = (nuint)( + mtctx->@params.nbWorkers > 1 ? mtctx->@params.nbWorkers : 1 + ); nuint sectionsSize = mtctx->targetSectionSize * nbWorkers; - nuint capacity = (windowSize > sectionsSize ? windowSize : sectionsSize) + slackSize; + nuint capacity = + (windowSize > sectionsSize ? windowSize : sectionsSize) + slackSize; if (mtctx->roundBuff.capacity < capacity) { if (mtctx->roundBuff.buffer != null) @@ -1098,7 +1340,9 @@ namespace ZstdSharp.Unsafe if (mtctx->roundBuff.buffer == null) { mtctx->roundBuff.capacity = 0; - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); } mtctx->roundBuff.capacity = capacity; @@ -1127,10 +1371,19 @@ namespace ZstdSharp.Unsafe } else { - mtctx->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, dictContentType, @params.cParams, mtctx->cMem); + mtctx->cdictLocal = ZSTD_createCDict_advanced( + dict, + dictSize, + ZSTD_dictLoadMethod_e.ZSTD_dlm_byRef, + dictContentType, + @params.cParams, + mtctx->cMem + ); mtctx->cdict = mtctx->cdictLocal; if (mtctx->cdictLocal == null) - return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); + return unchecked( + (nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation) + ); } } else @@ -1138,7 +1391,17 @@ namespace ZstdSharp.Unsafe mtctx->cdict = cdict; } - if (ZSTDMT_serialState_reset(&mtctx->serial, mtctx->seqPool, @params, mtctx->targetSectionSize, dict, dictSize, dictContentType) != 0) + if ( + ZSTDMT_serialState_reset( + &mtctx->serial, + mtctx->seqPool, + @params, + mtctx->targetSectionSize, + dict, + dictSize, + dictContentType + ) != 0 + ) return unchecked((nuint)(-(int)ZSTD_ErrorCode.ZSTD_error_memory_allocation)); return 0; } @@ -1168,13 +1431,19 @@ namespace ZstdSharp.Unsafe assert(job->consumed == 0); } - private static nuint ZSTDMT_createCompressionJob(ZSTDMT_CCtx_s* mtctx, nuint srcSize, ZSTD_EndDirective endOp) + private static nuint ZSTDMT_createCompressionJob( + ZSTDMT_CCtx_s* mtctx, + nuint srcSize, + ZSTD_EndDirective endOp + ) { uint jobID = mtctx->nextJobID & mtctx->jobIDMask; int endFrame = endOp == ZSTD_EndDirective.ZSTD_e_end ? 1 : 0; if (mtctx->nextJobID > mtctx->doneJobID + mtctx->jobIDMask) { - assert((mtctx->nextJobID & mtctx->jobIDMask) == (mtctx->doneJobID & mtctx->jobIDMask)); + assert( + (mtctx->nextJobID & mtctx->jobIDMask) == (mtctx->doneJobID & mtctx->jobIDMask) + ); return 0; } @@ -1198,14 +1467,20 @@ namespace ZstdSharp.Unsafe mtctx->jobs[jobID].jobID = mtctx->nextJobID; mtctx->jobs[jobID].firstJob = mtctx->nextJobID == 0 ? 1U : 0U; mtctx->jobs[jobID].lastJob = (uint)endFrame; - mtctx->jobs[jobID].frameChecksumNeeded = mtctx->@params.fParams.checksumFlag != 0 && endFrame != 0 && mtctx->nextJobID > 0 ? 1U : 0U; + mtctx->jobs[jobID].frameChecksumNeeded = + mtctx->@params.fParams.checksumFlag != 0 + && endFrame != 0 + && mtctx->nextJobID > 0 + ? 1U + : 0U; mtctx->jobs[jobID].dstFlushed = 0; mtctx->roundBuff.pos += srcSize; mtctx->inBuff.buffer = g_nullBuffer; mtctx->inBuff.filled = 0; if (endFrame == 0) { - nuint newPrefixSize = srcSize < mtctx->targetPrefixSize ? srcSize : mtctx->targetPrefixSize; + nuint newPrefixSize = + srcSize < mtctx->targetPrefixSize ? srcSize : mtctx->targetPrefixSize; mtctx->inBuff.prefix.start = src + srcSize - newPrefixSize; mtctx->inBuff.prefix.size = newPrefixSize; } @@ -1228,7 +1503,13 @@ namespace ZstdSharp.Unsafe } } - if (POOL_tryAdd(mtctx->factory, (delegate* managed)(&ZSTDMT_compressionJob), &mtctx->jobs[jobID]) != 0) + if ( + POOL_tryAdd( + mtctx->factory, + (delegate* managed)(&ZSTDMT_compressionJob), + &mtctx->jobs[jobID] + ) != 0 + ) { mtctx->nextJobID++; mtctx->jobReady = 0; @@ -1247,7 +1528,12 @@ namespace ZstdSharp.Unsafe * `output` : `pos` will be updated with amount of data flushed . * `blockToFlush` : if >0, the function will block and wait if there is no data available to flush . * @return : amount of data remaining within internal buffer, 0 if no more, 1 if unknown but > 0, or an error code */ - private static nuint ZSTDMT_flushProduced(ZSTDMT_CCtx_s* mtctx, ZSTD_outBuffer_s* output, uint blockToFlush, ZSTD_EndDirective end) + private static nuint ZSTDMT_flushProduced( + ZSTDMT_CCtx_s* mtctx, + ZSTD_outBuffer_s* output, + uint blockToFlush, + ZSTD_EndDirective end + ) { uint wJobID = mtctx->doneJobID & mtctx->jobIDMask; assert(output->size >= output->pos); @@ -1285,7 +1571,10 @@ namespace ZstdSharp.Unsafe if (srcConsumed == srcSize && mtctx->jobs[wJobID].frameChecksumNeeded != 0) { uint checksum = (uint)ZSTD_XXH64_digest(&mtctx->serial.xxhState); - MEM_writeLE32((sbyte*)mtctx->jobs[wJobID].dstBuff.start + mtctx->jobs[wJobID].cSize, checksum); + MEM_writeLE32( + (sbyte*)mtctx->jobs[wJobID].dstBuff.start + mtctx->jobs[wJobID].cSize, + checksum + ); cSize += 4; mtctx->jobs[wJobID].cSize += 4; mtctx->jobs[wJobID].frameChecksumNeeded = 0; @@ -1293,13 +1582,21 @@ namespace ZstdSharp.Unsafe if (cSize > 0) { - nuint toFlush = cSize - mtctx->jobs[wJobID].dstFlushed < output->size - output->pos ? cSize - mtctx->jobs[wJobID].dstFlushed : output->size - output->pos; + nuint toFlush = + cSize - mtctx->jobs[wJobID].dstFlushed < output->size - output->pos + ? cSize - mtctx->jobs[wJobID].dstFlushed + : output->size - output->pos; assert(mtctx->doneJobID < mtctx->nextJobID); assert(cSize >= mtctx->jobs[wJobID].dstFlushed); assert(mtctx->jobs[wJobID].dstBuff.start != null); if (toFlush > 0) { - memcpy((sbyte*)output->dst + output->pos, (sbyte*)mtctx->jobs[wJobID].dstBuff.start + mtctx->jobs[wJobID].dstFlushed, (uint)toFlush); + memcpy( + (sbyte*)output->dst + output->pos, + (sbyte*)mtctx->jobs[wJobID].dstBuff.start + + mtctx->jobs[wJobID].dstFlushed, + (uint)toFlush + ); } output->pos += toFlush; @@ -1397,7 +1694,11 @@ namespace ZstdSharp.Unsafe extDict.size = window.dictLimit - window.lowLimit; prefix.start = window.@base + window.dictLimit; prefix.size = (nuint)(window.nextSrc - (window.@base + window.dictLimit)); - return ZSTDMT_isOverlapped(buffer, extDict) != 0 || ZSTDMT_isOverlapped(buffer, prefix) != 0 ? 1 : 0; + return + ZSTDMT_isOverlapped(buffer, extDict) != 0 + || ZSTDMT_isOverlapped(buffer, prefix) != 0 + ? 1 + : 0; } private static void ZSTDMT_waitForLdmComplete(ZSTDMT_CCtx_s* mtctx, buffer_s buffer) @@ -1469,7 +1770,10 @@ namespace ZstdSharp.Unsafe * Otherwise, we will load as many bytes as possible and instruct the caller * to continue as normal. */ - private static SyncPoint findSynchronizationPoint(ZSTDMT_CCtx_s* mtctx, ZSTD_inBuffer_s input) + private static SyncPoint findSynchronizationPoint( + ZSTDMT_CCtx_s* mtctx, + ZSTD_inBuffer_s input + ) { byte* istart = (byte*)input.src + input.pos; ulong primePower = mtctx->rsync.primePower; @@ -1478,7 +1782,10 @@ namespace ZstdSharp.Unsafe ulong hash; byte* prev; nuint pos; - syncPoint.toLoad = input.size - input.pos < mtctx->targetSectionSize - mtctx->inBuff.filled ? input.size - input.pos : mtctx->targetSectionSize - mtctx->inBuff.filled; + syncPoint.toLoad = + input.size - input.pos < mtctx->targetSectionSize - mtctx->inBuff.filled + ? input.size - input.pos + : mtctx->targetSectionSize - mtctx->inBuff.filled; syncPoint.flush = 0; if (mtctx->@params.rsyncable == 0) return syncPoint; @@ -1549,7 +1856,12 @@ namespace ZstdSharp.Unsafe * internal use only - exposed to be invoked from zstd_compress.c * assumption : output and input are valid (pos <= size) * @return : minimum amount of data remaining to flush, 0 if none */ - private static nuint ZSTDMT_compressStream_generic(ZSTDMT_CCtx_s* mtctx, ZSTD_outBuffer_s* output, ZSTD_inBuffer_s* input, ZSTD_EndDirective endOp) + private static nuint ZSTDMT_compressStream_generic( + ZSTDMT_CCtx_s* mtctx, + ZSTD_outBuffer_s* output, + ZSTD_inBuffer_s* input, + ZSTD_EndDirective endOp + ) { uint forwardInputProgress = 0; assert(output->pos <= output->size); @@ -1579,7 +1891,11 @@ namespace ZstdSharp.Unsafe } assert(mtctx->inBuff.buffer.capacity >= mtctx->targetSectionSize); - memcpy((sbyte*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled, (sbyte*)input->src + input->pos, (uint)syncPoint.toLoad); + memcpy( + (sbyte*)mtctx->inBuff.buffer.start + mtctx->inBuff.filled, + (sbyte*)input->src + input->pos, + (uint)syncPoint.toLoad + ); input->pos += syncPoint.toLoad; mtctx->inBuff.filled += syncPoint.toLoad; forwardInputProgress = syncPoint.toLoad > 0 ? 1U : 0U; @@ -1588,11 +1904,20 @@ namespace ZstdSharp.Unsafe if (input->pos < input->size && endOp == ZSTD_EndDirective.ZSTD_e_end) { - assert(mtctx->inBuff.filled == 0 || mtctx->inBuff.filled == mtctx->targetSectionSize || mtctx->@params.rsyncable != 0); + assert( + mtctx->inBuff.filled == 0 + || mtctx->inBuff.filled == mtctx->targetSectionSize + || mtctx->@params.rsyncable != 0 + ); endOp = ZSTD_EndDirective.ZSTD_e_flush; } - if (mtctx->jobReady != 0 || mtctx->inBuff.filled >= mtctx->targetSectionSize || endOp != ZSTD_EndDirective.ZSTD_e_continue && mtctx->inBuff.filled > 0 || endOp == ZSTD_EndDirective.ZSTD_e_end && mtctx->frameEnded == 0) + if ( + mtctx->jobReady != 0 + || mtctx->inBuff.filled >= mtctx->targetSectionSize + || endOp != ZSTD_EndDirective.ZSTD_e_continue && mtctx->inBuff.filled > 0 + || endOp == ZSTD_EndDirective.ZSTD_e_end && mtctx->frameEnded == 0 + ) { nuint jobSize = mtctx->inBuff.filled; assert(mtctx->inBuff.filled <= mtctx->targetSectionSize); @@ -1607,7 +1932,12 @@ namespace ZstdSharp.Unsafe { /* block if there was no forward input progress */ - nuint remainingToFlush = ZSTDMT_flushProduced(mtctx, output, forwardInputProgress == 0 ? 1U : 0U, endOp); + nuint remainingToFlush = ZSTDMT_flushProduced( + mtctx, + output, + forwardInputProgress == 0 ? 1U : 0U, + endOp + ); if (input->pos < input->size) return remainingToFlush > 1 ? remainingToFlush : 1; return remainingToFlush; diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/_wksps_e__Union.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/_wksps_e__Union.cs index d13d9b12..c0da19f6 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/_wksps_e__Union.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/_wksps_e__Union.cs @@ -7,9 +7,11 @@ namespace ZstdSharp.Unsafe { [FieldOffset(0)] public HUF_buildCTable_wksp_tables buildCTable_wksp; + [FieldOffset(0)] public HUF_WriteCTableWksp writeCTable_wksp; + [FieldOffset(0)] public fixed uint hist_wksp[1024]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/algo_time_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/algo_time_t.cs index e54af207..5ea98f18 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/algo_time_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/algo_time_t.cs @@ -4,10 +4,11 @@ namespace ZstdSharp.Unsafe { public uint tableTime; public uint decode256Time; + public algo_time_t(uint tableTime, uint decode256Time) { this.tableTime = tableTime; this.decode256Time = decode256Time; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/base_directive_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/base_directive_e.cs index a77fea3b..1e95f5e0 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/base_directive_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/base_directive_e.cs @@ -3,6 +3,6 @@ namespace ZstdSharp.Unsafe public enum base_directive_e { base_0possible = 0, - base_1guaranteed = 1 + base_1guaranteed = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/blockProperties_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/blockProperties_t.cs index 058ddffd..4413c5d4 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/blockProperties_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/blockProperties_t.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public uint lastBlock; public uint origSize; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/blockType_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/blockType_e.cs index baa27390..44f7af6b 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/blockType_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/blockType_e.cs @@ -5,6 +5,6 @@ namespace ZstdSharp.Unsafe bt_raw, bt_rle, bt_compressed, - bt_reserved + bt_reserved, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/buffer_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/buffer_s.cs index 093a0b8d..332edb1f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/buffer_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/buffer_s.cs @@ -6,10 +6,11 @@ namespace ZstdSharp.Unsafe { public void* start; public nuint capacity; + public buffer_s(void* start, nuint capacity) { this.start = start; this.capacity = capacity; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/dictItem.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/dictItem.cs index a4b1abb3..b18b0c71 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/dictItem.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/dictItem.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public uint length; public uint savings; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/inBuff_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/inBuff_t.cs index 8fc65168..1c9e8524 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/inBuff_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/inBuff_t.cs @@ -10,4 +10,4 @@ namespace ZstdSharp.Unsafe public buffer_s buffer; public nuint filled; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmEntry_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmEntry_t.cs index 391fa98e..8b308fd5 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmEntry_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmEntry_t.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public uint offset; public uint checksum; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmMatchCandidate_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmMatchCandidate_t.cs index f3e41edd..a5958c31 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmMatchCandidate_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmMatchCandidate_t.cs @@ -7,4 +7,4 @@ namespace ZstdSharp.Unsafe public uint checksum; public ldmEntry_t* bucket; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmParams_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmParams_t.cs index 253e3bfe..4558d0c5 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmParams_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmParams_t.cs @@ -4,15 +4,20 @@ namespace ZstdSharp.Unsafe { /* ZSTD_ps_enable to enable LDM. ZSTD_ps_auto by default */ public ZSTD_paramSwitch_e 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; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmRollingHashState_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmRollingHashState_t.cs index 81038007..d4fa28ec 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmRollingHashState_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmRollingHashState_t.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public ulong rolling; public ulong stopMask; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmState_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmState_t.cs index a0244193..db4484e8 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmState_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/ldmState_t.cs @@ -8,10 +8,12 @@ namespace ZstdSharp.Unsafe 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; + #if NET8_0_OR_GREATER [InlineArray(64)] public unsafe struct _splitIndices_e__FixedBuffer @@ -166,4 +168,4 @@ namespace ZstdSharp.Unsafe } #endif } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/nodeElt_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/nodeElt_s.cs index 3e30dffc..6ec0c803 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/nodeElt_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/nodeElt_s.cs @@ -10,4 +10,4 @@ namespace ZstdSharp.Unsafe public byte @byte; public byte nbBits; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/offsetCount_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/offsetCount_t.cs index 7c652776..a29f87e2 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/offsetCount_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/offsetCount_t.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public uint offset; public uint count; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/optState_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/optState_t.cs index 5f08de1a..b88e4d55 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/optState_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/optState_t.cs @@ -4,36 +4,51 @@ namespace ZstdSharp.Unsafe { /* 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_SIZE */ public ZSTD_match_t* matchTable; + /* All positions tracked by optimal parser, of size ZSTD_OPT_SIZE */ 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_paramSwitch_e literalCompressionMode; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/rankPos.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/rankPos.cs index f78f027d..874302f6 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/rankPos.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/rankPos.cs @@ -5,4 +5,4 @@ namespace ZstdSharp.Unsafe public ushort @base; public ushort curr; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/rankValCol_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/rankValCol_t.cs index d6c4fb5d..be14d662 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/rankValCol_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/rankValCol_t.cs @@ -4,4 +4,4 @@ namespace ZstdSharp.Unsafe { public fixed uint Body[13]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/rawSeq.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/rawSeq.cs index adefb262..1161bd21 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/rawSeq.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/rawSeq.cs @@ -4,9 +4,11 @@ namespace ZstdSharp.Unsafe { /* Offset of sequence */ public uint offset; + /* Length of literals prior to match */ public uint litLength; + /* Raw length of match */ public uint matchLength; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/repcodes_s.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/repcodes_s.cs index c63ef35b..d0e18b12 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/repcodes_s.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/repcodes_s.cs @@ -4,4 +4,4 @@ namespace ZstdSharp.Unsafe { public fixed uint rep[3]; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/searchMethod_e.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/searchMethod_e.cs index fb38e0f6..6823088c 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/searchMethod_e.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/searchMethod_e.cs @@ -4,6 +4,6 @@ namespace ZstdSharp.Unsafe { search_hashChain = 0, search_binaryTree = 1, - search_rowHash = 2 + search_rowHash = 2, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/seqState_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/seqState_t.cs index 2743a146..5c7496ec 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/seqState_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/seqState_t.cs @@ -7,6 +7,7 @@ namespace ZstdSharp.Unsafe public ZSTD_fseState stateOffb; public ZSTD_fseState stateML; public _prevOffset_e__FixedBuffer prevOffset; + public unsafe struct _prevOffset_e__FixedBuffer { public nuint e0; @@ -14,4 +15,4 @@ namespace ZstdSharp.Unsafe public nuint e2; } } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/seqStoreSplits.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/seqStoreSplits.cs index b4b7a4d4..b227d69f 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/seqStoreSplits.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/seqStoreSplits.cs @@ -5,7 +5,8 @@ namespace ZstdSharp.Unsafe { /* Array of split indices */ public uint* splitLocations; + /* The current index within splitLocations being worked on */ public nuint idx; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/seq_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/seq_t.cs index d286898c..5dac7aad 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/seq_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/seq_t.cs @@ -6,4 +6,4 @@ namespace ZstdSharp.Unsafe public nuint matchLength; public nuint offset; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/sortedSymbol_t.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/sortedSymbol_t.cs index facf809c..6c320a04 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/sortedSymbol_t.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/sortedSymbol_t.cs @@ -4,4 +4,4 @@ namespace ZstdSharp.Unsafe { public byte symbol; } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/Unsafe/streaming_operation.cs b/src/SharpCompress/Compressors/ZStandard/Unsafe/streaming_operation.cs index fae3973f..db838c61 100644 --- a/src/SharpCompress/Compressors/ZStandard/Unsafe/streaming_operation.cs +++ b/src/SharpCompress/Compressors/ZStandard/Unsafe/streaming_operation.cs @@ -4,6 +4,6 @@ namespace ZstdSharp.Unsafe public enum streaming_operation { not_streaming = 0, - is_streaming = 1 + is_streaming = 1, } -} \ No newline at end of file +} diff --git a/src/SharpCompress/Compressors/ZStandard/UnsafeHelper.cs b/src/SharpCompress/Compressors/ZStandard/UnsafeHelper.cs index 09aef626..3a3722da 100644 --- a/src/SharpCompress/Compressors/ZStandard/UnsafeHelper.cs +++ b/src/SharpCompress/Compressors/ZStandard/UnsafeHelper.cs @@ -3,7 +3,6 @@ using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; - namespace ZstdSharp { public static unsafe class UnsafeHelper @@ -18,9 +17,9 @@ namespace ZstdSharp public static void* malloc(ulong size) { #if NET6_0_OR_GREATER - var ptr = NativeMemory.Alloc((nuint) size); + var ptr = NativeMemory.Alloc((nuint)size); #else - var ptr = (void*) Marshal.AllocHGlobal((nint) size); + var ptr = (void*)Marshal.AllocHGlobal((nint)size); #endif #if DEBUG return PoisonMemory(ptr, size); @@ -33,7 +32,7 @@ namespace ZstdSharp public static void* calloc(ulong num, ulong size) { #if NET6_0_OR_GREATER - return NativeMemory.AllocZeroed((nuint) num, (nuint) size); + return NativeMemory.AllocZeroed((nuint)num, (nuint)size); #else var total = num * size; assert(total <= uint.MaxValue); @@ -44,12 +43,12 @@ namespace ZstdSharp } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void memcpy(void* destination, void* source, uint size) - => System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(destination, source, size); + public static void memcpy(void* destination, void* source, uint size) => + System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(destination, source, size); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void memset(void* memPtr, byte val, uint size) - => System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(memPtr, val, size); + public static void memset(void* memPtr, byte val, uint size) => + System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(memPtr, val, size); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void free(void* ptr) @@ -57,12 +56,13 @@ namespace ZstdSharp #if NET6_0_OR_GREATER NativeMemory.Free(ptr); #else - Marshal.FreeHGlobal((IntPtr) ptr); + Marshal.FreeHGlobal((IntPtr)ptr); #endif } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T* GetArrayPointer(T[] array) where T : unmanaged + public static T* GetArrayPointer(T[] array) + where T : unmanaged { var size = (uint)(sizeof(T) * array.Length); #if NET9_0_OR_GREATER @@ -72,12 +72,17 @@ namespace ZstdSharp // loading the assembly in an unloadable AssemblyLoadContext. // While introduced in .NET 5, we call this only in .NET 9+, because // it's not implemented in the Mono runtime until then. - var destination = (T*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(UnsafeHelper), (int)size); + var destination = (T*) + RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(UnsafeHelper), (int)size); #else var destination = (T*)malloc(size); #endif fixed (void* source = &array[0]) - System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned(destination, source, size); + System.Runtime.CompilerServices.Unsafe.CopyBlockUnaligned( + destination, + source, + size + ); return destination; } @@ -91,16 +96,17 @@ namespace ZstdSharp } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void memmove(void* destination, void* source, ulong size) - => Buffer.MemoryCopy(source, destination, size, size); + public static void memmove(void* destination, void* source, ulong size) => + Buffer.MemoryCopy(source, destination, size, size); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int memcmp(void* buf1, void* buf2, ulong size) { assert(size <= int.MaxValue); var intSize = (int)size; - return new ReadOnlySpan(buf1, intSize) - .SequenceCompareTo(new ReadOnlySpan(buf2, intSize)); + return new ReadOnlySpan(buf1, intSize).SequenceCompareTo( + new ReadOnlySpan(buf2, intSize) + ); } } } diff --git a/src/SharpCompress/Compressors/ZStandard/ZstdException.cs b/src/SharpCompress/Compressors/ZStandard/ZstdException.cs index d16ce18b..1427bd84 100644 --- a/src/SharpCompress/Compressors/ZStandard/ZstdException.cs +++ b/src/SharpCompress/Compressors/ZStandard/ZstdException.cs @@ -4,12 +4,10 @@ using ZstdSharp.Unsafe; namespace ZstdSharp { - - public class ZstdException : SharpCompressException { - public ZstdException(ZSTD_ErrorCode code, string message) : base(message) - => Code = code; + public ZstdException(ZSTD_ErrorCode code, string message) + : base(message) => Code = code; public ZSTD_ErrorCode Code { get; } }