| | | 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.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using Microsoft.Win32.SafeHandles; |
| | | 9 | | |
| | | 10 | | namespace System.IO.Compression |
| | | 11 | | { |
| | | 12 | | /// <summary>Provides methods and properties to compress data using Zstandard compression.</summary> |
| | | 13 | | [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] |
| | | 14 | | [System.Runtime.Versioning.UnsupportedOSPlatform("wasi")] |
| | | 15 | | public sealed class ZstandardEncoder : IDisposable |
| | | 16 | | { |
| | | 17 | | internal SafeZstdCompressHandle _context; |
| | | 18 | | private bool _disposed; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// True if we finished compressing the entire input. |
| | | 22 | | /// </summary> |
| | | 23 | | private bool _finished; |
| | | 24 | | |
| | | 25 | | /// <summary>Initializes a new instance of the <see cref="ZstandardEncoder"/> class with default settings.</summ |
| | | 26 | | /// <exception cref="IOException">Failed to create the <see cref="ZstandardEncoder"/> instance.</exception> |
| | 0 | 27 | | public ZstandardEncoder() |
| | 0 | 28 | | { |
| | 0 | 29 | | _disposed = false; |
| | 0 | 30 | | InitializeEncoder(); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary>Initializes a new instance of the <see cref="ZstandardEncoder"/> class with the specified quality l |
| | | 34 | | /// <param name="quality">The compression quality level.</param> |
| | | 35 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> is not between the minimum and maxi |
| | | 36 | | /// <exception cref="IOException">Failed to create the <see cref="ZstandardEncoder"/> instance.</exception> |
| | 0 | 37 | | public ZstandardEncoder(int quality) |
| | 0 | 38 | | { |
| | 0 | 39 | | _disposed = false; |
| | 0 | 40 | | InitializeEncoder(); |
| | | 41 | | |
| | | 42 | | try |
| | 0 | 43 | | { |
| | 0 | 44 | | if (quality != 0) |
| | 0 | 45 | | { |
| | 0 | 46 | | SetQuality(_context, quality); |
| | 0 | 47 | | } |
| | 0 | 48 | | } |
| | 0 | 49 | | catch |
| | 0 | 50 | | { |
| | 0 | 51 | | _context.Dispose(); |
| | 0 | 52 | | throw; |
| | | 53 | | } |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary>Initializes a new instance of the <see cref="ZstandardEncoder"/> class with the specified dictionar |
| | | 57 | | /// <param name="dictionary">The compression dictionary to use.</param> |
| | | 58 | | /// <exception cref="ArgumentNullException"><paramref name="dictionary"/> is null.</exception> |
| | | 59 | | /// <exception cref="IOException">Failed to create the <see cref="ZstandardEncoder"/> instance.</exception> |
| | 0 | 60 | | public ZstandardEncoder(ZstandardDictionary dictionary) |
| | 0 | 61 | | { |
| | 0 | 62 | | _disposed = false; |
| | 0 | 63 | | InitializeEncoder(); |
| | | 64 | | |
| | | 65 | | try |
| | 0 | 66 | | { |
| | 0 | 67 | | SetDictionary(dictionary); |
| | 0 | 68 | | } |
| | 0 | 69 | | catch |
| | 0 | 70 | | { |
| | 0 | 71 | | _context.Dispose(); |
| | 0 | 72 | | throw; |
| | | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary>Initializes a new instance of the <see cref="ZstandardEncoder"/> class with the specified quality a |
| | | 77 | | /// <param name="quality">The compression quality level.</param> |
| | | 78 | | /// <param name="windowLog2">The base-2 logarithm of the window size for compression.</param> |
| | | 79 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> is not between the minimum and maxi |
| | | 80 | | /// <exception cref="IOException">Failed to create the <see cref="ZstandardEncoder"/> instance.</exception> |
| | 0 | 81 | | public ZstandardEncoder(int quality, int windowLog2) |
| | 0 | 82 | | { |
| | 0 | 83 | | _disposed = false; |
| | 0 | 84 | | InitializeEncoder(); |
| | | 85 | | |
| | | 86 | | try |
| | 0 | 87 | | { |
| | 0 | 88 | | if (quality != 0) |
| | 0 | 89 | | { |
| | 0 | 90 | | SetQuality(_context, quality); |
| | 0 | 91 | | } |
| | 0 | 92 | | if (windowLog2 != 0) |
| | 0 | 93 | | { |
| | 0 | 94 | | SetWindowLog(_context, windowLog2); |
| | 0 | 95 | | } |
| | 0 | 96 | | } |
| | 0 | 97 | | catch |
| | 0 | 98 | | { |
| | 0 | 99 | | _context.Dispose(); |
| | 0 | 100 | | throw; |
| | | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary>Initializes a new instance of the <see cref="ZstandardEncoder"/> class with the specified dictionar |
| | | 105 | | /// <param name="dictionary">The compression dictionary to use.</param> |
| | | 106 | | /// <param name="windowLog2">The base-2 logarithm of the window size for compression.</param> |
| | | 107 | | /// <exception cref="ArgumentNullException"><paramref name="dictionary"/> is null.</exception> |
| | | 108 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="windowLog2"/> is not between the minimum and m |
| | | 109 | | /// <exception cref="IOException">Failed to create the <see cref="ZstandardEncoder"/> instance.</exception> |
| | 0 | 110 | | public ZstandardEncoder(ZstandardDictionary dictionary, int windowLog2) |
| | 0 | 111 | | { |
| | 0 | 112 | | ArgumentNullException.ThrowIfNull(dictionary); |
| | | 113 | | |
| | 0 | 114 | | _disposed = false; |
| | 0 | 115 | | InitializeEncoder(); |
| | | 116 | | |
| | | 117 | | try |
| | 0 | 118 | | { |
| | 0 | 119 | | SetDictionary(dictionary); |
| | | 120 | | |
| | 0 | 121 | | if (windowLog2 != 0) |
| | 0 | 122 | | { |
| | 0 | 123 | | SetWindowLog(_context, windowLog2); |
| | 0 | 124 | | } |
| | 0 | 125 | | } |
| | 0 | 126 | | catch |
| | 0 | 127 | | { |
| | 0 | 128 | | _context.Dispose(); |
| | 0 | 129 | | throw; |
| | | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary>Initializes a new instance of the <see cref="ZstandardEncoder"/> class with the specified compressi |
| | | 134 | | /// <param name="compressionOptions">The compression options to use.</param> |
| | | 135 | | /// <exception cref="ArgumentNullException"><paramref name="compressionOptions"/> is null.</exception> |
| | | 136 | | /// <exception cref="ArgumentOutOfRangeException">A parameter from <paramref name="compressionOptions"/> is not |
| | | 137 | | /// <exception cref="IOException">Failed to create the <see cref="ZstandardEncoder"/> instance.</exception> |
| | 0 | 138 | | public ZstandardEncoder(ZstandardCompressionOptions compressionOptions) |
| | 0 | 139 | | { |
| | 0 | 140 | | ArgumentNullException.ThrowIfNull(compressionOptions); |
| | | 141 | | |
| | 0 | 142 | | _disposed = false; |
| | 0 | 143 | | InitializeEncoder(); |
| | | 144 | | |
| | | 145 | | try |
| | 0 | 146 | | { |
| | 0 | 147 | | if (compressionOptions.Dictionary is not null) |
| | 0 | 148 | | { |
| | 0 | 149 | | SetDictionary(compressionOptions.Dictionary); |
| | 0 | 150 | | } |
| | | 151 | | else |
| | 0 | 152 | | { |
| | 0 | 153 | | SetQuality(_context, compressionOptions.Quality); |
| | 0 | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | if (compressionOptions.WindowLog2 != 0) |
| | 0 | 157 | | { |
| | 0 | 158 | | SetWindowLog(_context, compressionOptions.WindowLog2); |
| | 0 | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | if (compressionOptions.AppendChecksum) |
| | 0 | 162 | | { |
| | 0 | 163 | | SetParameter(_context, Interop.Zstd.ZstdCParameter.ZSTD_c_checksumFlag, 1); |
| | 0 | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | if (compressionOptions.EnableLongDistanceMatching) |
| | 0 | 167 | | { |
| | 0 | 168 | | SetParameter(_context, Interop.Zstd.ZstdCParameter.ZSTD_c_enableLongDistanceMatching, 1); |
| | 0 | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | if (compressionOptions.TargetBlockSize != 0) |
| | 0 | 172 | | { |
| | 0 | 173 | | SetParameter(_context, Interop.Zstd.ZstdCParameter.ZSTD_c_targetCBlockSize, compressionOptions.Targe |
| | 0 | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | if (compressionOptions.HashLog2 != 0) |
| | 0 | 177 | | { |
| | 0 | 178 | | SetParameter(_context, Interop.Zstd.ZstdCParameter.ZSTD_c_hashLog, compressionOptions.HashLog2); |
| | 0 | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | if (compressionOptions.ChainLog2 != 0) |
| | 0 | 182 | | { |
| | 0 | 183 | | SetParameter(_context, Interop.Zstd.ZstdCParameter.ZSTD_c_chainLog, compressionOptions.ChainLog2); |
| | 0 | 184 | | } |
| | 0 | 185 | | } |
| | 0 | 186 | | catch |
| | 0 | 187 | | { |
| | 0 | 188 | | _context.Dispose(); |
| | 0 | 189 | | throw; |
| | | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | [MemberNotNull(nameof(_context))] |
| | | 194 | | private void InitializeEncoder() |
| | 0 | 195 | | { |
| | 0 | 196 | | _context = Interop.Zstd.ZSTD_createCCtx(); |
| | 0 | 197 | | if (_context.IsInvalid) |
| | 0 | 198 | | { |
| | 0 | 199 | | throw new OutOfMemoryException(); |
| | | 200 | | } |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <summary>Compresses the specified data.</summary> |
| | | 204 | | /// <param name="source">The data to compress.</param> |
| | | 205 | | /// <param name="destination">The buffer to write the compressed data to.</param> |
| | | 206 | | /// <param name="bytesConsumed">When this method returns, contains the number of bytes consumed from the source. |
| | | 207 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes written to the destination |
| | | 208 | | /// <param name="isFinalBlock"><see langword="true" /> if this is the final block of data to compress.</param> |
| | | 209 | | /// <returns>An <see cref="OperationStatus"/> indicating the result of the operation.</returns> |
| | | 210 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 211 | | /// <exception cref="IOException">An error occurred during compression.</exception> |
| | | 212 | | public OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out in |
| | 0 | 213 | | { |
| | 0 | 214 | | EnsureNotDisposed(); |
| | | 215 | | |
| | 0 | 216 | | bytesConsumed = 0; |
| | 0 | 217 | | bytesWritten = 0; |
| | | 218 | | |
| | 0 | 219 | | if (source.IsEmpty && !isFinalBlock) |
| | 0 | 220 | | { |
| | 0 | 221 | | return OperationStatus.Done; |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | return CompressCore(source, destination, out bytesConsumed, out bytesWritten, |
| | 0 | 225 | | isFinalBlock ? Interop.Zstd.ZstdEndDirective.ZSTD_e_end : Interop.Zstd.ZstdEndDirective.ZSTD_e_continue) |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary>Flushes any remaining processed data to the destination buffer.</summary> |
| | | 229 | | /// <param name="destination">The buffer to write the flushed data to.</param> |
| | | 230 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes written to the destination |
| | | 231 | | /// <returns>An <see cref="OperationStatus"/> indicating the result of the operation.</returns> |
| | | 232 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 233 | | /// <exception cref="IOException">An error occurred during the operation.</exception> |
| | | 234 | | public OperationStatus Flush(Span<byte> destination, out int bytesWritten) |
| | 0 | 235 | | { |
| | 0 | 236 | | EnsureNotDisposed(); |
| | | 237 | | |
| | 0 | 238 | | return CompressCore(ReadOnlySpan<byte>.Empty, destination, out _, out bytesWritten, |
| | 0 | 239 | | Interop.Zstd.ZstdEndDirective.ZSTD_e_flush); |
| | 0 | 240 | | } |
| | | 241 | | |
| | | 242 | | private OperationStatus CompressCore(ReadOnlySpan<byte> source, Span<byte> destination, |
| | | 243 | | out int bytesConsumed, out int bytesWritten, Interop.Zstd.ZstdEndDirective endDirective) |
| | 0 | 244 | | { |
| | 0 | 245 | | bytesConsumed = 0; |
| | 0 | 246 | | bytesWritten = 0; |
| | | 247 | | |
| | | 248 | | unsafe |
| | 0 | 249 | | { |
| | 0 | 250 | | fixed (byte* inBytes = &MemoryMarshal.GetReference(source)) |
| | 0 | 251 | | fixed (byte* outBytes = &MemoryMarshal.GetReference(destination)) |
| | 0 | 252 | | { |
| | 0 | 253 | | var input = new Interop.Zstd.ZstdInBuffer |
| | 0 | 254 | | { |
| | 0 | 255 | | src = inBytes, |
| | 0 | 256 | | size = (nuint)source.Length, |
| | 0 | 257 | | pos = 0 |
| | 0 | 258 | | }; |
| | | 259 | | |
| | 0 | 260 | | var output = new Interop.Zstd.ZstdOutBuffer |
| | 0 | 261 | | { |
| | 0 | 262 | | dst = outBytes, |
| | 0 | 263 | | size = (nuint)destination.Length, |
| | 0 | 264 | | pos = 0 |
| | 0 | 265 | | }; |
| | | 266 | | |
| | 0 | 267 | | nuint result = Interop.Zstd.ZSTD_compressStream2(_context, ref output, ref input, endDirective); |
| | | 268 | | |
| | 0 | 269 | | if (ZstandardUtils.IsError(result, out var error)) |
| | 0 | 270 | | { |
| | 0 | 271 | | if (error == Interop.Zstd.ZSTD_error.srcSize_wrong) |
| | 0 | 272 | | { |
| | 0 | 273 | | return OperationStatus.InvalidData; |
| | | 274 | | } |
| | | 275 | | |
| | 0 | 276 | | ZstandardUtils.Throw(error); |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | bytesConsumed = (int)input.pos; |
| | 0 | 280 | | bytesWritten = (int)output.pos; |
| | | 281 | | |
| | 0 | 282 | | if (input.pos == input.size) |
| | 0 | 283 | | { |
| | 0 | 284 | | _finished |= endDirective == Interop.Zstd.ZstdEndDirective.ZSTD_e_end; |
| | | 285 | | |
| | 0 | 286 | | return result == 0 ? OperationStatus.Done : OperationStatus.DestinationTooSmall; |
| | | 287 | | } |
| | | 288 | | else |
| | 0 | 289 | | { |
| | 0 | 290 | | return OperationStatus.DestinationTooSmall; |
| | | 291 | | } |
| | | 292 | | } |
| | | 293 | | } |
| | 0 | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary>Gets the maximum compressed size for the specified input length.</summary> |
| | | 297 | | /// <param name="inputLength">The length of the input data.</param> |
| | | 298 | | /// <returns>The maximum possible compressed size.</returns> |
| | | 299 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="inputLength"/> is less than 0.</exception> |
| | | 300 | | public static long GetMaxCompressedLength(long inputLength) |
| | 0 | 301 | | { |
| | 0 | 302 | | ArgumentOutOfRangeException.ThrowIfNegative(inputLength); |
| | 0 | 303 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(inputLength, nint.MaxValue); |
| | | 304 | | |
| | 0 | 305 | | nuint result = Interop.Zstd.ZSTD_compressBound((nuint)inputLength); |
| | 0 | 306 | | if (ZstandardUtils.IsError(result)) |
| | 0 | 307 | | { |
| | 0 | 308 | | throw new ArgumentOutOfRangeException(nameof(inputLength)); |
| | | 309 | | } |
| | | 310 | | |
| | 0 | 311 | | if (result > long.MaxValue) |
| | 0 | 312 | | { |
| | 0 | 313 | | throw new ArgumentOutOfRangeException(nameof(inputLength)); |
| | | 314 | | } |
| | | 315 | | |
| | 0 | 316 | | return (long)result; |
| | 0 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <summary>Attempts to compress the specified data.</summary> |
| | | 320 | | /// <param name="source">The data to compress.</param> |
| | | 321 | | /// <param name="destination">The buffer to write the compressed data to.</param> |
| | | 322 | | /// <param name="bytesWritten">When this method returns <see langword="true" />, contains the number of bytes wr |
| | | 323 | | /// <returns><see langword="true" /> on success; <see langword="false" /> if the destination buffer is too small |
| | | 324 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) |
| | 0 | 325 | | { |
| | 0 | 326 | | return TryCompress(source, destination, out bytesWritten, ZstandardUtils.Quality_Default, 0); |
| | 0 | 327 | | } |
| | | 328 | | |
| | | 329 | | /// <summary>Attempts to compress the specified data with the specified quality and window size.</summary> |
| | | 330 | | /// <param name="source">The data to compress.</param> |
| | | 331 | | /// <param name="destination">The buffer to write the compressed data to.</param> |
| | | 332 | | /// <param name="bytesWritten">When this method returns <see langword="true" />, contains the number of bytes wr |
| | | 333 | | /// <param name="quality">The compression quality level.</param> |
| | | 334 | | /// <param name="windowLog2">The base-2 logarithm of the window size for compression.</param> |
| | | 335 | | /// <returns><see langword="true" /> on success; <see langword="false" /> if the destination buffer is too small |
| | | 336 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> or <paramref name="windowLog2"/> is |
| | | 337 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual |
| | 0 | 338 | | { |
| | 0 | 339 | | return TryCompressCore(source, destination, out bytesWritten, quality, windowLog2, null); |
| | 0 | 340 | | } |
| | | 341 | | |
| | | 342 | | /// <summary>Attempts to compress the specified data with the specified dictionary and window size.</summary> |
| | | 343 | | /// <param name="source">The data to compress.</param> |
| | | 344 | | /// <param name="destination">The buffer to write the compressed data to.</param> |
| | | 345 | | /// <param name="bytesWritten">When this method returns <see langword="true" />, contains the number of bytes wr |
| | | 346 | | /// <param name="dictionary">The compression dictionary to use.</param> |
| | | 347 | | /// <param name="windowLog2">The base-2 logarithm of the window size for compression.</param> |
| | | 348 | | /// <returns><see langword="true" /> on success; <see langword="false" /> if the destination buffer is too small |
| | | 349 | | /// <exception cref="ArgumentNullException"><paramref name="dictionary"/> is null.</exception> |
| | | 350 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="windowLog2"/> is out of the valid range.</exce |
| | | 351 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, Zstandar |
| | 0 | 352 | | { |
| | 0 | 353 | | ArgumentNullException.ThrowIfNull(dictionary); |
| | 0 | 354 | | return TryCompressCore(source, destination, out bytesWritten, 0, windowLog2, dictionary); |
| | 0 | 355 | | } |
| | | 356 | | |
| | | 357 | | internal static bool TryCompressCore(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, in |
| | 0 | 358 | | { |
| | 0 | 359 | | bytesWritten = 0; |
| | | 360 | | |
| | 0 | 361 | | using SafeZstdCompressHandle ctx = Interop.Zstd.ZSTD_createCCtx(); |
| | 0 | 362 | | if (ctx.IsInvalid) |
| | 0 | 363 | | { |
| | 0 | 364 | | throw new OutOfMemoryException(); |
| | | 365 | | } |
| | | 366 | | |
| | 0 | 367 | | if (dictionary != null) |
| | 0 | 368 | | { |
| | 0 | 369 | | ctx.SetDictionary(dictionary.CompressionDictionary); |
| | 0 | 370 | | } |
| | | 371 | | else |
| | 0 | 372 | | { |
| | 0 | 373 | | SetQuality(ctx, quality); |
| | 0 | 374 | | } |
| | | 375 | | |
| | 0 | 376 | | if (windowLog2 != 0) |
| | 0 | 377 | | { |
| | 0 | 378 | | SetWindowLog(ctx, windowLog2); |
| | 0 | 379 | | } |
| | | 380 | | |
| | | 381 | | unsafe |
| | 0 | 382 | | { |
| | 0 | 383 | | fixed (byte* inBytes = &MemoryMarshal.GetReference(source)) |
| | 0 | 384 | | fixed (byte* outBytes = &MemoryMarshal.GetReference(destination)) |
| | 0 | 385 | | { |
| | 0 | 386 | | nuint result = Interop.Zstd.ZSTD_compress2(ctx, outBytes, (nuint)destination.Length, inBytes, (nuint |
| | | 387 | | |
| | 0 | 388 | | if (ZstandardUtils.IsError(result, out var error)) |
| | 0 | 389 | | { |
| | 0 | 390 | | return false; |
| | | 391 | | } |
| | | 392 | | |
| | 0 | 393 | | bytesWritten = (int)result; |
| | 0 | 394 | | return true; |
| | | 395 | | } |
| | | 396 | | } |
| | 0 | 397 | | } |
| | | 398 | | |
| | | 399 | | /// <summary>Resets the encoder session, allowing reuse for the next compression operation.</summary> |
| | | 400 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 401 | | /// <exception cref="IOException">Failed to reset the encoder session.</exception> |
| | | 402 | | public void Reset() |
| | 0 | 403 | | { |
| | 0 | 404 | | EnsureNotDisposed(); |
| | | 405 | | |
| | 0 | 406 | | _finished = false; |
| | 0 | 407 | | _context.Reset(); |
| | 0 | 408 | | } |
| | | 409 | | |
| | | 410 | | /// <summary>References a prefix for the next compression operation.</summary> |
| | | 411 | | /// <remarks> |
| | | 412 | | /// The prefix will be used only for the next compression frame and will be removed when <see cref="Reset"/> is |
| | | 413 | | /// When compressing with a prefix larger than ~32 MB at high quality levels, set <see cref="ZstandardCompressio |
| | | 414 | | /// </remarks> |
| | | 415 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 416 | | /// <exception cref="InvalidOperationException">The encoder is in an invalid state for setting a prefix.</except |
| | | 417 | | public void SetPrefix(ReadOnlyMemory<byte> prefix) |
| | 0 | 418 | | { |
| | 0 | 419 | | EnsureNotDisposed(); |
| | | 420 | | |
| | 0 | 421 | | if (_finished) |
| | 0 | 422 | | { |
| | 0 | 423 | | throw new InvalidOperationException(SR.ZstandardEncoderDecoder_InvalidState); |
| | | 424 | | } |
| | | 425 | | |
| | 0 | 426 | | nuint result = _context.SetPrefix(prefix); |
| | | 427 | | |
| | 0 | 428 | | if (ZstandardUtils.IsError(result, out var error)) |
| | 0 | 429 | | { |
| | 0 | 430 | | ZstandardUtils.Throw(error); |
| | | 431 | | } |
| | 0 | 432 | | } |
| | | 433 | | |
| | | 434 | | /// <summary>Sets the length of the uncompressed data for the next compression operation.</summary> |
| | | 435 | | /// <param name="length">The length of the source data in bytes.</param> |
| | | 436 | | /// <remarks> |
| | | 437 | | /// Setting the source length is optional. If set, the information is stored in the header of the compressed dat |
| | | 438 | | /// Calling <see cref="Reset"/> clears the length. The length is validated during compression and if the value i |
| | | 439 | | /// the operation status is <see cref="OperationStatus.InvalidData"/>. |
| | | 440 | | /// </remarks> |
| | | 441 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is negative.</exception> |
| | | 442 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 443 | | public void SetSourceLength(long length) |
| | 0 | 444 | | { |
| | 0 | 445 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | | 446 | | |
| | 0 | 447 | | EnsureNotDisposed(); |
| | | 448 | | |
| | 0 | 449 | | if (_finished) |
| | 0 | 450 | | { |
| | 0 | 451 | | throw new InvalidOperationException(SR.ZstandardEncoderDecoder_InvalidState); |
| | | 452 | | } |
| | | 453 | | |
| | 0 | 454 | | if (ZstandardUtils.IsError(Interop.Zstd.ZSTD_CCtx_setPledgedSrcSize(_context, (ulong)length), out var error) |
| | 0 | 455 | | { |
| | 0 | 456 | | ZstandardUtils.Throw(error); |
| | | 457 | | } |
| | 0 | 458 | | } |
| | | 459 | | |
| | | 460 | | /// <summary>Releases all resources used by the <see cref="ZstandardEncoder"/>.</summary> |
| | | 461 | | public void Dispose() |
| | 0 | 462 | | { |
| | 0 | 463 | | _disposed = true; |
| | 0 | 464 | | _context.Dispose(); |
| | 0 | 465 | | } |
| | | 466 | | |
| | | 467 | | private void EnsureNotDisposed() |
| | 0 | 468 | | { |
| | 0 | 469 | | ObjectDisposedException.ThrowIf(_disposed, nameof(ZstandardEncoder)); |
| | 0 | 470 | | } |
| | | 471 | | |
| | | 472 | | internal static void SetQuality(SafeZstdCompressHandle handle, int quality) |
| | 0 | 473 | | { |
| | 0 | 474 | | if (quality != 0) |
| | 0 | 475 | | { |
| | 0 | 476 | | ArgumentOutOfRangeException.ThrowIfLessThan(quality, ZstandardUtils.Quality_Min, nameof(quality)); |
| | 0 | 477 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(quality, ZstandardUtils.Quality_Max, nameof(quality)); |
| | 0 | 478 | | } |
| | | 479 | | |
| | 0 | 480 | | SetParameter(handle, Interop.Zstd.ZstdCParameter.ZSTD_c_compressionLevel, quality); |
| | 0 | 481 | | } |
| | | 482 | | |
| | | 483 | | internal static void SetWindowLog(SafeZstdCompressHandle handle, int windowLog2) |
| | 0 | 484 | | { |
| | 0 | 485 | | if (windowLog2 != 0) |
| | 0 | 486 | | { |
| | 0 | 487 | | ArgumentOutOfRangeException.ThrowIfLessThan(windowLog2, ZstandardUtils.WindowLog_Min, nameof(windowLog2) |
| | 0 | 488 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(windowLog2, ZstandardUtils.WindowLog_Max, nameof(windowLo |
| | 0 | 489 | | } |
| | | 490 | | |
| | 0 | 491 | | SetParameter(handle, Interop.Zstd.ZstdCParameter.ZSTD_c_windowLog, windowLog2); |
| | 0 | 492 | | } |
| | | 493 | | |
| | | 494 | | internal void SetDictionary(ZstandardDictionary dictionary) |
| | 0 | 495 | | { |
| | 0 | 496 | | Debug.Assert(_context != null); |
| | 0 | 497 | | ArgumentNullException.ThrowIfNull(dictionary); |
| | | 498 | | |
| | 0 | 499 | | _context.SetDictionary(dictionary.CompressionDictionary); |
| | 0 | 500 | | } |
| | | 501 | | |
| | | 502 | | internal static void SetParameter(SafeZstdCompressHandle handle, Interop.Zstd.ZstdCParameter parameter, int valu |
| | 0 | 503 | | { |
| | 0 | 504 | | Debug.Assert(handle != null); |
| | | 505 | | |
| | 0 | 506 | | nuint result = Interop.Zstd.ZSTD_CCtx_setParameter(handle, parameter, value); |
| | 0 | 507 | | ZstandardUtils.ThrowIfError(result); |
| | 0 | 508 | | } |
| | | 509 | | } |
| | | 510 | | } |
| | | 511 | | |