annotate libcoding/gsm7_decode_qstring.c @ 31:19476164c54d

doc/SMS-PDU-decoding: document imported utilities
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 14 Jun 2024 18:48:58 +0000
parents 7418ca2e9949
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements a function for decoding GSM7 strings
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * to ASCII with output to a stdio file; it is an implementation
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * of lossless conversion per our SIM-data-formats spec
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * in freecalypso-docs.
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 static char basic_table[128] = {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 '@', 0, '$', 0, 0, 0, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 0, 0, 'n'|0x80, 0, 0, 'r'|0x80, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 0, '_', 0, 0, 0, 0, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 0, 0, 0, 0, 0, 0, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 ' ', '!', '"'|0x80, '#', 0, '%', '&', 0x27,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 '(', ')', '*', '+', ',', '-', '.', '/',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 '0', '1', '2', '3', '4', '5', '6', '7',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 '8', '9', ':', ';', '<', '=', '>', '?',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 'X', 'Y', 'Z', 0, 0, 0, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 'x', 'y', 'z', 0, 0, 0, 0, 0
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 };
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 static char escape_table[128] = {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 0, 0, 0, 0, '^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 0, 0, 0, 0, 0, 0, 0, 0, '{', '}', 0, 0, 0, 0, 0, '\\'|0x80,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '[', '~', ']', 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 '|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
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,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 0, 0, 0, 0, 0,'E'|0x80,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
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
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 };
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 void
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 print_gsm7_string_to_file(data, nbytes, outf)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 u_char *data;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 unsigned nbytes;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 FILE *outf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 u_char *dp, *endp;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 int b, c;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 dp = data;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 endp = data + nbytes;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 putc('"', outf);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 while (dp < endp) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 b = *dp++;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (b == 0x1B) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (dp >= endp || *dp == 0x1B || *dp == '\n' ||
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 *dp == '\r') {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 putc('\\', outf);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 putc('e', outf);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 b = *dp++;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 c = escape_table[b];
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (!c) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 fprintf(outf, "\\e\\%02X", b);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 } else {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 c = basic_table[b];
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (!c) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 fprintf(outf, "\\%02X", b);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (c & 0x80) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 putc('\\', outf);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 c &= 0x7F;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 putc(c, outf);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 putc('"', outf);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }