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
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
)
49 for (len
= 0; srclen
; srclen
--, src
++, len
++)
54 if (*src
>= 0x800) 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
)
66 if (!dstlen
) return get_length_wcs_utf8( src
, srclen
);
68 for (len
= dstlen
; srclen
; srclen
--, src
++)
72 if (ch
< 0x80) /* 0x00-0x7f: 1 byte */
74 if (!len
--) return -1; /* overflow */
79 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
81 if ((len
-= 2) < 0) return -1; /* overflow */
82 dst
[1] = 0x80 | (ch
& 0x3f);
89 /* 0x800-0xffff: 3 bytes */
91 if ((len
-= 3) < 0) return -1; /* overflow */
92 dst
[2] = 0x80 | (ch
& 0x3f);
94 dst
[1] = 0x80 | (ch
& 0x3f);
102 /* query necessary dst length for src string */
103 inline static int get_length_mbs_utf8( const unsigned char *src
, int srclen
)
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])
116 if (src
>= srcend
) return ret
; /* ignore partial char */
117 if ((ch
= *src
^ 0x80) >= 0x40) continue;
120 if (src
>= srcend
) return ret
; /* ignore partial char */
121 if ((ch
= *src
^ 0x80) >= 0x40) continue;
124 if (src
>= srcend
) return ret
; /* ignore partial char */
125 if ((ch
= *src
^ 0x80) >= 0x40) continue;
128 if (src
>= srcend
) return ret
; /* ignore partial char */
129 if ((ch
= *src
^ 0x80) >= 0x40) continue;
132 if (src
>= srcend
) return ret
; /* ignore partial char */
133 if ((ch
= *src
^ 0x80) >= 0x40) continue;
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
)
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 */
158 len
= utf8_length
[ch
-0x80];
159 res
= ch
& utf8_mask
[len
];
164 if (src
>= srcend
) goto done
; /* ignore partial char */
165 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
166 res
= (res
<< 6) | ch
;
169 if (src
>= srcend
) goto done
; /* ignore partial char */
170 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
171 res
= (res
<< 6) | ch
;
174 if (src
>= srcend
) goto done
; /* ignore partial char */
175 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
176 res
= (res
<< 6) | ch
;
179 if (src
>= srcend
) goto done
; /* ignore partial char */
180 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
181 res
= (res
<< 6) | ch
;
184 if (src
>= srcend
) goto done
; /* ignore partial char */
185 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
186 res
= (res
<< 6) | ch
;
188 if (res
< utf8_minval
[len
]) goto bad
;
189 if (res
>= 0x10000) goto bad
; /* FIXME: maybe we should do surrogates here */
194 if (flags
& MB_ERR_INVALID_CHARS
) return -2; /* bad char */
197 if (src
< srcend
) return -1; /* overflow */
199 return dstlen
- count
;