comparison uptools/libcoding/utf8_decode2.c @ 376:83c755829e31

uptools/libcoding: added function for turning UTF-8 into UCS-2
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 08 Mar 2018 23:06:31 +0000
parents
children 6bf473f77fc4
comparison
equal deleted inserted replaced
375:759cb6dc501b 376:83c755829e31
1 /*
2 * This library module implements the function for converting UTF-8 input
3 * to UCS-2 in outgoing SMS composition.
4 */
5
6 #include <sys/types.h>
7
8 utf8_to_ucs2(inbuf, outbuf, outmax, outlenp)
9 u_char *inbuf;
10 u_short *outbuf;
11 unsigned outmax, *outlenp;
12 {
13 u_char *ip = inbuf;
14 u_short *op = outbuf;
15 unsigned outcnt = 0, c, n, uni;
16
17 while (c = *ip++) {
18 if (c < 0x80) {
19 uni = c;
20 goto gotuni;
21 }
22 if (c < 0xC0 || c > 0xEF)
23 return(-1);
24 uni = c & 0x1F;
25 if (c >= 0xE0)
26 n = 2;
27 else
28 n = 1;
29 for (; n; n--) {
30 c = *ip++;
31 if (c < 0x80 || c > 0xBF)
32 return(-1);
33 uni <<= 6;
34 uni |= c & 0x3F;
35 }
36 gotuni: if (outcnt >= outmax)
37 return(-2);
38 *op++ = uni;
39 outcnt++;
40 }
41 *outlenp = outcnt;
42 return(0);
43 }