110 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			110 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include "crc.h"
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| uint8_t crc_crc8(const uint8_t *data,int num)
 | |
| {
 | |
|   uint8_t crc = 0;
 | |
|   uint16_t j,i;
 | |
|   for (j = 0; j < num; j++)
 | |
|   {
 | |
|     crc ^= *(data+j);
 | |
|     for ( i = 0; i < 8; i++)
 | |
|     {
 | |
|       if ((crc & 0x01) != 0)
 | |
|       {
 | |
|         crc >>= 1;
 | |
|         crc ^= 0x8c;
 | |
|       }
 | |
|       else
 | |
|       {
 | |
|         crc >>= 1;
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   return crc;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void crc_crc16(const uint8_t *data, int len,uint8_t *lb,uint8_t *hb)
 | |
| {
 | |
|   if (len > 0)
 | |
|   {
 | |
|     uint16_t crc = 0xFFFF;
 | |
|     int i = 0;
 | |
|     for (; i < len; i++)
 | |
|     {
 | |
|       crc = (uint16_t)(crc ^ (data[i]));
 | |
|       for (int j = 0; j < 8; j++)
 | |
|       {
 | |
|         crc = (crc & 1) != 0 ? (uint16_t)((crc >> 1) ^ 0xA001) : (uint16_t)(crc >> 1);
 | |
|       }
 | |
|     }
 | |
|     uint8_t hi = (uint8_t)((crc & 0xFF00) >> 8); //高位置
 | |
|     uint8_t lo = (uint8_t)(crc & 0x00FF); //低位置
 | |
|     *lb=lo;*hb=hi;
 | |
|   }
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| uint32_t crc_crc32(const uint8_t *data,int size)
 | |
| {
 | |
|   uint32_t temp,crc=0xffffffff;
 | |
|   int i=0,j=0;
 | |
|   if((size%4)!=0)
 | |
|   {
 | |
|     return 0;
 | |
|   }
 | |
|   while(i<size)
 | |
|   {
 | |
|     temp=data[i]|(data[i+1]<<8)|(data[i+2]<<16)|(data[i+3]<<24);
 | |
|     i+=4;
 | |
|     for(j=0;j<32;j++)
 | |
|     {
 | |
|       if((crc^temp)&0x80000000)
 | |
|         crc=0x04c11db7^(crc<<1);
 | |
|       else
 | |
|         crc<<=1;
 | |
|       temp<<=1;
 | |
|     }
 | |
|     crc&=0xffffffff;
 | |
|   }
 | |
|   return crc;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| void crc_sumcheck(const uint8_t *data,int size,uint8_t *chka,uint8_t *chkb) 
 | |
| {
 | |
|   if(chka==0) return;
 | |
|   if(chkb==0) return;
 | |
|   *chka=0;
 | |
|   *chkb=0;
 | |
| 	for(int i=0;i<size;i++)
 | |
| 	{
 | |
| 		*chka+=data[i];
 | |
| 		*chkb+=*chka;
 | |
| 	}
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | 
