1 /* IRI related functions.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at
10 your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20 Additional permission under GNU GPL version 3 section 7
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
38 #include <stringprep.h>
46 static iconv_t locale2utf8;
49 static bool open_locale_to_utf8 (void);
50 static bool do_conversion (iconv_t cd, char *in, size_t inlen, char **out);
53 /* Given a string containing "charset=XXX", return the encoding if found,
56 parse_charset (char *str)
63 str = strcasestr (str, "charset=");
70 /* sXXXav: which chars should be banned ??? */
71 while (*charset && !c_isspace (*charset))
74 /* sXXXav: could strdupdelim return NULL ? */
75 charset = strdupdelim (str, charset);
77 /* Do a minimum check on the charset value */
78 if (!check_encoding_name (charset))
84 logprintf (LOG_VERBOSE, "parse_charset: %s\n", quote (charset));
89 /* Find the locale used, or fall back on a default value */
93 /* sXXXav, made our own function or use libidn one ?! */
94 return (char *) stringprep_locale_charset ();
97 /* Basic check of an encoding name. */
99 check_encoding_name (char *encoding)
105 if (!c_isascii(*s) || c_isspace(*s))
107 logprintf (LOG_VERBOSE, "Encoding %s isn't valid\n", quote(encoding));
117 /* Try opening an iconv_t descriptor for conversion from locale to UTF-8 */
119 open_locale_to_utf8 (void)
124 /* sXXXav : That shouldn't happen, just in case */
127 logprintf (LOG_VERBOSE, "open_locale_to_utf8: locale is unset\n");
128 opt.locale = find_locale ();
134 locale2utf8 = iconv_open ("UTF-8", opt.locale);
135 if (locale2utf8 != (iconv_t)(-1))
138 logprintf (LOG_VERBOSE, "Conversion from %s to %s isn't supported\n",
139 quote (opt.locale), quote("UTF-8"));
144 /* Return a new string */
146 locale_to_utf8 (const char *str)
150 if (!strcasecmp (opt.locale, "utf-8"))
153 if (!open_locale_to_utf8 ())
156 if (do_conversion (locale2utf8, (char *) str, strlen ((char *) str), &new))
157 return (const char *) new;
164 do_conversion (iconv_t cd, char *in, size_t inlen, char **out)
166 /* sXXXav : hummm hard to guess... */
167 size_t len, done, outlen = inlen * 2;
168 int invalid = 0, tooshort = 0;
171 s = xmalloc (outlen + 1);
176 /* sXXXav : put a maximum looping factor ??? */
179 if (iconv (cd, &in, &inlen, out, &outlen) != (size_t)(-1))
182 *(s + len - outlen - done) = '\0';
186 /* Incomplete or invalid multibyte sequence */
187 if (errno == EINVAL || errno == EILSEQ)
196 else if (errno == E2BIG) /* Output buffer full */
202 outlen = done + inlen * 2;
203 new = xmalloc (outlen + 1);
204 memcpy (new, s, done);
210 else /* Weird, we got an unspecified error */
212 logprintf (LOG_VERBOSE, "Unhandled errno %d\n", errno);
220 /* Try to encode UTF-8 host to ASCII. Return the new domain on success or NULL
222 char *idn_encode (char *host)
227 /* toASCII UTF-8 NULL terminated string */
228 ret = idna_to_ascii_8z (host, &new, 0);
229 if (ret != IDNA_SUCCESS)
231 logprintf (LOG_VERBOSE, "idn_encode failed (%d): %s\n", ret,
232 quote (idna_strerror (ret)));