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
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 */
60 return 0x10000 + ((src
[0] & 0x3ff) << 10) + (src
[1] & 0x3ff);
65 /* query necessary dst length for src string */
66 static inline int get_length_wcs_utf8( int flags
, const WCHAR
*src
, unsigned int srclen
)
71 for (len
= 0; srclen
; srclen
--, src
++)
73 if (*src
< 0x80) /* 0x00-0x7f: 1 byte */
78 if (*src
< 0x800) /* 0x80-0x7ff: 2 bytes */
83 if (!(val
= get_surrogate_value( src
, srclen
)))
85 if (flags
& WC_ERR_INVALID_CHARS
) return -2;
88 if (val
< 0x10000) /* 0x800-0xffff: 3 bytes */
90 else /* 0x10000-0x10ffff: 4 bytes */
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
)
106 if (!dstlen
) return get_length_wcs_utf8( flags
, src
, srclen
);
108 for (len
= dstlen
; srclen
; srclen
--, src
++)
113 if (ch
< 0x80) /* 0x00-0x7f: 1 byte */
115 if (!len
--) return -1; /* overflow */
120 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
122 if ((len
-= 2) < 0) return -1; /* overflow */
123 dst
[1] = 0x80 | (ch
& 0x3f);
130 if (!(val
= get_surrogate_value( src
, srclen
)))
132 if (flags
& WC_ERR_INVALID_CHARS
) return -2;
136 if (val
< 0x10000) /* 0x800-0xffff: 3 bytes */
138 if ((len
-= 3) < 0) return -1; /* overflow */
139 dst
[2] = 0x80 | (val
& 0x3f);
141 dst
[1] = 0x80 | (val
& 0x3f);
146 else /* 0x10000-0x10ffff: 4 bytes */
148 if ((len
-= 4) < 0) return -1; /* overflow */
149 dst
[3] = 0x80 | (val
& 0x3f);
151 dst
[2] = 0x80 | (val
& 0x3f);
153 dst
[1] = 0x80 | (val
& 0x3f);
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;
175 if ((ch
= end
[-3] ^ 0x80) >= 0x40) break;
176 res
= (res
<< 6) | ch
;
179 if ((ch
= end
[-2] ^ 0x80) >= 0x40) break;
180 res
= (res
<< 6) | ch
;
183 if ((ch
= end
[-1] ^ 0x80) >= 0x40) break;
184 res
= (res
<< 6) | ch
;
186 if (res
< utf8_minval
[len
]) break;
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
)
198 const char *srcend
= src
+ srclen
;
203 unsigned char ch
= *src
++;
204 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
210 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0xffff)
215 if ((composed
[0] = wine_compose( composed
))) continue;
220 else if (res
<= 0x10ffff)
223 composed
[0] = 0; /* no composition for surrogates */
225 else if (flags
& MB_ERR_INVALID_CHARS
) return -2; /* bad char */
226 /* otherwise ignore it */
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
)
236 const char *srcend
= src
+ srclen
;
238 WCHAR
*dstend
= dst
+ dstlen
;
240 if (!dstlen
) return get_length_mbs_utf8_compose( flags
, src
, srclen
);
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
;
252 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0xffff)
257 if ((composed
[0] = wine_compose( composed
)))
259 dst
[-1] = composed
[0];
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 */
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
)
285 const char *srcend
= src
+ srclen
;
289 unsigned char ch
= *src
++;
290 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
295 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0x10ffff)
297 if (res
> 0xffff) ret
++;
300 else if (flags
& MB_ERR_INVALID_CHARS
) return -2; /* bad char */
301 /* otherwise ignore it */
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
)
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 */
326 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0xffff)
330 else if (res
<= 0x10ffff) /* we need surrogates */
332 if (dst
== dstend
- 1) return -1; /* overflow */
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 */