2000-08-11 02:44:33 +02:00
|
|
|
/*
|
|
|
|
* UTF-8 support routines
|
|
|
|
*
|
|
|
|
* Copyright 2000 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-08-11 02:44:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
|
|
|
|
/* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
|
|
|
|
static const char utf8_length[128] =
|
|
|
|
{
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
|
2007-03-14 15:58:56 +01:00
|
|
|
0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
|
2000-08-11 02:44:33 +02:00
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
|
|
|
|
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
|
2007-03-14 15:58:56 +01:00
|
|
|
3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
|
2000-08-11 02:44:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* first byte mask depending on UTF-8 sequence length */
|
2007-03-14 15:58:56 +01:00
|
|
|
static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
|
2000-08-11 02:44:33 +02:00
|
|
|
|
|
|
|
/* minimum Unicode value depending on UTF-8 sequence length */
|
2007-03-14 15:58:56 +01:00
|
|
|
static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
|
2000-08-11 02:44:33 +02:00
|
|
|
|
|
|
|
|
2007-03-14 15:58:56 +01:00
|
|
|
/* get the next char value taking surrogates into account */
|
|
|
|
static inline unsigned int get_surrogate_value( const WCHAR *src, unsigned int srclen )
|
|
|
|
{
|
|
|
|
if (src[0] >= 0xd800 && src[0] <= 0xdfff) /* surrogate pair */
|
|
|
|
{
|
|
|
|
if (src[0] > 0xdbff || /* invalid high surrogate */
|
|
|
|
srclen <= 1 || /* missing low surrogate */
|
|
|
|
src[1] < 0xdc00 || src[1] > 0xdfff) /* invalid low surrogate */
|
|
|
|
return 0;
|
|
|
|
return 0x10000 + ((src[0] & 0x3ff) << 10) + (src[1] & 0x3ff);
|
|
|
|
}
|
|
|
|
return src[0];
|
|
|
|
}
|
|
|
|
|
2000-08-11 02:44:33 +02:00
|
|
|
/* query necessary dst length for src string */
|
2007-03-14 15:58:56 +01:00
|
|
|
static inline int get_length_wcs_utf8( int flags, const WCHAR *src, unsigned int srclen )
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
|
|
|
int len;
|
2007-03-14 15:58:56 +01:00
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
for (len = 0; srclen; srclen--, src++)
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
2007-03-14 15:58:56 +01:00
|
|
|
if (*src < 0x80) /* 0x00-0x7f: 1 byte */
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
|
|
|
len++;
|
2007-03-14 15:58:56 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*src < 0x800) /* 0x80-0x7ff: 2 bytes */
|
|
|
|
{
|
|
|
|
len += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(val = get_surrogate_value( src, srclen )))
|
|
|
|
{
|
|
|
|
if (flags & WC_ERR_INVALID_CHARS) return -2;
|
|
|
|
continue;
|
2000-08-11 02:44:33 +02:00
|
|
|
}
|
2007-03-14 15:58:56 +01:00
|
|
|
if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
|
|
|
|
len += 3;
|
|
|
|
else /* 0x10000-0x10ffff: 4 bytes */
|
2008-01-21 12:40:57 +01:00
|
|
|
{
|
2007-03-14 15:58:56 +01:00
|
|
|
len += 4;
|
2008-01-21 12:40:57 +01:00
|
|
|
src++;
|
|
|
|
srclen--;
|
|
|
|
}
|
2000-08-11 02:44:33 +02:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wide char to UTF-8 string conversion */
|
2007-03-14 15:57:50 +01:00
|
|
|
/* return -1 on dst buffer overflow, -2 on invalid input char */
|
|
|
|
int wine_utf8_wcstombs( int flags, const WCHAR *src, int srclen, char *dst, int dstlen )
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
2003-05-08 23:05:50 +02:00
|
|
|
int len;
|
2000-08-11 02:44:33 +02:00
|
|
|
|
2007-03-14 15:58:56 +01:00
|
|
|
if (!dstlen) return get_length_wcs_utf8( flags, src, srclen );
|
2000-08-11 02:44:33 +02:00
|
|
|
|
2003-05-08 23:05:50 +02:00
|
|
|
for (len = dstlen; srclen; srclen--, src++)
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
|
|
|
WCHAR ch = *src;
|
2007-03-14 15:58:56 +01:00
|
|
|
unsigned int val;
|
2000-08-11 02:44:33 +02:00
|
|
|
|
|
|
|
if (ch < 0x80) /* 0x00-0x7f: 1 byte */
|
|
|
|
{
|
2003-05-08 23:05:50 +02:00
|
|
|
if (!len--) return -1; /* overflow */
|
2000-08-11 02:44:33 +02:00
|
|
|
*dst++ = ch;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
|
|
|
|
{
|
2003-05-08 23:05:50 +02:00
|
|
|
if ((len -= 2) < 0) return -1; /* overflow */
|
2000-08-11 02:44:33 +02:00
|
|
|
dst[1] = 0x80 | (ch & 0x3f);
|
|
|
|
ch >>= 6;
|
|
|
|
dst[0] = 0xc0 | ch;
|
|
|
|
dst += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-03-14 15:58:56 +01:00
|
|
|
if (!(val = get_surrogate_value( src, srclen )))
|
|
|
|
{
|
|
|
|
if (flags & WC_ERR_INVALID_CHARS) return -2;
|
|
|
|
continue;
|
|
|
|
}
|
2000-08-11 02:44:33 +02:00
|
|
|
|
2007-03-14 15:58:56 +01:00
|
|
|
if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
|
|
|
|
{
|
|
|
|
if ((len -= 3) < 0) return -1; /* overflow */
|
|
|
|
dst[2] = 0x80 | (val & 0x3f);
|
|
|
|
val >>= 6;
|
|
|
|
dst[1] = 0x80 | (val & 0x3f);
|
|
|
|
val >>= 6;
|
|
|
|
dst[0] = 0xe0 | val;
|
|
|
|
dst += 3;
|
|
|
|
}
|
|
|
|
else /* 0x10000-0x10ffff: 4 bytes */
|
|
|
|
{
|
|
|
|
if ((len -= 4) < 0) return -1; /* overflow */
|
|
|
|
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;
|
2008-01-21 12:40:57 +01:00
|
|
|
src++;
|
|
|
|
srclen--;
|
2007-03-14 15:58:56 +01:00
|
|
|
}
|
2000-08-11 02:44:33 +02:00
|
|
|
}
|
2003-05-08 23:05:50 +02:00
|
|
|
return dstlen - len;
|
2000-08-11 02:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* query necessary dst length for src string */
|
2007-03-26 21:26:29 +02:00
|
|
|
static inline int get_length_mbs_utf8( int flags, const char *src, int srclen )
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
2007-03-14 15:58:56 +01:00
|
|
|
int len, ret = 0;
|
|
|
|
unsigned int res;
|
|
|
|
const char *srcend = src + srclen;
|
2000-08-11 02:44:33 +02:00
|
|
|
|
2007-03-14 15:58:56 +01:00
|
|
|
while (src < srcend)
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
|
|
|
unsigned char ch = *src++;
|
2007-03-14 15:58:56 +01:00
|
|
|
if (ch < 0x80) /* special fast case for 7-bit ASCII */
|
|
|
|
{
|
|
|
|
ret++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
len = utf8_length[ch-0x80];
|
|
|
|
if (src + len > srcend) goto bad;
|
|
|
|
res = ch & utf8_mask[len];
|
2000-08-11 02:44:33 +02:00
|
|
|
|
2007-03-14 15:58:56 +01:00
|
|
|
switch(len)
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
|
|
|
case 3:
|
2007-03-14 15:58:56 +01:00
|
|
|
if ((ch = *src ^ 0x80) >= 0x40) goto bad;
|
|
|
|
res = (res << 6) | ch;
|
2000-08-11 02:44:33 +02:00
|
|
|
src++;
|
|
|
|
case 2:
|
2007-03-14 15:58:56 +01:00
|
|
|
if ((ch = *src ^ 0x80) >= 0x40) goto bad;
|
|
|
|
res = (res << 6) | ch;
|
2000-08-11 02:44:33 +02:00
|
|
|
src++;
|
|
|
|
case 1:
|
2007-03-14 15:58:56 +01:00
|
|
|
if ((ch = *src ^ 0x80) >= 0x40) goto bad;
|
|
|
|
res = (res << 6) | ch;
|
2000-08-11 02:44:33 +02:00
|
|
|
src++;
|
2007-03-14 15:58:56 +01:00
|
|
|
if (res < utf8_minval[len]) goto bad;
|
|
|
|
if (res > 0x10ffff) goto bad;
|
|
|
|
if (res > 0xffff) ret++;
|
|
|
|
ret++;
|
|
|
|
continue;
|
2000-08-11 02:44:33 +02:00
|
|
|
}
|
2007-03-14 15:58:56 +01:00
|
|
|
bad:
|
|
|
|
if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
|
|
|
|
/* otherwise ignore it */
|
2000-08-11 02:44:33 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* UTF-8 to wide char string conversion */
|
|
|
|
/* return -1 on dst buffer overflow, -2 on invalid input char */
|
2003-03-21 22:30:51 +01:00
|
|
|
int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen )
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
2007-03-14 15:58:56 +01:00
|
|
|
int len;
|
2000-08-11 02:44:33 +02:00
|
|
|
unsigned int res;
|
|
|
|
const char *srcend = src + srclen;
|
2007-03-14 15:58:56 +01:00
|
|
|
WCHAR *dstend = dst + dstlen;
|
2000-08-11 02:44:33 +02:00
|
|
|
|
2007-03-14 15:58:56 +01:00
|
|
|
if (!dstlen) return get_length_mbs_utf8( flags, src, srclen );
|
2000-08-11 02:44:33 +02:00
|
|
|
|
2007-03-14 15:58:56 +01:00
|
|
|
while ((dst < dstend) && (src < srcend))
|
2000-08-11 02:44:33 +02:00
|
|
|
{
|
|
|
|
unsigned char ch = *src++;
|
|
|
|
if (ch < 0x80) /* special fast case for 7-bit ASCII */
|
|
|
|
{
|
2007-03-14 15:58:56 +01:00
|
|
|
*dst++ = ch;
|
2000-08-11 02:44:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
len = utf8_length[ch-0x80];
|
2007-03-14 15:58:56 +01:00
|
|
|
if (src + len > srcend) goto bad;
|
2000-08-11 02:44:33 +02:00
|
|
|
res = ch & utf8_mask[len];
|
|
|
|
|
|
|
|
switch(len)
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
if ((ch = *src ^ 0x80) >= 0x40) goto bad;
|
|
|
|
res = (res << 6) | ch;
|
|
|
|
src++;
|
|
|
|
case 2:
|
|
|
|
if ((ch = *src ^ 0x80) >= 0x40) goto bad;
|
|
|
|
res = (res << 6) | ch;
|
|
|
|
src++;
|
|
|
|
case 1:
|
|
|
|
if ((ch = *src ^ 0x80) >= 0x40) goto bad;
|
|
|
|
res = (res << 6) | ch;
|
|
|
|
src++;
|
|
|
|
if (res < utf8_minval[len]) goto bad;
|
2007-03-14 15:58:56 +01:00
|
|
|
if (res > 0x10ffff) goto bad;
|
|
|
|
if (res <= 0xffff) *dst++ = res;
|
|
|
|
else /* we need surrogates */
|
|
|
|
{
|
|
|
|
if (dst == dstend - 1) return -1; /* overflow */
|
|
|
|
res -= 0x10000;
|
|
|
|
*dst++ = 0xd800 | (res >> 10);
|
|
|
|
*dst++ = 0xdc00 | (res & 0x3ff);
|
|
|
|
}
|
2000-08-11 02:44:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bad:
|
|
|
|
if (flags & MB_ERR_INVALID_CHARS) return -2; /* bad char */
|
2007-03-14 15:58:56 +01:00
|
|
|
/* otherwise ignore it */
|
2000-08-11 02:44:33 +02:00
|
|
|
}
|
|
|
|
if (src < srcend) return -1; /* overflow */
|
2007-03-14 15:58:56 +01:00
|
|
|
return dstlen - (dstend - dst);
|
2000-08-11 02:44:33 +02:00
|
|
|
}
|