开发者

C++: Check if bmp file is legit

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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜