From 7649e582a142bde0810b677c43d870f9482b8dc4 Mon Sep 17 00:00:00 2001 From: frankmorgner Date: Wed, 5 May 2010 14:38:04 +0000 Subject: [PATCH] split up utils into stuff for program and sc handling git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@92 96b47cad-a561-4643-ad3b-153ac7d7599c --- ccid/Makefile | 4 +- ccid/binutil.c | 126 +++++++++++++++++++++++++++++++++++++ ccid/{util.h => binutil.h} | 11 +--- ccid/ccid.c | 2 +- ccid/pace-tool.c | 3 +- ccid/{util.c => scutil.c} | 108 +------------------------------ ccid/scutil.h | 30 +++++++++ ccid/usb.c | 2 +- 8 files changed, 168 insertions(+), 118 deletions(-) create mode 100644 ccid/binutil.c rename ccid/{util.h => binutil.h} (76%) rename ccid/{util.c => scutil.c} (61%) create mode 100644 ccid/scutil.h diff --git a/ccid/Makefile b/ccid/Makefile index 74c9025..7cab1ba 100644 --- a/ccid/Makefile +++ b/ccid/Makefile @@ -20,8 +20,8 @@ INSTALL_PROGRAM = $(INSTALL) TARGETS = ccid -CCID_OBJ = ccid.o usbstring.o usb.o util.o -PTOOL_OBJ = sm.o pace-tool.o util.o pace.o pace_lib.o +CCID_OBJ = ccid.o usbstring.o usb.o binutil.o scutil.o +PTOOL_OBJ = sm.o pace-tool.o binutil.o scutil.o pace.o pace_lib.o ifdef PACE TARGETS += pace-tool diff --git a/ccid/binutil.c b/ccid/binutil.c new file mode 100644 index 0000000..ad3f0e6 --- /dev/null +++ b/ccid/binutil.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 "binutil.h" +#include +#include +#include + +void print_usage(const char *app_name, const struct option options[], + const char *option_help[]) +{ + int i = 0; + printf("Usage: %s [OPTIONS]\nOptions:\n", app_name); + + while (options[i].name) { + char buf[40], tmp[5]; + const char *arg_str; + + /* Skip "hidden" options */ + if (option_help[i] == NULL) { + i++; + continue; + } + + if (options[i].val > 0 && options[i].val < 128) + sprintf(tmp, "-%c", options[i].val); + else + tmp[0] = 0; + switch (options[i].has_arg) { + case 1: + arg_str = " "; + break; + case 2: + arg_str = " [arg]"; + break; + default: + arg_str = ""; + break; + } + sprintf(buf, "--%-13s%s%s", options[i].name, tmp, arg_str); + if (strlen(buf) > 24) { + printf(" %s\n", buf); + buf[0] = '\0'; + } + printf(" %-24s %s\n", buf, option_help[i]); + i++; + } +} + +void parse_error(const char *app_name, const struct option options[], + const char *option_help[], const char *optarg, int opt_ind) +{ + printf("Could not parse %s ('%s').\n", options[opt_ind].name, optarg); + print_usage(app_name , options, option_help); +} + +static int list_readers(sc_context_t *ctx) +{ + unsigned int i, rcount = sc_ctx_get_reader_count(ctx); + + if (rcount == 0) { + printf("No smart card readers found.\n"); + return 0; + } + printf("Readers known about:\n"); + printf("Nr. Driver Name\n"); + for (i = 0; i < rcount; i++) { + sc_reader_t *screader = sc_ctx_get_reader(ctx, i); + printf("%-7d%-11s%s\n", i, screader->driver->short_name, + screader->name); + } + + return 0; +} + +static int list_drivers(sc_context_t *ctx) +{ + int i; + + if (ctx->card_drivers[0] == NULL) { + printf("No card drivers installed!\n"); + return 0; + } + printf("Configured card drivers:\n"); + for (i = 0; ctx->card_drivers[i] != NULL; i++) { + printf(" %-16s %s\n", ctx->card_drivers[i]->short_name, + ctx->card_drivers[i]->name); + } + + return 0; +} + +int print_avail(int verbose) +{ + sc_context_t *ctx = NULL; + + int r; + r = sc_context_create(&ctx, NULL); + if (r) { + fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r)); + return 1; + } + ctx->debug = verbose; + + r = list_readers(ctx)|list_drivers(ctx); + + if (ctx) + sc_release_context(ctx); + + return r; +} diff --git a/ccid/util.h b/ccid/binutil.h similarity index 76% rename from ccid/util.h rename to ccid/binutil.h index 6582b42..c38a270 100644 --- a/ccid/util.h +++ b/ccid/binutil.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Frank Morgner + * Copyright (C) 2010 Frank Morgner * * This file is part of ccid. * @@ -16,11 +16,10 @@ * You should have received a copy of the GNU General Public License along with * ccid. If not, see . */ -#ifndef _CCID_UTIL_H -#define _CCID_UTIL_H +#ifndef _CCID_BINUTIL_H +#define _CCID_BINUTIL_H #include -#include void print_usage(const char *app_name, const struct option options[], const char *option_help[]); @@ -28,9 +27,5 @@ void parse_error(const char *app_name, const struct option options[], const char *option_help[], const char *optarg, int opt_ind); int print_avail(int verbose); -int initialize(int reader_id, const char *cdriver, int verbose, - sc_context_t **ctx, sc_reader_t **reader); - -int build_apdu(sc_context_t *ctx, const u8 *buf, size_t len, sc_apdu_t *apdu); #endif diff --git a/ccid/ccid.c b/ccid/ccid.c index 465b06c..5a371f4 100644 --- a/ccid/ccid.c +++ b/ccid/ccid.c @@ -27,7 +27,7 @@ #include #include "ccid.h" -#include "util.h" +#include "scutil.h" static sc_context_t *ctx = NULL; static sc_card_t *card_in_slot[SC_MAX_SLOTS]; diff --git a/ccid/pace-tool.c b/ccid/pace-tool.c index 86fcc67..64018b5 100644 --- a/ccid/pace-tool.c +++ b/ccid/pace-tool.c @@ -16,8 +16,9 @@ * You should have received a copy of the GNU General Public License along with * ccid. If not, see . */ +#include "binutil.h" #include "pace.h" -#include "util.h" +#include "scutil.h" #include #include #include diff --git a/ccid/util.c b/ccid/scutil.c similarity index 61% rename from ccid/util.c rename to ccid/scutil.c index 7207a02..704c6fd 100644 --- a/ccid/util.c +++ b/ccid/scutil.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Frank Morgner + * Copyright (C) 2010 Frank Morgner * * This file is part of ccid. * @@ -16,59 +16,11 @@ * You should have received a copy of the GNU General Public License along with * ccid. If not, see . */ -#include "util.h" +#include "scutil.h" #include #include #include -void print_usage(const char *app_name, const struct option options[], - const char *option_help[]) -{ - int i = 0; - printf("Usage: %s [OPTIONS]\nOptions:\n", app_name); - - while (options[i].name) { - char buf[40], tmp[5]; - const char *arg_str; - - /* Skip "hidden" options */ - if (option_help[i] == NULL) { - i++; - continue; - } - - if (options[i].val > 0 && options[i].val < 128) - sprintf(tmp, "-%c", options[i].val); - else - tmp[0] = 0; - switch (options[i].has_arg) { - case 1: - arg_str = " "; - break; - case 2: - arg_str = " [arg]"; - break; - default: - arg_str = ""; - break; - } - sprintf(buf, "--%-13s%s%s", options[i].name, tmp, arg_str); - if (strlen(buf) > 24) { - printf(" %s\n", buf); - buf[0] = '\0'; - } - printf(" %-24s %s\n", buf, option_help[i]); - i++; - } -} - -void parse_error(const char *app_name, const struct option options[], - const char *option_help[], const char *optarg, int opt_ind) -{ - printf("Could not parse %s ('%s').\n", options[opt_ind].name, optarg); - print_usage(app_name , options, option_help); -} - int initialize(int reader_id, const char *cdriver, int verbose, sc_context_t **ctx, sc_reader_t **reader) { @@ -122,61 +74,6 @@ int initialize(int reader_id, const char *cdriver, int verbose, SC_FUNC_RETURN((*ctx), SC_LOG_TYPE_ERROR, SC_SUCCESS); } -static int list_readers(sc_context_t *ctx) -{ - unsigned int i, rcount = sc_ctx_get_reader_count(ctx); - - if (rcount == 0) { - printf("No smart card readers found.\n"); - return 0; - } - printf("Readers known about:\n"); - printf("Nr. Driver Name\n"); - for (i = 0; i < rcount; i++) { - sc_reader_t *screader = sc_ctx_get_reader(ctx, i); - printf("%-7d%-11s%s\n", i, screader->driver->short_name, - screader->name); - } - - return 0; -} - -static int list_drivers(sc_context_t *ctx) -{ - int i; - - if (ctx->card_drivers[0] == NULL) { - printf("No card drivers installed!\n"); - return 0; - } - printf("Configured card drivers:\n"); - for (i = 0; ctx->card_drivers[i] != NULL; i++) { - printf(" %-16s %s\n", ctx->card_drivers[i]->short_name, - ctx->card_drivers[i]->name); - } - - return 0; -} - -int print_avail(int verbose) -{ - sc_context_t *ctx = NULL; - - int r; - r = sc_context_create(&ctx, NULL); - if (r) { - fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r)); - return 1; - } - ctx->debug = verbose; - - r = list_readers(ctx)|list_drivers(ctx); - - if (ctx) - sc_release_context(ctx); - - return r; -} int build_apdu(sc_context_t *ctx, const u8 *buf, size_t len, sc_apdu_t *apdu) { @@ -242,3 +139,4 @@ int build_apdu(sc_context_t *ctx, const u8 *buf, size_t len, sc_apdu_t *apdu) SC_FUNC_RETURN(ctx, SC_LOG_TYPE_VERBOSE, SC_SUCCESS); } + diff --git a/ccid/scutil.h b/ccid/scutil.h new file mode 100644 index 0000000..6734912 --- /dev/null +++ b/ccid/scutil.h @@ -0,0 +1,30 @@ +/* + * 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 _CCID_SCUTIL_H +#define _CCID_SCUTIL_H + +#include + +int initialize(int reader_id, const char *cdriver, int verbose, + sc_context_t **ctx, sc_reader_t **reader); + +int build_apdu(sc_context_t *ctx, const u8 *buf, size_t len, sc_apdu_t *apdu); + +#endif + diff --git a/ccid/usb.c b/ccid/usb.c index 14691cc..b06507e 100644 --- a/ccid/usb.c +++ b/ccid/usb.c @@ -46,7 +46,7 @@ #include "usbstring.h" #include "ccid.h" -#include "util.h" +#include "binutil.h" #define DRIVER_VENDOR_NUM 0x0D46 /* KOBIL Systems */ #define DRIVER_PRODUCT_NUM 0x3010 /* KOBIL Class 3 Reader */