comparison libcoding/ucs2_decode.c @ 27:7418ca2e9949

libcoding: add functions from freecalypso-tools/uptools/libcoding that are needed for sms-pdu-decode & pcm-sms-decode
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 Jun 2024 02:29:29 +0000
parents
children
comparison
equal deleted inserted replaced
26:c8cb05b69118 27:7418ca2e9949
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 #include <stdio.h>
10
11 ucs2_to_ascii_or_ext(inbuf, inlen, outbuf, outlenp, ascii_ext, newline_ok)
12 u_char *inbuf, *outbuf;
13 unsigned inlen, *outlenp;
14 {
15 u_char *inp, *endp, *outp;
16 unsigned uni;
17
18 inp = inbuf;
19 endp = inbuf + (inlen & ~1);
20 outp = outbuf;
21 while (inp < endp) {
22 if ((endp - inp) >= 4 && (inp[0] & 0xFC) == 0xD8 &&
23 (inp[2] & 0xFC) == 0xDC) {
24 uni = ((inp[0] & 3) << 18) | (inp[1] << 10) |
25 ((inp[2] & 3) << 8) | inp[3];
26 inp += 4;
27 uni += 0x10000;
28 if (ascii_ext == 2)
29 outp += emit_utf8_char(uni, outp);
30 else {
31 sprintf(outp, "\\U%06X", uni);
32 outp += 8;
33 }
34 continue;
35 }
36 uni = (inp[0] << 8) | inp[1];
37 inp += 2;
38 if (uni == '\\') {
39 *outp++ = '\\';
40 *outp++ = '\\';
41 } else if (uni == '\r') {
42 *outp++ = '\\';
43 *outp++ = 'r';
44 } else if (uni == '\n') {
45 if (newline_ok)
46 *outp++ = '\n';
47 else {
48 *outp++ = '\\';
49 *outp++ = 'n';
50 }
51 } else if (!is_decoded_char_ok(uni, ascii_ext)) {
52 sprintf(outp, "\\u%04X", uni);
53 outp += 6;
54 } else if (ascii_ext == 2)
55 outp += emit_utf8_char(uni, outp);
56 else
57 *outp++ = uni;
58 }
59 *outp = '\0';
60 if (outlenp)
61 *outlenp = outp - outbuf;
62 }