FreeCalypso > hg > sms-coding-utils
comparison libcoding/utf8_decode2.c @ 0:2d0082216916
libcoding: beginning with a subset of uptools version
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sat, 05 Aug 2023 00:46:23 +0000 |
| parents | |
| children | e56bb9f09ff1 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:2d0082216916 |
|---|---|
| 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 } |
