CRC 模式及实现

CRC : Cyclic redundancy Check 循环冗余校验

概述参见wiki百科:http://en.wikipedia.org/wiki/Cyclic_redundancy_check

原理及精辟讲解参见:http://www.repairfaq.org/filipg/LINK/F_crc_v3.html

原理我也只是大致懂了,感觉还不是很透彻,岁数大了又不是搞数学的,吃力啊。不过有一些理解上的心得,对与不对说一下。另外提供封装好的源码。

1、关于原理

    CRC的算法是那么规定的,所以不要太过于纠结它的模2除法、多项式除法,也不要硬扯着熟悉的算术除法说事。异或一直是处理bit的一个很神奇的玩意。这样说并不是不负责任,不管是CRC还是什么加密算法之类的,算法规定该怎么弄了,人家有完备的数学推理及某些前提,除非你也钻研相关算法,否则想不透彻我觉得反倒正常,网上很多写博客的人说的好像那么回事,但是感觉也是东抄西抄,然后牵强附会。最起码我觉得我不够透彻,连A^B^C = A^(B^C)我也不知道为什么,其实A+B+C = A+(B+C)我也证明不出来。

2、关于模式

算法了解了,那么就应该关心它的模式了。毕竟CRC是校验用的,就会涉及到两端,就需要约定。因此,标准的或者通用的或者流行的模式最好了解的透彻些。比较常用的模式有CRC16/CRC16-CCITT/CRC32这三种。更多的模式及模式应用的领域在wiki百科里有个表,可以对照着看下。

 

//////////////////////////////////////////////////////////////////////////
// Hold the information about the crc model.
typedef struct _CRCMODEL
{
	CHAR		szModelName[32];// Indicate one exclusive model
	DWORD		dwWidth;	// The width of crc value in bits
	DWORD		dwPoly;	        // The top bit(leftmost) of the poly should be omitted.
	DWORD		dwInit;
	BOOL		bRefIn;		// TRUE : LSB  ,FALSE : MSB
	BOOL		bRefOut;	// Almost same as bRefIn
	DWORD		dwXorOut;
	DWORD*		pTable;	        // Using this static table when checking by byte.
	
}CRCMODEL,*PCRCMODEL;

 

受开头推荐的那篇文章影响,定义了以上的一个结构,用于表示不同的模式。那么模式的主要内容也就是以上那么多项了。

dwPoly:16进制表示的多项式,注意此值未包含多项式的top bit,严格意义上讲多项式是指包含top bit的,但鉴于top bit必须是1,而做模除时1^1一定消掉,所以不显示的表示也无不可之处。另外我定义的是32位的无符号整型,容纳不下33位。

 

// CRC-16(SDLC) :	    X16 + X15 + X2 + X0 
#define POLY_16		    0X8005
// CRC-ITU(CCITT、HDLC) :   X16 + X12 + X5 + X0
#define POLY_16_CCITT	    0X1021
// CRC-32  (ZIP,RAR,Ethernet)      
// X32 + X26+ X23 + X22 + X16 +X12 + X11 + X10 + X8 + X7 +X5+X4+X2+X1+X0
#define POLY_32	            0X04C11DB7

这里值得一提的是颠倒多项式:因为颠倒多项式是指将整个多项式的值(包含top bit)做位颠倒。那么在参与计算时直接将dwPoly颠倒会否出问题?答案是不会的,因为颠倒的多项式用于颠倒求crc算法,这时,颠倒多项式的top bit就是最右一bit。
dwInit:寄存器的初值,或者说用此值来与字节串开始的W位做异或,一般是全FF。这样可以避免开头多个0的串crc值相同的情况。(Hpmbcalc软件)

 


切记在颠倒算法中,此值也要做颠倒,开头推荐文章里也如此讲解,我也认同。但我发现Hpmbcalc未颠倒。后文我会拿出我认为的证据证明应该将初值颠倒。

bRefIn:这是个BOOL值,表示字符串是否进行了字节颠倒。某些硬件传输时使用LSB模式,即先传输低位的bit,这样使用MSB模式收到的字节就是颠倒的。我的理解是假如A传输的是1000 0000,但是底层硬件传输时按照0000 0001的顺序传给B,B的传输模式是MSB,即认为接收的第一个bit是高位bit。那么本来应该是0x80,但是B却认为它接收的是0x01.

对于这种情况,在做crc之前把每一个字节颠倒过来就好了。但是这么做将浪费大量时间和工作量,于是出现了颠倒crc算法,即除了源串字节不进行颠倒以外,初始值、poly、进寄存器方向、最后的crc结果都进行颠倒。这样与费时费事的颠倒每个字节计算的结果完全一致。

bRefOut: 我觉得这个就是为了颠倒crc算法准备的,因为上文提到了,颠倒crc算法最后的crc结果也要进行颠倒,那么如果传输端将本来crc结果进行了颠倒,那么接收端使用颠倒crc算法得到的crc结果就可以心安理得的不进行颠倒了。

dwXorOut:最后的crc值与此值做异或,为啥用不是很了解。。。但是它是最后最后的一步了,它是对crc结果的再处理,我认为处理后的已经不能算是crc值了,Hpmbcalc界面展示时也对经过dwXorOut后的值叫做After  Final XOR.这个我倒不是迎合Hpmbcalc,而是按理说接收方对于包含crc值的源串按同样poly进行CRC校验结果应该是0,但是如果对方传递的是经过了final xor的值那么别想得到0.除非你将收到的所谓crc值先做XOR然后再去校验整个串才能得到0.

3、一些验证

1、使用标准CRC16、CRC16-CCITT、CRC32进行验证,与HPMBCALC软件比较:



2、使用标准CRC32model进行验证,与winrar比较:



3、假如发送端发送的是0x01,但是硬件传输时使用的是LSB,而接收端是MSB,那么接收端接收的应该是0x80。

    a、在Initial value是0、Finial Xor也是0、发送端Crc结果不进行颠倒的情况下,使用Hpmbcalc计算结果和我的封装函数结果如下:



    b、在Initial value是0x12345678、Finial Xor是0、发送端Crc结果不进行颠倒的情况下,使用Hpmbcalc计算结果和我的封装函数结果如下:



 

4、实现源码

.c文件中提供了一些进行crc校验的函数,例如位颠倒函数、按bit计算crc、按字节计算crc、生成驱动表、按照自定义模式计算crc值、颠倒crc算法。

.h中提供了封装的导出函数,对字符串和文件计算crc值,对于字符串可以按bit和按字节计算,默认按照字节,对于文件默认按字节,以及定义了三种标准的CRCMODEL并提供函数直接使用。

代码方面,虽然是windows下的c,但是相信移植也不会很难,用到的库函数极少,类型已经宏定义。此份代码对于32及以内宽度的crc校验码可用,但对于非16bit和32bit未进行充分测试(hpmbcalc不提供对于非此两种的poly的支持,因此不好进行比对,但代码中已经对非此两种bit进行了处理)。

 

/************************************************************************
 *  Name            :   CrcFunction.h		                        *
 *  Author          :   XiaoJiu                                         *
 *  Date Commenced  :   2013-08-09                                      *
 *  Date Closed     :   2013-08-17                                      *
 *  Description     :                                                   *
 *		The declaration of structure/function  about CRC.       *
 ***********************************************************************/
#pragma once
#ifdef __cplusplus
	extern "C" {
#endif
//////////////////////////////////////////////////////////////////////////
// Hold the information about the crc model.
typedef struct _CRCMODEL
{
	CHAR		szModelName[32];	// Indicate one exclusive model
	DWORD		dwWidth;		// The width of crc value in bits
	DWORD		dwPoly;	 // The top bit(leftmost) of the poly should be omitted.
	DWORD		dwInit;
	BOOL		bRefIn;			// TRUE : LSB  ,FALSE : MSB
	BOOL		bRefOut;		// Almost same as bRefIn
	DWORD		dwXorOut;
	DWORD*		pTable;	// Using this static table when checking by byte.
	
}CRCMODEL,*PCRCMODEL;
//////////////////////////////////////////////////////////////////////////
// There 're 2 ways to check the crc value on the received end.
// 1、Calculate the new crc value of all bytes received, 0 means ok.
// 2、Calculate the crc value of received message without last several 
//    bytes placed crc value in,then compare the two crc value. 
// Using a model whose bRefOut is not FALSE or dwXorOut is not 0,we should
// check crc the second way,because the "crc value" placed at the end of 
// message is not the real crc value.
/////////////////////////////////////////////////////////////////////////
// Three famous standard,we defined in implementation file.
// "CRC-16" , 16 , POLY_16  , 0X0000 , TRUE , TRUE , 0X0000 ,TABLE16
extern const CRCMODEL Model_Crc16;
// "CRC-32" , 32 , POLY_32 , 0XFFFFFFFF , TRUE , TRUE , 0XFFFFFFFF ,TABLE32
extern const CRCMODEL Model_Crc32;
// "CRC-16/CCITT",16,POLY_16_CCITT,0XFFFF,FALSE,FALSE,0X0000,TABLE16_CCITT
extern const CRCMODEL Model_Crc16_CCITT;
//////////////////////////////////////////////////////////////////////////
// 
DWORD CrcReflectBits(DWORD dwToRef,DWORD dwNumsFromRight);
#define CrcMessage  CrcMessageByByte
DWORD CrcMessageByByte(UCHAR* pBuffer,DWORD dwLen,const CRCMODEL* pModel);
DWORD CrcMessageByBit(UCHAR* pBuffer,DWORD dwLen,const CRCMODEL* pModel);
DWORD CrcFile(CHAR* pszFileName,const CRCMODEL* pModel);

// Get crc value in the 3 famous standard model
#define GetCrc16(pBuffer,dwLen)		CrcMessage(pBuffer,dwLen,&Model_Crc16)
#define GetCrc16CCITT(pBuffer,dwLen)	CrcMessage(pBuffer,dwLen,&Model_Crc16_CCITT)
#define GetCrc32(pBuffer,dwLen)		CrcMessage(pBuffer,dwLen,&Model_Crc32)
#define GetFileCrc32(fileName)			CrcFile(fileName,&Model_Crc32)

#ifdef __cplusplus
	}
#endif

实现代码

/************************************************************************
 *  Name            :   CrcFunction.c		                        *
 *  Author          :   XiaoJiu                                         *
 *  Date Commenced  :   2013-08-09                                      *
 *  Date Closed     :   2013-08-17                                      *
 *  Description     :                                                   *
 *		The implementation function of crc.		  	*
 ***********************************************************************/
typedef unsigned long       DWORD;
typedef int                 BOOL;
typedef char		    CHAR;
typedef unsigned char       UCHAR;
typedef void	            VOID;
#define TRUE				1
#define FALSE				0
#define NULL				0
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include "CrcFunction.h"
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// 1、Reversed poly is the entire poly that is being reversed,including 
//    the top bit.Therefore,the top bit is the leftmost in poly while is 
//    the rightmost in reversed poly.
// 2、The top bit of the poly or reversed poly is omitted.
//////////////////////////////////////////////////////////////////////////
// CRC-16(SDLC) :				X16 + X15 + X2 + X0 
#define POLY_16					0X8005
// CRC-ITU(CCITT、HDLC) :		        X16 + X12 + X5 + X0
#define POLY_16_CCITT			        0X1021
// CRC-32  (ZIP,RAR,Ethernet)      
// X32 + X26+ X23 + X22 + X16 +X12 + X11 + X10 + X8 + X7 +X5+X4+X2+X1+X0
#define POLY_32					0X04C11DB7
/////////////////////////////////////////////////////////////////////////
// Three famous standard
extern DWORD TABLE16[256];
extern DWORD TABLE32[256];
extern DWORD TABLE16_CCITT[256];
//
const CRCMODEL Model_Crc16 = {"CRC-16" , 16 , POLY_16  , 0X0000 , TRUE , TRUE , 0X0000 ,TABLE16};
const CRCMODEL Model_Crc32 = {"CRC-32" , 32 , POLY_32 , 0XFFFFFFFF , TRUE , TRUE , 0XFFFFFFFF ,TABLE32};
const CRCMODEL Model_Crc16_CCITT = {"CRC-16/CCITT",16,POLY_16_CCITT,0XFFFF,FALSE,FALSE,0X0000,TABLE16_CCITT};

/////////////Internal Functions///////////////////////////////////////////////////////
DWORD CrcReflectBits(DWORD dwToRef,DWORD dwNumsFromRight)
{
	DWORD dwReturn = 0,dwIndex = 0x01;
	int i = 0;
	for (;i < dwNumsFromRight;++i, dwIndex <<= 1)
	{
		if (dwToRef & dwIndex)
		{
			dwReturn |= 0x01 << (dwNumsFromRight - 1 - i);
		}
	}
	return dwReturn;	
}

DWORD CrcByBitUnRef(UCHAR* pBuffer,DWORD dwLen,DWORD dwInit,DWORD dwWidth,DWORD dwPoly)
{
	DWORD dwCrc = dwInit,dwTop = 0x01;
	UCHAR i;
	dwTop <<= dwWidth - 1;

	while (dwLen --)
	{
		for (i = 0x80;i != 0;i >>= 1)
		{				
			if (dwCrc & dwTop)
			{
				dwCrc <<= 1;
				dwCrc = dwCrc ^ (dwTop << 1) ^ dwPoly;
			}else{
				dwCrc <<= 1;
			}
			if (*pBuffer & i)
			{
				dwCrc ^= dwPoly;
			}
		}	
		++pBuffer;
	}
	return dwCrc ;	
}
DWORD CrcByBitRef(UCHAR* pBuffer,DWORD dwLen,DWORD dwInit,DWORD dwPolyRef)
{
	// If data was transferred in LSB pattern,we should reflect every byte,then 
	// calculate the crc value.However,it will take too much work and a lot of time.
	// Some genius suggest that we leave the input message still and reverse the
	// Poly、Init and the crc result.
	DWORD dwCrc = dwInit;
	UCHAR i;
	while(dwLen --)
	{
		for(i = 0x01; i !=0; i<<= 1)
		{
			if (dwCrc & 0x01)
			{
				dwCrc >>= 1;
				dwCrc ^= dwPolyRef;
			}else{
				dwCrc >>= 1;
			}
			if(*pBuffer & i)
				dwCrc ^= dwPolyRef;
		}
		++pBuffer;
	}
	return dwCrc;
}

DWORD CrcByByteUnRef(UCHAR* pBuffer,DWORD dwLen,DWORD dwInit,DWORD dwWidth,DWORD* pTable)
{
	DWORD dwCrc = dwInit,dwRightMove = dwWidth - 8;
	UCHAR ucHi8 = 0;
	while (dwLen --)
	{
		ucHi8 = (dwCrc >> dwRightMove) & 0xFF;
		dwCrc <<= 8;
		dwCrc <<= 32 - dwWidth;
		dwCrc >>= 32 - dwWidth;
		dwCrc ^= pTable[*pBuffer++ ^ ucHi8] ;
	}
	return dwCrc;
}
DWORD CrcByByteRef(UCHAR* pBuffer,DWORD dwLen,DWORD dwInit,DWORD* pTable)
{
	DWORD dwCrc = dwInit;
	while (dwLen --)
	{
		dwCrc = pTable[*pBuffer++ ^ (dwCrc & 0xFF)] ^ (dwCrc >> 8);
	}
	return dwCrc;
}

DWORD CrcFinalProcess(DWORD dwCrc,BOOL bRefIn,BOOL bRefOut,DWORD dwWidth,DWORD dwXorOut)
{
	if (bRefIn ^ bRefOut)
	{
		dwCrc = CrcReflectBits(dwCrc,dwWidth);			
	}
	return dwCrc ^ dwXorOut;
}

VOID CrcCreateTableUnRef(DWORD* pTable,DWORD dwWidth,DWORD dwPoly)
{
	DWORD dwIndex = 0, dwCrc =0, dwTop = 0x01;
	UCHAR i;
	dwTop <<= (dwWidth - 1);
	for( ;dwIndex < 256; ++dwIndex)
	{
		dwCrc = 0;
		for (i = 0x80;i != 0;i >>= 1)
		{				
			if (dwCrc & dwTop)
			{
				dwCrc <<= 1;
				dwCrc = dwCrc ^ (dwTop << 1) ^ dwPoly;
			}else{
				dwCrc <<= 1;
			}
			if (dwIndex & i)
			{
				dwCrc ^= dwPoly;
			}
		}			
 		pTable[dwIndex] = dwCrc ;
	}
}
VOID CrcCreateTableRef(DWORD* pTable,DWORD dwPolyRef)
{
	DWORD dwIndex = 0, dwCrc = 0;
	UCHAR i;
	for( ;dwIndex < 256; ++dwIndex)
	{
		dwCrc = 0;
		for(i = 0x01; i !=0; i <<= 1)
		{
			if (dwCrc & 0x01)
			{
				dwCrc >>= 1;
				dwCrc ^= dwPolyRef;
			}else{
				dwCrc >>= 1;
			}
			if(dwIndex & i)
				dwCrc ^= dwPolyRef;
		}
		pTable[dwIndex] = dwCrc;
	}
}
BOOL CrcCheckTable(const CRCMODEL* pModel)
{
	DWORD CrcTableCreate[256] = {0};
	if (!pModel->pTable)
		return FALSE;	
	if (pModel->bRefIn)		
		CrcCreateTableRef(CrcTableCreate,CrcReflectBits(pModel->dwPoly,pModel->dwWidth));
	else
		CrcCreateTableUnRef(CrcTableCreate,pModel->dwWidth,pModel->dwPoly);
	return(!memcmp(CrcTableCreate,pModel->pTable,sizeof(DWORD) * 256 ));
}

////////////////Export Function////////////////////////////////////////////////////////
//
DWORD CrcMessageByByte(UCHAR* pBuffer,DWORD dwLen,const CRCMODEL* pModel)
{
	DWORD *CrcTable, dwCrc = pModel->dwInit;
	// prepare the right table
	if (!pModel->pTable)
	{
		CrcTable = (DWORD*)malloc(256 * sizeof(DWORD));	
		if (pModel->bRefIn)		
			CrcCreateTableRef(CrcTable,CrcReflectBits(pModel->dwPoly,pModel->dwWidth));
		else
			CrcCreateTableUnRef(CrcTable,pModel->dwWidth,pModel->dwPoly);
	}else{
		CrcTable = pModel->pTable;
	}
	// calculate the crc value
	if (pModel->bRefIn)
	{
		dwCrc = CrcReflectBits(pModel->dwInit,pModel->dwWidth);
		dwCrc = CrcByByteRef(pBuffer,dwLen,dwCrc,CrcTable);
	}else{
		dwCrc = pModel->dwInit;
		dwCrc = CrcByByteUnRef(pBuffer,dwLen,dwCrc,pModel->dwWidth,CrcTable);
	}
	// finally,process the crc value.
	dwCrc = CrcFinalProcess(dwCrc,pModel->bRefIn,pModel->bRefOut,pModel->dwWidth,pModel->dwXorOut);
	if(!pModel->pTable)
		free(CrcTable);
	return dwCrc;
}
//
DWORD CrcMessageByBit(UCHAR* pBuffer,DWORD dwLen,const CRCMODEL* pModel)
{
	DWORD dwCrc,dwPolyRef;
	if (pModel->bRefIn)
	{
		dwCrc = CrcReflectBits(pModel->dwInit,pModel->dwWidth);
		dwPolyRef = CrcReflectBits(pModel->dwPoly,pModel->dwWidth);
		dwCrc = CrcByBitRef(pBuffer,dwLen,dwCrc,dwPolyRef);
	}else{
		dwCrc = pModel->dwInit;
		dwCrc = CrcByBitUnRef(pBuffer,dwLen,dwCrc,pModel->dwWidth,pModel->dwPoly);
	}
	return CrcFinalProcess(dwCrc,pModel->bRefIn,pModel->bRefOut,pModel->dwWidth,pModel->dwXorOut);
}
//
DWORD CrcFile(CHAR* pszFileName,const CRCMODEL* pModel)
{
#define BUFFERSIZE		1024 * 4
	FILE* hFile = NULL;
	UCHAR szBuffer[BUFFERSIZE] = {0};
	DWORD dwReadBytes = 0;
	DWORD *CrcTable, dwCrc;
	fopen_s(&hFile,pszFileName,"rb");
	if(!hFile)
		return 0;
	// prepare the right table
	if (!pModel->pTable)
	{
		CrcTable = (DWORD*)malloc(256 * sizeof(DWORD));	
		if (pModel->bRefIn)		
			CrcCreateTableRef(CrcTable,CrcReflectBits(pModel->dwPoly,pModel->dwWidth));
		else
			CrcCreateTableUnRef(CrcTable,pModel->dwWidth,pModel->dwPoly);
	}else{
		CrcTable = pModel->pTable;
	}
	// calculate the crc value
	if (pModel->bRefIn)
	{
		dwCrc = CrcReflectBits(pModel->dwInit,pModel->dwWidth);
		while(dwReadBytes = fread(szBuffer,1,BUFFERSIZE,hFile))
		{
			dwCrc = CrcByByteRef(szBuffer,dwReadBytes,dwCrc,CrcTable);
		}
	}else{
		dwCrc = pModel->dwInit;
		while(dwReadBytes = fread(szBuffer,1,BUFFERSIZE,hFile))
		{
			dwCrc = CrcByByteUnRef(szBuffer,dwReadBytes,dwCrc,pModel->dwWidth,CrcTable);
		}
	}
	// finally,process the crc value.
	dwCrc = CrcFinalProcess(dwCrc,pModel->bRefIn,pModel->bRefOut,pModel->dwWidth,pModel->dwXorOut);
	if(!pModel->pTable)
		free(CrcTable);
	fclose(hFile);
	return dwCrc;
}

//////////////////////////////////////////////////////////////////////////
// static table fit the standards defined on the top of this file .
DWORD TABLE16_CCITT[256] =
{
	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 
	0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 
	0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 
	0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 
	0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 
	0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 
	0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 
	0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 
	0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 
	0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 
	0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 
	0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 
	0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188, 0x81a9, 
	0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 
	0x5004, 0x4025, 0x7046, 0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 
	0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 
	0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 
	0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, 
	0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 
	0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 
	0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d, 0xdb5c, 
	0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 
	0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 
	0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 
	0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 
	0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};	
//////////////////////////////////////////////////////////////////////////
//
DWORD TABLE16[256] =
{
	0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, 0xc601, 0x06c0,
	0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440, 0xcc01, 0x0cc0, 0x0d80, 0xcd41,
	0x0f00, 0xcfc1, 0xce81, 0x0e40, 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0,
	0x0880, 0xc841, 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
	0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41, 0x1400, 0xd4c1,
	0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, 0xd201, 0x12c0, 0x1380, 0xd341,
	0x1100, 0xd1c1, 0xd081, 0x1040, 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1,
	0xf281, 0x3240, 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
	0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41, 0xfa01, 0x3ac0,
	0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840, 0x2800, 0xe8c1, 0xe981, 0x2940,
	0xeb01, 0x2bc0, 0x2a80, 0xea41, 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1,
	0xec81, 0x2c40, 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
	0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041, 0xa001, 0x60c0,
	0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240, 0x6600, 0xa6c1, 0xa781, 0x6740,
	0xa501, 0x65c0, 0x6480, 0xa441, 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0,
	0x6e80, 0xae41, 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
	0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41, 0xbe01, 0x7ec0,
	0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40, 0xb401, 0x74c0, 0x7580, 0xb541,
	0x7700, 0xb7c1, 0xb681, 0x7640, 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0,
	0x7080, 0xb041, 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
	0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440, 0x9c01, 0x5cc0,
	0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40, 0x5a00, 0x9ac1, 0x9b81, 0x5b40,
	0x9901, 0x59c0, 0x5880, 0x9841, 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1,
	0x8a81, 0x4a40, 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
	0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, 0x8201, 0x42c0,
	0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
};
//////////////////////////////////////////////////////////////////////////
//
DWORD TABLE32[256] =
{
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 
	0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
	0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,	0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
	0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
	0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
	0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
	0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
	0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,	0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
	0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
	0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
	0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
	0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
	0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,	0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 
	0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 
	0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
	0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
	0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 
	0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,	0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
	0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
	0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
	0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
	0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
	0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,	0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
	0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
	0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
	0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
	0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
	0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,	0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 
	0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
	0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
	0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
	0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};


 


posted @ 2013-08-18 21:53  pangbangb  阅读(1158)  评论(0编辑  收藏  举报