fix missing gitignore
[RRG-proxmark3.git] / tools / mfc / card_reader / iso14443crc.c
blobc5acbb9bed8f3e1ca0e54dab7918a8b892ca08df
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 // ISO14443 CRC calculation code.
7 //-----------------------------------------------------------------------------
9 #include "iso14443crc.h"
11 static unsigned short UpdateCrc14443(unsigned char ch, unsigned short *lpwCrc) {
12 ch = (ch ^ (unsigned char)((*lpwCrc) & 0x00FF));
13 ch = (ch ^ (ch << 4));
14 *lpwCrc = (*lpwCrc >> 8) ^ ((unsigned short) ch << 8) ^
15 ((unsigned short) ch << 3) ^ ((unsigned short) ch >> 4);
16 return (*lpwCrc);
19 void ComputeCrc14443(int CrcType,
20 const unsigned char *Data, int Length,
21 unsigned char *TransmitFirst,
22 unsigned char *TransmitSecond) {
23 unsigned short wCrc = CrcType;
25 do {
26 unsigned char chBlock = *Data++;
27 UpdateCrc14443(chBlock, &wCrc);
28 } while (--Length);
30 if (CrcType == CRC_14443_B)
31 wCrc = ~wCrc; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
33 *TransmitFirst = (unsigned char)(wCrc & 0xFF);
34 *TransmitSecond = (unsigned char)((wCrc >> 8) & 0xFF);
35 return;
38 int CheckCrc14443(int CrcType, const unsigned char *Data, int Length) {
39 unsigned char b1;
40 unsigned char b2;
41 if (Length < 3) return 0;
42 ComputeCrc14443(CrcType, Data, Length - 2, &b1, &b2);
43 if ((b1 == Data[Length - 2]) && (b2 == Data[Length - 1])) return 1;
44 return 0;