Skip to content

Commit

Permalink
More early returns and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Nov 29, 2023
1 parent 637a109 commit 091cb1e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
33 changes: 22 additions & 11 deletions src/ImageSharp/Formats/Png/PngDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ private void ReadCompressedTextChunk(ImageMetadata baseMetadata, PngMetadata met

ReadOnlySpan<byte> compressedData = data[(zeroIndex + 2)..];

if (this.TryUncompressTextData(compressedData, PngConstants.Encoding, out string? uncompressed)
if (this.TryDecompressTextData(compressedData, PngConstants.Encoding, out string? uncompressed)
&& !TryReadTextChunkMetadata(baseMetadata, name, uncompressed))
{
metadata.TextData.Add(new PngTextData(name, uncompressed, string.Empty, string.Empty));
Expand Down Expand Up @@ -1539,19 +1539,19 @@ private void ReadColorProfileChunk(ImageMetadata metadata, ReadOnlySpan<byte> da

ReadOnlySpan<byte> compressedData = data[(zeroIndex + 2)..];

if (this.TryUncompressZlibData(compressedData, out byte[] iccpProfileBytes))
if (this.TryDecompressZlibData(compressedData, out byte[] iccpProfileBytes))
{
metadata.IccProfile = new IccProfile(iccpProfileBytes);
}
}

/// <summary>
/// Tries to un-compress zlib compressed data.
/// Tries to decompress zlib compressed data.
/// </summary>
/// <param name="compressedData">The compressed data.</param>
/// <param name="uncompressedBytesArray">The uncompressed bytes array.</param>
/// <returns>True, if de-compressing was successful.</returns>
private unsafe bool TryUncompressZlibData(ReadOnlySpan<byte> compressedData, out byte[] uncompressedBytesArray)
private unsafe bool TryDecompressZlibData(ReadOnlySpan<byte> compressedData, out byte[] uncompressedBytesArray)
{
fixed (byte* compressedDataBase = compressedData)
{
Expand Down Expand Up @@ -1688,7 +1688,7 @@ private void ReadInternationalTextChunk(ImageMetadata metadata, ReadOnlySpan<byt
{
ReadOnlySpan<byte> compressedData = data[dataStartIdx..];

if (this.TryUncompressTextData(compressedData, PngConstants.TranslatedEncoding, out string? uncompressed))
if (this.TryDecompressTextData(compressedData, PngConstants.TranslatedEncoding, out string? uncompressed))
{
pngMetadata.TextData.Add(new PngTextData(keyword, uncompressed, language, translatedKeyword));
}
Expand All @@ -1711,9 +1711,9 @@ private void ReadInternationalTextChunk(ImageMetadata metadata, ReadOnlySpan<byt
/// <param name="encoding">The string encoding to use.</param>
/// <param name="value">The uncompressed value.</param>
/// <returns>The <see cref="bool"/>.</returns>
private bool TryUncompressTextData(ReadOnlySpan<byte> compressedData, Encoding encoding, [NotNullWhen(true)] out string? value)
private bool TryDecompressTextData(ReadOnlySpan<byte> compressedData, Encoding encoding, [NotNullWhen(true)] out string? value)
{
if (this.TryUncompressZlibData(compressedData, out byte[] uncompressedData))
if (this.TryDecompressZlibData(compressedData, out byte[] uncompressedData))
{
value = encoding.GetString(uncompressedData);
return true;
Expand All @@ -1736,7 +1736,11 @@ private int ReadNextDataChunk()

Span<byte> buffer = stackalloc byte[20];

_ = this.currentStream.Read(buffer, 0, 4);
int length = this.currentStream.Read(buffer, 0, 4);
if (length == 0)
{
return 0;
}

if (this.TryReadChunk(buffer, out PngChunk chunk))
{
Expand Down Expand Up @@ -1765,7 +1769,11 @@ private int ReadNextFrameDataChunk()

Span<byte> buffer = stackalloc byte[20];

_ = this.currentStream.Read(buffer, 0, 4);
int length = this.currentStream.Read(buffer, 0, 4);
if (length == 0)
{
return 0;
}

if (this.TryReadChunk(buffer, out PngChunk chunk))
{
Expand Down Expand Up @@ -1802,8 +1810,9 @@ private bool TryReadChunk(Span<byte> buffer, out PngChunk chunk)
return true;
}

if (this.currentStream.Position == this.currentStream.Length)
if (this.currentStream.Position >= this.currentStream.Length - 1)
{
// IEND
chunk = default;
return false;
}
Expand All @@ -1820,6 +1829,7 @@ private bool TryReadChunk(Span<byte> buffer, out PngChunk chunk)
// Not a valid chunk so try again until we reach a known chunk.
if (!this.TryReadChunkLength(buffer, out length))
{
// IEND
chunk = default;
return false;
}
Expand All @@ -1832,10 +1842,11 @@ private bool TryReadChunk(Span<byte> buffer, out PngChunk chunk)
if (this.colorMetadataOnly && type != PngChunkType.Header && type != PngChunkType.Transparency && type != PngChunkType.Palette)
{
chunk = new PngChunk(length, type);

return true;
}

// A chunk might report a length that exceeds the length of the stream.
// Take the minimum of the two values to ensure we don't read past the end of the stream.
long position = this.currentStream.Position;
chunk = new PngChunk(
length: (int)Math.Min(length, this.currentStream.Length - position),
Expand Down
4 changes: 2 additions & 2 deletions tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,8 @@ public void Binary_PrematureEof()
PngDecoderOptions options = new() { PngCrcChunkHandling = PngCrcChunkHandling.IgnoreData };
using EofHitCounter eofHitCounter = EofHitCounter.RunDecoder(TestImages.Png.Bad.FlagOfGermany0000016446, decoder, options);

// TODO: Undo this.
Assert.True(eofHitCounter.EofHitCount <= 6);
// TODO: Try to reduce this to 1.
Assert.True(eofHitCounter.EofHitCount <= 3);
Assert.Equal(new Size(200, 120), eofHitCounter.Image.Size);
}
}

0 comments on commit 091cb1e

Please sign in to comment.