FreeCalypso > hg > fc-pcsc-tools
changeset 16:a6ca422323b9
simtool: saverestore.c split into savebin.c and restorebin.c
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 12 Feb 2021 01:41:21 +0000 |
parents | 5390dce9faa4 |
children | b8d27c72747a |
files | simtool/Makefile simtool/restorebin.c simtool/savebin.c simtool/saverestore.c |
diffstat | 4 files changed, 241 insertions(+), 229 deletions(-) [+] |
line wrap: on
line diff
--- a/simtool/Makefile Fri Feb 12 01:36:18 2021 +0000 +++ b/simtool/Makefile Fri Feb 12 01:41:21 2021 +0000 @@ -3,8 +3,8 @@ PROG= fc-simtool OBJS= a38.o chv.o curfile.o dispatch.o dumpdir.o grcard1.o grcard2.o hlread.o\ main.o pbcommon.o pbdump.o pberase.o pbupd_file.o pbupd_imm.o \ - pbupd_immhex.o readcmd.o readops.o saverestore.o script.o select.o \ - sysmo.o telsum.o writecmd.o writeops.o + pbupd_immhex.o readcmd.o readops.o restorebin.o savebin.o script.o \ + select.o sysmo.o telsum.o writecmd.o writeops.o LIBS= ../libcommon/libcommon.a INSTBIN=/opt/freecalypso/bin
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simtool/restorebin.c Fri Feb 12 01:41:21 2021 +0000 @@ -0,0 +1,120 @@ +/* + * This module implements the restore-file command; this command + * reads binary files previously saved with the savebin command + * and writes the backed-up bits back to the SIM. + */ + +#include <sys/types.h> +#include <sys/file.h> +#include <sys/stat.h> +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include "curfile.h" + +static +restore_transparent(data) + u_char *data; +{ + unsigned off, cc; + int rc; + + for (off = 0; off < curfile_total_size; off += cc) { + cc = curfile_total_size - off; + if (cc > 255) + cc = 255; + rc = update_bin_op(off, data + off, cc); + if (rc < 0) + return(rc); + } + return(0); +} + +static +restore_records(data) + u_char *data; +{ + unsigned recno; + u_char *dp; + int rc; + + dp = data; + for (recno = 1; recno <= curfile_record_count; recno++) { + rc = update_rec_op(recno, 0x04, dp, curfile_record_len); + if (rc < 0) + return(rc); + dp += curfile_record_len; + } + return(0); +} + +cmd_restore_file(argc, argv) + char **argv; +{ + int file_id, rc, fd; + struct stat st; + u_char *databuf; + + 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); + if (!curfile_total_size) { + printf("SIM indicates file of zero length, nothing to do\n"); + return(0); + } + fd = open(argv[2], O_RDONLY); + if (fd < 0) { + perror(argv[2]); + return(-1); + } + fstat(fd, &st); + if ((st.st_mode & S_IFMT) != S_IFREG) { + fprintf(stderr, "error: %s is not a regular file\n", argv[2]); + close(fd); + return(-1); + } + if (st.st_size != curfile_total_size) { + fprintf(stderr, + "error: length of %s does not match SIM EF length of %u bytes\n", + argv[2], curfile_total_size); + close(fd); + return(-1); + } + databuf = malloc(curfile_total_size); + if (!databuf) { + perror("malloc for file data"); + close(fd); + return(-1); + } + read(fd, databuf, curfile_total_size); + close(fd); + switch (curfile_structure) { + case 0x00: + /* transparent */ + rc = restore_transparent(databuf); + break; + case 0x01: + /* record-based */ + rc = restore_records(databuf); + break; + case 0x03: + fprintf(stderr, "error: cyclic files are not supported\n"); + rc = -1; + } + free(databuf); + return(rc); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simtool/savebin.c Fri Feb 12 01:41:21 2021 +0000 @@ -0,0 +1,119 @@ +/* + * This module implements commands 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 "curfile.h" +#include "file_id.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; + 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); +} + +cmd_save_sms_bin(argc, argv) + char **argv; +{ + int rc; + FILE *of; + + rc = select_op(DF_TELECOM); + if (rc < 0) + return(rc); + rc = select_op(EF_SMS); + if (rc < 0) + return(rc); + rc = parse_ef_select_response(); + if (rc < 0) + return(rc); + if (curfile_structure != 0x01 || curfile_record_len != 176) { + fprintf(stderr, + "error: EF_SMS is not linear fixed with 176-byte records\n"); + return(-1); + } + of = fopen(argv[1], "w"); + if (!of) { + perror(argv[1]); + return(-1); + } + rc = savebin_records(of); + fclose(of); + return(rc); +}
--- a/simtool/saverestore.c Fri Feb 12 01:36:18 2021 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,227 +0,0 @@ -/* - * This module implements commands for saving SIM file content in UNIX host - * files and restoring these backups back to the SIM. - */ - -#include <sys/types.h> -#include <sys/file.h> -#include <sys/stat.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include "simresp.h" -#include "curfile.h" -#include "file_id.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; - 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); -} - -cmd_save_sms_bin(argc, argv) - char **argv; -{ - int rc; - FILE *of; - - rc = select_op(DF_TELECOM); - if (rc < 0) - return(rc); - rc = select_op(EF_SMS); - if (rc < 0) - return(rc); - rc = parse_ef_select_response(); - if (rc < 0) - return(rc); - if (curfile_structure != 0x01 || curfile_record_len != 176) { - fprintf(stderr, - "error: EF_SMS is not linear fixed with 176-byte records\n"); - return(-1); - } - of = fopen(argv[1], "w"); - if (!of) { - perror(argv[1]); - return(-1); - } - rc = savebin_records(of); - fclose(of); - return(rc); -} - -static -restore_transparent(data) - u_char *data; -{ - unsigned off, cc; - int rc; - - for (off = 0; off < curfile_total_size; off += cc) { - cc = curfile_total_size - off; - if (cc > 255) - cc = 255; - rc = update_bin_op(off, data + off, cc); - if (rc < 0) - return(rc); - } - return(0); -} - -static -restore_records(data) - u_char *data; -{ - unsigned recno; - u_char *dp; - int rc; - - dp = data; - for (recno = 1; recno <= curfile_record_count; recno++) { - rc = update_rec_op(recno, 0x04, dp, curfile_record_len); - if (rc < 0) - return(rc); - dp += curfile_record_len; - } - return(0); -} - -cmd_restore_file(argc, argv) - char **argv; -{ - int file_id, rc, fd; - struct stat st; - u_char *databuf; - - 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); - if (!curfile_total_size) { - printf("SIM indicates file of zero length, nothing to do\n"); - return(0); - } - fd = open(argv[2], O_RDONLY); - if (fd < 0) { - perror(argv[2]); - return(-1); - } - fstat(fd, &st); - if ((st.st_mode & S_IFMT) != S_IFREG) { - fprintf(stderr, "error: %s is not a regular file\n", argv[2]); - close(fd); - return(-1); - } - if (st.st_size != curfile_total_size) { - fprintf(stderr, - "error: length of %s does not match SIM EF length of %u bytes\n", - argv[2], curfile_total_size); - close(fd); - return(-1); - } - databuf = malloc(curfile_total_size); - if (!databuf) { - perror("malloc for file data"); - close(fd); - return(-1); - } - read(fd, databuf, curfile_total_size); - close(fd); - switch (curfile_structure) { - case 0x00: - /* transparent */ - rc = restore_transparent(databuf); - break; - case 0x01: - /* record-based */ - rc = restore_records(databuf); - break; - case 0x03: - fprintf(stderr, "error: cyclic files are not supported\n"); - rc = -1; - } - free(databuf); - return(rc); -}