* UTF-8 support routines
*
* Copyright 2000 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <string.h>
#include "wine/unicode.h"
extern WCHAR wine_compose( const WCHAR *str ) DECLSPEC_HIDDEN;
static const char utf8_length[128] =
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0
};
static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
static inline unsigned int get_surrogate_value( const WCHAR *src, unsigned int srclen )
{
if (src[0] >= 0xd800 && src[0] <= 0xdfff)
{
if (src[0] > 0xdbff ||
srclen <= 1 ||
src[1] < 0xdc00 || src[1] > 0xdfff)
return 0;
return 0x10000 + ((src[0] & 0x3ff) << 10) + (src[1] & 0x3ff);
}
return src[0];
}
static inline int get_length_wcs_utf8( int flags, const WCHAR *src, unsigned int srclen )
{
int len;
unsigned int val;
for (len = 0; srclen; srclen--, src++)
{
if (*src < 0x80)
{
len++;
continue;
}
if (*src < 0x800)
{
len += 2;
continue;
}
if (!(val = get_surrogate_value( src, srclen )))
{
if (flags & WC_ERR_INVALID_CHARS) return -2;
continue;
}
if (val < 0x10000)
len += 3;
else
{
len += 4;
src++;
srclen--;
}
}
return len;
}
int wine_utf8_wcstombs( int flags, const WCHAR *src, int srclen, char *dst, int dstlen )
{
int len;
if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
for (len = dstlen; srclen; srclen--, src++)
{
WCHAR ch = *src;
unsigned int val;
if (ch < 0x80)
{
if (!len--) return -1;
*dst++ = ch;
continue;
}
if (ch < 0x800)
{
if ((len -= 2) < 0) return -1;
dst[1] = 0x80 | (ch & 0x3f);
ch >>= 6;
dst[0] = 0xc0 | ch;
dst += 2;
continue;
}
if (!(val = get_surrogate_value( src, srclen )))
{
if (flags & WC_ERR_INVALID_CHARS) return -2;
continue;
}
if (val < 0x10000)
{
if ((len -= 3) < 0) return -1;
dst[2] = 0x80 | (val & 0x3f);
val >>= 6;
dst[1] = 0x80 | (val & 0x3f);
val >>= 6;
dst[0] = 0xe0 | val;
dst += 3;
}
else
{
if ((len -= 4) < 0) return -1;
dst[3] = 0x80 | (val & 0x3f);
val >>= 6;
dst[2] = 0x80 | (val & 0x3f);
val >>= 6;
dst[1] = 0x80 | (val & 0x3f);
val >>= 6;
dst[0] = 0xf0 | val;
dst += 4;
src++;
srclen--;
}
}
return dstlen - len;
}
static inline unsigned int decode_utf8_char( unsigned char ch, const char **str, const char *strend )
{
unsigned int len = utf8_length[ch-0x80];
unsigned int res = ch & utf8_mask[len];
const char *end = *str + len;
if (end > strend) return ~0;
switch(len)
{
case 3:
if ((ch = end[-3] ^ 0x80) >= 0x40) break;
res = (res << 6) | ch;
(*str)++;
case 2:
if ((ch = end[-2] ^ 0x80) >= 0x40) break;
res = (res << 6) | ch;
(*str)++;
case 1:
if ((ch = end[-1] ^ 0x80) >= 0x40) break;
res = (res << 6) | ch;
(*str)++;
if (res < utf8_minval[len]) break;
return res;
}
return ~0;
}
static inline int get_length_mbs_utf8_compose( int flags, const char *src, int srclen )
{
int ret = 0;
unsigned int res;
WCHAR composed[2];
const char *srcend = src + srclen;
composed[0] = 0;
while (src < srcend)
{
unsigned char ch = *src++;
if (ch < 0x80)
{
composed[0] = ch;
ret++;
continue;
}
if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
{
if (composed[0])
{
composed[1] = res;
if ((composed[0] = wine_compose( composed ))) continue;
}
composed[0] = res;
ret++;
}
else if (res <= 0x10ffff)
{
ret += 2;
composed[0] = 0;
}
else if (flags & MB_ERR_INVALID_CHARS) return -2;
}
return ret;
}
static int utf8_mbstowcs_compose( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
{
unsigned int res;
const char *srcend = src + srclen;
WCHAR composed[2];
WCHAR *dstend = dst + dstlen;
if (!dstlen) return get_length_mbs_utf8_compose( flags, src, srclen );
composed[0] = 0;
while (src < srcend)
{
unsigned char ch = *src++;
if (ch < 0x80)
{
if (dst >= dstend) return -1;
*dst++ = composed[0] = ch;
continue;
}
if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
{
if (composed[0])
{
composed[1] = res;
if ((composed[0] = wine_compose( composed )))
{
dst[-1] = composed[0];
continue;
}
}
if (dst >= dstend) return -1;
*dst++ = composed[0] = res;
}
else if (res <= 0x10ffff)
{
if (dst >= dstend - 1) return -1;
res -= 0x10000;
*dst++ = 0xd800 | (res >> 10);
*dst++ = 0xdc00 | (res & 0x3ff);
composed[0] = 0;
}
else if (flags & MB_ERR_INVALID_CHARS) return -2;
}
return dstlen - (dstend - dst);
}
static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
{
int ret = 0;
unsigned int res;
const char *srcend = src + srclen;
while (src < srcend)
{
unsigned char ch = *src++;
if (ch < 0x80)
{
ret++;
continue;
}
if ((res = decode_utf8_char( ch, &src, srcend )) <= 0x10ffff)
{
if (res > 0xffff) ret++;
ret++;
}
else if (flags & MB_ERR_INVALID_CHARS) return -2;
}
return ret;
}
int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
{
unsigned int res;
const char *srcend = src + srclen;
WCHAR *dstend = dst + dstlen;
if (flags & MB_COMPOSITE) return utf8_mbstowcs_compose( flags, src, srclen, dst, dstlen );
if (!dstlen) return get_length_mbs_utf8( flags, src, srclen );
while ((dst < dstend) && (src < srcend))
{
unsigned char ch = *src++;
if (ch < 0x80)
{
*dst++ = ch;
continue;
}
if ((res = decode_utf8_char( ch, &src, srcend )) <= 0xffff)
{
*dst++ = res;
}
else if (res <= 0x10ffff)
{
if (dst == dstend - 1) return -1;
res -= 0x10000;
*dst++ = 0xd800 | (res >> 10);
*dst++ = 0xdc00 | (res & 0x3ff);
}
else if (flags & MB_ERR_INVALID_CHARS) return -2;
}
if (src < srcend) return -1;
return dstlen - (dstend - dst);
}