对文件或数据进行CRC校验

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

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

crc32.h

#ifndef CRC_32_H
#define CRC_32_H
#ifdef __cplusplus
extern "C" {
#endif
	void init_crc_table(void);
	unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size);  
	int calc_img_crc(const char *in_file, unsigned int *img_crc);  
#ifdef __cplusplus
}
#endif
#endif


crc32.c

#include <stdlib.h>
/***************************************************** 
 ** Name         : crc32.c  
 ** Author       :  
 ** Version      : 1.0 
 ** Date         :  
 ** Description  : CRC32 Checking 
 ******************************************************/  
#include <stdio.h>   
#include <stdlib.h>   
#include <string.h>   
#include <errno.h>   
#include <unistd.h>   
#include <fcntl.h>   
#include <sys/stat.h>   

#define BUFSIZE     1024*4   

static unsigned int crc_table[256];  
const static char * program_name = "crc32";  

/* 
 **初始化crc表,生成32位大小的crc表 
 */  
void init_crc_table(void)  
{  
    unsigned int c;  
    unsigned int i, j;  

    for (i = 0; i < 256; i++) {  
        c = (unsigned int)i;  
        for (j = 0; j < 8; j++) {  
            if (c & 1)  
                c = 0xedb88320L ^ (c >> 1);  
            else  
                c = c >> 1;  
        }  
        crc_table[i] = c;  
    }  
}  

/*计算buffer的crc校验码*/  
unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size)  
{  
    unsigned int i;  
    for (i = 0; i < size; i++) {  
        crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8);  
    }  
    return crc ;  
}  

/* 
 **计算大文件的CRC校验码:crc32函数,是对一个buffer进行处理, 
 **但如果一个文件相对较大,显然不能直接读取到内存当中 
 **所以只能将文件分段读取出来进行crc校验, 
 **然后循环将上一次的crc校验码再传递给新的buffer校验函数, 
 **到最后,生成的crc校验码就是该文件的crc校验码.
 */  
int calc_img_crc(const char *in_file, unsigned int *img_crc)  
{  
    int fd;  
    int nread;  
    int ret;  
    unsigned char buf[BUFSIZE];  
    /*第一次传入的值需要固定,如果发送端使用该值计算crc校验码, 
     **那么接收端也同样需要使用该值进行计算*/  
    unsigned int crc = 0xffffffff;   

    fd = open(in_file, O_RDONLY);  
    if (fd < 0) {  
        printf("%d:open %s.\n", __LINE__, strerror(errno));  
        return -1;  
    }  

    while ((nread = read(fd, buf, BUFSIZE)) > 0) {  
        crc = crc32(crc, buf, nread);  
    }  
    *img_crc = crc;  

    close(fd);  

    if (nread < 0) {  
        printf("%d:read %s.\n", __LINE__, strerror(errno));  
        return -1;  
    }  

    return 0;  
}  

/*
int main(int argc, char **argv)  
{  
    int ret;  
    unsigned int img_crc;  
    const char *in_file = argv[1];  

    if (argc < 2) {  
        exit(1);  
    }  

    init_crc_table();  

    ret = calc_img_crc(in_file, &img_crc);  
    if (ret < 0) {  
        exit(1);  
    }  

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

    return 0;  
}
*/

对文件进行校验:

         init_crc_table();
         unsigned int bin_crc;
         calc_img_crc(argv[1],&bin_crc);

对buf 中的数据校验:

     init_crc_table();
     unsigned int binCrcNew = 0xFFFFFFFF;
     binCrcNew = crc32(binCrcNew, (unsigned char*)fwBuff, binLen);

最简单的Makefile试例:

all: encryptBIN
encryptBIN:encryptBIN.cpp crc32.c
	gcc -c crc32.c -o crc32.o
	g++ -c encryptBIN.cpp -o encryptBIN.o
	gcc crc32.o encryptBIN.o -lstdc++ -o encryptBIN
clean:
	rm -rf *.o encryptBIN


  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值