# HG changeset patch # User Mychaela Falconia # Date 1611533659 0 # Node ID 2e35070d289f3fc37e7b49bcdfe45a092e47d444 # Parent 66c0cb0e9876c69f6f342de0aebeb6cf53bac778 fc-simtool: savebin command implemented diff -r 66c0cb0e9876 -r 2e35070d289f simtool/Makefile --- a/simtool/Makefile Sun Jan 24 22:46:41 2021 +0000 +++ b/simtool/Makefile Mon Jan 25 00:14:19 2021 +0000 @@ -2,7 +2,7 @@ CFLAGS= -O2 -I/usr/include/PCSC PROG= fc-simtool OBJS= apdu.o atr.o cardconnect.o dispatch.o globals.o hexdump.o hlread.o \ - main.o names.o readcmd.o readops.o select.o telsum.o + main.o names.o readcmd.o readops.o saverestore.o select.o telsum.o all: ${PROG} diff -r 66c0cb0e9876 -r 2e35070d289f simtool/dispatch.c --- a/simtool/dispatch.c Sun Jan 24 22:46:41 2021 +0000 +++ b/simtool/dispatch.c Mon Jan 25 00:14:19 2021 +0000 @@ -17,6 +17,7 @@ extern int cmd_readbin(); extern int cmd_readef(); extern int cmd_readrec(); +extern int cmd_savebin(); extern int cmd_select(); extern int cmd_telecom_sum(); @@ -42,6 +43,7 @@ {"readbin", 2, 2, cmd_readbin}, {"readef", 1, 1, cmd_readef}, {"readrec", 1, 2, cmd_readrec}, + {"savebin", 2, 2, cmd_savebin}, {"select", 1, 1, cmd_select}, {"sim-resp", 0, 0, display_sim_resp_in_hex}, {"telecom-sum", 0, 0, cmd_telecom_sum}, diff -r 66c0cb0e9876 -r 2e35070d289f simtool/saverestore.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simtool/saverestore.c Mon Jan 25 00:14:19 2021 +0000 @@ -0,0 +1,89 @@ +/* + * This module implements commands for saving SIM file content in UNIX host + * files and restoring these backups back to the SIM. + */ + +#include +#include +#include +#include +#include +#include "globals.h" + +static +savebin_transparent(outf) + FILE *outf; +{ + unsigned off, cc; + int rc; + + for (off = 0; off < curfile_total_size; off += cc) { + cc = curfile_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(outf) + FILE *outf; +{ + unsigned recno; + int rc; + + for (recno = 1; recno <= curfile_record_count; recno++) { + rc = readrec_op(recno, 0x04, curfile_record_len); + if (rc < 0) + return(rc); + fwrite(sim_resp_data, curfile_record_len, 1, outf); + } + return(0); +} + +cmd_savebin(argc, argv) + char **argv; +{ + int file_id, rc; + unsigned readlen; + 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 = parse_ef_select_response(); + if (rc < 0) + return(rc); + of = fopen(argv[2], "w"); + if (!of) { + perror(argv[2]); + return(-1); + } + switch (curfile_structure) { + case 0x00: + /* transparent */ + rc = savebin_transparent(of); + break; + case 0x01: + case 0x03: + /* record-based */ + rc = savebin_records(of); + break; + } + fclose(of); + return(rc); +}