c++获得大文件的CRC32值

使用方法:先调用init_crc32_tab生成查询表,再调用calc_img_crc获得文件的CRC值。

windows版本

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

using namespace std;

#define Poly 0xEDB88320L//CRC32标准
static unsigned int crc_tab32[256];//CRC查询表
//生成CRC查询表
void init_crc32_tab(void)
{
    int i, j;
    unsigned int crc;

    for (i = 0; i < 256; i++)
    {
        crc = (unsigned long)i;
        for (j = 0; j < 8; j++)
        {
            if (crc & 0x00000001L)
                crc = (crc >> 1) ^ Poly;
            else
                crc = crc >> 1;
        }
        crc_tab32[i] = crc;
    }
}
//获得CRC
unsigned int get_crc32(unsigned int crcinit, unsigned char * bs, unsigned int bssize)
{
    unsigned int crc = crcinit ^ 0xffffffff;

    while (bssize--)
        crc = (crc >> 8) ^ crc_tab32[(crc & 0xff) ^ *bs++];

    return crc ^ 0xffffffff;
}
//获得文件CRC
int calc_img_crc(TCHAR *pFileName, unsigned int *uiCrcValue)
{
    HANDLE hFile = CreateFile(pFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        return -1;
    }

    const unsigned int size = 16 * 1024;
    unsigned char crcbuf[size];
    DWORD rdlen;
    unsigned int crc = 0;//CRC初始值为0

    while (ReadFile(hFile, crcbuf, size, &rdlen, NULL), rdlen)
        crc = get_crc32(crc, crcbuf, rdlen);

    *uiCrcValue = crc;
    CloseHandle(hFile);

    return 0;
}

int main()
{
    init_crc32_tab();
    unsigned int crcvalue;
    calc_img_crc(L"H:\\software\\cn_windows_10_multiple_editions_x64_dvd_6848463.iso", &crcvalue);
    printf("crc32 = %x \r\n", crcvalue);
    system("pause");
    return 0;
}

 

linux版本

#include <stdio.h>   
#include <stdlib.h>   
#include <string.h>   
#include <errno.h>   
#include <unistd.h>   
#include <fcntl.h>   
#include <sys/stat.h>   
 
#define BUFSIZE   16 * 1024  
 
static unsigned int crc_table[256];  
 
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 = 0xedb88320 ^ (c >> 1);  
            else  
                c = c >> 1;  
        }  
        crc_table[i] = c;  
    }  
}  

unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size)  
{  
    unsigned int i = crc ^ 0xffffffff;  
    while (size--)
    {
		i = (i >> 8) ^ crc_table[(i & 0xff) ^ *buffer++];
    }
    return i ^ 0xffffffff;
}  
 
int calc_img_crc(const char *in_file, unsigned int *img_crc)  
{  
    int fd;  
    int nread;  
    int ret;  
    unsigned char buf[BUFSIZE];
    unsigned int crc = 0;   
 
    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 = 0xffffffff;  
    if (argc < 2)
    {
        printf("Usage:%s <filename> \r\n",argv[0]);
        return -1;
    }
 
    init_crc_table();  
    ret = calc_img_crc(argv[1], &img_crc);  
    if (ret < 0) 
	{  
        exit(1);  
    }  
    printf("The %s crc32 is:%x\n", argv[1], img_crc);  
    return 0;  
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值