歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> CRC32校驗算法-C實現

CRC32校驗算法-C實現

日期:2017/3/1 10:46:56   编辑:Linux編程

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. #define BUFSIZE 1024*4
  16. static unsigned int crc_table[256];
  17. const static char * program_name = "crc32";
  18. static void usage(void);
  19. static void init_crc_table(void);
  20. static unsigned int crc32(unsigned int crc, unsigned char * buffer, unsigned int size);
  21. static int calc_img_crc(const char * in_file, unsigned int * img_crc);
  22. static void usage(void)
  23. {
  24. fprintf(stderr, "Usage: %s input_file\n", program_name);
  25. }
  26. /*
  27. **初始化crc表,生成32位大小的crc表
  28. **也可以直接定義出crc表,直接查表,
  29. **但總共有256個,看著眼花,用生成的比較方便.
  30. */
  31. static void init_crc_table(void)
  32. {
  33. unsigned int c;
  34. unsigned int i, j;
  35. for (i = 0; i < 256; i++) {
  36. c = (unsigned int)i;
  37. for (j = 0; j < 8; j++) {
  38. if (c & 1)
  39. c = 0xedb88320L ^ (c >> 1);
  40. else
  41. c = c >> 1;
  42. }
  43. crc_table[i] = c;
  44. }
  45. }
  46. /*計算buffer的crc校驗碼*/
  47. static unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size)
  48. {
  49. unsigned int i;
  50. for (i = 0; i < size; i++) {
  51. crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8);
  52. }
  53. return crc ;
  54. }
  55. /*
  56. **計算大文件的CRC校驗碼:crc32函數,是對一個buffer進行處理,
  57. **但如果一個文件相對較大,顯然不能直接讀取到內存當中
  58. **所以只能將文件分段讀取出來進行crc校驗,
  59. **然後循環將上一次的crc校驗碼再傳遞給新的buffer校驗函數,
  60. **到最後,生成的crc校驗碼就是該文件的crc校驗碼.(經過測試)
  61. */
  62. static int calc_img_crc(const char *in_file, unsigned int *img_crc)
  63. {
  64. int fd;
  65. int nread;
  66. int ret;
  67. unsigned char buf[BUFSIZE];
  68. /*第一次傳入的值需要固定,如果發送端使用該值計算crc校驗碼,
  69. **那麼接收端也同樣需要使用該值進行計算*/
  70. unsigned int crc = 0xffffffff;
  71. fd = open(in_file, O_RDONLY);
  72. if (fd < 0) {
  73. printf("%d:open %s.\n", __LINE__, strerror(errno));
  74. return -1;
  75. }
  76. while ((nread = read(fd, buf, BUFSIZE)) > 0) {
  77. crc = crc32(crc, buf, nread);
  78. }
  79. *img_crc = crc;
  80. close(fd);
  81. if (nread < 0) {
  82. printf("%d:read %s.\n", __LINE__, strerror(errno));
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. int main(int argc, char **argv)
  88. {
  89. int ret;
  90. unsigned int img_crc;
  91. const char *in_file = argv[1];
  92. if (argc < 2) {
  93. usage();
  94. exit(1);
  95. }
  96. init_crc_table();
  97. ret = calc_img_crc(in_file, &img_crc);
  98. if (ret < 0) {
  99. exit(1);
  100. }
  101. printf("The crc of %s is:%u\n", in_file, img_crc);
  102. return 0;
  103. }
  104. /*
  105. **測試程序
  106. **環境:
  107. **Linux Ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux
  108. **gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
  109. **
  110. www.linuxidc.com @ubuntu:~/apue/crc32$ ls
  111. crc32.c
  112. www.linuxidc.com @ubuntu:~/apue/crc32$ gcc crc32.c -o crc32
  113. www.linuxidc.com @ubuntu:~/apue/crc32$ ./crc32 crc32.c
  114. The crc of crc32.c is:3892136086
  115. */
Copyright © Linux教程網 All Rights Reserved