| | | 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 | | namespace System.IO.Compression |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Specifies the compression format for zlib-based encoders. |
| | | 8 | | /// </summary> |
| | | 9 | | internal enum CompressionFormat |
| | | 10 | | { |
| | | 11 | | /// <summary>Raw deflate format (no header/trailer).</summary> |
| | | 12 | | Deflate, |
| | | 13 | | /// <summary>ZLib format (zlib header/trailer).</summary> |
| | | 14 | | ZLib, |
| | | 15 | | /// <summary>GZip format (gzip header/trailer).</summary> |
| | | 16 | | GZip |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | internal static class CompressionFormatHelper |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Resolves a windowLog value (8-15) to the windowBits parameter expected by zlib, |
| | | 23 | | /// based on the compression format. A windowLog of -1 is resolved to the default (15). |
| | | 24 | | /// </summary> |
| | | 25 | | /// <remarks> |
| | | 26 | | /// zlib-ng rejects windowBits 8 for raw deflate and gzip; classic zlib silently upgrades to 9. |
| | | 27 | | /// This method clamps to 9 for Deflate and GZip formats to match classic zlib behavior. |
| | | 28 | | /// </remarks> |
| | | 29 | | internal static int ResolveWindowBits(int windowLog, CompressionFormat format) |
| | 0 | 30 | | { |
| | 0 | 31 | | if (windowLog == -1) |
| | 0 | 32 | | { |
| | 0 | 33 | | windowLog = ZLibNative.DefaultWindowLog; |
| | 0 | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | if (format != CompressionFormat.ZLib) |
| | 0 | 37 | | { |
| | 0 | 38 | | windowLog = Math.Max(windowLog, 9); |
| | 0 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | return format switch |
| | 0 | 42 | | { |
| | 0 | 43 | | CompressionFormat.Deflate => -windowLog, |
| | 0 | 44 | | CompressionFormat.ZLib => windowLog, |
| | 0 | 45 | | CompressionFormat.GZip => windowLog + 16, |
| | 0 | 46 | | _ => throw new ArgumentOutOfRangeException(nameof(format)) |
| | 0 | 47 | | }; |
| | 0 | 48 | | } |
| | | 49 | | } |
| | | 50 | | } |