Debian Codegraph @

PCSC.xs

raw symbols SCIP
1 /**************************************************************************
2 * Authors : Lionel VICTOR <[email protected]>
3 * <[email protected]>
4 * Ludovic ROUSSEAU <[email protected]>
5 * Compiler : gcc, Visual C++
6 * Target : Unix, Windows
7 *
8 * Description : Perl wrapper to the PCSC API
9 *
10 * Copyright (C) 2001 - Lionel VICTOR
11 * Copyright (c) 2003-2015 Ludovic ROUSSEAU
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 * 02111-1307 USA
27 *
28 **************************************************************************/
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #include <stdlib.h>
35 #include "PCSCperl.h"
36
37 #include "EXTERN.h"
38 #include "perl.h"
39 #include "XSUB.h"
40
41
42 /* The following hack helps importing variables from the PCSC library.
43 * Those variables are dynamically imported...
44 * TODO: see how we can use them anyway...
45 */
46 #ifdef SCARD_PCI_T0
47 # undef SCARD_PCI_T0
48 # define SCARD_PCI_T0 (gpioSCardT0Pci)
49 #endif
50
51 #ifdef SCARD_PCI_T1
52 # undef SCARD_PCI_T1
53 # define SCARD_PCI_T1 (gpioSCardT1Pci)
54 #endif
55
56 #ifdef SCARD_PCI_RAW
57 # undef SCARD_PCI_RAW
58 # define SCARD_PCI_RAW (gpioSCardRawPci)
59 #endif
60
61 #ifdef __cplusplus
62 }
63 #endif
64
65 /* InitErrorCodes () initializes and creates a few variables describing
66 * important values like error codes and constants. Those values are set
67 * read-only so the user won't be able to eventually modify them.
68 * Those variables are set in the XS because their numerical value is
69 * platform dependent.
70 */
71 void _InitErrorCodes () {
72 SV * tmpSV;
73
74 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_S_SUCCESS", TRUE);
75 sv_setiv (tmpSV, SCARD_S_SUCCESS); SvREADONLY_on (tmpSV);
76 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_CANCELLED", TRUE);
77 sv_setiv (tmpSV, SCARD_E_CANCELLED); SvREADONLY_on (tmpSV);
78 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_CANT_DISPOSE", TRUE);
79 sv_setiv (tmpSV, SCARD_E_CANT_DISPOSE); SvREADONLY_on (tmpSV);
80 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_INSUFFICIENT_BUFFER", TRUE);
81 sv_setiv (tmpSV, SCARD_E_INSUFFICIENT_BUFFER); SvREADONLY_on (tmpSV);
82 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_INVALID_ATR", TRUE);
83 sv_setiv (tmpSV, SCARD_E_INVALID_ATR); SvREADONLY_on (tmpSV);
84 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_INVALID_HANDLE", TRUE);
85 sv_setiv (tmpSV, SCARD_E_INVALID_HANDLE); SvREADONLY_on (tmpSV);
86 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_INVALID_PARAMETER", TRUE);
87 sv_setiv (tmpSV, SCARD_E_INVALID_PARAMETER); SvREADONLY_on (tmpSV);
88 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_INVALID_TARGET", TRUE);
89 sv_setiv (tmpSV, SCARD_E_INVALID_TARGET); SvREADONLY_on (tmpSV);
90 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_INVALID_VALUE", TRUE);
91 sv_setiv (tmpSV, SCARD_E_INVALID_VALUE); SvREADONLY_on (tmpSV);
92 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_NO_MEMORY", TRUE);
93 sv_setiv (tmpSV, SCARD_E_NO_MEMORY); SvREADONLY_on (tmpSV);
94 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_UNKNOWN_READER", TRUE);
95 sv_setiv (tmpSV, SCARD_E_UNKNOWN_READER); SvREADONLY_on (tmpSV);
96 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_TIMEOUT", TRUE);
97 sv_setiv (tmpSV, SCARD_E_TIMEOUT); SvREADONLY_on (tmpSV);
98 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_SHARING_VIOLATION", TRUE);
99 sv_setiv (tmpSV, SCARD_E_SHARING_VIOLATION); SvREADONLY_on (tmpSV);
100 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_NO_SMARTCARD", TRUE);
101 sv_setiv (tmpSV, SCARD_E_NO_SMARTCARD); SvREADONLY_on (tmpSV);
102 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_UNKNOWN_CARD", TRUE);
103 sv_setiv (tmpSV, SCARD_E_UNKNOWN_CARD); SvREADONLY_on (tmpSV);
104 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_PROTO_MISMATCH", TRUE);
105 sv_setiv (tmpSV, SCARD_E_PROTO_MISMATCH); SvREADONLY_on (tmpSV);
106 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_NOT_READY", TRUE);
107 sv_setiv (tmpSV, SCARD_E_NOT_READY); SvREADONLY_on (tmpSV);
108 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_SYSTEM_CANCELLED", TRUE);
109 sv_setiv (tmpSV, SCARD_E_SYSTEM_CANCELLED); SvREADONLY_on (tmpSV);
110 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_NOT_TRANSACTED", TRUE);
111 sv_setiv (tmpSV, SCARD_E_NOT_TRANSACTED); SvREADONLY_on (tmpSV);
112 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_READER_UNAVAILABLE", TRUE);
113 sv_setiv (tmpSV, SCARD_E_READER_UNAVAILABLE); SvREADONLY_on (tmpSV);
114 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_PCI_TOO_SMALL", TRUE);
115 sv_setiv (tmpSV, SCARD_E_PCI_TOO_SMALL); SvREADONLY_on (tmpSV);
116 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_READER_UNSUPPORTED", TRUE);
117 sv_setiv (tmpSV, SCARD_E_READER_UNSUPPORTED); SvREADONLY_on (tmpSV);
118 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_DUPLICATE_READER", TRUE);
119 sv_setiv (tmpSV, SCARD_E_DUPLICATE_READER); SvREADONLY_on (tmpSV);
120 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_CARD_UNSUPPORTED", TRUE);
121 sv_setiv (tmpSV, SCARD_E_CARD_UNSUPPORTED); SvREADONLY_on (tmpSV);
122 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_NO_SERVICE", TRUE);
123 sv_setiv (tmpSV, SCARD_E_NO_SERVICE); SvREADONLY_on (tmpSV);
124 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_SERVICE_STOPPED", TRUE);
125 sv_setiv (tmpSV, SCARD_E_SERVICE_STOPPED); SvREADONLY_on (tmpSV);
126 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_E_UNSUPPORTED_FEATURE", TRUE);
127 sv_setiv (tmpSV, SCARD_E_UNSUPPORTED_FEATURE); SvREADONLY_on (tmpSV);
128 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_W_UNSUPPORTED_CARD", TRUE);
129 sv_setiv (tmpSV, SCARD_W_UNSUPPORTED_CARD); SvREADONLY_on (tmpSV);
130 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_W_UNRESPONSIVE_CARD", TRUE);
131 sv_setiv (tmpSV, SCARD_W_UNRESPONSIVE_CARD); SvREADONLY_on (tmpSV);
132 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_W_UNPOWERED_CARD", TRUE);
133 sv_setiv (tmpSV, SCARD_W_UNPOWERED_CARD); SvREADONLY_on (tmpSV);
134 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_W_RESET_CARD", TRUE);
135 sv_setiv (tmpSV, SCARD_W_RESET_CARD); SvREADONLY_on (tmpSV);
136 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_W_SECURITY_VIOLATION", TRUE);
137 sv_setiv (tmpSV, SCARD_W_SECURITY_VIOLATION); SvREADONLY_on (tmpSV);
138 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_W_REMOVED_CARD", TRUE);
139 sv_setiv (tmpSV, SCARD_W_REMOVED_CARD); SvREADONLY_on (tmpSV);
140
141 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_F_COMM_ERROR", TRUE);
142 sv_setiv (tmpSV, SCARD_F_COMM_ERROR); SvREADONLY_on (tmpSV);
143 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_F_INTERNAL_ERROR", TRUE);
144 sv_setiv (tmpSV, SCARD_F_INTERNAL_ERROR); SvREADONLY_on (tmpSV);
145 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_F_UNKNOWN_ERROR", TRUE);
146 sv_setiv (tmpSV, SCARD_F_UNKNOWN_ERROR); SvREADONLY_on (tmpSV);
147 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_F_WAITED_TOO_LONG", TRUE);
148 sv_setiv (tmpSV, SCARD_F_WAITED_TOO_LONG); SvREADONLY_on (tmpSV);
149
150 /* PCSC - Perl wrapper specific error codes */
151 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_P_ALREADY_CONNECTED", TRUE);
152 sv_setiv (tmpSV, SCARD_P_ALREADY_CONNECTED); SvREADONLY_on (tmpSV);
153 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_P_NOT_CONNECTED", TRUE);
154 sv_setiv (tmpSV, SCARD_P_NOT_CONNECTED); SvREADONLY_on (tmpSV);
155
156 /* PCSC standard constants */
157 /*
158 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_CONVENTION_DIRECT", TRUE);
159 sv_setiv (tmpSV, SCARD_CONVENTION_DIRECT); SvREADONLY_on (tmpSV);
160 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_CONVENTION_INVERSE", TRUE);
161 sv_setiv (tmpSV, SCARD_CONVENTION_INVERSE); SvREADONLY_on (tmpSV);
162 */
163 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_SCOPE_USER", TRUE);
164 sv_setiv (tmpSV, SCARD_SCOPE_USER); SvREADONLY_on (tmpSV);
165 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_SCOPE_TERMINAL", TRUE);
166 sv_setiv (tmpSV, SCARD_SCOPE_TERMINAL); SvREADONLY_on (tmpSV);
167 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_SCOPE_SYSTEM", TRUE);
168 sv_setiv (tmpSV, SCARD_SCOPE_SYSTEM); SvREADONLY_on (tmpSV);
169 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_PROTOCOL_T0", TRUE);
170 sv_setiv (tmpSV, SCARD_PROTOCOL_T0); SvREADONLY_on (tmpSV);
171 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_PROTOCOL_T1", TRUE);
172 sv_setiv (tmpSV, SCARD_PROTOCOL_T1); SvREADONLY_on (tmpSV);
173 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_PROTOCOL_RAW", TRUE);
174 sv_setiv (tmpSV, SCARD_PROTOCOL_RAW); SvREADONLY_on (tmpSV);
175 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE", TRUE);
176 sv_setiv (tmpSV, SCARD_SHARE_EXCLUSIVE); SvREADONLY_on (tmpSV);
177 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_SHARE_SHARED", TRUE);
178 sv_setiv (tmpSV, SCARD_SHARE_SHARED); SvREADONLY_on (tmpSV);
179 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_SHARE_DIRECT", TRUE);
180 sv_setiv (tmpSV, SCARD_SHARE_DIRECT); SvREADONLY_on (tmpSV);
181 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_LEAVE_CARD", TRUE);
182 sv_setiv (tmpSV, SCARD_LEAVE_CARD); SvREADONLY_on (tmpSV);
183 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_RESET_CARD", TRUE);
184 sv_setiv (tmpSV, SCARD_RESET_CARD); SvREADONLY_on (tmpSV);
185 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_UNPOWER_CARD", TRUE);
186 sv_setiv (tmpSV, SCARD_UNPOWER_CARD); SvREADONLY_on (tmpSV);
187 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_EJECT_CARD", TRUE);
188 sv_setiv (tmpSV, SCARD_EJECT_CARD); SvREADONLY_on (tmpSV);
189 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_UNKNOWN", TRUE);
190 sv_setiv (tmpSV, SCARD_UNKNOWN); SvREADONLY_on (tmpSV);
191 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_ABSENT", TRUE);
192 sv_setiv (tmpSV, SCARD_ABSENT); SvREADONLY_on (tmpSV);
193 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_PRESENT", TRUE);
194 sv_setiv (tmpSV, SCARD_PRESENT); SvREADONLY_on (tmpSV);
195 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_SWALLOWED", TRUE);
196 sv_setiv (tmpSV, SCARD_SWALLOWED); SvREADONLY_on (tmpSV);
197 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_POWERED", TRUE);
198 sv_setiv (tmpSV, SCARD_POWERED); SvREADONLY_on (tmpSV);
199 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_NEGOTIABLE", TRUE);
200 sv_setiv (tmpSV, SCARD_NEGOTIABLE); SvREADONLY_on (tmpSV);
201 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_SPECIFIC", TRUE);
202 sv_setiv (tmpSV, SCARD_SPECIFIC); SvREADONLY_on (tmpSV);
203 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_UNAWARE", TRUE);
204 sv_setiv (tmpSV, SCARD_STATE_UNAWARE); SvREADONLY_on (tmpSV);
205 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_IGNORE", TRUE);
206 sv_setiv (tmpSV, SCARD_STATE_IGNORE); SvREADONLY_on (tmpSV);
207 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_CHANGED", TRUE);
208 sv_setiv (tmpSV, SCARD_STATE_CHANGED); SvREADONLY_on (tmpSV);
209 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_UNKNOWN", TRUE);
210 sv_setiv (tmpSV, SCARD_STATE_UNKNOWN); SvREADONLY_on (tmpSV);
211 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_UNAVAILABLE", TRUE);
212 sv_setiv (tmpSV, SCARD_STATE_UNAVAILABLE); SvREADONLY_on (tmpSV);
213 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_EMPTY", TRUE);
214 sv_setiv (tmpSV, SCARD_STATE_EMPTY); SvREADONLY_on (tmpSV);
215 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_PRESENT", TRUE);
216 sv_setiv (tmpSV, SCARD_STATE_PRESENT); SvREADONLY_on (tmpSV);
217 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_ATRMATCH", TRUE);
218 sv_setiv (tmpSV, SCARD_STATE_ATRMATCH); SvREADONLY_on (tmpSV);
219 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_EXCLUSIVE", TRUE);
220 sv_setiv (tmpSV, SCARD_STATE_EXCLUSIVE); SvREADONLY_on (tmpSV);
221 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_INUSE", TRUE);
222 sv_setiv (tmpSV, SCARD_STATE_INUSE); SvREADONLY_on (tmpSV);
223 tmpSV = perl_get_sv ("Chipcard::PCSC::SCARD_STATE_MUTE", TRUE);
224 sv_setiv (tmpSV, SCARD_STATE_MUTE); SvREADONLY_on (tmpSV);
225 }
226
227 /* _StringifyError is copied from pcsc_stringify_error() which is a
228 * function taken from PCSClite
229 * It has been modified because I added a few internal errors to the
230 * wrapper (SCARD_P_NOT_CONNECTED for instance)
231 * I also feel like using strong types like const char * const and
232 * avoiding to use strcpy() is better from a security point of view.
233 *
234 * See file debuglog.c from your PCSClite distribution for extra
235 * informations
236 */
237 const char * _StringifyError (unsigned long Error) {
238 switch ( (DWORD)Error ) {
239 case SCARD_S_SUCCESS: return "Command successful.";
240 case SCARD_E_CANCELLED: return "Command cancelled.";
241 case SCARD_E_CANT_DISPOSE: return "Cannot dispose handle.";
242 case SCARD_E_CARD_UNSUPPORTED: return "Card is unsupported.";
243 case SCARD_E_DUPLICATE_READER: return "Reader already exists.";
244 case SCARD_E_INSUFFICIENT_BUFFER: return "Insufficient buffer.";
245 case SCARD_E_INVALID_ATR: return "Invalid ATR.";
246 case SCARD_E_INVALID_HANDLE: return "Invalid handle.";
247 case SCARD_E_INVALID_PARAMETER: return "Invalid parameter given.";
248 case SCARD_E_INVALID_TARGET: return "Invalid target given.";
249 case SCARD_E_INVALID_VALUE: return "Invalid value given.";
250 case SCARD_E_NO_MEMORY: return "Not enough memory.";
251 case SCARD_E_NO_SERVICE: return "Service not available.";
252 case SCARD_E_NO_SMARTCARD: return "No smartcard inserted.";
253 case SCARD_E_NOT_READY: return "Subsystem not ready.";
254 case SCARD_E_NOT_TRANSACTED: return "Transaction failed.";
255 case SCARD_E_PCI_TOO_SMALL: return "PCI struct too small.";
256 case SCARD_E_PROTO_MISMATCH: return "Card protocol mismatch.";
257 case SCARD_E_READER_UNAVAILABLE: return "Reader/s is unavailable.";
258 case SCARD_E_READER_UNSUPPORTED: return "Reader is unsupported.";
259 case SCARD_E_SERVICE_STOPPED: return "Service was stopped.";
260 case SCARD_E_SHARING_VIOLATION: return "Sharing violation.";
261 case SCARD_E_SYSTEM_CANCELLED: return "System cancelled.";
262 case SCARD_E_TIMEOUT: return "Command timeout.";
263 case SCARD_E_UNKNOWN_CARD: return "Unknown card.";
264 case SCARD_E_UNKNOWN_READER: return "Unknown reader specified.";
265 case SCARD_E_UNSUPPORTED_FEATURE: return "Feature not supported.";
266 case SCARD_F_COMM_ERROR: return "RPC transport error.";
267 case SCARD_F_INTERNAL_ERROR: return "Unknown internal error.";
268 case SCARD_F_UNKNOWN_ERROR: return "Unknown internal error.";
269 case SCARD_F_WAITED_TOO_LONG: return "Waited too long.";
270 case SCARD_W_REMOVED_CARD: return "Card was removed.";
271 case SCARD_W_RESET_CARD: return "Card was reset.";
272 case SCARD_W_SECURITY_VIOLATION: return "Access denied.";
273 case SCARD_W_UNPOWERED_CARD: return "Card is unpowered.";
274 case SCARD_W_UNRESPONSIVE_CARD: return "Card is unresponsive.";
275 case SCARD_W_UNSUPPORTED_CARD: return "Card is not supported.";
276
277 /* The following errors are specific to the Perl wrapper */
278 case SCARD_P_ALREADY_CONNECTED: return "Object is already connected";
279 case SCARD_P_NOT_CONNECTED: return "Object is not connected";
280
281 /* We finally end with a generic error message */
282 default: return "Unknown (reader specific ?) error...";
283 };
284 }
285
286 /*************************************************************************/
287 /*************** Double Typed Magical variable PCSC::errno ***************/
288 /*************************************************************************/
289
290 /* This is an accessor to our internal double-typed magical variable */
291 I32 gnLastError_get (pTHX_ IV nID, SV *sv) {
292 /* We have to set both int and double values */
293 sv_setiv (sv, (IV)gnLastError);
294 sv_setnv (sv, (double)gnLastError);
295
296 /* Then we set the error message string */
297 sv_setpv (sv, _StringifyError(gnLastError));
298
299 /* Then, we eventually put corresponding bits of the SV flag */
300 SvNOK_on (sv);
301 SvIOK_on (sv);
302
303 /* return value should be ignored (man samples use return 1) */
304 return 1;
305 }
306
307 /* This is a modifier to our internal double-typed magical variable */
308 I32 gnLastError_set (pTHX_ IV nID, SV *sv) {
309 /* just store the value in our global variable */
310 gnLastError = SvIV (sv);
311
312 /* return value should be ignored (man samples use return 1) */
313 return 1;
314 }
315
316 /* Initialize the double-typed magical variable Chipcard::PCSC::errno */
317 void _InitMagic () {
318 struct ufuncs uf_errno;
319 SV *sv;
320
321 /* Build a new immortal scalar */
322 sv = perl_get_sv ("Chipcard::PCSC::errno", TRUE);
323
324 /* Construct the magic virtual table */
325 uf_errno.uf_val = &gnLastError_get;
326 uf_errno.uf_set = &gnLastError_set;
327 uf_errno.uf_index = 0;
328
329 /* Then apply magic to it (use uf_eerno as a callback list) */
330 sv_magic (sv, 0, 'U', (char *)&uf_errno, sizeof(uf_errno));
331
332 /* Let the scalar be enchanted ! */
333 SvMAGICAL_on (sv);
334 }
335
336 MODULE = Chipcard::PCSC PACKAGE = Chipcard::PCSC
337 PROTOTYPES: ENABLE
338
339 #///////////////////////////////////////////////////////////////////////////
340
341 bool
342 _LoadPCSCLibrary ()
343 CODE:
344 if (ghDll) {
345 /* No need to load the library twice */
346 RETVAL = TRUE;
347 } else {
348 /* Then loads the dynamic library */
349 ghDll = LOAD_LIB();
350 if (ghDll == NULL) {
351 RETVAL = FALSE;
352 croak ("Failed to load PCSC library");
353 } else {
354 hEstablishContext = (TSCardEstablishContext) GET_FCT (ghDll, "SCardEstablishContext");
355 hReleaseContext = (TSCardReleaseContext) GET_FCT (ghDll, "SCardReleaseContext");
356 hReconnect = (TSCardReconnect) GET_FCT (ghDll, "SCardReconnect");
357 hDisconnect = (TSCardDisconnect) GET_FCT (ghDll, "SCardDisconnect");
358 hBeginTransaction = (TSCardBeginTransaction) GET_FCT (ghDll, "SCardBeginTransaction");
359 hEndTransaction = (TSCardEndTransaction) GET_FCT (ghDll, "SCardEndTransaction");
360 hTransmit = (TSCardTransmit) GET_FCT (ghDll, "SCardTransmit");
361 hControl = (TSCardControl) GET_FCT (ghDll, "SCardControl");
362 hCancel = (TSCardCancel) GET_FCT (ghDll, "SCardCancel");
363 #ifdef WIN32
364 hListReaders = (TSCardListReaders) GET_FCT (ghDll, "SCardListReadersA");
365 hConnect = (TSCardConnect) GET_FCT (ghDll, "SCardConnectA");
366 hStatus = (TSCardStatus) GET_FCT (ghDll, "SCardStatusA");
367 hGetStatusChange = (TSCardGetStatusChange) GET_FCT (ghDll, "SCardGetStatusChangeA");
368 #else
369 hListReaders = (TSCardListReaders) GET_FCT (ghDll, "SCardListReaders");
370 hConnect = (TSCardConnect) GET_FCT (ghDll, "SCardConnect");
371 hStatus = (TSCardStatus) GET_FCT (ghDll, "SCardStatus");
372 hGetStatusChange = (TSCardGetStatusChange) GET_FCT (ghDll, "SCardGetStatusChange");
373 #endif
374 if (
375 !hEstablishContext || !hReleaseContext || !hListReaders || !hConnect ||
376 !hReconnect || ! hDisconnect || !hBeginTransaction || !hEndTransaction ||
377 !hTransmit || !hStatus || !hGetStatusChange || !hCancel || !hControl)
378 {
379 RETVAL = FALSE;
380 croak ("PCSC library does not contain all the required symbols");
381 } else {
382 RETVAL = TRUE;
383 }
384 }
385 /* Initialize the magical variable */
386 _InitMagic ();
387 _InitErrorCodes ();
388 }
389 OUTPUT:
390 RETVAL
391
392 #///////////////////////////////////////////////////////////////////////////
393 #// EstablishContext ()
394 #//
395 #// INPUT :
396 #// - $dwScope -> Scope of the establishment. This can be either a local
397 #// or remote connection.
398 #// - $pvReserved1
399 #// - $pwReserved2 -> as of this writting, bothe the above parameters
400 #// are not used by PCSC... they should be 0
401 #//
402 #// OUTPUT :
403 #// EstablishContext() returns the connection context or the 'undef'
404 #// value if something goes wrong.
405 SV*
406 _EstablishContext (dwScope, pvReserved1, pvReserved2)
407 unsigned long dwScope
408 void* pvReserved1
409 void* pvReserved2
410 PREINIT:
411 SCARDCONTEXT hContext = 0;
412 CODE:
413 ST(0) = sv_newmortal();
414 gnLastError = hEstablishContext (dwScope, pvReserved1, pvReserved2, &hContext);
415
416 /* Then we either return an explicit 'UNDEF' value or the handle */
417 if (gnLastError != SCARD_S_SUCCESS) {
418 ST(0) = &PL_sv_undef;
419 } else {
420 sv_setiv (ST(0), hContext);
421 }
422
423 #///////////////////////////////////////////////////////////////////////////
424 #// ReleaseContext ()
425 #//
426 #// INPUT :
427 #// - $hContext -> Connection context to be closed
428 #//
429 #// OUTPUT :
430 #// - ReleaseContext returns a true value on successful operation and a
431 #// false value otherwise.
432 bool
433 _ReleaseContext (hContext)
434 unsigned long hContext
435 CODE:
436 gnLastError = hReleaseContext (hContext);
437
438 /* Then returns true or false according to the return code */
439 if (gnLastError != SCARD_S_SUCCESS) {
440 RETVAL = FALSE;
441 } else {
442 RETVAL = TRUE;
443 }
444 OUTPUT:
445 RETVAL
446
447 #///////////////////////////////////////////////////////////////////////////
448 #// ListReaders ()
449 #//
450 #// INPUT :
451 #// - $hContext -> Connection context to the PC/SC resource manager.
452 #// - $mszGroups -> List of groups to list readers. (as of this writing,
453 #// this is not used and should be 0
454 #//
455 #// OUTPUT :
456 #// ListReaders returns the 'undef' value if an error occurs otherwise it
457 #// returns the list of available readers. Note that this can be an empty
458 #// list...
459 #//
460 SV *
461 _ListReaders(hContext, svGroups)
462 unsigned long hContext
463 SV* svGroups
464 PREINIT:
465 DWORD nBufferSize = 0;
466 char* szBuffer = NULL;
467 char* szCurrentToken = NULL;
468 char* mszGroups;
469 PPCODE:
470 /* Before doing anything, we check that we have a valid group. */
471 if (SvPOK(svGroups)) {
472 /* TODO : see how this works... multistring stuff with time
473 *svGroups may become a reference to an array of groups... or undef
474 */
475 mszGroups = SvPV (svGroups, PL_na);
476 } else {
477 mszGroups = 0;
478 }
479
480 /* The first call to SCardListReaders gives us the size of the
481 * buffer we must allocate for the list.
482 */
483 gnLastError = hListReaders (hContext, mszGroups, 0, &nBufferSize);
484
485 /* In case of any error we immediately return an explicit UNDEF value */
486 if (gnLastError != SCARD_S_SUCCESS) {
487 XSRETURN_UNDEF;
488 }
489
490 if (nBufferSize > 0) {
491 /* At this point, nBufferSize contains the size of the buffer to
492 * alloate. We use New as recomended by perlguts(3pm). The
493 * buffer will be freed with Safefree()...
494 */
495 New (2018, szBuffer, nBufferSize, char);
496 if (szBuffer == NULL) {
497 gnLastError = SCARD_E_NO_MEMORY;
498 warn ("Could not allocate buffer at %s line %d\n\t",
499 __FILE__, __LINE__);
500 XSRETURN_UNDEF;
501 }
502
503 /* The buffer is ready, so we can now retrieve the whole list. */
504 gnLastError = hListReaders (hContext, mszGroups, szBuffer, &nBufferSize);
505
506 /* Then check for any error */
507 if (gnLastError != SCARD_S_SUCCESS) {
508 Safefree (szBuffer);
509 XSRETURN_UNDEF;
510 }
511
512 /* The string must be NULL terminated
513 * May be just too much paranoid but...
514 */
515 if (szBuffer[nBufferSize-1] != 0) {
516 Safefree (szBuffer);
517 gnLastError = SCARD_F_INTERNAL_ERROR;
518 warn ("PCSC did not return a NULL terminated multistring at %s line %d\n\t",
519 __FILE__, __LINE__);
520 XSRETURN_UNDEF;
521 }
522
523 /* Now we need to push each reader separately on the stack. If
524 * no readers are found, the stack is left empty...
525 */
526 szCurrentToken = szBuffer;
527 while (strlen(szCurrentToken)) {
528 XPUSHs (sv_2mortal(newSVpv(szCurrentToken,0)));
529 szCurrentToken = strchr (szCurrentToken, 0) + 1;
530 }
531 /* Free our buffer...
532 * DO NOT USE free() or delete() here ! Let Perl deal with this.
533 */
534 Safefree (szBuffer);
535 } else {
536 gnLastError = SCARD_F_INTERNAL_ERROR;
537 warn ("PCSC did not return a valid buffer length at %s line %d\n\t",
538 __FILE__, __LINE__);
539 XSRETURN_UNDEF;
540 }
541
542 #///////////////////////////////////////////////////////////////////////////
543 #// Connect ()
544 #//
545 #// INPUT :
546 #// - $hContext -> Connection context to the PC/SC Resource Manager
547 #// - $szReader -> ReaderName to connect to
548 #// - dwShareMode -> Mode of connection : exclusive or shared
549 #// - dwPreferredProtocols -> Desired protocol use
550 #//
551 #// OUTPUT :
552 #// Connect returns an array with the handle to the connection and the
553 #// active protocol for this connection ($hCard, $dwActiveProtocol).
554 #// If a problem occurs, it just return the 'undef' value.
555 SV*
556 _Connect (hContext, szReader, dwShareMode, dwPreferredProtocols)
557 unsigned long hContext
558 const char* szReader
559 unsigned long dwShareMode
560 unsigned long dwPreferredProtocols
561 PREINIT:
562 SCARDHANDLE hCard = 0;
563 DWORD dwActiveProtocol = 0;
564 PPCODE:
565 gnLastError = hConnect (hContext, szReader, dwShareMode, dwPreferredProtocols, &hCard, &dwActiveProtocol);
566
567 /* We return immediately in case of an error */
568 if (gnLastError != SCARD_S_SUCCESS)
569 XSRETURN_UNDEF;
570
571 /* If anything was successful, push the two scalar values */
572 XPUSHs (sv_2mortal(newSViv(hCard)));
573 XPUSHs (sv_2mortal(newSViv(dwActiveProtocol)));
574
575 #///////////////////////////////////////////////////////////////////////////
576 #// Reconnect ()
577 #//
578 #// INPUT :
579 #// - $hCard -> Handle to a previous call to connect
580 #// - dwShareMode -> Mode of connection : exclusive or shared
581 #// - dwPreferredProtocols -> Desired protocol use
582 #// - $dwInitialization -> Desired action taken on the card/reader
583 #//
584 #// OUTPUT :
585 #// Reconnect returns the new active protocol or the 'undef' value if an
586 #// error occurs
587 SV*
588 _Reconnect (hCard, dwShareMode, dwPreferredProtocols, dwInitialization)
589 unsigned long hCard
590 unsigned long dwShareMode
591 unsigned long dwPreferredProtocols
592 unsigned long dwInitialization
593 PREINIT:
594 DWORD dwActiveProtocol = 0;
595 CODE:
596 ST(0) = sv_newmortal();
597 gnLastError = hReconnect (hCard, dwShareMode, dwPreferredProtocols, dwInitialization, &dwActiveProtocol);
598 /* Return either an UNDEF value if an error occurs or the current
599 * active protocol as returned by PCSC
600 */
601 if (gnLastError == SCARD_S_SUCCESS)
602 sv_setiv (ST(0), dwActiveProtocol);
603 else
604 ST(0) = &PL_sv_undef;
605
606 #///////////////////////////////////////////////////////////////////////////
607 #// Disconnect ()
608 #//
609 #// INPUT :
610 #// - $hCard -> Connection made from Connect
611 #// - $dwDisposition -> Desired action taken on the card/reader
612 #//
613 #// OUTPUT :
614 #// Disconnect returns TRUE upon successful result. Oppositely, it
615 #// returns FALSE if somthing went bang
616 bool
617 _Disconnect (hCard, dwDisposition)
618 unsigned long hCard;
619 unsigned long dwDisposition;
620 CODE:
621 gnLastError = hDisconnect (hCard, dwDisposition);
622
623 /* Then check for an error */
624 if (gnLastError != SCARD_S_SUCCESS)
625 RETVAL = FALSE;
626 else
627 RETVAL = TRUE;
628 OUTPUT:
629 RETVAL
630
631 #///////////////////////////////////////////////////////////////////////////
632 #// Status ()
633 #//
634 #// INPUT :
635 #// - $hCard -> Connection made from Connect
636 #//
637 #// OUTPUT :
638 #// Status returns a list holding different informations about the
639 #// reader : ($szReaderName, $dwState, $dwProtocol, \@bAttr). If an
640 #// error pops up, the 'undef' value is returned.
641 #//
642 #// Important note:
643 #// We return the ATR in the form of a reference to an array of bytes.
644 #// When we build this reference, we start to build an array wich is
645 #// made mortal, then we fill it with non mortal items usin av_push().
646 #// These items need to be immortal at this point because av_push does
647 #// not increase the reference count of the scalar values it pushes into
648 #// the array. As our array dies when it passes out of scope, perl
649 #// would free its content and then any attempt to use them would result
650 #// in an 'Attempt to free unreferenced scalar' error...
651 SV*
652 _Status (hCard)
653 long hCard
654 PREINIT:
655 int nCount = 0;
656 #ifdef WIN32
657 char tmpReaderName[200];
658 char* szReaderName = tmpReaderName;
659 unsigned long cchReaderLen = sizeof(tmpReaderName);
660 char tmpAtr[MAX_ATR_SIZE];
661 unsigned char* pbAtr = tmpAtr;
662 unsigned long cbAtrLen = sizeof(tmpAtr);
663 #else
664 char* szReaderName = NULL;
665 DWORD cchReaderLen = 0;
666 unsigned char* pbAtr = NULL;
667 DWORD cbAtrLen = 0;
668 #endif
669 DWORD dwState = 0;
670 DWORD dwProtocol = 0;
671 AV* aATR = 0;
672 PPCODE:
673 /* We call the function with a null cchReaderLen : this should
674 * gives us the length of the buffer to allocate
675 */
676 gnLastError = hStatus (hCard, szReaderName, &cchReaderLen,
677 &dwState, &dwProtocol, (BYTE *)pbAtr, &cbAtrLen);
678
679 /* Behaviour differs here from PCSC and PCSClite :
680 * PCSC returns SUCCESS while PCSClite returns an error
681 */
682 if (gnLastError == SCARD_E_INSUFFICIENT_BUFFER || gnLastError == SCARD_S_SUCCESS) {
683 /* The call was hopefuly successful, so we allocate the
684 * buffer for the ATR
685 */
686 #ifndef WIN32
687 /* This hack should be temporary as PCSClite should eventually behave like PCSC */
688 cbAtrLen = MAX_ATR_SIZE;
689 #endif
690 if (cbAtrLen <= 0) {
691 gnLastError = SCARD_F_INTERNAL_ERROR;
692 warn ("PCSC did not return a valid buffer length at %s line %d\n\t",
693 __FILE__, __LINE__);
694 XSRETURN_UNDEF;
695 }
696 New (2018, pbAtr, cbAtrLen, unsigned char);
697 if (pbAtr == NULL) {
698 gnLastError = SCARD_E_NO_MEMORY;
699 warn ("Could not allocate buffer at %s line %d\n\t",
700 __FILE__, __LINE__);
701 XSRETURN_UNDEF;
702 }
703 /* we then allocate the buffer for the reader name */
704 if (cbAtrLen <= 0) {
705 gnLastError = SCARD_F_INTERNAL_ERROR;
706 warn ("PCSC did not return a valid buffer length at %s line %d\n\t",
707 __FILE__, __LINE__);
708 XSRETURN_UNDEF;
709 }
710 New (2018, szReaderName, cchReaderLen, char);
711 if (szReaderName == NULL) {
712 Safefree (pbAtr);
713 gnLastError = SCARD_E_NO_MEMORY;
714 warn ("Could not allocate buffer at %s line %d\n\t",
715 __FILE__, __LINE__);
716 XSRETURN_UNDEF;
717 }
718 /* Now we perform the real call to SCardStatus */
719 gnLastError = hStatus (hCard, szReaderName, &cchReaderLen,
720 &dwState, &dwProtocol, (BYTE *)pbAtr, &cbAtrLen);
721 if (gnLastError != SCARD_S_SUCCESS) {
722 Safefree (szReaderName);
723 Safefree (pbAtr);
724 XSRETURN_UNDEF;
725 }
726 } else {
727 /* As our first call should trigger the SCARD_E_INSUFFICIENT_BUFFER
728 * error or no error, we consider any other case as a failure...
729 */
730 XSRETURN_UNDEF;
731 }
732
733 /* we fill this array with every byte from the ATR
734 * note that we do not make this data mortal because av_push()
735 * does not increment the reference count. See the note in the
736 * function header above
737 */
738 if (cbAtrLen > 0) {
739 /* We first create an array for the ATR */
740 aATR = (AV*) sv_2mortal((SV*)newAV());
741
742 /* Then we fill it with every byte from the ATR
743 * note that we do not make this data mortal because av_push()
744 * does not increment the reference count. See the note in the
745 * function header above
746 */
747 for (nCount=0; nCount < cbAtrLen; nCount++) {
748 av_push (aATR, newSViv(pbAtr[nCount]));
749 }
750 }
751 /* In the event that no ATR is available, we used to fill aATR
752 * with '(AV*) &PL_sv_undef' However, pushing a reference to
753 * this seems is hard to handle I therefore prefer to do
754 * nothing and leave aATR with the default null value and
755 * the code below will not push the reference to the ATR array
756 */
757
758 /* eventually, we end up pushing all the values */
759 XPUSHs (sv_2mortal(newSVpv(szReaderName,0)));
760 XPUSHs (sv_2mortal(newSViv(dwState)));
761 XPUSHs (sv_2mortal(newSViv(dwProtocol)));
762 /* as well as a reference to the ATR array if available */
763 if (aATR) {
764 XPUSHs (sv_2mortal(newRV((SV*)aATR)));
765 }
766
767 /* As a conclusion, we just free what we took */
768 Safefree (szReaderName);
769 Safefree (pbAtr);
770
771 #///////////////////////////////////////////////////////////////////////////
772 #// Transmit ()
773 #//
774 #// INPUT :
775 #// - $hCard
776 #// - @inBuffer = ($Protocol, \@BytesToSend)
777 #// $Protocol contains the protocol (T0|T1)
778 #// @BytesToSend contains the bytes to transmit
779 #// Note: please note that @inBuffer is actually appended to the
780 #// parameters list, therefore, the following calls are equivalent:
781 #// @inBuffer ($Protocol, [0x00, 0x12, 0x33]); = ;Transmit ($hCard, @inBuffer);
782 #// Transmit ($hCard, $Protocol, [0x00, 0x12, 0x33]);
783 #//
784 #// OUTPUT :
785 #// - @outBuffer = ($Protocol, \@BytesRead)
786 #// - $Protocol may be undef
787 #// - @BytesRead contains the returned bytes
788 #// Transmit can return the 'undef' value alone if an error occurs.
789 SV*
790 _Transmit (hCard, dwProtocol, psvSendData)
791 unsigned long hCard;
792 unsigned long dwProtocol;
793 SV* psvSendData;
794 PREINIT:
795 int nCount = 0;
796 static char* pbSendBuffer = NULL;
797 static unsigned char pbRecvBuffer [MAX_BUFFER_SIZE_EXTENDED];
798 unsigned long cbSendLength = 0;
799 DWORD cbRecvLength = sizeof (pbRecvBuffer);
800 SCARD_IO_REQUEST ioSendPci, ioRecvPci;
801 AV* aRecvBuffer = NULL;
802 PPCODE:
803 /* We make sure that the array is sane */
804 if (psvSendData == NULL) {
805 gnLastError = SCARD_E_INVALID_PARAMETER;
806 warn ("psvSendData is a NULL pointer at %s line %d\n\t",
807 __FILE__, __LINE__);
808 XSRETURN_UNDEF;
809 }
810
811 /* Should the second parameter not be a reference, we return the
812 * SCARD_E_INVALID_PARAMETER error code.
813 */
814 if ((!SvROK(psvSendData))||(SvTYPE(SvRV(psvSendData)) != SVt_PVAV)) {
815 gnLastError = SCARD_E_INVALID_PARAMETER;
816 warn ("psvSendData is not a RVAV at %s line %d\n\t",
817 __FILE__, __LINE__);
818 XSRETURN_UNDEF;
819 }
820 /* We have to build up our IO_REQUEST structures according to
821 * $dwProtocol
822 */
823 switch (dwProtocol) {
824 case SCARD_PROTOCOL_T0:
825 case SCARD_PROTOCOL_T1:
826 case SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1:
827 case SCARD_PROTOCOL_RAW:
828 ioSendPci.dwProtocol = dwProtocol;
829 ioSendPci.cbPciLength = sizeof(ioSendPci);
830 ioRecvPci.dwProtocol = dwProtocol;
831 ioRecvPci.cbPciLength = sizeof(ioRecvPci);
832 break;
833 default:
834 /* If $dwProtocol holds an invalid value, we exist reporting
835 * the error SCARD_E_INVALID_VALUE.
836 */
837 gnLastError = SCARD_E_INVALID_VALUE;
838 warn ("unknown protocol %ld given at %s line %d\n\t",
839 dwProtocol, __FILE__, __LINE__);
840 XSRETURN_UNDEF;
841 }
842
843 /* Let's allocate some space for the send buffer */
844 cbSendLength = av_len((AV*)SvRV(psvSendData)) + 1;
845 if (cbSendLength <= 0) {
846 gnLastError = SCARD_E_INVALID_VALUE;
847 warn ("empty array given at %s line %d\n\t",
848 __FILE__, __LINE__);
849 XSRETURN_UNDEF;
850 }
851 New (2018, pbSendBuffer, cbSendLength, char);
852 if (pbSendBuffer == NULL) {
853 gnLastError = SCARD_E_NO_MEMORY;
854 warn ("Could not allocate buffer at %s line %d\n\t",
855 __FILE__, __LINE__);
856 XSRETURN_UNDEF;
857 }
858
859 /* We have to extract data from the array referenced by psvSendData */
860 for (nCount = 0; nCount < cbSendLength ; nCount++)
861 pbSendBuffer[nCount] = (char)SvIV(*av_fetch((AV*)SvRV(psvSendData), nCount, 0));
862
863 /* Everything is ready : call the real function... */
864 gnLastError = hTransmit (hCard, &ioSendPci, (BYTE *)pbSendBuffer, cbSendLength, &ioRecvPci, pbRecvBuffer, &cbRecvLength);
865 if (gnLastError != SCARD_S_SUCCESS) {
866 /* Free the buffer if something went wrong */
867 Safefree (pbSendBuffer);
868 XSRETURN_UNDEF;
869 }
870
871 /* At this point, the command was successful. We still need to
872 * return all the values from our buffer...
873 * so we build an array for the ATR
874 */
875 aRecvBuffer = (AV*) sv_2mortal((SV*)newAV());
876
877 /* we fill this array with every byte from the Response
878 * note that we do not make this data mortal because av_push()
879 * does not increment the reference count. See the note in the
880 * function header above
881 */
882 for (nCount = 0; nCount < cbRecvLength; nCount++)
883 av_push (aRecvBuffer, newSViv(pbRecvBuffer[nCount]));
884
885 XPUSHs (sv_2mortal(newSViv(ioRecvPci.dwProtocol)));
886 XPUSHs (sv_2mortal(newRV((SV*)aRecvBuffer)));
887
888 /* Do not forget to free the dynamically allocated buffer */
889 Safefree (pbSendBuffer);
890
891 #///////////////////////////////////////////////////////////////////////////
892 #// Control ()
893 #//
894 #// INPUT :
895 #// - $hCard
896 #// - $dwControlCode
897 #// - @inBuffer = (\@BytesToSend)
898 #// @BytesToSend contains the bytes to transmit
899 #//
900 #// OUTPUT :
901 #// - @outBuffer = (\@BytesRead)
902 #// - @BytesRead contains the returned bytes
903 #// Control can return the 'undef' value alone if an error occurs.
904 SV*
905 _Control (hCard, dwControlCode, psvSendData)
906 unsigned long hCard;
907 unsigned long dwControlCode;
908 SV* psvSendData;
909 PREINIT:
910 int nCount = 0;
911 static char* pbSendBuffer = NULL;
912 static unsigned char pbRecvBuffer [MAX_BUFFER_SIZE];
913 unsigned long cbSendLength = 0;
914 DWORD cbRecvLength = sizeof (pbRecvBuffer);
915 AV* aRecvBuffer = NULL;
916 PPCODE:
917 /* We make sure that the array is sane */
918 if (psvSendData == NULL) {
919 gnLastError = SCARD_E_INVALID_PARAMETER;
920 warn ("psvSendData is a NULL pointer at %s line %d\n\t",
921 __FILE__, __LINE__);
922 XSRETURN_UNDEF;
923 }
924
925 /* Should the second parameter not be a reference, we return the
926 * SCARD_E_INVALID_PARAMETER error code.
927 */
928 if ((!SvROK(psvSendData))||(SvTYPE(SvRV(psvSendData)) != SVt_PVAV)) {
929 gnLastError = SCARD_E_INVALID_PARAMETER;
930 warn ("psvSendData is not a RVAV at %s line %d\n\t",
931 __FILE__, __LINE__);
932 XSRETURN_UNDEF;
933 }
934
935 /* Let's allocate some space for the send buffer, if needed */
936 cbSendLength = av_len((AV*)SvRV(psvSendData)) + 1;
937 if (cbSendLength <= 0) {
938 gnLastError = SCARD_E_INVALID_VALUE;
939 warn ("empty array given at %s line %d\n\t",
940 __FILE__, __LINE__);
941 XSRETURN_UNDEF;
942 }
943 New (2018, pbSendBuffer, cbSendLength, char);
944 if (pbSendBuffer == NULL) {
945 gnLastError = SCARD_E_NO_MEMORY;
946 warn ("Could not allocate buffer at %s line %d\n\t",
947 __FILE__, __LINE__);
948 XSRETURN_UNDEF;
949 }
950
951 for (nCount = 0; nCount < cbSendLength ; nCount++)
952 pbSendBuffer[nCount] = (char)SvIV(*av_fetch((AV*)SvRV(psvSendData), nCount, 0));
953
954 /* Everything is ready : call the real function... */
955 gnLastError = hControl (hCard, dwControlCode,
956 (cbSendLength > 0 ? (BYTE *)pbSendBuffer : NULL), cbSendLength,
957 pbRecvBuffer, sizeof(pbRecvBuffer), &cbRecvLength);
958
959 if (gnLastError != SCARD_S_SUCCESS) {
960 /* Free the buffer if something went wrong */
961 Safefree (pbSendBuffer);
962 XSRETURN_UNDEF;
963 }
964
965 /* At this point, the command was successful. We still need to
966 * return all the values from our buffer...
967 */
968 aRecvBuffer = (AV*) sv_2mortal((SV*)newAV());
969
970 /* we fill this array with every byte from the Response
971 * note that we do not make this data mortal because av_push()
972 * does not increment the reference count. See the note in the
973 * function header above
974 */
975 for (nCount = 0; nCount < cbRecvLength; nCount++)
976 av_push (aRecvBuffer, newSViv(pbRecvBuffer[nCount]));
977
978 // XPUSHs (sv_2mortal(newSViv(ioRecvPci.dwProtocol)));
979 XPUSHs (sv_2mortal(newRV((SV*)aRecvBuffer)));
980
981 /* Do not forget to free the dynamically allocated buffer */
982 Safefree (pbSendBuffer);
983
984
985 #///////////////////////////////////////////////////////////////////////////
986 #// BeginTransaction ()
987 #//
988 #// INPUT :
989 #// - $hCard -> connection made from Connect()
990 #//
991 #// OUTPUT :
992 #// BeginTransaction returns true or false depending on its successful
993 #// completion
994 unsigned long
995 _BeginTransaction (hCard)
996 unsigned long hCard;
997 CODE:
998 gnLastError = hBeginTransaction (hCard);
999
1000 /* Then we check for an error */
1001 if (gnLastError != SCARD_S_SUCCESS)
1002 RETVAL = FALSE;
1003 else
1004 RETVAL = TRUE;
1005 OUTPUT:
1006 RETVAL
1007
1008 #///////////////////////////////////////////////////////////////////////////
1009 #// EndTransaction ()
1010 #//
1011 #// INPUT :
1012 #// - $hCard -> connection made from Connect()
1013 #// - $dwDisposition -> Desired action taken on the card/reader
1014 #//
1015 #// OUTPUT :
1016 #// EndTransaction returns true or false depending on its successful
1017 #// completion
1018 unsigned long
1019 _EndTransaction (hCard, dwDisposition)
1020 unsigned long hCard;
1021 unsigned long dwDisposition;
1022 CODE:
1023 gnLastError = hEndTransaction (hCard, dwDisposition);
1024
1025 /* Then we check for an error */
1026 if (gnLastError != SCARD_S_SUCCESS)
1027 RETVAL = FALSE;
1028 else
1029 RETVAL = TRUE;
1030 OUTPUT:
1031 RETVAL
1032
1033 #///////////////////////////////////////////////////////////////////////////
1034 #// GetStatusChange ()
1035 #//
1036 #// This
1037 #//
1038 #// INPUT :
1039 #// - $hContext -> Connection context to the PC/SC resource manager.
1040 #// - $nTimeout -> Time to wait for a change (or SCARD_INFINITE)
1041 #// - \@ReaderStates -> array of reader states
1042 #//
1043 #// OUTPUT :
1044 bool
1045 _GetStatusChange (hContext, dwTimeout, psvReaderStates)
1046 unsigned long hContext;
1047 unsigned long dwTimeout;
1048 SV* psvReaderStates;
1049 PREINIT:
1050 static SCARD_READERSTATE *rgReaderStates_t = NULL;
1051 unsigned int nCount = 0;
1052 unsigned int nATRCount = 0;
1053 unsigned int nReaders = 0;
1054 AV* aRecvBuffer = NULL;
1055
1056 PPCODE:
1057 if (psvReaderStates == NULL) {
1058 gnLastError = SCARD_E_INVALID_PARAMETER;
1059 warn ("psvReaderStates is a NULL pointer at %s line %d\n\t",
1060 __FILE__, __LINE__);
1061 XSRETURN_NO;
1062 }
1063
1064 /* Should the second parameter not be a reference, we return the
1065 * SCARD_E_INVALID_PARAMETER error code.
1066 */
1067 if ((!SvROK(psvReaderStates))||(SvTYPE(SvRV(psvReaderStates)) != SVt_PVAV)) {
1068 gnLastError = SCARD_E_INVALID_PARAMETER;
1069 warn ("psvReaderStates is not a RVAV at %s line %d\n\t",
1070 __FILE__, __LINE__);
1071 XSRETURN_NO;
1072 }
1073
1074 /* Get the total number of elements in our array */
1075 nReaders = av_len((AV*)SvRV(psvReaderStates)) + 1;
1076
1077 /* free the memory allocated during previous call to GetStatusChange */
1078 if (rgReaderStates_t)
1079 Safefree(rgReaderStates_t);
1080
1081 /* allocate the Reader States table */
1082 Newz(2018, rgReaderStates_t, nReaders, SCARD_READERSTATE);
1083 if (rgReaderStates_t == NULL)
1084 {
1085 warn ("Could not allocate buffer at %s line %d\n\t",
1086 __FILE__, __LINE__);
1087 XSRETURN_NO;
1088 }
1089
1090 for (nCount = 0; nCount < nReaders; nCount++) {
1091 /* As long as psvReaderStates is a reference to a PVAV we
1092 * should be able to use av_fetch() without error
1093 */
1094 SV *svCurrentToken = (*av_fetch((AV*)SvRV(psvReaderStates), nCount, 0));
1095
1096 /* Now see if the elements in the array are reference to hasharrays */
1097 if ((!SvROK(svCurrentToken)) || (SvTYPE(SvRV(svCurrentToken)) != SVt_PVHV)) {
1098 gnLastError = SCARD_E_INVALID_PARAMETER;
1099 warn ("psvReaderStates[%d] is not a RVHV at %s line %d\n\t", nCount,
1100 __FILE__, __LINE__);
1101 XSRETURN_NO;
1102 }
1103
1104 /* Checkout if the 'name' argument has been passed */
1105 if (hv_exists((HV*)SvRV(svCurrentToken), "reader_name", 11)) {
1106 SV** psvName = NULL;
1107
1108 /* fetch the value of reader_name */
1109 psvName = hv_fetch((HV*)SvRV(svCurrentToken), "reader_name", 11, 0);
1110
1111 /* Link the internal structure and the fetched pointer if appropriate */
1112 if ((psvName != NULL) && (SvTYPE(*psvName) == SVt_PV)) {
1113 // printf ("We got a name\n");
1114 rgReaderStates_t[nCount].szReader = SvPV (*psvName, PL_na);
1115 // printf ("which is : %s\n", rgReaderStates_t[nCount].szReader);
1116 } else {
1117 gnLastError = SCARD_E_INVALID_PARAMETER;
1118 warn ("reader_name is not valid (must be ASCII) at %s line %d\n\t",
1119 __FILE__, __LINE__);
1120 XSRETURN_NO;
1121 }
1122 }
1123
1124 /* Checkout if the 'current_state' argument has been passed */
1125 if (hv_exists((HV*)SvRV(svCurrentToken), "current_state", 13)) {
1126 SV** psvCurrentState = NULL;
1127
1128 /* fetch the value of current_state */
1129 psvCurrentState = hv_fetch((HV*)SvRV(svCurrentToken), "current_state", 13, 0);
1130
1131 /* Copy the current status into the struct */
1132 if (psvCurrentState != NULL) {
1133 if (SvTYPE(*psvCurrentState) == SVt_IV
1134 || SvTYPE(*psvCurrentState) == SVt_PVIV) {
1135 // printf ("We got a current_state\n");
1136 rgReaderStates_t[nCount].dwCurrentState = SvIV (*psvCurrentState);
1137 // printf ("which is : 0x%lX\n", rgReaderStates_t[nCount].dwCurrentState);
1138 } else {
1139 gnLastError = SCARD_E_INVALID_PARAMETER;
1140 warn ("current_state is not valid (must be numeric) at %s line %d\n\t",
1141 __FILE__, __LINE__);
1142 XSRETURN_NO;
1143 }
1144 }
1145 }
1146
1147 /* Checkout if the 'event_state' argument has been passed */
1148 if (hv_exists((HV*)SvRV(svCurrentToken), "event_state", 11)) {
1149 SV** psvEventState = NULL;
1150
1151 /* fetch the value of current_state */
1152 psvEventState = hv_fetch((HV*)SvRV(svCurrentToken), "event_state", 11, 0);
1153
1154 /* Copy the event status into the struct */
1155 if (psvEventState != NULL) {
1156 if (SvTYPE(*psvEventState) == SVt_IV
1157 || SvTYPE(*psvEventState) == SVt_PVIV) {
1158 rgReaderStates_t[nCount].dwEventState = SvIV (*psvEventState);
1159 } else {
1160 gnLastError = SCARD_E_INVALID_PARAMETER;
1161 warn ("event_state is not valid (must be numeric) at %s line %d\n\t",
1162 __FILE__, __LINE__);
1163 XSRETURN_NO;
1164 }
1165 }
1166 }
1167
1168
1169 /* Checkout if the 'ATR' argument has been passed */
1170 if (hv_exists((HV*)SvRV(svCurrentToken), "ATR", 3)) {
1171 SV** psvATR = NULL;
1172
1173 /* fetch the value of ATR */
1174 psvATR = hv_fetch((HV*)SvRV(svCurrentToken), "ATR", 3, 0);
1175
1176 if (psvATR != NULL) {
1177 /* Make sure we have a reference to an array */
1178 if ((SvTYPE(*psvATR) == SVt_RV) && (SvTYPE(SvRV(*psvATR)) == SVt_PVAV)) {
1179 int nATR = 0;
1180
1181 /* Fetch the ATR length */
1182 nATR = av_len((AV*)SvRV(*psvATR)) + 1;
1183
1184 for (nATRCount=0; nATRCount< nATR; nATRCount++) {
1185 /* Fetch all bytes of th ATR one by one */
1186 SV *svCurrentATRToken = (*av_fetch((AV*)SvRV(*psvATR), nATRCount, 0));
1187 if (SvTYPE(svCurrentATRToken) != SVt_IV) {
1188 /* Return SCARD_E_INVALID_PARAMETER if
1189 * the ATR is not made only of numbers
1190 */
1191 gnLastError = SCARD_E_INVALID_PARAMETER;
1192 warn ("invalid ATR (not a reference to a numerical array) at %s line %d\n\t",
1193 __FILE__, __LINE__);
1194 XSRETURN_NO;
1195 }
1196 rgReaderStates_t[nCount].rgbAtr[nATRCount] = (char)SvIV(svCurrentATRToken);
1197 }
1198 } else {
1199 /* ATR is invalid therefore we return SCARD_E_INVALID_PARAMETER */
1200 gnLastError = SCARD_E_INVALID_PARAMETER;
1201 warn ("invalid ATR (not a reference to an array) at %s line %d\n\t",
1202 __FILE__, __LINE__);
1203 XSRETURN_NO;
1204 }
1205 }
1206 }
1207 }
1208
1209 /* Eventually call the real PCSC function */
1210 gnLastError = hGetStatusChange (hContext, dwTimeout, rgReaderStates_t, nReaders);
1211
1212 /* Stop here upon failure */
1213 if (gnLastError != SCARD_S_SUCCESS) {
1214 XSRETURN_NO;
1215 }
1216
1217 /* Upon successful completion, we have to propagate changes from
1218 * the internalm structs to the hash arays, creating entries if
1219 * required
1220 */
1221 for (nCount = 0; nCount < nReaders; nCount++) {
1222 /* As long as psvReaderStates is a reference to a PVAV we
1223 * should be able to use av_fetch() without error
1224 */
1225 SV *svCurrentToken = (*av_fetch((AV*)SvRV(psvReaderStates), nCount, 0));
1226
1227 /* Propagates changes to the reader_name... */
1228 /* The name was mandatory so we should have it already
1229 * linked to our value...
1230 */
1231
1232 /* Propagate changes to the current_state */
1233 if (hv_exists((HV*)SvRV(svCurrentToken), "current_state", 13)) {
1234 /* If the current_state was provided we modify its
1235 * entry...
1236 * Most checks were performed already so this is a
1237 * simplified run...
1238 */
1239
1240 /* Copy the struct into current_state */
1241 sv_setiv (*hv_fetch((HV*)SvRV(svCurrentToken), "current_state", 13, 0),
1242 rgReaderStates_t[nCount].dwCurrentState);
1243 } else {
1244 /* If the current_state wasn't provided we create its
1245 * entry
1246 */
1247 hv_store ((HV*)SvRV(svCurrentToken), "current_state", 13,
1248 newSViv(rgReaderStates_t[nCount].dwCurrentState), 0);
1249 }
1250
1251 /* Propagate changes to the event_state */
1252 if (hv_exists((HV*)SvRV(svCurrentToken), "event_state", 11)) {
1253 /* If the event_state was provided we modify its
1254 * entry...
1255 * Most checks were performed already so this is a
1256 * simplified run...
1257 */
1258
1259 /* Copy the struct into event_state */
1260 sv_setiv (*hv_fetch((HV*)SvRV(svCurrentToken), "event_state", 11, 0),
1261 rgReaderStates_t[nCount].dwEventState);
1262 } else {
1263 /* If the current_state wasn't provided we create its
1264 * entry
1265 */
1266 hv_store ((HV*)SvRV(svCurrentToken), "event_state", 11,
1267 newSViv(rgReaderStates_t[nCount].dwEventState), 0);
1268 }
1269
1270 /* Build the ATR if possible */
1271 if (rgReaderStates_t[nCount].cbAtr > 0) {
1272 /* Create an AV* with the ATR */
1273 aRecvBuffer = (AV*) sv_2mortal((SV*)newAV());
1274
1275 for (nATRCount = 0; nATRCount <rgReaderStates_t[nCount].cbAtr; nATRCount++)
1276 av_push (aRecvBuffer, newSViv(rgReaderStates_t[nCount].rgbAtr[nATRCount]));
1277
1278 /* Propagates changes to the ATR */
1279 if (hv_exists((HV*)SvRV(svCurrentToken), "ATR", 11)) {
1280 /* If the ATR was provided we modify its entry...
1281 * Most checks were performed already so this is a
1282 * simplified run...
1283 */
1284
1285 /* Copy the struct into the ATR */
1286 sv_setsv (*hv_fetch((HV*)SvRV(svCurrentToken), "ATR", 3, 0),
1287 sv_2mortal(newRV((SV*)aRecvBuffer)));
1288 } else {
1289 hv_store ((HV*)SvRV(svCurrentToken), "ATR", 3,
1290 newRV((SV*)aRecvBuffer), 0);
1291 }
1292 } else {
1293 /* Deletes the variable to make sure we do not keep some
1294 * old outdated values
1295 */
1296 hv_delete((HV*)SvRV(svCurrentToken), "ATR", 3, G_DISCARD);
1297 }
1298 }
1299 XSRETURN_YES;
1300
1301
1302
1303 #///////////////////////////////////////////////////////////////////////////
1304 #// Cancel ()
1305 #//
1306 #// This function cancels pending blocking requests from _GetStatusChange ()
1307 #//
1308 #// INPUT :
1309 #// - $hContext -> Connection context to the PC/SC resource manager.
1310 #//
1311 #// OUTPUT :
1312 #// Cancel returns true upon successful conmpletion or false otherwise
1313 bool
1314 _Cancel (hContext)
1315 unsigned long hContext
1316 CODE:
1317 gnLastError = hCancel (hContext);
1318
1319 /* Then we check for an error */
1320 if (gnLastError != SCARD_S_SUCCESS)
1321 RETVAL = FALSE;
1322 else
1323 RETVAL = TRUE;
1324 OUTPUT:
1325 RETVAL
1326
1327 # End of File #