FreeCalypso > hg > freecalypso-tools
annotate uptools/libcoding/ucs2_decode.c @ 926:6a0aa8d36d06
rvinterf backslash escape: introduce libprint
The new helper function library named libprint is meant to replace
the badly misnamed libg23, and will soon contain functions for
printing all of the same kinds of GPF TST packets that are now handled
in libg23. However, we are also moving safe_print_trace() from libasync
to this new library, and changing it to emit our new backslash escape
format.
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Tue, 23 May 2023 03:47:46 +0000 |
| parents | 30fbaa652ea5 |
| children |
| 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 conversion of UCS2-encoded data |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * (typically received in SMS) into ASCII, ISO 8859-1 or UTF-8, |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * maintaining parallelism with the corresponding function for decoding |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * GSM7-encoded data. |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 */ |
|
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 #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
|
9 #include <stdio.h> |
|
328
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
|
804
30fbaa652ea5
pcm-sms-decode & sms-pdu-decode: rm remains of bad chars count
Mychaela Falconia <falcon@freecalypso.org>
parents:
802
diff
changeset
|
11 ucs2_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
|
12 u_char *inbuf, *outbuf; |
|
804
30fbaa652ea5
pcm-sms-decode & sms-pdu-decode: rm remains of bad chars count
Mychaela Falconia <falcon@freecalypso.org>
parents:
802
diff
changeset
|
13 unsigned inlen, *outlenp; |
|
328
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 uni; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 inp = inbuf; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 endp = inbuf + (inlen & ~1); |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 outp = outbuf; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 while (inp < endp) { |
|
802
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
22 if ((endp - inp) >= 4 && (inp[0] & 0xFC) == 0xD8 && |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
23 (inp[2] & 0xFC) == 0xDC) { |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
24 uni = ((inp[0] & 3) << 18) | (inp[1] << 10) | |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
25 ((inp[2] & 3) << 8) | inp[3]; |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
26 inp += 4; |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
27 uni += 0x10000; |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
28 if (ascii_ext == 2) |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
29 outp += emit_utf8_char(uni, outp); |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
30 else { |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
31 sprintf(outp, "\\U%06X", uni); |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
32 outp += 8; |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
33 } |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
34 continue; |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
35 } |
|
328
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 uni = (inp[0] << 8) | inp[1]; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 inp += 2; |
|
802
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
38 if (uni == '\\') { |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
39 *outp++ = '\\'; |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
40 *outp++ = '\\'; |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
41 } else if (uni == '\r') { |
|
328
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 *outp++ = '\\'; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 *outp++ = 'r'; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 } else if (uni == '\n') { |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 if (newline_ok) |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 *outp++ = '\n'; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 else { |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 *outp++ = '\\'; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 *outp++ = 'n'; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 } else if (!is_decoded_char_ok(uni, ascii_ext)) { |
|
802
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
52 sprintf(outp, "\\u%04X", uni); |
|
1c599681fd60
pcm-sms-decode & sms-pdu-decode: revamp bad char decoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
328
diff
changeset
|
53 outp += 6; |
|
328
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 } else if (ascii_ext == 2) |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 outp += emit_utf8_char(uni, outp); |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 else |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 *outp++ = uni; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 *outp = '\0'; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 if (outlenp) |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 *outlenp = outp - outbuf; |
|
978571e23318
uptools started with libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 } |
