comparison simtool/saverestore.c @ 99:2e35070d289f

fc-simtool: savebin command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 25 Jan 2021 00:14:19 +0000
parents
children fa7005185b84
comparison
equal deleted inserted replaced
98:66c0cb0e9876 99:2e35070d289f
1 /*
2 * This module implements commands for saving SIM file content in UNIX host
3 * files and restoring these backups back to the SIM.
4 */
5
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <pcsclite.h>
10 #include <winscard.h>
11 #include "globals.h"
12
13 static
14 savebin_transparent(outf)
15 FILE *outf;
16 {
17 unsigned off, cc;
18 int rc;
19
20 for (off = 0; off < curfile_total_size; off += cc) {
21 cc = curfile_total_size - off;
22 if (cc > 256)
23 cc = 256;
24 rc = readbin_op(off, cc);
25 if (rc < 0)
26 return(rc);
27 fwrite(sim_resp_data, 1, cc, outf);
28 }
29 return(0);
30 }
31
32 static
33 savebin_records(outf)
34 FILE *outf;
35 {
36 unsigned recno;
37 int rc;
38
39 for (recno = 1; recno <= curfile_record_count; recno++) {
40 rc = readrec_op(recno, 0x04, curfile_record_len);
41 if (rc < 0)
42 return(rc);
43 fwrite(sim_resp_data, curfile_record_len, 1, outf);
44 }
45 return(0);
46 }
47
48 cmd_savebin(argc, argv)
49 char **argv;
50 {
51 int file_id, rc;
52 unsigned readlen;
53 FILE *of;
54
55 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
56 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
57 file_id = strtoul(argv[1], 0, 16);
58 else
59 file_id = find_symbolic_file_name(argv[1]);
60 if (file_id < 0) {
61 fprintf(stderr,
62 "error: file ID argument is not a hex value or a recognized symbolic name\n");
63 return(-1);
64 }
65 rc = select_op(file_id);
66 if (rc < 0)
67 return(rc);
68 rc = parse_ef_select_response();
69 if (rc < 0)
70 return(rc);
71 of = fopen(argv[2], "w");
72 if (!of) {
73 perror(argv[2]);
74 return(-1);
75 }
76 switch (curfile_structure) {
77 case 0x00:
78 /* transparent */
79 rc = savebin_transparent(of);
80 break;
81 case 0x01:
82 case 0x03:
83 /* record-based */
84 rc = savebin_records(of);
85 break;
86 }
87 fclose(of);
88 return(rc);
89 }