annotate uicc/alpha_decode.c @ 151:d515cfbb3f39

fc-simtool: hex string parsing: add minimum length parameter
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Feb 2021 00:18:30 +0000
parents a21d348e01db
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module contains functions for decoding and displaying alpha fields
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * that exist in various SIM files.
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <string.h>
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <strings.h>
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 static char gsm7_decode_table[128] = {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 '@', 0, '$', 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 0, 0, '\n', 0, 0, '\r', 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 0, '_', 0, 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 0, 0, 0, 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 ' ', '!', '"', '#', 0, '%', '&', 0x27,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 '(', ')', '*', '+', ',', '-', '.', '/',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 '0', '1', '2', '3', '4', '5', '6', '7',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 '8', '9', ':', ';', '<', '=', '>', '?',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 'x', 'y', 'z', 0, 0, 0, 0, 0
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 };
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 static char gsm7ext_decode_table[128] = {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 0, 0, 0, 0, '^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 0, 0, 0, 0, 0, 0, 0, 0, '{', '}', 0, 0, 0, 0, 0, '\\',
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '[', '~', ']', 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 '|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 };
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 static
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 is_gsm7_string_ok(data, nbytes)
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 u_char *data;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 unsigned nbytes;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 u_char *dp, *endp;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 int c;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 dp = data;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 endp = data + nbytes;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 while (dp < endp) {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 c = *dp++;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (c == 0x1B && dp < endp)
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 c = gsm7ext_decode_table[*dp++];
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 else
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 c = gsm7_decode_table[c];
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (!c)
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 return(0);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 return(1);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 static void
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 print_alpha_field_gsmdecode(data, nbytes, outf)
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 u_char *data;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 unsigned nbytes;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 FILE *outf;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 u_char *dp, *endp;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 int c;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 dp = data;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 endp = data + nbytes;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 putc('"', outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 while (dp < endp) {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 c = *dp++;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 if (c == 0x1B && dp < endp)
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 c = gsm7ext_decode_table[*dp++];
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 else
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 c = gsm7_decode_table[c];
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (c == '\n') {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 putc('\\', outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 putc('n', outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 continue;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (c == '\r') {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 putc('\\', outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 putc('r', outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 continue;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (c == '"' || c == '\\')
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 putc('\\', outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 putc(c, outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 putc('"', outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 static void
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 print_alpha_field_hex(data, nbytes, outf)
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 u_char *data;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 unsigned nbytes;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 FILE *outf;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 u_char *dp, *endp;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 fputs("HEX ", outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 dp = data;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 endp = data + nbytes;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 while (dp < endp)
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 fprintf(outf, "%02X", *dp++);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 void
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 print_alpha_field(data, nbytes, outf)
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 u_char *data;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 unsigned nbytes;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 FILE *outf;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 if (!nbytes) {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 fputs("\"\"", outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 return;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 if (data[0] & 0x80) {
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 print_alpha_field_hex(data, nbytes, outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 return;
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 }
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 if (is_gsm7_string_ok(data, nbytes))
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 print_alpha_field_gsmdecode(data, nbytes, outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 else
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 print_alpha_field_hex(data, nbytes, outf);
a21d348e01db fc-uicc-tool: pb-dump command ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 }