FreeCalypso > hg > fc-pcsc-tools
comparison libcommon/alpha_decode.c @ 21:d4dc86195382
GSM7 quoted string output factored out, uses new escapes
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 12 Feb 2021 04:16:35 +0000 |
parents | f7145c77b7fb |
children |
comparison
equal
deleted
inserted
replaced
20:90e7020df08a | 21:d4dc86195382 |
---|---|
2 * This module contains functions for decoding and displaying alpha fields | 2 * This module contains functions for decoding and displaying alpha fields |
3 * that exist in various SIM files. | 3 * that exist in various SIM files. |
4 */ | 4 */ |
5 | 5 |
6 #include <sys/types.h> | 6 #include <sys/types.h> |
7 #include <string.h> | |
8 #include <strings.h> | |
9 #include <stdio.h> | 7 #include <stdio.h> |
10 #include <stdlib.h> | |
11 | |
12 static char gsm7_decode_table[128] = { | |
13 '@', 0, '$', 0, 0, 0, 0, 0, | |
14 0, 0, '\n', 0, 0, '\r', 0, 0, | |
15 0, '_', 0, 0, 0, 0, 0, 0, | |
16 0, 0, 0, 0, 0, 0, 0, 0, | |
17 ' ', '!', '"', '#', 0, '%', '&', 0x27, | |
18 '(', ')', '*', '+', ',', '-', '.', '/', | |
19 '0', '1', '2', '3', '4', '5', '6', '7', | |
20 '8', '9', ':', ';', '<', '=', '>', '?', | |
21 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', | |
22 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | |
23 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | |
24 'X', 'Y', 'Z', 0, 0, 0, 0, 0, | |
25 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', | |
26 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', | |
27 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', | |
28 'x', 'y', 'z', 0, 0, 0, 0, 0 | |
29 }; | |
30 | |
31 static char gsm7ext_decode_table[128] = { | |
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
33 0, 0, 0, 0, '^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
34 0, 0, 0, 0, 0, 0, 0, 0, '{', '}', 0, 0, 0, 0, 0, '\\', | |
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '[', '~', ']', 0, | |
36 '|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
40 }; | |
41 | |
42 static | |
43 is_gsm7_string_ok(data, nbytes) | |
44 u_char *data; | |
45 unsigned nbytes; | |
46 { | |
47 u_char *dp, *endp; | |
48 int c; | |
49 | |
50 dp = data; | |
51 endp = data + nbytes; | |
52 while (dp < endp) { | |
53 c = *dp++; | |
54 if (c == 0x1B && dp < endp) | |
55 c = gsm7ext_decode_table[*dp++]; | |
56 else | |
57 c = gsm7_decode_table[c]; | |
58 if (!c) | |
59 return(0); | |
60 } | |
61 return(1); | |
62 } | |
63 | |
64 static void | |
65 print_alpha_field_gsmdecode(data, nbytes, outf) | |
66 u_char *data; | |
67 unsigned nbytes; | |
68 FILE *outf; | |
69 { | |
70 u_char *dp, *endp; | |
71 int c; | |
72 | |
73 dp = data; | |
74 endp = data + nbytes; | |
75 putc('"', outf); | |
76 while (dp < endp) { | |
77 c = *dp++; | |
78 if (c == 0x1B && dp < endp) | |
79 c = gsm7ext_decode_table[*dp++]; | |
80 else | |
81 c = gsm7_decode_table[c]; | |
82 if (c == '\n') { | |
83 putc('\\', outf); | |
84 putc('n', outf); | |
85 continue; | |
86 } | |
87 if (c == '\r') { | |
88 putc('\\', outf); | |
89 putc('r', outf); | |
90 continue; | |
91 } | |
92 if (c == '"' || c == '\\') | |
93 putc('\\', outf); | |
94 putc(c, outf); | |
95 } | |
96 putc('"', outf); | |
97 } | |
98 | 8 |
99 static void | 9 static void |
100 print_alpha_field_hex(data, nbytes, outf) | 10 print_alpha_field_hex(data, nbytes, outf) |
101 u_char *data; | 11 u_char *data; |
102 unsigned nbytes; | 12 unsigned nbytes; |
119 { | 29 { |
120 if (!nbytes) { | 30 if (!nbytes) { |
121 fputs("\"\"", outf); | 31 fputs("\"\"", outf); |
122 return; | 32 return; |
123 } | 33 } |
124 if (data[0] & 0x80) { | 34 if (data[0] & 0x80) |
125 print_alpha_field_hex(data, nbytes, outf); | 35 print_alpha_field_hex(data, nbytes, outf); |
126 return; | |
127 } | |
128 if (is_gsm7_string_ok(data, nbytes)) | |
129 print_alpha_field_gsmdecode(data, nbytes, outf); | |
130 else | 36 else |
131 print_alpha_field_hex(data, nbytes, outf); | 37 print_gsm7_string_to_file(data, nbytes, outf); |
132 } | 38 } |