CRC General algorithm of CRC implementation (I.)

Source: Internet
Author: User

Look at the CRC for two days, the online data can be broadly divided into two categories, or a large number of mathematical formulas, and finally only tell you to use the first shift the corresponding bit, and then use short division to find the remainder is the corresponding CRC code, or all code, and only a CRC generation polynomial. Both of these are difficult to understand oh, look at my head is big. Finally found a combination of theory and practice, only a little thought, think carefully, leaf out, wrote the corresponding general algorithm, I only verified the situation of n=4 and n=16, other if there is any problem you can tell me, we discuss together. If you want this information, I can collect what I have to send you, please leave the mailbox.

Okay, don't talk nonsense, the general algorithm is as follows:

/*
* CRC General Algorithm *
* Version 1.0 *
* by Yinpei *
* Wuhan University *
* November 18, 2006 *
*/
#define CRC_NUM_N//Defines the number of checksum bits per CRC
/*
Crc_num_32 = = 32
crc_num_24 = = 24
Crc_num_16 = = 16
Crc_num_12 = = 12
Crc_num_8 = = 8
Crc_num_4 = = 4
*/
#define LIMIT_MASK_N//define the maximum value of each CRC check code
/*
Limit_mask_32 = = 0xFFFFFFFF
limit_mask_24 = = 0xFFFFFF
Limit_mask_16 = = 0xFFFF
Limit_mask_12 = = 0xFFF
Limit_mask_8 = = 0xFF
Limit_mask_4 = = 0xF
*/
#define MSB_MASK_N//is used to detect if the CRC checksum will overflow after the left shift (the number of bits exceeds crc_num_n)
/*
Msb_mask_32 = = 0x80000000
msb_mask_24 = = 0x800000
Msb_mask_16 = = 0x8000
Msb_mask_12 = = 0x800
Msb_mask_8 = = 0x80
Msb_mask_4 = = 0x8
*/
#define R_N//The corresponding remainder polynomial of 1 corresponding to the N-generation polynomial corresponds to the CRC (0 corresponding remainder is 0)
/*
G_24 (D) =d24+d23+d6+d5+d+1:r_24 = = 0000 0000 0000 0110 0011 = 0x800063
G_16 (D) =d16+d12+d5+1:r_16 = = 1000000100001 = = 0x1021
G_12 (D) =d12+d11+d3+d2+d+1:r_12 = = 100000001111 = = 0x80f
G_8 (D) =d8+d7+d4+d3+d+1:r_8 = = 10011011 = = 0x9b
G_4 (D) =d4+d3+d2+1:r_4 = = 1101 = = 0xD
*/
n represents the highest order of the generation of the polynomial, commonly such as 32, 24, 16, 12, 8, 4
unsigned int cal_crcn (unsigned char *ptr, unsigned char len) {
unsigned int i;
unsigned int crc=0;
while (Len--! =0)
{
Here the iteration variable is 0x80, which means that the source data is stored in bytes, and each byte is iterated 8 times, processing each bit individually
for (i=0x80; i!=0; i/=2)
{
Whether the CRC check code will overflow after the left shift by Msb_mask_n detection
if ((crc&msb_mask_n)!=0)
{
If it overflows
crc*=2;
crc&=limit_mask_n;//take a valid bit to make it within the Crc_num_n
Crc^=r_n;
}/* Residual CRC times 2 and then CRC */
else crc*=2;
if ((*ptr&i)!=0) crc^=r_n; /* plus the standard CRC */
}
ptr++;
}
return (CRC);
}

Verification Program:

Main ()
{
Char test[]={0x01};
int RES_CRC;
RES_CRC=CAL_CRC16 (test,8);
printf ("res_crc=%x/n", RES_CRC);
}

Finally add two sentences, the general algorithm is a bitwise calculation, although the code is simple, the use of less memory, but its biggest disadvantage is that a one-off calculation will occupy a lot of processor processing time, especially in high-speed communications occasions, this disadvantage is not tolerated. I'll give you a more efficient general-purpose algorithm in the future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.