diff --git a/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs b/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs index f05f237576..06a7c3928c 100644 --- a/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs +++ b/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs @@ -161,11 +161,6 @@ public override int Read(byte[] buffer, int offset, int count) bytesToRead = Math.Min(count - totalBytesRead, this.currentDataRemaining); this.currentDataRemaining -= bytesToRead; bytesRead = this.innerStream.Read(buffer, offset, bytesToRead); - if (bytesRead == 0) - { - return totalBytesRead; - } - totalBytesRead += bytesRead; } @@ -173,13 +168,22 @@ public override int Read(byte[] buffer, int offset, int count) } /// - public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotSupportedException(); + } /// - public override void SetLength(long value) => throw new NotSupportedException(); + public override void SetLength(long value) + { + throw new NotSupportedException(); + } /// - public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); + public override void Write(byte[] buffer, int offset, int count) + { + throw new NotSupportedException(); + } /// protected override void Dispose(bool disposing) @@ -242,17 +246,22 @@ private bool InitializeInflateStream(bool isCriticalChunk) // CINFO is not defined in this specification for CM not equal to 8. throw new ImageFormatException($"Invalid window size for ZLIB header: cinfo={cinfo}"); } - - return false; + else + { + return false; + } } } - else if (isCriticalChunk) - { - throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}"); - } else { - return false; + if (isCriticalChunk) + { + throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}"); + } + else + { + return false; + } } // The preset dictionary. @@ -261,11 +270,7 @@ private bool InitializeInflateStream(bool isCriticalChunk) { // We don't need this for inflate so simply skip by the next four bytes. // https://tools.ietf.org/html/rfc1950#page-6 - if (this.innerStream.Read(ChecksumBuffer, 0, 4) != 4) - { - return false; - } - + this.innerStream.Read(ChecksumBuffer, 0, 4); this.currentDataRemaining -= 4; } diff --git a/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs b/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs index 086eef0585..755e79e42e 100644 --- a/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs +++ b/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs @@ -40,7 +40,7 @@ public DirectoryReader(Stream stream, MemoryAllocator allocator) public IList Read() { this.ByteOrder = ReadByteOrder(this.stream); - HeaderReader headerReader = new(this.stream, this.ByteOrder); + var headerReader = new HeaderReader(this.stream, this.ByteOrder); headerReader.ReadFileHeader(); this.nextIfdOffset = headerReader.FirstIfdOffset; @@ -52,12 +52,7 @@ public IList Read() private static ByteOrder ReadByteOrder(Stream stream) { Span headerBytes = stackalloc byte[2]; - - if (stream.Read(headerBytes) != 2) - { - throw TiffThrowHelper.ThrowInvalidHeader(); - } - + stream.Read(headerBytes); if (headerBytes[0] == TiffConstants.ByteOrderLittleEndian && headerBytes[1] == TiffConstants.ByteOrderLittleEndian) { return ByteOrder.LittleEndian; @@ -73,10 +68,10 @@ private static ByteOrder ReadByteOrder(Stream stream) private IList ReadIfds(bool isBigTiff) { - List readers = new(); + var readers = new List(); while (this.nextIfdOffset != 0 && this.nextIfdOffset < (ulong)this.stream.Length) { - EntryReader reader = new(this.stream, this.ByteOrder, this.allocator); + var reader = new EntryReader(this.stream, this.ByteOrder, this.allocator); reader.ReadTags(isBigTiff, this.nextIfdOffset); if (reader.BigValues.Count > 0) @@ -90,11 +85,6 @@ private IList ReadIfds(bool isBigTiff) } } - if (this.nextIfdOffset >= reader.NextIfdOffset && reader.NextIfdOffset != 0) - { - TiffThrowHelper.ThrowImageFormatException("TIFF image contains circular directory offsets"); - } - this.nextIfdOffset = reader.NextIfdOffset; readers.Add(reader); @@ -104,11 +94,11 @@ private IList ReadIfds(bool isBigTiff) } } - List list = new(readers.Count); + var list = new List(readers.Count); foreach (EntryReader reader in readers) { reader.ReadBigValues(); - ExifProfile profile = new(reader.Values, reader.InvalidTags); + var profile = new ExifProfile(reader.Values, reader.InvalidTags); list.Add(profile); } diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs index 1f01cbbd8b..72b87bcf02 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs @@ -3,7 +3,6 @@ // ReSharper disable InconsistentNaming using System.Runtime.InteropServices; -using System.Runtime.Intrinsics.X86; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Tiff; using SixLabors.ImageSharp.Metadata; @@ -687,33 +686,6 @@ public void TiffDecoder_ThrowsException_WithTooManyDirectories(TestImage public void TiffDecoder_CanDecode_Fax4CompressedWithStrips(TestImageProvider provider) where TPixel : unmanaged, IPixel => TestTiffDecoder(provider); - - [Theory] - [WithFile(JpegCompressedGray0000539558, PixelTypes.Rgba32)] - public void TiffDecoder_ThrowsException_WithCircular_IFD_Offsets(TestImageProvider provider) - where TPixel : unmanaged, IPixel - => Assert.Throws( - () => - { - using (provider.GetImage(TiffDecoder.Instance)) - { - } - }); - - [Theory] - [WithFile(Tiled0000023664, PixelTypes.Rgba32)] - public void TiffDecoder_CanDecode_TiledWithBadZlib(TestImageProvider provider) - where TPixel : unmanaged, IPixel - { - using Image image = provider.GetImage(TiffDecoder.Instance); - - // ImageMagick cannot decode this image. - image.DebugSave(provider); - image.CompareToReferenceOutput( - ImageComparer.Exact, - provider, - appendPixelTypeToFileName: false); - } [Theory] [WithFileCollection(nameof(MultiframeTestImages), PixelTypes.Rgba32)] public void DecodeMultiframe(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index de3b3dc572..26d91cbc08 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -965,8 +965,6 @@ public static class Tiff public const string Issues2123 = "Tiff/Issues/Issue2123.tiff"; public const string Issues2149 = "Tiff/Issues/Group4CompressionWithStrips.tiff"; public const string Issues2255 = "Tiff/Issues/Issue2255.png"; - public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff"; - public const string Tiled0000023664 = "Tiff/Issues/tiled-0000023664.tiff"; public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff"; public const string SmallRgbLzw = "Tiff/rgb_small_lzw.tiff"; diff --git a/tests/Images/External/ReferenceOutput/TiffDecoderTests/TiffDecoder_CanDecode_TiledWithBadZlib_tiled-0000023664.png b/tests/Images/External/ReferenceOutput/TiffDecoderTests/TiffDecoder_CanDecode_TiledWithBadZlib_tiled-0000023664.png deleted file mode 100644 index d93f6ef3cd..0000000000 --- a/tests/Images/External/ReferenceOutput/TiffDecoderTests/TiffDecoder_CanDecode_TiledWithBadZlib_tiled-0000023664.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:456f0699fbba95953fbdba0164168583cc7d2efe1f858a6570938e8797b398cd -size 15586 diff --git a/tests/Images/Input/Tiff/Issues/JpegCompressedGray-0000539558.tiff b/tests/Images/Input/Tiff/Issues/JpegCompressedGray-0000539558.tiff deleted file mode 100644 index 934bf3c9a3..0000000000 --- a/tests/Images/Input/Tiff/Issues/JpegCompressedGray-0000539558.tiff +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:1f1ca630b5e46c7b5f21100fa8c0fbf27b79ca9da8cd95897667b64aedccf6e5 -size 539558 diff --git a/tests/Images/Input/Tiff/Issues/tiled-0000023664.tiff b/tests/Images/Input/Tiff/Issues/tiled-0000023664.tiff deleted file mode 100644 index 5106a027cc..0000000000 --- a/tests/Images/Input/Tiff/Issues/tiled-0000023664.tiff +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:eb28a028b2467b9b42451d9cb30d8170fd91ff4c4046b69cc1ae7f123bf7ba6f -size 23664