HINSTANCE hInst;
char wcPath[MAX_PATH]={0};
char Crc32Table[256]={0};
void InitCrc32(void)
{
UINT Value=0;
UINT Count=0;
for
(UINT i=0;i<1024;i+=4,Count++)
{
Value=Count;
for
(int j=8;j!=0;j--)
{
if
(((BYTE)Value)&1)
Value=(Value>>1)^0xEDB88320;
else
Value=(Value>>1);
}
*(DWORD*)(Crc32Table+i)=Value;
}
}
UINT CalculateCrc32(char *Buffer,int Size)
{
UINT Value=0xffffffff;
BYTE WChar;
for
(int i=0;i<Size;i++)
{
WChar=Buffer[i];
UINT Key=(Value&0xff)^WChar;
Value=(Value>>8)^*(DWORD*)(Crc32Table+(Key*4));
}
return
~Value;
}
void CalculateBinary(UINT Key,char *Buffer)
{
UINT Value;
for
(unsigned char i=0;i<32;i++)
{
Value=(Key<<i)&0x80000000;
if
(Value!=0x80000000)
{
*Buffer=0x30;
}
else
{
*Buffer=0x31;
}
Buffer++;
}
*Buffer=0x00;
}
BOOL GetOpenFile(HWND hWnd)
{
OPENFILENAME FileName = {0,0,0};
TCHAR szPe[]=
"Target(*.exe)\0*.exe\0Target(*.dll)\0*.dll\0"
;
FileName.hInstance = (HINSTANCE)hWnd;
FileName.hwndOwner = hWnd;
FileName.lStructSize = sizeof(OPENFILENAME);
FileName.lpstrFilter = szPe;
FileName.lpstrFile = wcPath;
FileName.Flags = OFN_FILEMUSTEXIST||OFN_PATHMUSTEXIST;
FileName.nMaxFile = sizeof(wcPath);
if
(!GetOpenFileName(&FileName))
{
return
FALSE;
}
return
TRUE;
}
int ButtonProc(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
char *lpText=0x00;
UINT Size=0;
DWORD dwRead=0;
HANDLE hFile=NULL;
char Crc32[32]={0};
DWORD Crc=0;
switch(LOWORD(wParam))
{
case
IDBROWSE:
GetOpenFile(hWnd);
SetDlgItemText(hWnd,IDPATH,wcPath);
break
;
case
IDEDIT:
Size=SendMessage(GetDlgItem(hWnd,IDEDIT),WM_GETTEXTLENGTH,0,0);
lpText=(char*)malloc(Size);
GetDlgItemText(hWnd,IDEDIT,lpText,Size+1);
InitCrc32();
Crc=CalculateCrc32(lpText,Size);
wsprintf(Crc32,
"%08X"
,Crc);
SetDlgItemText(hWnd,ID_CRC32,Crc32);
CalculateBinary(Crc,Crc32);
SetDlgItemText(hWnd,ID_BIN,Crc32);
break
;
case
IDOK:
hFile=CreateFile(wcPath,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
Size=GetFileSize(hFile,NULL);
lpText=(char*)malloc(Size);
ReadFile(hFile,lpText,Size,&dwRead,NULL);
InitCrc32();
Crc=CalculateCrc32(lpText,Size);
wsprintf(Crc32,
"%08X"
,Crc);
SetDlgItemText(hWnd,IDCRC32,Crc32);
CalculateBinary(Crc,Crc32);
SetDlgItemText(hWnd,IDBIN,Crc32);
break
;
}
return
0;
}
BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDROP hDrop;
HICON hIcon;
switch(uMsg)
{
case
WM_INITDIALOG:
hIcon = LoadIcon(hInst,(LPCTSTR)IDI_ICON);
SendMessage(hWnd,WM_SETICON,TRUE,(WPARAM)hIcon);
DragAcceptFiles(hWnd,TRUE);
break
;
case
WM_CLOSE:
EndDialog(hWnd, 0);
break
;
case
WM_COMMAND:
ButtonProc(hWnd,wParam,lParam);
break
;
case
WM_DROPFILES:
hDrop = ( HDROP )wParam;
UINT nFile = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
for
(int i=0;i<nFile;i++)
{
DragQueryFile(hDrop,i, wcPath, sizeof(wcPath));
SetDlgItemText(hWnd,IDPATH,wcPath);
}
DragFinish(hDrop);
break
;
}
return
FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
return
DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, DialogProc);
}