| | | 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 | | |
| | | 7 | | using ZErrorCode = System.IO.Compression.ZLibNative.ErrorCode; |
| | | 8 | | using ZFlushCode = System.IO.Compression.ZLibNative.FlushCode; |
| | | 9 | | |
| | | 10 | | namespace System.IO.Compression |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides a wrapper around the ZLib compression API. |
| | | 14 | | /// </summary> |
| | | 15 | | internal sealed class Deflater : IDisposable |
| | | 16 | | { |
| | | 17 | | private readonly ZLibNative.ZLibStreamHandle _zlibStream; |
| | | 18 | | private MemoryHandle _inputBufferHandle; |
| | | 19 | | private bool _isDisposed; |
| | | 20 | | private const int minWindowBits = -15; // WindowBits must be between -8..-15 to write no header, 8..15 for a |
| | | 21 | | private const int maxWindowBits = 31; // zlib header, or 24..31 for a GZip header |
| | | 22 | | |
| | | 23 | | // Note, DeflateStream or the deflater do not try to be thread safe. |
| | | 24 | | // The lock is just used to make writing to unmanaged structures atomic to make sure |
| | | 25 | | // that they do not get inconsistent fields that may lead to an unmanaged memory violation. |
| | | 26 | | // To prevent *managed* buffer corruption or other weird behavior users need to synchronize |
| | | 27 | | // on the stream explicitly. |
| | 0 | 28 | | private object SyncLock => this; |
| | | 29 | | |
| | 0 | 30 | | private Deflater(ZLibNative.ZLibStreamHandle zlibStream) |
| | 0 | 31 | | { |
| | 0 | 32 | | _zlibStream = zlibStream; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | ~Deflater() |
| | 0 | 36 | | { |
| | 0 | 37 | | Dispose(false); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public void Dispose() |
| | 0 | 41 | | { |
| | 0 | 42 | | Dispose(true); |
| | 0 | 43 | | GC.SuppressFinalize(this); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | private void Dispose(bool disposing) |
| | 0 | 47 | | { |
| | 0 | 48 | | if (!_isDisposed) |
| | 0 | 49 | | { |
| | 0 | 50 | | if (disposing) |
| | 0 | 51 | | { |
| | 0 | 52 | | _zlibStream.Dispose(); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | // Unpin the input buffer, but avoid modifying the ZLibStreamHandle (which may have been disposed of). |
| | 0 | 56 | | DeallocateInputBufferHandle(resetStreamHandle: false); |
| | 0 | 57 | | _isDisposed = true; |
| | 0 | 58 | | } |
| | 0 | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | public bool NeedsInput() => 0 == _zlibStream.AvailIn; |
| | | 62 | | |
| | | 63 | | internal unsafe void SetInput(ReadOnlyMemory<byte> inputBuffer) |
| | 0 | 64 | | { |
| | 0 | 65 | | Debug.Assert(NeedsInput(), "We have something left in previous input!"); |
| | 0 | 66 | | Debug.Assert(!inputBuffer.IsEmpty); |
| | | 67 | | |
| | 0 | 68 | | lock (SyncLock) |
| | 0 | 69 | | { |
| | 0 | 70 | | _inputBufferHandle = inputBuffer.Pin(); |
| | | 71 | | |
| | 0 | 72 | | _zlibStream.NextIn = (IntPtr)_inputBufferHandle.Pointer; |
| | 0 | 73 | | _zlibStream.AvailIn = (uint)inputBuffer.Length; |
| | 0 | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | internal unsafe void SetInput(byte* inputBufferPtr, int count) |
| | 0 | 78 | | { |
| | 0 | 79 | | Debug.Assert(NeedsInput(), "We have something left in previous input!"); |
| | 0 | 80 | | Debug.Assert(inputBufferPtr != null); |
| | 0 | 81 | | Debug.Assert(count > 0); |
| | | 82 | | |
| | 0 | 83 | | lock (SyncLock) |
| | 0 | 84 | | { |
| | 0 | 85 | | _zlibStream.NextIn = (IntPtr)inputBufferPtr; |
| | 0 | 86 | | _zlibStream.AvailIn = (uint)count; |
| | 0 | 87 | | } |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | internal int GetDeflateOutput(byte[] outputBuffer) |
| | 0 | 91 | | { |
| | 0 | 92 | | Debug.Assert(null != outputBuffer, "Can't pass in a null output buffer!"); |
| | 0 | 93 | | Debug.Assert(!NeedsInput(), "GetDeflateOutput should only be called after providing input"); |
| | | 94 | | |
| | | 95 | | try |
| | 0 | 96 | | { |
| | | 97 | | int bytesRead; |
| | 0 | 98 | | ReadDeflateOutput(outputBuffer, ZFlushCode.NoFlush, out bytesRead); |
| | 0 | 99 | | return bytesRead; |
| | | 100 | | } |
| | | 101 | | finally |
| | 0 | 102 | | { |
| | | 103 | | // Before returning, make sure to release input buffer if necessary: |
| | 0 | 104 | | if (0 == _zlibStream.AvailIn) |
| | 0 | 105 | | { |
| | 0 | 106 | | DeallocateInputBufferHandle(resetStreamHandle: true); |
| | 0 | 107 | | } |
| | 0 | 108 | | } |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | private unsafe ZErrorCode ReadDeflateOutput(byte[] outputBuffer, ZFlushCode flushCode, out int bytesRead) |
| | 0 | 112 | | { |
| | 0 | 113 | | Debug.Assert(outputBuffer?.Length > 0); |
| | | 114 | | |
| | 0 | 115 | | lock (SyncLock) |
| | 0 | 116 | | { |
| | 0 | 117 | | fixed (byte* bufPtr = &outputBuffer[0]) |
| | 0 | 118 | | { |
| | 0 | 119 | | _zlibStream.NextOut = (IntPtr)bufPtr; |
| | 0 | 120 | | _zlibStream.AvailOut = (uint)outputBuffer.Length; |
| | | 121 | | |
| | 0 | 122 | | ZErrorCode errC = Deflate(flushCode); |
| | 0 | 123 | | bytesRead = outputBuffer.Length - (int)_zlibStream.AvailOut; |
| | | 124 | | |
| | 0 | 125 | | return errC; |
| | | 126 | | } |
| | | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | internal bool Finish(byte[] outputBuffer, out int bytesRead) |
| | 0 | 131 | | { |
| | 0 | 132 | | Debug.Assert(null != outputBuffer, "Can't pass in a null output buffer!"); |
| | 0 | 133 | | Debug.Assert(outputBuffer.Length > 0, "Can't pass in an empty output buffer!"); |
| | | 134 | | |
| | 0 | 135 | | ZErrorCode errC = ReadDeflateOutput(outputBuffer, ZFlushCode.Finish, out bytesRead); |
| | 0 | 136 | | return errC == ZErrorCode.StreamEnd; |
| | 0 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Returns true if there was something to flush. Otherwise False. |
| | | 141 | | /// </summary> |
| | | 142 | | internal bool Flush(byte[] outputBuffer, out int bytesRead) |
| | 0 | 143 | | { |
| | 0 | 144 | | Debug.Assert(null != outputBuffer, "Can't pass in a null output buffer!"); |
| | 0 | 145 | | Debug.Assert(outputBuffer.Length > 0, "Can't pass in an empty output buffer!"); |
| | 0 | 146 | | Debug.Assert(NeedsInput(), "We have something left in previous input!"); |
| | | 147 | | |
| | | 148 | | |
| | | 149 | | // Note: we require that NeedsInput() == true, i.e. that 0 == _zlibStream.AvailIn. |
| | | 150 | | // If there is still input left we should never be getting here; instead we |
| | | 151 | | // should be calling GetDeflateOutput. |
| | | 152 | | |
| | 0 | 153 | | return ReadDeflateOutput(outputBuffer, ZFlushCode.SyncFlush, out bytesRead) == ZErrorCode.Ok; |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | private void DeallocateInputBufferHandle(bool resetStreamHandle) |
| | 0 | 157 | | { |
| | 0 | 158 | | lock (SyncLock) |
| | 0 | 159 | | { |
| | 0 | 160 | | if (resetStreamHandle) |
| | 0 | 161 | | { |
| | 0 | 162 | | _zlibStream.AvailIn = 0; |
| | 0 | 163 | | _zlibStream.NextIn = ZLibNative.ZNullPtr; |
| | 0 | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | _inputBufferHandle.Dispose(); |
| | 0 | 167 | | } |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | private ZErrorCode Deflate(ZFlushCode flushCode) |
| | 0 | 171 | | { |
| | | 172 | | ZErrorCode errC; |
| | | 173 | | try |
| | 0 | 174 | | { |
| | 0 | 175 | | errC = _zlibStream.Deflate(flushCode); |
| | 0 | 176 | | } |
| | 0 | 177 | | catch (Exception cause) |
| | 0 | 178 | | { |
| | 0 | 179 | | throw new ZLibException(SR.ZLibErrorDLLLoadError, cause); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | switch (errC) |
| | | 183 | | { |
| | | 184 | | case ZErrorCode.Ok: |
| | | 185 | | case ZErrorCode.StreamEnd: |
| | 0 | 186 | | return errC; |
| | | 187 | | |
| | | 188 | | case ZErrorCode.BufError: |
| | 0 | 189 | | return errC; // This is a recoverable error |
| | | 190 | | |
| | | 191 | | case ZErrorCode.StreamError: |
| | 0 | 192 | | throw new ZLibException(SR.ZLibErrorInconsistentStream, "deflate", (int)errC, _zlibStream.GetErrorMe |
| | | 193 | | |
| | | 194 | | default: |
| | 0 | 195 | | throw new ZLibException(SR.ZLibErrorUnexpected, "deflate", (int)errC, _zlibStream.GetErrorMessage()) |
| | | 196 | | } |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | public static Deflater CreateDeflater(ZLibNative.CompressionLevel compressionLevel, ZLibNative.CompressionStrate |
| | 0 | 200 | | { |
| | 0 | 201 | | Debug.Assert(windowBits >= minWindowBits && windowBits <= maxWindowBits); |
| | | 202 | | |
| | 0 | 203 | | ZLibNative.ZLibStreamHandle zlibStream = ZLibNative.ZLibStreamHandle.CreateForDeflate(compressionLevel, wind |
| | | 204 | | |
| | 0 | 205 | | return new Deflater(zlibStream); |
| | 0 | 206 | | } |
| | | 207 | | } |
| | | 208 | | } |