changeset 90:3afa61d98961

fc-uicc-tool: savebin ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 04:32:26 +0000
parents db131929ee96
children abef3d5668b9
files uicc/Makefile uicc/cmdtab.c uicc/savebin.c
diffstat 3 files changed, 95 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/uicc/Makefile	Sun Apr 11 04:17:58 2021 +0000
+++ b/uicc/Makefile	Sun Apr 11 04:32:26 2021 +0000
@@ -3,7 +3,8 @@
 CPPFLAGS=-I../libcommon
 PROG=	fc-uicc-tool
 OBJS=	bfsearch.o cmdtab.o createfile.o dumpdir.o getresp.o hlread.o main.o \
-	pins.o readcmd.o readef.o readops.o select.o sws.o writecmd.o writeops.o
+	pins.o readcmd.o readef.o readops.o savebin.o select.o sws.o writecmd.o\
+	writeops.o
 LIBS=	../libcommon/libcommon.a ../libutil/libutil.a
 
 INSTALL_PREFIX=	/opt/freecalypso
--- a/uicc/cmdtab.c	Sun Apr 11 04:17:58 2021 +0000
+++ b/uicc/cmdtab.c	Sun Apr 11 04:32:26 2021 +0000
@@ -25,6 +25,7 @@
 extern int cmd_readbin();
 extern int cmd_readef();
 extern int cmd_readrec();
+extern int cmd_savebin();
 extern int cmd_select();
 extern int cmd_select_aid();
 extern int cmd_select_isim();
@@ -66,6 +67,7 @@
 	{"readbin", 2, 2, 1, cmd_readbin},
 	{"readef", 1, 1, 1, cmd_readef},
 	{"readrec", 1, 2, 1, cmd_readrec},
+	{"savebin", 2, 2, 0, cmd_savebin},
 	{"select", 1, 1, 1, cmd_select},
 	{"select-aid", 1, 1, 1, cmd_select_aid},
 	{"select-isim", 0, 0, 1, cmd_select_isim},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/savebin.c	Sun Apr 11 04:32:26 2021 +0000
@@ -0,0 +1,91 @@
+/*
+ * This module implements the savebin command for saving SIM file content
+ * in UNIX host files.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "simresp.h"
+#include "efstruct.h"
+
+static
+savebin_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);
+		fwrite(sim_resp_data, 1, cc, outf);
+	}
+	return(0);
+}
+
+static
+savebin_records(efs, outf)
+	struct ef_struct *efs;
+	FILE *outf;
+{
+	unsigned recno;
+	int rc;
+
+	for (recno = 1; recno <= efs->record_count; recno++) {
+		rc = readrec_op(recno, 0x04, efs->record_len);
+		if (rc < 0)
+			return(rc);
+		fwrite(sim_resp_data, efs->record_len, 1, outf);
+	}
+	return(0);
+}
+
+cmd_savebin(argc, argv)
+	char **argv;
+{
+	int file_id, rc;
+	struct ef_struct efs;
+	FILE *of;
+
+	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);
+	of = fopen(argv[2], "w");
+	if (!of) {
+		perror(argv[2]);
+		return(-1);
+	}
+	switch (efs.structure) {
+	case 0x01:
+		/* transparent */
+		rc = savebin_transparent(&efs, of);
+		break;
+	case 0x02:
+	case 0x06:
+		/* record-based */
+		rc = savebin_records(&efs, of);
+		break;
+	}
+	fclose(of);
+	return(rc);
+}