comparison uptools/libcoding/ucs2_decode.c @ 328:978571e23318

uptools started with libcoding
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 03 Feb 2018 20:07:05 +0000
parents
children 1c599681fd60
comparison
equal deleted inserted replaced
327:973d885a68a0 328:978571e23318
1 /*
2 * This library module implements the conversion of UCS2-encoded data
3 * (typically received in SMS) into ASCII, ISO 8859-1 or UTF-8,
4 * maintaining parallelism with the corresponding function for decoding
5 * GSM7-encoded data.
6 */
7
8 #include <sys/types.h>
9
10 ucs2_to_ascii_or_ext(inbuf, inlen, outbuf, outlenp, ascii_ext, newline_ok, errp)
11 u_char *inbuf, *outbuf;
12 unsigned inlen, *outlenp, *errp;
13 {
14 u_char *inp, *endp, *outp;
15 unsigned errcnt = 0;
16 unsigned uni;
17
18 inp = inbuf;
19 endp = inbuf + (inlen & ~1);
20 outp = outbuf;
21 while (inp < endp) {
22 uni = (inp[0] << 8) | inp[1];
23 inp += 2;
24 if (uni == '\r') {
25 *outp++ = '\\';
26 *outp++ = 'r';
27 errcnt++;
28 } else if (uni == '\n') {
29 if (newline_ok)
30 *outp++ = '\n';
31 else {
32 *outp++ = '\\';
33 *outp++ = 'n';
34 errcnt++;
35 }
36 } else if (!is_decoded_char_ok(uni, ascii_ext)) {
37 *outp++ = '?';
38 errcnt++;
39 } else if (ascii_ext == 2)
40 outp += emit_utf8_char(uni, outp);
41 else
42 *outp++ = uni;
43 }
44 *outp = '\0';
45 if (outlenp)
46 *outlenp = outp - outbuf;
47 if (errp)
48 *errp = errcnt;
49 }