2 * Copyright (C) 2001 Edmund Grimley Evans <edmundo@rano.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #if !defined _WIN32 && defined HAVE_ICONV
32 #include "share/alloc.h"
33 #include "share/safe_str.h"
36 * Convert data from one encoding to another. Return:
38 * -2 : memory allocation failed
39 * -1 : unknown encoding
40 * 0 : data was converted exactly
41 * 1 : data was converted inexactly
42 * 2 : data was invalid (but still converted)
44 * We convert in two steps, via UTF-8, as this is the only
45 * reliable way of distinguishing between invalid input
46 * and valid input which iconv refuses to transliterate.
47 * We convert from UTF-8 twice, because we have no way of
48 * knowing whether the conversion was exact if iconv returns
49 * E2BIG (due to a bug in the specification of iconv).
50 * An alternative approach is to assume that the output of
51 * iconv is never more than 4 times as long as the input,
52 * but I prefer to avoid that assumption if possible.
55 int iconvert(const char *fromcode, const char *tocode,
56 const char *from, size_t fromlen,
57 char **to, size_t *tolen)
63 char *utfbuf = 0, *outbuf, *newbuf;
64 size_t utflen, outlen, ibl, obl, k;
67 cd1 = iconv_open("UTF-8", fromcode);
68 if (cd1 == (iconv_t)(-1))
72 /* Don't use strcasecmp() as it's locale-dependent. */
73 if (!strchr("Uu", tocode[0]) ||
74 !strchr("Tt", tocode[1]) ||
75 !strchr("Ff", tocode[2]) ||
80 size_t dest_len = strlen(tocode) + 11;
82 * Try using this non-standard feature of glibc and libiconv.
83 * This is deliberately not a config option as people often
84 * change their iconv library without rebuilding applications.
86 tocode1 = safe_malloc_(dest_len);
90 safe_strncpy(tocode1, tocode, dest_len);
91 safe_strncat(tocode1, "//TRANSLIT", dest_len);
92 cd2 = iconv_open(tocode1, "UTF-8");
95 if (cd2 == (iconv_t)(-1))
96 cd2 = iconv_open(tocode, fromcode);
98 if (cd2 == (iconv_t)(-1)) {
104 utflen = 1; /*fromlen * 2 + 1; XXX */
105 utfbuf = malloc(utflen);
109 /* Convert to UTF-8 */
115 k = iconv(cd1, &ib, &ibl, &ob, &obl);
116 assert((!k && !ibl) ||
117 (k == (size_t)(-1) && errno == E2BIG && ibl && obl < 6) ||
118 (k == (size_t)(-1) &&
119 (errno == EILSEQ || errno == EINVAL) && ibl));
123 /* Enlarge the buffer */
124 if(utflen*2 < utflen) /* overflow check */
127 newbuf = realloc(utfbuf, utflen);
130 ob = (ob - utfbuf) + newbuf;
131 obl = utflen - (ob - newbuf);
139 iconv(cd1, 0, 0, 0, 0);
143 if (cd2 == (iconv_t)(-1)) {
144 /* The target encoding was UTF-8 */
146 *tolen = ob - utfbuf;
152 newbuf = safe_realloc_add_2op_(utfbuf, (ob - utfbuf), /*+*/1);
155 ob = (ob - utfbuf) + newbuf;
162 /* Truncate the buffer to be tidy */
163 utflen = ob - utfbuf;
164 newbuf = realloc(utfbuf, utflen);
169 /* Convert from UTF-8 to discover how long the output is */
176 k = iconv(cd2, &ib, &ibl, &ob, &obl);
177 assert((k != (size_t)(-1) && !ibl) ||
178 (k == (size_t)(-1) && errno == E2BIG && ibl) ||
179 (k == (size_t)(-1) && errno == EILSEQ && ibl));
180 if (ibl && !(k == (size_t)(-1) && errno == E2BIG)) {
181 /* Replace one character */
188 k = iconv(cd2, &tb, &tbl, &ob, &obl);
189 assert((!k && !tbl) ||
190 (k == (size_t)(-1) && errno == EILSEQ && tbl));
191 for (++ib, --ibl; ibl && (*ib & 0x80); ib++, ibl--)
198 k = iconv(cd2, 0, 0, &ob, &obl);
202 /* Convert from UTF-8 for real */
203 outbuf = safe_malloc_add_2op_(outlen, /*+*/1);
211 k = iconv(cd2, &ib, &ibl, &ob, &obl);
212 assert((k != (size_t)(-1) && !ibl) ||
213 (k == (size_t)(-1) && errno == EILSEQ && ibl));
216 if (ibl && !(k == (size_t)(-1) && errno == E2BIG)) {
217 /* Replace one character */
221 k = iconv(cd2, &tb, &tbl, &ob, &obl);
222 assert((!k && !tbl) ||
223 (k == (size_t)(-1) && errno == EILSEQ && tbl));
224 for (++ib, --ibl; ibl && (*ib & 0x80); ib++, ibl--)
228 k = iconv(cd2, 0, 0, &ob, &obl);
249 if (cd2 != (iconv_t)(-1))
254 #endif /* HAVE_ICONV */