Code:
#include <stdio.h>
#include <windows.h>
typedef struct BMP { /* i have both BITMAPINFOHEADER and BITMAPCOREHEADER because i don't really know which one bitmaps actually use */
BITMAPFILEHEADER bmfh;
union u1 {
BITMAPINFOHEADER bmih;
BITMAPCOREHEADER bmch;
}u1;
BYTE* bmp_data; //palette and data
};
int getsectorcount(int x,char* str) { /*a function that returns number of sectors in file*sectorsize */
DWORD y[4];
GetDiskFreeSpace(str,&y[0],&y[1],&y[2],&y[3]);
y[0]=x%y[1];
if(y[0]!=0)x+=y[1]-y[0];
return x;
}
int main(int narg, char** arg) {
DWORD i,*x,y;
HANDLE f1,f2;
BMP* b1;
char fname[256],**temp;
GetFullPathName(arg[1],256,fname,temp);
f1=CreateFile(fname,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_NO_BUFFERING,NULL);
if(f1==INVALID_HANDLE_VALUE) { printf("Unable to open file \"%s\"",arg[1]); return -1; };
i=GetFileSize(f1,NULL);
fname[3]='\0';
printf("file size(bytes): %d\nfile size(sectors): %d\n",i,getsectorcount(i,fname));
x=(DWORD*)VirtualAlloc(NULL,i,MEM_COMMIT,PAGE_READWRITE);
ReadFile(f1,x,getsectorcount(i,fname),&y,NULL);
for(y=0;y<i;printf("%02x ",((BYTE*)x)[y++]))if(y%26==0)printf("\n");
b1=(BMP*)x;
printf("\nBitmapfileheader data:\nimage type: %x\nfile size: %d\nreserved value 1: %d\nreserved value 2: %d\nimage data offset: %d\n",b1->bmfh.bfType,b1->bmfh.bfSize);
printf("Bitmapinfoheader data:\nstructure size: %d\nimage height: %d\nimage width: %d\n",b1->u1.bmih.biSize,b1->u1.bmih.biHeight,b1->u1.bmih.biWidth);
printf("Bitmapcoreheader data:\nstructure size: %d\nimage height: %d\nimage width: %d\n",b1->u1.bmch.bcSize,b1->u1.bmch.bcHeight,b1->u1.bmch.bcWidth);
VirtualFree(x,i,MEM_DECOMMIT);
CloseHandle(f1);
return 0;
}
Here is an example of running the program: