parse options, specify ykneo slot
authorEugene Crosser <crosser@average.org>
Sun, 20 Oct 2013 20:22:07 +0000 (00:22 +0400)
committerEugene Crosser <crosser@average.org>
Sun, 20 Oct 2013 20:22:07 +0000 (00:22 +0400)
Makefile
pcsc_cr.c
pcsc_cr.h
test_cr.c
token.h
ykneo.c

index db68800130e5c2540076280a69be41eaa5fbbb4d..80c9948c959f444cd3a77f7b14301b272ea319c1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,3 +4,7 @@ CFLAGS += -g $(shell pkg-config --cflags libpcsclite)
 LDLIBS += $(shell pkg-config --libs libpcsclite)
 
 test_cr: test_cr.o pcsc_cr.o ykneo.o
+
+test_cr.o: pcsc_cr.h
+pcsc_cr.o: pcsc_cr.h token.h
+ykneo.o: token.h
index 0354c32023e7ddfeb58fe1265bf1f8a462c9746a..0559212e1e6946f480623ebb6a0162b82a7cb808 100644 (file)
--- a/pcsc_cr.c
+++ b/pcsc_cr.c
@@ -1,4 +1,6 @@
+#include <stdio.h>
 #include <string.h>
+#include <alloca.h>
 #include "token.h"
 #include <reader.h>
 
@@ -97,3 +99,26 @@ free_out:
 char *pcsc_errstr(long err) {
        return pcsc_stringify_error(err);
 }
+
+int pcsc_option(char *option)
+{
+       char *name, *key, *val;
+       int i, rc = -1;
+       struct token_interface *type;
+
+       name=(char *)alloca(strlen(option)+1);
+       strcpy(name, option);
+       if ((key = strchr(name, ':'))) *(key++) = '\0';
+       else return -1;
+       if ((val = strchr(key, '='))) *(val++) = '\0';
+       else return -1;
+       if (*val == '\0') return -1;
+       for (i = 0; types[i]; i++) {
+               type = types[i];
+               if (!strcmp(type->name,name)) {
+                       rc = type->parse_option(key, val);
+                       break;
+               }
+       }
+       return rc;
+}
index da9733350e3ea77137ffaf75cc7b8a4faa545dce..35be3a41c7947441128f7c804e67372bd0c09099 100644 (file)
--- a/pcsc_cr.h
+++ b/pcsc_cr.h
@@ -1,6 +1,7 @@
 #ifndef _PCSC_CR_H
 #define _PCSC_CR_H
 
+int pcsc_option(char *option);
 long pcsc_cr(unsigned char *chal, int csize, unsigned char *resp, int *rsize);
 char *pcsc_errstr(long err);
 
index 2d66d5fd11ca29162d1abc7e9f20698d7285720d..987df3f50860357633a8e940a578c15809fa8cc5 100644 (file)
--- a/test_cr.c
+++ b/test_cr.c
@@ -1,15 +1,39 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
 #include "pcsc_cr.h"
 
 char chal[] = {
 0x0f,0x65,0xd1,0x3a,0xfe,0xcb,0xc4,0xb9,0x52,0xb1,0x60,0xcf,0xe8,0x55,0x6a,0xdd,0xfb,0xef,0xf6,0x55,0x83,0x4c,0x8d,0xea,0x38,0xea,0x3b,0x26,0xf7,0x0a,0xe8,0x0d,0x31,0x38,0xee,0x16,0x5d,0xab,0x8b,0x7f,0xf0,0x1b,0xe3,0xbe,0xd8,0x4b,0x6e,0x44,0x42,0x8d,0x0f,0xc1,0x3b,0x23,0xea,0xfe,0xc0,0x68,0xc1,0x0f,0x60,0x6c,0xf4};
 
+static void usage(const char const *cmd)
+{
+       fprintf(stderr, "usage: %s [-o backend:name=value] ...\n", cmd);
+}
+
 int main(int argc, char *argv[])
 {
        unsigned char rbuf[20];
        int rsize = sizeof(rbuf);
        int i;
        long rc;
+       int c;
+
+       while ((c = getopt(argc, argv, "ho:")) != -1) switch (c) {
+       case 'h':
+               usage(argv[0]);
+               exit(0);
+       case 'o':
+               if (pcsc_option(optarg)) {
+                       fprintf(stderr, "Option \"%s\" bad\n", optarg);
+                       exit(1);
+               }
+               break;
+       default:
+               usage(argv[0]);
+               exit(1);
+       }
 
        memset(rbuf, 0xFE, sizeof(rbuf));
        rc = pcsc_cr(chal, sizeof(chal), rbuf, &rsize);
diff --git a/token.h b/token.h
index 743aafbb672a81a14f3cd8545063f8fd8e74d073..3af9ddb54aef5b790831c05444316ae9337e75d8 100644 (file)
--- a/token.h
+++ b/token.h
@@ -6,6 +6,8 @@
 extern SCARD_IO_REQUEST pioSendPci;
 
 struct token_interface {
+       char *name;
+       int (*parse_option)(char *key, char *val);
        DWORD (*check_atr_hb)(LPTSTR str, DWORD size);
        DWORD (*prologue)(SCARDHANDLE hCard,LPTSTR envp[]);
        DWORD (*trancieve)(SCARDHANDLE hCard,LPTSTR envp[],
diff --git a/ykneo.c b/ykneo.c
index 3399f711a0c8323fbf71c597c05af6763fd3d492..79c86e5302ec3176ac5172f9752171a33b85ca33 100644 (file)
--- a/ykneo.c
+++ b/ykneo.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <string.h>
 #include <alloca.h>
 
@@ -7,7 +8,27 @@
 
 static const BYTE selcmd[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0,
                                0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
-static const BYTE cr_cmd[] = {0x00, 0x01, 0x38, 0x00};
+static const BYTE cr_cmd[] = {0x00, 0x01, 0xff, 0x00};
+
+static BYTE cr_for_slot[3] = {0xff, 0x30, 0x38};
+
+static int slot;
+
+static int ykn_parse_option(char *key, char *val)
+{
+       if (!strcmp(key, "slot")) {
+               if (!strcmp(val, "1")) {
+                       slot = 1;
+               } else if (!strcmp(val, "2")) {
+                       slot = 2;
+               } else {
+                       return -1;
+               }
+       } else {
+               return -1;
+       }
+       return 0;
+}
 
 static DWORD ykn_check_atr_hb(LPTSTR str, DWORD size)
 {
@@ -39,6 +60,7 @@ static DWORD ykn_trancieve(SCARDHANDLE hCard,LPTSTR envp[],
        BYTE *rbuf = alloca(rsize);
        BYTE *sbuf = alloca(sendsize + 6);
        memcpy(sbuf, cr_cmd, sizeof(cr_cmd));
+       sbuf[2] = cr_for_slot[slot];
        sbuf[sizeof(cr_cmd)] = sendsize;
        memcpy(sbuf + sizeof(cr_cmd) + 1, send, sendsize);
        sbuf[sendsize + 5] = rsize;
@@ -58,6 +80,8 @@ static DWORD ykn_epilogue(SCARDHANDLE hCard,LPTSTR envp[])
 }
 
 struct token_interface ykneo_interface = {
+       .name           = "ykneo",
+       .parse_option   = ykn_parse_option,
        .check_atr_hb   = ykn_check_atr_hb,
        .prologue       = ykn_prologue,
        .trancieve      = ykn_trancieve,