push b59ba84f7e04af9ef068bd4c6e96701941f0256e
[wine/hacks.git] / libs / wine / utf8.c
blobfcbbeb056d833905cf0cca01945afd9aeb391dcc
1 /*
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
21 #include <string.h>
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 */
53 return 0;
54 return 0x10000 + ((src[0] & 0x3ff) << 10) + (src[1] & 0x3ff);
56 return src[0];
59 /* query necessary dst length for src string */
60 static inline int get_length_wcs_utf8( int flags, const WCHAR *src, unsigned int srclen )
62 int len;
63 unsigned int val;
65 for (len = 0; srclen; srclen--, src++)
67 if (*src < 0x80) /* 0x00-0x7f: 1 byte */
69 len++;
70 continue;
72 if (*src < 0x800) /* 0x80-0x7ff: 2 bytes */
74 len += 2;
75 continue;
77 if (!(val = get_surrogate_value( src, srclen )))
79 if (flags & WC_ERR_INVALID_CHARS) return -2;
80 continue;
82 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
83 len += 3;
84 else /* 0x10000-0x10ffff: 4 bytes */
86 len += 4;
87 src++;
88 srclen--;
91 return len;
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 )
98 int len;
100 if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
102 for (len = dstlen; srclen; srclen--, src++)
104 WCHAR ch = *src;
105 unsigned int val;
107 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
109 if (!len--) return -1; /* overflow */
110 *dst++ = ch;
111 continue;
114 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
116 if ((len -= 2) < 0) return -1; /* overflow */
117 dst[1] = 0x80 | (ch & 0x3f);
118 ch >>= 6;
119 dst[0] = 0xc0 | ch;
120 dst += 2;
121 continue;
124 if (!(val = get_surrogate_value( src, srclen )))
126 if (flags & WC_ERR_INVALID_CHARS) return -2;
127 continue;
130 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
132 if ((len -= 3) < 0) return -1; /* overflow */
133 dst[2] = 0x80 | (val & 0x3f);
134 val >>= 6;
135 dst[1] = 0x80 | (val & 0x3f);
136 val >>= 6;
137 dst[0] = 0xe0 | val;
138 dst += 3;
140 else /* 0x10000-0x10ffff: 4 bytes */
142 if ((len -= 4) < 0) return -1; /* overflow */
143 dst[3] = 0x80 | (val & 0x3f);
144 val >>= 6;
145 dst[2] = 0x80 | (val & 0x3f);
146 val >>= 6;
147 dst[1] = 0x80 | (val & 0x3f);
148 val >>= 6;
149 dst[0] = 0xf0 | val;
150 dst += 4;
151 src++;
152 srclen--;
155 return dstlen - len;
158 /* query necessary dst length for src string */
159 static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
161 int len, ret = 0;
162 unsigned int res;
163 const char *srcend = src + srclen;
165 while (src < srcend)
167 unsigned char ch = *src++;
168 if (ch < 0x80) /* special fast case for 7-bit ASCII */
170 ret++;
171 continue;
173 len = utf8_length[ch-0x80];
174 if (src + len > srcend) goto bad;
175 res = ch & utf8_mask[len];
177 switch(len)
179 case 3:
180 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
181 res = (res << 6) | ch;
182 src++;
183 case 2:
184 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
185 res = (res << 6) | ch;
186 src++;
187 case 1:
188 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
189 res = (res << 6) | ch;
190 src++;
191 if (res < utf8_minval[len]) goto bad;
192 if (res > 0x10ffff) goto bad;
193 if (res > 0xffff) ret++;
194 ret++;
195 continue;
197 bad:
198 if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
199 /* otherwise ignore it */
201 return ret;
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 )
208 int len;
209 unsigned int res;
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 */
220 *dst++ = ch;
221 continue;
223 len = utf8_length[ch-0x80];
224 if (src + len > srcend) goto bad;
225 res = ch & utf8_mask[len];
227 switch(len)
229 case 3:
230 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
231 res = (res << 6) | ch;
232 src++;
233 case 2:
234 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
235 res = (res << 6) | ch;
236 src++;
237 case 1:
238 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
239 res = (res << 6) | ch;
240 src++;
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 */
247 res -= 0x10000;
248 *dst++ = 0xd800 | (res >> 10);
249 *dst++ = 0xdc00 | (res & 0x3ff);
251 continue;
253 bad:
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);