diff uicc/readef.c @ 89:db131929ee96

fc-uicc-tool: extended readef ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 04:17:58 +0000
parents simtool/readef.c@49b7e02787c1
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/readef.c	Sun Apr 11 04:17:58 2021 +0000
@@ -0,0 +1,122 @@
+/*
+ * This module implements the readef command for dumping the complete
+ * content of SIM files.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "simresp.h"
+#include "efstruct.h"
+
+static void
+hexdump_with_offset(outf, extoff)
+	FILE *outf;
+	unsigned extoff;
+{
+	unsigned off, cc, n, c;
+
+	for (off = 0; off < sim_resp_data_len; off += cc) {
+		fprintf(outf, "%04X:", extoff + off);
+		cc = 16;
+		if (sim_resp_data_len - off < cc)
+			cc = sim_resp_data_len - off;
+		for (n = 0; n < 16; n++) {
+			if (n == 0 || n == 8)
+				putc(' ', outf);
+			putc(' ', outf);
+			if (n < cc)
+				fprintf(outf, "%02X", sim_resp_data[off + n]);
+			else {
+				putc(' ', outf);
+				putc(' ', outf);
+			}
+		}
+		putc(' ', outf);
+		putc(' ', outf);
+		for (n = 0; n < cc; n++) {
+			c = sim_resp_data[off + n];
+			if (c < 0x20 || c > 0x7E)
+				c = '.';
+			putc(c, outf);
+		}
+		putc('\n', outf);
+	}
+}
+
+static
+readef_transparent(efs, outf)
+	struct ef_struct *efs;
+	FILE *outf;
+{
+	unsigned off, cc;
+	int rc;
+
+	for (off = 0; off < efs->total_size; off += cc) {
+		cc = efs->total_size - off;
+		if (cc > 256)
+			cc = 256;
+		rc = readbin_op(off, cc);
+		if (rc < 0)
+			return(rc);
+		hexdump_with_offset(outf, off);
+	}
+	return(0);
+}
+
+static
+readef_records(efs, outf)
+	struct ef_struct *efs;
+	FILE *outf;
+{
+	unsigned recno;
+	int rc;
+
+	for (recno = 1; recno <= efs->record_count; recno++) {
+		fprintf(outf, "Record #%u:\n", recno);
+		rc = readrec_op(recno, 0x04, efs->record_len);
+		if (rc < 0)
+			return(rc);
+		display_sim_resp_in_hex(outf);
+	}
+	return(0);
+}
+
+cmd_readef(argc, argv, outf)
+	char **argv;
+	FILE *outf;
+{
+	int file_id, rc;
+	struct ef_struct efs;
+
+	if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
+	    isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
+		file_id = strtoul(argv[1], 0, 16);
+	else
+		file_id = find_symbolic_file_name(argv[1]);
+	if (file_id < 0) {
+		fprintf(stderr,
+"error: file ID argument is not a hex value or a recognized symbolic name\n");
+		return(-1);
+	}
+	rc = select_op(file_id);
+	if (rc < 0)
+		return(rc);
+	rc = select_resp_get_ef_struct(&efs);
+	if (rc < 0)
+		return(rc);
+	switch (efs.structure) {
+	case 0x01:
+		fprintf(outf, "Transparent EF of %u byte(s)\n", efs.total_size);
+		return readef_transparent(&efs, outf);
+	case 0x02:
+		fprintf(outf, "%u records of %u bytes (linear fixed)\n",
+			efs.record_count, efs.record_len);
+		return readef_records(&efs, outf);
+	case 0x06:
+		fprintf(outf, "%u records of %u bytes (cyclic)\n",
+			efs.record_count, efs.record_len);
+		return readef_records(&efs, outf);
+	}
+}