1 /*
2 * Copyright (c) 2017-2021 Free Software Foundation, Inc.
3 *
4 * This file is part of libwget.
5 *
6 * Libwget is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Libwget 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 Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with libwget. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "wget.h"
29 #include "fuzzer.h"
30
test(char * in,size_t len,const char * encoding)31 static void test(char *in, size_t len, const char *encoding)
32 {
33 wget_iri *base;
34 base = wget_iri_parse("http://x.org", encoding);
35 assert(base != NULL);
36
37 wget_iri *iri, *iri2;
38 iri = wget_iri_parse(in, encoding);
39 iri2 = wget_iri_clone(iri);
40 wget_iri_free(&iri2);
41 iri2 = wget_iri_parse_base(NULL, in, encoding);
42 wget_iri_free(&iri2);
43 iri2 = wget_iri_parse_base(base, in, encoding);
44 int x = wget_iri_compare(iri, iri2);
45 wget_iri_free(&iri2);
46
47 wget_buffer buf;
48 wget_buffer_init(&buf, NULL, 32);
49 wget_buffer_printf(&buf, "%d", x); // use x to avoid optimization (removal of call to wget_iri_compare)
50
51 wget_iri_relative_to_abs(base, (const char *) in, len, &buf);
52 wget_iri_escape(in, &buf);
53 wget_iri_escape_path(in, &buf);
54 wget_iri_escape_query(in, &buf);
55 if (iri) {
56 if (wget_iri_supported(iri))
57 wget_iri_set_scheme(iri, WGET_IRI_SCHEME_HTTPS);
58 wget_iri_get_escaped_host(iri, &buf);
59 wget_iri_get_escaped_resource(iri, &buf);
60 wget_iri_get_path(iri, &buf, encoding);
61 wget_iri_get_query_as_filename(iri, &buf, encoding);
62 wget_iri_get_basename(iri, &buf, encoding, WGET_IRI_WITH_QUERY);
63 wget_iri_get_connection_part(iri, &buf);
64 }
65
66 wget_buffer_deinit(&buf);
67 wget_iri_free(&iri);
68 wget_iri_free(&base);
69 }
70
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72 {
73 if (size > 10000) // same as max_len = 10000 in .options file
74 return 0;
75
76 char *in = (char *) malloc(size + 1);
77 assert(in != NULL);
78
79 // 0 terminate
80 memcpy(in, data, size);
81 in[size] = 0;
82
83 // the expression avoids removal of calls to pure functions
84 if (wget_iri_isreserved('='))
85 wget_iri_set_defaultpage("index.html");
86
87 test(in, size, "iso-8859-1");
88 test(in, size, "utf-8");
89
90 free(in);
91
92 return 0;
93 }
94