pcsc-lite  1.8.8
libpcscspy.c
1 /*
2  Log PC/SC arguments
3  Copyright (C) 2001 Ludovic Rousseau
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program 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
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /* $Id: libpcscspy.c 6382 2012-07-01 16:01:49Z rousseau $ */
20 
21 #include <dlfcn.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/time.h>
30 #include <pthread.h>
31 
32 #include "misc.h"
33 #include <winscard.h>
34 
35 #define DEBUG
36 
37 #ifdef __APPLE__
38 #define SCardControl SCardControl132
39 
40 PCSC_API int32_t SCardControl132(SCARDHANDLE hCard, uint32_t dwControlCode,
41  const void *pbSendBuffer, uint32_t cbSendLength,
42  void *pbRecvBuffer, uint32_t cbRecvLength, uint32_t *lpBytesReturned);
43 #endif
44 
45 /* function prototypes */
46 
47 #define p_SCardEstablishContext(fct) LONG(fct)(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
48 
49 #define p_SCardReleaseContext(fct) LONG(fct)(SCARDCONTEXT hContext)
50 
51 #define p_SCardIsValidContext(fct) LONG(fct) (SCARDCONTEXT hContext)
52 
53 #define p_SCardConnect(fct) LONG(fct) (SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
54 
55 #define p_SCardReconnect(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
56 
57 #define p_SCardDisconnect(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwDisposition)
58 
59 #define p_SCardBeginTransaction(fct) LONG(fct) (SCARDHANDLE hCard)
60 
61 #define p_SCardEndTransaction(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwDisposition)
62 
63 #define p_SCardStatus(fct) LONG(fct) (SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
64 
65 #define p_SCardGetStatusChange(fct) LONG(fct) (SCARDCONTEXT hContext, DWORD dwTimeout, LPSCARD_READERSTATE rgReaderStates, DWORD cReaders)
66 
67 #define p_SCardControl(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
68 
69 #define p_SCardTransmit(fct) LONG(fct) (SCARDHANDLE hCard, const SCARD_IO_REQUEST * pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST * pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
70 
71 #define p_SCardListReaderGroups(fct) LONG(fct) (SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
72 
73 #define p_SCardListReaders(fct) LONG(fct) (SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
74 
75 #define p_SCardFreeMemory(fct) LONG(fct) (SCARDCONTEXT hContext, LPCVOID pvMem)
76 
77 #define p_SCardCancel(fct) LONG(fct) (SCARDCONTEXT hContext)
78 
79 #define p_SCardGetAttrib(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
80 
81 #define p_SCardSetAttrib(fct) LONG(fct) (SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
82 
83 #define p_pcsc_stringify_error(fct) char *(fct)(const LONG pcscError)
84 
85 /* fake function to just return en error code */
86 static LONG internal_error(void)
87 {
89 }
90 
91 static const char * internal_stringify_error(void)
92 {
93  return "No spy pcsc_stringify_error() function";
94 }
95 
96 /* contains pointers to real functions */
97 static struct
98 {
99  p_SCardEstablishContext(*SCardEstablishContext);
100  p_SCardReleaseContext(*SCardReleaseContext);
101  p_SCardIsValidContext(*SCardIsValidContext);
102  p_SCardConnect(*SCardConnect);
103  p_SCardReconnect(*SCardReconnect);
104  p_SCardDisconnect(*SCardDisconnect);
105  p_SCardBeginTransaction(*SCardBeginTransaction);
106  p_SCardEndTransaction(*SCardEndTransaction);
107  p_SCardStatus(*SCardStatus);
108  p_SCardGetStatusChange(*SCardGetStatusChange);
109  p_SCardControl(*SCardControl);
110  p_SCardTransmit(*SCardTransmit);
111  p_SCardListReaderGroups(*SCardListReaderGroups);
112  p_SCardListReaders(*SCardListReaders);
113  p_SCardFreeMemory(*SCardFreeMemory);
114  p_SCardCancel(*SCardCancel);
115  p_SCardGetAttrib(*SCardGetAttrib);
116  p_SCardSetAttrib(*SCardSetAttrib);
117  p_pcsc_stringify_error(*pcsc_stringify_error);
118 } spy = {
119  /* initialized with the fake internal_error() function */
120  .SCardEstablishContext = (p_SCardEstablishContext(*))internal_error,
121  .SCardReleaseContext = (p_SCardReleaseContext(*))internal_error,
122  .SCardIsValidContext = (p_SCardIsValidContext(*))internal_error,
123  .SCardConnect = (p_SCardConnect(*))internal_error,
124  .SCardReconnect = (p_SCardReconnect(*))internal_error,
125  .SCardDisconnect = (p_SCardDisconnect(*))internal_error,
126  .SCardBeginTransaction = (p_SCardBeginTransaction(*))internal_error,
127  .SCardEndTransaction = (p_SCardEndTransaction(*))internal_error,
128  .SCardStatus = (p_SCardStatus(*))internal_error,
129  .SCardGetStatusChange = (p_SCardGetStatusChange(*))internal_error,
130  .SCardControl = (p_SCardControl(*))internal_error,
131  .SCardTransmit = (p_SCardTransmit(*))internal_error,
132  .SCardListReaderGroups = (p_SCardListReaderGroups(*))internal_error,
133  .SCardListReaders = (p_SCardListReaders(*))internal_error,
134  .SCardFreeMemory = (p_SCardFreeMemory(*))internal_error,
135  .SCardCancel = (p_SCardCancel(*))internal_error,
136  .SCardGetAttrib = (p_SCardGetAttrib(*))internal_error,
137  .SCardSetAttrib = (p_SCardSetAttrib(*))internal_error,
138  .pcsc_stringify_error = (p_pcsc_stringify_error(*))internal_stringify_error
139 };
140 
141 #define LOG log_line("%s:%d", __FILE__, __LINE__)
142 
143 static int Log_fd = -1;
144 static void *Lib_handle = NULL;
145 static pthread_mutex_t Log_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
146 
147 #ifdef DEBUG
148 static void log_line(const char *fmt, ...)
149 {
150  va_list args;
151 
152  va_start(args, fmt);
153  vprintf(fmt, args);
154  printf("\n");
155  va_end(args);
156 }
157 #else
158 static void log_line(const char *fmt, ...)
159 {
160 }
161 #endif
162 
163 static void spy_line_direct(char *line)
164 {
165  char threadid[30];
166 
167  /* spying disabled */
168  if (Log_fd < 0)
169  return;
170 
171  snprintf(threadid, sizeof threadid, "%lX@", pthread_self());
172  pthread_mutex_lock(&Log_fd_mutex);
173  write(Log_fd, threadid, strlen(threadid));
174  write(Log_fd, line, strlen(line));
175  write(Log_fd, "\n", 1);
176  pthread_mutex_unlock(&Log_fd_mutex);
177 }
178 
179 static void spy_line(const char *fmt, ...)
180 {
181  va_list args;
182  char line[256];
183  int size;
184  char threadid[30];
185 
186  /* spying disabled */
187  if (Log_fd < 0)
188  return;
189 
190  va_start(args, fmt);
191  size = vsnprintf(line, sizeof line, fmt, args);
192  va_end(args);
193  if ((size_t)size >= sizeof line)
194  {
195  printf("libpcsc-spy: Buffer is too small!\n");
196  return;
197  }
198  snprintf(threadid, sizeof threadid, "%lX@", pthread_self());
199  pthread_mutex_lock(&Log_fd_mutex);
200  write(Log_fd, threadid, strlen(threadid));
201  write(Log_fd, line, size);
202  write(Log_fd, "\n", 1);
203  pthread_mutex_unlock(&Log_fd_mutex);
204 }
205 
206 static void spy_enter(const char *fname)
207 {
208  struct timeval profile_time;
209 
210  gettimeofday(&profile_time, NULL);
211  spy_line(">|%d|%d|%s", profile_time.tv_sec, profile_time.tv_usec, fname);
212 }
213 
214 static void spy_quit(const char *fname, LONG rv)
215 {
216  struct timeval profile_time;
217 
218  gettimeofday(&profile_time, NULL);
219  spy_line("<|%d|%d|%s|%s|0x%08X", profile_time.tv_sec,
220  profile_time.tv_usec, fname, spy.pcsc_stringify_error(rv), rv);
221 }
222 
223 #define Enter() spy_enter(__FUNCTION__)
224 #define Quit() spy_quit(__FUNCTION__, rv)
225 
226 static void spy_long(long arg)
227 {
228  spy_line("0x%08lX", arg);
229 }
230 
231 static void spy_ptr_long(LONG *arg)
232 {
233  if (arg)
234  spy_line("0x%08lX", *arg);
235  else
236  spy_line("NULL");
237 }
238 
239 static void spy_ptr_ulong(ULONG *arg)
240 {
241  if (arg)
242  spy_line("0x%08lX", *arg);
243  else
244  spy_line("NULL");
245 }
246 
247 static void spy_pvoid(const void *ptr)
248 {
249  spy_line("%p", ptr);
250 }
251 
252 static void spy_buffer(const unsigned char *buffer, size_t length)
253 {
254  spy_long(length);
255 
256  if (NULL == buffer)
257  spy_line("NULL");
258  else
259  {
260  /* "78 79 7A" */
261  char log_buffer[length * 3 +1], *p;
262  size_t i;
263 
264  p = log_buffer;
265  log_buffer[0] = '\0';
266  for (i=0; i<length; i++)
267  {
268  snprintf(p, 4, "%02X ", buffer[i]);
269  p += 3;
270  }
271  *p = '\0';
272 
273  spy_line_direct(log_buffer);
274  }
275 }
276 
277 static void spy_str(const char *str)
278 {
279  spy_line("%s", str);
280 }
281 
282 static void spy_n_str(const char *str, ULONG *len, int autoallocate)
283 {
284  spy_ptr_ulong(len);
285  if (NULL == len)
286  {
287  spy_line("\"\"");
288  }
289  else
290  {
291  if (NULL == str)
292  {
293  spy_line("NULL");
294  }
295  else
296  {
297  const char *s = str;
298  unsigned int length = 0;
299 
300  if (autoallocate)
301  s = *(char **)str;
302 
303  do
304  {
305  spy_line("%s", s);
306  length += strlen(s)+1;
307  s += strlen(s)+1;
308  } while(length < *len);
309  }
310  }
311 }
312 
313 
314 static void spy_readerstate(SCARD_READERSTATE * rgReaderStates, int cReaders)
315 {
316  int i;
317 
318  for (i=0; i<cReaders; i++)
319  {
320  spy_str(rgReaderStates[i].szReader);
321  spy_long(rgReaderStates[i].dwCurrentState);
322  spy_long(rgReaderStates[i].dwEventState);
323  if (rgReaderStates[i].cbAtr <= MAX_ATR_SIZE)
324  spy_buffer(rgReaderStates[i].rgbAtr, rgReaderStates[i].cbAtr);
325  else
326  spy_buffer(NULL, rgReaderStates[i].cbAtr);
327  }
328 }
329 
330 static LONG load_lib(void)
331 {
332 
333 #ifdef __APPLE__
334 /* We should be able to directly use this
335  * #define LIBPCSC_NOSPY "/System/Library/Frameworks/PCSC.framework/PCSC"
336  * but for a yet unknown reason the dlsym() returns symbols from the spy
337  * library and not from the framework.
338  * Just copying the framework in /tmp does solve the problem.
339  */
340 #define LIBPCSC_NOSPY "/tmp/PCSC"
341 #define LIBPCSC "/tmp/PCSC"
342 #else
343 #define LIBPCSC_NOSPY "libpcsclite_nospy.so.1"
344 #define LIBPCSC "libpcsclite.so.1"
345 #endif
346 
347  /* first try to load the NOSPY library
348  * this is used for programs doing an explicit dlopen like
349  * Perl and Python wrappers */
350  Lib_handle = dlopen(LIBPCSC_NOSPY, RTLD_LAZY);
351  if (NULL == Lib_handle)
352  {
353  log_line("%s", dlerror());
354 
355  /* load the normal library */
356  Lib_handle = dlopen(LIBPCSC, RTLD_LAZY);
357  if (NULL == Lib_handle)
358  {
359  log_line("%s", dlerror());
360  return SCARD_F_INTERNAL_ERROR;
361  }
362  }
363 
364 #define get_symbol(s) do { spy.s = dlsym(Lib_handle, #s); if (NULL == spy.s) { log_line("%s", dlerror()); return SCARD_F_INTERNAL_ERROR; } } while (0)
365 
366  if (SCardEstablishContext == dlsym(Lib_handle, "SCardEstablishContext"))
367  {
368  log_line("Symbols dlsym error");
369  return SCARD_F_INTERNAL_ERROR;
370  }
371 
372  get_symbol(SCardEstablishContext);
373  get_symbol(SCardReleaseContext);
374  get_symbol(SCardIsValidContext);
375  get_symbol(SCardConnect);
376  get_symbol(SCardReconnect);
377  get_symbol(SCardDisconnect);
378  get_symbol(SCardBeginTransaction);
379  get_symbol(SCardEndTransaction);
380  get_symbol(SCardStatus);
381  get_symbol(SCardGetStatusChange);
382  get_symbol(SCardControl);
383  get_symbol(SCardTransmit);
384  get_symbol(SCardListReaderGroups);
385  get_symbol(SCardListReaders);
386  /* Mac OS X do not have SCardFreeMemory() */
387  if (dlsym(Lib_handle, "SCardFreeMemory"))
388  get_symbol(SCardFreeMemory);
389  get_symbol(SCardCancel);
390  get_symbol(SCardGetAttrib);
391  get_symbol(SCardSetAttrib);
392  get_symbol(pcsc_stringify_error);
393 
394  return SCARD_S_SUCCESS;
395 }
396 
397 
398 /* exported functions */
399 PCSC_API p_SCardEstablishContext(SCardEstablishContext)
400 {
401  LONG rv;
402  static int init = 0;
403 
404  if (!init)
405  {
406  const char *home;
407  char log_pipe[128];
408 
409  init = 1;
410 
411  /* load the real library */
412  rv = load_lib();
413  if (rv != SCARD_S_SUCCESS)
414  return rv;
415 
416  /* check if we can log */
417  home = getenv("HOME");
418  if (NULL == home)
419  home = "/tmp";
420 
421  snprintf(log_pipe, sizeof log_pipe, "%s/pcsc-spy", home);
422  Log_fd = open(log_pipe, O_WRONLY);
423  if (Log_fd < 0)
424  {
425  log_line("open %s failed: %s", log_pipe, strerror(errno));
426  }
427  }
428 
429  Enter();
430  spy_long(dwScope);
431  rv = spy.SCardEstablishContext(dwScope, pvReserved1, pvReserved2,
432  phContext);
433  spy_ptr_long(phContext);
434  Quit();
435  return rv;
436 }
437 
438 PCSC_API p_SCardReleaseContext(SCardReleaseContext)
439 {
440  LONG rv;
441 
442  Enter();
443  spy_long(hContext);
444  rv = spy.SCardReleaseContext(hContext);
445  Quit();
446  return rv;
447 }
448 
449 PCSC_API p_SCardIsValidContext(SCardIsValidContext)
450 {
451  LONG rv;
452 
453  Enter();
454  spy_long(hContext);
455  rv = spy.SCardIsValidContext(hContext);
456  Quit();
457  return rv;
458 }
459 
460 PCSC_API p_SCardConnect(SCardConnect)
461 {
462  LONG rv;
463 
464  Enter();
465  spy_long(hContext);
466  spy_str(szReader);
467  spy_long(dwShareMode);
468  spy_long(dwPreferredProtocols);
469  spy_ptr_long(phCard);
470  spy_ptr_ulong(pdwActiveProtocol);
471  rv = spy.SCardConnect(hContext, szReader, dwShareMode,
472  dwPreferredProtocols, phCard, pdwActiveProtocol);
473  spy_ptr_long(phCard);
474  spy_ptr_ulong(pdwActiveProtocol);
475  Quit();
476  return rv;
477 }
478 
479 PCSC_API p_SCardReconnect(SCardReconnect)
480 {
481  LONG rv;
482 
483  Enter();
484  spy_long(hCard);
485  spy_long(dwShareMode);
486  spy_long(dwPreferredProtocols);
487  spy_long(dwInitialization);
488  rv = spy.SCardReconnect(hCard, dwShareMode, dwPreferredProtocols,
489  dwInitialization, pdwActiveProtocol);
490  spy_ptr_ulong(pdwActiveProtocol);
491  Quit();
492  return rv;
493 }
494 
495 PCSC_API p_SCardDisconnect(SCardDisconnect)
496 {
497  LONG rv;
498 
499  Enter();
500  spy_long(hCard);
501  spy_long(dwDisposition);
502  rv = spy.SCardDisconnect(hCard, dwDisposition);
503  Quit();
504  return rv;
505 }
506 
507 PCSC_API p_SCardBeginTransaction(SCardBeginTransaction)
508 {
509  LONG rv;
510 
511  Enter();
512  spy_long(hCard);
513  rv = spy.SCardBeginTransaction(hCard);
514  Quit();
515  return rv;
516 }
517 
518 PCSC_API p_SCardEndTransaction(SCardEndTransaction)
519 {
520  LONG rv;
521 
522  Enter();
523  spy_long(hCard);
524  spy_long(dwDisposition);
525  rv = spy.SCardEndTransaction(hCard, dwDisposition);
526  Quit();
527  return rv;
528 }
529 
530 PCSC_API p_SCardStatus(SCardStatus)
531 {
532  LONG rv;
533  int autoallocate_ReaderName = 0, autoallocate_Atr = 0;
534 
535  if (pcchReaderLen)
536  autoallocate_ReaderName = *pcchReaderLen == SCARD_AUTOALLOCATE;
537 
538  if (pcbAtrLen)
539  autoallocate_Atr = *pcbAtrLen == SCARD_AUTOALLOCATE;
540 
541  Enter();
542  spy_long(hCard);
543  spy_ptr_ulong(pcchReaderLen);
544  spy_ptr_ulong(pcbAtrLen);
545  rv = spy.SCardStatus(hCard, mszReaderName, pcchReaderLen, pdwState,
546  pdwProtocol, pbAtr, pcbAtrLen);
547  spy_n_str(mszReaderName, pcchReaderLen, autoallocate_ReaderName);
548  spy_ptr_ulong(pdwState);
549  spy_ptr_ulong(pdwProtocol);
550  if (NULL == pcbAtrLen)
551  spy_line("NULL");
552  else
553  {
554  if (autoallocate_Atr)
555  {
556  const unsigned char *b = *(unsigned char **)pbAtr;
557 
558  spy_buffer(b, *pcbAtrLen);
559  }
560  else
561  spy_buffer(pbAtr, *pcbAtrLen);
562  }
563  Quit();
564  return rv;
565 }
566 
567 PCSC_API p_SCardGetStatusChange(SCardGetStatusChange)
568 {
569  LONG rv;
570 
571  Enter();
572  spy_long(hContext);
573  spy_long(dwTimeout);
574  spy_long(cReaders);
575  spy_readerstate(rgReaderStates, cReaders);
576  rv = spy.SCardGetStatusChange(hContext, dwTimeout, rgReaderStates,
577  cReaders);
578  spy_readerstate(rgReaderStates, cReaders);
579  Quit();
580  return rv;
581 }
582 
583 PCSC_API p_SCardControl(SCardControl)
584 {
585  LONG rv;
586 
587  Enter();
588  spy_long(hCard);
589  spy_long(dwControlCode);
590  spy_buffer(pbSendBuffer, cbSendLength);
591  rv = spy.SCardControl(hCard, dwControlCode, pbSendBuffer, cbSendLength,
592  pbRecvBuffer, cbRecvLength, lpBytesReturned);
593  if (lpBytesReturned)
594  spy_buffer(pbRecvBuffer, *lpBytesReturned);
595  else
596  spy_buffer(NULL, 0);
597  Quit();
598  return rv;
599 }
600 
601 PCSC_API p_SCardTransmit(SCardTransmit)
602 {
603  LONG rv;
604 
605  Enter();
606  spy_long(hCard);
607  spy_buffer(pbSendBuffer, cbSendLength);
608  rv = spy.SCardTransmit(hCard, pioSendPci, pbSendBuffer, cbSendLength,
609  pioRecvPci, pbRecvBuffer, pcbRecvLength);
610  if (pcbRecvLength)
611  spy_buffer(pbRecvBuffer, *pcbRecvLength);
612  else
613  spy_buffer(NULL, 0);
614  Quit();
615  return rv;
616 }
617 
618 PCSC_API p_SCardListReaderGroups(SCardListReaderGroups)
619 {
620  LONG rv;
621  int autoallocate = 0;
622 
623  if (pcchGroups)
624  autoallocate = *pcchGroups == SCARD_AUTOALLOCATE;
625 
626  Enter();
627  spy_long(hContext);
628  spy_ptr_ulong(pcchGroups);
629  rv = spy.SCardListReaderGroups(hContext, mszGroups, pcchGroups);
630  spy_n_str(mszGroups, pcchGroups, autoallocate);
631  Quit();
632  return rv;
633 }
634 
635 PCSC_API p_SCardListReaders(SCardListReaders)
636 {
637  LONG rv;
638  int autoallocate = 0;
639 
640  if (pcchReaders)
641  autoallocate = *pcchReaders == SCARD_AUTOALLOCATE;
642 
643  Enter();
644  spy_long(hContext);
645  spy_str(mszGroups);
646  rv = spy.SCardListReaders(hContext, mszGroups, mszReaders, pcchReaders);
647  spy_n_str(mszReaders, pcchReaders, autoallocate);
648  Quit();
649  return rv;
650 }
651 
652 PCSC_API p_SCardFreeMemory(SCardFreeMemory)
653 {
654  LONG rv;
655 
656  Enter();
657  spy_long(hContext);
658  spy_pvoid(pvMem);
659  rv = spy.SCardFreeMemory(hContext, pvMem);
660  Quit();
661  return rv;
662 }
663 
664 PCSC_API p_SCardCancel(SCardCancel)
665 {
666  LONG rv;
667 
668  Enter();
669  spy_long(hContext);
670  rv = spy.SCardCancel(hContext);
671  Quit();
672  return rv;
673 }
674 
675 PCSC_API p_SCardGetAttrib(SCardGetAttrib)
676 {
677  LONG rv;
678  int autoallocate = 0;
679 
680  if (pcbAttrLen)
681  autoallocate = *pcbAttrLen == SCARD_AUTOALLOCATE;
682 
683  Enter();
684  spy_long(hCard);
685  spy_long(dwAttrId);
686  rv = spy.SCardGetAttrib(hCard, dwAttrId, pbAttr, pcbAttrLen);
687  if (NULL == pcbAttrLen)
688  spy_buffer(NULL, 0);
689  else
690  {
691  const unsigned char *s = pbAttr;
692 
693  if (autoallocate)
694  s = *(unsigned char **)pbAttr;
695 
696  spy_buffer(s, *pcbAttrLen);
697  }
698  Quit();
699  return rv;
700 }
701 
702 PCSC_API p_SCardSetAttrib(SCardSetAttrib)
703 {
704  LONG rv;
705 
706  Enter();
707  spy_long(hCard);
708  spy_long(dwAttrId);
709  spy_buffer(pbAttr, cbAttrLen);
710  rv = spy.SCardSetAttrib(hCard, dwAttrId, pbAttr, cbAttrLen);
711  Quit();
712  return rv;
713 }
714 
715 PCSC_API p_pcsc_stringify_error(pcsc_stringify_error)
716 {
717  return spy.pcsc_stringify_error(pcscError);
718 }
719 
PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
Returns a list of currently available readers on the system.
PCSC_API LONG SCardListReaderGroups(SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups)
Returns a list of currently available reader groups on the system.
PCSC_API char * pcsc_stringify_error(const LONG pcscError)
Returns a human readable text for the given PC/SC error code.
Definition: error.c:56
PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext)
Creates an Application Context to the PC/SC Resource Manager.
Definition: winscard.c:173
#define SCARD_PROTOCOL_T1
T=1 active protocol.
Definition: pcsclite.h:153
PCSC_API LONG SCardFreeMemory(SCARDCONTEXT hContext, LPCVOID pvMem)
Releases memory that has been returned from the resource manager using the SCARD_AUTOALLOCATE length ...
LONG SCardGetStatusChange(SCARDCONTEXT hContext, DWORD dwTimeout, SCARD_READERSTATE *rgReaderStates, DWORD cReaders)
Blocks execution until the current availability of the cards in a specific set of readers changes...
PCSC_API LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen)
Get an attribute from the IFD Handler (reader driver).
Definition: winscard.c:1349
PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition)
Terminates a connection made through SCardConnect().
Definition: winscard.c:799
PCSC_API LONG SCardCancel(SCARDCONTEXT hContext)
Cancels all pending blocking requests on the SCardGetStatusChange() function.
const SCARD_IO_REQUEST g_rgSCardRawPci
Protocol Control Information for raw access.
Definition: libpcscspy.c:722
PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext)
Check if a SCARDCONTEXT is valid.
#define SCARD_AUTOALLOCATE
see SCardFreeMemory()
Definition: pcsclite.h:145
#define SCARD_PROTOCOL_T0
T=0 active protocol.
Definition: pcsclite.h:152
PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol)
Reestablishes a connection to a reader that was previously connected to using SCardConnect().
Definition: winscard.c:497
PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
Establishes a connection to the reader specified in * szReader.
Definition: winscard.c:209
PCSC_API LONG SCardStatus(SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
Returns the current status of the reader connected to by hCard.
Definition: winscard.c:1227
PCSC_API LONG SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
Set an attribute of the IFD Handler.
Definition: winscard.c:1425
LONG SCARDHANDLE
hCard returned by SCardConnect()
Definition: pcsclite.h:34
PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard)
Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.
Definition: winscard.c:1049
#define SCARD_PROTOCOL_RAW
Raw active protocol.
Definition: pcsclite.h:154
Protocol Control Information (PCI)
Definition: pcsclite.h:58
PCSC_API LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned)
Sends a command directly to the IFD Handler (reader driver) to be processed by the reader...
Definition: winscard.c:1290
#define MAX_ATR_SIZE
Maximum ATR size.
Definition: pcsclite.h:38
PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition)
Ends a previously begun transaction.
Definition: winscard.c:1091
PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
Sends an APDU to the smart card contained in the reader connected to by SCardConnect().
Definition: winscard.c:1475
const SCARD_IO_REQUEST g_rgSCardT1Pci
Protocol Control Information for T=1.
Definition: libpcscspy.c:721
#define SCARD_S_SUCCESS
error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx
Definition: pcsclite.h:80
const SCARD_IO_REQUEST g_rgSCardT0Pci
Protocol Control Information for T=0.
Definition: libpcscspy.c:720
This handles smart card reader communications.
PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext)
Destroys a communication context to the PC/SC Resource Manager.
Definition: winscard.c:198
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
Definition: pcsclite.h:81