libnfc  1.4.2
iso14443-subr.c
Go to the documentation of this file.
1 /*-
2  * Public platform independent Near Field Communication (NFC) library
3  *
4  * Copyright (C) 2009, Roel Verdult
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>
18  */
19 
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif // HAVE_CONFIG_H
28 
29 #include <stdio.h>
30 
31 #include <nfc/nfc.h>
32 
33 void
34 iso14443a_crc (byte_t * pbtData, size_t szLen, byte_t * pbtCrc)
35 {
36  byte_t bt;
37  uint32_t wCrc = 0x6363;
38 
39  do {
40  bt = *pbtData++;
41  bt = (bt ^ (byte_t) (wCrc & 0x00FF));
42  bt = (bt ^ (bt << 4));
43  wCrc = (wCrc >> 8) ^ ((uint32_t) bt << 8) ^ ((uint32_t) bt << 3) ^ ((uint32_t) bt >> 4);
44  } while (--szLen);
45 
46  *pbtCrc++ = (byte_t) (wCrc & 0xFF);
47  *pbtCrc = (byte_t) ((wCrc >> 8) & 0xFF);
48 }
49 
50 void
51 iso14443a_crc_append (byte_t * pbtData, size_t szLen)
52 {
53  iso14443a_crc (pbtData, szLen, pbtData + szLen);
54 }
55 
56 byte_t *
57 iso14443a_locate_historical_bytes(byte_t * pbtAts, size_t szAts, size_t * pszTk)
58 {
59  if (szAts) {
60  size_t offset = 1;
61  if (pbtAts[0] & 0x10) { // TA
62  offset++;
63  }
64  if (pbtAts[0] & 0x20) { // TB
65  offset++;
66  }
67  if (pbtAts[0] & 0x40) { // TC
68  offset++;
69  }
70  if (szAts > offset) {
71  *pszTk = (szAts-offset);
72  return (pbtAts+offset);
73  }
74  }
75  *pszTk = 0;
76  return NULL;
77 }