source: trunk/csctapi/t1_block.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

File size: 4.9 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2/*
3 t1_block.h
4 T=1 block abstract data type implementation
5
6 This file is part of the Unix driver for Towitoko smartcard readers
7 Copyright (C) 2000 Carlos Prados <cprados@yahoo.com>
8
9 This version is modified by doz21 to work in a special manner ;)
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24*/
25
26#include "t1_block.h"
27#include <stdlib.h>
28#include <string.h>
29
30/*
31 * Not exported constants definition
32 */
33
34#define T1_BLOCK_NAD 0x00
35
36/*
37 * Not exported functions declaration
38 */
39static unsigned char T1_Block_LRC (unsigned char * data, unsigned length);
40
41/*
42 * Exported functions definition
43 */
44
45T1_Block * T1_Block_New (unsigned char * buffer, unsigned length)
46{
47 T1_Block * block;
48
49 if (length < 4)
50 return NULL;
51
52 block = (T1_Block *) malloc (sizeof (T1_Block));
53
54 if (block != NULL) {
55 if (length > T1_BLOCK_MAX_SIZE)
56 block->length = T1_BLOCK_MAX_SIZE;
57 else
58 block->length = length;
59 block->data = (unsigned char *) calloc (block->length, sizeof (unsigned char));
60
61 if (block->data != NULL)
62 memcpy (block->data, buffer, block->length);
63 else {
64 free (block);
65 block = NULL;
66 }
67 }
68 return block;
69}
70
71T1_Block * T1_Block_NewIBlock (unsigned char len, unsigned char * inf, unsigned char ns, int more)
72{
73 T1_Block * block;
74 block = (T1_Block *) malloc (sizeof (T1_Block));
75 if (block != NULL) {
76 block->length = len + 4;
77 block->data = (unsigned char *) calloc (block->length, sizeof (unsigned char));
78 if (block->data != NULL) {
79 block->data[0] = T1_BLOCK_NAD;
80 block->data[1] = T1_BLOCK_I | ((ns << 6) & 0x40);
81 if (more)
82 block->data[1] |= 0x20;
83 block->data[2] = len;
84 if (len != 0x00)
85 memcpy (block->data + 3, inf, len);
86 block->data[len+3] = T1_Block_LRC (block->data, len+3);
87 }
88 else {
89 free (block);
90 block = NULL;
91 }
92 }
93 return block;
94}
95
96T1_Block * T1_Block_NewRBlock (unsigned char type, unsigned char nr)
97{
98 T1_Block * block;
99 block = (T1_Block *) malloc (sizeof (T1_Block));
100 if (block != NULL) {
101 block->length = 4;
102 block->data = (unsigned char *) calloc (block->length, sizeof (unsigned char));
103 if (block->data != NULL) {
104 block->data[0] = T1_BLOCK_NAD;
105 block->data[1] = type | ((nr << 4) & 0x10);
106 block->data[2] = 0x00;
107 block->data[3] = T1_Block_LRC (block->data, 3);
108 }
109 else {
110 free (block);
111 block = NULL;
112 }
113 }
114 return block;
115}
116
117T1_Block * T1_Block_NewSBlock (unsigned char type, unsigned char len, unsigned char * inf)
118{
119 T1_Block * block;
120 block = (T1_Block *) malloc (sizeof (T1_Block));
121 if (block != NULL) {
122 block->length = 4 + len;
123 block->data = (unsigned char *) calloc (block->length, sizeof (unsigned char));
124 if (block->data != NULL) {
125 block->data[0] = T1_BLOCK_NAD;
126 block->data[1] = type;
127 block->data[2] = len;
128 if (len != 0x00)
129 memcpy (block->data + 3, inf, len);
130 block->data[len+3] = T1_Block_LRC (block->data, len+3);
131 }
132 else {
133 free (block);
134 block = NULL;
135 }
136 }
137 return block;
138}
139
140unsigned char T1_Block_GetType (T1_Block * block)
141{
142 if ((block->data[1] & 0x80) == T1_BLOCK_I)
143 return T1_BLOCK_I;
144 return (block->data[1] & 0xEF);
145}
146
147unsigned char T1_Block_GetNS (T1_Block * block)
148{
149 return ((block->data[1] >> 6)& 0x01);
150}
151
152int T1_Block_GetMore (T1_Block * block)
153{
154 return ((block->data[1] >> 5) & 0x01);
155}
156
157unsigned char T1_Block_GetNR (T1_Block * block)
158{
159 return ((block->data[1] >> 4) & 0x01);
160}
161
162unsigned char T1_Block_GetLen (T1_Block * block)
163{
164 return block->data[2];
165}
166
167unsigned char * T1_Block_GetInf (T1_Block * block)
168{
169 if (block->length < 5)
170 return NULL;
171 return block->data + 3;
172}
173
174void T1_Block_Delete (T1_Block * block)
175{
176 free (block->data);
177 free (block);
178}
179
180/*
181 * Not exported functions definition
182 */
183
184static unsigned char T1_Block_LRC (unsigned char * data, unsigned length)
185{
186 unsigned char lrc;
187 unsigned i;
188 lrc = 0x00;
189 for (i = 0; i < length; i++)
190 lrc ^= data[i];
191 return lrc;
192}
Note: See TracBrowser for help on using the repository browser.