Browse Source

Merge pull request #2325 from stefannikolei/stefannikolei/nullable/zlib

Remove nullable disable from zlib
pull/2326/head
James Jackson-South 3 years ago
committed by GitHub
parent
commit
ce4b49fb7d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      src/ImageSharp/Compression/Zlib/DeflateThrowHelper.cs
  2. 2
      src/ImageSharp/Compression/Zlib/Deflater.cs
  3. 12
      src/ImageSharp/Compression/Zlib/DeflaterEngine.cs
  4. 9
      src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs
  5. 3
      src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs
  6. 2
      src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs
  7. 3
      src/ImageSharp/Compression/Zlib/ZlibDeflateStream.cs
  8. 6
      src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs
  9. 7
      src/ImageSharp/Diagnostics/MemoryDiagnostics.cs
  10. 22
      src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs

10
src/ImageSharp/Compression/Zlib/DeflateThrowHelper.cs

@ -1,23 +1,33 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Compression.Zlib; namespace SixLabors.ImageSharp.Compression.Zlib;
internal static class DeflateThrowHelper internal static class DeflateThrowHelper
{ {
[DoesNotReturn]
public static void ThrowAlreadyFinished() => throw new InvalidOperationException("Finish() already called."); public static void ThrowAlreadyFinished() => throw new InvalidOperationException("Finish() already called.");
[DoesNotReturn]
public static void ThrowAlreadyClosed() => throw new InvalidOperationException("Deflator already closed."); public static void ThrowAlreadyClosed() => throw new InvalidOperationException("Deflator already closed.");
[DoesNotReturn]
public static void ThrowUnknownCompression() => throw new InvalidOperationException("Unknown compression function."); public static void ThrowUnknownCompression() => throw new InvalidOperationException("Unknown compression function.");
[DoesNotReturn]
public static void ThrowNotProcessed() => throw new InvalidOperationException("Old input was not completely processed."); public static void ThrowNotProcessed() => throw new InvalidOperationException("Old input was not completely processed.");
[DoesNotReturn]
public static void ThrowNull(string name) => throw new ArgumentNullException(name); public static void ThrowNull(string name) => throw new ArgumentNullException(name);
[DoesNotReturn]
public static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name); public static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name);
[DoesNotReturn]
public static void ThrowHeapViolated() => throw new InvalidOperationException("Huffman heap invariant violated."); public static void ThrowHeapViolated() => throw new InvalidOperationException("Huffman heap invariant violated.");
[DoesNotReturn]
public static void ThrowNoDeflate() => throw new ImageFormatException("Cannot deflate all input."); public static void ThrowNoDeflate() => throw new ImageFormatException("Cannot deflate all input.");
} }

2
src/ImageSharp/Compression/Zlib/Deflater.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
@ -285,7 +284,6 @@ internal sealed class Deflater : IDisposable
if (!this.isDisposed) if (!this.isDisposed)
{ {
this.engine.Dispose(); this.engine.Dispose();
this.engine = null;
this.isDisposed = true; this.isDisposed = true;
} }
} }

12
src/ImageSharp/Compression/Zlib/DeflaterEngine.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Buffers; using System.Buffers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -87,7 +86,7 @@ internal sealed unsafe class DeflaterEngine : IDisposable
/// <summary> /// <summary>
/// The input data for compression. /// The input data for compression.
/// </summary> /// </summary>
private byte[] inputBuf; private byte[]? inputBuf;
/// <summary> /// <summary>
/// The offset into inputBuf, where input data starts. /// The offset into inputBuf, where input data starts.
@ -222,7 +221,7 @@ internal sealed unsafe class DeflaterEngine : IDisposable
/// <param name="buffer">The buffer containing input data.</param> /// <param name="buffer">The buffer containing input data.</param>
/// <param name="offset">The offset of the first byte of data.</param> /// <param name="offset">The offset of the first byte of data.</param>
/// <param name="count">The number of bytes of data to use as input.</param> /// <param name="count">The number of bytes of data to use as input.</param>
public void SetInput(byte[] buffer, int offset, int count) public void SetInput(byte[]? buffer, int offset, int count)
{ {
if (buffer is null) if (buffer is null)
{ {
@ -362,6 +361,8 @@ internal sealed unsafe class DeflaterEngine : IDisposable
more = this.inputEnd - this.inputOff; more = this.inputEnd - this.inputOff;
} }
ArgumentNullException.ThrowIfNull(this.inputBuf);
Unsafe.CopyBlockUnaligned( Unsafe.CopyBlockUnaligned(
ref this.window.Span[this.strstart + this.lookahead], ref this.window.Span[this.strstart + this.lookahead],
ref this.inputBuf[this.inputOff], ref this.inputBuf[this.inputOff],
@ -393,11 +394,6 @@ internal sealed unsafe class DeflaterEngine : IDisposable
this.prevMemoryHandle.Dispose(); this.prevMemoryHandle.Dispose();
this.prevMemoryOwner.Dispose(); this.prevMemoryOwner.Dispose();
this.windowMemoryOwner = null;
this.headMemoryOwner = null;
this.prevMemoryOwner = null;
this.huffman = null;
this.isDisposed = true; this.isDisposed = true;
} }
} }

9
src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Buffers; using System.Buffers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -427,10 +426,6 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
this.blTree.Dispose(); this.blTree.Dispose();
this.distTree.Dispose(); this.distTree.Dispose();
this.Pending = null;
this.literalTree = null;
this.blTree = null;
this.distTree = null;
this.isDisposed = true; this.isDisposed = true;
} }
} }
@ -977,10 +972,6 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
this.codesMemoryHandle.Dispose(); this.codesMemoryHandle.Dispose();
this.codesMemoryOwner.Dispose(); this.codesMemoryOwner.Dispose();
this.frequenciesMemoryOwner = null;
this.lengthsMemoryOwner = null;
this.codesMemoryOwner = null;
this.isDisposed = true; this.isDisposed = true;
} }
} }

3
src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Buffers; using System.Buffers;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
@ -137,8 +136,6 @@ internal sealed class DeflaterOutputStream : Stream
this.memoryOwner.Dispose(); this.memoryOwner.Dispose();
} }
this.deflater = null;
this.memoryOwner = null;
this.isDisposed = true; this.isDisposed = true;
base.Dispose(disposing); base.Dispose(disposing);
} }

2
src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Buffers; using System.Buffers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -180,7 +179,6 @@ internal sealed unsafe class DeflaterPendingBuffer : IDisposable
{ {
this.bufferMemoryHandle.Dispose(); this.bufferMemoryHandle.Dispose();
this.bufferMemoryOwner.Dispose(); this.bufferMemoryOwner.Dispose();
this.bufferMemoryOwner = null;
this.isDisposed = true; this.isDisposed = true;
} }
} }

3
src/ImageSharp/Compression/Zlib/ZlibDeflateStream.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Png;
@ -172,8 +171,6 @@ internal sealed class ZlibDeflateStream : Stream
this.rawStream.WriteByte((byte)(crc & 0xFF)); this.rawStream.WriteByte((byte)(crc & 0xFF));
} }
this.deflateStream = null;
base.Dispose(disposing); base.Dispose(disposing);
this.isDisposed = true; this.isDisposed = true;
} }

6
src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs

@ -1,7 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Diagnostics.CodeAnalysis;
using System.IO.Compression; using System.IO.Compression;
using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.IO;
@ -90,7 +90,7 @@ internal sealed class ZlibInflateStream : Stream
/// <summary> /// <summary>
/// Gets the compressed stream over the deframed inner stream. /// Gets the compressed stream over the deframed inner stream.
/// </summary> /// </summary>
public DeflateStream CompressedStream { get; private set; } public DeflateStream? CompressedStream { get; private set; }
/// <summary> /// <summary>
/// Adds new bytes from a frame found in the original stream. /// Adds new bytes from a frame found in the original stream.
@ -98,6 +98,7 @@ internal sealed class ZlibInflateStream : Stream
/// <param name="bytes">The current remaining data according to the chunk length.</param> /// <param name="bytes">The current remaining data according to the chunk length.</param>
/// <param name="isCriticalChunk">Whether the chunk to be inflated is a critical chunk.</param> /// <param name="isCriticalChunk">Whether the chunk to be inflated is a critical chunk.</param>
/// <returns>The <see cref="bool"/>.</returns> /// <returns>The <see cref="bool"/>.</returns>
[MemberNotNullWhen(true, nameof(CompressedStream))]
public bool AllocateNewBytes(int bytes, bool isCriticalChunk) public bool AllocateNewBytes(int bytes, bool isCriticalChunk)
{ {
this.currentDataRemaining = bytes; this.currentDataRemaining = bytes;
@ -210,6 +211,7 @@ internal sealed class ZlibInflateStream : Stream
this.isDisposed = true; this.isDisposed = true;
} }
[MemberNotNullWhen(true, nameof(CompressedStream))]
private bool InitializeInflateStream(bool isCriticalChunk) private bool InitializeInflateStream(bool isCriticalChunk)
{ {
// Read the zlib header : http://tools.ietf.org/html/rfc1950 // Read the zlib header : http://tools.ietf.org/html/rfc1950

7
src/ImageSharp/Diagnostics/MemoryDiagnostics.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
namespace SixLabors.ImageSharp.Diagnostics; namespace SixLabors.ImageSharp.Diagnostics;
@ -17,7 +16,7 @@ public static class MemoryDiagnostics
{ {
private static int totalUndisposedAllocationCount; private static int totalUndisposedAllocationCount;
private static UndisposedAllocationDelegate undisposedAllocation; private static UndisposedAllocationDelegate? undisposedAllocation;
private static int undisposedAllocationSubscriptionCounter; private static int undisposedAllocationSubscriptionCounter;
private static readonly object SyncRoot = new(); private static readonly object SyncRoot = new();
@ -50,12 +49,12 @@ public static class MemoryDiagnostics
/// <summary> /// <summary>
/// Fires when ImageSharp allocates memory from a MemoryAllocator /// Fires when ImageSharp allocates memory from a MemoryAllocator
/// </summary> /// </summary>
internal static event Action MemoryAllocated; internal static event Action? MemoryAllocated;
/// <summary> /// <summary>
/// Fires when ImageSharp releases memory allocated from a MemoryAllocator /// Fires when ImageSharp releases memory allocated from a MemoryAllocator
/// </summary> /// </summary>
internal static event Action MemoryReleased; internal static event Action? MemoryReleased;
/// <summary> /// <summary>
/// Gets a value indicating the total number of memory resource objects leaked to the finalizer. /// Gets a value indicating the total number of memory resource objects leaked to the finalizer.

22
src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs

@ -50,19 +50,21 @@ internal sealed class DeflateTiffCompression : TiffBaseDecompressor
return left > 0 ? left : 0; return left > 0 ? left : 0;
})) }))
{ {
deframeStream.AllocateNewBytes(byteCount, true); if (deframeStream.AllocateNewBytes(byteCount, true))
DeflateStream dataStream = deframeStream.CompressedStream;
int totalRead = 0;
while (totalRead < buffer.Length)
{ {
int bytesRead = dataStream.Read(buffer, totalRead, buffer.Length - totalRead); DeflateStream? dataStream = deframeStream.CompressedStream;
if (bytesRead <= 0)
int totalRead = 0;
while (totalRead < buffer.Length)
{ {
break; int bytesRead = dataStream.Read(buffer, totalRead, buffer.Length - totalRead);
} if (bytesRead <= 0)
{
break;
}
totalRead += bytesRead; totalRead += bytesRead;
}
} }
} }

Loading…
Cancel
Save