From 7635468ed975bc4417ff47f4ec0d4be279dfab9b Mon Sep 17 00:00:00 2001 From: fabled Date: Fri, 14 Jun 2002 12:52:56 +0000 Subject: [PATCH] - Initial support for win32 git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@654 c6295689-39f2-0310-b995-f0e70906c6a9 --- Makefile.mak | 8 ++++++ README.Win32 | 26 +++++++++++++++++ src/Makefile.am | 3 ++ src/Makefile.mak | 8 ++++++ src/common/Makefile.am | 2 +- src/common/Makefile.mak | 14 +++++++++ src/include/Makefile.mak | 5 ++++ src/include/winconfig.h | 26 +++++++++++++++++ src/libopensc/Makefile.am | 2 +- src/libopensc/Makefile.mak | 26 +++++++++++++++++ src/libopensc/asn1.c | 10 ++++++- src/libopensc/card-default.c | 3 +- src/libopensc/card-etoken.c | 2 +- src/libopensc/card-flex.c | 16 +++++------ src/libopensc/card-miocos.c | 4 +-- src/libopensc/card-tcos.c | 1 + src/libopensc/card.c | 26 ++++++++++++----- src/libopensc/ctx.c | 6 ++-- src/libopensc/log.c | 11 ++++++-- src/libopensc/log.h | 5 ++-- src/libopensc/module.c | 7 +++++ src/libopensc/opensc.h | 2 +- src/libopensc/pkcs15-cache.c | 2 ++ src/libopensc/pkcs15-cert.c | 2 ++ src/libopensc/pkcs15-sec.c | 2 ++ src/libopensc/reader-pcsc.c | 55 +++++++++++++++++++++++++----------- src/libopensc/sc.c | 7 ++++- src/libopensc/sec.c | 2 ++ src/scconf/Makefile.am | 2 ++ src/scconf/Makefile.mak | 17 +++++++++++ src/scconf/parse.c | 2 ++ src/scconf/scconf.c | 4 +++ src/tests/Makefile.am | 2 +- src/tests/pintest.c | 1 - src/tests/sc-test.c | 2 +- src/tools/Makefile.am | 2 ++ src/tools/Makefile.mak | 17 +++++++++++ src/tools/opensc-explorer.c | 2 +- src/tools/opensc-tool.c | 2 ++ src/tools/pkcs15-crypt.c | 4 ++- src/tools/util.h | 7 +++-- win32/Make.rules.mak | 11 ++++++++ win32/makedef.pl | 33 ++++++++++++++++++++++ 43 files changed, 337 insertions(+), 54 deletions(-) create mode 100644 Makefile.mak create mode 100644 README.Win32 create mode 100644 src/Makefile.mak create mode 100644 src/common/Makefile.mak create mode 100644 src/include/Makefile.mak create mode 100644 src/include/winconfig.h create mode 100644 src/libopensc/Makefile.mak create mode 100644 src/scconf/Makefile.mak create mode 100644 src/tools/Makefile.mak create mode 100644 win32/Make.rules.mak create mode 100644 win32/makedef.pl diff --git a/Makefile.mak b/Makefile.mak new file mode 100644 index 00000000..c6adfd4d --- /dev/null +++ b/Makefile.mak @@ -0,0 +1,8 @@ + +SUBDIRS = src + +all:: + +all depend install clean:: + @for %i in ( $(SUBDIRS) ) do \ + @cmd /c "cd %i && $(MAKE) /nologo /f Makefile.mak $@" diff --git a/README.Win32 b/README.Win32 new file mode 100644 index 00000000..4e832eaf --- /dev/null +++ b/README.Win32 @@ -0,0 +1,26 @@ +README for Win32 port of OpenSC + + +Compiling +========= + +Type "nmake -f makefile.mak" to compile. + +You need also perl and flex installed for the compile process +to complete succesfully. + +What works +========== + +At the moment only libopensc and basic tools have been ported +to windows. They should at least compile. + +What needs to be done +===================== + +Other parts of OpenSC should ported as well (other tools & pkcs11 +module). Also we should implement native Win32 APIs such as +CryptoAPI Provider, some login stuff and ActiveX plugin for +Explorer to do the signing. + + diff --git a/src/Makefile.am b/src/Makefile.am index 3562785e..73bcd306 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,5 +2,8 @@ MAINTAINERCLEANFILES = Makefile.in +EXTRA_DIST = Makefile.mak + # Order IS important SUBDIRS = common include scconf scldap scrandom libopensc pkcs15init tests tools openssh scam pam sia signer pkcs11 + diff --git a/src/Makefile.mak b/src/Makefile.mak new file mode 100644 index 00000000..873a585c --- /dev/null +++ b/src/Makefile.mak @@ -0,0 +1,8 @@ + +SUBDIRS = include common scconf libopensc tools + +all:: + +all depend install clean:: + @for %i in ( $(SUBDIRS) ) do \ + @cmd /c "cd %i && $(MAKE) /nologo /f Makefile.mak $@" diff --git a/src/common/Makefile.am b/src/common/Makefile.am index a3a269e4..34696eae 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -2,4 +2,4 @@ MAINTAINERCLEANFILES = Makefile.in -EXTRA_DIST = getopt.c getopt.h getopt1.c +EXTRA_DIST = Makefile.mak getopt.c getopt.h getopt1.c getpass.c diff --git a/src/common/Makefile.mak b/src/common/Makefile.mak new file mode 100644 index 00000000..71649b84 --- /dev/null +++ b/src/common/Makefile.mak @@ -0,0 +1,14 @@ +TOPDIR = ..\.. + +HEADERS = getopt.h +TARGET = common.lib +OBJECTS = getopt.obj getopt1.obj getpass.obj + + +all: $(TARGET) install-headers + +$(TARGET): $(OBJECTS) + lib /nologo /machine:ix86 /out:$(TARGET) $(OBJECTS) + +!INCLUDE $(TOPDIR)\win32\Make.rules.mak + diff --git a/src/include/Makefile.mak b/src/include/Makefile.mak new file mode 100644 index 00000000..50540e46 --- /dev/null +++ b/src/include/Makefile.mak @@ -0,0 +1,5 @@ + +all: config.h + +config.h: winconfig.h + @copy /y winconfig.h config.h diff --git a/src/include/winconfig.h b/src/include/winconfig.h new file mode 100644 index 00000000..079baa4d --- /dev/null +++ b/src/include/winconfig.h @@ -0,0 +1,26 @@ +#ifndef _OPENSC_WINCONFIG_H +#define _OPENSC_WINCONFIG_H + +#ifndef strcasecmp +#define strcasecmp stricmp +#endif + +#ifndef strncasecmp +#define strncasecmp strnicmp +#endif + +#ifndef snprintf +#define snprintf _snprintf +#endif + +#ifndef vsnprintf +#define vsnprintf _vsnprintf +#endif + +#ifndef isatty +#define isatty _isatty +#endif + +#define OPENSC_CONF_PATH "C:\\WINNT\\opensc.conf" + +#endif diff --git a/src/libopensc/Makefile.am b/src/libopensc/Makefile.am index f898309e..6534b8a3 100644 --- a/src/libopensc/Makefile.am +++ b/src/libopensc/Makefile.am @@ -4,7 +4,7 @@ includedir = ${prefix}/include/opensc MAINTAINERCLEANFILES = Makefile.in -EXTRA_DIST = opensc-config.in +EXTRA_DIST = Makefile.mak opensc-config.in if HAVE_SSL SSL_LIB = @LIBCRYPTO@ diff --git a/src/libopensc/Makefile.mak b/src/libopensc/Makefile.mak new file mode 100644 index 00000000..14db3876 --- /dev/null +++ b/src/libopensc/Makefile.mak @@ -0,0 +1,26 @@ +TOPDIR = ..\.. + + +TARGET = opensc.dll + +HEADERS = opensc.h pkcs15.h emv.h \ + errors.h types.h \ + cardctl.h asn1.h log.h + +OBJECTS = sc.obj ctx.obj module.obj asn1.obj log.obj base64.obj \ + errors.obj sec.obj card.obj iso7816.obj dir.obj \ + pkcs15.obj pkcs15-cert.obj pkcs15-pin.obj \ + pkcs15-prkey.obj pkcs15-pubkey.obj pkcs15-sec.obj \ + pkcs15-wrap.obj pkcs15-algo.obj \ + pkcs15-cache.obj reader-pcsc.obj \ + card-setcos.obj card-miocos.obj card-flex.obj card-gpk.obj \ + card-etoken.obj card-tcos.obj card-emv.obj card-default.obj + +all: $(TARGET) install-headers + +!INCLUDE $(TOPDIR)\win32\Make.rules.mak + +$(TARGET): $(OBJECTS) + perl $(TOPDIR)\win32\makedef.pl $*.def $* $(OBJECTS) + link $(LINKFLAGS) /dll /def:$*.def /implib:$*.lib /out:$(TARGET) $(OBJECTS) ..\scconf\scconf.lib winscard.lib + diff --git a/src/libopensc/asn1.c b/src/libopensc/asn1.c index 72487ec0..986e2db5 100644 --- a/src/libopensc/asn1.c +++ b/src/libopensc/asn1.c @@ -153,7 +153,11 @@ static void sc_asn1_print_utf8string(const u8 * buf, size_t buflen) static void sc_asn1_print_integer(const u8 * buf, size_t buflen) { +#ifndef _WIN32 long long a = 0; +#else + __int64 a = 0; +#endif int i; if (buflen > sizeof(a)) { @@ -169,7 +173,11 @@ static void sc_asn1_print_integer(const u8 * buf, size_t buflen) static void sc_asn1_print_bit_string(const u8 * buf, size_t buflen) { - unsigned long long a = 0; +#ifndef _WIN32 + long long a = 0; +#else + __int64 a = 0; +#endif int i, r; if (buflen > sizeof(a) + 1) { diff --git a/src/libopensc/card-default.c b/src/libopensc/card-default.c index c9ed9646..9e2863ec 100644 --- a/src/libopensc/card-default.c +++ b/src/libopensc/card-default.c @@ -58,7 +58,8 @@ static int autodetect_class(struct sc_card *card) apdu.datalen = 0; apdu.lc = 0; apdu.le = 256; - apdu.resplen = sizeof(rbuf); + apdu.resp = rbuf; + apdu.resplen = sizeof(rbuf); r = sc_transmit_apdu(card, &apdu); SC_TEST_RET(card->ctx, r, "APDU transmit failed"); if (apdu.sw1 == 0x6E) diff --git a/src/libopensc/card-etoken.c b/src/libopensc/card-etoken.c index 66d5930e..add1f340 100644 --- a/src/libopensc/card-etoken.c +++ b/src/libopensc/card-etoken.c @@ -386,7 +386,7 @@ static void parse_sec_attr(struct sc_file *file, const u8 *buf, size_t len) /* acl defaults to 0xFF if unspecified */ for (i = 0; i < 9; i++) if (idx[i] != -1) - add_acl_entry(file, idx[i], (i < len) ? buf[i] : 0xFF); + add_acl_entry(file, idx[i], (u8)((i < len) ? buf[i] : 0xFF)); } static int etoken_select_file(struct sc_card *card, diff --git a/src/libopensc/card-flex.c b/src/libopensc/card-flex.c index f65df3df..fa1477cb 100644 --- a/src/libopensc/card-flex.c +++ b/src/libopensc/card-flex.c @@ -195,18 +195,18 @@ static int parse_flex_sf_reply(struct sc_context *ctx, const u8 *buf, int buflen } p += 2; if (file->type == SC_FILE_TYPE_DF) { - add_acl_entry(file, SC_AC_OP_LIST_FILES, p[0] >> 4, is_mf); - add_acl_entry(file, SC_AC_OP_DELETE, p[1] >> 4, is_mf); - add_acl_entry(file, SC_AC_OP_CREATE, p[1] & 0x0F, is_mf); + add_acl_entry(file, SC_AC_OP_LIST_FILES, (u8)(p[0] >> 4), is_mf); + add_acl_entry(file, SC_AC_OP_DELETE, (u8)(p[1] >> 4), is_mf); + add_acl_entry(file, SC_AC_OP_CREATE, (u8)(p[1] & 0x0F), is_mf); } else { /* EF */ - add_acl_entry(file, SC_AC_OP_READ, p[0] >> 4, 0); + add_acl_entry(file, SC_AC_OP_READ, (u8)(p[0] >> 4), 0); switch (file->ef_structure) { case SC_FILE_EF_TRANSPARENT: - add_acl_entry(file, SC_AC_OP_UPDATE, p[0] & 0x0F, 0); + add_acl_entry(file, SC_AC_OP_UPDATE, (u8)(p[0] & 0x0F), 0); break; case SC_FILE_EF_LINEAR_FIXED: case SC_FILE_EF_LINEAR_VARIABLE: - add_acl_entry(file, SC_AC_OP_UPDATE, p[0] & 0x0F, 0); + add_acl_entry(file, SC_AC_OP_UPDATE, (u8)(p[0] & 0x0F), 0); break; case SC_FILE_EF_CYCLIC: #if 0 @@ -216,8 +216,8 @@ static int parse_flex_sf_reply(struct sc_context *ctx, const u8 *buf, int buflen break; } } - add_acl_entry(file, SC_AC_OP_REHABILITATE, p[2] >> 4, is_mf); - add_acl_entry(file, SC_AC_OP_INVALIDATE, p[2] & 0x0F, is_mf); + add_acl_entry(file, SC_AC_OP_REHABILITATE, (u8)(p[2] >> 4), is_mf); + add_acl_entry(file, SC_AC_OP_INVALIDATE, (u8)(p[2] & 0x0F), is_mf); p += 3; if (*p++) file->status = SC_FILE_STATUS_ACTIVATED; diff --git a/src/libopensc/card-miocos.c b/src/libopensc/card-miocos.c index b9c8504e..6db8323a 100644 --- a/src/libopensc/card-miocos.c +++ b/src/libopensc/card-miocos.c @@ -319,9 +319,9 @@ static void parse_sec_attr(struct sc_file *file, const u8 *buf, size_t len) if (ops[i] == -1) continue; if ((i & 1) == 0) - add_acl_entry(file, ops[i], buf[i / 2] >> 4); + add_acl_entry(file, ops[i], (u8)(buf[i / 2] >> 4)); else - add_acl_entry(file, ops[i], buf[i / 2] & 0x0F); + add_acl_entry(file, ops[i], (u8)(buf[i / 2] & 0x0F)); } } diff --git a/src/libopensc/card-tcos.c b/src/libopensc/card-tcos.c index 49e052b2..f264b659 100644 --- a/src/libopensc/card-tcos.c +++ b/src/libopensc/card-tcos.c @@ -25,6 +25,7 @@ #include "cardctl.h" #include #include +#include static const char *tcos_atrs[] = { "3B:BA:13:00:81:31:86:5D:00:64:05:0A:02:01:31:80:90:00:8B", /* SLE44 */ diff --git a/src/libopensc/card.c b/src/libopensc/card.c index fc66239f..1f7725db 100644 --- a/src/libopensc/card.c +++ b/src/libopensc/card.c @@ -288,8 +288,10 @@ static struct sc_card * sc_card_new() return NULL; } card->app_count = -1; - card->magic = SC_CARD_MAGIC; + card->magic = SC_CARD_MAGIC; +#ifdef HAVE_PTHREAD pthread_mutex_init(&card->mutex, NULL); +#endif return card; } @@ -311,8 +313,10 @@ static void sc_card_free(struct sc_card *card) free(card->ops); if (card->algorithms != NULL) free(card->algorithms); +#ifdef HAVE_PTHREAD pthread_mutex_destroy(&card->mutex); - card->magic = 0; +#endif + card->magic = 0; free(card); } @@ -429,8 +433,10 @@ int sc_lock(struct sc_card *card) assert(card != NULL); SC_FUNC_CALLED(card->ctx, 2); +#ifdef HAVE_PTHREAD pthread_mutex_lock(&card->mutex); - if (card->lock_count == 0) { +#endif + if (card->lock_count == 0) { if (card->reader->ops->lock != NULL) r = card->reader->ops->lock(card->reader, card->slot); if (r == 0) @@ -438,8 +444,10 @@ int sc_lock(struct sc_card *card) } if (r == 0) card->lock_count++; +#ifdef HAVE_PTHREAD pthread_mutex_unlock(&card->mutex); - SC_FUNC_RETURN(card->ctx, 2, r); +#endif + SC_FUNC_RETURN(card->ctx, 2, r); } int sc_unlock(struct sc_card *card) @@ -448,16 +456,20 @@ int sc_unlock(struct sc_card *card) assert(card != NULL); SC_FUNC_CALLED(card->ctx, 2); +#ifdef HAVE_PTHREAD pthread_mutex_lock(&card->mutex); - card->lock_count--; +#endif + card->lock_count--; assert(card->lock_count >= 0); if (card->lock_count == 0) { if (card->reader->ops->unlock != NULL) r = card->reader->ops->unlock(card->reader, card->slot); card->cache_valid = 0; memset(&card->cache, 0, sizeof(card->cache)); - } - pthread_mutex_unlock(&card->mutex); + } +#ifdef HAVE_PTHREAD + pthread_mutex_unlock(&card->mutex); +#endif SC_FUNC_RETURN(card->ctx, 2, r); } diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c index 4e1a7c59..c645c7c4 100644 --- a/src/libopensc/ctx.c +++ b/src/libopensc/ctx.c @@ -60,10 +60,12 @@ static const struct _sc_driver_entry internal_card_drivers[] = { }; static const struct _sc_driver_entry internal_reader_drivers[] = { -#ifdef HAVE_LIBPCSCLITE +#if defined(HAVE_LIBPCSCLITE) || defined(_WIN32) { "pcsc", (void *) sc_get_pcsc_driver, NULL }, #endif - { "ctapi", (void *) sc_get_ctapi_driver, NULL }, +#ifndef _WIN32 + { "ctapi", (void *) sc_get_ctapi_driver, NULL }, +#endif { NULL, NULL, NULL } }; diff --git a/src/libopensc/log.c b/src/libopensc/log.c index 87e79eb7..aebe5345 100644 --- a/src/libopensc/log.c +++ b/src/libopensc/log.c @@ -18,14 +18,21 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#ifdef HAVE_CONFIG_H +#include +#endif #include "log.h" #include #include #include -#include #include #include +#ifndef _WIN32 +#include #include +#else +#include +#endif #ifndef __GNUC__ void error(struct sc_context *ctx, const char *format, ...) @@ -90,11 +97,9 @@ void do_log2(struct sc_context *ctx, int type, const char *file, FILE *outf = NULL; char buf[1024], *p; int left, r; - struct timeval tv; const char *color_pfx = "", *color_sfx = ""; assert(ctx != NULL); - gettimeofday(&tv, NULL); switch (type) { case SC_LOG_TYPE_ERROR: if (ctx->log_errors == 0) diff --git a/src/libopensc/log.h b/src/libopensc/log.h index 32b7c1cb..df084367 100644 --- a/src/libopensc/log.h +++ b/src/libopensc/log.h @@ -32,8 +32,9 @@ extern "C" { #define SC_LOG_TYPE_VERBOSE 1 #define SC_LOG_TYPE_DEBUG 2 -#ifdef __GNUC__ -#define error(ctx, format, args...) do_log(ctx, SC_LOG_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__ , format , ## args) +#if defined(__GNUC__) + +#define error(ctx, format, args...) do_log(ctx, SC_LOG_TYPE_ERROR, __FILE__, __LINE__, __FUNCTION__, format , ## args) #define debug(ctx, format, args...) do_log(ctx, SC_LOG_TYPE_DEBUG, __FILE__, __LINE__, __FUNCTION__, format , ## args) #else diff --git a/src/libopensc/module.c b/src/libopensc/module.c index 06561f45..8f125323 100644 --- a/src/libopensc/module.c +++ b/src/libopensc/module.c @@ -27,6 +27,8 @@ #include #endif +#ifndef _WIN32 + int sc_module_open(struct sc_context *ctx, void **mod_handle, const char *filename) { const char *error; @@ -96,3 +98,8 @@ int sc_module_get_address(struct sc_context *ctx, void *mod_handle, void **sym_a *sym_address = address; return SC_SUCCESS; } + +#else + +#endif + diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h index 2f6d277a..99b8a5d0 100644 --- a/src/libopensc/opensc.h +++ b/src/libopensc/opensc.h @@ -688,7 +688,7 @@ struct sc_card_error { const char *errorstr; }; -extern const char *sc_version; +extern const char *sc_get_version(void); extern const struct sc_reader_driver *sc_get_pcsc_driver(void); extern const struct sc_reader_driver *sc_get_ctapi_driver(void); diff --git a/src/libopensc/pkcs15-cache.c b/src/libopensc/pkcs15-cache.c index b1c35017..97957bd8 100644 --- a/src/libopensc/pkcs15-cache.c +++ b/src/libopensc/pkcs15-cache.c @@ -21,7 +21,9 @@ #include "internal.h" #include "pkcs15.h" #include "log.h" +#ifndef _WIN32 #include +#endif #include #include #include diff --git a/src/libopensc/pkcs15-cert.c b/src/libopensc/pkcs15-cert.c index ffa4de11..21f04e85 100644 --- a/src/libopensc/pkcs15-cert.c +++ b/src/libopensc/pkcs15-cert.c @@ -26,7 +26,9 @@ #include #include #include +#ifndef _WIN32 #include +#endif #include static int parse_x509_cert(struct sc_context *ctx, const u8 *buf, size_t buflen, struct sc_pkcs15_cert *cert) diff --git a/src/libopensc/pkcs15-sec.c b/src/libopensc/pkcs15-sec.c index 54e849c4..05e037f7 100644 --- a/src/libopensc/pkcs15-sec.c +++ b/src/libopensc/pkcs15-sec.c @@ -25,7 +25,9 @@ #include #include #include +#ifndef _WIN32 #include +#endif static int pkcs1_strip_padding(u8 *data, size_t len) { diff --git a/src/libopensc/reader-pcsc.c b/src/libopensc/reader-pcsc.c index f6bc09b8..c640e6a3 100644 --- a/src/libopensc/reader-pcsc.c +++ b/src/libopensc/reader-pcsc.c @@ -23,7 +23,7 @@ #include #include #include -#include +/* #include */ #include /* Default timeout value for SCardGetStatusChange @@ -36,6 +36,20 @@ #define SC_STATUS_TIMEOUT SC_CUSTOM_STATUS_TIMEOUT #endif +#ifdef _WIN32 +/* Some windows specific kludge */ +#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0) +#define SCARD_SCOPE_GLOBAL SCARD_SCOPE_USER + +/* Error printing */ +#define PCSC_ERROR(ctx, desc, rv) error(ctx, desc ": %lx\n", rv); + +#else + +#define PCSC_ERROR(ctx, desc, rv) error(ctx, desc ": %s\n", pcsc_stringify_error(rv)); + +#endif + #define GET_SLOT_PTR(s, i) (&(s)->slot[(i)]) #define GET_PRIV_DATA(r) ((struct pcsc_private_data *) (r)->drv_data) #define GET_SLOT_DATA(r) ((struct pcsc_slot_data *) (r)->drv_data) @@ -112,9 +126,9 @@ static int pcsc_transmit(struct sc_reader *reader, struct sc_slot_info *slot, card = pslot->pcsc_card; sSendPci.dwProtocol = opensc_proto_to_pcsc(slot->active_protocol); - sSendPci.cbPciLength = 0; + sSendPci.cbPciLength = sizeof(sSendPci); sRecvPci.dwProtocol = opensc_proto_to_pcsc(slot->active_protocol); - sRecvPci.cbPciLength = 0; + sRecvPci.cbPciLength = sizeof(sRecvPci); if (prv->gpriv->apdu_fix && sendsize >= 6) { /* Check if the APDU in question is of Case 4 */ @@ -134,10 +148,13 @@ static int pcsc_transmit(struct sc_reader *reader, struct sc_slot_info *slot, dwSendLength = sendsize; dwRecvLength = *recvsize; - if (dwRecvLength > 255) + + if (dwRecvLength > 255) dwRecvLength = 255; - rv = SCardTransmit(card, &sSendPci, sendbuf, dwSendLength, - &sRecvPci, recvbuf, &dwRecvLength); + + rv = SCardTransmit(card, &sSendPci, sendbuf, dwSendLength, + &sRecvPci, recvbuf, &dwRecvLength); + if (rv != SCARD_S_SUCCESS) { switch (rv) { case SCARD_W_REMOVED_CARD: @@ -151,8 +168,8 @@ static int pcsc_transmit(struct sc_reader *reader, struct sc_slot_info *slot, return SC_ERROR_CARD_REMOVED; #endif return SC_ERROR_TRANSMIT_FAILED; - default: - error(reader->ctx, "SCardTransmit failed: %s\n", pcsc_stringify_error(rv)); + default: + PCSC_ERROR(reader->ctx, "SCardTransmit failed", rv); return SC_ERROR_TRANSMIT_FAILED; } } @@ -174,7 +191,7 @@ static int pcsc_detect_card_presence(struct sc_reader *reader, struct sc_slot_in rgReaderStates[0].dwEventState = SCARD_STATE_UNAWARE; ret = SCardGetStatusChange(priv->pcsc_ctx, SC_STATUS_TIMEOUT, rgReaderStates, 1); if (ret != 0) { - error(reader->ctx, "SCardGetStatusChange failed: %s\n", pcsc_stringify_error(ret)); + PCSC_ERROR(reader->ctx, "SCardGetStatusChange failed", ret); SC_FUNC_RETURN(reader->ctx, 1, pcsc_ret_to_error(ret)); } if (rgReaderStates[0].dwEventState & SCARD_STATE_PRESENT) { @@ -196,7 +213,7 @@ static int refresh_slot_attributes(struct sc_reader *reader, struct sc_slot_info rgReaderStates[0].dwEventState = SCARD_STATE_UNAWARE; ret = SCardGetStatusChange(priv->pcsc_ctx, SC_STATUS_TIMEOUT, rgReaderStates, 1); if (ret != 0) { - error(reader->ctx, "SCardGetStatusChange failed: %s\n", pcsc_stringify_error(ret)); + PCSC_ERROR(reader->ctx, "SCardGetStatusChange failed", ret); return pcsc_ret_to_error(ret); } slot->flags = 0; @@ -230,11 +247,11 @@ static int pcsc_connect(struct sc_reader *reader, struct sc_slot_info *slot) pslot = (struct pcsc_slot_data *) malloc(sizeof(struct pcsc_slot_data)); if (pslot == NULL) return SC_ERROR_OUT_OF_MEMORY; - rv = SCardConnect(priv->pcsc_ctx, priv->reader_name, + rv = SCardConnect(priv->pcsc_ctx, priv->reader_name, SCARD_SHARE_SHARED, SCARD_PROTOCOL_ANY, - &card_handle, &active_proto); + &card_handle, &active_proto); if (rv != 0) { - error(reader->ctx, "SCardConnect failed: %s\n", pcsc_stringify_error(rv)); + PCSC_ERROR(reader->ctx, "SCardConnect failed", rv); free(pslot); return pcsc_ret_to_error(rv); } @@ -265,7 +282,7 @@ static int pcsc_lock(struct sc_reader *reader, struct sc_slot_info *slot) assert(pslot != NULL); rv = SCardBeginTransaction(pslot->pcsc_card); if (rv != SCARD_S_SUCCESS) { - error(reader->ctx, "SCardBeginTransaction failed: %s\n", pcsc_stringify_error(rv)); + PCSC_ERROR(reader->ctx, "SCardBeginTransaction failed", rv); return pcsc_ret_to_error(rv); } return 0; @@ -279,7 +296,7 @@ static int pcsc_unlock(struct sc_reader *reader, struct sc_slot_info *slot) assert(pslot != NULL); rv = SCardEndTransaction(pslot->pcsc_card, SCARD_LEAVE_CARD); if (rv != SCARD_S_SUCCESS) { - error(reader->ctx, "SCardEndTransaction failed: %s\n", pcsc_stringify_error(rv)); + PCSC_ERROR(reader->ctx, "SCardEndTransaction failed", rv); return pcsc_ret_to_error(rv); } return 0; @@ -313,7 +330,13 @@ static int pcsc_init(struct sc_context *ctx, void **reader_data) struct pcsc_global_private_data *gpriv; scconf_block **blocks = NULL, *conf_block = NULL; - rv = SCardEstablishContext(SCARD_SCOPE_GLOBAL, "localhost", NULL, + rv = SCardEstablishContext(SCARD_SCOPE_GLOBAL, +#ifndef _WIN32 + "localhost", +#else + NULL, +#endif + NULL, &pcsc_ctx); if (rv != SCARD_S_SUCCESS) return pcsc_ret_to_error(rv); diff --git a/src/libopensc/sc.c b/src/libopensc/sc.c index 61043cb8..d3dd4663 100644 --- a/src/libopensc/sc.c +++ b/src/libopensc/sc.c @@ -26,13 +26,18 @@ #include #include +#undef sc_version #ifdef VERSION const char *sc_version = VERSION; #else -#warning FIXME: version info undefined const char *sc_version = "(undef)"; #endif +const char *sc_get_version(void) +{ + return sc_version; +} + int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen) { int err = 0; diff --git a/src/libopensc/sec.c b/src/libopensc/sec.c index deef8745..511b7267 100644 --- a/src/libopensc/sec.c +++ b/src/libopensc/sec.c @@ -20,7 +20,9 @@ #include "internal.h" #include "log.h" +#ifndef _WIN32 #include +#endif #include #include diff --git a/src/scconf/Makefile.am b/src/scconf/Makefile.am index 241ef324..ce44d039 100644 --- a/src/scconf/Makefile.am +++ b/src/scconf/Makefile.am @@ -4,6 +4,8 @@ includedir = ${prefix}/include/opensc MAINTAINERCLEANFILES = Makefile.in lex-parse.c +EXTRA_DIST = Makefile.am + include_HEADERS = scconf.h noinst_HEADERS = internal.h diff --git a/src/scconf/Makefile.mak b/src/scconf/Makefile.mak new file mode 100644 index 00000000..906f411f --- /dev/null +++ b/src/scconf/Makefile.mak @@ -0,0 +1,17 @@ +TOPDIR = ..\.. + +TARGET = scconf.lib +HEADERS = scconf.h +OBJECTS = parse.obj scconf.obj write.obj lex-parse.obj + +.SUFFIXES : .l + +all: $(TARGET) install-headers + +lex-parse.c: lex-parse.l + flex -olex-parse.c < lex-parse.l + +$(TARGET): $(OBJECTS) + lib /nologo /machine:ix86 /out:$(TARGET) $(OBJECTS) + +!INCLUDE $(TOPDIR)\win32\Make.rules.mak diff --git a/src/scconf/parse.c b/src/scconf/parse.c index c492b0f7..8a772f25 100644 --- a/src/scconf/parse.c +++ b/src/scconf/parse.c @@ -25,7 +25,9 @@ #include #include #include +#ifndef _WIN32 #include +#endif #include "scconf.h" #include "internal.h" diff --git a/src/scconf/scconf.c b/src/scconf/scconf.c index e8d9c2a7..83609fb2 100644 --- a/src/scconf/scconf.c +++ b/src/scconf/scconf.c @@ -25,7 +25,11 @@ #include #include #include +#ifndef _WIN32 #include +#else +#define strcasecmp stricmp +#endif #include #include "scconf.h" diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index dbc0b8ac..466c16bb 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -1,6 +1,6 @@ # Process this file with automake to create Makefile.in -MAINTAINERCLEANFILES = Makefile.in +MAINTAINERCLEANFILES = Makefile.in Makefile.mak INCLUDES = @CFLAGS_OPENSC@ LDFLAGS = @LDFLAGS@ @LIBOPENSC@ diff --git a/src/tests/pintest.c b/src/tests/pintest.c index ccf046cf..4f03e0ac 100644 --- a/src/tests/pintest.c +++ b/src/tests/pintest.c @@ -6,7 +6,6 @@ #include #include -#include #include #include #include diff --git a/src/tests/sc-test.c b/src/tests/sc-test.c index 4a5073aa..fe0028b7 100644 --- a/src/tests/sc-test.c +++ b/src/tests/sc-test.c @@ -16,7 +16,7 @@ int sc_test_init(int *argc, char *argv[]) { int i, c; - printf("Using libopensc version %s.\n", sc_version); + printf("Using libopensc version %s.\n", sc_get_version()); i = sc_establish_context(&ctx, "tests"); if (i != SC_SUCCESS) { printf("Failed to establish context: %s\n", sc_strerror(i)); diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am index 194d1e4e..78495b3f 100644 --- a/src/tools/Makefile.am +++ b/src/tools/Makefile.am @@ -2,6 +2,8 @@ MAINTAINERCLEANFILES = Makefile.in +EXTRA_DIST = Makefile.mak + INCLUDES = @CFLAGS_OPENSC@ LDFLAGS = @LDFLAGS@ @LIBOPENSC@ diff --git a/src/tools/Makefile.mak b/src/tools/Makefile.mak new file mode 100644 index 00000000..ad7e9846 --- /dev/null +++ b/src/tools/Makefile.mak @@ -0,0 +1,17 @@ + +TOPDIR = ..\.. + +TARGETS = opensc-explorer.exe opensc-tool.exe \ + pkcs15-tool.exe pkcs15-crypt.exe #pkcs15-init.exe + +all: util.obj $(TARGETS) + +!INCLUDE $(TOPDIR)\win32\Make.rules.mak + +.c.obj: + cl $(COPTS) /c $< + +.c.exe: + cl $(COPTS) /c $< + link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj util.obj ..\common\common.lib ..\libopensc\opensc.lib + diff --git a/src/tools/opensc-explorer.c b/src/tools/opensc-explorer.c index 4739bdde..0cb1d511 100644 --- a/src/tools/opensc-explorer.c +++ b/src/tools/opensc-explorer.c @@ -1169,7 +1169,7 @@ int main(int argc, char * const argv[]) int cargc; char *cargv[20]; - printf("OpenSC Explorer version %s\n", sc_version); + printf("OpenSC Explorer version %s\n", sc_get_version()); while (1) { c = getopt_long(argc, argv, "r:c:d", options, &long_optind); diff --git a/src/tools/opensc-tool.c b/src/tools/opensc-tool.c index 4aefdf85..cef60941 100644 --- a/src/tools/opensc-tool.c +++ b/src/tools/opensc-tool.c @@ -23,7 +23,9 @@ #endif #include #include +#ifndef _WIN32 #include +#endif #include #include #include diff --git a/src/tools/pkcs15-crypt.c b/src/tools/pkcs15-crypt.c index 6137c41a..d581c477 100644 --- a/src/tools/pkcs15-crypt.c +++ b/src/tools/pkcs15-crypt.c @@ -23,7 +23,9 @@ #endif #include #include +#ifndef _WIN32 #include +#endif #include #include #include @@ -128,7 +130,7 @@ int write_output(const u8 *buf, int len) int output_binary = 1; if (opt_output != NULL) { - outf = fopen(opt_output, "w"); + outf = fopen(opt_output, "wb"); if (outf == NULL) { fprintf(stderr, "Unable to open '%s' for writing.\n", opt_output); return -1; diff --git a/src/tools/util.h b/src/tools/util.h index f9eda9d2..a1d84f1c 100644 --- a/src/tools/util.h +++ b/src/tools/util.h @@ -9,11 +9,14 @@ #endif #include #include -#include #include -#include #include #include +#include +#ifndef _WIN32 +#include +#include +#endif #include #include diff --git a/win32/Make.rules.mak b/win32/Make.rules.mak new file mode 100644 index 00000000..d3d8370d --- /dev/null +++ b/win32/Make.rules.mak @@ -0,0 +1,11 @@ +COPTS = /Zi /ML /nologo /DHAVE_CONFIG_H /DVERSION=\"0.7.0\" /I$(TOPDIR)\src\include +LINKFLAGS = /DEBUG /NOLOGO /INCREMENTAL:NO /MACHINE:IX86 + + +install-headers: + @for %i in ( $(HEADERS) ) do \ + @xcopy /d /q /y %i ..\include > nul + +.c.obj:: + cl $(COPTS) /c $< + diff --git a/win32/makedef.pl b/win32/makedef.pl new file mode 100644 index 00000000..92446c1a --- /dev/null +++ b/win32/makedef.pl @@ -0,0 +1,33 @@ +$def = $ARGV[0]; +shift @ARGV; +$lib = $ARGV[0]; +shift @ARGV; +$dumpbin = "dumpbin /symbols @ARGV"; + +open(DUMP, "$dumpbin |") + || die "Can't run `$dumpbin': $!.\n"; + +open(DEF, "> $def") + || die "Can't open `$def': $!.\n"; + +print DEF "LIBRARY $lib\n"; +print DEF "EXPORTS\n"; + +while() +{ + if(!/\bUNDEF\b/ && /\bExternal\b/) + { + s/^.*\|\s+//; + split; + $_ = $_[0]; + + if(!/^\?\?_G/ && !/^\?\?_E/) + { + # Stupid windows linker needs to have + # preceding underscore for ANSI C programs + s/^_//; + + print DEF " $_\n"; + } + } +}