1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // Generic CRC calculation code.
7 //-----------------------------------------------------------------------------
8 // the Check value below in the comments is CRC of the string '123456789'
12 #include "commonutil.h"
14 void crc_init_ref(crc_t
*crc
, int order
, uint32_t polynom
, uint32_t initial_value
, uint32_t final_xor
, bool refin
, bool refout
) {
15 crc_init(crc
, order
, polynom
, initial_value
, final_xor
);
21 void crc_init(crc_t
*crc
, int order
, uint32_t polynom
, uint32_t initial_value
, uint32_t final_xor
) {
23 crc
->topbit
= BITMASK(order
- 1);
24 crc
->polynom
= polynom
;
25 crc
->initial_value
= initial_value
;
26 crc
->final_xor
= final_xor
;
27 crc
->mask
= (1L << order
) - 1;
33 void crc_clear(crc_t
*crc
) {
35 crc
->state
= crc
->initial_value
& crc
->mask
;
37 crc
->state
= reflect(crc
->state
, crc
->order
);
40 void crc_update2(crc_t
*crc
, uint32_t data
, int data_width
) {
43 data
= reflect(data
, data_width
);
45 // Bring the next byte into the remainder.
46 crc
->state
^= data
<< (crc
->order
- data_width
);
48 for (uint8_t bit
= data_width
; bit
> 0; --bit
) {
50 if (crc
->state
& crc
->topbit
)
51 crc
->state
= (crc
->state
<< 1) ^ crc
->polynom
;
53 crc
->state
= (crc
->state
<< 1);
57 void crc_update(crc_t
*crc
, uint32_t data
, int data_width
) {
59 data
= reflect(data
, data_width
);
62 for (i
= 0; i
< data_width
; i
++) {
63 int oldstate
= crc
->state
;
64 crc
->state
= crc
->state
>> 1;
65 if ((oldstate
^ data
) & 1) {
66 crc
->state
^= crc
->polynom
;
72 uint32_t crc_finish(crc_t
*crc
) {
73 uint32_t val
= crc
->state
;
75 val
= reflect(val
, crc
->order
);
76 return (val
^ crc
->final_xor
) & crc
->mask
;
80 static void print_crc(crc_t *crc) {
81 printf(" Order %d\n Poly %x\n Init %x\n Final %x\n Mask %x\n topbit %x\n RefIn %s\n RefOut %s\n State %x\n",
88 (crc->refin) ? "TRUE":"FALSE",
89 (crc->refout) ? "TRUE":"FALSE",
95 // width=8 poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xA1 name="CRC-8/MAXIM"
96 uint32_t CRC8Maxim(uint8_t *buff
, size_t size
) {
98 crc_init_ref(&crc
, 8, 0x31, 0, 0, true, true);
99 for (size_t i
= 0; i
< size
; ++i
)
100 crc_update2(&crc
, buff
[i
], 8);
101 return crc_finish(&crc
);
103 // width=8 poly=0x1d, init=0xc7 (0xe3 - WRONG! but it mentioned in MAD datasheet) refin=false refout=false xorout=0x00 name="CRC-8/MIFARE-MAD"
104 uint32_t CRC8Mad(uint8_t *buff
, size_t size
) {
106 crc_init_ref(&crc
, 8, 0x1d, 0xc7, 0, false, false);
107 for (size_t i
= 0; i
< size
; ++i
)
108 crc_update2(&crc
, buff
[i
], 8);
109 return crc_finish(&crc
);
111 // width=4 poly=0xC, reversed poly=0x7 init=0x5 refin=true refout=true xorout=0x0000 check= name="CRC-4/LEGIC"
112 uint32_t CRC4Legic(uint8_t *buff
, size_t size
) {
114 crc_init_ref(&crc
, 4, 0x19 >> 1, 0x5, 0, true, true);
115 crc_update2(&crc
, 1, 1); /* CMD_READ */
116 crc_update2(&crc
, buff
[0], 8);
117 crc_update2(&crc
, buff
[1], 8);
118 return reflect(crc_finish(&crc
), 4);
120 // width=8 poly=0x63, reversed poly=0x8D init=0x55 refin=true refout=true xorout=0x0000 check=0xC6 name="CRC-8/LEGIC"
121 // the CRC needs to be reversed before returned.
122 uint32_t CRC8Legic(uint8_t *buff
, size_t size
) {
124 crc_init_ref(&crc
, 8, 0x63, 0x55, 0, true, true);
125 for (size_t i
= 0; i
< size
; ++i
)
126 crc_update2(&crc
, buff
[i
], 8);
127 return reflect8(crc_finish(&crc
));
129 // width=8 poly=0x107, init=0x2C refin=true refout=true xorout=0x0000 check=0 name="CRC-8/CARDX"
130 uint32_t CRC8Cardx(uint8_t *buff
, size_t size
) {
132 crc_init_ref(&crc
, 8, 0x107, 0x2C, 0, true, true);
133 for (size_t i
= 0; i
< size
; ++i
)
134 crc_update2(&crc
, buff
[i
], 8);
135 return crc_finish(&crc
);