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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5