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