⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crc32.cpp

📁 利用CRC-32检测程序的完整性
💻 CPP
字号:
// Crc32.cpp: implementation of the CCrc32 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TestCRC32.h"
#include "Crc32.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCrc32::CCrc32()
{
	this->InitCRC32Table();
}

CCrc32::~CCrc32()
{

}

void CCrc32::InitCRC32Table()
{
	ULONG ulPolynomial = 0x04c11db7;
	
	// 256 values representing ASCII character codes.
	for(int i = 0xFF+1; i--; )
	{
		m_crc32Table[i]=Reflect(i, 8) << 24;
		for (int j = 8; j--; )
			m_crc32Table[i] = (m_crc32Table[i] << 1) ^ (m_crc32Table[i] & (1 << 31) ? ulPolynomial : 0);
		m_crc32Table[i] = Reflect(m_crc32Table[i], 32);
	}
}

inline ULONG CCrc32::Reflect(ULONG ref, char ch)
{
	ULONG value(0);

	// Swap bit 0 for bit 7 , bit 1 for bit 6, etc.
	for(int i = 1; i <= ch; i++)
	{
		if(ref & 1)
			value |= 1 << (ch - i);
		ref >>= 1;
	}
	return value;
}

int CCrc32::GetCRC(LPCTSTR csData, DWORD dwSize)
{
	// 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;

	len = dwSize;
	buffer = (unsigned char*)csData;
	// Perform the algorithm on each character
	// in the string, using the lookup table values.
	while(len--)
		crc = (crc >> 8) ^ m_crc32Table[(crc & 0xFF) ^ *buffer++];

	// Exclusive OR the result with the beginning value.
	return crc^0xffffffff;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -