initial checkin of harald welte's original librfid project
[rfid/librfid.git] / src / rfid_iso14443_common.c
1
2 /*
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License version 2 
5  *  as published by the Free Software Foundation
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17
18 static unsigned int fsdi_table[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 };
19
20 int iso14443_fsdi_to_fsd(unsigned int *fsd, unsigned char fsdi)
21 {
22         /* ISO 14443-4:2000(E) Section 5.1. */
23         if (fsdi > sizeof(fsdi_table)/sizeof(unsigned int))
24                 return -1;
25
26         *fsd = fsdi_table[fsdi];
27         return 0;
28 }
29
30 int iso14443_fsd_to_fsdi(unsigned char *fsdi, unsigned int fsd)
31 {
32         int i;
33
34         for (i = 0; i < sizeof(fsdi_table)/sizeof(unsigned int); i++) {
35                 if (fsdi_table[i] == fsd) {
36                         *fsdi = i;
37                         return 0;
38                 }
39         }
40
41         return -1;
42 }
43
44 /* calculate the fsd that is equal or the closest smaller value
45  * that can be coded as fsd */
46 unsigned int iso14443_fsd_approx(unsigned int fsd)
47 {
48         unsigned int tbl_size = sizeof(fsdi_table)/sizeof(unsigned int);
49         int i;
50
51         for (i = tbl_size-1; i >= 0; i--) {
52                 if (fsdi_table[i] <= fsd)
53                         return fsdi_table[i];
54         }
55         /* not reached: return smallest possible FSD */
56         return fsdi_table[0];
57 }
58