| | | 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 | | } |
| | 0 | 175 | | } |
| | 0 | 176 | | catch |
| | 0 | 177 | | { |
| | 0 | 178 | | _context.Dispose(); |
| | 0 | 179 | | throw; |
| | | 180 | | } |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | [MemberNotNull(nameof(_context))] |
| | | 184 | | private void InitializeEncoder() |
| | 0 | 185 | | { |
| | 0 | 186 | | _context = Interop.Zstd.ZSTD_createCCtx(); |
| | 0 | 187 | | if (_context.IsInvalid) |
| | 0 | 188 | | { |
| | 0 | 189 | | throw new OutOfMemoryException(); |
| | | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary>Compresses the specified data.</summary> |
| | | 194 | | /// <param name="source">The data to compress.</param> |
| | | 195 | | /// <param name="destination">The buffer to write the compressed data to.</param> |
| | | 196 | | /// <param name="bytesConsumed">When this method returns, contains the number of bytes consumed from the source. |
| | | 197 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes written to the destination |
| | | 198 | | /// <param name="isFinalBlock"><see langword="true" /> if this is the final block of data to compress.</param> |
| | | 199 | | /// <returns>An <see cref="OperationStatus"/> indicating the result of the operation.</returns> |
| | | 200 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 201 | | /// <exception cref="IOException">An error occurred during compression.</exception> |
| | | 202 | | public OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out in |
| | 0 | 203 | | { |
| | 0 | 204 | | EnsureNotDisposed(); |
| | | 205 | | |
| | 0 | 206 | | bytesConsumed = 0; |
| | 0 | 207 | | bytesWritten = 0; |
| | | 208 | | |
| | 0 | 209 | | if (source.IsEmpty && !isFinalBlock) |
| | 0 | 210 | | { |
| | 0 | 211 | | return OperationStatus.Done; |
| | | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | return CompressCore(source, destination, out bytesConsumed, out bytesWritten, |
| | 0 | 215 | | isFinalBlock ? Interop.Zstd.ZstdEndDirective.ZSTD_e_end : Interop.Zstd.ZstdEndDirective.ZSTD_e_continue) |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary>Flushes any remaining processed data to the destination buffer.</summary> |
| | | 219 | | /// <param name="destination">The buffer to write the flushed data to.</param> |
| | | 220 | | /// <param name="bytesWritten">When this method returns, contains the number of bytes written to the destination |
| | | 221 | | /// <returns>An <see cref="OperationStatus"/> indicating the result of the operation.</returns> |
| | | 222 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 223 | | /// <exception cref="IOException">An error occurred during the operation.</exception> |
| | | 224 | | public OperationStatus Flush(Span<byte> destination, out int bytesWritten) |
| | 0 | 225 | | { |
| | 0 | 226 | | EnsureNotDisposed(); |
| | | 227 | | |
| | 0 | 228 | | return CompressCore(ReadOnlySpan<byte>.Empty, destination, out _, out bytesWritten, |
| | 0 | 229 | | Interop.Zstd.ZstdEndDirective.ZSTD_e_flush); |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | private OperationStatus CompressCore(ReadOnlySpan<byte> source, Span<byte> destination, |
| | | 233 | | out int bytesConsumed, out int bytesWritten, Interop.Zstd.ZstdEndDirective endDirective) |
| | 0 | 234 | | { |
| | 0 | 235 | | bytesConsumed = 0; |
| | 0 | 236 | | bytesWritten = 0; |
| | | 237 | | |
| | | 238 | | unsafe |
| | 0 | 239 | | { |
| | 0 | 240 | | fixed (byte* inBytes = &MemoryMarshal.GetReference(source)) |
| | 0 | 241 | | fixed (byte* outBytes = &MemoryMarshal.GetReference(destination)) |
| | 0 | 242 | | { |
| | 0 | 243 | | var input = new Interop.Zstd.ZstdInBuffer |
| | 0 | 244 | | { |
| | 0 | 245 | | src = inBytes, |
| | 0 | 246 | | size = (nuint)source.Length, |
| | 0 | 247 | | pos = 0 |
| | 0 | 248 | | }; |
| | | 249 | | |
| | 0 | 250 | | var output = new Interop.Zstd.ZstdOutBuffer |
| | 0 | 251 | | { |
| | 0 | 252 | | dst = outBytes, |
| | 0 | 253 | | size = (nuint)destination.Length, |
| | 0 | 254 | | pos = 0 |
| | 0 | 255 | | }; |
| | | 256 | | |
| | 0 | 257 | | nuint result = Interop.Zstd.ZSTD_compressStream2(_context, ref output, ref input, endDirective); |
| | | 258 | | |
| | 0 | 259 | | if (ZstandardUtils.IsError(result, out var error)) |
| | 0 | 260 | | { |
| | 0 | 261 | | if (error == Interop.Zstd.ZSTD_error.srcSize_wrong) |
| | 0 | 262 | | { |
| | 0 | 263 | | return OperationStatus.InvalidData; |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | ZstandardUtils.Throw(error); |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | bytesConsumed = (int)input.pos; |
| | 0 | 270 | | bytesWritten = (int)output.pos; |
| | | 271 | | |
| | 0 | 272 | | if (input.pos == input.size) |
| | 0 | 273 | | { |
| | 0 | 274 | | _finished |= endDirective == Interop.Zstd.ZstdEndDirective.ZSTD_e_end; |
| | | 275 | | |
| | 0 | 276 | | return result == 0 ? OperationStatus.Done : OperationStatus.DestinationTooSmall; |
| | | 277 | | } |
| | | 278 | | else |
| | 0 | 279 | | { |
| | 0 | 280 | | return OperationStatus.DestinationTooSmall; |
| | | 281 | | } |
| | | 282 | | } |
| | | 283 | | } |
| | 0 | 284 | | } |
| | | 285 | | |
| | | 286 | | /// <summary>Gets the maximum compressed size for the specified input length.</summary> |
| | | 287 | | /// <param name="inputLength">The length of the input data.</param> |
| | | 288 | | /// <returns>The maximum possible compressed size.</returns> |
| | | 289 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="inputLength"/> is less than 0.</exception> |
| | | 290 | | public static long GetMaxCompressedLength(long inputLength) |
| | 0 | 291 | | { |
| | 0 | 292 | | ArgumentOutOfRangeException.ThrowIfNegative(inputLength); |
| | 0 | 293 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(inputLength, nint.MaxValue); |
| | | 294 | | |
| | 0 | 295 | | nuint result = Interop.Zstd.ZSTD_compressBound((nuint)inputLength); |
| | 0 | 296 | | if (ZstandardUtils.IsError(result)) |
| | 0 | 297 | | { |
| | 0 | 298 | | throw new ArgumentOutOfRangeException(nameof(inputLength)); |
| | | 299 | | } |
| | | 300 | | |
| | 0 | 301 | | if (result > long.MaxValue) |
| | 0 | 302 | | { |
| | 0 | 303 | | throw new ArgumentOutOfRangeException(nameof(inputLength)); |
| | | 304 | | } |
| | | 305 | | |
| | 0 | 306 | | return (long)result; |
| | 0 | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary>Attempts to compress the specified data.</summary> |
| | | 310 | | /// <param name="source">The data to compress.</param> |
| | | 311 | | /// <param name="destination">The buffer to write the compressed data to.</param> |
| | | 312 | | /// <param name="bytesWritten">When this method returns <see langword="true" />, contains the number of bytes wr |
| | | 313 | | /// <returns><see langword="true" /> on success; <see langword="false" /> if the destination buffer is too small |
| | | 314 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) |
| | 0 | 315 | | { |
| | 0 | 316 | | return TryCompress(source, destination, out bytesWritten, ZstandardUtils.Quality_Default, 0); |
| | 0 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <summary>Attempts to compress the specified data with the specified quality and window size.</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 | | /// <param name="quality">The compression quality level.</param> |
| | | 324 | | /// <param name="windowLog2">The base-2 logarithm of the window size for compression.</param> |
| | | 325 | | /// <returns><see langword="true" /> on success; <see langword="false" /> if the destination buffer is too small |
| | | 326 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> or <paramref name="windowLog2"/> is |
| | | 327 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual |
| | 0 | 328 | | { |
| | 0 | 329 | | return TryCompressCore(source, destination, out bytesWritten, quality, windowLog2, null); |
| | 0 | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary>Attempts to compress the specified data with the specified dictionary and window size.</summary> |
| | | 333 | | /// <param name="source">The data to compress.</param> |
| | | 334 | | /// <param name="destination">The buffer to write the compressed data to.</param> |
| | | 335 | | /// <param name="bytesWritten">When this method returns <see langword="true" />, contains the number of bytes wr |
| | | 336 | | /// <param name="dictionary">The compression dictionary to use.</param> |
| | | 337 | | /// <param name="windowLog2">The base-2 logarithm of the window size for compression.</param> |
| | | 338 | | /// <returns><see langword="true" /> on success; <see langword="false" /> if the destination buffer is too small |
| | | 339 | | /// <exception cref="ArgumentNullException"><paramref name="dictionary"/> is null.</exception> |
| | | 340 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="windowLog2"/> is out of the valid range.</exce |
| | | 341 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, Zstandar |
| | 0 | 342 | | { |
| | 0 | 343 | | ArgumentNullException.ThrowIfNull(dictionary); |
| | 0 | 344 | | return TryCompressCore(source, destination, out bytesWritten, 0, windowLog2, dictionary); |
| | 0 | 345 | | } |
| | | 346 | | |
| | | 347 | | internal static bool TryCompressCore(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, in |
| | 0 | 348 | | { |
| | 0 | 349 | | bytesWritten = 0; |
| | | 350 | | |
| | 0 | 351 | | using SafeZstdCompressHandle ctx = Interop.Zstd.ZSTD_createCCtx(); |
| | 0 | 352 | | if (ctx.IsInvalid) |
| | 0 | 353 | | { |
| | 0 | 354 | | throw new OutOfMemoryException(); |
| | | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | if (dictionary != null) |
| | 0 | 358 | | { |
| | 0 | 359 | | ctx.SetDictionary(dictionary.CompressionDictionary); |
| | 0 | 360 | | } |
| | | 361 | | else |
| | 0 | 362 | | { |
| | 0 | 363 | | SetQuality(ctx, quality); |
| | 0 | 364 | | } |
| | | 365 | | |
| | 0 | 366 | | if (windowLog2 != 0) |
| | 0 | 367 | | { |
| | 0 | 368 | | SetWindowLog(ctx, windowLog2); |
| | 0 | 369 | | } |
| | | 370 | | |
| | | 371 | | unsafe |
| | 0 | 372 | | { |
| | 0 | 373 | | fixed (byte* inBytes = &MemoryMarshal.GetReference(source)) |
| | 0 | 374 | | fixed (byte* outBytes = &MemoryMarshal.GetReference(destination)) |
| | 0 | 375 | | { |
| | 0 | 376 | | nuint result = Interop.Zstd.ZSTD_compress2(ctx, outBytes, (nuint)destination.Length, inBytes, (nuint |
| | | 377 | | |
| | 0 | 378 | | if (ZstandardUtils.IsError(result, out var error)) |
| | 0 | 379 | | { |
| | 0 | 380 | | return false; |
| | | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | bytesWritten = (int)result; |
| | 0 | 384 | | return true; |
| | | 385 | | } |
| | | 386 | | } |
| | 0 | 387 | | } |
| | | 388 | | |
| | | 389 | | /// <summary>Resets the encoder session, allowing reuse for the next compression operation.</summary> |
| | | 390 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 391 | | /// <exception cref="IOException">Failed to reset the encoder session.</exception> |
| | | 392 | | public void Reset() |
| | 0 | 393 | | { |
| | 0 | 394 | | EnsureNotDisposed(); |
| | | 395 | | |
| | 0 | 396 | | _finished = false; |
| | 0 | 397 | | _context.Reset(); |
| | 0 | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <summary>References a prefix for the next compression operation.</summary> |
| | | 401 | | /// <remarks>The prefix will be used only for the next compression frame and will be removed when <see cref="Res |
| | | 402 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 403 | | /// <exception cref="InvalidOperationException">The encoder is in an invalid state for setting a prefix.</except |
| | | 404 | | public void SetPrefix(ReadOnlyMemory<byte> prefix) |
| | 0 | 405 | | { |
| | 0 | 406 | | EnsureNotDisposed(); |
| | | 407 | | |
| | 0 | 408 | | if (_finished) |
| | 0 | 409 | | { |
| | 0 | 410 | | throw new InvalidOperationException(SR.ZstandardEncoderDecoder_InvalidState); |
| | | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | nuint result = _context.SetPrefix(prefix); |
| | | 414 | | |
| | 0 | 415 | | if (ZstandardUtils.IsError(result, out var error)) |
| | 0 | 416 | | { |
| | 0 | 417 | | ZstandardUtils.Throw(error); |
| | | 418 | | } |
| | 0 | 419 | | } |
| | | 420 | | |
| | | 421 | | /// <summary>Sets the length of the uncompressed data for the next compression operation.</summary> |
| | | 422 | | /// <param name="length">The length of the source data in bytes.</param> |
| | | 423 | | /// <remarks> |
| | | 424 | | /// Setting the source length is optional. If set, the information is stored in the header of the compressed dat |
| | | 425 | | /// Calling <see cref="Reset"/> clears the length. The length is validated during compression and if the value i |
| | | 426 | | /// the operation status is <see cref="OperationStatus.InvalidData"/>. |
| | | 427 | | /// </remarks> |
| | | 428 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is negative.</exception> |
| | | 429 | | /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception> |
| | | 430 | | public void SetSourceLength(long length) |
| | 0 | 431 | | { |
| | 0 | 432 | | ArgumentOutOfRangeException.ThrowIfNegative(length); |
| | | 433 | | |
| | 0 | 434 | | EnsureNotDisposed(); |
| | | 435 | | |
| | 0 | 436 | | if (_finished) |
| | 0 | 437 | | { |
| | 0 | 438 | | throw new InvalidOperationException(SR.ZstandardEncoderDecoder_InvalidState); |
| | | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | if (ZstandardUtils.IsError(Interop.Zstd.ZSTD_CCtx_setPledgedSrcSize(_context, (ulong)length), out var error) |
| | 0 | 442 | | { |
| | 0 | 443 | | ZstandardUtils.Throw(error); |
| | | 444 | | } |
| | 0 | 445 | | } |
| | | 446 | | |
| | | 447 | | /// <summary>Releases all resources used by the <see cref="ZstandardEncoder"/>.</summary> |
| | | 448 | | public void Dispose() |
| | 0 | 449 | | { |
| | 0 | 450 | | _disposed = true; |
| | 0 | 451 | | _context.Dispose(); |
| | 0 | 452 | | } |
| | | 453 | | |
| | | 454 | | private void EnsureNotDisposed() |
| | 0 | 455 | | { |
| | 0 | 456 | | ObjectDisposedException.ThrowIf(_disposed, nameof(ZstandardEncoder)); |
| | 0 | 457 | | } |
| | | 458 | | |
| | | 459 | | internal static void SetQuality(SafeZstdCompressHandle handle, int quality) |
| | 0 | 460 | | { |
| | 0 | 461 | | if (quality != 0) |
| | 0 | 462 | | { |
| | 0 | 463 | | ArgumentOutOfRangeException.ThrowIfLessThan(quality, ZstandardUtils.Quality_Min, nameof(quality)); |
| | 0 | 464 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(quality, ZstandardUtils.Quality_Max, nameof(quality)); |
| | 0 | 465 | | } |
| | | 466 | | |
| | 0 | 467 | | SetParameter(handle, Interop.Zstd.ZstdCParameter.ZSTD_c_compressionLevel, quality); |
| | 0 | 468 | | } |
| | | 469 | | |
| | | 470 | | internal static void SetWindowLog(SafeZstdCompressHandle handle, int windowLog2) |
| | 0 | 471 | | { |
| | 0 | 472 | | if (windowLog2 != 0) |
| | 0 | 473 | | { |
| | 0 | 474 | | ArgumentOutOfRangeException.ThrowIfLessThan(windowLog2, ZstandardUtils.WindowLog_Min, nameof(windowLog2) |
| | 0 | 475 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(windowLog2, ZstandardUtils.WindowLog_Max, nameof(windowLo |
| | 0 | 476 | | } |
| | | 477 | | |
| | 0 | 478 | | SetParameter(handle, Interop.Zstd.ZstdCParameter.ZSTD_c_windowLog, windowLog2); |
| | 0 | 479 | | } |
| | | 480 | | |
| | | 481 | | internal void SetDictionary(ZstandardDictionary dictionary) |
| | 0 | 482 | | { |
| | 0 | 483 | | Debug.Assert(_context != null); |
| | 0 | 484 | | ArgumentNullException.ThrowIfNull(dictionary); |
| | | 485 | | |
| | 0 | 486 | | _context.SetDictionary(dictionary.CompressionDictionary); |
| | 0 | 487 | | } |
| | | 488 | | |
| | | 489 | | internal static void SetParameter(SafeZstdCompressHandle handle, Interop.Zstd.ZstdCParameter parameter, int valu |
| | 0 | 490 | | { |
| | 0 | 491 | | Debug.Assert(handle != null); |
| | | 492 | | |
| | 0 | 493 | | nuint result = Interop.Zstd.ZSTD_CCtx_setParameter(handle, parameter, value); |
| | 0 | 494 | | ZstandardUtils.ThrowIfError(result); |
| | 0 | 495 | | } |
| | | 496 | | } |
| | | 497 | | } |