comparison simtool/readef.c @ 10:ddd767f6e15b

fc-simtool ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 07:11:25 +0000
parents
children 49b7e02787c1
comparison
equal deleted inserted replaced
9:c9ef9e91dd8e 10:ddd767f6e15b
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 "curfile.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(outf)
50 FILE *outf;
51 {
52 unsigned off, cc;
53 int rc;
54
55 for (off = 0; off < curfile_total_size; off += cc) {
56 cc = curfile_total_size - off;
57 if (cc > 256)
58 cc = 256;
59 rc = readbin_op(off, cc);
60 if (rc < 0)
61 return(rc);
62 hexdump_with_offset(outf, off);
63 }
64 return(0);
65 }
66
67 static
68 readef_records(outf)
69 FILE *outf;
70 {
71 unsigned recno;
72 int rc;
73
74 for (recno = 1; recno <= curfile_record_count; recno++) {
75 fprintf(outf, "Record #%u:\n", recno);
76 rc = readrec_op(recno, 0x04, curfile_record_len);
77 if (rc < 0)
78 return(rc);
79 display_sim_resp_in_hex(outf);
80 }
81 return(0);
82 }
83
84 cmd_readef(argc, argv, outf)
85 char **argv;
86 FILE *outf;
87 {
88 int file_id, rc;
89 unsigned readlen;
90
91 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
92 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
93 file_id = strtoul(argv[1], 0, 16);
94 else
95 file_id = find_symbolic_file_name(argv[1]);
96 if (file_id < 0) {
97 fprintf(stderr,
98 "error: file ID argument is not a hex value or a recognized symbolic name\n");
99 return(-1);
100 }
101 rc = select_op(file_id);
102 if (rc < 0)
103 return(rc);
104 rc = parse_ef_select_response();
105 if (rc < 0)
106 return(rc);
107 show_access_conditions(outf, "UPDATE", sim_resp_data[8] & 0xF);
108 show_access_conditions(outf, "READ & SEEK", sim_resp_data[8] >> 4);
109 show_access_conditions(outf, "INCREASE", sim_resp_data[9] >> 4);
110 show_access_conditions(outf, "INVALIDATE", sim_resp_data[10] & 0xF);
111 show_access_conditions(outf, "REHABILITATE", sim_resp_data[10] >> 4);
112 fprintf(outf, "File status: %02X\n", sim_resp_data[11]);
113 switch (curfile_structure) {
114 case 0x00:
115 fprintf(outf, "Transparent EF of %u byte(s)\n",
116 curfile_total_size);
117 return readef_transparent(outf);
118 case 0x01:
119 fprintf(outf, "%u records of %u bytes (linear fixed)\n",
120 curfile_record_count, curfile_record_len);
121 return readef_records(outf);
122 case 0x03:
123 fprintf(outf, "%u records of %u bytes (cyclic)\n",
124 curfile_record_count, curfile_record_len);
125 return readef_records(outf);
126 }
127 }