[IT] CRC, is how it happened?

 

1. Why is the CRC?

answer:

Data may be changed, you need to check if it is to change, and can not take up too much of bytes, so with the checksum.

Is circulated to a byte (8 bits) of a byte is calculated so as to check whether data is changed.

 

2. Modify the CRC check must be out of it?

answer:

No, but you can check out a certain probability.

Parity is a special case belonging to the CRC.

Therefore, in order to better check, there is a polynomial.

Better polynomial, a higher probability of inspection data is changed.

Often referred to binary trigger calculate CRC, the dividend is polynomial (polynomial).

  CCITT CRC16 CRC32
Checksum bit width W 16 16 32
Generating polynomial x16+x12+x5+1 x16+x15+x2+1 x32+x26+x23+x22+x16+ x12+x11+x10+x8+x7+x5+ x4+x2+x1+1
Divisor (polynomial) 0x1021 0x8005 0x04C11DB7
The initial value of the remainder 0xFFFF 0x0000 0xFFFFFFFF
XOR result value 0x0000 0x0000 0xFFFFFFFF

 

 

3. crc16 algorithm code table Fan query method, what?

 

 1 #include <stdio.h>
 2 #include <stdint.h>
 3 
 4 #define POLY        0x1021
 5 
 6 uint16_t crc16(unsigned char *addr, int num, uint16_t crc)
 7 {
 8     int i;
 9 
10     while (num--) {
11         crc = crc ^ (*addr++ << 8);
12         for (i = 0; i < 8; i++) {
13             if (crc & 0x8000) {
14                 crc = (crc << 1) ^ POLY;
15             } else {
16                 crc <<= 1;
17             }
18         }
19         crc &= 0xFFFF;
20     }
21     return crc;
22 }
23 
24 int main(void)
25 {
26     uint8_t data[] = {0x02, 0x05};
27 
28     printf("%04X\n", crc16(data, 2, 0xFFFF));
29 
30     return 0;
31 }

 

4. crc16 is a logical part of the process is what?

    a, 11-line operations, the lower 8 bits of crc change, only 8 bits are high crc modified

    b, 12 lines to 18 lines: the upper 8 bits will determine how many times the fixed number POLY (polynomial) exclusive-OR operation.

 

5. Table query method, is how it happened?

    Q: Why table query method?

    A: Because the above is a bit a bit operation. So more time to calculate the data, it is affecting the efficiency.

 

    Q: How do table lookup method is optimized?

    answer:

        a, XOR operation is associative, commutative. + And operates similarly.

        B, assuming an 8-bit number, respectively, H1, H2, H3, H4, L1, L2, L3, L4 represented.

              V1, V2, V3, V4, V5, V6, V7, V8 represents POLY (polynomial) binary.

                

 

 

               Down XOR operation, high four low 4 will determine how many times and POLY XOR.

               Due to the XOR operation is associative, commutative. The above operation of the XOR operation is equivalent to XOR below.

               Therefore, when the calculation is repeated more time, the following calculation result between H1 and Vx can be saved to a table, a subsequent query to use directly.

               CRC check process is due to a one byte (8 bits), the table 2 there are eight power = 256.

 

6, an example of the look-up table:

 

 1 #include <stdio.h>
 2 
 3 #include <stdint.h>
 4 
 5 #define POLY    0x1021
 6 #define INIT    0xFFFF
 7 #define FINAL   0x0000
 8 
 9 typedef uint16_t width_t;
10 
11 #define WIDTH   (8 * sizeof(width_t))
12 #define TOPBIT  (1 << (WIDTH - 1))
13 
14 uint16_t crc16(unsigned char *addr, int num, uint16_t crc)
15 {
16     int i;
17 
18     while (num--) {
19         crc = crc ^ (*addr++ << 8);
20         for (i = 0; i < 8; i++) {
21             if (crc & 0x8000) {
22                 crc = (crc << 1) ^ POLY;
23             } else {
24                 crc <<= 1;
25             }
26         }
27         crc &= 0xFFFF;
28     }
29     return crc;
30 }
31 
32 
33 
34 static width_t crc_table[256];
35 
36 void crc_init(void)
37 {
38     width_t reg;
39     width_t i, j;
40 
41     for (i = 0; i < 256; i++) {
42         reg = i << (WIDTH - 8);
43         for(j = 0; j < 8; j++) {
44             if(reg & TOPBIT) {
45                 reg = (reg << 1) ^ POLY;
46             } else {
47                 reg = reg << 1;
48             }
49         }
50         crc_table[i] = reg;
51     }
52 }
53 
54 width_t crc_compute(unsigned char *addr, unsigned int num)
55 {
56     unsigned int i;
57     unsigned int high;    
58     width_t reg = INIT;
59 
60     for (i = 0; i < num; i++) {
61         high = (reg >> (WIDTH - 8)) ^ addr[i];
62         reg = crc_table[high] ^ (reg << 8);
63     }
64 
65     return (reg ^ FINAL);
66 }
67 
68 
69 int main(void)
70 {
71     uint8_t data[] = {0x02, 0x05};
72 
73     printf("%04X\n", crc16(data, 2, 0xFFFF));
74 
75     crc_init();
76     printf("%04X\n", crc_compute(data, 2));
77 
78     return 0;
79 }

 

 

reference:

[Getting cyclic redundancy check (CRC) algorithm boot] https://blog.csdn.net/liyuanbhu/article/details/7882789

[CRC lookup table method to derive and implement the code comparison] https://blog.csdn.net/huang_shiyang/article/details/50881305

【A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS】https://wenku.baidu.com/view/6e700a3b31126edb6f1a1084.html

 【On-line CRC calculation and free library】https://www.lammertbies.nl/comm/info/crc-calculation.html

Guess you like

Origin www.cnblogs.com/YBhello/p/11518196.html