comparison simtool/pbdump.c @ 1:2071b28cd0c7

simtool: first refactored version
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 11 Feb 2021 23:04:28 +0000
parents
children ce189c97b7b1
comparison
equal deleted inserted replaced
0:f7145c77b7fb 1:2071b28cd0c7
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 "simresp.h"
11 #include "curfile.h"
12
13 static char gsm_address_digits[16] =
14 {'0','1','2','3','4','5','6','7','8','9','*','#','a','b','c','?'};
15
16 static
17 check_all_blank()
18 {
19 u_char *dp, *endp;
20
21 dp = sim_resp_data;
22 endp = sim_resp_data + sim_resp_data_len;
23 while (dp < endp)
24 if (*dp++ != 0xFF)
25 return(0);
26 return(1);
27 }
28
29 static
30 decode_number(data, nbytes, out)
31 u_char *data;
32 unsigned nbytes;
33 char *out;
34 {
35 u_char *dp, *endp;
36 int c;
37
38 dp = data;
39 endp = data + nbytes;
40 while (dp < endp) {
41 c = *dp & 0xF;
42 if (c == 0xF)
43 return(-1);
44 *out++ = gsm_address_digits[c];
45 c = *dp >> 4;
46 if (c == 0xF) {
47 if (dp + 1 == endp)
48 break;
49 else
50 return(-1);
51 }
52 *out++ = gsm_address_digits[c];
53 dp++;
54 }
55 *out = '\0';
56 return(0);
57 }
58
59 static
60 check_blank_area(dp, endp)
61 u_char *dp, *endp;
62 {
63 while (dp < endp)
64 if (*dp++ != 0xFF)
65 return(-1);
66 return(0);
67 }
68
69 static void
70 dump_record(recno, outf)
71 unsigned recno;
72 FILE *outf;
73 {
74 int rc;
75 unsigned textlen;
76 u_char *fixp;
77 char digits[21];
78
79 fprintf(outf, "#%u: ", recno);
80 if (curfile_record_len > 14) {
81 rc = validate_alpha_field(sim_resp_data,
82 curfile_record_len - 14,
83 &textlen);
84 if (rc < 0) {
85 malformed: fprintf(outf, "malformed record\n");
86 return;
87 }
88 } else
89 textlen = 0;
90 fixp = sim_resp_data + sim_resp_data_len - 14;
91 if (fixp[0] < 2 || fixp[0] > 11)
92 goto malformed;
93 rc = decode_number(fixp + 2, fixp[0] - 1, digits);
94 if (rc < 0)
95 goto malformed;
96 rc = check_blank_area(fixp + 1 + fixp[0], fixp + 12);
97 if (rc < 0)
98 goto malformed;
99 /* all checks passed */
100 fprintf(outf, "%s,0x%02X ", digits, fixp[1]);
101 if (fixp[12] != 0xFF)
102 fprintf(outf, "CCP=%u ", fixp[12]);
103 if (fixp[13] != 0xFF)
104 fprintf(outf, "EXT=%u ", fixp[13]);
105 print_alpha_field(sim_resp_data, textlen, outf);
106 putc('\n', 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 (rc < 0)
118 return(rc);
119 if (argv[2]) {
120 outf = fopen(argv[2], "w");
121 if (!outf) {
122 perror(argv[2]);
123 return(-1);
124 }
125 } else
126 outf = stdout;
127 for (recno = 1; recno <= curfile_record_count; recno++) {
128 rc = readrec_op(recno, 0x04, curfile_record_len);
129 if (rc < 0) {
130 if (argv[2])
131 fclose(outf);
132 return(rc);
133 }
134 if (check_all_blank())
135 continue;
136 dump_record(recno, outf);
137 }
138 if (argv[2])
139 fclose(outf);
140 return(0);
141 }
142
143 cmd_pb_dump_rec(argc, argv)
144 char **argv;
145 {
146 int rc;
147 unsigned recno, startrec, endrec;
148
149 rc = phonebook_op_common(argv[1]);
150 if (rc < 0)
151 return(rc);
152 startrec = strtoul(argv[2], 0, 0);
153 if (startrec < 1 || startrec > curfile_record_count) {
154 fprintf(stderr,
155 "error: specified starting record number is invalid\n");
156 return(-1);
157 }
158 if (argv[3]) {
159 endrec = strtoul(argv[3], 0, 0);
160 if (endrec < 1 || endrec > curfile_record_count) {
161 fprintf(stderr,
162 "error: specified final record number is invalid\n");
163 return(-1);
164 }
165 if (startrec > endrec) {
166 fprintf(stderr,
167 "error: reverse record range specified\n");
168 return(-1);
169 }
170 } else
171 endrec = startrec;
172 for (recno = startrec; recno <= endrec; recno++) {
173 rc = readrec_op(recno, 0x04, curfile_record_len);
174 if (rc < 0)
175 return(rc);
176 if (check_all_blank())
177 continue;
178 dump_record(recno, stdout);
179 }
180 return(0);
181 }