CRC32校验
{
DWORD crcTable[256],crcTmp1;
//动态生成CRC-32表
for (int i=0; i<256; i++)
{
crcTmp1 = i;
for (int j=8; j>0; j--)
{
if (crcTmp1&1) crcTmp1 = (crcTmp1 >> 1) ^ 0xEDB88320L;
else crcTmp1 >>= 1;
}
crcTable[i] = crcTmp1;
}
//计算CRC32值
DWORD crcTmp2= 0xFFFFFFFF;
while(Size--)
{
crcTmp2 = ((crcTmp2>>8) & 0x00FFFFFF) ^ crcTable[ (crcTmp2^(*ptr)) & 0xFF ];
ptr++;
}
return (crcTmp2^0xFFFFFFFF);
}
bool isModified = false;
void CheckCRC32()
{
DWORD dwNumberOfBytesReadWritten;
HMODULE hmod;
HGLOBAL hMemory;
LPVOID pMemory;
DWORD dwFileSize;
CHAR szFileName[128];
hmod = ::GetModuleHandle(0);
::GetModuleFileName(hmod, szFileName, MAX_PATH);//获取自己的句柄
HANDLE hFile = ::CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0);//打开自己
if(hFile != INVALID_HANDLE_VALUE)
dwFileSize = ::GetFileSize(hFile, NULL);//如果打开成功则分配内存
hMemory = ::GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, dwFileSize);
pMemory = ::GlobalLock(hMemory);//锁定内存
ReadFile(hFile, pMemory, dwFileSize, &dwNumberOfBytesReadWritten, NULL);//读取文件
::CloseHandle(hFile);
DWORD dwCOFFHead, dwReserved;
DWORD OriginalCRC32;
BYTE* Version;
BYTE* pDosHead;
char CRC_ERROR[] = "CRC_ERROR!";
pDosHead = static_cast<BYTE*>(pMemory) + 0x3c;//指向DOS头最后的指针
dwCOFFHead = *((DWORD*)pDosHead); //获取COFF入口地址偏移值
dwReserved = dwCOFFHead + 0x4c; //获取校验值偏移值
Version = static_cast<BYTE*>(pMemory) + dwReserved; //指向校验值地址
OriginalCRC32 = *((DWORD*)Version);//获取CRC校验值
*((DWORD*)Version) = 0;//将CRC校验值清空 (只是内存中清空,不然死锁~)
DWORD NowCRC32 = CRC32((BYTE*)pMemory, dwFileSize); //进行CRC校验计算
if(OriginalCRC32 != NowCRC32)
{
}
}