VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/libWine/utf8.c@ 19982

Last change on this file since 19982 was 16477, checked in by vboxsync, 16 years ago

LGPL disclaimer by filemuncher

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1/*
2 * UTF-8 support routines
3 *
4 * Copyright 2000 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21/*
22 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
23 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
24 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
25 * a choice of LGPL license versions is made available with the language indicating
26 * that LGPLv2 or any later version may be used, or where a choice of which version
27 * of the LGPL is applied is otherwise unspecified.
28 */
29
30#include <string.h>
31
32#include "wine/unicode.h"
33
34extern WCHAR compose( const WCHAR *str );
35
36/* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
37static const char utf8_length[128] =
38{
39 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
40 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
41 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
42 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
43 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
44 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
45 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
46 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
47};
48
49/* first byte mask depending on UTF-8 sequence length */
50static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
51
52/* minimum Unicode value depending on UTF-8 sequence length */
53static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
54
55
56/* get the next char value taking surrogates into account */
57static inline unsigned int get_surrogate_value( const WCHAR *src, unsigned int srclen )
58{
59 if (src[0] >= 0xd800 && src[0] <= 0xdfff) /* surrogate pair */
60 {
61 if (src[0] > 0xdbff || /* invalid high surrogate */
62 srclen <= 1 || /* missing low surrogate */
63 src[1] < 0xdc00 || src[1] > 0xdfff) /* invalid low surrogate */
64 return 0;
65 return 0x10000 + ((src[0] & 0x3ff) << 10) + (src[1] & 0x3ff);
66 }
67 return src[0];
68}
69
70/* query necessary dst length for src string */
71static inline int get_length_wcs_utf8( int flags, const WCHAR *src, unsigned int srclen )
72{
73 int len;
74 unsigned int val;
75
76 for (len = 0; srclen; srclen--, src++)
77 {
78 if (*src < 0x80) /* 0x00-0x7f: 1 byte */
79 {
80 len++;
81 continue;
82 }
83 if (*src < 0x800) /* 0x80-0x7ff: 2 bytes */
84 {
85 len += 2;
86 continue;
87 }
88 if (!(val = get_surrogate_value( src, srclen )))
89 {
90 if (flags & WC_ERR_INVALID_CHARS) return -2;
91 continue;
92 }
93 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
94 len += 3;
95 else /* 0x10000-0x10ffff: 4 bytes */
96 {
97 len += 4;
98 src++;
99 srclen--;
100 }
101 }
102 return len;
103}
104
105/* wide char to UTF-8 string conversion */
106/* return -1 on dst buffer overflow, -2 on invalid input char */
107int wine_utf8_wcstombs( int flags, const WCHAR *src, int srclen, char *dst, int dstlen )
108{
109 int len;
110
111 if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
112
113 for (len = dstlen; srclen; srclen--, src++)
114 {
115 WCHAR ch = *src;
116 unsigned int val;
117
118 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
119 {
120 if (!len--) return -1; /* overflow */
121 *dst++ = ch;
122 continue;
123 }
124
125 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
126 {
127 if ((len -= 2) < 0) return -1; /* overflow */
128 dst[1] = 0x80 | (ch & 0x3f);
129 ch >>= 6;
130 dst[0] = 0xc0 | ch;
131 dst += 2;
132 continue;
133 }
134
135 if (!(val = get_surrogate_value( src, srclen )))
136 {
137 if (flags & WC_ERR_INVALID_CHARS) return -2;
138 continue;
139 }
140
141 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
142 {
143 if ((len -= 3) < 0) return -1; /* overflow */
144 dst[2] = 0x80 | (val & 0x3f);
145 val >>= 6;
146 dst[1] = 0x80 | (val & 0x3f);
147 val >>= 6;
148 dst[0] = 0xe0 | val;
149 dst += 3;
150 }
151 else /* 0x10000-0x10ffff: 4 bytes */
152 {
153 if ((len -= 4) < 0) return -1; /* overflow */
154 dst[3] = 0x80 | (val & 0x3f);
155 val >>= 6;
156 dst[2] = 0x80 | (val & 0x3f);
157 val >>= 6;
158 dst[1] = 0x80 | (val & 0x3f);
159 val >>= 6;
160 dst[0] = 0xf0 | val;
161 dst += 4;
162 src++;
163 srclen--;
164 }
165 }
166 return dstlen - len;
167}
168
169/* helper for the various utf8 mbstowcs functions */
170static inline unsigned int decode_utf8_char( unsigned char ch, const char **str, const char *strend )
171{
172 unsigned int len = utf8_length[ch-0x80];
173 unsigned int res = ch & utf8_mask[len];
174 const char *end = *str + len;
175
176 if (end > strend) return ~0;
177 switch(len)
178 {
179 case 3:
180 if ((ch = end[-3] ^ 0x80) >= 0x40) break;
181 res = (res << 6) | ch;
182 (*str)++;
183 case 2:
184 if ((ch = end[-2] ^ 0x80) >= 0x40) break;
185 res = (res << 6) | ch;
186 (*str)++;
187 case 1:
188 if ((ch = end[-1] ^ 0x80) >= 0x40) break;
189 res = (res << 6) | ch;
190 (*str)++;
191 if (res < utf8_minval[len]) break;
192 return res;
193 }
194 return ~0;
195}
196
197/* query necessary dst length for src string with composition */
198static inline int get_length_mbs_utf8_compose( int flags, const char *src, int srclen )
199{
200 int ret = 0;
201 unsigned int res;
202 WCHAR composed[2];
203 const char *srcend = src + srclen;
204
205 composed[0] = 0;
206 while (src < srcend)
207 {
208 unsigned char ch = *src++;
209 if (ch < 0x80) /* special fast case for 7-bit ASCII */
210 {
211 composed[0] = ch;
212 ret++;
213 continue;
214 }
215 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
216 {
217 if (composed[0])
218 {
219 composed[1] = res;
220 if ((composed[0] = compose( composed ))) continue;
221 }
222 composed[0] = res;
223 ret++;
224 }
225 else if (res <= 0x10ffff)
226 {
227 ret += 2;
228 composed[0] = 0; /* no composition for surrogates */
229 }
230 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
231 /* otherwise ignore it */
232 }
233 return ret;
234}
235
236/* UTF-8 to wide char string conversion with composition */
237/* return -1 on dst buffer overflow, -2 on invalid input char */
238static int utf8_mbstowcs_compose( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
239{
240 unsigned int res;
241 const char *srcend = src + srclen;
242 WCHAR composed[2];
243 WCHAR *dstend = dst + dstlen;
244
245 if (!dstlen) return get_length_mbs_utf8_compose( flags, src, srclen );
246
247 composed[0] = 0;
248 while (src < srcend)
249 {
250 unsigned char ch = *src++;
251 if (ch < 0x80) /* special fast case for 7-bit ASCII */
252 {
253 if (dst >= dstend) return -1; /* overflow */
254 *dst++ = composed[0] = ch;
255 continue;
256 }
257 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
258 {
259 if (composed[0])
260 {
261 composed[1] = res;
262 if ((composed[0] = compose( composed )))
263 {
264 dst[-1] = composed[0];
265 continue;
266 }
267 }
268 if (dst >= dstend) return -1; /* overflow */
269 *dst++ = composed[0] = res;
270 }
271 else if (res <= 0x10ffff) /* we need surrogates */
272 {
273 if (dst >= dstend - 1) return -1; /* overflow */
274 res -= 0x10000;
275 *dst++ = 0xd800 | (res >> 10);
276 *dst++ = 0xdc00 | (res & 0x3ff);
277 composed[0] = 0; /* no composition for surrogates */
278 }
279 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
280 /* otherwise ignore it */
281 }
282 return dstlen - (dstend - dst);
283}
284
285/* query necessary dst length for src string */
286static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
287{
288 int ret = 0;
289 unsigned int res;
290 const char *srcend = src + srclen;
291
292 while (src < srcend)
293 {
294 unsigned char ch = *src++;
295 if (ch < 0x80) /* special fast case for 7-bit ASCII */
296 {
297 ret++;
298 continue;
299 }
300 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0x10ffff)
301 {
302 if (res > 0xffff) ret++;
303 ret++;
304 }
305 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
306 /* otherwise ignore it */
307 }
308 return ret;
309}
310
311/* UTF-8 to wide char string conversion */
312/* return -1 on dst buffer overflow, -2 on invalid input char */
313int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
314{
315 unsigned int res;
316 const char *srcend = src + srclen;
317 WCHAR *dstend = dst + dstlen;
318
319 if (flags & MB_COMPOSITE) return utf8_mbstowcs_compose( flags, src, srclen, dst, dstlen );
320
321 if (!dstlen) return get_length_mbs_utf8( flags, src, srclen );
322
323 while ((dst < dstend) && (src < srcend))
324 {
325 unsigned char ch = *src++;
326 if (ch < 0x80) /* special fast case for 7-bit ASCII */
327 {
328 *dst++ = ch;
329 continue;
330 }
331 if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
332 {
333 *dst++ = res;
334 }
335 else if (res <= 0x10ffff) /* we need surrogates */
336 {
337 if (dst == dstend - 1) return -1; /* overflow */
338 res -= 0x10000;
339 *dst++ = 0xd800 | (res >> 10);
340 *dst++ = 0xdc00 | (res & 0x3ff);
341 }
342 else if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
343 /* otherwise ignore it */
344 }
345 if (src < srcend) return -1; /* overflow */
346 return dstlen - (dstend - dst);
347}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette