changeset 6:2c72709e0891

EF_DIR dump meaty function factored out into libcommon
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 11 Feb 2021 23:42:53 +0000
parents ce189c97b7b1
children 4360a7906f34
files libcommon/Makefile libcommon/dumpdirfunc.c simtool/dumpdir.c
diffstat 3 files changed, 104 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- a/libcommon/Makefile	Thu Feb 11 23:31:51 2021 +0000
+++ b/libcommon/Makefile	Thu Feb 11 23:42:53 2021 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2 -I/usr/include/PCSC
 OBJS=	alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o chkblank.o \
-	exit.o hexdump.o hexread.o hexstr.o names.o pinentry.o
+	dumpdirfunc.o exit.o hexdump.o hexread.o hexstr.o names.o pinentry.o
 LIB=	libcommon.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libcommon/dumpdirfunc.c	Thu Feb 11 23:42:53 2021 +0000
@@ -0,0 +1,101 @@
+/*
+ * This module implements the common function for dumping EF_DIR.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include "simresp.h"
+
+static void
+dump_aid(tlv)
+	u_char *tlv;
+{
+	unsigned reclen, n;
+
+	reclen = tlv[1];
+	printf(" AID:");
+	for (n = 0; n < reclen; n++)
+		printf(" %02X", tlv[n+2]);
+	putchar('\n');
+}
+
+static void
+dump_label(tlv)
+	u_char *tlv;
+{
+	int rc;
+	unsigned textlen;
+
+	printf(" Label: ");
+	rc = validate_alpha_field(tlv + 2, tlv[1], &textlen);
+	if (rc < 0) {
+		printf("malformed\n");
+		return;
+	}
+	print_alpha_field(tlv + 2, textlen, stdout);
+	putchar('\n');
+}
+
+static void
+dump_unknown_tlv(tlv)
+	u_char *tlv;
+{
+	unsigned reclen, n;
+
+	reclen = tlv[1] + 2;
+	printf(" TLV:");
+	for (n = 0; n < reclen; n++)
+		printf(" %02X", tlv[n]);
+	putchar('\n');
+}
+
+void
+dump_efdir_record()
+{
+	unsigned totlen, reclen;
+	u_char *dp, *endp;
+
+	if (sim_resp_data[0] != 0x61) {
+		printf(" bad: first byte != 0x61\n");
+		return;
+	}
+	totlen = sim_resp_data[1];
+	if (totlen < 3 || totlen > 0x7F) {
+		printf(" bad: global length byte 0x%02X is invalid\n", totlen);
+		return;
+	}
+	if (totlen + 2 > sim_resp_data_len) {
+		printf(" bad: TLV global length exceeds EF record length\n");
+		return;
+	}
+	dp = sim_resp_data + 2;
+	endp = sim_resp_data + 2 + totlen;
+	while (dp < endp) {
+		if (endp - dp < 2) {
+trunc_error:		printf(" bad: truncated TLV record\n");
+			return;
+		}
+		if ((dp[0] & 0x1F) == 0x1F) {
+			printf(" bad: extended tag not supported\n");
+			return;
+		}
+		if (dp[1] & 0x80) {
+			printf(" bad: extended length not supported\n");
+			return;
+		}
+		reclen = dp[1] + 2;
+		if (endp - dp < reclen)
+			goto trunc_error;
+		switch (dp[0]) {
+		case 0x4F:
+			dump_aid(dp);
+			break;
+		case 0x50:
+			dump_label(dp);
+			break;
+		default:
+			dump_unknown_tlv(dp);
+		}
+		dp += reclen;
+	}
+}
--- a/simtool/dumpdir.c	Thu Feb 11 23:31:51 2021 +0000
+++ b/simtool/dumpdir.c	Thu Feb 11 23:42:53 2021 +0000
@@ -4,107 +4,9 @@
 
 #include <sys/types.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include "simresp.h"
 #include "curfile.h"
 #include "file_id.h"
 
-static void
-dump_aid(tlv)
-	u_char *tlv;
-{
-	unsigned reclen, n;
-
-	reclen = tlv[1];
-	printf(" AID:");
-	for (n = 0; n < reclen; n++)
-		printf(" %02X", tlv[n+2]);
-	putchar('\n');
-}
-
-static void
-dump_label(tlv)
-	u_char *tlv;
-{
-	int rc;
-	unsigned textlen;
-
-	printf(" Label: ");
-	rc = validate_alpha_field(tlv + 2, tlv[1], &textlen);
-	if (rc < 0) {
-		printf("malformed\n");
-		return;
-	}
-	print_alpha_field(tlv + 2, textlen, stdout);
-	putchar('\n');
-}
-
-static void
-dump_unknown_tlv(tlv)
-	u_char *tlv;
-{
-	unsigned reclen, n;
-
-	reclen = tlv[1] + 2;
-	printf(" TLV:");
-	for (n = 0; n < reclen; n++)
-		printf(" %02X", tlv[n]);
-	putchar('\n');
-}
-
-static void
-dump_record(recno)
-	unsigned recno;
-{
-	unsigned totlen, reclen;
-	u_char *dp, *endp;
-
-	printf("Record #%u:\n", recno);
-	if (sim_resp_data[0] != 0x61) {
-		printf(" bad: first byte != 0x61\n");
-		return;
-	}
-	totlen = sim_resp_data[1];
-	if (totlen < 3 || totlen > 0x7F) {
-		printf(" bad: global length byte 0x%02X is invalid\n", totlen);
-		return;
-	}
-	if (totlen + 2 > sim_resp_data_len) {
-		printf(" bad: TLV global length exceeds EF record length\n");
-		return;
-	}
-	dp = sim_resp_data + 2;
-	endp = sim_resp_data + 2 + totlen;
-	while (dp < endp) {
-		if (endp - dp < 2) {
-trunc_error:		printf(" bad: truncated TLV record\n");
-			return;
-		}
-		if ((dp[0] & 0x1F) == 0x1F) {
-			printf(" bad: extended tag not supported\n");
-			return;
-		}
-		if (dp[1] & 0x80) {
-			printf(" bad: extended length not supported\n");
-			return;
-		}
-		reclen = dp[1] + 2;
-		if (endp - dp < reclen)
-			goto trunc_error;
-		switch (dp[0]) {
-		case 0x4F:
-			dump_aid(dp);
-			break;
-		case 0x50:
-			dump_label(dp);
-			break;
-		default:
-			dump_unknown_tlv(dp);
-		}
-		dp += reclen;
-	}
-}
-
 cmd_uicc_dir()
 {
 	int rc;
@@ -133,7 +35,8 @@
 			return(rc);
 		if (check_simresp_all_blank())
 			continue;
-		dump_record(recno);
+		printf("Record #%u:\n", recno);
+		dump_efdir_record();
 	}
 	return(0);
 }