C语言CRC-16 CCITT格式校验函数

news/2024/7/7 20:48:07

C语言CRC-16 CCITT格式校验函数

CRC-16校验产生2个字节长度的数据校验码,通过计算得到的校验码和获得的校验码比较,用于验证获得的数据的正确性。基本的CRC-16校验算法实现,参考: C语言标准CRC-16校验函数。

不同同应用规范通过对输入数据前处理和输出数据后处理的方式不同,又产生了不同的应用规范校验函数,这里介绍CCITT格式的CRC-16校验函数。CCITT格式对输入数据,按照单个字节进行位反序。对于输出的校验码,进行整体位反序。

生成多项式为x^16 + x^12 + x^5 + 1

正向算法

正向算法是符合标准CRC-16的计算理论,从左向右计算,也即计算过程中移位时,向左移出。几种正向算法的实现如下:

CRC-16 CCITT格式校验函数一(8位输入数据格式,64位装载计算):

#include <stdio.h>
#include <stdlib.h>
uint16_t PY_CRC_16_CCITT(uint8_t *di, uint32_t len)
{
    uint32_t crc_poly = 0x00011021;  //X^16+X^12+X^5+1 total 17 effective bits. Computed total data shall be compensated 16-bit '0' before CRC computing.

	uint8_t *datain;
	uint64_t cdata = 0; //Computed total data
    uint32_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+2)/6;
    uint32_t cr=(len+2)%6;

	uint32_t j;

	datain = malloc(len+2);
	for(j=0;j<len;j++)
	{
		datain[j] = 0;
		for(uint8_t m=0; m<=7; m++)
		{
			datain[j] |= ( ( di[j]>>(7-m) ) & 1 ) << m;
		}
	}
        datain[len] = 0; datain[len+1] = 0;//Compensate 16-bit '0' for input data

    if(len<=6)   //Mount data for only one segment
     {
    	 for(j=0;j<=(len+1);j++)
    	 {
    		 cdata = (cdata<<8);
    		 cdata = cdata|datain[j];
    	 }
    	 cn = 1;
     }
    else
     {
    	 if(cr==0)
    	 {
    		 cr = 6;
    	 }
         else if(cr==1)
         {
             cr = 7;

         }
         else if(cr==2)
         {
             cr = 8;

         }
    	 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-16));
 				{
 					data_t = data_t ^ crc_poly;
 				}

 	            while((index!=0x5555)&&(index!=0xaaaa))
 	            {

	 	    		for(uint8_t n=1;n<17;n++)
	 	    		{
	 	    			if ((data_t>>(16-n))&1) {rec = n;break;}
	 	    			if (n==16) rec=17;
	 	    		}

 	    			if((index-16)<rec)
 	    			{
 	    				data_t = data_t<<(index-16);
 	    				data_t |=  (uint32_t)((cdata<<(64-(index-16)))>>(64-(index-16)));
 	    				index = 0x5555;
 	    			}
 	    			else
 	    			{
 	        			for(uint8_t i=1;i<=rec;i++)
 	        			{
 	        				data_t = (data_t<<1)|((cdata>>(index-16-i))&1) ;
 	        			}

 	        			if(rec!= 17)
 	        			{
 	        				data_t = data_t ^ crc_poly;
 	        				index -= rec;
 	        			}
 	        			else
 	        			{
 	        				data_t = 0;
 	        				index_t = index-16-1;
 	        				index = 0xaaaa;

 	        			}

 	    			}

 	            }
 				if(index==0x5555) break;
 			}
 			else
 			{
 				index_t--;
 				if(index_t<16) break;
 			}
         }

 		if(cn>0) //next segment
 		{
  			cdata = data_t&0x00ffff;

 			for(uint8_t k=0;k<6;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);

     uint16_t i_data_t = 0;

     for(uint8_t n=0; n<=15; n++)
     {
    	 i_data_t |=  ( ( data_t>>(15-n) ) & 1 ) << n;
     }

     return i_data_t;
}

CRC-16 CCITT格式校验函数二(8位输入数据格式):

uint16_t PY_CRC_16_S_CCITT(uint8_t *di, uint32_t len)
{
    uint16_t crc_poly = 0x1021;  //X^16+X^12+X^5+1 total 16 effective bits without X^16. Computed total data shall be compensated 16-bit '0' before CRC computing.

	uint32_t clen = len+2;
	uint8_t cdata[clen] ;
	for(uint32_t j=0;j<len;j++)
	{
		cdata[j] = 0;

		for(uint8_t m=0; m<=7; m++)
		{
			cdata[j] |= ( ( di[j]>>(7-m) ) & 1 ) << m;
		}
	}
	cdata[len]=0; cdata[len+1]=0;

	uint16_t data_t = (((uint16_t)cdata[0]) << 8) + cdata[1]; //CRC register

    for (uint32_t i = 2; i < clen; i++)
    {
        for (uint8_t j = 0; j <= 7; j++)
        {
            if(data_t&0x8000)
            	data_t = ( (data_t<<1) | ( (cdata[i]>>(7-j))&0x01) ) ^ crc_poly;
            else
            	data_t = ( (data_t<<1) | ( (cdata[i]>>(7-j))&0x01) ) ;
        }
    }

    uint16_t i_data_t = 0;

    for(uint8_t n=0; n<=15; n++)
    {
   	 i_data_t |=  ( ( data_t>>(15-n) ) & 1 ) << n;
    }

    return i_data_t;
}

CRC-16 CCITT格式校验函数三(16位输入数据格式):

uint16_t PY_CRC_16_T16_CCITT(uint16_t *di, uint32_t len)
{
	uint16_t crc_poly = 0x1021;  //X^16+X^12+X^5+1 total 16 effective bits without X^16. 
	uint16_t data_t = 0; //CRC register

	uint16_t cdata[len];
	for(uint32_t j=0;j<len;j++)
	{
		cdata[j] = 0;
		for(uint8_t m=0; m<=7; m++)
		{
			cdata[j] |= ( ( ( (di[j]>>8)>>(7-m) ) & 1 ) << m ) | ( ( ( ( (di[j]&0x00ff)>>(7-m) ) & 1 ) << m ) <<8 );
		}

	}

    for(uint32_t i = 0; i < len; i++)
    {
    	data_t ^= cdata[i]; //16-bit data

        for (uint8_t j = 0; j < 16; j++)
        {
            if (data_t & 0x8000)
            	data_t = (data_t << 1) ^ crc_poly;
            else
            	data_t <<= 1;
        }
    }

    uint16_t i_data_t = 0;

    for(uint8_t n=0; n<=15; n++)
    {
   	 i_data_t |=  ( ( data_t>>(15-n) ) & 1 ) << n;
    }

    return i_data_t;
}

CRC-16 CCITT格式校验函数四(8位输入数据格式):

uint16_t PY_CRC_16_T8_CCITT(uint8_t *di, uint32_t len)
{
	uint16_t crc_poly = 0x1021;  //X^16+X^12+X^5+1 total 16 effective bits without X^16. 
	uint16_t data_t = 0; //CRC register

	uint8_t cdata[len];
	for(uint32_t j=0;j<len;j++)
	{
		cdata[j] = 0;
		for(uint8_t m=0; m<=7; m++)
		{
			cdata[j] |= ( ( di[j]>>(7-m) ) & 1 ) << m;
		}
	}

    for(uint32_t i = 0; i < len; i++)
    {
    	data_t ^= cdata[i]<<8; //8-bit data

        for (uint8_t j = 0; j < 8; j++)
        {
            if (data_t & 0x8000)
            	data_t = (data_t << 1) ^ crc_poly;
            else
            	data_t <<= 1;

        }
    }

    uint16_t i_data_t = 0;

    for(uint8_t n=0; n<=15; n++)
    {
   	 i_data_t |=  ( ( data_t>>(15-n) ) & 1 ) << n;
    }

    return i_data_t;
}

反向算法

反向算法是从由右向左计算,也即计算过程中移位时,向右移出。而计算过程中的输入数据高优先计算位和校验参数的对齐关系不变。因此把一个字节放在CRC计算寄存器的最低字节时,对于CCITT格式,最右侧最低位实际上是高优先计算位,而校验参数要相应倒序,从而计算位置对照关系不变。

CRC-16 CCITT格式校验函数五(反向算法,8位输入数据格式):

uint16_t PY_CRC_16_T8_CCITT_i(uint8_t *di, uint32_t len)
{
	uint16_t crc_poly = 0x8408; //Bit sequence inversion of 0x1021
	uint16_t data_t = 0; //CRC register

    for(uint32_t i = 0; i < len; i++)
    {
    	data_t ^= di[i]; //8-bit data

        for (uint8_t j = 0; j < 8; j++)
        {
            if (data_t & 0x0001)
            	data_t = (data_t >> 1) ^ crc_poly;
            else
            	data_t >>= 1;
        }
    }

    return data_t;
}

算法验证

5种算法结果相同:
在这里插入图片描述

通过在线CRC工具对照验证成功:
在这里插入图片描述

–End–


http://lihuaxi.xjx100.cn/news/1133170.html

相关文章

Javajdk8新特性中lambda 详解

Java 8 开始支持 lambda 表达式&#xff0c;是 Java 编程语言中对函数式编程的一种支持。Lambda 表达式也被称为闭包或匿名函数&#xff0c;可以让开发者轻松地写出简单而强大的代码&#xff0c;并带来显著的性能优势和更好的可读性。本文将详细介绍 Java Lambda 的概念、语法、…

怎样实现RPC框架

随着微服务架构的盛行&#xff0c;远程调用成了开发微服务必不可少的能力&#xff0c;RPC 框架作为微服务体系的底层支撑&#xff0c;也成了日常开发的必备工具。当下&#xff0c;RPC 框架已经不仅是进行远程调用的基础工具&#xff0c;还需要提供路由、服务发现、负载均衡、容…

九耶丨阁瑞钛伦特-springboot(一)

Spring Boot是一种基于Spring框架的快速应用开发框架。相比于传统的Spring框架&#xff0c;Spring Boot具有更加简洁明了的配置方式&#xff0c;可以帮助开发者快速搭建和部署项目&#xff0c;提高开发效率。 在工程技术领域&#xff0c;快速响应市场需求是企业发展的关键。传…

计算机网络 | 五种I/O模型

欢迎关注博主 Mindtechnist 或加入【Linux C/C/Python社区】一起学习和分享Linux、C、C、Python、Matlab&#xff0c;机器人运动控制、多机器人协作&#xff0c;智能优化算法&#xff0c;滤波估计、多传感器信息融合&#xff0c;机器学习&#xff0c;人工智能等相关领域的知识和…

什么是独享数据库(Database per Microservice)?解决了什么问题?

独享数据库&#xff08;Database per Microservice&#xff09;是一种微服务架构模式&#xff0c;涉及为每个微服务创建单独的数据库。在这种模式下&#xff0c;每个微服务都有自己的数据库&#xff0c;这允许更大的可扩展性、灵活性和自治性。 使用这种模式&#xff0c;每个微…

[day28]算法训练

房屋偷盗 解题思路 动态规划&#xff0c;创建一个数组dp&#xff0c;dp[i]表示偷到第i家时最大的金额&#xff0c;由于偷到相邻两家时&#xff0c;报警器会进行报警&#xff0c;所以有对于dp[i]有两种可能&#xff1a;第一种不偷第i家&#xff0c;则结果为dp[i-1],第二种偷第…

设计模式-职责链模式

职责链模式 文章目录 职责链模式什么是职责链模式为什么要用职责链模式如何使用职责链模式总结 什么是职责链模式 将请求的发送和接收解耦&#xff0c;让多个接收对象都有机会处理这个请求。将这些接收对象串成一条链&#xff0c;并沿着这条链传递这个请求&#xff0c;直到链上…

定风波、渡重山、至未来:2023中国数字能源生态大会开启的新旅程

全球碳中和的时代背景下&#xff0c;面向3060发展目标&#xff0c;新使命、新技术、新应用的到来&#xff0c;都给能源产业带来了持续变革的必要性与可能性。 2023年5月11日&#xff0c;在2023中国数字能源生态大会上&#xff0c;华为数字能源技术有限公司总裁侯金龙发表了“融…