对文件或数据进行CRC校验

用于对一个文件进行CRC校验,以确保文件数据传输的正确性。

废话不多说,直接上代码!

crc32.h

 


 
  1. #ifndef CRC_32_H

  2. #define CRC_32_H

  3. #ifdef __cplusplus

  4. extern "C" {

  5. #endif

  6. void init_crc_table(void);

  7. unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size);

  8. int calc_img_crc(const char *in_file, unsigned int *img_crc);

  9. #ifdef __cplusplus

  10. }

  11. #endif

  12. #endif


 

crc32.c

 


 
  1. #include <stdlib.h>

  2. /*****************************************************

  3. ** Name : crc32.c

  4. ** Author :

  5. ** Version : 1.0

  6. ** Date :

  7. ** Description : CRC32 Checking

  8. ******************************************************/

  9. #include <stdio.h>

  10. #include <stdlib.h>

  11. #include <string.h>

  12. #include <errno.h>

  13. #include <unistd.h>

  14. #include <fcntl.h>

  15. #include <sys/stat.h>

  16.  
  17. #define BUFSIZE 1024*4

  18.  
  19. static unsigned int crc_table[256];

  20. const static char * program_name = "crc32";

  21.  
  22. /*

  23. **初始化crc表,生成32位大小的crc表

  24. */

  25. void init_crc_table(void)

  26. {

  27. unsigned int c;

  28. unsigned int i, j;

  29.  
  30. for (i = 0; i < 256; i++) {

  31. c = (unsigned int)i;

  32. for (j = 0; j < 8; j++) {

  33. if (c & 1)

  34. c = 0xedb88320L ^ (c >> 1);

  35. else

  36. c = c >> 1;

  37. }

  38. crc_table[i] = c;

  39. }

  40. }

  41.  
  42. /*计算buffer的crc校验码*/

  43. unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size)

  44. {

  45. unsigned int i;

  46. for (i = 0; i < size; i++) {

  47. crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8);

  48. }

  49. return crc ;

  50. }

  51.  
  52. /*

  53. **计算大文件的CRC校验码:crc32函数,是对一个buffer进行处理,

  54. **但如果一个文件相对较大,显然不能直接读取到内存当中

  55. **所以只能将文件分段读取出来进行crc校验,

  56. **然后循环将上一次的crc校验码再传递给新的buffer校验函数,

  57. **到最后,生成的crc校验码就是该文件的crc校验码.

  58. */

  59. int calc_img_crc(const char *in_file, unsigned int *img_crc)

  60. {

  61. int fd;

  62. int nread;

  63. int ret;

  64. unsigned char buf[BUFSIZE];

  65. /*第一次传入的值需要固定,如果发送端使用该值计算crc校验码,

  66. **那么接收端也同样需要使用该值进行计算*/

  67. unsigned int crc = 0xffffffff;

  68.  
  69. fd = open(in_file, O_RDONLY);

  70. if (fd < 0) {

  71. printf("%d:open %s.\n", __LINE__, strerror(errno));

  72. return -1;

  73. }

  74.  
  75. while ((nread = read(fd, buf, BUFSIZE)) > 0) {

  76. crc = crc32(crc, buf, nread);

  77. }

  78. *img_crc = crc;

  79.  
  80. close(fd);

  81.  
  82. if (nread < 0) {

  83. printf("%d:read %s.\n", __LINE__, strerror(errno));

  84. return -1;

  85. }

  86.  
  87. return 0;

  88. }

  89.  
  90. /*

  91. int main(int argc, char **argv)

  92. {

  93. int ret;

  94. unsigned int img_crc;

  95. const char *in_file = argv[1];

  96.  
  97. if (argc < 2) {

  98. exit(1);

  99. }

  100.  
  101. init_crc_table();

  102.  
  103. ret = calc_img_crc(in_file, &img_crc);

  104. if (ret < 0) {

  105. exit(1);

  106. }

  107.  
  108. printf("The crc of %s is:%u\n", in_file, img_crc);

  109.  
  110. return 0;

  111. }

  112. */


对文件进行校验:

 


 
  1. init_crc_table();

  2. unsigned int bin_crc;

  3. calc_img_crc(argv[1],&bin_crc);


对buf 中的数据校验:

 


 
  1. init_crc_table();

  2. unsigned int binCrcNew = 0xFFFFFFFF;

  3. binCrcNew = crc32(binCrcNew, (unsigned char*)fwBuff, binLen);

最简单的Makefile试例:

 


 
  1. all: encryptBIN

  2. encryptBIN:encryptBIN.cpp crc32.c

  3. gcc -c crc32.c -o crc32.o

  4. g++ -c encryptBIN.cpp -o encryptBIN.o

  5. gcc crc32.o encryptBIN.o -lstdc++ -o encryptBIN

  6. clean:

  7. rm -rf *.o encryptBIN

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值