1 /* IRI related functions.
2 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or (at
9 your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget. If not, see <http://www.gnu.org/licenses/>.
19 Additional permission under GNU GPL version 3 section 7
21 If you modify this program, or any covered work, by linking or
22 combining it with the OpenSSL project's OpenSSL library (or a
23 modified version of that library), containing parts covered by the
24 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
25 grants you additional permission to convey the resulting work.
26 Corresponding Source for a non-source form of such a combination
27 shall include the source code for the parts of OpenSSL used as well
28 as that of the covered work. */
36 #include <stringprep.h>
42 /* RFC3987 section 3.1 mandates STD3 ASCII RULES */
43 #define IDNA_FLAGS IDNA_USE_STD3_ASCII_RULES
45 /* Note: locale encoding is kept in options struct (opt.locale) */
47 static bool do_conversion (iconv_t cd, char *in, size_t inlen, char **out);
50 /* Given a string containing "charset=XXX", return the encoding if found,
53 parse_charset (char *str)
60 str = strcasestr (str, "charset=");
67 /* sXXXav: which chars should be banned ??? */
68 while (*charset && !c_isspace (*charset))
71 /* sXXXav: could strdupdelim return NULL ? */
72 charset = strdupdelim (str, charset);
74 /* Do a minimum check on the charset value */
75 if (!check_encoding_name (charset))
81 /*logprintf (LOG_VERBOSE, "parse_charset: %s\n", quote (charset));*/
86 /* Find the locale used, or fall back on a default value */
90 return (char *) stringprep_locale_charset ();
93 /* Basic check of an encoding name. */
95 check_encoding_name (char *encoding)
101 if (!c_isascii (*s) || c_isspace (*s))
103 logprintf (LOG_VERBOSE, _("Encoding %s isn't valid\n"), quote (encoding));
113 /* Try converting string str from locale to UTF-8. Return a new string
114 on success, or str on error or if conversion isn't needed. */
116 locale_to_utf8 (const char *str)
121 /* That shouldn't happen, just in case */
124 logprintf (LOG_VERBOSE, _("locale_to_utf8: locale is unset\n"));
125 opt.locale = find_locale ();
128 if (!opt.locale || !strcasecmp (opt.locale, "utf-8"))
131 l2u = iconv_open ("UTF-8", opt.locale);
132 if (l2u != (iconv_t)(-1))
134 logprintf (LOG_VERBOSE, _("Conversion from %s to %s isn't supported\n"),
135 quote (opt.locale), quote ("UTF-8"));
139 if (do_conversion (l2u, (char *) str, strlen ((char *) str), &new))
140 return (const char *) new;
145 /* Do the conversion according to the passed conversion descriptor cd. *out
146 will contain the transcoded string on success. *out content is
147 unspecified otherwise. */
149 do_conversion (iconv_t cd, char *in, size_t inlen, char **out)
151 /* sXXXav : hummm hard to guess... */
152 size_t len, done, outlen = inlen * 2;
153 int invalid = 0, tooshort = 0;
156 s = xmalloc (outlen + 1);
163 if (iconv (cd, &in, &inlen, out, &outlen) != (size_t)(-1))
166 *(s + len - outlen - done) = '\0';
170 /* Incomplete or invalid multibyte sequence */
171 if (errno == EINVAL || errno == EILSEQ)
174 logprintf (LOG_VERBOSE,
175 _("Incomplete or invalid multibyte sequence encountered\n"));
184 else if (errno == E2BIG) /* Output buffer full */
190 outlen = done + inlen * 2;
191 new = xmalloc (outlen + 1);
192 memcpy (new, s, done);
198 else /* Weird, we got an unspecified error */
200 logprintf (LOG_VERBOSE, _("Unhandled errno %d\n"), errno);
208 /* Try to "ASCII encode" UTF-8 host. Return the new domain on success or NULL
211 idn_encode (struct iri *i, char *host)
216 /* Encode to UTF-8 if not done */
219 if (!remote_to_utf8 (i, (const char *) host, (const char **) &new))
220 return NULL; /* Nothing to encode or an error occured */
224 /* toASCII UTF-8 NULL terminated string */
225 ret = idna_to_ascii_8z (host, &new, IDNA_FLAGS);
226 if (ret != IDNA_SUCCESS)
228 /* sXXXav : free new when needed ! */
229 logprintf (LOG_VERBOSE, _("idn_encode failed (%d): %s\n"), ret,
230 quote (idna_strerror (ret)));
237 /* Try to decode an "ASCII encoded" host. Return the new domain in the locale
238 on success or NULL on error. */
240 idn_decode (char *host)
245 ret = idna_to_unicode_8zlz (host, &new, IDNA_FLAGS);
246 if (ret != IDNA_SUCCESS)
248 logprintf (LOG_VERBOSE, _("idn_decode failed (%d): %s\n"), ret,
249 quote (idna_strerror (ret)));
256 /* Try to transcode string str from remote encoding to UTF-8. On success, *new
257 contains the transcoded string. *new content is unspecified otherwise. */
259 remote_to_utf8 (struct iri *i, const char *str, const char **new)
264 if (!i->uri_encoding)
267 /* When `i->uri_encoding' == "UTF-8" there is nothing to convert. But we must
268 test for non-ASCII symbols for correct hostname processing in `idn_encode'
270 if (!strcmp (i->uri_encoding, "UTF-8"))
272 int i, len = strlen (str);
273 for (i = 0; i < len; i++)
274 if ((unsigned char) str[i] >= (unsigned char) '\200')
282 cd = iconv_open ("UTF-8", i->uri_encoding);
283 if (cd == (iconv_t)(-1))
286 if (do_conversion (cd, (char *) str, strlen ((char *) str), (char **) new))
291 /* Test if something was converted */
292 if (!strcmp (str, *new))
294 xfree ((char *) *new);
301 /* Allocate a new iri structure and return a pointer to it. */
305 struct iri *i = xmalloc (sizeof *i);
306 i->uri_encoding = opt.encoding_remote ? xstrdup (opt.encoding_remote) : NULL;
307 i->content_encoding = NULL;
309 i->utf8_encode = opt.enable_iri;
313 struct iri *iri_dup (const struct iri *src)
315 struct iri *i = xmalloc (sizeof *i);
316 i->uri_encoding = src->uri_encoding ? xstrdup (src->uri_encoding) : NULL;
317 i->content_encoding = (src->content_encoding ?
318 xstrdup (src->content_encoding) : NULL);
319 i->orig_url = src->orig_url ? xstrdup (src->orig_url) : NULL;
320 i->utf8_encode = src->utf8_encode;
324 /* Completely free an iri structure. */
326 iri_free (struct iri *i)
328 xfree_null (i->uri_encoding);
329 xfree_null (i->content_encoding);
330 xfree_null (i->orig_url);
334 /* Set uri_encoding of struct iri i. If a remote encoding was specified, use
335 it unless force is true. */
337 set_uri_encoding (struct iri *i, char *charset, bool force)
339 DEBUGP (("URI encoding = %s\n", charset ? quote (charset) : "None"));
340 if (!force && opt.encoding_remote)
344 if (charset && !strcasecmp (i->uri_encoding, charset))
346 xfree (i->uri_encoding);
349 i->uri_encoding = charset ? xstrdup (charset) : NULL;
352 /* Set content_encoding of struct iri i. */
354 set_content_encoding (struct iri *i, char *charset)
356 DEBUGP (("URI content encoding = %s\n", charset ? quote (charset) : "None"));
357 if (opt.encoding_remote)
359 if (i->content_encoding)
361 if (charset && !strcasecmp (i->content_encoding, charset))
363 xfree (i->content_encoding);
366 i->content_encoding = charset ? xstrdup (charset) : NULL;