]> wimlib.net Git - wimlib/blob - tools/afl-fuzz/decompress/fuzz.c
tools/afl-fuzz: fix ctype calculation
[wimlib] / tools / afl-fuzz / decompress / fuzz.c
1 #include <assert.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <wimlib.h>
8
9 int main(int argc, char *argv[])
10 {
11         int fd;
12         struct stat stbuf;
13         uint8_t ctype;
14         size_t csize, uspace;
15         void *cdata, *udata;
16         struct wimlib_decompressor *d;
17         int ret;
18
19         fd = open(argv[1], O_RDONLY);
20         assert(fd >= 0);
21         ret = fstat(fd, &stbuf);
22         assert(!ret);
23
24         if (stbuf.st_size < 1)
25                 return 0;
26         ret = read(fd, &ctype, 1);
27         assert(ret == 1);
28         ctype = 1 + ((uint8_t)(ctype - 1) % 3); /* 1-3 */
29         csize = stbuf.st_size - 1;
30         uspace = csize * 8;
31
32         cdata = malloc(csize);
33         udata = malloc(uspace);
34
35         ret = read(fd, cdata, csize);
36         assert(ret == csize);
37
38         ret = wimlib_create_decompressor(ctype, uspace, &d);
39         if (ret == 0)
40                 wimlib_decompress(cdata, csize, udata, uspace, d);
41
42         free(udata);
43         free(cdata);
44         wimlib_free_decompressor(d);
45         return 0;
46 }