Added basic BMP detection & extraction #822
Open
+211
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds basic support for detecting and extracting BMP (Bitmap) images as specified here:
https://learn.microsoft.com/en-us/windows/win32/gdi/bitmap-header-types
It supports bitmaps with the following DIB headers:
The magic value of bitmaps is "BM", so parsing and validation gets triggered quite often. To prevent a major performance impact, I tried to keep the validation as simple as possible:
The first header is used to perform boundary checks, which are the majority of the validation.
The size of the second header, which can be, if valid, on of the DIB headers above, is then checked. This is the recommended way of determining the type of the Header, as mentioned in the Microsoft documentation:
"The number of bytes required by the structure. Applications should use this member to determine which bitmap information header structure is being used."
Testing
To test it, I used a Lenovo BIOS image from a project I am working on which contains bitmaps and a lot of occurrences of "BM". I trimmed the image down to the part that contains the bitmaps and added it as a unit test:
As a false positive, I modified the BIOS image so that the DIB header has an invalid size:
Valid bitmap with 40 (0x28) byte DIB header:
Invalid bitmap with 0x42 byte DIB header:
The invalid image did not get detected:
Thanks!