Browse Source

Respecting fileHeader Offset, skipping bytes if necessary (#819)

pull/831/head
Brian Popow 7 years ago
committed by James Jackson-South
parent
commit
637f76707b
  1. 11
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  2. 12
      tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. BIN
      tests/Images/Input/Bmp/pal8offs.bmp

11
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -1103,6 +1103,17 @@ namespace SixLabors.ImageSharp.Formats.Bmp
this.infoHeader.VerifyDimensions();
int skipAmount = this.fileHeader.Offset - (int)this.stream.Position;
if ((skipAmount + (int)this.stream.Position) > this.stream.Length)
{
BmpThrowHelper.ThrowImageFormatException($"Invalid fileheader offset found. Offset is greater than the stream length.");
}
if (skipAmount > 0)
{
this.stream.Skip(skipAmount);
}
return bytesPerColorMapEntry;
}
}

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

@ -150,6 +150,18 @@ namespace SixLabors.ImageSharp.Tests
}
}
[Theory]
[WithFile(Pal8Offset, PixelTypes.Rgba32)]
public void BmpDecoder_RespectsFileHeaderOffset<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(new BmpDecoder()))
{
image.DebugSave(provider);
image.CompareToOriginal(provider);
}
}
[Theory]
[WithFile(F, CommonNonDefaultPixelTypes)]
public void BmpDecoder_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider)

1
tests/ImageSharp.Tests/TestImages.cs

@ -218,6 +218,7 @@ namespace SixLabors.ImageSharp.Tests
public const string Bit8Palette4 = "Bmp/pal8-0.bmp";
public const string Os2v2Short = "Bmp/pal8os2v2-16.bmp";
public const string Os2v2 = "Bmp/pal8os2v2.bmp";
public const string Pal8Offset = "Bmp/pal8offs.bmp";
// Bitmap images with compression type BITFIELDS
public const string Rgb32bfdef = "Bmp/rgb32bfdef.bmp";

BIN
tests/Images/Input/Bmp/pal8offs.bmp

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Loading…
Cancel
Save