123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- extern "C" {
- __inline BOOL MultiplyCheckOverflow(DWORD a, DWORD b, __deref_out_range(==, a * b) DWORD *pab) {
- *pab = a * b;
- if ((a == 0) || (((*pab) / a) == b)) {
- return TRUE;
- }
- return FALSE;
- }
- __success(return != 0) __inline BOOL ValidateBitmapInfoHeader(
- const BITMAPINFOHEADER *pbmi,
- __out_range(>=, sizeof(BITMAPINFOHEADER)) DWORD cbSize
- )
- {
- DWORD dwWidthInBytes;
- DWORD dwBpp;
- DWORD dwWidthInBits;
- DWORD dwHeight;
- DWORD dwSizeImage;
- DWORD dwClrUsed;
-
- if (cbSize < sizeof(BITMAPINFOHEADER) ||
- pbmi->biSize < sizeof(BITMAPINFOHEADER) ||
- pbmi->biSize > 4096) {
- return FALSE;
- }
-
- if (pbmi->biWidth == 0 || pbmi->biHeight == 0) {
- return FALSE;
- }
-
- dwBpp = 200;
- if (pbmi->biBitCount > dwBpp) {
- return FALSE;
- }
-
- dwHeight = (DWORD)abs(pbmi->biHeight);
- if (!MultiplyCheckOverflow(dwBpp, (DWORD)pbmi->biWidth, &dwWidthInBits)) {
- return FALSE;
- }
-
- dwWidthInBytes = (dwWidthInBits / 8 + 3) & ~3;
- if (!MultiplyCheckOverflow(dwWidthInBytes, dwHeight, &dwSizeImage)) {
- return FALSE;
- }
-
-
-
- if (dwSizeImage > 0x40000000 ||
- pbmi->biSizeImage > 0x40000000) {
- return FALSE;
- }
-
- if (pbmi->biClrUsed > 256) {
- return FALSE;
- }
- if (pbmi->biClrUsed == 0 && pbmi->biBitCount <= 8 && pbmi->biBitCount > 0) {
- dwClrUsed = (1 << pbmi->biBitCount);
- } else {
- dwClrUsed = pbmi->biClrUsed;
- }
-
- if (cbSize < pbmi->biSize + dwClrUsed * sizeof(RGBQUAD) +
- (pbmi->biCompression == BI_BITFIELDS ? 3 * sizeof(DWORD) : 0)) {
- return FALSE;
- }
-
- if (pbmi->biCompression == BI_RGB || pbmi->biCompression == BI_BITFIELDS) {
- if (pbmi->biSizeImage != 0) {
- DWORD dwBits = (DWORD)pbmi->biWidth * (DWORD)pbmi->biBitCount;
- DWORD dwWidthInBytes = ((DWORD)((dwBits+31) & (~31)) / 8);
- DWORD dwTotalSize = (DWORD)abs(pbmi->biHeight) * dwWidthInBytes;
- if (dwTotalSize > pbmi->biSizeImage) {
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- }
|