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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 * Convert data from one encoding to another. Return:
36 * -2 : memory allocation failed
37 * -1 : unknown encoding
38 * 0 : data was converted exactly
39 * 1 : data was converted inexactly
40 * 2 : data was invalid (but still converted)
42 * We convert in two steps, via UTF-8, as this is the only
43 * reliable way of distinguishing between invalid input
44 * and valid input which iconv refuses to transliterate.
45 * We convert from UTF-8 twice, because we have no way of
46 * knowing whether the conversion was exact if iconv returns
47 * E2BIG (due to a bug in the specification of iconv).
48 * An alternative approach is to assume that the output of
49 * iconv is never more than 4 times as long as the input,
50 * but I prefer to avoid that assumption if possible.
53 int iconvert(const char *fromcode, const char *tocode,
54 const char *from, size_t fromlen,
55 char **to, size_t *tolen)
61 char *utfbuf = 0, *outbuf, *newbuf;
62 size_t utflen, outlen, ibl, obl, k;
65 cd1 = iconv_open("UTF-8", fromcode);
66 if (cd1 == (iconv_t)(-1))
70 /* Don't use strcasecmp() as it's locale-dependent. */
71 if (!strchr("Uu", tocode[0]) ||
72 !strchr("Tt", tocode[1]) ||
73 !strchr("Ff", tocode[2]) ||
80 * Try using this non-standard feature of glibc and libiconv.
81 * This is deliberately not a config option as people often
82 * change their iconv library without rebuilding applications.
84 tocode1 = (char *)malloc(strlen(tocode) + 11);
88 strcpy(tocode1, tocode);
89 strcat(tocode1, "//TRANSLIT");
90 cd2 = iconv_open(tocode1, "UTF-8");
93 if (cd2 == (iconv_t)(-1))
94 cd2 = iconv_open(tocode, fromcode);
96 if (cd2 == (iconv_t)(-1)) {
102 utflen = 1; /*fromlen * 2 + 1; XXX */
103 utfbuf = (char *)malloc(utflen);
107 /* Convert to UTF-8 */
113 k = iconv(cd1, &ib, &ibl, &ob, &obl);
114 assert((!k && !ibl) ||
115 (k == (size_t)(-1) && errno == E2BIG && ibl && obl < 6) ||
116 (k == (size_t)(-1) &&
117 (errno == EILSEQ || errno == EINVAL) && ibl));
121 /* Enlarge the buffer */
123 newbuf = (char *)realloc(utfbuf, utflen);
126 ob = (ob - utfbuf) + newbuf;
127 obl = utflen - (ob - newbuf);
135 iconv(cd1, 0, 0, 0, 0);
139 if (cd2 == (iconv_t)(-1)) {
140 /* The target encoding was UTF-8 */
142 *tolen = ob - utfbuf;
148 newbuf = (char *)realloc(utfbuf, (ob - utfbuf) + 1);
151 ob = (ob - utfbuf) + newbuf;
158 /* Truncate the buffer to be tidy */
159 utflen = ob - utfbuf;
160 newbuf = (char *)realloc(utfbuf, utflen);
165 /* Convert from UTF-8 to discover how long the output is */
172 k = iconv(cd2, &ib, &ibl, &ob, &obl);
173 assert((k != (size_t)(-1) && !ibl) ||
174 (k == (size_t)(-1) && errno == E2BIG && ibl) ||
175 (k == (size_t)(-1) && errno == EILSEQ && ibl));
176 if (ibl && !(k == (size_t)(-1) && errno == E2BIG)) {
177 /* Replace one character */
184 k = iconv(cd2, &tb, &tbl, &ob, &obl);
185 assert((!k && !tbl) ||
186 (k == (size_t)(-1) && errno == EILSEQ && tbl));
187 for (++ib, --ibl; ibl && (*ib & 0x80); ib++, ibl--)
194 k = iconv(cd2, 0, 0, &ob, &obl);
198 /* Convert from UTF-8 for real */
199 outbuf = (char *)malloc(outlen + 1);
207 k = iconv(cd2, &ib, &ibl, &ob, &obl);
208 assert((k != (size_t)(-1) && !ibl) ||
209 (k == (size_t)(-1) && errno == EILSEQ && ibl));
212 if (ibl && !(k == (size_t)(-1) && errno == E2BIG)) {
213 /* Replace one character */
217 k = iconv(cd2, &tb, &tbl, &ob, &obl);
218 assert((!k && !tbl) ||
219 (k == (size_t)(-1) && errno == EILSEQ && tbl));
220 for (++ib, --ibl; ibl && (*ib & 0x80); ib++, ibl--)
224 k = iconv(cd2, 0, 0, &ob, &obl);
245 if (cd2 != (iconv_t)(-1))
250 #endif /* HAVE_ICONV */