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
24 #include "wine/unicode.h"
26 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
27 static const char utf8_length
[128] =
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 */
39 /* first byte mask depending on UTF-8 sequence length */
40 static const unsigned char utf8_mask
[6] = { 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
42 /* minimum Unicode value depending on UTF-8 sequence length */
43 static const unsigned int utf8_minval
[6] = { 0x0, 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
46 /* query necessary dst length for src string */
47 inline static int get_length_wcs_utf8( const WCHAR
*src
, unsigned int srclen
)
50 for (len
= 0; srclen
; srclen
--, src
++, len
++)
55 if (*src
>= 0x800) len
++;
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
)
67 if (!dstlen
) return get_length_wcs_utf8( src
, srclen
);
69 for (ret
= srclen
; srclen
; srclen
--, src
++)
73 if (ch
< 0x80) /* 0x00-0x7f: 1 byte */
75 if (!dstlen
--) return -1; /* overflow */
80 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
82 if ((dstlen
-= 2) < 0) return -1; /* overflow */
83 dst
[1] = 0x80 | (ch
& 0x3f);
90 /* 0x800-0xffff: 3 bytes */
92 if ((dstlen
-= 3) < 0) return -1; /* overflow */
93 dst
[2] = 0x80 | (ch
& 0x3f);
95 dst
[1] = 0x80 | (ch
& 0x3f);
103 /* query necessary dst length for src string */
104 inline static int get_length_mbs_utf8( const unsigned char *src
, int srclen
)
107 const unsigned char *srcend
= src
+ srclen
;
109 for (ret
= 0; src
< srcend
; ret
++)
111 unsigned char ch
= *src
++;
112 if (ch
< 0xc0) continue;
114 switch(utf8_length
[ch
-0x80])
117 if (src
>= srcend
) return ret
; /* ignore partial char */
118 if ((ch
= *src
^ 0x80) >= 0x40) continue;
121 if (src
>= srcend
) return ret
; /* ignore partial char */
122 if ((ch
= *src
^ 0x80) >= 0x40) continue;
125 if (src
>= srcend
) return ret
; /* ignore partial char */
126 if ((ch
= *src
^ 0x80) >= 0x40) continue;
129 if (src
>= srcend
) return ret
; /* ignore partial char */
130 if ((ch
= *src
^ 0x80) >= 0x40) continue;
133 if (src
>= srcend
) return ret
; /* ignore partial char */
134 if ((ch
= *src
^ 0x80) >= 0x40) continue;
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
)
147 const char *srcend
= src
+ srclen
;
149 if (!dstlen
) return get_length_mbs_utf8( src
, srclen
);
151 for (count
= dstlen
; count
&& (src
< srcend
); count
--, dst
++)
153 unsigned char ch
= *src
++;
154 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
159 len
= utf8_length
[ch
-0x80];
160 res
= ch
& utf8_mask
[len
];
165 if (src
>= srcend
) goto done
; /* ignore partial char */
166 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
167 res
= (res
<< 6) | ch
;
170 if (src
>= srcend
) goto done
; /* ignore partial char */
171 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
172 res
= (res
<< 6) | ch
;
175 if (src
>= srcend
) goto done
; /* ignore partial char */
176 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
177 res
= (res
<< 6) | ch
;
180 if (src
>= srcend
) goto done
; /* ignore partial char */
181 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
182 res
= (res
<< 6) | ch
;
185 if (src
>= srcend
) goto done
; /* ignore partial char */
186 if ((ch
= *src
^ 0x80) >= 0x40) goto bad
;
187 res
= (res
<< 6) | ch
;
189 if (res
< utf8_minval
[len
]) goto bad
;
190 if (res
>= 0x10000) goto bad
; /* FIXME: maybe we should do surrogates here */
195 if (flags
& MB_ERR_INVALID_CHARS
) return -2; /* bad char */
198 if (src
< srcend
) return -1; /* overflow */
200 return dstlen
- count
;