From cc2350808242b9d77297513e087933a19a8373ef Mon Sep 17 00:00:00 2001 From: frankmorgner Date: Tue, 10 Apr 2012 22:15:57 +0000 Subject: [PATCH] stringify_error now returns a string instead of printing the error git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@741 96b47cad-a561-4643-ad3b-153ac7d7599c --- ccid/src/pcscutil.c | 126 ------------------------------------- ccid/src/pcscutil.h | 64 ------------------- pcsc-relay/src/pcscutil.c | 127 +++++++++++++++++++++++++++++++++++++- pcsc-relay/src/pcscutil.h | 65 ++++++++++++++++++- 4 files changed, 190 insertions(+), 192 deletions(-) delete mode 100644 ccid/src/pcscutil.c delete mode 100644 ccid/src/pcscutil.h mode change 120000 => 100644 pcsc-relay/src/pcscutil.c mode change 120000 => 100644 pcsc-relay/src/pcscutil.h diff --git a/ccid/src/pcscutil.c b/ccid/src/pcscutil.c deleted file mode 100644 index 8e4dc9d..0000000 --- a/ccid/src/pcscutil.c +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2010 Frank Morgner - * - * This file is part of ccid. - * - * ccid is free software: you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * ccid is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * ccid. If not, see . - */ -#include -#include - -#include "pcscutil.h" - -#pragma comment(lib, "winscard.lib") - -LONG -pcsc_connect(unsigned int readernum, DWORD dwShareMode, DWORD dwPreferredProtocols, - LPSCARDCONTEXT phContext, LPSTR *readers, LPSCARDHANDLE phCard, - LPDWORD pdwActiveProtocol) -{ - LONG r; - DWORD ctl, readerslen; - char *reader; - size_t l, i; - - r = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, phContext); - if (r != SCARD_S_SUCCESS) { - fprintf(stderr, "Could not connect to PC/SC Service\n"); - goto err; - } - - - readerslen = SCARD_AUTOALLOCATE; - r = SCardListReaders(*phContext, NULL, (LPTSTR) readers, &readerslen); - if (r != SCARD_S_SUCCESS) { - fprintf(stderr, "Could not get readers\n"); - goto err; - } - - for (reader = *readers, i = 0; readerslen > 0; - l = strlen(reader) + 1, readerslen -= l, reader += l, i++) { - - if (i == readernum) - break; - } - if (readerslen <= 0) { - fprintf(stderr, "Could not find reader number %u\n", readernum); - r = SCARD_E_UNKNOWN_READER; - goto err; - } - - - r = SCardConnect(*phContext, reader, dwShareMode, dwPreferredProtocols, - phCard, pdwActiveProtocol); - if (r != SCARD_S_SUCCESS) { - fprintf(stderr, "Could not connect to %s\n", reader); - goto err; - } - printf("Connected to %s\n", reader); - -err: - return r; -} - -void -pcsc_disconnect(SCARDCONTEXT hContext, SCARDHANDLE hCard, LPSTR readers) -{ - SCardDisconnect(hCard, SCARD_LEAVE_CARD); - SCardFreeMemory(hContext, readers); - SCardReleaseContext(hContext); -} - -LONG -pcsc_transmit(DWORD dwActiveProtocol, SCARDHANDLE hCard, - LPCBYTE pbSendBuffer, DWORD cbSendLength, - LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength) -{ - LONG r; - SCARD_IO_REQUEST ioRecvPci; - - switch (dwActiveProtocol) { - case SCARD_PROTOCOL_T0: - r = SCardTransmit(hCard, SCARD_PCI_T0, pbSendBuffer, cbSendLength, - &ioRecvPci, pbRecvBuffer, pcbRecvLength); - break; - - case SCARD_PROTOCOL_T1: - r = SCardTransmit(hCard, SCARD_PCI_T1, pbSendBuffer, cbSendLength, - &ioRecvPci, pbRecvBuffer, pcbRecvLength); - break; - - default: - fprintf(stderr, "Could not transmit with unknown protocol %u\n", - (unsigned int) dwActiveProtocol); - r = SCARD_E_PROTO_MISMATCH; - break; - } - - return r; -} - -void -printb(const char *label, unsigned char *buf, size_t len) -{ - size_t i = 0; - printf("%s", label); - while (i < len) { - printf("%02X", buf[i]); - i++; - if (i%20) - printf(" "); - else if (i != len) - printf("\n"); - } - printf("\n"); -} diff --git a/ccid/src/pcscutil.h b/ccid/src/pcscutil.h deleted file mode 100644 index 693b1cb..0000000 --- a/ccid/src/pcscutil.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2010 Frank Morgner - * - * This file is part of ccid. - * - * ccid is free software: you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * ccid is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * ccid. If not, see . - */ -#ifndef _PCSCUTIL_H -#define _PCSCUTIL_H - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include -#ifdef HAVE_PCSCLITE_H -#include -#define stringify_error(r) { if (r != SCARD_S_SUCCESS) fputs(pcsc_stringify_error(r), stderr); } -#else -#define stringify_error(r) { if (r != SCARD_S_SUCCESS) fprintf(stderr, "PC/SC error code %u\n", (unsigned int) r); } -#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1) -#endif - -#ifdef HAVE_READER_H -#include -#endif - - -#ifdef __cplusplus -extern "C" { -#endif - -LONG -pcsc_connect(unsigned int readernum, DWORD dwShareMode, DWORD dwPreferredProtocol, - SCARDCONTEXT *hContext, LPSTR *readers, LPSCARDHANDLE phCard, - LPDWORD pdwActiveProtocol); - -void -pcsc_disconnect(SCARDCONTEXT hContext, SCARDHANDLE hCard, LPSTR readers); - -LONG -pcsc_transmit(DWORD dwActiveProtocol, SCARDHANDLE hCard, - LPCBYTE pbSendBuffer, DWORD cbSendLength, - LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength); - -void -printb(const char *label, unsigned char *buf, size_t len); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/pcsc-relay/src/pcscutil.c b/pcsc-relay/src/pcscutil.c deleted file mode 120000 index 51b2c32..0000000 --- a/pcsc-relay/src/pcscutil.c +++ /dev/null @@ -1 +0,0 @@ -../../ccid/src/pcscutil.c \ No newline at end of file diff --git a/pcsc-relay/src/pcscutil.c b/pcsc-relay/src/pcscutil.c new file mode 100644 index 0000000..8e4dc9d --- /dev/null +++ b/pcsc-relay/src/pcscutil.c @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2010 Frank Morgner + * + * This file is part of ccid. + * + * ccid is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * ccid is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * ccid. If not, see . + */ +#include +#include + +#include "pcscutil.h" + +#pragma comment(lib, "winscard.lib") + +LONG +pcsc_connect(unsigned int readernum, DWORD dwShareMode, DWORD dwPreferredProtocols, + LPSCARDCONTEXT phContext, LPSTR *readers, LPSCARDHANDLE phCard, + LPDWORD pdwActiveProtocol) +{ + LONG r; + DWORD ctl, readerslen; + char *reader; + size_t l, i; + + r = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, phContext); + if (r != SCARD_S_SUCCESS) { + fprintf(stderr, "Could not connect to PC/SC Service\n"); + goto err; + } + + + readerslen = SCARD_AUTOALLOCATE; + r = SCardListReaders(*phContext, NULL, (LPTSTR) readers, &readerslen); + if (r != SCARD_S_SUCCESS) { + fprintf(stderr, "Could not get readers\n"); + goto err; + } + + for (reader = *readers, i = 0; readerslen > 0; + l = strlen(reader) + 1, readerslen -= l, reader += l, i++) { + + if (i == readernum) + break; + } + if (readerslen <= 0) { + fprintf(stderr, "Could not find reader number %u\n", readernum); + r = SCARD_E_UNKNOWN_READER; + goto err; + } + + + r = SCardConnect(*phContext, reader, dwShareMode, dwPreferredProtocols, + phCard, pdwActiveProtocol); + if (r != SCARD_S_SUCCESS) { + fprintf(stderr, "Could not connect to %s\n", reader); + goto err; + } + printf("Connected to %s\n", reader); + +err: + return r; +} + +void +pcsc_disconnect(SCARDCONTEXT hContext, SCARDHANDLE hCard, LPSTR readers) +{ + SCardDisconnect(hCard, SCARD_LEAVE_CARD); + SCardFreeMemory(hContext, readers); + SCardReleaseContext(hContext); +} + +LONG +pcsc_transmit(DWORD dwActiveProtocol, SCARDHANDLE hCard, + LPCBYTE pbSendBuffer, DWORD cbSendLength, + LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength) +{ + LONG r; + SCARD_IO_REQUEST ioRecvPci; + + switch (dwActiveProtocol) { + case SCARD_PROTOCOL_T0: + r = SCardTransmit(hCard, SCARD_PCI_T0, pbSendBuffer, cbSendLength, + &ioRecvPci, pbRecvBuffer, pcbRecvLength); + break; + + case SCARD_PROTOCOL_T1: + r = SCardTransmit(hCard, SCARD_PCI_T1, pbSendBuffer, cbSendLength, + &ioRecvPci, pbRecvBuffer, pcbRecvLength); + break; + + default: + fprintf(stderr, "Could not transmit with unknown protocol %u\n", + (unsigned int) dwActiveProtocol); + r = SCARD_E_PROTO_MISMATCH; + break; + } + + return r; +} + +void +printb(const char *label, unsigned char *buf, size_t len) +{ + size_t i = 0; + printf("%s", label); + while (i < len) { + printf("%02X", buf[i]); + i++; + if (i%20) + printf(" "); + else if (i != len) + printf("\n"); + } + printf("\n"); +} diff --git a/pcsc-relay/src/pcscutil.h b/pcsc-relay/src/pcscutil.h deleted file mode 120000 index 91d334e..0000000 --- a/pcsc-relay/src/pcscutil.h +++ /dev/null @@ -1 +0,0 @@ -../../ccid/src/pcscutil.h \ No newline at end of file diff --git a/pcsc-relay/src/pcscutil.h b/pcsc-relay/src/pcscutil.h new file mode 100644 index 0000000..c5d31c4 --- /dev/null +++ b/pcsc-relay/src/pcscutil.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2010-2012 Frank Morgner . + * + * This file is part of ccid. + * + * ccid is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * ccid is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * ccid. If not, see . + */ +#ifndef _PCSCUTIL_H +#define _PCSCUTIL_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#ifdef HAVE_PCSCLITE_H +#include +#define stringify_error(r) pcsc_stringify_error(r) +#else +#define stringify_error(r) "PC/SC error code %u\n", (unsigned int) r +#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1) +#endif + +#ifdef HAVE_READER_H +#include +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + +LONG +pcsc_connect(unsigned int readernum, DWORD dwShareMode, DWORD dwPreferredProtocol, + SCARDCONTEXT *hContext, LPSTR *readers, LPSCARDHANDLE phCard, + LPDWORD pdwActiveProtocol); + +void +pcsc_disconnect(SCARDCONTEXT hContext, SCARDHANDLE hCard, LPSTR readers); + +LONG +pcsc_transmit(DWORD dwActiveProtocol, SCARDHANDLE hCard, + LPCBYTE pbSendBuffer, DWORD cbSendLength, + LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength); + +void +printb(const char *label, unsigned char *buf, size_t len); + +#ifdef __cplusplus +} +#endif +#endif