changeset 107:f1836c8d36cb

hexdump output commands reworked to support redirection
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 19 Feb 2021 06:43:01 +0000
parents dcfec53643c5
children e17940d7ce35
files libcommon/hexdump.c simtool/dispatch.c simtool/readcmd.c uicc/dispatch.c uicc/readcmd.c
diffstat 5 files changed, 58 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/libcommon/hexdump.c	Fri Feb 19 06:24:06 2021 +0000
+++ b/libcommon/hexdump.c	Fri Feb 19 06:43:01 2021 +0000
@@ -2,35 +2,43 @@
 #include <stdio.h>
 #include "simresp.h"
 
-display_sim_resp_in_hex()
+display_sim_resp_in_hex(outf)
+	FILE *outf;
 {
 	unsigned off, cc, n, c;
 
 	for (off = 0; off < sim_resp_data_len; off += cc) {
-		printf("%02X:", off);
+		fprintf(outf, "%02X:", 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)
-				putchar(' ');
-			putchar(' ');
+				putc(' ', outf);
+			putc(' ', outf);
 			if (n < cc)
-				printf("%02X", sim_resp_data[off + n]);
+				fprintf(outf, "%02X", sim_resp_data[off + n]);
 			else {
-				putchar(' ');
-				putchar(' ');
+				putc(' ', outf);
+				putc(' ', outf);
 			}
 		}
-		putchar(' ');
-		putchar(' ');
+		putc(' ', outf);
+		putc(' ', outf);
 		for (n = 0; n < cc; n++) {
 			c = sim_resp_data[off + n];
 			if (c < 0x20 || c > 0x7E)
 				c = '.';
-			putchar(c);
+			putc(c, outf);
 		}
-		putchar('\n');
+		putc('\n', outf);
 	}
 	return(0);
 }
+
+cmd_sim_resp(argc, argv, outf)
+	char **argv;
+	FILE *outf;
+{
+	return display_sim_resp_in_hex(outf);
+}
--- a/simtool/dispatch.c	Fri Feb 19 06:24:06 2021 +0000
+++ b/simtool/dispatch.c	Fri Feb 19 06:43:01 2021 +0000
@@ -55,6 +55,7 @@
 extern int cmd_savebin();
 extern int cmd_save_sms_bin();
 extern int cmd_select();
+extern int cmd_sim_resp();
 extern int cmd_sms_erase_all();
 extern int cmd_sms_erase_one();
 extern int cmd_sms_erase_range();
@@ -81,7 +82,6 @@
 extern int cmd_write_iccid();
 extern int cmd_write_imsi();
 
-extern int display_sim_resp_in_hex();
 extern int good_exit();
 extern int retrieve_atr();
 
@@ -148,14 +148,14 @@
 	{"plmnsel-write", 2, 2, 0, cmd_plmnsel_write},
 	{"pnn-dump", 0, 0, 0, cmd_pnn_dump},
 	{"quit", 0, 0, 0, good_exit},
-	{"readbin", 2, 2, 0, cmd_readbin},
-	{"readef", 1, 1, 0, cmd_readef},
-	{"readrec", 1, 2, 0, cmd_readrec},
+	{"readbin", 2, 2, 1, cmd_readbin},
+	{"readef", 1, 1, 1, cmd_readef},
+	{"readrec", 1, 2, 1, cmd_readrec},
 	{"restore-file", 2, 2, 0, cmd_restore_file},
 	{"savebin", 2, 2, 0, cmd_savebin},
 	{"save-sms-bin", 1, 1, 0, cmd_save_sms_bin},
 	{"select", 1, 1, 0, cmd_select},
-	{"sim-resp", 0, 0, 0, display_sim_resp_in_hex},
+	{"sim-resp", 0, 0, 1, cmd_sim_resp},
 	{"sms-erase-all", 0, 0, 0, cmd_sms_erase_all},
 	{"sms-erase-one", 1, 1, 0, cmd_sms_erase_one},
 	{"sms-erase-range", 2, 2, 0, cmd_sms_erase_range},
--- a/simtool/readcmd.c	Fri Feb 19 06:24:06 2021 +0000
+++ b/simtool/readcmd.c	Fri Feb 19 06:43:01 2021 +0000
@@ -5,8 +5,9 @@
 #include "simresp.h"
 #include "curfile.h"
 
-cmd_readbin(argc, argv)
+cmd_readbin(argc, argv, outf)
 	char **argv;
+	FILE *outf;
 {
 	unsigned offset, len;
 	int rc;
@@ -24,12 +25,13 @@
 	rc = readbin_op(offset, len);
 	if (rc < 0)
 		return(rc);
-	display_sim_resp_in_hex();
+	display_sim_resp_in_hex(outf);
 	return(0);
 }
 
-cmd_readrec(argc, argv)
+cmd_readrec(argc, argv, outf)
 	char **argv;
+	FILE *outf;
 {
 	unsigned recno, len;
 	int rc;
@@ -58,12 +60,13 @@
 	rc = readrec_op(recno, 0x04, len);
 	if (rc < 0)
 		return(rc);
-	display_sim_resp_in_hex();
+	display_sim_resp_in_hex(outf);
 	return(0);
 }
 
-cmd_readef(argc, argv)
+cmd_readef(argc, argv, outf)
 	char **argv;
+	FILE *outf;
 {
 	int file_id, rc;
 	unsigned readlen;
@@ -89,13 +92,15 @@
 			"error: readef command is only for transparent EFs\n");
 		return(-1);
 	}
-	printf("Transparent EF of %u byte(s)\n", curfile_total_size);
-	printf("File status: %02X\n", sim_resp_data[11]);
-	show_access_conditions("UPDATE", sim_resp_data[8] & 0xF);
-	show_access_conditions("READ & SEEK", sim_resp_data[8] >> 4);
-	show_access_conditions("INCREASE", sim_resp_data[9] >> 4);
-	show_access_conditions("INVALIDATE", sim_resp_data[10] & 0xF);
-	show_access_conditions("REHABILITATE", sim_resp_data[10] >> 4);
+	if (outf == stdout) {
+		printf("Transparent EF of %u byte(s)\n", curfile_total_size);
+		printf("File status: %02X\n", sim_resp_data[11]);
+		show_access_conditions("UPDATE", sim_resp_data[8] & 0xF);
+		show_access_conditions("READ & SEEK", sim_resp_data[8] >> 4);
+		show_access_conditions("INCREASE", sim_resp_data[9] >> 4);
+		show_access_conditions("INVALIDATE", sim_resp_data[10] & 0xF);
+		show_access_conditions("REHABILITATE", sim_resp_data[10] >> 4);
+	}
 	if (!curfile_total_size)
 		return(0);
 	readlen = curfile_total_size;
@@ -104,6 +109,6 @@
 	rc = readbin_op(0, readlen);
 	if (rc < 0)
 		return(rc);
-	display_sim_resp_in_hex();
+	display_sim_resp_in_hex(outf);
 	return(0);
 }
--- a/uicc/dispatch.c	Fri Feb 19 06:24:06 2021 +0000
+++ b/uicc/dispatch.c	Fri Feb 19 06:43:01 2021 +0000
@@ -18,11 +18,11 @@
 extern int cmd_select_aid();
 extern int cmd_select_isim();
 extern int cmd_select_usim();
+extern int cmd_sim_resp();
 extern int cmd_update_bin();
 extern int cmd_update_bin_imm();
 extern int cmd_update_rec();
 
-extern int display_sim_resp_in_hex();
 extern int good_exit();
 extern int retrieve_atr();
 
@@ -39,14 +39,14 @@
 	{"exit", 0, 0, 0, good_exit},
 	{"iccid", 0, 0, 0, cmd_iccid},
 	{"quit", 0, 0, 0, good_exit},
-	{"readbin", 2, 2, 0, cmd_readbin},
-	{"readef", 1, 1, 0, cmd_readef},
-	{"readrec", 1, 2, 0, cmd_readrec},
+	{"readbin", 2, 2, 1, cmd_readbin},
+	{"readef", 1, 1, 1, cmd_readef},
+	{"readrec", 1, 2, 1, cmd_readrec},
 	{"select", 1, 1, 0, cmd_select},
 	{"select-aid", 1, 1, 0, cmd_select_aid},
 	{"select-isim", 0, 0, 0, cmd_select_isim},
 	{"select-usim", 0, 0, 0, cmd_select_usim},
-	{"sim-resp", 0, 0, 0, display_sim_resp_in_hex},
+	{"sim-resp", 0, 0, 1, cmd_sim_resp},
 	{"update-bin", 2, 2, 0, cmd_update_bin},
 	{"update-bin-imm", 2, 2, 0, cmd_update_bin_imm},
 	{"update-rec", 2, 2, 0, cmd_update_rec},
--- a/uicc/readcmd.c	Fri Feb 19 06:24:06 2021 +0000
+++ b/uicc/readcmd.c	Fri Feb 19 06:43:01 2021 +0000
@@ -6,8 +6,9 @@
 
 extern unsigned last_sel_file_record_len;
 
-cmd_readbin(argc, argv)
+cmd_readbin(argc, argv, outf)
 	char **argv;
+	FILE *outf;
 {
 	unsigned offset, len;
 	int rc;
@@ -25,12 +26,13 @@
 	rc = readbin_op(offset, len);
 	if (rc < 0)
 		return(rc);
-	display_sim_resp_in_hex();
+	display_sim_resp_in_hex(outf);
 	return(0);
 }
 
-cmd_readrec(argc, argv)
+cmd_readrec(argc, argv, outf)
 	char **argv;
+	FILE *outf;
 {
 	unsigned recno, len;
 	int rc;
@@ -59,12 +61,13 @@
 	rc = readrec_op(recno, 0x04, len);
 	if (rc < 0)
 		return(rc);
-	display_sim_resp_in_hex();
+	display_sim_resp_in_hex(outf);
 	return(0);
 }
 
-cmd_readef(argc, argv)
+cmd_readef(argc, argv, outf)
 	char **argv;
+	FILE *outf;
 {
 	int file_id, rc;
 	unsigned file_len, readlen;
@@ -85,7 +88,8 @@
 	rc = select_resp_get_transparent(&file_len);
 	if (rc < 0)
 		return(rc);
-	printf("Transparent EF of %u byte(s)\n", file_len);
+	if (outf == stdout)
+		printf("Transparent EF of %u byte(s)\n", file_len);
 	if (!file_len)
 		return(0);
 	readlen = file_len;
@@ -94,6 +98,6 @@
 	rc = readbin_op(0, readlen);
 	if (rc < 0)
 		return(rc);
-	display_sim_resp_in_hex();
+	display_sim_resp_in_hex(outf);
 	return(0);
 }