FreeCalypso > hg > fc-pcsc-tools
comparison libcommon/alpha_decode.c @ 0:f7145c77b7fb
starting libcommon: factored out of fc-simtool
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 11 Feb 2021 22:28:45 +0000 |
parents | |
children | d4dc86195382 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f7145c77b7fb |
---|---|
1 /* | |
2 * This module contains functions for decoding and displaying alpha fields | |
3 * that exist in various SIM files. | |
4 */ | |
5 | |
6 #include <sys/types.h> | |
7 #include <string.h> | |
8 #include <strings.h> | |
9 #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 | |
99 static void | |
100 print_alpha_field_hex(data, nbytes, outf) | |
101 u_char *data; | |
102 unsigned nbytes; | |
103 FILE *outf; | |
104 { | |
105 u_char *dp, *endp; | |
106 | |
107 fputs("HEX ", outf); | |
108 dp = data; | |
109 endp = data + nbytes; | |
110 while (dp < endp) | |
111 fprintf(outf, "%02X", *dp++); | |
112 } | |
113 | |
114 void | |
115 print_alpha_field(data, nbytes, outf) | |
116 u_char *data; | |
117 unsigned nbytes; | |
118 FILE *outf; | |
119 { | |
120 if (!nbytes) { | |
121 fputs("\"\"", outf); | |
122 return; | |
123 } | |
124 if (data[0] & 0x80) { | |
125 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 | |
131 print_alpha_field_hex(data, nbytes, outf); | |
132 } |