tags:

views:

66

answers:

3

Hi guys and gals! I'm using C++ (Visual Studio) and I want to check if a .bmp file is legit (not some renamed virus.exe) before the user can share it over the internet with other users using my application. I'm using DirectX 2d rendering and boost framework.

Is there an (easy?) way to validate bitmaps?

Thanks.

+2  A: 

You could always check the bitmap header to ensure that it is valid. The format of the bitmap header can be found here

Jim
That's a good idea. I hope that DirectX just ignores the file if there is garbage after the header.
Eliasdx
A: 

Check this library: http://easybmp.sourceforge.net/

You can use this function:

bool ReadFromFile(char* FileName). 

It will return false if the given file is not a valid bmp file. Unfortunately, this is going to load the whole file into the memory and you only need to test the header. You can test the header manually, or find some API that will do that for you.

Hope it helps.

edit: Check this link too: http://www.gamedev.net/reference/articles/article1966.asp

Klark
+1  A: 

You can try loading the bitmap with Win32 LoadImage, which should fail for malformed bitmaps.

As others have mentioned, you can check the bitmap header and sanity check things like the file size (based on what you find in the header). That would be faster than LoadImage, but it's a lot of code to write and test. There are many legal variations of the bitmap header.

Presumably, LoadImage is well tested since it has been around forever.

Adrian McCarthy
Header not corresponding to the rest of the file does not necessarily mean the file is a virus. It could just be a malformed image file, which some applications have no problem with and others reject.
UncleBens
@UncleBens: True, but the question was posed as "Is there an (easy) way to validate bitmaps?".
Adrian McCarthy