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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 670 - (show annotations) (download)
Tue Feb 3 15:42:24 2004 UTC (9 years, 3 months ago) by rousseau
File MIME type: text/plain
File size: 4945 byte(s)
use my own debug routines
1 /*
2 t1_block.h
3 T=1 block 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 "t1_block.h"
24 #include <stdlib.h>
25 #include <string.h>
26
27 /*
28 * Not exported constants definition
29 */
30
31 #define T1_BLOCK_NAD 0x00
32
33 /*
34 * Not exported functions declaration
35 */
36 static BYTE
37 T1_Block_LRC (BYTE * data, unsigned length);
38
39 /*
40 * Exported functions definition
41 */
42
43 T1_Block *
44 T1_Block_New (BYTE * buffer, unsigned length)
45 {
46 T1_Block * block;
47
48 if (length < 4)
49 return NULL;
50
51 block = (T1_Block *) malloc (sizeof (T1_Block));
52
53 if (block != NULL)
54 {
55 block->length = MIN(length, T1_BLOCK_MAX_SIZE);
56 block->data = (BYTE *) calloc (block->length, sizeof (BYTE));
57
58 if (block->data != NULL)
59 {
60 memcpy (block->data, buffer, block->length);
61 }
62 else
63 {
64 free (block);
65 block = NULL;
66 }
67 }
68
69 return block;
70 }
71
72 T1_Block *
73 T1_Block_NewIBlock (BYTE len, BYTE * inf, BYTE ns, bool more)
74 {
75 T1_Block * block;
76
77 block = (T1_Block *) malloc (sizeof (T1_Block));
78
79 if (block != NULL)
80 {
81 block->length = len + 4;
82 block->data = (BYTE *) calloc (block->length, sizeof (BYTE));
83
84 if (block->data != NULL)
85 {
86 block->data[0] = T1_BLOCK_NAD;
87 block->data[1] = T1_BLOCK_I | ((ns << 6) & 0x40);
88
89 if (more)
90 block->data[1] |= 0x20;
91
92 block->data[2] = len;
93
94 if (len != 0x00)
95 memcpy (block->data + 3, inf, len);
96
97 block->data[len+3] = T1_Block_LRC (block->data, len+3);
98 }
99 else
100 {
101 free (block);
102 block = NULL;
103 }
104 }
105
106 return block;
107 }
108
109 T1_Block *
110 T1_Block_NewRBlock (BYTE type, BYTE nr)
111 {
112 T1_Block * block;
113
114 block = (T1_Block *) malloc (sizeof (T1_Block));
115
116 if (block != NULL)
117 {
118 block->length = 4;
119 block->data = (BYTE *) calloc (block->length, sizeof (BYTE));
120
121 if (block->data != NULL)
122 {
123 block->data[0] = T1_BLOCK_NAD;
124 block->data[1] = type | ((nr << 4) & 0x10);
125 block->data[2] = 0x00;
126 block->data[3] = T1_Block_LRC (block->data, 3);
127 }
128 else
129 {
130 free (block);
131 block = NULL;
132 }
133 }
134
135 return block;
136 }
137
138 T1_Block *
139 T1_Block_NewSBlock (BYTE type, BYTE len, BYTE * inf)
140 {
141 T1_Block * block;
142
143 block = (T1_Block *) malloc (sizeof (T1_Block));
144
145 if (block != NULL)
146 {
147 block->length = 4 + len;
148 block->data = (BYTE *) calloc (block->length, sizeof (BYTE));
149
150 if (block->data != NULL)
151 {
152 block->data[0] = T1_BLOCK_NAD;
153 block->data[1] = type;
154 block->data[2] = len;
155
156 if (len != 0x00)
157 memcpy (block->data + 3, inf, len);
158
159 block->data[len+3] = T1_Block_LRC (block->data, len+3);
160 }
161 else
162 {
163 free (block);
164 block = NULL;
165 }
166 }
167
168 return block;
169 }
170
171 BYTE
172 T1_Block_GetType (T1_Block * block)
173 {
174 if ((block->data[1] & 0x80) == T1_BLOCK_I)
175 return T1_BLOCK_I;
176
177 return (block->data[1] & 0xEF);
178 }
179
180 BYTE
181 T1_Block_GetNS (T1_Block * block)
182 {
183 return ((block->data[1] >> 6)& 0x01);
184 }
185
186 bool
187 T1_Block_GetMore (T1_Block * block)
188 {
189 return ((block->data[1] >> 5) & 0x01);
190 }
191
192 BYTE
193 T1_Block_GetNR (T1_Block * block)
194 {
195 return ((block->data[1] >> 4) & 0x01);
196 }
197
198 BYTE
199 T1_Block_GetLen (T1_Block * block)
200 {
201 return block->data[2];
202 }
203
204 BYTE *
205 T1_Block_GetInf (T1_Block * block)
206 {
207 if (block->length < 5)
208 return NULL;
209
210 return block->data + 3;
211 }
212
213 BYTE *
214 T1_Block_Raw (T1_Block * block)
215 {
216 return block->data;
217 }
218
219 unsigned
220 T1_Block_RawLen (T1_Block * block)
221 {
222 return block->length;
223 }
224
225 void
226 T1_Block_Delete (T1_Block * block)
227 {
228 free (block->data);
229 free (block);
230 }
231
232 /*
233 * Not exported functions definition
234 */
235
236 static BYTE
237 T1_Block_LRC (BYTE * data, unsigned length)
238 {
239 BYTE lrc;
240 unsigned i;
241
242 lrc = 0x00;
243 for (i = 0; i < length; i++)
244 {
245 lrc ^= data[i];
246 }
247
248 return lrc;
249 }
250

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5