Skip to content

Commit d0a024f

Browse files
committed
Revert "Tiff decoding robustness improvements (#2550)"
(accidental push to unprotected branch)
1 parent f65ffaa commit d0a024f

File tree

7 files changed

+31
-75
lines changed

7 files changed

+31
-75
lines changed

src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,29 @@ public override int Read(byte[] buffer, int offset, int count)
161161
bytesToRead = Math.Min(count - totalBytesRead, this.currentDataRemaining);
162162
this.currentDataRemaining -= bytesToRead;
163163
bytesRead = this.innerStream.Read(buffer, offset, bytesToRead);
164-
if (bytesRead == 0)
165-
{
166-
return totalBytesRead;
167-
}
168-
169164
totalBytesRead += bytesRead;
170165
}
171166

172167
return totalBytesRead;
173168
}
174169

175170
/// <inheritdoc/>
176-
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
171+
public override long Seek(long offset, SeekOrigin origin)
172+
{
173+
throw new NotSupportedException();
174+
}
177175

178176
/// <inheritdoc/>
179-
public override void SetLength(long value) => throw new NotSupportedException();
177+
public override void SetLength(long value)
178+
{
179+
throw new NotSupportedException();
180+
}
180181

181182
/// <inheritdoc/>
182-
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
183+
public override void Write(byte[] buffer, int offset, int count)
184+
{
185+
throw new NotSupportedException();
186+
}
183187

184188
/// <inheritdoc/>
185189
protected override void Dispose(bool disposing)
@@ -242,17 +246,22 @@ private bool InitializeInflateStream(bool isCriticalChunk)
242246
// CINFO is not defined in this specification for CM not equal to 8.
243247
throw new ImageFormatException($"Invalid window size for ZLIB header: cinfo={cinfo}");
244248
}
245-
246-
return false;
249+
else
250+
{
251+
return false;
252+
}
247253
}
248254
}
249-
else if (isCriticalChunk)
250-
{
251-
throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}");
252-
}
253255
else
254256
{
255-
return false;
257+
if (isCriticalChunk)
258+
{
259+
throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}");
260+
}
261+
else
262+
{
263+
return false;
264+
}
256265
}
257266

258267
// The preset dictionary.
@@ -261,11 +270,7 @@ private bool InitializeInflateStream(bool isCriticalChunk)
261270
{
262271
// We don't need this for inflate so simply skip by the next four bytes.
263272
// https://tools.ietf.org/html/rfc1950#page-6
264-
if (this.innerStream.Read(ChecksumBuffer, 0, 4) != 4)
265-
{
266-
return false;
267-
}
268-
273+
this.innerStream.Read(ChecksumBuffer, 0, 4);
269274
this.currentDataRemaining -= 4;
270275
}
271276

src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public DirectoryReader(Stream stream, MemoryAllocator allocator)
4040
public IList<ExifProfile> Read()
4141
{
4242
this.ByteOrder = ReadByteOrder(this.stream);
43-
HeaderReader headerReader = new(this.stream, this.ByteOrder);
43+
var headerReader = new HeaderReader(this.stream, this.ByteOrder);
4444
headerReader.ReadFileHeader();
4545

4646
this.nextIfdOffset = headerReader.FirstIfdOffset;
@@ -52,12 +52,7 @@ public IList<ExifProfile> Read()
5252
private static ByteOrder ReadByteOrder(Stream stream)
5353
{
5454
Span<byte> headerBytes = stackalloc byte[2];
55-
56-
if (stream.Read(headerBytes) != 2)
57-
{
58-
throw TiffThrowHelper.ThrowInvalidHeader();
59-
}
60-
55+
stream.Read(headerBytes);
6156
if (headerBytes[0] == TiffConstants.ByteOrderLittleEndian && headerBytes[1] == TiffConstants.ByteOrderLittleEndian)
6257
{
6358
return ByteOrder.LittleEndian;
@@ -73,10 +68,10 @@ private static ByteOrder ReadByteOrder(Stream stream)
7368

7469
private IList<ExifProfile> ReadIfds(bool isBigTiff)
7570
{
76-
List<EntryReader> readers = new();
71+
var readers = new List<EntryReader>();
7772
while (this.nextIfdOffset != 0 && this.nextIfdOffset < (ulong)this.stream.Length)
7873
{
79-
EntryReader reader = new(this.stream, this.ByteOrder, this.allocator);
74+
var reader = new EntryReader(this.stream, this.ByteOrder, this.allocator);
8075
reader.ReadTags(isBigTiff, this.nextIfdOffset);
8176

8277
if (reader.BigValues.Count > 0)
@@ -90,11 +85,6 @@ private IList<ExifProfile> ReadIfds(bool isBigTiff)
9085
}
9186
}
9287

93-
if (this.nextIfdOffset >= reader.NextIfdOffset && reader.NextIfdOffset != 0)
94-
{
95-
TiffThrowHelper.ThrowImageFormatException("TIFF image contains circular directory offsets");
96-
}
97-
9888
this.nextIfdOffset = reader.NextIfdOffset;
9989
readers.Add(reader);
10090

@@ -104,11 +94,11 @@ private IList<ExifProfile> ReadIfds(bool isBigTiff)
10494
}
10595
}
10696

107-
List<ExifProfile> list = new(readers.Count);
97+
var list = new List<ExifProfile>(readers.Count);
10898
foreach (EntryReader reader in readers)
10999
{
110100
reader.ReadBigValues();
111-
ExifProfile profile = new(reader.Values, reader.InvalidTags);
101+
var profile = new ExifProfile(reader.Values, reader.InvalidTags);
112102
list.Add(profile);
113103
}
114104

tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
// ReSharper disable InconsistentNaming
55
using System.Runtime.InteropServices;
6-
using System.Runtime.Intrinsics.X86;
76
using SixLabors.ImageSharp.Formats;
87
using SixLabors.ImageSharp.Formats.Tiff;
98
using SixLabors.ImageSharp.Metadata;
@@ -687,33 +686,6 @@ public void TiffDecoder_ThrowsException_WithTooManyDirectories<TPixel>(TestImage
687686
public void TiffDecoder_CanDecode_Fax4CompressedWithStrips<TPixel>(TestImageProvider<TPixel> provider)
688687
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
689688

690-
691-
[Theory]
692-
[WithFile(JpegCompressedGray0000539558, PixelTypes.Rgba32)]
693-
public void TiffDecoder_ThrowsException_WithCircular_IFD_Offsets<TPixel>(TestImageProvider<TPixel> provider)
694-
where TPixel : unmanaged, IPixel<TPixel>
695-
=> Assert.Throws<ImageFormatException>(
696-
() =>
697-
{
698-
using (provider.GetImage(TiffDecoder.Instance))
699-
{
700-
}
701-
});
702-
703-
[Theory]
704-
[WithFile(Tiled0000023664, PixelTypes.Rgba32)]
705-
public void TiffDecoder_CanDecode_TiledWithBadZlib<TPixel>(TestImageProvider<TPixel> provider)
706-
where TPixel : unmanaged, IPixel<TPixel>
707-
{
708-
using Image<TPixel> image = provider.GetImage(TiffDecoder.Instance);
709-
710-
// ImageMagick cannot decode this image.
711-
image.DebugSave(provider);
712-
image.CompareToReferenceOutput(
713-
ImageComparer.Exact,
714-
provider,
715-
appendPixelTypeToFileName: false);
716-
}
717689
[Theory]
718690
[WithFileCollection(nameof(MultiframeTestImages), PixelTypes.Rgba32)]
719691
public void DecodeMultiframe<TPixel>(TestImageProvider<TPixel> provider)

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,6 @@ public static class Tiff
965965
public const string Issues2123 = "Tiff/Issues/Issue2123.tiff";
966966
public const string Issues2149 = "Tiff/Issues/Group4CompressionWithStrips.tiff";
967967
public const string Issues2255 = "Tiff/Issues/Issue2255.png";
968-
public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff";
969-
public const string Tiled0000023664 = "Tiff/Issues/tiled-0000023664.tiff";
970968

971969
public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff";
972970
public const string SmallRgbLzw = "Tiff/rgb_small_lzw.tiff";

tests/Images/External/ReferenceOutput/TiffDecoderTests/TiffDecoder_CanDecode_TiledWithBadZlib_tiled-0000023664.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/Images/Input/Tiff/Issues/JpegCompressedGray-0000539558.tiff

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/Images/Input/Tiff/Issues/tiled-0000023664.tiff

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)