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