wordpad: Updated Korean resource.
[wine.git] / libs / wine / utf8.c
blob14cdb9d35dee010cfde6eba9593d50886f38e7e8
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 */
85 len += 4;
87 return len;
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 )
94 int len;
96 if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
98 for (len = dstlen; srclen; srclen--, src++)
100 WCHAR ch = *src;
101 unsigned int val;
103 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
105 if (!len--) return -1; /* overflow */
106 *dst++ = ch;
107 continue;
110 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
112 if ((len -= 2) < 0) return -1; /* overflow */
113 dst[1] = 0x80 | (ch & 0x3f);
114 ch >>= 6;
115 dst[0] = 0xc0 | ch;
116 dst += 2;
117 continue;
120 if (!(val = get_surrogate_value( src, srclen )))
122 if (flags & WC_ERR_INVALID_CHARS) return -2;
123 continue;
126 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
128 if ((len -= 3) < 0) return -1; /* overflow */
129 dst[2] = 0x80 | (val & 0x3f);
130 val >>= 6;
131 dst[1] = 0x80 | (val & 0x3f);
132 val >>= 6;
133 dst[0] = 0xe0 | val;
134 dst += 3;
136 else /* 0x10000-0x10ffff: 4 bytes */
138 if ((len -= 4) < 0) return -1; /* overflow */
139 dst[3] = 0x80 | (val & 0x3f);
140 val >>= 6;
141 dst[2] = 0x80 | (val & 0x3f);
142 val >>= 6;
143 dst[1] = 0x80 | (val & 0x3f);
144 val >>= 6;
145 dst[0] = 0xf0 | val;
146 dst += 4;
149 return dstlen - len;
152 /* query necessary dst length for src string */
153 static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
155 int len, ret = 0;
156 unsigned int res;
157 const char *srcend = src + srclen;
159 while (src < srcend)
161 unsigned char ch = *src++;
162 if (ch < 0x80) /* special fast case for 7-bit ASCII */
164 ret++;
165 continue;
167 len = utf8_length[ch-0x80];
168 if (src + len > srcend) goto bad;
169 res = ch & utf8_mask[len];
171 switch(len)
173 case 3:
174 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
175 res = (res << 6) | ch;
176 src++;
177 case 2:
178 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
179 res = (res << 6) | ch;
180 src++;
181 case 1:
182 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
183 res = (res << 6) | ch;
184 src++;
185 if (res < utf8_minval[len]) goto bad;
186 if (res > 0x10ffff) goto bad;
187 if (res > 0xffff) ret++;
188 ret++;
189 continue;
191 bad:
192 if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
193 /* otherwise ignore it */
195 return ret;
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 )
202 int len;
203 unsigned int res;
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 */
214 *dst++ = ch;
215 continue;
217 len = utf8_length[ch-0x80];
218 if (src + len > srcend) goto bad;
219 res = ch & utf8_mask[len];
221 switch(len)
223 case 3:
224 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
225 res = (res << 6) | ch;
226 src++;
227 case 2:
228 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
229 res = (res << 6) | ch;
230 src++;
231 case 1:
232 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
233 res = (res << 6) | ch;
234 src++;
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 */
241 res -= 0x10000;
242 *dst++ = 0xd800 | (res >> 10);
243 *dst++ = 0xdc00 | (res & 0x3ff);
245 continue;
247 bad:
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);