xref: /freebsd/contrib/libpcap/testprogs/fuzz/onefile.c (revision 7ab1a32cd43cbae61ad4dd435d6a482bbf61cb52)
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 
5 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
6 void fuzz_openFile(const char * name);
7 
8 int main(int argc, char** argv)
9 {
10     FILE * fp;
11     uint8_t *Data;
12     size_t Size;
13 
14     if (argc == 3) {
15         fuzz_openFile(argv[2]);
16     } else if (argc != 2) {
17         return 1;
18     }
19     //opens the file, get its size, and reads it into a buffer
20     fp = fopen(argv[1], "rb");
21     if (fp == NULL) {
22         return 2;
23     }
24     if (fseek(fp, 0L, SEEK_END) != 0) {
25         fclose(fp);
26         return 2;
27     }
28     Size = ftell(fp);
29     if (Size == (size_t) -1) {
30         fclose(fp);
31         return 2;
32     }
33     if (fseek(fp, 0L, SEEK_SET) != 0) {
34         fclose(fp);
35         return 2;
36     }
37     Data = malloc(Size);
38     if (Data == NULL) {
39         fclose(fp);
40         return 2;
41     }
42     if (fread(Data, Size, 1, fp) != 1) {
43         fclose(fp);
44         free(Data);
45         return 2;
46     }
47 
48     //launch fuzzer
49     LLVMFuzzerTestOneInput(Data, Size);
50     free(Data);
51     fclose(fp);
52     return 0;
53 }
54 
55