comparison uicc/readef.c @ 89:db131929ee96

fc-uicc-tool: extended readef ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 04:17:58 +0000
parents simtool/readef.c@49b7e02787c1
children
comparison
equal deleted inserted replaced
88:49b7e02787c1 89:db131929ee96
1 /*
2 * This module implements the readef command for dumping the complete
3 * content of SIM files.
4 */
5
6 #include <sys/types.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "simresp.h"
11 #include "efstruct.h"
12
13 static void
14 hexdump_with_offset(outf, extoff)
15 FILE *outf;
16 unsigned extoff;
17 {
18 unsigned off, cc, n, c;
19
20 for (off = 0; off < sim_resp_data_len; off += cc) {
21 fprintf(outf, "%04X:", extoff + off);
22 cc = 16;
23 if (sim_resp_data_len - off < cc)
24 cc = sim_resp_data_len - off;
25 for (n = 0; n < 16; n++) {
26 if (n == 0 || n == 8)
27 putc(' ', outf);
28 putc(' ', outf);
29 if (n < cc)
30 fprintf(outf, "%02X", sim_resp_data[off + n]);
31 else {
32 putc(' ', outf);
33 putc(' ', outf);
34 }
35 }
36 putc(' ', outf);
37 putc(' ', outf);
38 for (n = 0; n < cc; n++) {
39 c = sim_resp_data[off + n];
40 if (c < 0x20 || c > 0x7E)
41 c = '.';
42 putc(c, outf);
43 }
44 putc('\n', outf);
45 }
46 }
47
48 static
49 readef_transparent(efs, outf)
50 struct ef_struct *efs;
51 FILE *outf;
52 {
53 unsigned off, cc;
54 int rc;
55
56 for (off = 0; off < efs->total_size; off += cc) {
57 cc = efs->total_size - off;
58 if (cc > 256)
59 cc = 256;
60 rc = readbin_op(off, cc);
61 if (rc < 0)
62 return(rc);
63 hexdump_with_offset(outf, off);
64 }
65 return(0);
66 }
67
68 static
69 readef_records(efs, outf)
70 struct ef_struct *efs;
71 FILE *outf;
72 {
73 unsigned recno;
74 int rc;
75
76 for (recno = 1; recno <= efs->record_count; recno++) {
77 fprintf(outf, "Record #%u:\n", recno);
78 rc = readrec_op(recno, 0x04, efs->record_len);
79 if (rc < 0)
80 return(rc);
81 display_sim_resp_in_hex(outf);
82 }
83 return(0);
84 }
85
86 cmd_readef(argc, argv, outf)
87 char **argv;
88 FILE *outf;
89 {
90 int file_id, rc;
91 struct ef_struct efs;
92
93 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
94 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
95 file_id = strtoul(argv[1], 0, 16);
96 else
97 file_id = find_symbolic_file_name(argv[1]);
98 if (file_id < 0) {
99 fprintf(stderr,
100 "error: file ID argument is not a hex value or a recognized symbolic name\n");
101 return(-1);
102 }
103 rc = select_op(file_id);
104 if (rc < 0)
105 return(rc);
106 rc = select_resp_get_ef_struct(&efs);
107 if (rc < 0)
108 return(rc);
109 switch (efs.structure) {
110 case 0x01:
111 fprintf(outf, "Transparent EF of %u byte(s)\n", efs.total_size);
112 return readef_transparent(&efs, outf);
113 case 0x02:
114 fprintf(outf, "%u records of %u bytes (linear fixed)\n",
115 efs.record_count, efs.record_len);
116 return readef_records(&efs, outf);
117 case 0x06:
118 fprintf(outf, "%u records of %u bytes (cyclic)\n",
119 efs.record_count, efs.record_len);
120 return readef_records(&efs, outf);
121 }
122 }