Release 20040914.
[wine/gsoc-2012-control.git] / libs / unicode / utf8.c
blob6f6781aa18618ea99bd72d06b3fe20370cdd993c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 1,1,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,3,3,3,4,4,4,4,5,5,0,0 /* 0xf0-0xff */
38 /* first byte mask depending on UTF-8 sequence length */
39 static const unsigned char utf8_mask[6] = { 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
41 /* minimum Unicode value depending on UTF-8 sequence length */
42 static const unsigned int utf8_minval[6] = { 0x0, 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
45 /* query necessary dst length for src string */
46 inline static int get_length_wcs_utf8( const WCHAR *src, unsigned int srclen )
48 int len;
49 for (len = 0; srclen; srclen--, src++, len++)
51 if (*src >= 0x80)
53 len++;
54 if (*src >= 0x800) len++;
57 return len;
60 /* wide char to UTF-8 string conversion */
61 /* return -1 on dst buffer overflow */
62 int wine_utf8_wcstombs( const WCHAR *src, int srclen, char *dst, int dstlen )
64 int len;
66 if (!dstlen) return get_length_wcs_utf8( src, srclen );
68 for (len = dstlen; srclen; srclen--, src++)
70 WCHAR ch = *src;
72 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
74 if (!len--) return -1; /* overflow */
75 *dst++ = ch;
76 continue;
79 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
81 if ((len -= 2) < 0) return -1; /* overflow */
82 dst[1] = 0x80 | (ch & 0x3f);
83 ch >>= 6;
84 dst[0] = 0xc0 | ch;
85 dst += 2;
86 continue;
89 /* 0x800-0xffff: 3 bytes */
91 if ((len -= 3) < 0) return -1; /* overflow */
92 dst[2] = 0x80 | (ch & 0x3f);
93 ch >>= 6;
94 dst[1] = 0x80 | (ch & 0x3f);
95 ch >>= 6;
96 dst[0] = 0xe0 | ch;
97 dst += 3;
99 return dstlen - len;
102 /* query necessary dst length for src string */
103 inline static int get_length_mbs_utf8( const unsigned char *src, int srclen )
105 int ret;
106 const unsigned char *srcend = src + srclen;
108 for (ret = 0; src < srcend; ret++)
110 unsigned char ch = *src++;
111 if (ch < 0xc0) continue;
113 switch(utf8_length[ch-0x80])
115 case 5:
116 if (src >= srcend) return ret; /* ignore partial char */
117 if ((ch = *src ^ 0x80) >= 0x40) continue;
118 src++;
119 case 4:
120 if (src >= srcend) return ret; /* ignore partial char */
121 if ((ch = *src ^ 0x80) >= 0x40) continue;
122 src++;
123 case 3:
124 if (src >= srcend) return ret; /* ignore partial char */
125 if ((ch = *src ^ 0x80) >= 0x40) continue;
126 src++;
127 case 2:
128 if (src >= srcend) return ret; /* ignore partial char */
129 if ((ch = *src ^ 0x80) >= 0x40) continue;
130 src++;
131 case 1:
132 if (src >= srcend) return ret; /* ignore partial char */
133 if ((ch = *src ^ 0x80) >= 0x40) continue;
134 src++;
137 return ret;
140 /* UTF-8 to wide char string conversion */
141 /* return -1 on dst buffer overflow, -2 on invalid input char */
142 int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
144 int len, count;
145 unsigned int res;
146 const char *srcend = src + srclen;
148 if (!dstlen) return get_length_mbs_utf8( src, srclen );
150 for (count = dstlen; count && (src < srcend); count--, dst++)
152 unsigned char ch = *src++;
153 if (ch < 0x80) /* special fast case for 7-bit ASCII */
155 *dst = ch;
156 continue;
158 len = utf8_length[ch-0x80];
159 res = ch & utf8_mask[len];
161 switch(len)
163 case 5:
164 if (src >= srcend) goto done; /* ignore partial char */
165 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
166 res = (res << 6) | ch;
167 src++;
168 case 4:
169 if (src >= srcend) goto done; /* ignore partial char */
170 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
171 res = (res << 6) | ch;
172 src++;
173 case 3:
174 if (src >= srcend) goto done; /* ignore partial char */
175 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
176 res = (res << 6) | ch;
177 src++;
178 case 2:
179 if (src >= srcend) goto done; /* ignore partial char */
180 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
181 res = (res << 6) | ch;
182 src++;
183 case 1:
184 if (src >= srcend) goto done; /* ignore partial char */
185 if ((ch = *src ^ 0x80) >= 0x40) goto bad;
186 res = (res << 6) | ch;
187 src++;
188 if (res < utf8_minval[len]) goto bad;
189 if (res >= 0x10000) goto bad; /* FIXME: maybe we should do surrogates here */
190 *dst = res;
191 continue;
193 bad:
194 if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
195 *dst = (WCHAR)'?';
197 if (src < srcend) return -1; /* overflow */
198 done:
199 return dstlen - count;