for(i=8;i>0;i--)
{
if((data^accum)&0x8000)
accum=(accum<<1)^genpoly;
else
accum<<=1;
data<<=1;
}
return (accum);
}
/* 函数mk-crctbl利用函数crchware建立内存中的CRC数值表 */
unsigned short *mk-crctbl(poly,crcfn);
unsigned short poly;/* CRC除数--CRC生成多项式 */
R>unsigned short (*crcfn)();/* 指向CRC函数(例如crchware)的指针 */
{
/* unsigned short */malloc(); */
unsigned short *crctp;
int i;
if((crctp=(unsigned short*)malloc(256*sizeof(unsigned)))==0)
return 0;
for(i=0;i<256;i++)
crctp[i]=(*crcfn)(i,poly,0);
return crctp;
}
/* 函数mk-crctbl的使用范例 */
if((crctblp=mk-crctbl(CRCCCITT,crchware))==NIL)
{
puts("insuff memory for CRC lookup table.\n");
return 1; */
/* 函数crcupdate用以用查表法计算CRC值并更新CRC累加器值 */
void crcupdate(data,accum,crctab)
unsigned short data;/* 输入的数据 */
unsigned short *accum;/* 指向CRC累加器的指针 */
unsigned short *crctab;/* 指向内存中CRC表的指针 */
{
static short comb-val;
comb-val=(*accum>>8)^data;
*accum=(*accum<<8)^crctab[comb-val];
}
/* 函数crcrevhware是传统的CRC算法的反序算法,其返回值即CRC值 */
unsigned short crcrevhware(data,genpoly,accum)
unsigned short data;
unsigned short genpoly;
unsigned short accum;
{
static int i;
data<<=1;
for(i=8;i>0;i--)
{
data>>=1;
if((data^accum)&0x0001)
accum=(accum>>1)^genpoly;
else
accum>>=1;
}
return accum;
}
/* 函数crcrevupdate用以用反序查表法计算CRC值并更新CRC累加器值 */
void crcrevupdate(data,accum,crcrevtab)
unsigned short data;
unsigned short *accum;DvNew