winelib: Replace inline static with static inline.
[wine] / libs / wine / utf8.c
1 /*
2  * UTF-8 support routines
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <string.h>
22
23 #include "wine/unicode.h"
24
25 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
26 static const char utf8_length[128] =
27 {
28     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
29     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
30     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
31     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
32     0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
33     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
34     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
35     3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0  /* 0xf0-0xff */
36 };
37
38 /* first byte mask depending on UTF-8 sequence length */
39 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
40
41 /* minimum Unicode value depending on UTF-8 sequence length */
42 static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
43
44
45 /* get the next char value taking surrogates into account */
46 static inline unsigned int get_surrogate_value( const WCHAR *src, unsigned int srclen )
47 {
48     if (src[0] >= 0xd800 && src[0] <= 0xdfff)  /* surrogate pair */
49     {
50         if (src[0] > 0xdbff || /* invalid high surrogate */
51             srclen <= 1 ||     /* missing low surrogate */
52             src[1] < 0xdc00 || src[1] > 0xdfff) /* invalid low surrogate */
53             return 0;
54         return 0x10000 + ((src[0] & 0x3ff) << 10) + (src[1] & 0x3ff);
55     }
56     return src[0];
57 }
58
59 /* query necessary dst length for src string */
60 static inline int get_length_wcs_utf8( int flags, const WCHAR *src, unsigned int srclen )
61 {
62     int len;
63     unsigned int val;
64
65     for (len = 0; srclen; srclen--, src++)
66     {
67         if (*src < 0x80)  /* 0x00-0x7f: 1 byte */
68         {
69             len++;
70             continue;
71         }
72         if (*src < 0x800)  /* 0x80-0x7ff: 2 bytes */
73         {
74             len += 2;
75             continue;
76         }
77         if (!(val = get_surrogate_value( src, srclen )))
78         {
79             if (flags & WC_ERR_INVALID_CHARS) return -2;
80             continue;
81         }
82         if (val < 0x10000)  /* 0x800-0xffff: 3 bytes */
83             len += 3;
84         else   /* 0x10000-0x10ffff: 4 bytes */
85             len += 4;
86     }
87     return len;
88 }
89
90 /* wide char to UTF-8 string conversion */
91 /* return -1 on dst buffer overflow, -2 on invalid input char */
92 int wine_utf8_wcstombs( int flags, const WCHAR *src, int srclen, char *dst, int dstlen )
93 {
94     int len;
95
96     if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
97
98     for (len = dstlen; srclen; srclen--, src++)
99     {
100         WCHAR ch = *src;
101         unsigned int val;
102
103         if (ch < 0x80)  /* 0x00-0x7f: 1 byte */
104         {
105             if (!len--) return -1;  /* overflow */
106             *dst++ = ch;
107             continue;
108         }
109
110         if (ch < 0x800)  /* 0x80-0x7ff: 2 bytes */
111         {
112             if ((len -= 2) < 0) return -1;  /* overflow */
113             dst[1] = 0x80 | (ch & 0x3f);
114             ch >>= 6;
115             dst[0] = 0xc0 | ch;
116             dst += 2;
117             continue;
118         }
119
120         if (!(val = get_surrogate_value( src, srclen )))
121         {
122             if (flags & WC_ERR_INVALID_CHARS) return -2;
123             continue;
124         }
125
126         if (val < 0x10000)  /* 0x800-0xffff: 3 bytes */
127         {
128             if ((len -= 3) < 0) return -1;  /* overflow */
129             dst[2] = 0x80 | (val & 0x3f);
130             val >>= 6;
131             dst[1] = 0x80 | (val & 0x3f);
132             val >>= 6;
133             dst[0] = 0xe0 | val;
134             dst += 3;
135         }
136         else   /* 0x10000-0x10ffff: 4 bytes */
137         {
138             if ((len -= 4) < 0) return -1;  /* overflow */
139             dst[3] = 0x80 | (val & 0x3f);
140             val >>= 6;
141             dst[2] = 0x80 | (val & 0x3f);
142             val >>= 6;
143             dst[1] = 0x80 | (val & 0x3f);
144             val >>= 6;
145             dst[0] = 0xf0 | val;
146             dst += 4;
147         }
148     }
149     return dstlen - len;
150 }
151
152 /* query necessary dst length for src string */
153 static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
154 {
155     int len, ret = 0;
156     unsigned int res;
157     const char *srcend = src + srclen;
158
159     while (src < srcend)
160     {
161         unsigned char ch = *src++;
162         if (ch < 0x80)  /* special fast case for 7-bit ASCII */
163         {
164             ret++;
165             continue;
166         }
167         len = utf8_length[ch-0x80];
168         if (src + len > srcend) goto bad;
169         res = ch & utf8_mask[len];
170
171         switch(len)
172         {
173         case 3:
174             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
175             res = (res << 6) | ch;
176             src++;
177         case 2:
178             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
179             res = (res << 6) | ch;
180             src++;
181         case 1:
182             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
183             res = (res << 6) | ch;
184             src++;
185             if (res < utf8_minval[len]) goto bad;
186             if (res > 0x10ffff) goto bad;
187             if (res > 0xffff) ret++;
188             ret++;
189             continue;
190         }
191     bad:
192         if (flags & MB_ERR_INVALID_CHARS) return -2;  /* bad char */
193         /* otherwise ignore it */
194     }
195     return ret;
196 }
197
198 /* UTF-8 to wide char string conversion */
199 /* return -1 on dst buffer overflow, -2 on invalid input char */
200 int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
201 {
202     int len;
203     unsigned int res;
204     const char *srcend = src + srclen;
205     WCHAR *dstend = dst + dstlen;
206
207     if (!dstlen) return get_length_mbs_utf8( flags, src, srclen );
208
209     while ((dst < dstend) && (src < srcend))
210     {
211         unsigned char ch = *src++;
212         if (ch < 0x80)  /* special fast case for 7-bit ASCII */
213         {
214             *dst++ = ch;
215             continue;
216         }
217         len = utf8_length[ch-0x80];
218         if (src + len > srcend) goto bad;
219         res = ch & utf8_mask[len];
220
221         switch(len)
222         {
223         case 3:
224             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
225             res = (res << 6) | ch;
226             src++;
227         case 2:
228             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
229             res = (res << 6) | ch;
230             src++;
231         case 1:
232             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
233             res = (res << 6) | ch;
234             src++;
235             if (res < utf8_minval[len]) goto bad;
236             if (res > 0x10ffff) goto bad;
237             if (res <= 0xffff) *dst++ = res;
238             else /* we need surrogates */
239             {
240                 if (dst == dstend - 1) return -1;  /* overflow */
241                 res -= 0x10000;
242                 *dst++ = 0xd800 | (res >> 10);
243                 *dst++ = 0xdc00 | (res & 0x3ff);
244             }
245             continue;
246         }
247     bad:
248         if (flags & MB_ERR_INVALID_CHARS) return -2;  /* bad char */
249         /* otherwise ignore it */
250     }
251     if (src < srcend) return -1;  /* overflow */
252     return dstlen - (dstend - dst);
253 }