/[pcsclite]/trunk/Drivers/ccid/src/towitoko/atr.c
ViewVC logotype

Contents of /trunk/Drivers/ccid/src/towitoko/atr.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 877 - (show annotations) (download)
Mon May 17 14:35:51 2004 UTC (9 years ago) by rousseau
File MIME type: text/plain
File size: 10175 byte(s)
reorganize header files
1 /*
2 atr.c
3 ISO 7816 ICC's answer to reset abstract data type implementation
4
5 This file is part of the Unix driver for Towitoko smartcard readers
6 Copyright (C) 2000 Carlos Prados <cprados@yahoo.com>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include "atr.h"
24 #include <stdlib.h>
25 #include <string.h>
26
27 /*
28 * Not exported variables definition
29 */
30
31 static unsigned long
32 atr_fs_table[16] =
33 {
34 0, 5000000L, 6000000L, 8000000L, 12000000L, 16000000L, 20000000L,
35 0, 0, 5000000L, 7500000L, 10000000L, 15000000L, 20000000L, 0, 0
36 };
37
38 static unsigned
39 atr_num_ib_table[16] =
40 {
41 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
42 };
43
44 /*
45 * Exported variables definition
46 */
47
48 unsigned
49 atr_f_table[16] =
50 {
51 0, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0
52 };
53
54 double
55 atr_d_table[16] =
56 {
57 0, 1, 2, 4, 8, 16, 0, 0, 0, 0, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625
58 };
59
60 unsigned
61 atr_i_table[4] =
62 {
63 25, 50, 100, 0
64 };
65
66 /*
67 * Exported funcions definition
68 */
69
70 ATR *
71 ATR_New (void)
72 {
73 ATR *atr;
74
75 /* Allocate memory */
76 atr = (ATR *) malloc (sizeof (ATR));
77
78 return atr;
79 }
80
81 int
82 ATR_InitFromArray (ATR * atr, BYTE atr_buffer[ATR_MAX_SIZE], unsigned length)
83 {
84 BYTE TDi;
85 BYTE buffer[ATR_MAX_SIZE];
86 unsigned pointer = 0, pn = 0;
87
88 /* Check size of buffer */
89 if (length < 2)
90 return (ATR_MALFORMED);
91
92 /* Check if ATR is from a inverse convention card */
93 if (atr_buffer[0] == 0x03)
94 {
95 for (pointer = 0; pointer < length; pointer++)
96 buffer[pointer] = ~(INVERT_BYTE (atr_buffer[pointer]));
97 }
98 else
99 memcpy (buffer, atr_buffer, length);
100
101 /* Store T0 and TS */
102 atr->TS = buffer[0];
103
104 atr->T0 = TDi = buffer[1];
105 pointer = 1;
106
107 /* Store number of historical bytes */
108 atr->hbn = TDi & 0x0F;
109
110 /* TCK is not present by default */
111 (atr->TCK).present = FALSE;
112
113 /* Extract interface bytes */
114 while (pointer < length)
115 {
116 /* Check buffer is long enought */
117 if (pointer + atr_num_ib_table[(0xF0 & TDi) >> 4] >= length)
118 {
119 return (ATR_MALFORMED);
120 }
121 /* Check TAi is present */
122 if ((TDi | 0xEF) == 0xFF)
123 {
124 pointer++;
125 atr->ib[pn][ATR_INTERFACE_BYTE_TA].value = buffer[pointer];
126 atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = TRUE;
127 }
128 else
129 atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = FALSE;
130 /* Check TBi is present */
131 if ((TDi | 0xDF) == 0xFF)
132 {
133 pointer++;
134 atr->ib[pn][ATR_INTERFACE_BYTE_TB].value = buffer[pointer];
135 atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = TRUE;
136 }
137 else
138 atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = FALSE;
139
140 /* Check TCi is present */
141 if ((TDi | 0xBF) == 0xFF)
142 {
143 pointer++;
144 atr->ib[pn][ATR_INTERFACE_BYTE_TC].value = buffer[pointer];
145 atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = TRUE;
146 }
147 else
148 atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = FALSE;
149
150 /* Read TDi if present */
151 if ((TDi | 0x7F) == 0xFF)
152 {
153 pointer++;
154 TDi = atr->ib[pn][ATR_INTERFACE_BYTE_TD].value = buffer[pointer];
155 atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = TRUE;
156 (atr->TCK).present = ((TDi & 0x0F) != ATR_PROTOCOL_TYPE_T0);
157 if (pn >= ATR_MAX_PROTOCOLS)
158 return (ATR_MALFORMED);
159 pn++;
160 }
161 else
162 {
163 atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = FALSE;
164 break;
165 }
166 }
167
168 /* Store number of protocols */
169 atr->pn = pn + 1;
170
171 /* Store historical bytes */
172 if (pointer + atr->hbn >= length)
173 return (ATR_MALFORMED);
174
175 memcpy (atr->hb, buffer + pointer + 1, atr->hbn);
176 pointer += (atr->hbn);
177
178 /* Store TCK */
179 if ((atr->TCK).present)
180 {
181
182 if (pointer + 1 >= length)
183 return (ATR_MALFORMED);
184
185 pointer++;
186
187 (atr->TCK).value = buffer[pointer];
188 }
189
190 atr->length = pointer + 1;
191 return (ATR_OK);
192 }
193
194
195 void
196 ATR_Delete (ATR * atr)
197 {
198 free (atr);
199 }
200
201 int
202 ATR_GetConvention (ATR * atr, int *convention)
203 {
204 if (atr->TS == 0x3B)
205 (*convention) = ATR_CONVENTION_DIRECT;
206 else if (atr->TS == 0x3F)
207 (*convention) = ATR_CONVENTION_INVERSE;
208 else
209 return (ATR_MALFORMED);
210 return (ATR_OK);
211 }
212
213 int
214 ATR_GetSize (ATR * atr, unsigned *size)
215 {
216 (*size) = atr->length;
217 return (ATR_OK);
218 }
219
220 int
221 ATR_GetNumberOfProtocols (ATR * atr, unsigned *number_protocols)
222 {
223 (*number_protocols) = atr->pn;
224 return (ATR_OK);
225 }
226
227 int
228 ATR_GetProtocolType (ATR * atr, unsigned number_protocol, BYTE *protocol_type)
229 {
230 if ((number_protocol > atr->pn) || number_protocol < 2)
231 return ATR_NOT_FOUND;
232
233 if (atr->ib[number_protocol - 2][ATR_INTERFACE_BYTE_TD].present)
234 (*protocol_type) =
235 (atr->ib[number_protocol - 2][ATR_INTERFACE_BYTE_TD].value & 0x0F);
236 else
237 (*protocol_type) = ATR_PROTOCOL_TYPE_T0;
238
239 return (ATR_OK);
240 }
241
242 int
243 ATR_GetInterfaceByte (ATR * atr, unsigned number, int character, BYTE * value)
244 {
245 if (number > atr->pn || number < 1)
246 return (ATR_NOT_FOUND);
247
248 if (atr->ib[number - 1][character].present &&
249 (character == ATR_INTERFACE_BYTE_TA ||
250 character == ATR_INTERFACE_BYTE_TB ||
251 character == ATR_INTERFACE_BYTE_TC ||
252 character == ATR_INTERFACE_BYTE_TD))
253 (*value) = atr->ib[number - 1][character].value;
254 else
255 return (ATR_NOT_FOUND);
256
257 return (ATR_OK);
258 }
259
260 int
261 ATR_GetIntegerValue (ATR * atr, int name, BYTE * value)
262 {
263 int ret;
264
265 if (name == ATR_INTEGER_VALUE_FI)
266 {
267 if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present)
268 {
269 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0xF0) >> 4;
270 ret = ATR_OK;
271 }
272 else
273 ret = ATR_NOT_FOUND;
274 }
275
276 else if (name == ATR_INTEGER_VALUE_DI)
277 {
278 if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present)
279 {
280 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0x0F);
281 ret = ATR_OK;
282 }
283 else
284 ret = ATR_NOT_FOUND;
285 }
286
287 else if (name == ATR_INTEGER_VALUE_II)
288 {
289 if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present)
290 {
291 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x60) >> 5;
292 ret = ATR_OK;
293 }
294 else
295 ret = ATR_NOT_FOUND;
296 }
297
298 else if (name == ATR_INTEGER_VALUE_PI1)
299 {
300 if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present)
301 {
302 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x1F);
303 ret = ATR_OK;
304 }
305 else
306 ret = ATR_NOT_FOUND;
307 }
308
309 else if (name == ATR_INTEGER_VALUE_PI2)
310 {
311 if (atr->ib[1][ATR_INTERFACE_BYTE_TB].present)
312 {
313 (*value) = atr->ib[1][ATR_INTERFACE_BYTE_TB].value;
314 ret = ATR_OK;
315 }
316 else
317 ret = ATR_NOT_FOUND;
318 }
319
320 else if (name == ATR_INTEGER_VALUE_N)
321 {
322 if (atr->ib[0][ATR_INTERFACE_BYTE_TC].present)
323 {
324 (*value) = atr->ib[0][ATR_INTERFACE_BYTE_TC].value;
325 ret = ATR_OK;
326 }
327 else
328 ret = ATR_NOT_FOUND;
329 }
330 else
331 ret = ATR_NOT_FOUND;
332
333 return ret;
334 }
335
336 int
337 ATR_GetParameter (ATR * atr, int name, double *parameter)
338 {
339 BYTE FI, DI, II, PI1, PI2, N;
340
341 if (name == ATR_PARAMETER_F)
342 {
343 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &FI) == ATR_OK)
344 (*parameter) = (double) (atr_f_table[FI]);
345 else
346 (*parameter) = (double) ATR_DEFAULT_F;
347 return (ATR_OK);
348 }
349
350 else if (name == ATR_PARAMETER_D)
351 {
352 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_DI, &DI) == ATR_OK)
353 (*parameter) = (double) (atr_d_table[DI]);
354 else
355 (*parameter) = (double) ATR_DEFAULT_D;
356 return (ATR_OK);
357 }
358
359 else if (name == ATR_PARAMETER_I)
360 {
361 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_II, &II) == ATR_OK)
362 (*parameter) = (double) (atr_i_table[II]);
363 else
364 (*parameter) = ATR_DEFAULT_I;
365 return (ATR_OK);
366 }
367
368 else if (name == ATR_PARAMETER_P)
369 {
370 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI2, &PI2) == ATR_OK)
371 (*parameter) = (double) PI2;
372 else if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI1, &PI1) == ATR_OK)
373 (*parameter) = (double) PI1;
374 else
375 (*parameter) = (double) ATR_DEFAULT_P;
376 return (ATR_OK);
377 }
378
379 else if (name == ATR_PARAMETER_N)
380 {
381 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_N, &N) == ATR_OK)
382 (*parameter) = (double) N;
383 else
384 (*parameter) = (double) ATR_DEFAULT_N;
385 return (ATR_OK);
386 }
387
388 return (ATR_NOT_FOUND);
389 }
390
391 int
392 ATR_GetHistoricalBytes (ATR * atr, BYTE hist[ATR_MAX_HISTORICAL], unsigned *length)
393 {
394 if (atr->hbn == 0)
395 return (ATR_NOT_FOUND);
396
397 (*length) = atr->hbn;
398 memcpy (hist, atr->hb, atr->hbn);
399 return (ATR_OK);
400 }
401
402 int
403 ATR_GetRaw (ATR * atr, BYTE buffer[ATR_MAX_SIZE], unsigned *length)
404 {
405 unsigned i, j;
406
407 buffer[0] = atr->TS;
408 buffer[1] = atr->T0;
409
410 j = 2;
411
412 for (i = 0; i < atr->pn; i++)
413 {
414 if (atr->ib[i][ATR_INTERFACE_BYTE_TA].present)
415 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TA].value;
416
417 if (atr->ib[i][ATR_INTERFACE_BYTE_TB].present)
418 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TB].value;
419
420 if (atr->ib[i][ATR_INTERFACE_BYTE_TC].present)
421 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TC].value;
422
423 if (atr->ib[i][ATR_INTERFACE_BYTE_TD].present)
424 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TD].value;
425 }
426
427 if (atr->hbn > 0)
428 {
429 memcpy (&(buffer[j]), atr->hb, atr->hbn);
430 j += atr->hbn;
431 }
432
433 if ((atr->TCK).present)
434 buffer[j++] = (atr->TCK).value;
435
436 (*length) = j;
437
438 return ATR_OK;
439 }
440
441 int
442 ATR_GetCheckByte (ATR * atr, BYTE * check_byte)
443 {
444 if (!((atr->TCK).present))
445 return (ATR_NOT_FOUND);
446
447 (*check_byte) = (atr->TCK).value;
448 return (ATR_OK);
449 }
450
451 int
452 ATR_GetFsMax (ATR * atr, unsigned long *fsmax)
453 {
454 BYTE FI;
455
456 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &FI) == ATR_OK)
457 (*fsmax) = atr_fs_table[FI];
458 else
459 (*fsmax) = atr_fs_table[1];
460
461 return (ATR_OK);
462 }
463

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5