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
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2008-08-11 20:44:12 +04:00
|
|
|
#include <strings.h>
|
2005-04-16 09:09:33 +04:00
|
|
|
#include <iconv.h>
|
2009-02-18 02:44:17 +03:00
|
|
|
#include <parserutils/charset/utf8.h>
|
|
|
|
|
2008-08-11 20:44:12 +04:00
|
|
|
#include "utils/config.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
|
|
|
|
2016-05-30 13:20:15 +03:00
|
|
|
#include "netsurf/utf8.h"
|
2014-10-16 12:48:09 +04:00
|
|
|
#include "desktop/gui_internal.h"
|
|
|
|
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2009-02-18 02:44:17 +03:00
|
|
|
uint32_t utf8_to_ucs4(const char *s_in, size_t l)
|
2005-04-16 09:09:33 +04:00
|
|
|
{
|
2009-02-18 02:44:17 +03:00
|
|
|
uint32_t ucs4;
|
|
|
|
size_t len;
|
|
|
|
parserutils_error perror;
|
2005-04-16 09:09:33 +04:00
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
perror = parserutils_charset_utf8_to_ucs4((const uint8_t *) s_in, l,
|
2009-02-18 02:44:17 +03:00
|
|
|
&ucs4, &len);
|
|
|
|
if (perror != PARSERUTILS_OK)
|
|
|
|
ucs4 = 0xfffd;
|
|
|
|
|
|
|
|
return ucs4;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2009-02-18 02:44:17 +03:00
|
|
|
size_t utf8_from_ucs4(uint32_t c, char *s)
|
2005-04-16 09:09:33 +04:00
|
|
|
{
|
2009-02-18 02:44:17 +03:00
|
|
|
uint8_t *in = (uint8_t *) s;
|
|
|
|
size_t len = 6;
|
|
|
|
parserutils_error perror;
|
2005-04-16 09:09:33 +04:00
|
|
|
|
2009-02-18 02:44:17 +03:00
|
|
|
perror = parserutils_charset_utf8_from_ucs4(c, &in, &len);
|
|
|
|
if (perror != PARSERUTILS_OK) {
|
|
|
|
s[0] = 0xef;
|
|
|
|
s[1] = 0xbf;
|
|
|
|
s[2] = 0xbd;
|
|
|
|
return 3;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
2009-02-18 02:44:17 +03:00
|
|
|
|
2009-02-18 15:53:44 +03:00
|
|
|
return 6 - len;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2005-04-16 09:09:33 +04:00
|
|
|
size_t utf8_length(const char *s)
|
|
|
|
{
|
2009-02-18 02:44:17 +03:00
|
|
|
return utf8_bounded_length(s, strlen(s));
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2009-02-18 02:44:17 +03:00
|
|
|
size_t utf8_bounded_length(const char *s, size_t l)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
parserutils_error perror;
|
|
|
|
|
|
|
|
perror = parserutils_charset_utf8_length((const uint8_t *) s, l, &len);
|
|
|
|
if (perror != PARSERUTILS_OK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2013-03-18 02:01:42 +04:00
|
|
|
size_t utf8_bounded_byte_length(const char *s, size_t l, size_t c)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
while (len < l && c-- > 0)
|
|
|
|
len = utf8_next(s, l, len);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2009-02-18 02:44:17 +03:00
|
|
|
size_t utf8_char_byte_length(const char *s)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
parserutils_error perror;
|
|
|
|
|
|
|
|
perror = parserutils_charset_utf8_char_byte_length((const uint8_t *) s,
|
|
|
|
&len);
|
|
|
|
assert(perror == PARSERUTILS_OK);
|
|
|
|
|
|
|
|
return len;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2005-04-16 09:09:33 +04:00
|
|
|
size_t utf8_prev(const char *s, size_t o)
|
|
|
|
{
|
2009-02-18 02:44:17 +03:00
|
|
|
uint32_t prev;
|
|
|
|
parserutils_error perror;
|
|
|
|
|
|
|
|
perror = parserutils_charset_utf8_prev((const uint8_t *) s, o, &prev);
|
|
|
|
assert(perror == PARSERUTILS_OK);
|
|
|
|
|
|
|
|
return prev;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2005-04-16 09:09:33 +04:00
|
|
|
size_t utf8_next(const char *s, size_t l, size_t o)
|
|
|
|
{
|
2009-02-18 02:44:17 +03:00
|
|
|
uint32_t next;
|
|
|
|
parserutils_error perror;
|
2005-05-24 02:27:37 +04:00
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
perror = parserutils_charset_utf8_next((const uint8_t *) s, l, o,
|
2009-02-18 02:44:17 +03:00
|
|
|
&next);
|
|
|
|
assert(perror == PARSERUTILS_OK);
|
|
|
|
|
|
|
|
return next;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2013-05-16 02:30:01 +04:00
|
|
|
static inline void utf8_clear_cd_cache(void)
|
|
|
|
{
|
|
|
|
last_cd.from[0] = '\0';
|
|
|
|
last_cd.to[0] = '\0';
|
|
|
|
last_cd.cd = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
|
|
|
nserror utf8_finalise(void)
|
2005-07-11 22:10:10 +04:00
|
|
|
{
|
|
|
|
if (last_cd.cd != 0)
|
|
|
|
iconv_close(last_cd.cd);
|
|
|
|
|
|
|
|
/* paranoia follows */
|
2013-05-16 02:30:01 +04:00
|
|
|
utf8_clear_cd_cache();
|
2014-01-29 01:40:13 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
2005-07-11 22:10:10 +04:00
|
|
|
}
|
2005-04-16 09:09:33 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a string from one encoding to another
|
|
|
|
*
|
|
|
|
* \param string The NULL-terminated string to convert
|
2008-08-26 10:21:10 +04:00
|
|
|
* \param len Length of input string to consider (in bytes), or 0
|
2005-04-16 09:09:33 +04:00
|
|
|
* \param from The encoding name to convert from
|
|
|
|
* \param to The encoding name to convert to
|
2013-10-25 23:58:19 +04:00
|
|
|
* \param result Pointer to location in which to store result.
|
|
|
|
* \param result_len Pointer to location in which to store result length.
|
2014-01-29 01:40:13 +04:00
|
|
|
* \return NSERROR_OK for no error, NSERROR_NOMEM on allocation error,
|
|
|
|
* NSERROR_BAD_ENCODING for a bad character encoding
|
2005-04-16 09:09:33 +04:00
|
|
|
*/
|
2014-01-29 01:40:13 +04:00
|
|
|
static nserror
|
|
|
|
utf8_convert(const char *string,
|
|
|
|
size_t len,
|
|
|
|
const char *from,
|
|
|
|
const char *to,
|
|
|
|
char **result,
|
|
|
|
size_t *result_len)
|
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;
|
|
|
|
|
2008-08-26 10:21:10 +04:00
|
|
|
assert(string && from && to && result);
|
|
|
|
|
|
|
|
if (string[0] == '\0') {
|
2014-01-29 01:40:13 +04:00
|
|
|
/* On AmigaOS, iconv() returns an error if we pass an
|
|
|
|
* empty string. This prevents iconv() being called as
|
2008-08-26 10:21:10 +04:00
|
|
|
* there is no conversion necessary anyway. */
|
2008-08-26 04:53:02 +04:00
|
|
|
*result = strdup("");
|
2008-08-26 10:21:10 +04:00
|
|
|
if (!(*result)) {
|
|
|
|
*result = NULL;
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_NOMEM;
|
2008-08-26 04:53:02 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_OK;
|
2008-08-26 10:21:10 +04:00
|
|
|
}
|
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;
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_NOMEM;
|
2005-07-02 18:35:43 +04:00
|
|
|
}
|
2005-06-29 03:46:16 +04:00
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_OK;
|
2005-06-29 03:46:16 +04:00
|
|
|
}
|
|
|
|
|
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 */
|
2008-03-09 19:34:57 +03:00
|
|
|
if (strncasecmp(last_cd.from, from, sizeof(last_cd.from)) == 0 &&
|
|
|
|
strncasecmp(last_cd.to, to, sizeof(last_cd.to)) == 0) {
|
2005-07-11 22:10:10 +04:00
|
|
|
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)
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_BAD_ENCODING;
|
2005-07-11 22:10:10 +04:00
|
|
|
/* default to no memory */
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_NOMEM;
|
2005-07-11 22:10:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2014-05-18 13:44:51 +04:00
|
|
|
snprintf(last_cd.from, sizeof(last_cd.from), "%s", from);
|
|
|
|
snprintf(last_cd.to, sizeof(last_cd.to), "%s", to);
|
2005-07-11 22:10:10 +04:00
|
|
|
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);
|
2012-12-31 03:33:27 +04:00
|
|
|
/* Worst case = ASCII -> UCS4, so allocate an output buffer
|
2005-04-16 09:09:33 +04:00
|
|
|
* 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);
|
2014-01-29 01:40:13 +04:00
|
|
|
if (!out) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2005-04-16 09:09:33 +04:00
|
|
|
|
|
|
|
/* perform conversion */
|
2011-03-10 00:30:54 +03:00
|
|
|
if (iconv(cd, (void *) &in, &slen, &out, &rlen) == (size_t)-1) {
|
2005-04-16 09:09:33 +04:00
|
|
|
free(temp);
|
2006-02-11 21:11:09 +03:00
|
|
|
/* clear the cached conversion descriptor as it's invalid */
|
2009-02-18 17:10:35 +03:00
|
|
|
if (last_cd.cd)
|
|
|
|
iconv_close(last_cd.cd);
|
2013-05-16 02:30:01 +04:00
|
|
|
utf8_clear_cd_cache();
|
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 */
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_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 */
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_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
|
|
|
|
2013-10-25 23:58:19 +04:00
|
|
|
if (result_len != NULL) {
|
|
|
|
*result_len = (out - temp);
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_OK;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
2012-12-31 03:33:27 +04:00
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
|
|
|
nserror utf8_to_enc(const char *string, const char *encname,
|
2013-10-25 23:58:19 +04:00
|
|
|
size_t len, char **result)
|
|
|
|
{
|
|
|
|
return utf8_convert(string, len, "UTF-8", encname, result, NULL);
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
|
|
|
nserror utf8_from_enc(const char *string, const char *encname,
|
2013-10-25 23:58:19 +04:00
|
|
|
size_t len, char **result, size_t *result_len)
|
|
|
|
{
|
|
|
|
return utf8_convert(string, len, encname, "UTF-8", result, result_len);
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/**
|
|
|
|
* convert a chunk of html data
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
utf8_convert_html_chunk(iconv_t cd,
|
|
|
|
const char *chunk,
|
|
|
|
size_t inlen,
|
|
|
|
char **out,
|
|
|
|
size_t *outlen)
|
2012-12-31 03:33:27 +04:00
|
|
|
{
|
|
|
|
size_t ret, esclen;
|
|
|
|
uint32_t ucs4;
|
|
|
|
char *pescape, escape[11];
|
|
|
|
|
|
|
|
while (inlen > 0) {
|
|
|
|
ret = iconv(cd, (void *) &chunk, &inlen, (void *) out, outlen);
|
|
|
|
if (ret != (size_t) -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (errno != EILSEQ)
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_NOMEM;
|
2012-12-31 03:33:27 +04:00
|
|
|
|
|
|
|
ucs4 = utf8_to_ucs4(chunk, inlen);
|
|
|
|
esclen = snprintf(escape, sizeof(escape), "&#x%06x;", ucs4);
|
|
|
|
pescape = escape;
|
|
|
|
ret = iconv(cd, (void *) &pescape, &esclen,
|
|
|
|
(void *) out, outlen);
|
|
|
|
if (ret == (size_t) -1)
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_NOMEM;
|
2012-12-31 03:33:27 +04:00
|
|
|
|
|
|
|
esclen = utf8_next(chunk, inlen, 0);
|
|
|
|
chunk += esclen;
|
|
|
|
inlen -= esclen;
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_OK;
|
2012-12-31 03:33:27 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
|
|
|
nserror
|
|
|
|
utf8_to_html(const char *string, const char *encname, size_t len, char **result)
|
2012-12-31 03:33:27 +04:00
|
|
|
{
|
|
|
|
iconv_t cd;
|
|
|
|
const char *in;
|
|
|
|
char *out, *origout;
|
|
|
|
size_t off, prev_off, inlen, outlen, origoutlen, esclen;
|
2014-01-29 01:40:13 +04:00
|
|
|
nserror ret;
|
2012-12-31 03:33:27 +04:00
|
|
|
char *pescape, escape[11];
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
len = strlen(string);
|
|
|
|
|
2013-05-16 01:13:30 +04:00
|
|
|
/* we cache the last used conversion descriptor,
|
|
|
|
* so check if we're trying to use it here */
|
|
|
|
if (strncasecmp(last_cd.from, "UTF-8", sizeof(last_cd.from)) == 0 &&
|
|
|
|
strncasecmp(last_cd.to, encname,
|
2013-05-16 02:30:01 +04:00
|
|
|
sizeof(last_cd.to)) == 0 &&
|
|
|
|
last_cd.cd != 0) {
|
2013-05-16 01:13:30 +04:00
|
|
|
cd = last_cd.cd;
|
2014-01-29 01:40:13 +04:00
|
|
|
} else {
|
2013-05-16 01:13:30 +04:00
|
|
|
/* no match, so create a new cd */
|
|
|
|
cd = iconv_open(encname, "UTF-8");
|
2013-05-16 02:30:01 +04:00
|
|
|
if (cd == (iconv_t) -1) {
|
2013-05-16 01:13:30 +04:00
|
|
|
if (errno == EINVAL)
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_BAD_ENCODING;
|
2013-05-16 01:13:30 +04:00
|
|
|
/* default to no memory */
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_NOMEM;
|
2013-05-16 01:13:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* close the last cd - we don't care if this fails */
|
|
|
|
if (last_cd.cd)
|
|
|
|
iconv_close(last_cd.cd);
|
|
|
|
|
2014-05-12 02:29:55 +04:00
|
|
|
/* and safely copy the to/from/cd data into last_cd */
|
|
|
|
snprintf(last_cd.from, sizeof(last_cd.from), "UTF-8");
|
|
|
|
snprintf(last_cd.to, sizeof(last_cd.to), "%s", encname);
|
2013-05-16 01:13:30 +04:00
|
|
|
last_cd.cd = cd;
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* Worst case is ASCII -> UCS4, with all characters escaped:
|
|
|
|
* "&#xYYYYYY;", thus each input character may become a string
|
2013-05-16 02:30:01 +04:00
|
|
|
* of 10 UCS4 characters, each 4 bytes in length, plus four for
|
|
|
|
* terminating the string */
|
|
|
|
origoutlen = outlen = len * 10 * 4 + 4;
|
2012-12-31 03:33:27 +04:00
|
|
|
origout = out = malloc(outlen);
|
|
|
|
if (out == NULL) {
|
|
|
|
iconv_close(cd);
|
2013-05-16 02:30:01 +04:00
|
|
|
utf8_clear_cd_cache();
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_NOMEM;
|
2012-12-31 03:33:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process input in chunks between characters we must escape */
|
|
|
|
prev_off = off = 0;
|
|
|
|
while (off < len) {
|
|
|
|
/* Must escape '&', '<', and '>' */
|
|
|
|
if (string[off] == '&' || string[off] == '<' ||
|
|
|
|
string[off] == '>') {
|
|
|
|
if (off - prev_off > 0) {
|
|
|
|
/* Emit chunk */
|
|
|
|
in = string + prev_off;
|
|
|
|
inlen = off - prev_off;
|
|
|
|
ret = utf8_convert_html_chunk(cd, in, inlen,
|
|
|
|
&out, &outlen);
|
2014-01-29 01:40:13 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
2012-12-31 03:33:27 +04:00
|
|
|
free(origout);
|
|
|
|
iconv_close(cd);
|
2013-05-16 02:30:01 +04:00
|
|
|
utf8_clear_cd_cache();
|
2012-12-31 03:33:27 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emit mandatory escape */
|
|
|
|
esclen = snprintf(escape, sizeof(escape),
|
|
|
|
"&#x%06x;", string[off]);
|
|
|
|
pescape = escape;
|
|
|
|
ret = utf8_convert_html_chunk(cd, pescape, esclen,
|
|
|
|
&out, &outlen);
|
2014-01-29 01:40:13 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
2012-12-31 03:33:27 +04:00
|
|
|
free(origout);
|
|
|
|
iconv_close(cd);
|
2013-05-16 02:30:01 +04:00
|
|
|
utf8_clear_cd_cache();
|
2012-12-31 03:33:27 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_off = off = utf8_next(string, len, off);
|
|
|
|
} else {
|
|
|
|
off = utf8_next(string, len, off);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process final chunk */
|
|
|
|
if (prev_off < len) {
|
|
|
|
in = string + prev_off;
|
|
|
|
inlen = len - prev_off;
|
|
|
|
ret = utf8_convert_html_chunk(cd, in, inlen, &out, &outlen);
|
2014-01-29 01:40:13 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
2012-12-31 03:33:27 +04:00
|
|
|
free(origout);
|
|
|
|
iconv_close(cd);
|
2013-05-16 02:30:01 +04:00
|
|
|
utf8_clear_cd_cache();
|
2012-12-31 03:33:27 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 02:30:01 +04:00
|
|
|
/* Terminate string */
|
|
|
|
memset(out, 0, 4);
|
|
|
|
outlen -= 4;
|
2012-12-31 03:33:27 +04:00
|
|
|
|
|
|
|
/* Shrink-wrap */
|
2013-05-16 02:30:01 +04:00
|
|
|
*result = realloc(origout, origoutlen - outlen);
|
2012-12-31 03:33:27 +04:00
|
|
|
if (*result == NULL) {
|
|
|
|
free(origout);
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_NOMEM;
|
2012-12-31 03:33:27 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
return NSERROR_OK;
|
2012-12-31 03:33:27 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
/* exported interface documented in utils/utf8.h */
|
2013-02-22 16:19:35 +04:00
|
|
|
bool utf8_save_text(const char *utf8_text, const char *path)
|
|
|
|
{
|
2014-01-29 01:40:13 +04:00
|
|
|
nserror ret;
|
2013-02-22 16:19:35 +04:00
|
|
|
char *conv;
|
|
|
|
FILE *out;
|
|
|
|
|
2014-02-02 02:17:36 +04:00
|
|
|
ret = guit->utf8->utf8_to_local(utf8_text, strlen(utf8_text), &conv);
|
2014-01-29 01:40:13 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"failed to convert to local encoding, return %d", ret);
|
2013-02-22 16:19:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = fopen(path, "w");
|
|
|
|
if (out) {
|
|
|
|
int res = fputs(conv, out);
|
|
|
|
if (res < 0) {
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO, "Warning: writing data failed");
|
2013-02-22 16:19:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
res = fputs("\n", out);
|
|
|
|
fclose(out);
|
|
|
|
free(conv);
|
|
|
|
return (res != EOF);
|
|
|
|
}
|
|
|
|
free(conv);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|