[3181] | 1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
[8] | 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 | */
|
---|
[1743] | 39 | static unsigned char T1_Block_LRC (unsigned char * data, unsigned length);
|
---|
[8] | 40 |
|
---|
| 41 | /*
|
---|
| 42 | * Exported functions definition
|
---|
| 43 | */
|
---|
| 44 |
|
---|
[1743] | 45 | T1_Block * T1_Block_New (unsigned char * buffer, unsigned length)
|
---|
[8] | 46 | {
|
---|
| 47 | T1_Block * block;
|
---|
| 48 |
|
---|
| 49 | if (length < 4)
|
---|
| 50 | return NULL;
|
---|
| 51 |
|
---|
| 52 | block = (T1_Block *) malloc (sizeof (T1_Block));
|
---|
| 53 |
|
---|
[1743] | 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));
|
---|
[8] | 60 |
|
---|
| 61 | if (block->data != NULL)
|
---|
| 62 | memcpy (block->data, buffer, block->length);
|
---|
[1743] | 63 | else {
|
---|
[8] | 64 | free (block);
|
---|
| 65 | block = NULL;
|
---|
| 66 | }
|
---|
[1743] | 67 | }
|
---|
[8] | 68 | return block;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[1743] | 71 | T1_Block * T1_Block_NewIBlock (unsigned char len, unsigned char * inf, unsigned char ns, int more)
|
---|
[8] | 72 | {
|
---|
| 73 | T1_Block * block;
|
---|
| 74 | block = (T1_Block *) malloc (sizeof (T1_Block));
|
---|
[1743] | 75 | if (block != NULL) {
|
---|
[8] | 76 | block->length = len + 4;
|
---|
[1743] | 77 | block->data = (unsigned char *) calloc (block->length, sizeof (unsigned char));
|
---|
| 78 | if (block->data != NULL) {
|
---|
[8] | 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 | }
|
---|
[1743] | 88 | else {
|
---|
[8] | 89 | free (block);
|
---|
| 90 | block = NULL;
|
---|
| 91 | }
|
---|
[1743] | 92 | }
|
---|
[8] | 93 | return block;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[1743] | 96 | T1_Block * T1_Block_NewRBlock (unsigned char type, unsigned char nr)
|
---|
[8] | 97 | {
|
---|
| 98 | T1_Block * block;
|
---|
| 99 | block = (T1_Block *) malloc (sizeof (T1_Block));
|
---|
[1743] | 100 | if (block != NULL) {
|
---|
[8] | 101 | block->length = 4;
|
---|
[1743] | 102 | block->data = (unsigned char *) calloc (block->length, sizeof (unsigned char));
|
---|
| 103 | if (block->data != NULL) {
|
---|
[8] | 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);
|
---|
[1743] | 108 | }
|
---|
| 109 | else {
|
---|
[8] | 110 | free (block);
|
---|
| 111 | block = NULL;
|
---|
[1743] | 112 | }
|
---|
| 113 | }
|
---|
[8] | 114 | return block;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[1743] | 117 | T1_Block * T1_Block_NewSBlock (unsigned char type, unsigned char len, unsigned char * inf)
|
---|
[8] | 118 | {
|
---|
| 119 | T1_Block * block;
|
---|
| 120 | block = (T1_Block *) malloc (sizeof (T1_Block));
|
---|
[1743] | 121 | if (block != NULL) {
|
---|
[8] | 122 | block->length = 4 + len;
|
---|
[1743] | 123 | block->data = (unsigned char *) calloc (block->length, sizeof (unsigned char));
|
---|
| 124 | if (block->data != NULL) {
|
---|
[8] | 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);
|
---|
[1743] | 131 | }
|
---|
| 132 | else {
|
---|
[8] | 133 | free (block);
|
---|
| 134 | block = NULL;
|
---|
[1743] | 135 | }
|
---|
| 136 | }
|
---|
[8] | 137 | return block;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[1743] | 140 | unsigned char T1_Block_GetType (T1_Block * block)
|
---|
[8] | 141 | {
|
---|
| 142 | if ((block->data[1] & 0x80) == T1_BLOCK_I)
|
---|
| 143 | return T1_BLOCK_I;
|
---|
| 144 | return (block->data[1] & 0xEF);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[1743] | 147 | unsigned char T1_Block_GetNS (T1_Block * block)
|
---|
[8] | 148 | {
|
---|
| 149 | return ((block->data[1] >> 6)& 0x01);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[1743] | 152 | int T1_Block_GetMore (T1_Block * block)
|
---|
[8] | 153 | {
|
---|
| 154 | return ((block->data[1] >> 5) & 0x01);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[1743] | 157 | unsigned char T1_Block_GetNR (T1_Block * block)
|
---|
[8] | 158 | {
|
---|
| 159 | return ((block->data[1] >> 4) & 0x01);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[1743] | 162 | unsigned char T1_Block_GetLen (T1_Block * block)
|
---|
[8] | 163 | {
|
---|
| 164 | return block->data[2];
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[1743] | 167 | unsigned char * T1_Block_GetInf (T1_Block * block)
|
---|
[8] | 168 | {
|
---|
| 169 | if (block->length < 5)
|
---|
| 170 | return NULL;
|
---|
| 171 | return block->data + 3;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[1743] | 174 | void T1_Block_Delete (T1_Block * block)
|
---|
[8] | 175 | {
|
---|
| 176 | free (block->data);
|
---|
| 177 | free (block);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /*
|
---|
| 181 | * Not exported functions definition
|
---|
| 182 | */
|
---|
| 183 |
|
---|
[1743] | 184 | static unsigned char T1_Block_LRC (unsigned char * data, unsigned length)
|
---|
[8] | 185 | {
|
---|
[1743] | 186 | unsigned char lrc;
|
---|
[8] | 187 | unsigned i;
|
---|
| 188 | lrc = 0x00;
|
---|
| 189 | for (i = 0; i < length; i++)
|
---|
| 190 | lrc ^= data[i];
|
---|
| 191 | return lrc;
|
---|
| 192 | }
|
---|