comparison uptools/libcoding/utf8_decode2.c @ 967:6bf473f77fc4

fcup-smsend: support backslash escapes in UCS-2 mode too
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 01 Sep 2023 16:43:35 +0000
parents 83c755829e31
children
comparison
equal deleted inserted replaced
966:ec7e23d5151f 967:6bf473f77fc4
2 * This library module implements the function for converting UTF-8 input 2 * This library module implements the function for converting UTF-8 input
3 * to UCS-2 in outgoing SMS composition. 3 * to UCS-2 in outgoing SMS composition.
4 */ 4 */
5 5
6 #include <sys/types.h> 6 #include <sys/types.h>
7 #include <ctype.h>
7 8
8 utf8_to_ucs2(inbuf, outbuf, outmax, outlenp) 9 static int
10 handle_escape(ipp, outp)
11 u_char **ipp;
12 unsigned *outp;
13 {
14 unsigned c, n, acc;
15
16 c = *(*ipp)++;
17 switch (c) {
18 case '"':
19 case '\\':
20 *outp = c;
21 return(0);
22 case 'n':
23 *outp = '\n';
24 return(0);
25 case 'r':
26 *outp = '\r';
27 return(0);
28 case 'u':
29 acc = 0;
30 for (n = 0; n < 4; n++) {
31 c = *(*ipp)++;
32 if (!isxdigit(c))
33 return(-3);
34 acc <<= 4;
35 acc |= decode_hex_digit(c);
36 }
37 *outp = acc;
38 return(0);
39 default:
40 return(-3);
41 }
42 }
43
44 utf8_to_ucs2(inbuf, outbuf, outmax, outlenp, allow_escape)
9 u_char *inbuf; 45 u_char *inbuf;
10 u_short *outbuf; 46 u_short *outbuf;
11 unsigned outmax, *outlenp; 47 unsigned outmax, *outlenp;
12 { 48 {
13 u_char *ip = inbuf; 49 u_char *ip = inbuf;
14 u_short *op = outbuf; 50 u_short *op = outbuf;
15 unsigned outcnt = 0, c, n, uni; 51 unsigned outcnt = 0, c, n, uni;
52 int rc;
16 53
17 while (c = *ip++) { 54 while (c = *ip++) {
55 if (c == '\\' && allow_escape) {
56 rc = handle_escape(&ip, &uni);
57 if (rc < 0)
58 return(rc);
59 goto gotuni;
60 }
18 if (c < 0x80) { 61 if (c < 0x80) {
19 uni = c; 62 uni = c;
20 goto gotuni; 63 goto gotuni;
21 } 64 }
22 if (c < 0xC0 || c > 0xEF) 65 if (c < 0xC0 || c > 0xEF)