comparison simtool/pbdump.c @ 104:a60645b75a57

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