]> git.zerfleddert.de Git - proxmark3-svn/blob - common/crc.c
90d57afab6a56c90bdfbc4b8840b718ffea247cf
[proxmark3-svn] / common / crc.c
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
4 // the license.
5 //-----------------------------------------------------------------------------
6 // Generic CRC calculation code.
7 //-----------------------------------------------------------------------------
8
9 #include "crc.h"
10
11 void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor)
12 {
13 crc->order = order;
14 crc->polynom = polynom;
15 crc->initial_value = initial_value;
16 crc->final_xor = final_xor;
17 crc->mask = (1L<<order)-1;
18 crc_clear(crc);
19 }
20
21 void crc_update(crc_t *crc, uint32_t data, int data_width)
22 {
23 int i;
24 for(i=0; i<data_width; i++) {
25 int oldstate = crc->state;
26 crc->state = crc->state >> 1;
27 if( (oldstate^data) & 1 ) {
28 crc->state ^= crc->polynom;
29 }
30 data >>= 1;
31 }
32 }
33
34 void crc_clear(crc_t *crc)
35 {
36 crc->state = crc->initial_value & crc->mask;
37 }
38
39 uint32_t crc_finish(crc_t *crc)
40 {
41 return ( crc->state ^ crc->final_xor ) & crc->mask;
42 }
Impressum, Datenschutz