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 */
90 /* wide char to UTF-8 string conversion */
91 /* return -1 on dst buffer overflow, -2 on invalid input char */
92 int wine_utf8_wcstombs( int flags, const WCHAR *src, int srclen, char *dst, int dstlen )
96 if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
98 for (len = dstlen; srclen; srclen--, src++)
103 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
105 if (!len--) return -1; /* overflow */
110 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
112 if ((len -= 2) < 0) return -1; /* overflow */
113 dst[1] = 0x80 | (ch & 0x3f);
120 if (!(val = get_surrogate_value( src, srclen )))
122 if (flags & WC_ERR_INVALID_CHARS) return -2;
126 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
128 if ((len -= 3) < 0) return -1; /* overflow */
129 dst[2] = 0x80 | (val & 0x3f);
131 dst[1] = 0x80 | (val & 0x3f);
136 else /* 0x10000-0x10ffff: 4 bytes */
138 if ((len -= 4) < 0) return -1; /* overflow */
139 dst[3] = 0x80 | (val & 0x3f);
141 dst[2] = 0x80 | (val & 0x3f);
143 dst[1] = 0x80 | (val & 0x3f);
152 /* query necessary dst length for src string */
153 static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
157 const char *srcend = src + srclen;
161 unsigned char ch = *src++;
162 if (ch < 0x80) /* special fast case for 7-bit ASCII */
167 len = utf8_length[ch-0x80];
168 if (src + len > srcend) goto bad;
169 res = ch & utf8_mask[len];
174 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
175 res = (res << 6) | ch;
178 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
179 res = (res << 6) | ch;
182 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
183 res = (res << 6) | ch;
185 if (res < utf8_minval[len]) goto bad;
186 if (res > 0x10ffff) goto bad;
187 if (res > 0xffff) ret++;
192 if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
193 /* otherwise ignore it */
198 /* UTF-8 to wide char string conversion */
199 /* return -1 on dst buffer overflow, -2 on invalid input char */
200 int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
204 const char *srcend = src + srclen;
205 WCHAR *dstend = dst + dstlen;
207 if (!dstlen) return get_length_mbs_utf8( flags, src, srclen );
209 while ((dst < dstend) && (src < srcend))
211 unsigned char ch = *src++;
212 if (ch < 0x80) /* special fast case for 7-bit ASCII */
217 len = utf8_length[ch-0x80];
218 if (src + len > srcend) goto bad;
219 res = ch & utf8_mask[len];
224 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
225 res = (res << 6) | ch;
228 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
229 res = (res << 6) | ch;
232 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
233 res = (res << 6) | ch;
235 if (res < utf8_minval[len]) goto bad;
236 if (res > 0x10ffff) goto bad;
237 if (res <= 0xffff) *dst++ = res;
238 else /* we need surrogates */
240 if (dst == dstend - 1) return -1; /* overflow */
242 *dst++ = 0xd800 | (res >> 10);
243 *dst++ = 0xdc00 | (res & 0x3ff);
248 if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
249 /* otherwise ignore it */
251 if (src < srcend) return -1; /* overflow */
252 return dstlen - (dstend - dst);