Initial commit
[platform/upstream/ccid.git] / src / utils.c
1 /*
2     utils.c:
3     Copyright (C) 2003-2008   Ludovic Rousseau
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14
15         You should have received a copy of the GNU Lesser General Public License
16         along with this library; if not, write to the Free Software Foundation,
17         Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <string.h>
21 #include <pcsclite.h>
22
23 #include <config.h>
24 #include "ccid.h"
25 #include "defs.h"
26 #include "ccid_ifdhandler.h"
27 #include "utils.h"
28 #include "debug.h"
29
30 int ReaderIndex[CCID_DRIVER_MAX_READERS];
31
32 void InitReaderIndex(void)
33 {
34         int i;
35
36         for (i=0; i<CCID_DRIVER_MAX_READERS; i++)
37                 ReaderIndex[i] = -1;
38 } /* InitReaderIndex */
39
40 int GetNewReaderIndex(const int Lun)
41 {
42         int i;
43
44         /* check that Lun is NOT already used */
45         for (i=0; i<CCID_DRIVER_MAX_READERS; i++)
46                 if (Lun == ReaderIndex[i])
47                         break;
48
49         if (i < CCID_DRIVER_MAX_READERS)
50         {
51                 DEBUG_CRITICAL2("Lun: %d is already used", Lun);
52                 return -1;
53         }
54
55         for (i=0; i<CCID_DRIVER_MAX_READERS; i++)
56                 if (-1 == ReaderIndex[i])
57                 {
58                         ReaderIndex[i] = Lun;
59                         return i;
60                 }
61
62         DEBUG_CRITICAL("ReaderIndex[] is full");
63         return -1;
64 } /* GetReaderIndex */
65
66 int LunToReaderIndex(const int Lun)
67 {
68         int i;
69
70         for (i=0; i<CCID_DRIVER_MAX_READERS; i++)
71                 if (Lun == ReaderIndex[i])
72                         return i;
73
74         DEBUG_CRITICAL2("Lun: %X not found", Lun);
75         return -1;
76 } /* LunToReaderIndex */
77
78 void ReleaseReaderIndex(const int index)
79 {
80         ReaderIndex[index] = -1;
81 } /* ReleaseReaderIndex */
82
83 /* Read a non aligned 16-bit integer */
84 uint16_t get_U16(void *buf)
85 {
86         uint16_t value;
87
88         memcpy(&value, buf, sizeof value);
89
90         return value;
91 }
92
93 /* Read a non aligned 32-bit integer */
94 uint32_t get_U32(void *buf)
95 {
96         uint32_t value;
97
98         memcpy(&value, buf, sizeof value);
99
100         return value;
101 }
102
103 /* Write a non aligned 16-bit integer */
104 void set_U16(void *buf, uint16_t value)
105 {
106         memcpy(buf, &value, sizeof value);
107 }
108
109 /* Write a non aligned 32-bit integer */
110 void set_U32(void *buf, uint32_t value)
111 {
112         memcpy(buf, &value, sizeof value);
113 }
114
115 /* swap a 16-bits integer in memory */
116 /* "AB" -> "BA" */
117 void p_bswap_16(void *ptr)
118 {
119         uint8_t *array, tmp;
120
121         array = ptr;
122         tmp = array[0];
123         array[0] = array[1];
124         array[1] = tmp;
125 }
126
127 /* swap a 32-bits integer in memory */
128 /* "ABCD" -> "DCBA" */
129 void p_bswap_32(void *ptr)
130 {
131         uint8_t *array, tmp;
132
133         array = ptr;
134         tmp = array[0];
135         array[0] = array[3];
136         array[3] = tmp;
137
138         tmp = array[1];
139         array[1] = array[2];
140         array[2] = tmp;
141 }