annotate simtool/pbdump.c @ 109:4aaf722ab933

fc-simtool: update-bin-imm command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 26 Jan 2021 01:27:58 +0000
parents 66f04193a906
children a6de34816297
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
104
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements the pb-dump command.
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <string.h>
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <strings.h>
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <pcsclite.h>
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <winscard.h>
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "globals.h"
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 static char gsm_address_digits[16] =
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 {'0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'};
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 static
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 check_all_blank()
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 u_char *dp, *endp;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 dp = sim_resp_data;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 endp = sim_resp_data + sim_resp_data_len;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 while (dp < endp)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (*dp++ != 0xFF)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 return(0);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 return(1);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 static
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 decode_number(data, nbytes, out)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 u_char *data;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 unsigned nbytes;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 char *out;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 u_char *dp, *endp;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 int c;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 dp = data;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 endp = data + nbytes;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 while (dp < endp) {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 c = *dp & 0xF;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (c == 0xF)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 return(-1);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 *out++ = gsm_address_digits[c];
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 c = *dp >> 4;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (c == 0xF) {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if (dp + 1 == endp)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 break;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 else
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 return(-1);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 *out++ = gsm_address_digits[c];
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 dp++;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 *out = '\0';
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 return(0);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 static
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 check_blank_area(dp, endp)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 u_char *dp, *endp;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 while (dp < endp)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 if (*dp++ != 0xFF)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 return(-1);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 return(0);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 static void
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 dump_record(recno, outf)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 unsigned recno;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 FILE *outf;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 int rc;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 unsigned textlen;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 u_char *fixp;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 char digits[21];
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 fprintf(outf, "#%u: ", recno);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (curfile_record_len > 14) {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 rc = validate_alpha_field(sim_resp_data,
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 curfile_record_len - 14,
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 &textlen);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (rc < 0) {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 malformed: fprintf(outf, "malformed record\n");
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 return;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 } else
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 textlen = 0;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 fixp = sim_resp_data + sim_resp_data_len - 14;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (fixp[0] < 2 || fixp[0] > 11)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 goto malformed;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 rc = decode_number(fixp + 2, fixp[0] - 1, digits);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if (rc < 0)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 goto malformed;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 rc = check_blank_area(fixp + 1 + fixp[0], fixp + 12);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 if (rc < 0)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 goto malformed;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 /* all checks passed */
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 fprintf(outf, "%s,0x%02X ", digits, fixp[1]);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 if (fixp[12] != 0xFF)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 fprintf(outf, "CCP=%u ", fixp[12]);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 if (fixp[13] != 0xFF)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 fprintf(outf, "EXT=%u ", fixp[12]);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 print_alpha_field(sim_resp_data, textlen, outf);
106
66f04193a906 fc-simtool: pb-dump: forgot newline at the end of record
Mychaela Falconia <falcon@freecalypso.org>
parents: 105
diff changeset
107 putc('\n', outf);
104
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 cmd_pb_dump(argc, argv)
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 char **argv;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 int rc;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 FILE *outf;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 unsigned recno;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 rc = phonebook_op_common(argv[1]);
105
b1bf0ec6fff5 fc-simtool: pb-dump: first round of bugfixes
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
118 if (rc < 0)
b1bf0ec6fff5 fc-simtool: pb-dump: first round of bugfixes
Mychaela Falconia <falcon@freecalypso.org>
parents: 104
diff changeset
119 return(rc);
104
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 if (argv[2]) {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 outf = fopen(argv[2], "w");
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 if (!outf) {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 perror(argv[2]);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 return(-1);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 } else
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 outf = stdout;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 for (recno = 1; recno <= curfile_record_count; recno++) {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 rc = readrec_op(recno, 0x04, curfile_record_len);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 if (rc < 0) {
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 if (argv[2])
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 fclose(outf);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 return(rc);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 if (check_all_blank())
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 continue;
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 dump_record(recno, outf);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 }
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 if (argv[2])
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 fclose(outf);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 return(0);
a60645b75a57 fc-simtool: phonebook dump implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 }