| | | 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 | | internal const int TargetBlockSize_Min = 1340; |
| | | 22 | | internal const int TargetBlockSize_Max = 131072; // (2^17) |
| | | 23 | | |
| | | 24 | | // Buffer sizes for Zstandard operations |
| | | 25 | | internal const int DefaultInternalBufferSize = (1 << 16) - 16; // 65520 bytes, similar to Brotli |
| | | 26 | | |
| | | 27 | | /// <summary>Checks if a Zstandard operation result indicates an error.</summary> |
| | 0 | 28 | | internal static bool IsError(nuint result) => Interop.Zstd.ZSTD_isError(result) != 0; |
| | | 29 | | internal static bool IsError(nuint result, out Interop.Zstd.ZSTD_error error) |
| | 0 | 30 | | { |
| | 0 | 31 | | if (IsError(result)) |
| | 0 | 32 | | { |
| | 0 | 33 | | error = (Interop.Zstd.ZSTD_error)result; |
| | 0 | 34 | | return true; |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | error = Interop.Zstd.ZSTD_error.no_error; |
| | 0 | 38 | | return false; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary>Gets the error message for a Zstandard error code.</summary> |
| | | 42 | | internal static string GetErrorMessage(Interop.Zstd.ZSTD_error error) |
| | 0 | 43 | | { |
| | 0 | 44 | | IntPtr errorNamePtr = Interop.Zstd.ZSTD_getErrorName((nuint)error); |
| | 0 | 45 | | return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(errorNamePtr) ?? $"Unknown error {error}"; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | internal static void ThrowIfError(nuint result) |
| | 0 | 49 | | { |
| | 0 | 50 | | if (IsError(result, out var error)) |
| | 0 | 51 | | { |
| | 0 | 52 | | Throw(error); |
| | | 53 | | } |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | [DoesNotReturn] |
| | | 57 | | internal static void Throw(Interop.Zstd.ZSTD_error error) |
| | 0 | 58 | | { |
| | 0 | 59 | | Debug.Assert(IsError((nuint)error)); |
| | 0 | 60 | | throw CreateExceptionForError(error); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | internal static Exception CreateExceptionForError(Interop.Zstd.ZSTD_error error) |
| | 0 | 64 | | { |
| | 0 | 65 | | Debug.Assert(IsError((nuint)error)); |
| | | 66 | | |
| | 0 | 67 | | switch (error) |
| | | 68 | | { |
| | | 69 | | case Interop.Zstd.ZSTD_error.frameParameter_windowTooLarge: |
| | 0 | 70 | | return new IOException(SR.ZstandardDecoder_WindowTooLarge); |
| | | 71 | | |
| | | 72 | | case Interop.Zstd.ZSTD_error.dictionary_wrong: |
| | 0 | 73 | | return new InvalidDataException(SR.ZstandardDecoder_DictionaryWrong); |
| | | 74 | | |
| | | 75 | | case Interop.Zstd.ZSTD_error.memory_allocation: |
| | 0 | 76 | | return new OutOfMemoryException(); |
| | | 77 | | |
| | | 78 | | case Interop.Zstd.ZSTD_error.stage_wrong: |
| | 0 | 79 | | return new InvalidOperationException(SR.ZstandardEncoderDecoder_InvalidState); |
| | | 80 | | |
| | | 81 | | default: |
| | 0 | 82 | | return new IOException(SR.Format(SR.Zstd_InternalError, GetErrorMessage(error))); |
| | | 83 | | } |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | internal static int GetQualityFromCompressionLevel(CompressionLevel compressionLevel) => |
| | 0 | 87 | | compressionLevel switch |
| | 0 | 88 | | { |
| | 0 | 89 | | // zstd supports negative quality levels, all negative levels map to the |
| | 0 | 90 | | // same behavior (essentially no compression). Quality 0 means "default" = 3. |
| | 0 | 91 | | // 1 is therefore the fastest compression level with some compression. |
| | 0 | 92 | | CompressionLevel.NoCompression => Quality_Min, |
| | 0 | 93 | | CompressionLevel.Fastest => 1, |
| | 0 | 94 | | CompressionLevel.Optimal => Quality_Default, |
| | 0 | 95 | | CompressionLevel.SmallestSize => Quality_Max, |
| | 0 | 96 | | _ => throw new ArgumentOutOfRangeException(nameof(compressionLevel), compressionLevel, SR.ArgumentOutOfR |
| | 0 | 97 | | }; |
| | | 98 | | } |
| | | 99 | | } |