ntdll: Remove wait timeout in get_thread_context().
[wine.git] / libs / wine / utf8.c
blobe0d13c01a55871956cfaff53502c750081d816cd
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/asm.h"
25 #ifdef __ASM_OBSOLETE
27 #include "unicode.h"
29 extern WCHAR wine_compose( const WCHAR *str ) DECLSPEC_HIDDEN;
31 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
32 static const char utf8_length[128] =
34 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
35 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
36 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
37 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
38 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
39 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
40 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
41 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
44 /* first byte mask depending on UTF-8 sequence length */
45 static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
47 /* minimum Unicode value depending on UTF-8 sequence length */
48 static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
51 /* get the next char value taking surrogates into account */
52 static inline unsigned int get_surrogate_value( const WCHAR *src, unsigned int srclen )
54 if (src[0] >= 0xd800 && src[0] <= 0xdfff) /* surrogate pair */
56 if (src[0] > 0xdbff || /* invalid high surrogate */
57 srclen <= 1 || /* missing low surrogate */
58 src[1] < 0xdc00 || src[1] > 0xdfff) /* invalid low surrogate */
59 return 0;
60 return 0x10000 + ((src[0] & 0x3ff) << 10) + (src[1] & 0x3ff);
62 return src[0];
65 /* query necessary dst length for src string */
66 static inline int get_length_wcs_utf8( int flags, const WCHAR *src, unsigned int srclen )
68 int len;
69 unsigned int val;
71 for (len = 0; srclen; srclen--, src++)
73 if (*src < 0x80) /* 0x00-0x7f: 1 byte */
75 len++;
76 continue;
78 if (*src < 0x800) /* 0x80-0x7ff: 2 bytes */
80 len += 2;
81 continue;
83 if (!(val = get_surrogate_value( src, srclen )))
85 if (flags & WC_ERR_INVALID_CHARS) return -2;
86 continue;
88 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
89 len += 3;
90 else /* 0x10000-0x10ffff: 4 bytes */
92 len += 4;
93 src++;
94 srclen--;
97 return len;
100 /* wide char to UTF-8 string conversion */
101 /* return -1 on dst buffer overflow, -2 on invalid input char */
102 int wine_utf8_wcstombs_obsolete( int flags, const WCHAR *src, int srclen, char *dst, int dstlen )
104 int len;
106 if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
108 for (len = dstlen; srclen; srclen--, src++)
110 WCHAR ch = *src;
111 unsigned int val;
113 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
115 if (!len--) return -1; /* overflow */
116 *dst++ = ch;
117 continue;
120 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
122 if ((len -= 2) < 0) return -1; /* overflow */
123 dst[1] = 0x80 | (ch & 0x3f);
124 ch >>= 6;
125 dst[0] = 0xc0 | ch;
126 dst += 2;
127 continue;
130 if (!(val = get_surrogate_value( src, srclen )))
132 if (flags & WC_ERR_INVALID_CHARS) return -2;
133 continue;
136 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
138 if ((len -= 3) < 0) return -1; /* overflow */
139 dst[2] = 0x80 | (val & 0x3f);
140 val >>= 6;
141 dst[1] = 0x80 | (val & 0x3f);
142 val >>= 6;
143 dst[0] = 0xe0 | val;
144 dst += 3;
146 else /* 0x10000-0x10ffff: 4 bytes */
148 if ((len -= 4) < 0) return -1; /* overflow */
149 dst[3] = 0x80 | (val & 0x3f);
150 val >>= 6;
151 dst[2] = 0x80 | (val & 0x3f);
152 val >>= 6;
153 dst[1] = 0x80 | (val & 0x3f);
154 val >>= 6;
155 dst[0] = 0xf0 | val;
156 dst += 4;
157 src++;
158 srclen--;
161 return dstlen - len;
164 /* helper for the various utf8 mbstowcs functions */
165 static inline unsigned int decode_utf8_char( unsigned char ch, const char **str, const char *strend )
167 unsigned int len = utf8_length[ch-0x80];
168 unsigned int res = ch & utf8_mask[len];
169 const char *end = *str + len;
171 if (end > strend) return ~0;
172 switch(len)
174 case 3:
175 if ((ch = end[-3] ^ 0x80) >= 0x40) break;
176 res = (res << 6) | ch;
177 (*str)++;
178 case 2:
179 if ((ch = end[-2] ^ 0x80) >= 0x40) break;
180 res = (res << 6) | ch;
181 (*str)++;
182 case 1:
183 if ((ch = end[-1] ^ 0x80) >= 0x40) break;
184 res = (res << 6) | ch;
185 (*str)++;
186 if (res < utf8_minval[len]) break;
187 return res;
189 return ~0;
192 /* query necessary dst length for src string with composition */
193 static inline int get_length_mbs_utf8_compose( int flags, const char *src, int srclen )
195 int ret = 0;
196 unsigned int res;
197 WCHAR composed[2];
198 const char *srcend = src + srclen;
200 composed[0] = 0;
201 while (src < srcend)
203 unsigned char ch = *src++;
204 if (ch < 0x80) /* special fast case for 7-bit ASCII */
206 composed[0] = ch;
207 ret++;
208 continue;
210 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
212 if (composed[0])
214 composed[1] = res;
215 if ((composed[0] = wine_compose( composed ))) continue;
217 composed[0] = res;
218 ret++;
220 else if (res <= 0x10ffff)
222 ret += 2;
223 composed[0] = 0; /* no composition for surrogates */
225 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
226 /* otherwise ignore it */
228 return ret;
231 /* UTF-8 to wide char string conversion with composition */
232 /* return -1 on dst buffer overflow, -2 on invalid input char */
233 static int utf8_mbstowcs_compose( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
235 unsigned int res;
236 const char *srcend = src + srclen;
237 WCHAR composed[2];
238 WCHAR *dstend = dst + dstlen;
240 if (!dstlen) return get_length_mbs_utf8_compose( flags, src, srclen );
242 composed[0] = 0;
243 while (src < srcend)
245 unsigned char ch = *src++;
246 if (ch < 0x80) /* special fast case for 7-bit ASCII */
248 if (dst >= dstend) return -1; /* overflow */
249 *dst++ = composed[0] = ch;
250 continue;
252 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
254 if (composed[0])
256 composed[1] = res;
257 if ((composed[0] = wine_compose( composed )))
259 dst[-1] = composed[0];
260 continue;
263 if (dst >= dstend) return -1; /* overflow */
264 *dst++ = composed[0] = res;
266 else if (res <= 0x10ffff) /* we need surrogates */
268 if (dst >= dstend - 1) return -1; /* overflow */
269 res -= 0x10000;
270 *dst++ = 0xd800 | (res >> 10);
271 *dst++ = 0xdc00 | (res & 0x3ff);
272 composed[0] = 0; /* no composition for surrogates */
274 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
275 /* otherwise ignore it */
277 return dstlen - (dstend - dst);
280 /* query necessary dst length for src string */
281 static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
283 int ret = 0;
284 unsigned int res;
285 const char *srcend = src + srclen;
287 while (src < srcend)
289 unsigned char ch = *src++;
290 if (ch < 0x80) /* special fast case for 7-bit ASCII */
292 ret++;
293 continue;
295 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0x10ffff)
297 if (res > 0xffff) ret++;
298 ret++;
300 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
301 /* otherwise ignore it */
303 return ret;
306 /* UTF-8 to wide char string conversion */
307 /* return -1 on dst buffer overflow, -2 on invalid input char */
308 int wine_utf8_mbstowcs_obsolete( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
310 unsigned int res;
311 const char *srcend = src + srclen;
312 WCHAR *dstend = dst + dstlen;
314 if (flags & MB_COMPOSITE) return utf8_mbstowcs_compose( flags, src, srclen, dst, dstlen );
316 if (!dstlen) return get_length_mbs_utf8( flags, src, srclen );
318 while ((dst < dstend) && (src < srcend))
320 unsigned char ch = *src++;
321 if (ch < 0x80) /* special fast case for 7-bit ASCII */
323 *dst++ = ch;
324 continue;
326 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
328 *dst++ = res;
330 else if (res <= 0x10ffff) /* we need surrogates */
332 if (dst == dstend - 1) return -1; /* overflow */
333 res -= 0x10000;
334 *dst++ = 0xd800 | (res >> 10);
335 *dst++ = 0xdc00 | (res & 0x3ff);
337 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
338 /* otherwise ignore it */
340 if (src < srcend) return -1; /* overflow */
341 return dstlen - (dstend - dst);
344 __ASM_OBSOLETE(wine_utf8_wcstombs);
345 __ASM_OBSOLETE(wine_utf8_mbstowcs);
347 #endif /* __ASM_OBSOLETE */