Debian Codegraph @

src/iri.c

raw
1 /* IRI related functions.
2 Copyright (C) 2008-2011, 2015, 2018-2024 Free Software Foundation,
3 Inc.
4
5 This file is part of GNU Wget.
6
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.
11
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.
16
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/>.
19
20 Additional permission under GNU GPL version 3 section 7
21
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. */
30
31 #include "wget.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <langinfo.h>
37 #include <errno.h>
38 #ifdef HAVE_ICONV
39 # include <iconv.h>
40 #endif
41 #include <idn2.h>
42 #if IDN2_VERSION_NUMBER < 0x00140000
43 # include <unicase.h>
44 # include <unistr.h>
45 #endif
46
47 #include "utils.h"
48 #include "url.h"
49 #include "c-strcase.h"
50 #include "c-strcasestr.h"
51 #include "xstrndup.h"
52
53 /* Note: locale encoding is kept in options struct (opt.locale) */
54
55 /* Given a string containing "charset=XXX", return the encoding if found,
56 or NULL otherwise */
57 char *
58 parse_charset (const char *str)
59 {
60 const char *end;
61 char *charset;
62
63 if (!str || !*str)
64 return NULL;
65
66 str = c_strcasestr (str, "charset=");
67 if (!str)
68 return NULL;
69
70 str += 8;
71 end = str;
72
73 /* sXXXav: which chars should be banned ??? */
74 while (*end && !c_isspace (*end))
75 end++;
76
77 /* sXXXav: could strdupdelim return NULL ? */
78 charset = strdupdelim (str, end);
79
80 /* Do a minimum check on the charset value */
81 if (!check_encoding_name (charset))
82 {
83 xfree (charset);
84 return NULL;
85 }
86
87 /*logprintf (LOG_VERBOSE, "parse_charset: %s\n", quote (charset));*/
88
89 return charset;
90 }
91
92 /* Find the locale used, or fall back on a default value */
93 const char *
94 find_locale (void)
95 {
96 const char *encoding = nl_langinfo(CODESET);
97
98 if (!encoding || !*encoding)
99 return xstrdup("ASCII");
100
101 return xstrdup(encoding);
102 }
103
104 /* Basic check of an encoding name. */
105 bool
106 check_encoding_name (const char *encoding)
107 {
108 const char *s = encoding;
109
110 while (*s)
111 {
112 if (!c_isascii (*s) || c_isspace (*s))
113 {
114 logprintf (LOG_VERBOSE, _("Encoding %s isn't valid\n"), quote (encoding));
115 return false;
116 }
117
118 s++;
119 }
120
121 return true;
122 }
123
124 #ifdef HAVE_ICONV
125 /* Do the conversion according to the passed conversion descriptor cd. *out
126 will contain the transcoded string on success. *out content is
127 unspecified otherwise. */
128 static bool
129 do_conversion (const char *tocode, const char *fromcode, char const *in_org, size_t inlen, char **out)
130 {
131 iconv_t cd;
132 /* sXXXav : hummm hard to guess... */
133 size_t len, done, outlen;
134 int invalid = 0, tooshort = 0;
135 char *s, *in, *in_save;
136
137 cd = iconv_open (tocode, fromcode);
138 if (cd == (iconv_t)(-1))
139 {
140 logprintf (LOG_VERBOSE, _("Conversion from %s to %s isn't supported\n"),
141 quote_n (0, fromcode), quote_n (1, tocode));
142 *out = NULL;
143 return false;
144 }
145
146 /* iconv() has to work on an unescaped string */
147 in_save = in = xstrndup (in_org, inlen);
148 url_unescape_except_reserved (in);
149 inlen = strlen(in);
150
151 len = outlen = inlen * 2;
152 *out = s = xmalloc (outlen + 1);
153 done = 0;
154
155 for (;;)
156 {
157 if (iconv (cd, (ICONV_CONST char **) &in, &inlen, out, &outlen) != (size_t)(-1) &&
158 iconv (cd, NULL, NULL, out, &outlen) != (size_t)(-1))
159 {
160 *out = s;
161 *(s + len - outlen - done) = '\0';
162 xfree(in_save);
163 iconv_close(cd);
164 IF_DEBUG
165 {
166 /* not not print out embedded passwords, in_org might be an URL */
167 if (!strchr(in_org, '@') && !strchr(*out, '@'))
168 debug_logprintf ("converted '%s' (%s) -> '%s' (%s)\n", in_org, fromcode, *out, tocode);
169 else
170 debug_logprintf ("logging suppressed, strings may contain password\n");
171 }
172 return true;
173 }
174
175 /* Incomplete or invalid multibyte sequence */
176 if (errno == EINVAL || errno == EILSEQ)
177 {
178 if (!invalid)
179 logprintf (LOG_VERBOSE,
180 _("Incomplete or invalid multibyte sequence encountered\n"));
181
182 invalid++;
183 **out = *in;
184 in++;
185 inlen--;
186 (*out)++;
187 outlen--;
188 }
189 else if (errno == E2BIG) /* Output buffer full */
190 {
191 tooshort++;
192 done = len;
193 len = done + inlen * 2;
194 char *s_new = xrealloc (s, len + 1);
195 if (s_new != s)
196 {
197 /* Help valgrind by initializing additional memory. */
198 s = s_new;
199 memset(s + done, 0, inlen * 2 + 1);
200 }
201 *out = s + done - outlen;
202 outlen += inlen * 2;
203 }
204 else /* Weird, we got an unspecified error */
205 {
206 logprintf (LOG_VERBOSE, _("Unhandled errno %d\n"), errno);
207 break;
208 }
209 }
210
211 xfree(in_save);
212 iconv_close(cd);
213 IF_DEBUG
214 {
215 /* not not print out embedded passwords, in_org might be an URL */
216 if (!strchr(in_org, '@') && !strchr(*out, '@'))
217 debug_logprintf ("converted '%s' (%s) -> '%s' (%s)\n", in_org, fromcode, *out, tocode);
218 else
219 debug_logprintf ("logging suppressed, strings may contain password\n");
220 }
221 return false;
222 }
223 #else
224 static bool
225 do_conversion (const char *tocode _GL_UNUSED, const char *fromcode _GL_UNUSED,
226 char const *in_org _GL_UNUSED, size_t inlen _GL_UNUSED, char **out)
227 {
228 *out = NULL;
229 return false;
230 }
231 #endif
232
233 /* Try converting string str from locale to UTF-8. Return a new string
234 on success, or str on error or if conversion isn't needed. */
235 const char *
236 locale_to_utf8 (const char *str)
237 {
238 char *new;
239
240 /* That shouldn't happen, just in case */
241 if (!opt.locale)
242 {
243 logprintf (LOG_VERBOSE, _("locale_to_utf8: locale is unset\n"));
244 opt.locale = find_locale ();
245 }
246
247 if (!opt.locale || !c_strcasecmp (opt.locale, "utf-8"))
248 return str;
249
250 if (do_conversion ("UTF-8", opt.locale, (char *) str, strlen ((char *) str), &new))
251 return (const char *) new;
252
253 xfree (new);
254 return str;
255 }
256
257 /* Try to "ASCII encode" UTF-8 host. Return the new domain on success or NULL
258 on error. */
259 char *
260 idn_encode (const struct iri *i, const char *host)
261 {
262 int ret;
263 char *ascii_encoded;
264 char *utf8_encoded = NULL;
265 const char *src;
266 #if IDN2_VERSION_NUMBER < 0x00140000
267 uint8_t *lower;
268 size_t len = 0;
269 #endif
270
271 /* Encode to UTF-8 if not done */
272 if (!i->utf8_encode)
273 {
274 if (!remote_to_utf8 (i, host, &utf8_encoded))
275 return NULL; /* Nothing to encode or an error occurred */
276 src = utf8_encoded;
277 }
278 else
279 src = host;
280
281 #if IDN2_VERSION_NUMBER >= 0x00140000
282 /* IDN2_TRANSITIONAL implies input NFC encoding */
283 ret = idn2_lookup_u8 ((uint8_t *) src, (uint8_t **) &ascii_encoded, IDN2_NONTRANSITIONAL);
284 if (ret != IDN2_OK)
285 /* fall back to TR46 Transitional mode, max IDNA2003 compatibility */
286 ret = idn2_lookup_u8 ((uint8_t *) src, (uint8_t **) &ascii_encoded, IDN2_TRANSITIONAL);
287
288 if (ret != IDN2_OK)
289 logprintf (LOG_VERBOSE, _("idn_encode failed (%d): %s\n"), ret,
290 quote (idn2_strerror (ret)));
291 #else
292 /* we need a conversion to lowercase */
293 lower = u8_tolower ((uint8_t *) src, u8_strlen ((uint8_t *) src) + 1, 0, UNINORM_NFKC, NULL, &len);
294 if (!lower)
295 {
296 logprintf (LOG_VERBOSE, _("Failed to convert to lower: %d: %s\n"),
297 errno, quote (src));
298 xfree (utf8_encoded);
299 return NULL;
300 }
301
302 if ((ret = idn2_lookup_u8 (lower, (uint8_t **) &ascii_encoded, IDN2_NFC_INPUT)) != IDN2_OK)
303 {
304 logprintf (LOG_VERBOSE, _("idn_encode failed (%d): %s\n"), ret,
305 quote (idn2_strerror (ret)));
306 }
307
308 xfree (lower);
309 #endif
310
311 xfree (utf8_encoded);
312
313 if (ret == IDN2_OK && ascii_encoded)
314 {
315 char *tmp = xstrdup (ascii_encoded);
316 idn2_free (ascii_encoded);
317 ascii_encoded = tmp;
318 }
319
320 return ret == IDN2_OK ? ascii_encoded : NULL;
321 }
322
323 /* Try to decode an "ASCII encoded" host. Return the new domain in the locale
324 on success or NULL on error. */
325 char *
326 idn_decode (const char *host)
327 {
328 /*
329 char *new;
330 int ret;
331
332 ret = idn2_register_u8 (NULL, host, (uint8_t **) &new, 0);
333 if (ret != IDN2_OK)
334 {
335 logprintf (LOG_VERBOSE, _("idn2_register_u8 failed (%d): %s: %s\n"), ret,
336 quote (idn2_strerror (ret)), host);
337 return NULL;
338 }
339
340 return new;
341 */
342 /* idn2_register_u8() just works label by label.
343 * That is pretty much overhead for just displaying the original ulabels.
344 * To keep at least the debug output format, return a cloned host. */
345 return xstrdup(host);
346 }
347
348 /* Try to transcode string str from remote encoding to UTF-8. On success, *new
349 contains the transcoded string. *new content is unspecified otherwise. */
350 bool
351 remote_to_utf8 (const struct iri *iri, const char *str, char **new)
352 {
353 bool ret = false;
354
355 if (!iri->uri_encoding)
356 return false;
357
358 /* When `i->uri_encoding' == "UTF-8" there is nothing to convert. But we must
359 test for non-ASCII symbols for correct hostname processing in `idn_encode'
360 function. */
361 if (!c_strcasecmp (iri->uri_encoding, "UTF-8"))
362 {
363 const unsigned char *p;
364 for (p = (unsigned char *) str; *p; p++)
365 if (*p > 127)
366 {
367 *new = strdup (str);
368 return true;
369 }
370 return false;
371 }
372
373 if (do_conversion ("UTF-8", iri->uri_encoding, str, strlen (str), new))
374 ret = true;
375
376 /* Test if something was converted */
377 if (*new && !strcmp (str, *new))
378 {
379 xfree (*new);
380 return false;
381 }
382
383 return ret;
384 }
385
386 /* Allocate a new iri structure and return a pointer to it. */
387 struct iri *
388 iri_new (void)
389 {
390 struct iri *i = xmalloc (sizeof *i);
391 i->uri_encoding = opt.encoding_remote ? xstrdup (opt.encoding_remote) : NULL;
392 i->content_encoding = NULL;
393 i->orig_url = NULL;
394 i->utf8_encode = opt.enable_iri;
395 return i;
396 }
397
398 struct iri *iri_dup (const struct iri *src)
399 {
400 struct iri *i = xmalloc (sizeof *i);
401 i->uri_encoding = src->uri_encoding ? xstrdup (src->uri_encoding) : NULL;
402 i->content_encoding = (src->content_encoding ?
403 xstrdup (src->content_encoding) : NULL);
404 i->orig_url = src->orig_url ? xstrdup (src->orig_url) : NULL;
405 i->utf8_encode = src->utf8_encode;
406 return i;
407 }
408
409 /* Completely free an iri structure. */
410 void
411 iri_free (struct iri *i)
412 {
413 if (i)
414 {
415 xfree (i->uri_encoding);
416 xfree (i->content_encoding);
417 xfree (i->orig_url);
418 xfree (i);
419 }
420 }
421
422 /* Set uri_encoding of struct iri i. If a remote encoding was specified, use
423 it unless force is true. */
424 void
425 set_uri_encoding (struct iri *i, const char *charset, bool force)
426 {
427 DEBUGP (("URI encoding = %s\n", charset ? quote (charset) : "None"));
428 if (!force && opt.encoding_remote)
429 return;
430 if (i->uri_encoding)
431 {
432 if (charset && !c_strcasecmp (i->uri_encoding, charset))
433 return;
434 xfree (i->uri_encoding);
435 }
436
437 i->uri_encoding = charset ? xstrdup (charset) : NULL;
438 }
439
440 /* Set content_encoding of struct iri i. */
441 void
442 set_content_encoding (struct iri *i, const char *charset)
443 {
444 DEBUGP (("URI content encoding = %s\n", charset ? quote (charset) : "None"));
445 if (opt.encoding_remote)
446 return;
447 if (i->content_encoding)
448 {
449 if (charset && !c_strcasecmp (i->content_encoding, charset))
450 return;
451 xfree (i->content_encoding);
452 }
453
454 i->content_encoding = charset ? xstrdup (charset) : NULL;
455 }