名称简介
特点
目 录
1算法
CRC的本质
是模-2除法的余数,采用的除数不同,CRC的类型也就不一样。通常,CRC的除数用生成多项式来表示。最常用的CRC码的生成多项式如表1所示。
@@10A08800.GIF;表1.最常用的CRC码及生成多项式@@
由于CRC在通讯和数据处理软件中经常采用,笔者在实际工作中对其算法进行了研究和比较,总结并编写了一个具有最高效率的CRC通用程序库。该程序采用查表法计算CRC,在速度上优于一般的直接模仿硬件的算法,可以应用于通讯和数据压缩程序。
通常算法
通常的CRC算法在计算一个数据段的CRC值时,其CRC值是由求解每个数值的CRC值的和对CRC寄存器的值反复更新而得到的。这样,求解CRC的速度较慢。通过对CRC算法的研究,我们发现:一个8位数据加到16位累加器中去,只有累加器的高8位或低8位与数据相作用,其结果仅有256种可能的组合值。因而,我们可以用查表法来代替反复的运算,这也同样适用于CRC32的计算。本文所提供的程序库中,函数crchware是一般的16位CRC的算法;mk-crctbl用以在内存中建立一个CRC数值表;crcupdate用以查表并更新CRC累加器的值;crcrevhware和crcrevupdate是反序算法的两个函数;BuildCRCTable、CalculateBlockCRC32和UpdateCharac
terCRC32用于CRC32的计算。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
| /* CRC.C——CRC程序库*/#define CRCCCITT 0x1021#define CCITT-REV 0x8408#define CRC16 0x8005#define CRC16-REV 0xA001#define CRC32-POLYNOMIAL 0xEDB88320L/* 以上为CRC除数的定义 */#define NIL 0#define crcupdate(d,a,t)*(a)=(*(a)<<8 a="" t="">>8)^(d)];8>#define crcupdate16(d,a,t)*(a)=(*(a)>>8^(t)[(*(a)^(d))&0x00ff])/* 以上两个宏可以代替函数crcupdate和crcrevupdate */#include#include#include/* 函数crchware是传统的CRC算法,其返回值即CRC值 */unsigned short crchware(data,genpoly,accum)unsigned short data;/* 输入的数据 */unsigned short genpoly;/* CRC除数 */unsigned short accum;/* CRC累加器值 */{ static int i; data<<=8; for(i=8;i>0;i--) { if((data^accum)&0x8000) accum=(accum<<1 code="" genpoly="">1> 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 code="" i="">256> crctp=(*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 code="" comb-val="" crctab="">8>}/* 函数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;DvNews |
2.
crc32 — 计算一个字符串的 crc32 多项式
2说明
int crc32
( string$str )
生成str的 32 位循环冗余校验码多项式。这通常用于检查传输的数据是否完整。
示例中的第二行演示了如何使用printf()函数转换校验码:
校验码
Example #1 显示 crc32 校验码
1
2
3
4
| $checksum = crc32("The quick brown fox jumped over the lazy dog.");printf("%u\n", $checksum);?> |
留言