annotate libcoding/gsm7_decode.c @ 29:aae078d9eaa6

immigrate sms-pdu-decode and pcm-sms-decode here
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 Jun 2024 02:39:21 +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 library module implements the decoding of GSM7-encoded data
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * into ASCII, ISO 8859-1 or UTF-8.
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 extern u_short gsm7_decode_table[128];
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 extern u_short gsm7ext_decode_table[128];
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 gsm7_to_ascii_or_ext(inbuf, inlen, outbuf, outlenp, ascii_ext, newline_ok)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 u_char *inbuf, *outbuf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 unsigned inlen, *outlenp;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 u_char *inp, *endp, *outp;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 unsigned gsm, uni;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 int is_ext;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 inp = inbuf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 endp = inbuf + inlen;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 outp = outbuf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 while (inp < endp) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 gsm = *inp++;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (gsm == 0x1B && inp < endp && *inp != 0x1B && *inp != '\n'
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 && *inp != '\r') {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 gsm = *inp++;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 uni = gsm7ext_decode_table[gsm];
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (uni == '\\') {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (uni == 0x20AC && ascii_ext < 2) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 *outp++ = 'E';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 is_ext = 1;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 } else {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 switch (gsm) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 case 0x1B:
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 *outp++ = 'e';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 case '\n':
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (newline_ok)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 *outp++ = '\n';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 else {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 *outp++ = 'n';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 case '\r':
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 *outp++ = 'r';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 uni = gsm7_decode_table[gsm];
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 is_ext = 0;
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 if (!uni || !is_decoded_char_ok(uni, ascii_ext)) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (is_ext) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 *outp++ = 'e';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 sprintf(outp, "\\%02X", gsm);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 outp += 3;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 } else if (ascii_ext == 2)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 outp += emit_utf8_char(uni, outp);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 else
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 *outp++ = uni;
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 *outp = '\0';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (outlenp)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 *outlenp = outp - outbuf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }