crc16代码-标C实现

16位循环冗余校验

忘记最初的源码是从哪里获取的了,保存下代码用于备忘

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>


typedef unsigned long   u32;
typedef unsigned short  u16;
typedef unsigned char   u8;


#if     __CRC_LSB_MODE__
unsigned short do_crc16(unsigned short crc_val,unsigned char *message, unsigned int len)
{
    unsigned int i, j;
    unsigned short crc_reg =crc_val;
    unsigned short current = 0;
       
    for (i = 0; i < len; i++)
    {
        current = message[i];
        for (j = 0; j < 8; j++)
        {
            if ((crc_reg ^ current) & 0x0001)
                crc_reg = (crc_reg >> 1) ^ 0x8408;
            else
                crc_reg >>= 1;
            current >>= 1;           
        }
    }
    return crc_reg;
} 
#else
unsigned short do_crc16(unsigned short crc_val,unsigned char *message, unsigned int len)
{
    unsigned int i, j;
    unsigned short crc_reg = crc_val;
    unsigned short current = 0;
       
    for (i = 0; i < len; i++)
    {
        current = message[i] << 8;
        for (j = 0; j < 8; j++)
        {
            if ((short)(crc_reg ^ current) < 0)
                crc_reg = (crc_reg << 1) ^ 0x1021;
            else
                crc_reg <<= 1;
            current <<= 1;           
        }
    }
    return crc_reg;
}

#define __CRC_TEST_MODULE__     0

#if    0 //__CRC_TEST_MODULE__
/*
CRC-CCITT: 0x1021 = x16 + x12 + x5 + 1
Initial value: 0000
Final XOR Value: 0

input data:123456789
00 01 00 0b 31 32 33 34 35 36 37 38 39 31 c3

*/

/*
    测试代码
*/
int main(int argc,char*argv[])
{
        uint8_t databuff[378];
        u32 i=0,file_len=0;
        u16 crc_val=0;
        int len=0,fd=0;

        if(argc<2||argv[1]==NULL)
        {
                printf("check_crc [file]\n");
                exit(1);
        }       
        fd=open(argv[1],O_RDWR);
        if(fd<0)
        {
                perror("open error");
                exit(1);
        }
        file_len = getfilesize(fd);

        for(i=0;file_len;i++)
        {
                len=read(fd,databuff,sizeof(databuff));   
                if(len<=0)
                      break;
                file_len -=len;                
                crc_val=do_crc(crc_val,databuff,len);
        }
        printf("crc=[0x%04x]  %s\n",crc_val,argv[1]);

        return 0;
}

#endif

/*返回文件的大小*/
u32 get_file_size(int fd)
{
        struct stat statbuf;  
        fstat(fd, &statbuf);
        return statbuf.st_size;
}

/*返回文件的crc16值*/
unsigned short file_crc16(char * path)
{
        uint8_t databuff[256];
        u32 i=0,file_len=0;
        u16 crc_val=0;
        int len=0,fd=0;
        fd=open(path,O_RDONLY);
        if(fd<0)
        {
                return 0;
        }
        file_len = get_file_size(fd);

        for(i=0;file_len;i++)
        {
                len=read(fd,databuff,sizeof(databuff));   
                if(len<=0)
                      break;
                file_len -=len;                
                crc_val=do_crc16(crc_val,databuff,len);
        }
        close(fd);
        return crc_val;
}

#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值