CRC校验C实现

来源:互联网 发布:a股人工智能概念股 编辑:程序博客网 时间:2024/04/30 03:20

CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意选定。

CRC校验实用程序库在数据存储和数据通讯领域,为了保证数据的正确,就不得不采用检错的手段。

以下是CRC32的C语言实现,经过测试,能够正确运行:

  1. /***************************************************** 
  2. ** Name         : crc32.c  
  3. ** Author       : gzshun 
  4. ** Version      : 1.0 
  5. ** Date         : 2011-12 
  6. ** Description  : CRC32 Checking 
  7. ******************************************************/  
  8. #include <stdio.h>  
  9. #include <stdlib.h>  
  10. #include <string.h>  
  11. #include <errno.h>  
  12. #include <unistd.h>  
  13. #include <fcntl.h>  
  14. #include <sys/stat.h>  
  15.   
  16. #define BUFSIZE     1024*4  
  17.   
  18. static unsigned int crc_table[256];  
  19. const static char * program_name = "crc32";  
  20.   
  21. static void usage(void);  
  22. static void init_crc_table(void);  
  23. static unsigned int crc32(unsigned int crc, unsigned char * buffer, unsigned int size);  
  24. static int calc_img_crc(const char * in_file, unsigned int * img_crc);  
  25.   
  26. static void usage(void)  
  27. {  
  28.     fprintf(stderr, "Usage: %s input_file\n", program_name);  
  29. }  
  30.   
  31. /* 
  32. **初始化crc表,生成32位大小的crc表 
  33. **也可以直接定义出crc表,直接查表, 
  34. **但总共有256个,看着眼花,用生成的比较方便. 
  35. */  
  36. static void init_crc_table(void)  
  37. {  
  38.     unsigned int c;  
  39.     unsigned int i, j;  
  40.       
  41.     for (i = 0; i < 256; i++) {  
  42.         c = (unsigned int)i;  
  43.         for (j = 0; j < 8; j++) {  
  44.             if (c & 1)  
  45.                 c = 0xedb88320L ^ (c >> 1);  
  46.             else  
  47.                 c = c >> 1;  
  48.         }  
  49.         crc_table[i] = c;  
  50.     }  
  51. }  
  52.   
  53. /*计算buffer的crc校验码*/  
  54. static unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size)  
  55. {  
  56.     unsigned int i;  
  57.     for (i = 0; i < size; i++) {  
  58.         crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8);  
  59.     }  
  60.     return crc ;  
  61. }  
  62.   
  63. /* 
  64. **计算大文件的CRC校验码:crc32函数,是对一个buffer进行处理, 
  65. **但如果一个文件相对较大,显然不能直接读取到内存当中 
  66. **所以只能将文件分段读取出来进行crc校验, 
  67. **然后循环将上一次的crc校验码再传递给新的buffer校验函数, 
  68. **到最后,生成的crc校验码就是该文件的crc校验码.(经过测试) 
  69. */  
  70. static int calc_img_crc(const char *in_file, unsigned int *img_crc)  
  71. {  
  72.     int fd;  
  73.     int nread;  
  74.     int ret;  
  75.     unsigned char buf[BUFSIZE];  
  76.     /*第一次传入的值需要固定,如果发送端使用该值计算crc校验码, 
  77.     **那么接收端也同样需要使用该值进行计算*/  
  78.     unsigned int crc = 0xffffffff;   
  79.   
  80.     fd = open(in_file, O_RDONLY);  
  81.     if (fd < 0) {  
  82.         printf("%d:open %s.\n", __LINE__, strerror(errno));  
  83.         return -1;  
  84.     }  
  85.           
  86.     while ((nread = read(fd, buf, BUFSIZE)) > 0) {  
  87.         crc = crc32(crc, buf, nread);  
  88.     }  
  89.     *img_crc = crc;  
  90.   
  91.     close(fd);  
  92.       
  93.     if (nread < 0) {  
  94.         printf("%d:read %s.\n", __LINE__, strerror(errno));  
  95.         return -1;  
  96.     }  
  97.       
  98.     return 0;  
  99. }  
  100.   
  101. int main(int argc, char **argv)  
  102. {  
  103.     int ret;  
  104.     unsigned int img_crc;  
  105.     const char *in_file = argv[1];  
  106.   
  107.     if (argc < 2) {  
  108.         usage();  
  109.         exit(1);  
  110.     }  
  111.   
  112.     init_crc_table();  
  113.       
  114.     ret = calc_img_crc(in_file, &img_crc);  
  115.     if (ret < 0) {  
  116.         exit(1);  
  117.     }  
  118.   
  119.     printf("The crc of %s is:%u\n", in_file, img_crc);  
  120.   
  121.     return 0;  
  122. }  
  123.   
  124. /* 
  125. **测试程序 
  126. **环境: 
  127. **Linux ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux 
  128. **gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 
  129. ** 
  130. gzshun@ubuntu:~/apue/crc32$ ls 
  131. crc32.c 
  132. gzshun@ubuntu:~/apue/crc32$ gcc crc32.c -o crc32 
  133. gzshun@ubuntu:~/apue/crc32$ ./crc32 crc32.c 
  134. The crc of crc32.c is:3892136086 
  135. */ 


以上代码原著中有误。

结果应和0xffffffff异或后才为正确结果。

0 0