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 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
26 static const char utf8_length
[128] =
28 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
29 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
30 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
31 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
32 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
33 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
34 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
35 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
38 /* first byte mask depending on UTF-8 sequence length */
39 static const unsigned char utf8_mask
[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
41 /* minimum Unicode value depending on UTF-8 sequence length */
42 static const unsigned int utf8_minval
[4] = { 0x0, 0x80, 0x800, 0x10000 };
45 /* get the next char value taking surrogates into account */
46 static inline unsigned int get_surrogate_value( const WCHAR
*src
, unsigned int srclen
)
48 if (src
[0] >= 0xd800 && src
[0] <= 0xdfff) /* surrogate pair */
50 if (src
[0] > 0xdbff || /* invalid high surrogate */
51 srclen
<= 1 || /* missing low surrogate */
52 src
[1] < 0xdc00 || src
[1] > 0xdfff) /* invalid low surrogate */
54 return 0x10000 + ((src
[0] & 0x3ff) << 10) + (src
[1] & 0x3ff);
59 /* query necessary dst length for src string */
60 static inline int get_length_wcs_utf8( int flags
, const WCHAR
*src
, unsigned int srclen
)
65 for (len
= 0; srclen
; srclen
--, src
++)
67 if (*src
< 0x80) /* 0x00-0x7f: 1 byte */
72 if (*src
< 0x800) /* 0x80-0x7ff: 2 bytes */
77 if (!(val
= get_surrogate_value( src
, srclen
)))
79 if (flags
& WC_ERR_INVALID_CHARS
) return -2;
82 if (val
< 0x10000) /* 0x800-0xffff: 3 bytes */
84 else /* 0x10000-0x10ffff: 4 bytes */
94 /* wide char to UTF-8 string conversion */
95 /* return -1 on dst buffer overflow, -2 on invalid input char */
96 int wine_utf8_wcstombs( int flags
, const WCHAR
*src
, int srclen
, char *dst
, int dstlen
)
100 if (!dstlen
) return get_length_wcs_utf8( flags
, src
, srclen
);
102 for (len
= dstlen
; srclen
; srclen
--, src
++)
107 if (ch
< 0x80) /* 0x00-0x7f: 1 byte */
109 if (!len
--) return -1; /* overflow */
114 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
116 if ((len
-= 2) < 0) return -1; /* overflow */
117 dst
[1] = 0x80 | (ch
& 0x3f);
124 if (!(val
= get_surrogate_value( src
, srclen
)))
126 if (flags
& WC_ERR_INVALID_CHARS
) return -2;
130 if (val
< 0x10000) /* 0x800-0xffff: 3 bytes */
132 if ((len
-= 3) < 0) return -1; /* overflow */
133 dst
[2] = 0x80 | (val
& 0x3f);
135 dst
[1] = 0x80 | (val
& 0x3f);
140 else /* 0x10000-0x10ffff: 4 bytes */
142 if ((len
-= 4) < 0) return -1; /* overflow */
143 dst
[3] = 0x80 | (val
& 0x3f);
145 dst
[2] = 0x80 | (val
& 0x3f);
147 dst
[1] = 0x80 | (val
& 0x3f);
158 /* query necessary dst length for src string */
159 static inline int get_length_mbs_utf8( int flags
, const char *src
, int srclen
)
163 const char *srcend
= src
+ srclen
;
167 unsigned char ch
= *src
++;
168 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
173 len
= utf8_length
[ch
-0x80];
174 if (src
+ len
> srcend
) goto bad
;
175 res
= ch
& utf8_mask
[len
];
180 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
181 res
= (res
<< 6) | ch
;
184 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
185 res
= (res
<< 6) | ch
;
188 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
189 res
= (res
<< 6) | ch
;
191 if (res
< utf8_minval
[len
]) goto bad
;
192 if (res
> 0x10ffff) goto bad
;
193 if (res
> 0xffff) ret
++;
198 if (flags
& MB_ERR_INVALID_CHARS
) return -2; /* bad char */
199 /* otherwise ignore it */
204 /* UTF-8 to wide char string conversion */
205 /* return -1 on dst buffer overflow, -2 on invalid input char */
206 int wine_utf8_mbstowcs( int flags
, const char *src
, int srclen
, WCHAR
*dst
, int dstlen
)
210 const char *srcend
= src
+ srclen
;
211 WCHAR
*dstend
= dst
+ dstlen
;
213 if (!dstlen
) return get_length_mbs_utf8( flags
, src
, srclen
);
215 while ((dst
< dstend
) && (src
< srcend
))
217 unsigned char ch
= *src
++;
218 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
223 len
= utf8_length
[ch
-0x80];
224 if (src
+ len
> srcend
) goto bad
;
225 res
= ch
& utf8_mask
[len
];
230 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
231 res
= (res
<< 6) | ch
;
234 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
235 res
= (res
<< 6) | ch
;
238 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
239 res
= (res
<< 6) | ch
;
241 if (res
< utf8_minval
[len
]) goto bad
;
242 if (res
> 0x10ffff) goto bad
;
243 if (res
<= 0xffff) *dst
++ = res
;
244 else /* we need surrogates */
246 if (dst
== dstend
- 1) return -1; /* overflow */
248 *dst
++ = 0xd800 | (res
>> 10);
249 *dst
++ = 0xdc00 | (res
& 0x3ff);
254 if (flags
& MB_ERR_INVALID_CHARS
) return -2; /* bad char */
255 /* otherwise ignore it */
257 if (src
< srcend
) return -1; /* overflow */
258 return dstlen
- (dstend
- dst
);