rpcrt4: Make UserMarshalFlags static.
[wine] / libs / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <string.h>
22
23 #include "wine/unicode.h"
24
25 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
26 static const char utf8_length[128] =
27 {
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 */
36 };
37
38 /* first byte mask depending on UTF-8 sequence length */
39 static const unsigned char utf8_mask[6] = { 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
40
41 /* minimum Unicode value depending on UTF-8 sequence length */
42 static const unsigned int utf8_minval[6] = { 0x0, 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
43
44
45 /* query necessary dst length for src string */
46 inline static int get_length_wcs_utf8( const WCHAR *src, unsigned int srclen )
47 {
48     int len;
49     for (len = 0; srclen; srclen--, src++, len++)
50     {
51         if (*src >= 0x80)
52         {
53             len++;
54             if (*src >= 0x800) len++;
55         }
56     }
57     return len;
58 }
59
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 )
63 {
64     int len;
65
66     if (!dstlen) return get_length_wcs_utf8( src, srclen );
67
68     for (len = dstlen; srclen; srclen--, src++)
69     {
70         WCHAR ch = *src;
71
72         if (ch < 0x80)  /* 0x00-0x7f: 1 byte */
73         {
74             if (!len--) return -1;  /* overflow */
75             *dst++ = ch;
76             continue;
77         }
78
79         if (ch < 0x800)  /* 0x80-0x7ff: 2 bytes */
80         {
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;
87         }
88
89         /* 0x800-0xffff: 3 bytes */
90
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;
98     }
99     return dstlen - len;
100 }
101
102 /* query necessary dst length for src string */
103 inline static int get_length_mbs_utf8( const unsigned char *src, int srclen )
104 {
105     int ret;
106     const unsigned char *srcend = src + srclen;
107
108     for (ret = 0; src < srcend; ret++)
109     {
110         unsigned char ch = *src++;
111         if (ch < 0xc0) continue;
112
113         switch(utf8_length[ch-0x80])
114         {
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++;
135         }
136     }
137     return ret;
138 }
139
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 )
143 {
144     int len, count;
145     unsigned int res;
146     const char *srcend = src + srclen;
147
148     if (!dstlen) return get_length_mbs_utf8( (const unsigned char*)src, srclen );
149
150     for (count = dstlen; count && (src < srcend); count--, dst++)
151     {
152         unsigned char ch = *src++;
153         if (ch < 0x80)  /* special fast case for 7-bit ASCII */
154         {
155             *dst = ch;
156             continue;
157         }
158         len = utf8_length[ch-0x80];
159         res = ch & utf8_mask[len];
160
161         switch(len)
162         {
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;
192         }
193     bad:
194         if (flags & MB_ERR_INVALID_CHARS) return -2;  /* bad char */
195         *dst = (WCHAR)'?';
196     }
197     if (src < srcend) return -1;  /* overflow */
198 done:
199     return dstlen - count;
200 }