Skip to content

Commit ce4b49f

Browse files
Merge pull request #2325 from stefannikolei/stefannikolei/nullable/zlib
Remove nullable disable from zlib
2 parents ea2e6da + 06f1edf commit ce4b49f

File tree

10 files changed

+33
-43
lines changed

10 files changed

+33
-43
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace SixLabors.ImageSharp.Compression.Zlib;
57

68
internal static class DeflateThrowHelper
79
{
10+
[DoesNotReturn]
811
public static void ThrowAlreadyFinished() => throw new InvalidOperationException("Finish() already called.");
912

13+
[DoesNotReturn]
1014
public static void ThrowAlreadyClosed() => throw new InvalidOperationException("Deflator already closed.");
1115

16+
[DoesNotReturn]
1217
public static void ThrowUnknownCompression() => throw new InvalidOperationException("Unknown compression function.");
1318

19+
[DoesNotReturn]
1420
public static void ThrowNotProcessed() => throw new InvalidOperationException("Old input was not completely processed.");
1521

22+
[DoesNotReturn]
1623
public static void ThrowNull(string name) => throw new ArgumentNullException(name);
1724

25+
[DoesNotReturn]
1826
public static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name);
1927

28+
[DoesNotReturn]
2029
public static void ThrowHeapViolated() => throw new InvalidOperationException("Huffman heap invariant violated.");
2130

31+
[DoesNotReturn]
2232
public static void ThrowNoDeflate() => throw new ImageFormatException("Cannot deflate all input.");
2333
}

src/ImageSharp/Compression/Zlib/Deflater.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Runtime.CompilerServices;
65
using SixLabors.ImageSharp.Memory;
@@ -285,7 +284,6 @@ public void Dispose()
285284
if (!this.isDisposed)
286285
{
287286
this.engine.Dispose();
288-
this.engine = null;
289287
this.isDisposed = true;
290288
}
291289
}

src/ImageSharp/Compression/Zlib/DeflaterEngine.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using System.Runtime.CompilerServices;
@@ -87,7 +86,7 @@ internal sealed unsafe class DeflaterEngine : IDisposable
8786
/// <summary>
8887
/// The input data for compression.
8988
/// </summary>
90-
private byte[] inputBuf;
89+
private byte[]? inputBuf;
9190

9291
/// <summary>
9392
/// The offset into inputBuf, where input data starts.
@@ -222,7 +221,7 @@ public bool Deflate(bool flush, bool finish)
222221
/// <param name="buffer">The buffer containing input data.</param>
223222
/// <param name="offset">The offset of the first byte of data.</param>
224223
/// <param name="count">The number of bytes of data to use as input.</param>
225-
public void SetInput(byte[] buffer, int offset, int count)
224+
public void SetInput(byte[]? buffer, int offset, int count)
226225
{
227226
if (buffer is null)
228227
{
@@ -362,6 +361,8 @@ public void FillWindow()
362361
more = this.inputEnd - this.inputOff;
363362
}
364363

364+
ArgumentNullException.ThrowIfNull(this.inputBuf);
365+
365366
Unsafe.CopyBlockUnaligned(
366367
ref this.window.Span[this.strstart + this.lookahead],
367368
ref this.inputBuf[this.inputOff],
@@ -393,11 +394,6 @@ public void Dispose()
393394
this.prevMemoryHandle.Dispose();
394395
this.prevMemoryOwner.Dispose();
395396

396-
this.windowMemoryOwner = null;
397-
this.headMemoryOwner = null;
398-
this.prevMemoryOwner = null;
399-
this.huffman = null;
400-
401397
this.isDisposed = true;
402398
}
403399
}

src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using System.Runtime.CompilerServices;
@@ -427,10 +426,6 @@ public void Dispose()
427426
this.blTree.Dispose();
428427
this.distTree.Dispose();
429428

430-
this.Pending = null;
431-
this.literalTree = null;
432-
this.blTree = null;
433-
this.distTree = null;
434429
this.isDisposed = true;
435430
}
436431
}
@@ -977,10 +972,6 @@ public void Dispose()
977972
this.codesMemoryHandle.Dispose();
978973
this.codesMemoryOwner.Dispose();
979974

980-
this.frequenciesMemoryOwner = null;
981-
this.lengthsMemoryOwner = null;
982-
this.codesMemoryOwner = null;
983-
984975
this.isDisposed = true;
985976
}
986977
}

src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using SixLabors.ImageSharp.Memory;
@@ -137,8 +136,6 @@ protected override void Dispose(bool disposing)
137136
this.memoryOwner.Dispose();
138137
}
139138

140-
this.deflater = null;
141-
this.memoryOwner = null;
142139
this.isDisposed = true;
143140
base.Dispose(disposing);
144141
}

src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers;
65
using System.Runtime.CompilerServices;
@@ -180,7 +179,6 @@ public void Dispose()
180179
{
181180
this.bufferMemoryHandle.Dispose();
182181
this.bufferMemoryOwner.Dispose();
183-
this.bufferMemoryOwner = null;
184182
this.isDisposed = true;
185183
}
186184
}

src/ImageSharp/Compression/Zlib/ZlibDeflateStream.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Runtime.CompilerServices;
65
using SixLabors.ImageSharp.Formats.Png;
@@ -172,8 +171,6 @@ protected override void Dispose(bool disposing)
172171
this.rawStream.WriteByte((byte)(crc & 0xFF));
173172
}
174173

175-
this.deflateStream = null;
176-
177174
base.Dispose(disposing);
178175
this.isDisposed = true;
179176
}

src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

4+
using System.Diagnostics.CodeAnalysis;
55
using System.IO.Compression;
66
using SixLabors.ImageSharp.IO;
77

@@ -90,14 +90,15 @@ public ZlibInflateStream(BufferedReadStream innerStream, Func<int> getData)
9090
/// <summary>
9191
/// Gets the compressed stream over the deframed inner stream.
9292
/// </summary>
93-
public DeflateStream CompressedStream { get; private set; }
93+
public DeflateStream? CompressedStream { get; private set; }
9494

9595
/// <summary>
9696
/// Adds new bytes from a frame found in the original stream.
9797
/// </summary>
9898
/// <param name="bytes">The current remaining data according to the chunk length.</param>
9999
/// <param name="isCriticalChunk">Whether the chunk to be inflated is a critical chunk.</param>
100100
/// <returns>The <see cref="bool"/>.</returns>
101+
[MemberNotNullWhen(true, nameof(CompressedStream))]
101102
public bool AllocateNewBytes(int bytes, bool isCriticalChunk)
102103
{
103104
this.currentDataRemaining = bytes;
@@ -210,6 +211,7 @@ protected override void Dispose(bool disposing)
210211
this.isDisposed = true;
211212
}
212213

214+
[MemberNotNullWhen(true, nameof(CompressedStream))]
213215
private bool InitializeInflateStream(bool isCriticalChunk)
214216
{
215217
// Read the zlib header : http://tools.ietf.org/html/rfc1950

src/ImageSharp/Diagnostics/MemoryDiagnostics.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
namespace SixLabors.ImageSharp.Diagnostics;
65

@@ -17,7 +16,7 @@ public static class MemoryDiagnostics
1716
{
1817
private static int totalUndisposedAllocationCount;
1918

20-
private static UndisposedAllocationDelegate undisposedAllocation;
19+
private static UndisposedAllocationDelegate? undisposedAllocation;
2120
private static int undisposedAllocationSubscriptionCounter;
2221
private static readonly object SyncRoot = new();
2322

@@ -50,12 +49,12 @@ public static event UndisposedAllocationDelegate UndisposedAllocation
5049
/// <summary>
5150
/// Fires when ImageSharp allocates memory from a MemoryAllocator
5251
/// </summary>
53-
internal static event Action MemoryAllocated;
52+
internal static event Action? MemoryAllocated;
5453

5554
/// <summary>
5655
/// Fires when ImageSharp releases memory allocated from a MemoryAllocator
5756
/// </summary>
58-
internal static event Action MemoryReleased;
57+
internal static event Action? MemoryReleased;
5958

6059
/// <summary>
6160
/// Gets a value indicating the total number of memory resource objects leaked to the finalizer.

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, int
5050
return left > 0 ? left : 0;
5151
}))
5252
{
53-
deframeStream.AllocateNewBytes(byteCount, true);
54-
DeflateStream dataStream = deframeStream.CompressedStream;
55-
56-
int totalRead = 0;
57-
while (totalRead < buffer.Length)
53+
if (deframeStream.AllocateNewBytes(byteCount, true))
5854
{
59-
int bytesRead = dataStream.Read(buffer, totalRead, buffer.Length - totalRead);
60-
if (bytesRead <= 0)
55+
DeflateStream? dataStream = deframeStream.CompressedStream;
56+
57+
int totalRead = 0;
58+
while (totalRead < buffer.Length)
6159
{
62-
break;
63-
}
60+
int bytesRead = dataStream.Read(buffer, totalRead, buffer.Length - totalRead);
61+
if (bytesRead <= 0)
62+
{
63+
break;
64+
}
6465

65-
totalRead += bytesRead;
66+
totalRead += bytesRead;
67+
}
6668
}
6769
}
6870

0 commit comments

Comments
 (0)