1 /*
2 * Copyright (c) 2017-2019, 2021 Free Software Foundation, Inc.
3 *
4 * This file is part of GNU Wget.
5 *
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
9 * (at your option) any later version.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Wget. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21
22 #include <sys/types.h>
23 #include <dirent.h> // opendir, readdir
24 #include <stdint.h> // uint8_t
25 #include <stdio.h> // fmemopen
26 #include <string.h> // strncmp
27 #include <stdlib.h> // free
28 #include <fcntl.h> // open flags
29 #include <unistd.h> // close
30 #include <setjmp.h> // longjmp, setjmp
31
32 #include "wget.h"
33
34 #undef fopen_wgetrc
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 #include "progress.h"
40
41 // declarations for wget internal functions
42 int main_wget(int argc, const char **argv);
43 void cleanup(void);
44 FILE *fopen_wget(const char *pathname, const char *mode);
45 FILE *fopen_wgetrc(const char *pathname, const char *mode);
46 void exit_wget(int status);
47 #ifdef __cplusplus
48 }
49 #endif
50
51 #include "fuzzer.h"
52
fopen_wget(const char * pathname,const char * mode)53 FILE *fopen_wget(const char *pathname, const char *mode)
54 {
55 (void) pathname;
56 return fopen("/dev/null", mode);
57 }
58
fopen_wgetrc(const char * pathname,const char * mode)59 FILE *fopen_wgetrc(const char *pathname, const char *mode)
60 {
61 (void) pathname;
62 (void) mode;
63 return NULL;
64 }
65
66 #ifdef FUZZING
exit_wget(int status)67 void exit_wget(int status)
68 {
69 (void) status;
70 }
71 #endif
72
73
74 #define NAMEPOS (2 * sizeof(wgint) + sizeof(double))
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
76 {
77 void *progress;
78
79 if (size > 4096) // same as max_len = ... in .options file
80 return 0;
81
82 if (size < NAMEPOS)
83 return 0;
84
85 // CLOSE_STDERR
86
87 wgint start = ((wgint *) data)[0];
88 wgint end = ((wgint *) data)[1];
89 double dltime = ((wgint *) data)[2];
90
91 if (start < 0 || end < 0)
92 return 0;
93
94 if (start > end) {
95 wgint x = start;
96 start = end;
97 end = x;
98 }
99
100 // double dltime = ((double *) (data + 2 * sizeof(wgint)))[0];
101
102 char *filename = strndup((char *) (data + NAMEPOS), size - NAMEPOS);
103
104 // printf("%ld %ld %lf %s\n", start, end, dltime, filename);
105
106 set_progress_implementation("bar:force"); // [:force][:noscroll]
107
108 progress = progress_create (filename, start, end);
109 progress_update (progress, 0, dltime);
110 progress_update (progress, end - start, dltime);
111 progress_finish (progress, dltime);
112
113 set_progress_implementation("dot:default");// [:default|:binary|:mega|:giga]
114 progress = progress_create (filename, start, end);
115 progress_update (progress, 0, dltime);
116 progress_update (progress, end - start, dltime);
117 progress_finish (progress, dltime);
118
119 set_progress_implementation("dot:binary");// [:default|:binary|:mega|:giga]
120 progress = progress_create (filename, start, end);
121 progress_update (progress, 0, dltime);
122 progress_update (progress, end - start, dltime);
123 progress_finish (progress, dltime);
124
125 set_progress_implementation("dot:mega");// [:default|:binary|:mega|:giga]
126 progress = progress_create (filename, start, end);
127 progress_update (progress, 0, dltime);
128 progress_update (progress, end - start, dltime);
129 progress_finish (progress, dltime);
130
131 set_progress_implementation("dot:giga");// [:default|:binary|:mega|:giga]
132 progress = progress_create (filename, start, end);
133 progress_update (progress, 0, dltime);
134 progress_update (progress, end - start, dltime);
135 progress_finish (progress, dltime);
136
137 free(filename);
138
139 // RESTORE_STDERR
140
141 return 0;
142 }
143