Add pancyrillic font
[platform/upstream/kbd.git] / src / utf8.c
1 /* utf8.c - collect routines for conversion to/from utf8 */
2 #include "utf8.h"
3
4 /*
5  * Convert utf8 to long.
6  * On success: update *inptr to be the first nonread character,
7  *   set *err to 0, and return the obtained value.
8  * On failure: leave *inptr unchanged, set *err to some nonzero error value:
9  *   UTF8_BAD: bad utf8, UTF8_SHORT: input too short
10  *   and return 0;
11  *
12  * cnt is either 0 or gives the number of available bytes
13  */
14 unsigned long
15 from_utf8(char **inptr, int cnt, int *err) {
16         unsigned char *in;
17         unsigned int uc, uc2;
18         int need, bit, bad = 0;
19
20         in = (unsigned char *)(* inptr);
21         uc = *in++;
22         need = 0;
23         bit = 0x80;
24         while(uc & bit) {
25                 need++;
26                 bit >>= 1;
27         }
28         uc &= (bit-1);
29         if (cnt && cnt < need) {
30                 *err = UTF8_SHORT;
31                 return 0;
32         }
33         if (need == 1)
34                 bad = 1;
35         else if (need) while(--need) {
36                 uc2 = *in++;
37                 if ((uc2 & 0xc0) != 0x80) {
38                         bad = 1;
39                         break;
40                 }
41                 uc = ((uc << 6) | (uc2 & 0x3f));
42         }
43         if (bad) {
44                 *err = UTF8_BAD;
45                 return 0;
46         }
47         *inptr = (char *)in;
48         *err = 0;
49         return uc;
50 }