CRC32算法例程

unsigned long crc_slow(const unsigned char *btMessage,unsigned long nBytes, unsigned long nInitValue)
{ 
	unsigned long	nByte = 0; 
	unsigned long	nBit	= 0; 
	unsigned long	nRemainder = ~reflect(nInitValue, WIDTH);
	
	for (nByte = 0; nByte < nBytes; nByte++) 
	{ 
		nRemainder ^= (reflect(btMessage[nByte], 8) << (WIDTH - 8));

		for (nBit = 0; nBit < 8; nBit++) 
		{ 
			if (nRemainder & TOPBIT) 
			{ 
				nRemainder = (nRemainder << 1) ^ POLYNOMIAL; 
			} 
			else 
			{ 
				nRemainder = (nRemainder << 1); 
			} 
		}
	} 
	nRemainder = reflect(nRemainder, WIDTH);
	return (~nRemainder);
}
unsigned long reflect(unsigned long data, unsigned long nBits) 
{ 
	if (REFLECT_DATA)
	{
		unsigned long reflection = 0; 
		while (nBits != 0)
		{ 
			reflection <<= 1;
			if (data & 0x01)
			{
				reflection |= 0x01;
			}
			data >>= 1;
			nBits--;
		} 
		return (reflection); 
	}
	else
	{
		return (data);
	}
}

 

你可能感兴趣的:(CRC32算法例程)