| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace System.IO.Compression |
| | | 8 | | { |
| | | 9 | | internal static class ZstandardUtils |
| | | 10 | | { |
| | | 11 | | // Zstandard compression level constants from native library |
| | 0 | 12 | | internal static readonly int Quality_Min = Interop.Zstd.ZSTD_minCLevel(); |
| | 0 | 13 | | internal static readonly int Quality_Max = Interop.Zstd.ZSTD_maxCLevel(); |
| | 0 | 14 | | internal static readonly int Quality_Default = Interop.Zstd.ZSTD_defaultCLevel(); |
| | | 15 | | |
| | | 16 | | // Window size constraints based on Zstandard specification |
| | | 17 | | internal const int WindowLog_Min = 10; // 1KB window |
| | 0 | 18 | | internal static int WindowLog_Max => Environment.Is64BitProcess ? 31 : 30; // 1GB or 2GB window, depending on |
| | | 19 | | internal const int WindowLog_Default = 23; // 8MB window |
| | | 20 | | |
| | | 21 | | // Hash table and chain table constraints based on Zstandard specification |
| | | 22 | | internal const int HashLog_Min = 6; |
| | | 23 | | internal const int HashLog_Max = 30; |
| | | 24 | | |
| | | 25 | | internal const int ChainLog_Min = 6; |
| | 0 | 26 | | internal static int ChainLog_Max => Environment.Is64BitProcess ? 30 : 29; |
| | | 27 | | |
| | | 28 | | internal const int TargetBlockSize_Min = 1340; |
| | | 29 | | internal const int TargetBlockSize_Max = 131072; // (2^17) |
| | | 30 | | |
| | | 31 | | // Buffer sizes for Zstandard operations |
| | | 32 | | internal const int DefaultInternalBufferSize = (1 << 16) - 16; // 65520 bytes, similar to Brotli |
| | | 33 | | |
| | | 34 | | /// <summary>Checks if a Zstandard operation result indicates an error.</summary> |
| | 0 | 35 | | internal static bool IsError(nuint result) => Interop.Zstd.ZSTD_isError(result) != 0; |
| | | 36 | | internal static bool IsError(nuint result, out Interop.Zstd.ZSTD_error error) |
| | 0 | 37 | | { |
| | 0 | 38 | | if (IsError(result)) |
| | 0 | 39 | | { |
| | 0 | 40 | | error = (Interop.Zstd.ZSTD_error)result; |
| | 0 | 41 | | return true; |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | error = Interop.Zstd.ZSTD_error.no_error; |
| | 0 | 45 | | return false; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary>Gets the error message for a Zstandard error code.</summary> |
| | | 49 | | internal static string GetErrorMessage(Interop.Zstd.ZSTD_error error) |
| | 0 | 50 | | { |
| | 0 | 51 | | IntPtr errorNamePtr = Interop.Zstd.ZSTD_getErrorName((nuint)error); |
| | 0 | 52 | | return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(errorNamePtr) ?? $"Unknown error {error}"; |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | internal static void ThrowIfError(nuint result) |
| | 0 | 56 | | { |
| | 0 | 57 | | if (IsError(result, out var error)) |
| | 0 | 58 | | { |
| | 0 | 59 | | Throw(error); |
| | | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | [DoesNotReturn] |
| | | 64 | | internal static void Throw(Interop.Zstd.ZSTD_error error) |
| | 0 | 65 | | { |
| | 0 | 66 | | Debug.Assert(IsError((nuint)error)); |
| | 0 | 67 | | throw CreateExceptionForError(error); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | internal static Exception CreateExceptionForError(Interop.Zstd.ZSTD_error error) |
| | 0 | 71 | | { |
| | 0 | 72 | | Debug.Assert(IsError((nuint)error)); |
| | | 73 | | |
| | 0 | 74 | | switch (error) |
| | | 75 | | { |
| | | 76 | | case Interop.Zstd.ZSTD_error.frameParameter_windowTooLarge: |
| | 0 | 77 | | return new IOException(SR.ZstandardDecoder_WindowTooLarge); |
| | | 78 | | |
| | | 79 | | case Interop.Zstd.ZSTD_error.dictionary_wrong: |
| | 0 | 80 | | return new InvalidDataException(SR.ZstandardDecoder_DictionaryWrong); |
| | | 81 | | |
| | | 82 | | case Interop.Zstd.ZSTD_error.memory_allocation: |
| | 0 | 83 | | return new OutOfMemoryException(); |
| | | 84 | | |
| | | 85 | | case Interop.Zstd.ZSTD_error.stage_wrong: |
| | 0 | 86 | | return new InvalidOperationException(SR.ZstandardEncoderDecoder_InvalidState); |
| | | 87 | | |
| | | 88 | | default: |
| | 0 | 89 | | return new IOException(SR.Format(SR.Zstd_InternalError, GetErrorMessage(error))); |
| | | 90 | | } |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | internal static int GetQualityFromCompressionLevel(CompressionLevel compressionLevel) => |
| | 0 | 94 | | compressionLevel switch |
| | 0 | 95 | | { |
| | 0 | 96 | | // zstd supports negative quality levels, all negative levels map to the |
| | 0 | 97 | | // same behavior (essentially no compression). Quality 0 means "default" = 3. |
| | 0 | 98 | | // 1 is therefore the fastest compression level with some compression. |
| | 0 | 99 | | CompressionLevel.NoCompression => Quality_Min, |
| | 0 | 100 | | CompressionLevel.Fastest => 1, |
| | 0 | 101 | | CompressionLevel.Optimal => Quality_Default, |
| | 0 | 102 | | CompressionLevel.SmallestSize => Quality_Max, |
| | 0 | 103 | | _ => throw new ArgumentOutOfRangeException(nameof(compressionLevel), compressionLevel, SR.ArgumentOutOfR |
| | 0 | 104 | | }; |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |