changeset 56:966a54303d68

simsniff-dec: factor out high-level decoding
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 04 Oct 2023 01:18:50 +0000
parents 5268246520de
children eb4274e7f4da
files sw/sniff-dec/Makefile sw/sniff-dec/command.c sw/sniff-dec/hl_decode.c
diffstat 3 files changed, 54 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/sw/sniff-dec/Makefile	Wed Oct 04 00:20:05 2023 +0000
+++ b/sw/sniff-dec/Makefile	Wed Oct 04 01:18:50 2023 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	simsniff-dec
-OBJS=	atr.o command.o dispatch.o invtable.o main.o parity.o pps.o
+OBJS=	atr.o command.o dispatch.o hl_decode.o invtable.o main.o parity.o pps.o
 
 INSTALL_PREFIX=	/opt/freecalypso
 
--- a/sw/sniff-dec/command.c	Wed Oct 04 00:20:05 2023 +0000
+++ b/sw/sniff-dec/command.c	Wed Oct 04 01:18:50 2023 +0000
@@ -37,84 +37,6 @@
 }
 
 static void
-decode_cmd_opcode()
-{
-	char *cmdname;
-
-	switch (cmd_hdr[1]) {
-	case 0xA4:
-		cmdname = "SELECT";
-		break;
-	case 0xF2:
-		cmdname = "STATUS";
-		break;
-	case 0xB0:
-		cmdname = "READ BINARY";
-		break;
-	case 0xD6:
-		cmdname = "UPDATE BINARY";
-		break;
-	case 0xB2:
-		cmdname = "READ RECORD";
-		break;
-	case 0xDC:
-		cmdname = "UPDATE RECORD";
-		break;
-	case 0xA2:
-		cmdname = "SEEK";
-		break;
-	case 0x32:
-		cmdname = "INCREASE";
-		break;
-	case 0x20:
-		cmdname = "VERIFY PIN";
-		break;
-	case 0x24:
-		cmdname = "CHANGE PIN";
-		break;
-	case 0x26:
-		cmdname = "DISABLE PIN";
-		break;
-	case 0x28:
-		cmdname = "ENABLE PIN";
-		break;
-	case 0x2C:
-		cmdname = "UNBLOCK PIN";
-		break;
-	case 0x04:
-		cmdname = "INVALIDATE";
-		break;
-	case 0x44:
-		cmdname = "REHABILITATE";
-		break;
-	case 0x88:
-		cmdname = "RUN GSM ALGO";
-		break;
-	case 0xFA:
-		cmdname = "SLEEP";
-		break;
-	case 0xC0:
-		cmdname = "GET RESPONSE";
-		break;
-	case 0x10:
-		cmdname = "TERMINAL PROFILE";
-		break;
-	case 0xC2:
-		cmdname = "ENVELOPE";
-		break;
-	case 0x12:
-		cmdname = "FETCH";
-		break;
-	case 0x14:
-		cmdname = "TERMINAL RESPONSE";
-		break;
-	default:
-		cmdname = "Unknown!";
-	}
-	printf(" Command: %s\n", cmdname);
-}
-
-static void
 print_cmd_hdr()
 {
 	unsigned n;
@@ -137,7 +59,7 @@
 		state = STATE_ERROR;
 		return;
 	}
-	decode_cmd_opcode();
+	decode_cmd_opcode(cmd_hdr[1]);
 	if (cmd_hdr[4])
 		data_total = cmd_hdr[4];
 	else
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sw/sniff-dec/hl_decode.c	Wed Oct 04 01:18:50 2023 +0000
@@ -0,0 +1,52 @@
+/*
+ * Here we implement higher-level decoding of INS codes and SELECT file IDs.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+static struct ins_code_tab {
+	u_char	ins_code;
+	char	*cmdname;
+} ins_code_table[] = {
+	{0xA4, "SELECT"},
+	{0xF2, "STATUS"},
+	{0xB0, "READ BINARY"},
+	{0xD6, "UPDATE BINARY"},
+	{0xB2, "READ RECORD"},
+	{0xDC, "UPDATE RECORD"},
+	{0xA2, "SEEK"},
+	{0x32, "INCREASE"},
+	{0x20, "VERIFY PIN"},
+	{0x24, "CHANGE PIN"},
+	{0x26, "DISABLE PIN"},
+	{0x28, "ENABLE PIN"},
+	{0x2C, "UNBLOCK PIN"},
+	{0x04, "INVALIDATE"},
+	{0x44, "REHABILITATE"},
+	{0x88, "RUN GSM ALGO"},
+	{0xFA, "SLEEP"},
+	{0xC0, "GET RESPONSE"},
+	{0x10, "TERMINAL PROFILE"},
+	{0xC2, "ENVELOPE"},
+	{0x12, "FETCH"},
+	{0x14, "TERMINAL RESPONSE"},
+	/* table search terminator */
+	{0,    0}
+};
+
+void
+decode_cmd_opcode(ins)
+	u_char ins;
+{
+	struct ins_code_tab *tp;
+
+	for (tp = ins_code_table; tp->cmdname; tp++)
+		if (tp->ins_code == ins)
+			break;
+	if (tp->cmdname)
+		printf(" Command: %s\n", tp->cmdname);
+}