Fixed some issues found by winapi_check.
[wine] / unicode / utf8.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <string.h>
22
23 #include "winnls.h"
24 #include "wine/unicode.h"
25
26 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
27 static const char utf8_length[128] =
28 {
29     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
30     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
31     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
32     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
33     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
34     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
35     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
36     3,3,3,3,3,3,3,3,4,4,4,4,5,5,0,0  /* 0xf0-0xff */
37 };
38
39 /* first byte mask depending on UTF-8 sequence length */
40 static const unsigned char utf8_mask[6] = { 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
41
42 /* minimum Unicode value depending on UTF-8 sequence length */
43 static const unsigned int utf8_minval[6] = { 0x0, 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
44
45
46 /* query necessary dst length for src string */
47 inline static int get_length_wcs_utf8( const WCHAR *src, unsigned int srclen )
48 {
49     int len;
50     for (len = 0; srclen; srclen--, src++, len++)
51     {
52         if (*src >= 0x80)
53         {
54             len++;
55             if (*src >= 0x800) len++;
56         }
57     }
58     return len;
59 }
60
61 /* wide char to UTF-8 string conversion */
62 /* return -1 on dst buffer overflow */
63 int utf8_wcstombs( const WCHAR *src, int srclen, char *dst, int dstlen )
64 {
65     int ret = srclen;
66
67     if (!dstlen) return get_length_wcs_utf8( src, srclen );
68
69     for (ret = srclen; srclen; srclen--, src++)
70     {
71         WCHAR ch = *src;
72
73         if (ch < 0x80)  /* 0x00-0x7f: 1 byte */
74         {
75             if (!dstlen--) return -1;  /* overflow */
76             *dst++ = ch;
77             continue;
78         }
79
80         if (ch < 0x800)  /* 0x80-0x7ff: 2 bytes */
81         {
82             if ((dstlen -= 2) < 0) return -1;  /* overflow */
83             dst[1] = 0x80 | (ch & 0x3f);
84             ch >>= 6;
85             dst[0] = 0xc0 | ch;
86             dst += 2;
87             continue;
88         }
89
90         /* 0x800-0xffff: 3 bytes */
91
92         if ((dstlen -= 3) < 0) return -1;  /* overflow */
93         dst[2] = 0x80 | (ch & 0x3f);
94         ch >>= 6;
95         dst[1] = 0x80 | (ch & 0x3f);
96         ch >>= 6;
97         dst[0] = 0xe0 | ch;
98         dst += 3;
99     }
100     return ret;
101 }
102
103 /* query necessary dst length for src string */
104 inline static int get_length_mbs_utf8( const unsigned char *src, int srclen )
105 {
106     int ret;
107     const unsigned char *srcend = src + srclen;
108
109     for (ret = 0; src < srcend; ret++)
110     {
111         unsigned char ch = *src++;
112         if (ch < 0xc0) continue;
113
114         switch(utf8_length[ch-0x80])
115         {
116         case 5:
117             if (src >= srcend) return ret;  /* ignore partial char */
118             if ((ch = *src ^ 0x80) >= 0x40) continue;
119             src++;
120         case 4:
121             if (src >= srcend) return ret;  /* ignore partial char */
122             if ((ch = *src ^ 0x80) >= 0x40) continue;
123             src++;
124         case 3:
125             if (src >= srcend) return ret;  /* ignore partial char */
126             if ((ch = *src ^ 0x80) >= 0x40) continue;
127             src++;
128         case 2:
129             if (src >= srcend) return ret;  /* ignore partial char */
130             if ((ch = *src ^ 0x80) >= 0x40) continue;
131             src++;
132         case 1:
133             if (src >= srcend) return ret;  /* ignore partial char */
134             if ((ch = *src ^ 0x80) >= 0x40) continue;
135             src++;
136         }
137     }
138     return ret;
139 }
140
141 /* UTF-8 to wide char string conversion */
142 /* return -1 on dst buffer overflow, -2 on invalid input char */
143 int utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
144 {
145     int len, count;
146     unsigned int res;
147     const char *srcend = src + srclen;
148
149     if (!dstlen) return get_length_mbs_utf8( src, srclen );
150
151     for (count = dstlen; count && (src < srcend); count--, dst++)
152     {
153         unsigned char ch = *src++;
154         if (ch < 0x80)  /* special fast case for 7-bit ASCII */
155         {
156             *dst = ch;
157             continue;
158         }
159         len = utf8_length[ch-0x80];
160         res = ch & utf8_mask[len];
161
162         switch(len)
163         {
164         case 5:
165             if (src >= srcend) goto done;  /* ignore partial char */
166             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
167             res = (res << 6) | ch;
168             src++;
169         case 4:
170             if (src >= srcend) goto done;  /* ignore partial char */
171             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
172             res = (res << 6) | ch;
173             src++;
174         case 3:
175             if (src >= srcend) goto done;  /* ignore partial char */
176             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
177             res = (res << 6) | ch;
178             src++;
179         case 2:
180             if (src >= srcend) goto done;  /* ignore partial char */
181             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
182             res = (res << 6) | ch;
183             src++;
184         case 1:
185             if (src >= srcend) goto done;  /* ignore partial char */
186             if ((ch = *src ^ 0x80) >= 0x40) goto bad;
187             res = (res << 6) | ch;
188             src++;
189             if (res < utf8_minval[len]) goto bad;
190             if (res >= 0x10000) goto bad;  /* FIXME: maybe we should do surrogates here */
191             *dst = res;
192             continue;
193         }
194     bad:
195         if (flags & MB_ERR_INVALID_CHARS) return -2;  /* bad char */
196         *dst = (WCHAR)'?';
197     }
198     if (src < srcend) return -1;  /* overflow */
199 done:
200     return dstlen - count;
201 }