| | | 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.Runtime.InteropServices; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace System.IO.Compression |
| | | 10 | | { |
| | | 11 | | // DeflateManagedStream supports decompression of Deflate64 format only. |
| | | 12 | | internal sealed partial class DeflateManagedStream : Stream |
| | | 13 | | { |
| | | 14 | | internal const int DefaultBufferSize = 8192; |
| | | 15 | | |
| | | 16 | | private Stream? _stream; |
| | | 17 | | private InflaterManaged _inflater; |
| | | 18 | | private readonly byte[] _buffer; |
| | | 19 | | |
| | | 20 | | private int _asyncOperations; |
| | | 21 | | |
| | | 22 | | // A specific constructor to allow decompression of Deflate64 |
| | 0 | 23 | | internal DeflateManagedStream(Stream stream, ZipCompressionMethod method, long uncompressedSize = -1) |
| | 0 | 24 | | { |
| | 0 | 25 | | ArgumentNullException.ThrowIfNull(stream); |
| | | 26 | | |
| | 0 | 27 | | if (!stream.CanRead) |
| | 0 | 28 | | throw new ArgumentException(SR.NotSupported_UnreadableStream, nameof(stream)); |
| | | 29 | | |
| | 0 | 30 | | Debug.Assert(method == ZipCompressionMethod.Deflate64); |
| | | 31 | | |
| | 0 | 32 | | _inflater = new InflaterManaged(method == ZipCompressionMethod.Deflate64, uncompressedSize); |
| | | 33 | | |
| | 0 | 34 | | _stream = stream; |
| | 0 | 35 | | _buffer = new byte[DefaultBufferSize]; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public override bool CanRead |
| | | 39 | | { |
| | | 40 | | get |
| | 0 | 41 | | { |
| | 0 | 42 | | if (_stream == null) |
| | 0 | 43 | | { |
| | 0 | 44 | | return false; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | return _stream.CanRead; |
| | 0 | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public override bool CanWrite |
| | | 52 | | { |
| | | 53 | | get |
| | 0 | 54 | | { |
| | 0 | 55 | | return false; |
| | 0 | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | public override bool CanSeek => false; |
| | | 60 | | |
| | | 61 | | public override long Length |
| | | 62 | | { |
| | 0 | 63 | | get { throw new NotSupportedException(SR.NotSupported); } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public override long Position |
| | | 67 | | { |
| | 0 | 68 | | get { throw new NotSupportedException(SR.NotSupported); } |
| | 0 | 69 | | set { throw new NotSupportedException(SR.NotSupported); } |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | public override void Flush() |
| | 0 | 73 | | { |
| | 0 | 74 | | EnsureNotDisposed(); |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | 0 | 78 | | { |
| | 0 | 79 | | EnsureNotDisposed(); |
| | 0 | 80 | | return cancellationToken.IsCancellationRequested ? |
| | 0 | 81 | | Task.FromCanceled(cancellationToken) : |
| | 0 | 82 | | Task.CompletedTask; |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public override long Seek(long offset, SeekOrigin origin) |
| | 0 | 86 | | { |
| | 0 | 87 | | throw new NotSupportedException(SR.NotSupported); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public override void SetLength(long value) |
| | 0 | 91 | | { |
| | 0 | 92 | | throw new NotSupportedException(SR.NotSupported); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public override int Read(byte[] buffer, int offset, int count) |
| | 0 | 96 | | { |
| | 0 | 97 | | ValidateBufferArguments(buffer, offset, count); |
| | 0 | 98 | | return Read(new Span<byte>(buffer, offset, count)); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | public override int Read(Span<byte> buffer) |
| | 0 | 102 | | { |
| | 0 | 103 | | EnsureNotDisposed(); |
| | | 104 | | |
| | 0 | 105 | | int initialLength = buffer.Length; |
| | | 106 | | |
| | | 107 | | int bytesRead; |
| | 0 | 108 | | while (true) |
| | 0 | 109 | | { |
| | 0 | 110 | | bytesRead = _inflater.Inflate(buffer); |
| | 0 | 111 | | buffer = buffer.Slice(bytesRead); |
| | | 112 | | |
| | 0 | 113 | | if (buffer.Length == 0) |
| | 0 | 114 | | { |
| | 0 | 115 | | break; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | if (_inflater.Finished()) |
| | 0 | 119 | | { |
| | | 120 | | // if we finished decompressing, we can't have anything left in the outputwindow. |
| | 0 | 121 | | Debug.Assert(_inflater.AvailableOutput == 0, "We should have copied all stuff out!"); |
| | 0 | 122 | | break; |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | int bytes = _stream!.Read(_buffer, 0, _buffer.Length); |
| | 0 | 126 | | if (bytes <= 0) |
| | 0 | 127 | | { |
| | 0 | 128 | | break; |
| | | 129 | | } |
| | 0 | 130 | | else if (bytes > _buffer.Length) |
| | 0 | 131 | | { |
| | | 132 | | // The stream is either malicious or poorly implemented and returned a number of |
| | | 133 | | // bytes larger than the buffer supplied to it. |
| | 0 | 134 | | throw new InvalidDataException(SR.GenericInvalidData); |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | _inflater.SetInput(_buffer, 0, bytes); |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | return initialLength - buffer.Length; |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | public override int ReadByte() |
| | 0 | 144 | | { |
| | 0 | 145 | | byte b = default; |
| | 0 | 146 | | return Read(new Span<byte>(ref b)) == 1 ? b : -1; |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | private void EnsureNotDisposed() |
| | 0 | 150 | | { |
| | 0 | 151 | | ObjectDisposedException.ThrowIf(_stream is null, this); |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, objec |
| | 0 | 155 | | TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState) |
| | | 156 | | |
| | | 157 | | public override int EndRead(IAsyncResult asyncResult) => |
| | 0 | 158 | | TaskToAsyncResult.End<int>(asyncResult); |
| | | 159 | | |
| | | 160 | | private ValueTask<int> ReadAsyncInternal(Memory<byte> buffer, CancellationToken cancellationToken) |
| | 0 | 161 | | { |
| | 0 | 162 | | if (cancellationToken.IsCancellationRequested) |
| | 0 | 163 | | { |
| | 0 | 164 | | return ValueTask.FromCanceled<int>(cancellationToken); |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | Interlocked.Increment(ref _asyncOperations); |
| | 0 | 168 | | bool startedAsyncWork = false; |
| | | 169 | | |
| | | 170 | | try |
| | 0 | 171 | | { |
| | | 172 | | // Try to read decompressed data in output buffer |
| | 0 | 173 | | int bytesRead = _inflater.Inflate(buffer.Span); |
| | 0 | 174 | | if (bytesRead != 0) |
| | 0 | 175 | | { |
| | | 176 | | // If decompression output buffer is not empty, return immediately. |
| | 0 | 177 | | return ValueTask.FromResult(bytesRead); |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | if (_inflater.Finished()) |
| | 0 | 181 | | { |
| | | 182 | | // end of compression stream |
| | 0 | 183 | | return ValueTask.FromResult(0); |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | // If there is no data on the output buffer and we are not at |
| | | 187 | | // the end of the stream, we need to get more data from the base stream |
| | 0 | 188 | | ValueTask<int> readTask = _stream!.ReadAsync(_buffer.AsMemory(), cancellationToken); |
| | 0 | 189 | | startedAsyncWork = true; |
| | | 190 | | |
| | 0 | 191 | | return ReadAsyncCore(readTask, buffer, cancellationToken); |
| | | 192 | | } |
| | | 193 | | finally |
| | 0 | 194 | | { |
| | | 195 | | // if we haven't started any async work, decrement the counter to end the transaction |
| | 0 | 196 | | if (!startedAsyncWork) |
| | 0 | 197 | | { |
| | 0 | 198 | | Interlocked.Decrement(ref _asyncOperations); |
| | 0 | 199 | | } |
| | 0 | 200 | | } |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | private async ValueTask<int> ReadAsyncCore(ValueTask<int> readTask, Memory<byte> buffer, CancellationToken cance |
| | 0 | 204 | | { |
| | | 205 | | try |
| | 0 | 206 | | { |
| | 0 | 207 | | while (true) |
| | 0 | 208 | | { |
| | 0 | 209 | | int bytesRead = await readTask.ConfigureAwait(false); |
| | 0 | 210 | | EnsureNotDisposed(); |
| | | 211 | | |
| | 0 | 212 | | if (bytesRead <= 0) |
| | 0 | 213 | | { |
| | | 214 | | // This indicates the base stream has received EOF |
| | 0 | 215 | | return 0; |
| | | 216 | | } |
| | 0 | 217 | | else if (bytesRead > _buffer.Length) |
| | 0 | 218 | | { |
| | | 219 | | // The stream is either malicious or poorly implemented and returned a number of |
| | | 220 | | // bytes larger than the buffer supplied to it. |
| | 0 | 221 | | throw new InvalidDataException(SR.GenericInvalidData); |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 225 | | |
| | | 226 | | // Feed the data from base stream into decompression engine |
| | 0 | 227 | | _inflater.SetInput(_buffer, 0, bytesRead); |
| | 0 | 228 | | bytesRead = _inflater.Inflate(buffer.Span); |
| | | 229 | | |
| | 0 | 230 | | if (bytesRead == 0 && !_inflater.Finished()) |
| | 0 | 231 | | { |
| | | 232 | | // We could have read in head information and didn't get any data. |
| | | 233 | | // Read from the base stream again. |
| | 0 | 234 | | readTask = _stream!.ReadAsync(_buffer.AsMemory(), cancellationToken); |
| | 0 | 235 | | } |
| | | 236 | | else |
| | 0 | 237 | | { |
| | 0 | 238 | | return bytesRead; |
| | | 239 | | } |
| | 0 | 240 | | } |
| | | 241 | | } |
| | | 242 | | finally |
| | 0 | 243 | | { |
| | 0 | 244 | | Interlocked.Decrement(ref _asyncOperations); |
| | 0 | 245 | | } |
| | 0 | 246 | | } |
| | | 247 | | |
| | | 248 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 249 | | { |
| | | 250 | | // We use this checking order for compat to earlier versions: |
| | 0 | 251 | | if (_asyncOperations != 0) |
| | 0 | 252 | | throw new InvalidOperationException(SR.InvalidBeginCall); |
| | | 253 | | |
| | 0 | 254 | | ValidateBufferArguments(buffer, offset, count); |
| | 0 | 255 | | EnsureNotDisposed(); |
| | | 256 | | |
| | 0 | 257 | | return ReadAsyncInternal(buffer.AsMemory(offset, count), cancellationToken).AsTask(); |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| | 0 | 261 | | { |
| | | 262 | | // We use this checking order for compat to earlier versions: |
| | 0 | 263 | | if (_asyncOperations != 0) |
| | 0 | 264 | | throw new InvalidOperationException(SR.InvalidBeginCall); |
| | | 265 | | |
| | 0 | 266 | | EnsureNotDisposed(); |
| | | 267 | | |
| | 0 | 268 | | return ReadAsyncInternal(buffer, cancellationToken); |
| | 0 | 269 | | } |
| | | 270 | | |
| | | 271 | | public override void Write(byte[] buffer, int offset, int count) |
| | 0 | 272 | | { |
| | 0 | 273 | | throw new InvalidOperationException(SR.CannotWriteToDeflateStream); |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | // This is called by Dispose: |
| | | 277 | | private void PurgeBuffers(bool disposing) |
| | 0 | 278 | | { |
| | 0 | 279 | | if (!disposing) |
| | 0 | 280 | | return; |
| | | 281 | | |
| | 0 | 282 | | if (_stream == null) |
| | 0 | 283 | | return; |
| | | 284 | | |
| | 0 | 285 | | Flush(); |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | protected override void Dispose(bool disposing) |
| | 0 | 289 | | { |
| | | 290 | | try |
| | 0 | 291 | | { |
| | 0 | 292 | | PurgeBuffers(disposing); |
| | 0 | 293 | | } |
| | | 294 | | finally |
| | 0 | 295 | | { |
| | | 296 | | // Close the underlying stream even if PurgeBuffers threw. |
| | | 297 | | // Stream.Close() may throw here (may or may not be due to the same error). |
| | | 298 | | // In this case, we still need to clean up internal resources, hence the inner finally blocks. |
| | | 299 | | try |
| | 0 | 300 | | { |
| | 0 | 301 | | if (disposing && _stream != null) |
| | 0 | 302 | | _stream.Dispose(); |
| | 0 | 303 | | } |
| | | 304 | | finally |
| | 0 | 305 | | { |
| | 0 | 306 | | _stream = null!; |
| | 0 | 307 | | _inflater = null!; |
| | 0 | 308 | | base.Dispose(disposing); |
| | 0 | 309 | | } |
| | 0 | 310 | | } |
| | 0 | 311 | | } |
| | | 312 | | } |
| | | 313 | | } |