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
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 */
56 return 0x10000 + ((src
[0] & 0x3ff) << 10) + (src
[1] & 0x3ff);
61 /* query necessary dst length for src string */
62 static inline int get_length_wcs_utf8( int flags
, const WCHAR
*src
, unsigned int srclen
)
67 for (len
= 0; srclen
; srclen
--, src
++)
69 if (*src
< 0x80) /* 0x00-0x7f: 1 byte */
74 if (*src
< 0x800) /* 0x80-0x7ff: 2 bytes */
79 if (!(val
= get_surrogate_value( src
, srclen
)))
81 if (flags
& WC_ERR_INVALID_CHARS
) return -2;
84 if (val
< 0x10000) /* 0x800-0xffff: 3 bytes */
86 else /* 0x10000-0x10ffff: 4 bytes */
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
)
102 if (!dstlen
) return get_length_wcs_utf8( flags
, src
, srclen
);
104 for (len
= dstlen
; srclen
; srclen
--, src
++)
109 if (ch
< 0x80) /* 0x00-0x7f: 1 byte */
111 if (!len
--) return -1; /* overflow */
116 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
118 if ((len
-= 2) < 0) return -1; /* overflow */
119 dst
[1] = 0x80 | (ch
& 0x3f);
126 if (!(val
= get_surrogate_value( src
, srclen
)))
128 if (flags
& WC_ERR_INVALID_CHARS
) return -2;
132 if (val
< 0x10000) /* 0x800-0xffff: 3 bytes */
134 if ((len
-= 3) < 0) return -1; /* overflow */
135 dst
[2] = 0x80 | (val
& 0x3f);
137 dst
[1] = 0x80 | (val
& 0x3f);
142 else /* 0x10000-0x10ffff: 4 bytes */
144 if ((len
-= 4) < 0) return -1; /* overflow */
145 dst
[3] = 0x80 | (val
& 0x3f);
147 dst
[2] = 0x80 | (val
& 0x3f);
149 dst
[1] = 0x80 | (val
& 0x3f);
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;
171 if ((ch
= end
[-3] ^ 0x80) >= 0x40) break;
172 res
= (res
<< 6) | ch
;
175 if ((ch
= end
[-2] ^ 0x80) >= 0x40) break;
176 res
= (res
<< 6) | ch
;
179 if ((ch
= end
[-1] ^ 0x80) >= 0x40) break;
180 res
= (res
<< 6) | ch
;
182 if (res
< utf8_minval
[len
]) break;
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
)
194 const char *srcend
= src
+ srclen
;
199 unsigned char ch
= *src
++;
200 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
206 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0xffff)
211 if ((composed
[0] = compose( composed
))) continue;
216 else if (res
<= 0x10ffff)
219 composed
[0] = 0; /* no composition for surrogates */
221 else if (flags
& MB_ERR_INVALID_CHARS
) return -2; /* bad char */
222 /* otherwise ignore it */
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
)
232 const char *srcend
= src
+ srclen
;
234 WCHAR
*dstend
= dst
+ dstlen
;
236 if (!dstlen
) return get_length_mbs_utf8_compose( flags
, src
, srclen
);
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
;
248 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0xffff)
253 if ((composed
[0] = compose( composed
)))
255 dst
[-1] = composed
[0];
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 */
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
)
281 const char *srcend
= src
+ srclen
;
285 unsigned char ch
= *src
++;
286 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
291 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0x10ffff)
293 if (res
> 0xffff) ret
++;
296 else if (flags
& MB_ERR_INVALID_CHARS
) return -2; /* bad char */
297 /* otherwise ignore it */
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
)
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 */
322 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0xffff)
326 else if (res
<= 0x10ffff) /* we need surrogates */
328 if (dst
== dstend
- 1) return -1; /* overflow */
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
);