annotate uptools/libcoding/gsm7_decode.c @ 904:5041bcb8140f

tchtools/fc-vm2hex.c: update header comment for new situation
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 28 Dec 2022 08:41:57 +0000
parents 30fbaa652ea5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This library module implements the decoding of GSM7-encoded data
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * into ASCII, ISO 8859-1 or UTF-8.
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
802
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
7 #include <stdio.h>
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 extern u_short gsm7_decode_table[128];
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 extern u_short gsm7ext_decode_table[128];
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
804
30fbaa652ea5 pcm-sms-decode & sms-pdu-decode: rm remains of bad chars count
Mychaela Falconia <falcon@freecalypso.org>
parents: 803
diff changeset
12 gsm7_to_ascii_or_ext(inbuf, inlen, outbuf, outlenp, ascii_ext, newline_ok)
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 u_char *inbuf, *outbuf;
804
30fbaa652ea5 pcm-sms-decode & sms-pdu-decode: rm remains of bad chars count
Mychaela Falconia <falcon@freecalypso.org>
parents: 803
diff changeset
14 unsigned inlen, *outlenp;
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 {
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 u_char *inp, *endp, *outp;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 unsigned gsm, uni;
802
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
18 int is_ext;
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 inp = inbuf;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 endp = inbuf + inlen;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 outp = outbuf;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 while (inp < endp) {
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 gsm = *inp++;
802
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
25 if (gsm == 0x1B && inp < endp && *inp != 0x1B && *inp != '\n'
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
26 && *inp != '\r') {
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
27 gsm = *inp++;
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
28 uni = gsm7ext_decode_table[gsm];
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
29 if (uni == '\\') {
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
30 *outp++ = '\\';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
31 *outp++ = '\\';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
32 continue;
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
33 }
803
5637794913a8 pcm-sms-decode & sms-pdu-decode: decode \E for Euro symbol
Mychaela Falconia <falcon@freecalypso.org>
parents: 802
diff changeset
34 if (uni == 0x20AC && ascii_ext < 2) {
5637794913a8 pcm-sms-decode & sms-pdu-decode: decode \E for Euro symbol
Mychaela Falconia <falcon@freecalypso.org>
parents: 802
diff changeset
35 *outp++ = '\\';
5637794913a8 pcm-sms-decode & sms-pdu-decode: decode \E for Euro symbol
Mychaela Falconia <falcon@freecalypso.org>
parents: 802
diff changeset
36 *outp++ = 'E';
5637794913a8 pcm-sms-decode & sms-pdu-decode: decode \E for Euro symbol
Mychaela Falconia <falcon@freecalypso.org>
parents: 802
diff changeset
37 continue;
5637794913a8 pcm-sms-decode & sms-pdu-decode: decode \E for Euro symbol
Mychaela Falconia <falcon@freecalypso.org>
parents: 802
diff changeset
38 }
802
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
39 is_ext = 1;
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
40 } else {
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
41 switch (gsm) {
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
42 case 0x1B:
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
43 *outp++ = '\\';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
44 *outp++ = 'e';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
45 continue;
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
46 case '\n':
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
47 if (newline_ok)
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
48 *outp++ = '\n';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
49 else {
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
50 *outp++ = '\\';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
51 *outp++ = 'n';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
52 }
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
53 continue;
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
54 case '\r':
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
55 *outp++ = '\\';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
56 *outp++ = 'r';
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
57 continue;
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
58 }
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 uni = gsm7_decode_table[gsm];
802
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
60 is_ext = 0;
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
61 }
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
62 if (!uni || !is_decoded_char_ok(uni, ascii_ext)) {
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
63 if (is_ext) {
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 *outp++ = '\\';
802
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
65 *outp++ = 'e';
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
802
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
67 sprintf(outp, "\\%02X", gsm);
1c599681fd60 pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 328
diff changeset
68 outp += 3;
328
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 } else if (ascii_ext == 2)
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 outp += emit_utf8_char(uni, outp);
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 else
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 *outp++ = uni;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 *outp = '\0';
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (outlenp)
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 *outlenp = outp - outbuf;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }