Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/ImageSharp/Formats/Tiff/Compression/Decompressors/LzwString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,33 @@ public int WriteTo(Span<byte> buffer, int offset)
return 0;
}

if (this.Length == 1)
int available = buffer.Length - offset;
if (available <= 0)
{
buffer[offset] = this.value;
return 1;
return 0;
}

int numToWrite = this.Length;
if (numToWrite > available)
{
numToWrite = available;
}

LzwString e = this;
int endIdx = this.Length - 1;
if (endIdx >= buffer.Length)

// if string is too long, skip bytes at the end
int toSkip = this.Length - numToWrite;
for (int i = 0; i < toSkip; i++)
{
TiffThrowHelper.ThrowImageFormatException("Error reading lzw compressed stream. Either pixel buffer to write to is to small or code length is invalid!");
e = e.previous;
}

for (int i = endIdx; i >= 0; i--)
for (int i = numToWrite - 1; i >= 0; i--)
{
buffer[offset + i] = e.value;
e = e.previous;
}

return this.Length;
return numToWrite;
}
}
5 changes: 5 additions & 0 deletions tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -833,4 +833,9 @@ public void TiffDecoder_Decode_Resize<TPixel>(TestImageProvider<TPixel> provider
[WithFile(ExtraSamplesUnspecified, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_ExtraSamplesUnspecified<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);

[Theory]
[WithFile(Issue2983, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_Issue2983<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
}
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,7 @@ public static class Tiff
public const string IptcData = "Tiff/iptc.tiff";

public const string Issue2909 = "Tiff/Issues/Issue2909.tiff";
public const string Issue2983 = "Tiff/Issues/Issue2983.tiff";

public static readonly string[] Multiframes = [MultiframeDeflateWithPreview, MultiframeLzwPredictor /*, MultiFrameDifferentSize, MultiframeDifferentSizeTiled, MultiFrameDifferentVariants,*/
];
Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Tiff/Issues/Issue2983.tiff
Git LFS file not shown
Loading