2005-04-16 09:09:33 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2005 John M Bell <jmb202@ecs.soton.ac.uk>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-04-16 09:09:33 +04:00
|
|
|
*/
|
|
|
|
|
2005-07-02 22:18:48 +04:00
|
|
|
/** \file
|
|
|
|
* UTF-8 manipulation functions (implementation).
|
2005-04-16 09:09:33 +04:00
|
|
|
*/
|
|
|
|
|
2005-08-21 03:04:35 +04:00
|
|
|
#define _GNU_SOURCE /* for strndup */
|
2005-04-16 09:09:33 +04:00
|
|
|
#include <assert.h>
|
2005-06-08 01:29:26 +04:00
|
|
|
#include <errno.h>
|
2005-04-16 12:20:20 +04:00
|
|
|
#include <stdlib.h>
|
2005-04-16 09:09:33 +04:00
|
|
|
#include <string.h>
|
2007-06-16 02:07:11 +04:00
|
|
|
#define LIBICONV_PLUG
|
2005-04-16 09:09:33 +04:00
|
|
|
#include <iconv.h>
|
|
|
|
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/utf8.h"
|
2005-04-16 09:09:33 +04:00
|
|
|
|
2005-06-08 01:29:26 +04:00
|
|
|
static utf8_convert_ret utf8_convert(const char *string, size_t len,
|
|
|
|
const char *from, const char *to, char **result);
|
2005-04-16 09:09:33 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a UTF-8 multibyte sequence into a single UCS4 character
|
|
|
|
*
|
|
|
|
* Encoding of UCS values outside the UTF-16 plane has been removed from
|
2005-06-08 01:29:26 +04:00
|
|
|
* RFC3629. This function conforms to RFC2279, however.
|
2005-04-16 09:09:33 +04:00
|
|
|
*
|
|
|
|
* \param s The sequence to process
|
|
|
|
* \param l Length of sequence
|
|
|
|
* \return UCS4 character
|
|
|
|
*/
|
2008-02-25 20:58:00 +03:00
|
|
|
size_t utf8_to_ucs4(const char *s_in, size_t l)
|
2005-04-16 09:09:33 +04:00
|
|
|
{
|
|
|
|
size_t c = 0;
|
2008-02-25 20:58:00 +03:00
|
|
|
const unsigned char *s = (const unsigned char *) s_in;
|
2005-04-16 09:09:33 +04:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
assert(0);
|
|
|
|
else if (l > 0 && *s < 0x80)
|
|
|
|
c = *s;
|
|
|
|
else if (l > 1 && (*s & 0xE0) == 0xC0 && (*(s+1) & 0xC0) == 0x80)
|
|
|
|
c = ((*s & 0x1F) << 6) | (*(s+1) & 0x3F);
|
|
|
|
else if (l > 2 && (*s & 0xF0) == 0xE0 && (*(s+1) & 0xC0) == 0x80 &&
|
|
|
|
(*(s+2) & 0xC0) == 0x80)
|
|
|
|
c = ((*s & 0x0F) << 12) | ((*(s+1) & 0x3F) << 6) |
|
|
|
|
(*(s+2) & 0x3F);
|
|
|
|
else if (l > 3 && (*s & 0xF8) == 0xF0 && (*(s+1) & 0xC0) == 0x80 &&
|
|
|
|
(*(s+2) & 0xC0) == 0x80 && (*(s+3) & 0xC0) == 0x80)
|
|
|
|
c = ((*s & 0x0F) << 18) | ((*(s+1) & 0x3F) << 12) |
|
|
|
|
((*(s+2) & 0x3F) << 6) | (*(s+3) & 0x3F);
|
|
|
|
else if (l > 4 && (*s & 0xFC) == 0xF8 && (*(s+1) & 0xC0) == 0x80 &&
|
|
|
|
(*(s+2) & 0xC0) == 0x80 && (*(s+3) & 0xC0) == 0x80 &&
|
|
|
|
(*(s+4) & 0xC0) == 0x80)
|
|
|
|
c = ((*s & 0x0F) << 24) | ((*(s+1) & 0x3F) << 18) |
|
|
|
|
((*(s+2) & 0x3F) << 12) | ((*(s+3) & 0x3F) << 6) |
|
|
|
|
(*(s+4) & 0x3F);
|
|
|
|
else if (l > 5 && (*s & 0xFE) == 0xFC && (*(s+1) & 0xC0) == 0x80 &&
|
|
|
|
(*(s+2) & 0xC0) == 0x80 && (*(s+3) & 0xC0) == 0x80 &&
|
|
|
|
(*(s+4) & 0xC0) == 0x80 && (*(s+5) & 0xC0) == 0x80)
|
|
|
|
c = ((*s & 0x0F) << 28) | ((*(s+1) & 0x3F) << 24) |
|
|
|
|
((*(s+2) & 0x3F) << 18) | ((*(s+3) & 0x3F) << 12) |
|
|
|
|
((*(s+4) & 0x3F) << 6) | (*(s+5) & 0x3F);
|
|
|
|
else
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a single UCS4 character into a UTF-8 multibyte sequence
|
|
|
|
*
|
|
|
|
* Encoding of UCS values outside the UTF-16 plane has been removed from
|
2005-06-08 01:29:26 +04:00
|
|
|
* RFC3629. This function conforms to RFC2279, however.
|
2005-04-16 09:09:33 +04:00
|
|
|
*
|
|
|
|
* \param c The character to process (0 <= c <= 0x7FFFFFFF)
|
|
|
|
* \param s Pointer to 6 byte long output buffer
|
|
|
|
* \return Length of multibyte sequence
|
|
|
|
*/
|
|
|
|
size_t utf8_from_ucs4(size_t c, char *s)
|
|
|
|
{
|
|
|
|
size_t l = 0;
|
|
|
|
|
2005-05-24 02:27:37 +04:00
|
|
|
if (c > 0x7FFFFFFF || s == NULL)
|
2005-04-16 09:09:33 +04:00
|
|
|
assert(0);
|
|
|
|
else if (c < 0x80) {
|
|
|
|
*s = (char)c;
|
|
|
|
l = 1;
|
|
|
|
}
|
|
|
|
else if (c < 0x800) {
|
|
|
|
*s = 0xC0 | ((c >> 6) & 0x1F);
|
|
|
|
*(s+1) = 0x80 | (c & 0x3F);
|
|
|
|
l = 2;
|
|
|
|
}
|
|
|
|
else if (c < 0x10000) {
|
|
|
|
*s = 0xE0 | ((c >> 12) & 0xF);
|
|
|
|
*(s+1) = 0x80 | ((c >> 6) & 0x3F);
|
|
|
|
*(s+2) = 0x80 | (c & 0x3F);
|
|
|
|
l = 3;
|
|
|
|
}
|
|
|
|
else if (c < 0x200000) {
|
|
|
|
*s = 0xF0 | ((c >> 18) & 0x7);
|
|
|
|
*(s+1) = 0x80 | ((c >> 12) & 0x3F);
|
|
|
|
*(s+2) = 0x80 | ((c >> 6) & 0x3F);
|
|
|
|
*(s+3) = 0x80 | (c & 0x3F);
|
|
|
|
l = 4;
|
|
|
|
}
|
|
|
|
else if (c < 0x4000000) {
|
|
|
|
*s = 0xF8 | ((c >> 24) & 0x3);
|
|
|
|
*(s+1) = 0x80 | ((c >> 18) & 0x3F);
|
|
|
|
*(s+2) = 0x80 | ((c >> 12) & 0x3F);
|
|
|
|
*(s+3) = 0x80 | ((c >> 6) & 0x3F);
|
|
|
|
*(s+4) = 0x80 | (c & 0x3F);
|
|
|
|
l = 5;
|
|
|
|
}
|
|
|
|
else if (c <= 0x7FFFFFFF) {
|
|
|
|
*s = 0xFC | ((c >> 30) & 0x1);
|
|
|
|
*(s+1) = 0x80 | ((c >> 24) & 0x3F);
|
|
|
|
*(s+2) = 0x80 | ((c >> 18) & 0x3F);
|
|
|
|
*(s+3) = 0x80 | ((c >> 12) & 0x3F);
|
|
|
|
*(s+4) = 0x80 | ((c >> 6) & 0x3F);
|
|
|
|
*(s+5) = 0x80 | (c & 0x3F);
|
|
|
|
l = 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the length (in characters) of a NULL-terminated UTF-8 string
|
|
|
|
*
|
|
|
|
* \param s The string
|
|
|
|
* \return Length of string
|
|
|
|
*/
|
|
|
|
size_t utf8_length(const char *s)
|
|
|
|
{
|
|
|
|
const char *__s = s;
|
|
|
|
int l = 0;
|
2005-05-24 02:27:37 +04:00
|
|
|
|
|
|
|
assert(__s != NULL);
|
|
|
|
|
2005-04-16 09:09:33 +04:00
|
|
|
while (*__s != '\0') {
|
|
|
|
if ((*__s & 0x80) == 0x00)
|
|
|
|
__s += 1;
|
|
|
|
else if ((*__s & 0xE0) == 0xC0)
|
|
|
|
__s += 2;
|
|
|
|
else if ((*__s & 0xF0) == 0xE0)
|
|
|
|
__s += 3;
|
|
|
|
else if ((*__s & 0xF8) == 0xF0)
|
|
|
|
__s += 4;
|
|
|
|
else if ((*__s & 0xFC) == 0xF8)
|
|
|
|
__s += 5;
|
|
|
|
else if ((*__s & 0xFE) == 0xFC)
|
|
|
|
__s += 6;
|
|
|
|
else
|
|
|
|
assert(0);
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find previous legal UTF-8 char in string
|
|
|
|
*
|
|
|
|
* \param s The string
|
|
|
|
* \param o Offset in the string to start at
|
|
|
|
* \return Offset of first byte of previous legal character
|
|
|
|
*/
|
|
|
|
size_t utf8_prev(const char *s, size_t o)
|
|
|
|
{
|
2005-05-24 02:27:37 +04:00
|
|
|
assert(s != NULL);
|
|
|
|
|
2005-07-16 20:15:37 +04:00
|
|
|
while (o != 0 && (s[--o] & 0xC0) == 0x80)
|
2005-04-16 09:09:33 +04:00
|
|
|
/* do nothing */;
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find next legal UTF-8 char in string
|
|
|
|
*
|
|
|
|
* \param s The string
|
|
|
|
* \param l Maximum offset in string
|
|
|
|
* \param o Offset in the string to start at
|
|
|
|
* \return Offset of first byte of next legal character
|
|
|
|
*/
|
|
|
|
size_t utf8_next(const char *s, size_t l, size_t o)
|
|
|
|
{
|
2005-05-24 02:27:37 +04:00
|
|
|
assert(s != NULL);
|
|
|
|
|
2005-07-16 20:15:37 +04:00
|
|
|
while (o != l && (s[++o] & 0xC0) == 0x80)
|
2005-04-16 09:09:33 +04:00
|
|
|
/* do nothing */;
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2005-07-11 22:10:10 +04:00
|
|
|
/* Cache of previous iconv conversion descriptor used by utf8_convert */
|
|
|
|
static struct {
|
|
|
|
char from[32]; /**< Encoding name to convert from */
|
|
|
|
char to[32]; /**< Encoding name to convert to */
|
|
|
|
iconv_t cd; /**< Iconv conversion descriptor */
|
|
|
|
} last_cd;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalise the UTF-8 library
|
|
|
|
*/
|
|
|
|
void utf8_finalise(void)
|
|
|
|
{
|
|
|
|
if (last_cd.cd != 0)
|
|
|
|
iconv_close(last_cd.cd);
|
|
|
|
|
|
|
|
/* paranoia follows */
|
|
|
|
last_cd.from[0] = '\0';
|
|
|
|
last_cd.to[0] = '\0';
|
|
|
|
last_cd.cd = 0;
|
|
|
|
}
|
2005-04-16 09:09:33 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a UTF8 string into the named encoding
|
|
|
|
*
|
|
|
|
* \param string The NULL-terminated string to convert
|
|
|
|
* \param encname The encoding name (suitable for passing to iconv)
|
|
|
|
* \param len Length of input string to consider (in bytes), or 0
|
2005-06-08 01:29:26 +04:00
|
|
|
* \param result Pointer to location to store result (allocated on heap)
|
|
|
|
* \return Appropriate utf8_convert_ret value
|
2005-04-16 09:09:33 +04:00
|
|
|
*/
|
2005-06-08 01:29:26 +04:00
|
|
|
utf8_convert_ret utf8_to_enc(const char *string, const char *encname,
|
|
|
|
size_t len, char **result)
|
2005-04-16 09:09:33 +04:00
|
|
|
{
|
2005-06-08 01:29:26 +04:00
|
|
|
return utf8_convert(string, len, "UTF-8", encname, result);
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-08 01:29:26 +04:00
|
|
|
* Convert a string in the named encoding into a UTF-8 string
|
2005-04-16 09:09:33 +04:00
|
|
|
*
|
|
|
|
* \param string The NULL-terminated string to convert
|
|
|
|
* \param encname The encoding name (suitable for passing to iconv)
|
|
|
|
* \param len Length of input string to consider (in bytes), or 0
|
2005-06-08 01:29:26 +04:00
|
|
|
* \param result Pointer to location to store result (allocated on heap)
|
|
|
|
* \return Appropriate utf8_convert_ret value
|
2005-04-16 09:09:33 +04:00
|
|
|
*/
|
2005-06-08 01:29:26 +04:00
|
|
|
utf8_convert_ret utf8_from_enc(const char *string, const char *encname,
|
|
|
|
size_t len, char **result)
|
2005-04-16 09:09:33 +04:00
|
|
|
{
|
2005-06-08 01:29:26 +04:00
|
|
|
return utf8_convert(string, len, encname, "UTF-8", result);
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a string from one encoding to another
|
|
|
|
*
|
|
|
|
* \param string The NULL-terminated string to convert
|
|
|
|
* \param len Length of input string to consider (in bytes)
|
|
|
|
* \param from The encoding name to convert from
|
|
|
|
* \param to The encoding name to convert to
|
2005-06-08 01:29:26 +04:00
|
|
|
* \param result Pointer to location in which to store result
|
|
|
|
* \return Appropriate utf8_convert_ret value
|
2005-04-16 09:09:33 +04:00
|
|
|
*/
|
2005-06-08 01:29:26 +04:00
|
|
|
utf8_convert_ret utf8_convert(const char *string, size_t len,
|
|
|
|
const char *from, const char *to, char **result)
|
2005-04-16 09:09:33 +04:00
|
|
|
{
|
|
|
|
iconv_t cd;
|
2005-07-02 18:35:43 +04:00
|
|
|
char *temp, *out, *in;
|
2005-04-16 09:09:33 +04:00
|
|
|
size_t slen, rlen;
|
|
|
|
|
2005-06-08 01:29:26 +04:00
|
|
|
assert(string && from && to && result);
|
2005-04-16 09:09:33 +04:00
|
|
|
|
2005-06-29 03:46:16 +04:00
|
|
|
if (strcasecmp(from, to) == 0) {
|
|
|
|
/* conversion from an encoding to itself == strdup */
|
|
|
|
slen = len ? len : strlen(string);
|
2005-07-02 18:35:43 +04:00
|
|
|
*(result) = strndup(string, slen);
|
|
|
|
if (!(*result)) {
|
|
|
|
*(result) = NULL;
|
2005-06-29 03:46:16 +04:00
|
|
|
return UTF8_CONVERT_NOMEM;
|
2005-07-02 18:35:43 +04:00
|
|
|
}
|
2005-06-29 03:46:16 +04:00
|
|
|
|
|
|
|
return UTF8_CONVERT_OK;
|
|
|
|
}
|
|
|
|
|
2005-04-16 09:09:33 +04:00
|
|
|
in = (char *)string;
|
|
|
|
|
2005-07-11 22:10:10 +04:00
|
|
|
/* we cache the last used conversion descriptor,
|
|
|
|
* so check if we're trying to use it here */
|
|
|
|
if (strncasecmp(last_cd.from, from, 32) == 0 &&
|
|
|
|
strncasecmp(last_cd.to, to, 32) == 0) {
|
|
|
|
cd = last_cd.cd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* no match, so create a new cd */
|
|
|
|
cd = iconv_open(to, from);
|
|
|
|
if (cd == (iconv_t)-1) {
|
|
|
|
if (errno == EINVAL)
|
|
|
|
return UTF8_CONVERT_BADENC;
|
|
|
|
/* default to no memory */
|
|
|
|
return UTF8_CONVERT_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close the last cd - we don't care if this fails */
|
2006-03-17 02:30:46 +03:00
|
|
|
if (last_cd.cd)
|
|
|
|
iconv_close(last_cd.cd);
|
2005-07-11 22:10:10 +04:00
|
|
|
|
|
|
|
/* and copy the to/from/cd data into last_cd */
|
|
|
|
strncpy(last_cd.from, from, 32);
|
|
|
|
strncpy(last_cd.to, to, 32);
|
|
|
|
last_cd.cd = cd;
|
2005-06-08 01:29:26 +04:00
|
|
|
}
|
2005-04-16 09:09:33 +04:00
|
|
|
|
|
|
|
slen = len ? len : strlen(string);
|
|
|
|
/* Worst case = ACSII -> UCS4, so allocate an output buffer
|
|
|
|
* 4 times larger than the input buffer, and add 4 bytes at
|
|
|
|
* the end for the NULL terminator
|
|
|
|
*/
|
|
|
|
rlen = slen * 4 + 4;
|
|
|
|
|
2005-07-02 18:35:43 +04:00
|
|
|
temp = out = malloc(rlen);
|
2005-07-11 22:10:10 +04:00
|
|
|
if (!out)
|
2005-06-08 01:29:26 +04:00
|
|
|
return UTF8_CONVERT_NOMEM;
|
2005-04-16 09:09:33 +04:00
|
|
|
|
|
|
|
/* perform conversion */
|
|
|
|
if (iconv(cd, &in, &slen, &out, &rlen) == (size_t)-1) {
|
|
|
|
free(temp);
|
2006-02-11 21:11:09 +03:00
|
|
|
/* clear the cached conversion descriptor as it's invalid */
|
|
|
|
last_cd.from[0] = '\0';
|
|
|
|
last_cd.to[0] = '\0';
|
|
|
|
last_cd.cd = 0;
|
2005-06-08 01:29:26 +04:00
|
|
|
/** \todo handle the various cases properly
|
|
|
|
* There are 3 possible error cases:
|
|
|
|
* a) Insufficiently large output buffer
|
|
|
|
* b) Invalid input byte sequence
|
|
|
|
* c) Incomplete input sequence */
|
|
|
|
return UTF8_CONVERT_NOMEM;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
2005-07-02 18:35:43 +04:00
|
|
|
*(result) = realloc(temp, out - temp + 4);
|
|
|
|
if (!(*result)) {
|
2005-04-16 09:09:33 +04:00
|
|
|
free(temp);
|
2005-07-02 18:35:43 +04:00
|
|
|
*(result) = NULL; /* for sanity's sake */
|
|
|
|
return UTF8_CONVERT_NOMEM;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
2005-07-02 18:35:43 +04:00
|
|
|
/* NULL terminate - needs 4 characters as we may have
|
|
|
|
* converted to UTF-32 */
|
|
|
|
memset((*result) + (out - temp), 0, 4);
|
2005-06-08 01:29:26 +04:00
|
|
|
|
|
|
|
return UTF8_CONVERT_OK;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|