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  | 2.63k  | static void fuzzer_write_data(FILE *file, const uint8_t *data, size_t size) { | 
30  | 2.63k  |   int    bzerr         = 0;  | 
31  | 2.63k  |   int    blockSize100k = 9;  | 
32  | 2.63k  |   int    verbosity     = 0;  | 
33  | 2.63k  |   int    workFactor    = 30;  | 
34  | 2.63k  |   uint   nbytes_in_lo32, nbytes_in_hi32;  | 
35  | 2.63k  |   uint   nbytes_out_lo32, nbytes_out_hi32;  | 
36  |  |  | 
37  | 2.63k  |   BZFILE* bzf = BZ2_bzWriteOpen ( &bzerr, file,  | 
38  | 2.63k  |                            blockSize100k, verbosity, workFactor );  | 
39  |  |  | 
40  | 2.63k  |   BZ2_bzwrite(bzf, (void*)data, size);  | 
41  |  |  | 
42  | 2.63k  |   BZ2_bzWriteClose64 ( &bzerr, bzf, 0,  | 
43  | 2.63k  |                         &nbytes_in_lo32, &nbytes_in_hi32,  | 
44  | 2.63k  |                         &nbytes_out_lo32, &nbytes_out_hi32 );  | 
45  | 2.63k  | }  | 
46  |  |  | 
47  | 2.63k  | static void fuzzer_read_data(const int file_descriptor) { | 
48  | 2.63k  |   int    bzerr         = 0;  | 
49  | 2.63k  |   int    nUnused       = 0;  | 
50  | 2.63k  |   char   obuf[BZ_MAX_UNUSED];  | 
51  | 2.63k  |   void*  unusedTmpV;  | 
52  |  |  | 
53  | 2.63k  |   BZFILE* bzf2 = BZ2_bzdopen(file_descriptor, "rb");  | 
54  |  |  | 
55  | 5.27k  |   while (bzerr == BZ_OK) { | 
56  | 2.63k  |       BZ2_bzread(bzf2, obuf, BZ_MAX_UNUSED);  | 
57  | 2.63k  |       BZ2_bzReadGetUnused( &bzerr, bzf2, &unusedTmpV, &nUnused);  | 
58  | 2.63k  |   }  | 
59  |  |  | 
60  | 2.63k  |   BZ2_bzclose(bzf2);  | 
61  | 2.63k  | }  | 
62  |  |  | 
63  |  | int  | 
64  |  | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)  | 
65  | 6.06k  | { | 
66  | 6.06k  |   int*  bzerror;  | 
67  | 6.06k  |   char* filename = strdup("/tmp/generate_temporary_file.XXXXXX"); | 
68  |  |  | 
69  | 6.06k  |   if (!filename) { | 
70  | 0  |     perror("Failed to allocate file name buffer.\n"); | 
71  | 0  |     abort();  | 
72  | 0  |   }  | 
73  | 6.06k  |   const int file_descriptor = mkstemp(filename);  | 
74  | 6.06k  |   if (file_descriptor < 0) { | 
75  | 0  |     perror("Failed to make temporary file.\n"); | 
76  | 0  |     abort();  | 
77  | 0  |   }  | 
78  | 6.06k  |   FILE* file = fdopen(file_descriptor, "wb");  | 
79  |  |     | 
80  | 6.06k  |   if (!file) { | 
81  | 0  |     perror("Failed to open file descriptor."); | 
82  | 0  |     close(file_descriptor);  | 
83  | 0  |     BZ2_bzerror(file,bzerror);  | 
84  | 0  |     abort();  | 
85  | 0  |   }  | 
86  |  |  | 
87  | 6.06k  |   fuzzer_write_data(file, data, size);  | 
88  |  |  | 
89  | 6.06k  |   fuzzer_read_data(file_descriptor);  | 
90  |  |  | 
91  | 6.06k  |   BZ2_bzlibVersion();  | 
92  |  |     | 
93  | 6.06k  |   BZ2_bzflush(file);  | 
94  | 6.06k  |   fclose(file);  | 
95  |  |  | 
96  | 6.06k  |   if (unlink(filename) != 0) { | 
97  | 0  |     perror("WARNING: Failed to delete temporary file."); | 
98  | 0  |   }  | 
99  | 6.06k  |   free(filename);  | 
100  | 6.06k  |   return 0;  | 
101  | 6.06k  | }  |