]> git.zerfleddert.de Git - proxmark3-svn/blame - common/crc.c
FIX: For ld (linker) there is no "-=", so lets try re-assign ldlibs instead
[proxmark3-svn] / common / crc.c
CommitLineData
bd20f8f4 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//-----------------------------------------------------------------------------
68d9d60a 8#include "crc.h"
ee4e2816 9#include "util.h"
73d04bb4 10#include <stdint.h>
11#include <stddef.h>
68d9d60a 12
13void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor)
14{
15 crc->order = order;
16 crc->polynom = polynom;
17 crc->initial_value = initial_value;
18 crc->final_xor = final_xor;
19 crc->mask = (1L<<order)-1;
20 crc_clear(crc);
21}
22
23void crc_update(crc_t *crc, uint32_t data, int data_width)
24{
ee4e2816 25 for( int i=0; i < data_width; i++) {
68d9d60a 26 int oldstate = crc->state;
27 crc->state = crc->state >> 1;
28 if( (oldstate^data) & 1 ) {
29 crc->state ^= crc->polynom;
30 }
31 data >>= 1;
32 }
33}
34
35void crc_clear(crc_t *crc)
36{
37 crc->state = crc->initial_value & crc->mask;
38}
39
40uint32_t crc_finish(crc_t *crc)
41{
42 return ( crc->state ^ crc->final_xor ) & crc->mask;
43}
73d04bb4 44
e74fc2ec 45//credits to iceman
6bb7609c 46uint32_t CRC8Maxim(uint8_t *buff, size_t size) {
73d04bb4 47 crc_t crc;
48 crc_init(&crc, 9, 0x8c, 0x00, 0x00);
49 crc_clear(&crc);
50
ee4e2816 51 for (size_t i=0; i < size; ++i)
73d04bb4 52 crc_update(&crc, buff[i], 8);
ee4e2816 53
73d04bb4 54 return crc_finish(&crc);
55}
ee4e2816 56
07f970aa 57//credits to iceman
ee4e2816 58uint32_t CRC8Legic(uint8_t *buff, size_t size) {
59
60 // Poly 0x63, reversed poly 0xC6, Init 0x55, Final 0x00
61 crc_t crc;
62 crc_init(&crc, 8, 0xC6, 0x55, 0);
63 crc_clear(&crc);
64
65 for ( int i = 0; i < size; ++i)
66 crc_update(&crc, buff[i], 8);
67 return SwapBits(crc_finish(&crc), 8);
68}
69
6bb7609c 70
Impressum, Datenschutz