📄 crc.cpp
字号:
// CRC.cpp: implementation of the CCRC class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CRC.h"
#define CRC_TABLE_SIZE 256
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCRC::CCRC()
{
}
CCRC::~CCRC()
{
}
// Reflection is a requirement for the official CRC-32 standard.
// You can create CRCs without it, but they won't conform to the standard.
ULONG CCRC::Reflect(ULONG ref, char ch)
{// Used only by Init_CRC32_Table()
ULONG value(0);
// Swap bit 0 for bit 7 , bit 1 for bit 6, etc.
for(int i = 1; i < (ch + 1); i++)
{
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return value;
}
void CCRC::Init_CRC32_Table(ULONG *crc32_table)
{
// This is the official polynomial used by CRC-32 in PKZip, WinZip and Ethernet.
ULONG ulPolynomial = 0x04c11db7;
// 256 values representing ASCII character codes.
for(int i = 0; i <= 0xFF; i++)
{
crc32_table[i]=Reflect(i, 8) << 24;
for (int j = 0; j < 8; j++)
crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
crc32_table[i] = Reflect(crc32_table[i], 32);
}
}
// This function uses the crc32_table lookup table to generate a CRC for csData
int CCRC::Get_CRC(PBYTE pbBuf, DWORD dwSize)
{
static BOOL bInitiated=FALSE;
static ULONG crc32_table[256]={0,}; // Lookup table arrays
if(!bInitiated)
{
Init_CRC32_Table(crc32_table);
bInitiated=TRUE;
}
// Be sure to use unsigned variables,
// because negative values introduce high bits
// where zero bits are required.
ULONG crc(0xffffffff);
int len;
unsigned char* buffer=pbBuf;
len = dwSize;
// Perform the algorithm on each character
// in the string, using the lookup table values.
while(len--)
crc = (crc >> 8) ^ crc32_table[(crc & 0xFF) ^ *buffer++];
// Exclusive OR the result with the beginning value.
return crc^0xffffffff;
}
BOOL CCRC::GetFileCRC32(const char * pszFilePath,ULONG& ulCRC)
{
ulCRC=0;
HANDLE hFile=CreateFile(pszFilePath, GENERIC_READ , FILE_SHARE_READ, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(!hFile)
{
return FALSE;
}
DWORD dwSize=GetFileSize(hFile,0);
if(dwSize==0)
{
return TRUE;
}
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, dwSize,NULL);
if(!hFileMap)
{
CloseHandle(hFile);
return FALSE;
}
PBYTE pbBuf=(PBYTE)MapViewOfFile(hFileMap,FILE_MAP_READ, 0, 0,dwSize);
if(!pbBuf)
{
CloseHandle(hFileMap);
CloseHandle(hFile);
return FALSE;
}
ulCRC = Get_CRC(pbBuf,dwSize);
UnmapViewOfFile(pbBuf);
CloseHandle(hFileMap);
CloseHandle(hFile);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -