Alon Levy 4d1515a
From 703a72f26668f26f2bb98851f1a2edd9a8681f86 Mon Sep 17 00:00:00 2001
Alon Levy 4d1515a
From: Alon Levy <alevy@redhat.com>
Alon Levy 4d1515a
Date: Tue, 5 Mar 2013 16:27:43 +0200
Alon Levy 4d1515a
Subject: [PATCH 237/241] libcacard: move atr setting from macro to function
Alon Levy 4d1515a
MIME-Version: 1.0
Alon Levy 4d1515a
Content-Type: text/plain; charset=UTF-8
Alon Levy 4d1515a
Content-Transfer-Encoding: 8bit
Alon Levy 4d1515a
Alon Levy 4d1515a
Only because qemu's checkpatch complains about it.
Alon Levy 4d1515a
Alon Levy 4d1515a
Signed-off-by: Alon Levy <alevy@redhat.com>
Alon Levy 4d1515a
Reviewed-by: Marc-André Lureau <mlureau@redhat.com>
Alon Levy 4d1515a
(cherry picked from commit 0b6a16c1a47b622b1a692ab179013d9e30e9cf3b)
Alon Levy 4d1515a
---
Alon Levy 4d1515a
 Makefile.objs               |  1 +
Alon Levy 4d1515a
 libcacard/vcard_emul_nss.c  | 14 +++++++++++---
Alon Levy 4d1515a
 libcacard/vcardt.c          | 40 ++++++++++++++++++++++++++++++++++++++++
Alon Levy 4d1515a
 libcacard/vcardt.h          | 13 -------------
Alon Levy 4d1515a
 libcacard/vcardt_internal.h |  6 ++++++
Alon Levy 4d1515a
 5 files changed, 58 insertions(+), 16 deletions(-)
Alon Levy 4d1515a
 create mode 100644 libcacard/vcardt.c
Alon Levy 4d1515a
 create mode 100644 libcacard/vcardt_internal.h
Alon Levy 4d1515a
Alon Levy 4d1515a
diff --git a/Makefile.objs b/Makefile.objs
Alon Levy 4d1515a
index 3884790..729be2b 100644
Alon Levy 4d1515a
--- a/Makefile.objs
Alon Levy 4d1515a
+++ b/Makefile.objs
Alon Levy 4d1515a
@@ -32,6 +32,7 @@ libcacard-y += libcacard/vcard.o libcacard/vreader.o
Alon Levy 4d1515a
 libcacard-y += libcacard/vcard_emul_nss.o
Alon Levy 4d1515a
 libcacard-y += libcacard/vcard_emul_type.o
Alon Levy 4d1515a
 libcacard-y += libcacard/card_7816.o
Alon Levy 4d1515a
+libcacard-y += libcacard/vcardt.o
Alon Levy 4d1515a
 
Alon Levy 4d1515a
 ######################################################################
Alon Levy 4d1515a
 # Target independent part of system emulation. The long term path is to
Alon Levy 4d1515a
diff --git a/libcacard/vcard_emul_nss.c b/libcacard/vcard_emul_nss.c
Alon Levy 4d1515a
index 9ba80fb..1a3e568 100644
Alon Levy 4d1515a
--- a/libcacard/vcard_emul_nss.c
Alon Levy 4d1515a
+++ b/libcacard/vcard_emul_nss.c
Alon Levy 4d1515a
@@ -33,6 +33,9 @@
Alon Levy 4d1515a
 #include "vreader.h"
Alon Levy 4d1515a
 #include "vevent.h"
Alon Levy 4d1515a
 
Alon Levy 4d1515a
+#include "libcacard/vcardt_internal.h"
Alon Levy 4d1515a
+
Alon Levy 4d1515a
+
Alon Levy 4d1515a
 typedef enum {
Alon Levy 4d1515a
     VCardEmulUnknown = -1,
Alon Levy 4d1515a
     VCardEmulFalse = 0,
Alon Levy 4d1515a
@@ -519,18 +522,23 @@ vcard_emul_reader_get_slot(VReader *vreader)
Alon Levy 4d1515a
 }
Alon Levy 4d1515a
 
Alon Levy 4d1515a
 /*
Alon Levy 4d1515a
- *  Card ATR's map to physical cards. VCARD_ATR_PREFIX will set appropriate
Alon Levy 4d1515a
+ *  Card ATR's map to physical cards. vcard_alloc_atr will set appropriate
Alon Levy 4d1515a
  *  historical bytes for any software emulated card. The remaining bytes can be
Alon Levy 4d1515a
  *  used to indicate the actual emulator
Alon Levy 4d1515a
  */
Alon Levy 4d1515a
-static const unsigned char nss_atr[] = { VCARD_ATR_PREFIX(3), 'N', 'S', 'S' };
Alon Levy 4d1515a
+static unsigned char *nss_atr;
Alon Levy 4d1515a
+static int nss_atr_len;
Alon Levy 4d1515a
 
Alon Levy 4d1515a
 void
Alon Levy 4d1515a
 vcard_emul_get_atr(VCard *card, unsigned char *atr, int *atr_len)
Alon Levy 4d1515a
 {
Alon Levy 4d1515a
-    int len = MIN(sizeof(nss_atr), *atr_len);
Alon Levy 4d1515a
+    int len;
Alon Levy 4d1515a
     assert(atr != NULL);
Alon Levy 4d1515a
 
Alon Levy 4d1515a
+    if (nss_atr == NULL) {
Alon Levy 4d1515a
+        nss_atr = vcard_alloc_atr("NSS", &nss_atr_len);
Alon Levy 4d1515a
+    }
Alon Levy 4d1515a
+    len = MIN(nss_atr_len, *atr_len);
Alon Levy 4d1515a
     memcpy(atr, nss_atr, len);
Alon Levy 4d1515a
     *atr_len = len;
Alon Levy 4d1515a
 }
Alon Levy 4d1515a
diff --git a/libcacard/vcardt.c b/libcacard/vcardt.c
Alon Levy 4d1515a
new file mode 100644
Alon Levy 4d1515a
index 0000000..9ce4648
Alon Levy 4d1515a
--- /dev/null
Alon Levy 4d1515a
+++ b/libcacard/vcardt.c
Alon Levy 4d1515a
@@ -0,0 +1,40 @@
Alon Levy 4d1515a
+#include <stdlib.h>
Alon Levy 4d1515a
+#include <string.h>
Alon Levy 4d1515a
+#include <glib.h>
Alon Levy 4d1515a
+
Alon Levy 4d1515a
+#include "libcacard/vcardt.h"
Alon Levy 4d1515a
+
Alon Levy 4d1515a
+#include "libcacard/vcardt_internal.h"
Alon Levy 4d1515a
+
Alon Levy 4d1515a
+/* create an ATR with appropriate historical bytes */
Alon Levy 4d1515a
+#define ATR_TS_DIRECT_CONVENTION 0x3b
Alon Levy 4d1515a
+#define ATR_TA_PRESENT 0x10
Alon Levy 4d1515a
+#define ATR_TB_PRESENT 0x20
Alon Levy 4d1515a
+#define ATR_TC_PRESENT 0x40
Alon Levy 4d1515a
+#define ATR_TD_PRESENT 0x80
Alon Levy 4d1515a
+
Alon Levy 4d1515a
+unsigned char *vcard_alloc_atr(const char *postfix, int *atr_len)
Alon Levy 4d1515a
+{
Alon Levy 4d1515a
+    int postfix_len;
Alon Levy 4d1515a
+    const char prefix[] = "VCARD_";
Alon Levy 4d1515a
+    const char default_postfix[] = "DEFAULT";
Alon Levy 4d1515a
+    const int prefix_len = sizeof(prefix) - 1;
Alon Levy 4d1515a
+    int total_len;
Alon Levy 4d1515a
+    unsigned char *atr;
Alon Levy 4d1515a
+
Alon Levy 4d1515a
+    if (postfix == NULL) {
Alon Levy 4d1515a
+        postfix = default_postfix;
Alon Levy 4d1515a
+    }
Alon Levy 4d1515a
+    postfix_len = strlen(postfix);
Alon Levy 4d1515a
+    total_len = 3 + prefix_len + postfix_len;
Alon Levy 4d1515a
+    atr = g_malloc(total_len);
Alon Levy 4d1515a
+    atr[0] = ATR_TS_DIRECT_CONVENTION;
Alon Levy 4d1515a
+    atr[1] = ATR_TD_PRESENT + prefix_len + postfix_len;
Alon Levy 4d1515a
+    atr[2] = 0x00;
Alon Levy 4d1515a
+    memcpy(&atr[3], prefix, prefix_len);
Alon Levy 4d1515a
+    memcpy(&atr[3 + prefix_len], postfix, postfix_len);
Alon Levy 4d1515a
+    if (atr_len) {
Alon Levy 4d1515a
+        *atr_len = total_len;
Alon Levy 4d1515a
+    }
Alon Levy 4d1515a
+    return atr;
Alon Levy 4d1515a
+}
Alon Levy 4d1515a
diff --git a/libcacard/vcardt.h b/libcacard/vcardt.h
Alon Levy 4d1515a
index 3b9a619..795e265 100644
Alon Levy 4d1515a
--- a/libcacard/vcardt.h
Alon Levy 4d1515a
+++ b/libcacard/vcardt.h
Alon Levy 4d1515a
@@ -25,19 +25,6 @@ typedef struct VCardEmulStruct VCardEmul;
Alon Levy 4d1515a
 
Alon Levy 4d1515a
 #define MAX_CHANNEL 4
Alon Levy 4d1515a
 
Alon Levy 4d1515a
-/* create an ATR with appropriate historical bytes */
Alon Levy 4d1515a
-#define TS_DIRECT_CONVENTION 0x3b
Alon Levy 4d1515a
-#define TA_PRESENT 0x10
Alon Levy 4d1515a
-#define TB_PRESENT 0x20
Alon Levy 4d1515a
-#define TC_PRESENT 0x40
Alon Levy 4d1515a
-#define TD_PRESENT 0x80
Alon Levy 4d1515a
-
Alon Levy 4d1515a
-#define VCARD_ATR_PREFIX(size) \
Alon Levy 4d1515a
-    TS_DIRECT_CONVENTION, \
Alon Levy 4d1515a
-    TD_PRESENT + (6 + size), \
Alon Levy 4d1515a
-    0x00, \
Alon Levy 4d1515a
-    'V', 'C', 'A', 'R', 'D', '_'
Alon Levy 4d1515a
-
Alon Levy 4d1515a
 typedef enum {
Alon Levy 4d1515a
     VCARD_DONE,
Alon Levy 4d1515a
     VCARD_NEXT,
Alon Levy 4d1515a
diff --git a/libcacard/vcardt_internal.h b/libcacard/vcardt_internal.h
Alon Levy 4d1515a
new file mode 100644
Alon Levy 4d1515a
index 0000000..e5c8d2d
Alon Levy 4d1515a
--- /dev/null
Alon Levy 4d1515a
+++ b/libcacard/vcardt_internal.h
Alon Levy 4d1515a
@@ -0,0 +1,6 @@
Alon Levy 4d1515a
+#ifndef VCARDT_INTERNAL_H
Alon Levy 4d1515a
+#define VCARDT_INTERNAL_H
Alon Levy 4d1515a
+
Alon Levy 4d1515a
+unsigned char *vcard_alloc_atr(const char *postfix, int *atr_len);
Alon Levy 4d1515a
+
Alon Levy 4d1515a
+#endif
Alon Levy 4d1515a
-- 
Alon Levy 4d1515a
1.8.3.1
Alon Levy 4d1515a