d3dx9/tests: Add basic tests for ID3DXRenderToEnvMap.
[wine/multimedia.git] / libs / wine / utf8.c
blobc95bc5191556da3ef541cac9f922d978b2e6e431
1 /*
2 * UTF-8 support routines
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include <string.h>
23 #include "wine/unicode.h"
25 extern WCHAR compose( const WCHAR *str );
27 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
28 static const char utf8_length[128] =
30 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
31 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
32 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
33 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
34 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
35 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
36 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
37 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
40 /* first byte mask depending on UTF-8 sequence length */
41 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
43 /* minimum Unicode value depending on UTF-8 sequence length */
44 static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
47 /* get the next char value taking surrogates into account */
48 static inline unsigned int get_surrogate_value( const WCHAR *src, unsigned int srclen )
50 if (src[0] >= 0xd800 && src[0] <= 0xdfff) /* surrogate pair */
52 if (src[0] > 0xdbff || /* invalid high surrogate */
53 srclen <= 1 || /* missing low surrogate */
54 src[1] < 0xdc00 || src[1] > 0xdfff) /* invalid low surrogate */
55 return 0;
56 return 0x10000 + ((src[0] & 0x3ff) << 10) + (src[1] & 0x3ff);
58 return src[0];
61 /* query necessary dst length for src string */
62 static inline int get_length_wcs_utf8( int flags, const WCHAR *src, unsigned int srclen )
64 int len;
65 unsigned int val;
67 for (len = 0; srclen; srclen--, src++)
69 if (*src < 0x80) /* 0x00-0x7f: 1 byte */
71 len++;
72 continue;
74 if (*src < 0x800) /* 0x80-0x7ff: 2 bytes */
76 len += 2;
77 continue;
79 if (!(val = get_surrogate_value( src, srclen )))
81 if (flags & WC_ERR_INVALID_CHARS) return -2;
82 continue;
84 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
85 len += 3;
86 else /* 0x10000-0x10ffff: 4 bytes */
88 len += 4;
89 src++;
90 srclen--;
93 return len;
96 /* wide char to UTF-8 string conversion */
97 /* return -1 on dst buffer overflow, -2 on invalid input char */
98 int wine_utf8_wcstombs( int flags, const WCHAR *src, int srclen, char *dst, int dstlen )
100 int len;
102 if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
104 for (len = dstlen; srclen; srclen--, src++)
106 WCHAR ch = *src;
107 unsigned int val;
109 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
111 if (!len--) return -1; /* overflow */
112 *dst++ = ch;
113 continue;
116 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
118 if ((len -= 2) < 0) return -1; /* overflow */
119 dst[1] = 0x80 | (ch & 0x3f);
120 ch >>= 6;
121 dst[0] = 0xc0 | ch;
122 dst += 2;
123 continue;
126 if (!(val = get_surrogate_value( src, srclen )))
128 if (flags & WC_ERR_INVALID_CHARS) return -2;
129 continue;
132 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
134 if ((len -= 3) < 0) return -1; /* overflow */
135 dst[2] = 0x80 | (val & 0x3f);
136 val >>= 6;
137 dst[1] = 0x80 | (val & 0x3f);
138 val >>= 6;
139 dst[0] = 0xe0 | val;
140 dst += 3;
142 else /* 0x10000-0x10ffff: 4 bytes */
144 if ((len -= 4) < 0) return -1; /* overflow */
145 dst[3] = 0x80 | (val & 0x3f);
146 val >>= 6;
147 dst[2] = 0x80 | (val & 0x3f);
148 val >>= 6;
149 dst[1] = 0x80 | (val & 0x3f);
150 val >>= 6;
151 dst[0] = 0xf0 | val;
152 dst += 4;
153 src++;
154 srclen--;
157 return dstlen - len;
160 /* helper for the various utf8 mbstowcs functions */
161 static inline unsigned int decode_utf8_char( unsigned char ch, const char **str, const char *strend )
163 unsigned int len = utf8_length[ch-0x80];
164 unsigned int res = ch & utf8_mask[len];
165 const char *end = *str + len;
167 if (end > strend) return ~0;
168 switch(len)
170 case 3:
171 if ((ch = end[-3] ^ 0x80) >= 0x40) break;
172 res = (res << 6) | ch;
173 (*str)++;
174 case 2:
175 if ((ch = end[-2] ^ 0x80) >= 0x40) break;
176 res = (res << 6) | ch;
177 (*str)++;
178 case 1:
179 if ((ch = end[-1] ^ 0x80) >= 0x40) break;
180 res = (res << 6) | ch;
181 (*str)++;
182 if (res < utf8_minval[len]) break;
183 return res;
185 return ~0;
188 /* query necessary dst length for src string with composition */
189 static inline int get_length_mbs_utf8_compose( int flags, const char *src, int srclen )
191 int ret = 0;
192 unsigned int res;
193 WCHAR composed[2];
194 const char *srcend = src + srclen;
196 composed[0] = 0;
197 while (src < srcend)
199 unsigned char ch = *src++;
200 if (ch < 0x80) /* special fast case for 7-bit ASCII */
202 composed[0] = ch;
203 ret++;
204 continue;
206 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
208 if (composed[0])
210 composed[1] = res;
211 if ((composed[0] = compose( composed ))) continue;
213 composed[0] = res;
214 ret++;
216 else if (res <= 0x10ffff)
218 ret += 2;
219 composed[0] = 0; /* no composition for surrogates */
221 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
222 /* otherwise ignore it */
224 return ret;
227 /* UTF-8 to wide char string conversion with composition */
228 /* return -1 on dst buffer overflow, -2 on invalid input char */
229 static int utf8_mbstowcs_compose( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
231 unsigned int res;
232 const char *srcend = src + srclen;
233 WCHAR composed[2];
234 WCHAR *dstend = dst + dstlen;
236 if (!dstlen) return get_length_mbs_utf8_compose( flags, src, srclen );
238 composed[0] = 0;
239 while (src < srcend)
241 unsigned char ch = *src++;
242 if (ch < 0x80) /* special fast case for 7-bit ASCII */
244 if (dst >= dstend) return -1; /* overflow */
245 *dst++ = composed[0] = ch;
246 continue;
248 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
250 if (composed[0])
252 composed[1] = res;
253 if ((composed[0] = compose( composed )))
255 dst[-1] = composed[0];
256 continue;
259 if (dst >= dstend) return -1; /* overflow */
260 *dst++ = composed[0] = res;
262 else if (res <= 0x10ffff) /* we need surrogates */
264 if (dst >= dstend - 1) return -1; /* overflow */
265 res -= 0x10000;
266 *dst++ = 0xd800 | (res >> 10);
267 *dst++ = 0xdc00 | (res & 0x3ff);
268 composed[0] = 0; /* no composition for surrogates */
270 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
271 /* otherwise ignore it */
273 return dstlen - (dstend - dst);
276 /* query necessary dst length for src string */
277 static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
279 int ret = 0;
280 unsigned int res;
281 const char *srcend = src + srclen;
283 while (src < srcend)
285 unsigned char ch = *src++;
286 if (ch < 0x80) /* special fast case for 7-bit ASCII */
288 ret++;
289 continue;
291 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0x10ffff)
293 if (res > 0xffff) ret++;
294 ret++;
296 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
297 /* otherwise ignore it */
299 return ret;
302 /* UTF-8 to wide char string conversion */
303 /* return -1 on dst buffer overflow, -2 on invalid input char */
304 int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
306 unsigned int res;
307 const char *srcend = src + srclen;
308 WCHAR *dstend = dst + dstlen;
310 if (flags & MB_COMPOSITE) return utf8_mbstowcs_compose( flags, src, srclen, dst, dstlen );
312 if (!dstlen) return get_length_mbs_utf8( flags, src, srclen );
314 while ((dst < dstend) && (src < srcend))
316 unsigned char ch = *src++;
317 if (ch < 0x80) /* special fast case for 7-bit ASCII */
319 *dst++ = ch;
320 continue;
322 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
324 *dst++ = res;
326 else if (res <= 0x10ffff) /* we need surrogates */
328 if (dst == dstend - 1) return -1; /* overflow */
329 res -= 0x10000;
330 *dst++ = 0xd800 | (res >> 10);
331 *dst++ = 0xdc00 | (res & 0x3ff);
333 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
334 /* otherwise ignore it */
336 if (src < srcend) return -1; /* overflow */
337 return dstlen - (dstend - dst);