FreeCalypso > hg > fc-pcsc-tools
comparison libcommon/gsm7_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 | libcommon/alpha_decode.c@f7145c77b7fb |
children |
comparison
equal
deleted
inserted
replaced
20:90e7020df08a | 21:d4dc86195382 |
---|---|
1 /* | |
2 * This module contains functions for decoding GSM7 strings | |
3 * that exist in various SIM files. | |
4 */ | |
5 | |
6 #include <sys/types.h> | |
7 #include <stdio.h> | |
8 | |
9 static char gsm7_decode_table[128] = { | |
10 '@', 0, '$', 0, 0, 0, 0, 0, | |
11 0, 0, '\n', 0, 0, '\r', 0, 0, | |
12 0, '_', 0, 0, 0, 0, 0, 0, | |
13 0, 0, 0, 0, 0, 0, 0, 0, | |
14 ' ', '!', '"', '#', 0, '%', '&', 0x27, | |
15 '(', ')', '*', '+', ',', '-', '.', '/', | |
16 '0', '1', '2', '3', '4', '5', '6', '7', | |
17 '8', '9', ':', ';', '<', '=', '>', '?', | |
18 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', | |
19 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | |
20 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | |
21 'X', 'Y', 'Z', 0, 0, 0, 0, 0, | |
22 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', | |
23 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', | |
24 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', | |
25 'x', 'y', 'z', 0, 0, 0, 0, 0 | |
26 }; | |
27 | |
28 static char gsm7ext_decode_table[128] = { | |
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
30 0, 0, 0, 0, '^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
31 0, 0, 0, 0, 0, 0, 0, 0, '{', '}', 0, 0, 0, 0, 0, '\\', | |
32 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, 0, 0, 0, | |
35 0, 0, 0, 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, 0 | |
37 }; | |
38 | |
39 void | |
40 print_gsm7_string_to_file(data, nbytes, outf) | |
41 u_char *data; | |
42 unsigned nbytes; | |
43 FILE *outf; | |
44 { | |
45 u_char *dp, *endp; | |
46 int b, c; | |
47 | |
48 dp = data; | |
49 endp = data + nbytes; | |
50 putc('"', outf); | |
51 while (dp < endp) { | |
52 b = *dp++; | |
53 if (b == 0x1B) { | |
54 if (dp >= endp || *dp == 0x1B || *dp == '\n' || | |
55 *dp == '\r') { | |
56 putc('\\', outf); | |
57 putc('e', outf); | |
58 continue; | |
59 } | |
60 b = *dp++; | |
61 c = gsm7ext_decode_table[b]; | |
62 if (!c) { | |
63 fprintf(outf, "\\e\\%02X", b); | |
64 continue; | |
65 } | |
66 } else { | |
67 c = gsm7_decode_table[b]; | |
68 if (!c) { | |
69 fprintf(outf, "\\%02X", b); | |
70 continue; | |
71 } | |
72 } | |
73 if (c == '\n') { | |
74 putc('\\', outf); | |
75 putc('n', outf); | |
76 continue; | |
77 } | |
78 if (c == '\r') { | |
79 putc('\\', outf); | |
80 putc('r', outf); | |
81 continue; | |
82 } | |
83 if (c == '"' || c == '\\') | |
84 putc('\\', outf); | |
85 putc(c, outf); | |
86 } | |
87 putc('"', outf); | |
88 } |