C语言CRC-8 ITU格式校验函数
CRC校验基于前处理和后处理的不同,由不同的协会标准推出了一些不同格式的版本。这里介绍CRC-8 ITU格式的校验函数。
CRC-8 ITU格式特征
标准CRC-8的校验函数参考: C语言标准CRC-8校验函数
CRC-8 ITU格式和标准CRC-8校验算法比较,增加了输出数据异或0x55的后处理,因此在标准CRC-8校验函数上增加此部分即可实现CRC-8 ITU格式校验函数。
CRC-8 ITU校验函数正向算法
正向算法是符合标准CRC-8的计算理论,从左向右计算,也即计算过程中移位时,向左移出。几种正向算法的实现如下:
CRC-8 ITU格式校验函数一(8位输入数据格式,64位装载计算):
#include <stdio.h>
#include <stdlib.h>
uint8_t PY_CRC_8_ITU(uint8_t *di, uint32_t len)
{ //Written by Pegasus Yu 2022/04/22
uint16_t crc_poly = 0x0107; //X^8+X^2+X^1+1 total 9 effective bits. Computed total data shall be compensated 8-bit '0' before CRC computing.
uint8_t *datain;
uint64_t cdata = 0; //Computed total data
uint16_t data_t = 0; //Process data of CRC computing
uint16_t index_t = 63; ///bit shifting index for initial '1' searching
uint16_t index = 63; //bit shifting index for CRC computing
uint8_t rec = 0; //bit number needed to be compensated for next CRC computing
uint32_t cn=(len+1)/7;
uint32_t cr=(len+1)%7;
uint32_t j;
datain = malloc(len+1);
for(j=0;j<len;j++)
{
datain[j]=di[j];
}
datain[len]=0; //Compensate 8-bit '0' for input data
if(len<=7)
{
for(j=0;j<=len;j++)
{
cdata = (cdata<<8);
cdata = cdata|datain[j];
}
cn = 1;
}
else
{
if(cr==0)
{
cr=7;
}
if(cr==1)
{
cr=8;
cn++;
}
else
{
cn++;
}
for(j=0;j<cr;j++)
{
cdata = (cdata<<8);
cdata = cdata|datain[j];
}
}
do
{
cn--;
while(index_t>0)
{
if( (cdata>>index_t)&1 )
{
index = index_t;
index_t = 0;
data_t |= (cdata>>(index-8));
{
data_t = data_t ^ crc_poly;
}
while((index!=0x5555)&&(index!=0xaaaa))
{
/*
if ((data_t>>7)&1) rec = 1;
else if ((data_t>>6)&1) rec = 2;
else if ((data_t>>5)&1) rec = 3;
else if ((data_t>>4)&1) rec = 4;
else if ((data_t>>3)&1) rec = 5;
else if ((data_t>>2)&1) rec = 6;
else if ((data_t>>1)&1) rec = 7;
else if ((data_t>>0)&1) rec = 8;
else rec = 9; */
for(uint8_t n=1;n<9;n++)
{
if ((data_t>>(8-n))&1) {rec = n;break;}
if (n==8) rec=9;
}
if((index-8)<rec)
{
data_t = data_t<<(index-8);
data_t |= (uint16_t)((cdata<<(64-(index-8)))>>(64-(index-8)));
index = 0x5555;
}
else
{
for(uint8_t i=1;i<=rec;i++)
{
data_t = (data_t<<1)|((cdata>>(index-8-i))&1) ;
}
if(rec!= 9)
{
data_t = data_t ^ crc_poly;
index -= rec;
}
else
{
data_t = 0;
index_t = index-8-1;
index = 0xaaaa;
}
}
}
if(index==0x5555) break;
}
else
{
index_t--;
if(index_t<8) break;
}
}
if(cn>0) //next segment
{
cdata = data_t&0x00ff;
for(uint8_t k=0;k<7;k++)
{
cdata = (cdata<<8);
cdata = cdata|datain[j++];
}
data_t = 0;
index_t = 63; ///bit shifting index for initial '1' searching
index = 63; //bit shifting index for CRC computing
rec = 0; //bit number needed to be compensated for next CRC computing
}
}
while(cn>0);
free(datain);
return (uint8_t)(data_t ^ 0x55);
}
CRC-8 ITU格式校验函数二(8位输入数据格式):
#include <stdio.h>
#include <stdlib.h>
uint8_t PY_CRC_8_S_ITU(uint8_t *di, uint32_t len)
{
uint8_t crc_poly = 0x07; //X^8+X^2+X^1+1 total 8 effective bits without X^8. Computed total data shall be compensated 8-bit '0' before CRC computing.
uint32_t clen = len+1;
uint8_t cdata[clen] ;
memcpy(cdata, di, len); cdata[len]=0;
uint8_t data_t = cdata[0]; //CRC register
for (uint32_t i = 1; i < clen; i++)
{
for (uint8_t j = 0; j <= 7; j++)
{
if(data_t&0x80)
data_t = ( (data_t<<1) | ( (cdata[i]>>(7-j))&0x01) ) ^ crc_poly;
else
data_t = ( (data_t<<1) | ( (cdata[i]>>(7-j))&0x01) ) ;
}
}
return (data_t ^ 0x55);
}
CRC-8 ITU格式校验函数三(8位输入数据格式):
uint8_t PY_CRC_8_T_ITU(uint8_t *di, uint32_t len)
{
uint8_t crc_poly = 0x07; //X^8+X^2+X^1+1 total 8 effective bits without X^8.
uint8_t data_t = 0;
while(len--)
{
data_t ^= *di++;
for (int8_t i=8; i>0; --i)
{
if (data_t & 0x80)
data_t = (data_t<<1) ^ crc_poly;
else
data_t = (data_t<<1);
}
}
return (data_t ^ 0x55);
}
算法验证
3种算法结果相同:
通过在线CRC工具对照验证成功:
–End–