annotate libcoding/ucs2_decode.c @ 33:a91fb88a57b2 default tip

add INSTALL document
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 14 Jun 2024 20:29:17 +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 conversion of UCS2-encoded data
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * (typically received in SMS) 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 * maintaining parallelism with the corresponding function for decoding
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * GSM7-encoded data.
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 ucs2_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
12 u_char *inbuf, *outbuf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 unsigned inlen, *outlenp;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 u_char *inp, *endp, *outp;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 unsigned uni;
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 inp = inbuf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 endp = inbuf + (inlen & ~1);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 outp = outbuf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 while (inp < endp) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if ((endp - inp) >= 4 && (inp[0] & 0xFC) == 0xD8 &&
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 (inp[2] & 0xFC) == 0xDC) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 uni = ((inp[0] & 3) << 18) | (inp[1] << 10) |
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 ((inp[2] & 3) << 8) | inp[3];
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 inp += 4;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 uni += 0x10000;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (ascii_ext == 2)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 outp += emit_utf8_char(uni, outp);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 else {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 sprintf(outp, "\\U%06X", uni);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 outp += 8;
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 continue;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 uni = (inp[0] << 8) | inp[1];
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 inp += 2;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (uni == '\\') {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 } else if (uni == '\r') {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 *outp++ = 'r';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 } else if (uni == '\n') {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (newline_ok)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 *outp++ = '\n';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 else {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 *outp++ = '\\';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 *outp++ = 'n';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 } else if (!is_decoded_char_ok(uni, ascii_ext)) {
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 sprintf(outp, "\\u%04X", uni);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 outp += 6;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 } else if (ascii_ext == 2)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 outp += emit_utf8_char(uni, outp);
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 else
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 *outp++ = uni;
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 *outp = '\0';
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (outlenp)
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 *outlenp = outp - outbuf;
7418ca2e9949 libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }