annotate uptools/libcoding/gsm7_decode.c @ 342:c6165837d141

uptools/Makefile: atinterf added
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 04 Feb 2018 07:57:06 +0000
parents 978571e23318
children 1c599681fd60
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>
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 extern u_short gsm7_decode_table[128];
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 extern u_short gsm7ext_decode_table[128];
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 gsm7_to_ascii_or_ext(inbuf, inlen, outbuf, outlenp, ascii_ext, newline_ok, errp)
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 u_char *inbuf, *outbuf;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 unsigned inlen, *outlenp, *errp;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 {
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 u_char *inp, *endp, *outp;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 unsigned errcnt = 0;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 unsigned gsm, uni;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 inp = inbuf;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 endp = inbuf + inlen;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 outp = outbuf;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 while (inp < endp) {
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 gsm = *inp++;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 if (gsm == 0x1B && inp < endp)
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 uni = gsm7ext_decode_table[*inp++];
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 else
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 uni = gsm7_decode_table[gsm];
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (uni == '\r') {
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 *outp++ = '\\';
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 *outp++ = 'r';
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 errcnt++;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 } else if (uni == '\n') {
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (newline_ok)
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 *outp++ = '\n';
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 else {
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 *outp++ = '\\';
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 *outp++ = 'n';
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 errcnt++;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 } else if (!uni || !is_decoded_char_ok(uni, ascii_ext)) {
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 *outp++ = '?';
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 errcnt++;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 } else if (ascii_ext == 2)
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 outp += emit_utf8_char(uni, outp);
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 else
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 *outp++ = uni;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 *outp = '\0';
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (outlenp)
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 *outlenp = outp - outbuf;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (errp)
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 *errp = errcnt;
978571e23318 uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }