Line  | Count  | Source  | 
1  |  | /*  | 
2  |  | # Copyright 2022 Google LLC  | 
3  |  | #  | 
4  |  | # Licensed under the Apache License, Version 2.0 (the "License");  | 
5  |  | # you may not use this file except in compliance with the License.  | 
6  |  | # You may obtain a copy of the License at  | 
7  |  | #  | 
8  |  | #      http://www.apache.org/licenses/LICENSE-2.0  | 
9  |  | #  | 
10  |  | # Unless required by applicable law or agreed to in writing, software  | 
11  |  | # distributed under the License is distributed on an "AS IS" BASIS,  | 
12  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
13  |  | # See the License for the specific language governing permissions and  | 
14  |  | # limitations under the License.  | 
15  |  | #  | 
16  |  | ###############################################################################  | 
17  |  | */  | 
18  |  |  | 
19  |  | #include "bzlib.h"  | 
20  |  | #include <stdint.h>  | 
21  |  | #include <stdlib.h>  | 
22  |  | #include <assert.h>  | 
23  |  | #include <string.h>  | 
24  |  | #include <stddef.h>  | 
25  |  | #include <unistd.h>  | 
26  |  | #include <stdio.h>  | 
27  |  | #include <stdbool.h>  | 
28  |  |  | 
29  | 3.42k  | static void fuzzer_write_data(FILE *file, const uint8_t *data, size_t size) { | 
30  | 3.42k  |   int    bzerr         = 0;  | 
31  | 3.42k  |   int    blockSize100k = 9;  | 
32  | 3.42k  |   int    verbosity     = 0;  | 
33  | 3.42k  |   int    workFactor    = 30;  | 
34  | 3.42k  |   uint   nbytes_in_lo32, nbytes_in_hi32;  | 
35  | 3.42k  |   uint   nbytes_out_lo32, nbytes_out_hi32;  | 
36  |  |  | 
37  | 3.42k  |   BZFILE* bzf = BZ2_bzWriteOpen ( &bzerr, file,  | 
38  | 3.42k  |                            blockSize100k, verbosity, workFactor );  | 
39  |  |  | 
40  | 3.42k  |   BZ2_bzWrite (&bzerr, bzf, (void*)data, size);  | 
41  |  |  | 
42  | 3.42k  |   BZ2_bzWriteClose64 ( &bzerr, bzf, 0,  | 
43  | 3.42k  |                         &nbytes_in_lo32, &nbytes_in_hi32,  | 
44  | 3.42k  |                         &nbytes_out_lo32, &nbytes_out_hi32 );  | 
45  | 3.42k  |   return;  | 
46  | 3.42k  | }  | 
47  |  |  | 
48  | 3.42k  | static void fuzzer_read_data(char *filename) { | 
49  | 3.42k  |   int    bzerr          = 0;  | 
50  | 3.42k  |   char   obuf[BZ_MAX_UNUSED];  | 
51  |  |    | 
52  | 3.42k  |   BZFILE* bzf2 = BZ2_bzopen(filename,"rb");  | 
53  |  |  | 
54  | 78.2k  |   while (bzerr == BZ_OK) { | 
55  | 74.8k  |       BZ2_bzRead ( &bzerr, bzf2, obuf, BZ_MAX_UNUSED);  | 
56  | 74.8k  |   }  | 
57  |  |  | 
58  | 3.42k  |   BZ2_bzReadClose ( &bzerr, bzf2);  | 
59  | 3.42k  |   return;  | 
60  | 3.42k  | }  | 
61  |  |  | 
62  |  | int  | 
63  |  | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)  | 
64  | 6.06k  | { | 
65  | 6.06k  |   char* filename = strdup("/tmp/generate_temporary_file.XXXXXX"); | 
66  | 6.06k  |   if (!filename) { | 
67  | 0  |     perror("Failed to allocate file name buffer."); | 
68  | 0  |     abort();  | 
69  | 0  |   }  | 
70  | 6.06k  |   const int file_descriptor = mkstemp(filename);  | 
71  | 6.06k  |   if (file_descriptor < 0) { | 
72  | 0  |     perror("Failed to make temporary file."); | 
73  | 0  |     abort();  | 
74  | 0  |   }  | 
75  | 6.06k  |   FILE* file = fdopen(file_descriptor, "wb");  | 
76  | 6.06k  |   if (!file) { | 
77  | 0  |     perror("Failed to open file descriptor."); | 
78  | 0  |     close(file_descriptor);  | 
79  | 0  |     abort();  | 
80  | 0  |   }  | 
81  |  |  | 
82  | 6.06k  |   fuzzer_write_data(file, data, size);  | 
83  |  |  | 
84  | 6.06k  |   fuzzer_read_data(filename);  | 
85  |  |  | 
86  | 6.06k  |   BZ2_bzlibVersion();  | 
87  |  |  | 
88  | 6.06k  |   BZ2_bzflush(file);  | 
89  | 6.06k  |   fclose(file);  | 
90  |  |  | 
91  | 6.06k  |   if (unlink(filename) != 0) { | 
92  | 0  |     perror("WARNING: Failed to delete temporary file."); | 
93  | 0  |   }  | 
94  | 6.06k  |   free(filename);  | 
95  | 6.06k  |   return 0;  | 
96  | 6.06k  | }  |