0adf01b5a1ee2414fa8bd6fcacc9dfafa4ab1afb
[jansson.git] / src / utf.c
1 #include <string.h>
2
3 int utf8_encode(int codepoint, char *buffer, int *size)
4 {
5     if(codepoint < 0)
6         return -1;
7     else if(codepoint < 0x80)
8     {
9         buffer[0] = (char)codepoint;
10         *size = 1;
11     }
12     else if(codepoint < 0x800)
13     {
14         buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6);
15         buffer[1] = 0x80 + ((codepoint & 0x03F));
16         *size = 2;
17     }
18     else if(codepoint < 0x10000)
19     {
20         buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12);
21         buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6);
22         buffer[2] = 0x80 + ((codepoint & 0x003F));
23         *size = 3;
24     }
25     else if(codepoint <= 0x10FFFF)
26     {
27         buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18);
28         buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12);
29         buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6);
30         buffer[3] = 0x80 + ((codepoint & 0x00003F));
31         *size = 4;
32     }
33     else
34         return -1;
35
36     return 0;
37 }
38
39 int utf8_check_first(char byte)
40 {
41     unsigned char u = (unsigned char)byte;
42
43     if(u < 0x80)
44         return 1;
45
46     if(0x80 <= u && u <= 0xBF) {
47         /* second, third or fourth byte of a multi-byte
48            sequence, i.e. a "continuation byte" */
49         return 0;
50     }
51     else if(u == 0xC0 || u == 0xC1) {
52         /* overlong encoding of an ASCII byte */
53         return 0;
54     }
55     else if(0xC2 <= u && u <= 0xDF) {
56         /* 2-byte sequence */
57         return 2;
58     }
59
60     else if(0xE0 <= u && u <= 0xEF) {
61         /* 3-byte sequence */
62         return 3;
63     }
64     else if(0xF0 <= u && u <= 0xF4) {
65         /* 4-byte sequence */
66         return 4;
67     }
68     else { /* u >= 0xF5 */
69         /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
70            UTF-8 */
71         return 0;
72     }
73 }
74
75 int utf8_check_full(const char *buffer, int size)
76 {
77     int i, value = 0;
78     unsigned char u = (unsigned char)buffer[0];
79
80     if(size == 2)
81     {
82         value = u & 0x1F;
83     }
84     else if(size == 3)
85     {
86         value = u & 0xF;
87     }
88     else if(size == 4)
89     {
90         value = u & 0x7;
91     }
92     else
93         return 0;
94
95     for(i = 1; i < size; i++)
96     {
97         u = (unsigned char)buffer[i];
98
99         if(u < 0x80 || u > 0xBF) {
100             /* not a continuation byte */
101             return 0;
102         }
103
104         value = (value << 6) + (u & 0x3F);
105     }
106
107     if(value > 0x10FFFF) {
108         /* not in Unicode range */
109         return 0;
110     }
111
112     else if(0xD800 <= value && value <= 0xDFFF) {
113         /* invalid code point (UTF-16 surrogate halves) */
114         return 0;
115     }
116
117     else if((size == 2 && value < 0x80) ||
118             (size == 3 && value < 0x800) ||
119             (size == 4 && value < 0x10000)) {
120         /* overlong encoding */
121         return 0;
122     }
123
124     return 1;
125 }
126
127 int utf8_check_string(const char *string, int length)
128 {
129     int i;
130
131     if(length == -1)
132         length = strlen(string);
133
134     for(i = 0; i < length; i++)
135     {
136         int count = utf8_check_first(string[i]);
137         if(count == 0)
138             return 0;
139         else if(count > 1)
140         {
141             if(i + count > length)
142                 return 0;
143
144             if(!utf8_check_full(&string[i], count))
145                 return 0;
146
147             i += count - 1;
148         }
149     }
150
151     return 1;
152 }