annotate libutil/gsm7_decode.c @ 63:f4eb486aab40

doc/Admin-write-commands: fix verify-hex command name
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 23 Mar 2021 23:29:31 +0000
parents 34bbb0585cab
children c5e7c9e1d857
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module contains functions for decoding GSM7 strings
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * that exist in various SIM files.
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 static char gsm7_decode_table[128] = {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 '@', 0, '$', 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 0, 0, '\n', 0, 0, '\r', 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 0, '_', 0, 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 0, 0, 0, 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 ' ', '!', '"', '#', 0, '%', '&', 0x27,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 '(', ')', '*', '+', ',', '-', '.', '/',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 '0', '1', '2', '3', '4', '5', '6', '7',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 '8', '9', ':', ';', '<', '=', '>', '?',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 'x', 'y', 'z', 0, 0, 0, 0, 0
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 };
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 static char gsm7ext_decode_table[128] = {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 0, 0, 0, 0, '^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 0, 0, 0, 0, 0, 0, 0, 0, '{', '}', 0, 0, 0, 0, 0, '\\',
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '[', '~', ']', 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 '|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 };
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 void
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 print_gsm7_string_to_file(data, nbytes, outf)
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 u_char *data;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 unsigned nbytes;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 FILE *outf;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 u_char *dp, *endp;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 int b, c;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 dp = data;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 endp = data + nbytes;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 putc('"', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 while (dp < endp) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 b = *dp++;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 if (b == 0x1B) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (dp >= endp || *dp == 0x1B || *dp == '\n' ||
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 *dp == '\r') {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 putc('\\', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 putc('e', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 continue;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 b = *dp++;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 c = gsm7ext_decode_table[b];
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (!c) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 fprintf(outf, "\\e\\%02X", b);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 continue;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 } else {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 c = gsm7_decode_table[b];
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (!c) {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fprintf(outf, "\\%02X", b);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 continue;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (c == '\n') {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 putc('\\', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 putc('n', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 continue;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 if (c == '\r') {
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 putc('\\', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 putc('r', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 continue;
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (c == '"' || c == '\\')
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 putc('\\', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 putc(c, outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 putc('"', outf);
34bbb0585cab libutil: import from previous fc-pcsc-tools version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }