循环冗余校验(Cyclic Redundancy Check)算法

#ifndef _CRC32_H_
#define _CRC32_H_

/*
* 循环冗余校验(Cyclic Redundancy Check, CRC)
*/

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

#define ARRAY_SIZE(x)   ((sizeof(x))/(sizeof(x[0])))

#define CRC_NORMAL      0x00
#define CRC_REVERSE     0x01

#define CRC32_0X04C11DB7        0x04c11db7
#define CRC32_0xEDB88320        0xedb88320

/*
* 生成CRC32校验字段表
* flag:顺序异或/反序异或
* poly:生成多项式
*/
extern void crc_init(u8 flag, u32 poly);

/*
* 计算CRC32 checksum
* flag:顺序异或/反序异或
* init_value:初始值
* ptr:数据首地址
* len:数据长度
*/
extern u32 crc_calc(u8 flag, u32 init_value, u8 *ptr, u32 len);

#endif /* crc32.h */
#include "crc32.h"

static u32 crc_tbl[256] = { 0 };

extern void crc_init(u8 flag, u32 poly)
{
    u16 i, j;
    u32 tmp = 0;

    for (i = 0; i < 256; i++) {
        tmp = i;
        if (CRC_NORMAL == flag) {
            tmp = tmp << 24;
            for (j = 0; j < 8; j ++) {
                if (tmp & 0x80000000) {
                    tmp = (tmp << 1) ^ poly;
                } else {
                    tmp = tmp << 1;
                }
            }
        } else { /* if (CRC_REVERSE == flag) */
            for (j = 0; j < 8; j ++) {
                if (tmp & 0x00000001) {
                    tmp = (tmp >> 1) ^ poly;
                } else {
                    tmp = tmp >> 1;
                }
            }
        }
        crc_tbl[i] = tmp;
    }
}

extern u32 crc_calc(u8 flag, u32 init_value, u8 *ptr, u32 len)
{
    u32 crc_res;

    if (NULL == ptr)
        goto err_ptr;

    if (0 == len)
        goto err_len;

    crc_res = init_value;

    if (CRC_NORMAL == flag) {
        while (len --) {
            crc_res = crc_tbl[((crc_res >> 24) ^ *ptr) & 0xFF] ^ ((crc_res << 8) & 0xFFFFFF00);
            ptr ++;
        }
    } else { /* if (CRC_REVERSE == flag) */
        while (len --) {
            crc_res = crc_tbl[(crc_res ^ *ptr) & 0xFF] ^ ((crc_res >> 8) & 0x00FFFFFF);
            ptr ++;
        }
        crc_res = crc_res ^ 0xFFFFFFFF;
    }

err_ptr:
err_len:
    return (crc_res);
}
#if 0
    crc_init(CRC_NORMAL, CRC32_0X04C11DB7);
#else
    crc_init(CRC_REVERSE, CRC32_0xEDB88320);
#endif
    printf("crc = %x\n", crc_calc(CRC_REVERSE, 0xFFFFFFFF, ch8_test, ARRAY_SIZE(ch8_test)));
#if 0
    for (u16 i = 0; i < 256; i++) {
        printf("0x%08X\n", crc_tbl[i]);
    }
#endif

#define cb_CRC_POLY 0x2F

u8 calc_crc8(u8 *data_byte_array, u8 data_byte_array_size)
{
    u8 byte_index, bit_index;
    u8 crc = 0xFF;                           // initial value = 0xFF

    for (byte_index = 0; byte_index < data_byte_array_size; ++ byte_index) {
        crc ^= data_byte_array[byte_index];
        for (bit_index = 0; bit_index < 8; ++ bit_index) {
            if ((crc & 0x80) != 0)
                crc = (crc << 1) ^ cb_CRC_POLY;
            else
                crc = (crc << 1);
           }
    }

    return 0xFF ^ crc;   //return inverse value
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值