Skip to content

Commit 637f767

Browse files
brianpopowJimBobSquarePants
authored andcommitted
Respecting fileHeader Offset, skipping bytes if necessary (#819)
1 parent 6a60638 commit 637f767

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,17 @@ private int ReadImageHeaders(Stream stream, out bool inverted, out byte[] palett
11031103

11041104
this.infoHeader.VerifyDimensions();
11051105

1106+
int skipAmount = this.fileHeader.Offset - (int)this.stream.Position;
1107+
if ((skipAmount + (int)this.stream.Position) > this.stream.Length)
1108+
{
1109+
BmpThrowHelper.ThrowImageFormatException($"Invalid fileheader offset found. Offset is greater than the stream length.");
1110+
}
1111+
1112+
if (skipAmount > 0)
1113+
{
1114+
this.stream.Skip(skipAmount);
1115+
}
1116+
11061117
return bytesPerColorMapEntry;
11071118
}
11081119
}

tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ public void BmpDecoder_CanDecodeBmpv5<TPixel>(TestImageProvider<TPixel> provider
150150
}
151151
}
152152

153+
[Theory]
154+
[WithFile(Pal8Offset, PixelTypes.Rgba32)]
155+
public void BmpDecoder_RespectsFileHeaderOffset<TPixel>(TestImageProvider<TPixel> provider)
156+
where TPixel : struct, IPixel<TPixel>
157+
{
158+
using (Image<TPixel> image = provider.GetImage(new BmpDecoder()))
159+
{
160+
image.DebugSave(provider);
161+
image.CompareToOriginal(provider);
162+
}
163+
}
164+
153165
[Theory]
154166
[WithFile(F, CommonNonDefaultPixelTypes)]
155167
public void BmpDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ public static class Bmp
218218
public const string Bit8Palette4 = "Bmp/pal8-0.bmp";
219219
public const string Os2v2Short = "Bmp/pal8os2v2-16.bmp";
220220
public const string Os2v2 = "Bmp/pal8os2v2.bmp";
221+
public const string Pal8Offset = "Bmp/pal8offs.bmp";
221222

222223
// Bitmap images with compression type BITFIELDS
223224
public const string Rgb32bfdef = "Bmp/rgb32bfdef.bmp";
9.13 KB
Binary file not shown.

0 commit comments

Comments
 (0)