Color output for derdump (after "pip install colored")
[hexio] / pcscio.c
1 /* pcscio.c -- input/output through PCSClite */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <string.h>
8
9 #include <sys/types.h>
10 #include <sys/select.h>
11
12 #include <pthread.h>
13 #include <winscard.h>
14
15
16 /*
17 int             pthread_detach __P((pthread_t pth)) {
18         fprintf (stderr, "ERROR: pthread_detach called\n");
19 }
20 */
21
22
23 int main (int argc, char *argv []) {
24         int busy=1;
25         SCARDCONTEXT ctx;
26         SCARDHANDLE card;
27         LONG err;
28         DWORD actproto;
29
30         if (SCardEstablishContext (SCARD_SCOPE_SYSTEM, NULL, NULL, &ctx) != SCARD_S_SUCCESS) {
31                 fprintf (stderr, "Failed to contact pcscd\n");
32                 exit (1);
33         }
34         if (argc != 2) {
35                 DWORD listsz=0;
36                 LPTSTR readers=NULL, reader=NULL;
37                 fprintf (stderr, "Usage: %s readername\n", argv [0]);
38                 if ((SCardListReaders (ctx, NULL, NULL, &listsz) == SCARD_S_SUCCESS)
39                  && (readers=malloc (listsz))
40                  && (SCardListReaders (ctx, NULL, readers, &listsz) == SCARD_S_SUCCESS)) {
41                         fprintf (stderr, "Currently attached readers:\n");
42                         reader = readers;
43                         while (*reader) {
44                                 fprintf (stderr, " - %s\n", reader);
45                                 reader = reader + strlen (reader);
46                                 reader++;
47                         }
48                 }
49                 if (readers) {
50                         free (readers);
51                 }
52                 SCardReleaseContext (ctx);
53                 exit (1);
54         }
55         err = SCardConnect (ctx, argv [1], SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T1, &card, &actproto);
56         if (err != SCARD_S_SUCCESS) {
57                 fprintf (stderr, "Failed to access the smart card\n");
58                 busy = 0;
59         }
60         fprintf (stderr, "Yippy!\n");
61         while (busy) {
62                 BYTE txbuf [275], rxbuf [275];
63                 DWORD rxlen;
64                 SCARD_IO_REQUEST req;
65                 int txlen = read (0, txbuf, sizeof (txbuf));
66                 if (txlen < 0) {
67                         perror ("Error on stdin");
68                         busy = 0;
69                 } else if (txlen == 0) {
70                         /* EOF encountered */
71                         busy = 0;
72                 } else {
73                         req.dwProtocol = actproto;
74                         req.cbPciLength = sizeof (req);
75                         rxlen = sizeof (rxbuf);
76                         err = SCardTransmit (card, &req, txbuf, txlen, &req, rxbuf, &rxlen);
77                         if (err != SCARD_S_SUCCESS) {
78                                 fprintf (stderr, "Error writing to smart card\n");
79                                 busy = 0;
80                         } else {
81                                 if (write (1, txbuf, rxlen) < rxlen) {
82                                         perror ("Partial output");
83                                         busy = 0;
84                                 }
85                         }
86                 }
87         }
88         SCardReleaseContext (ctx);
89         return 0;
90 }