/src/bzip2_compress_target.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | # Copyright 2018 Google Inc. |
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 | | |
25 | | extern int BZ2_bzBuffToBuffCompress(char* dest, |
26 | | unsigned int* destLen, |
27 | | char* source, |
28 | | unsigned int sourceLen, |
29 | | int blockSize100k, |
30 | | int verbosity, |
31 | | int workFactor); |
32 | | |
33 | | extern int BZ2_bzBuffToBuffDecompress(char* dest, |
34 | | unsigned int* destLen, |
35 | | char* source, |
36 | | unsigned int sourceLen, |
37 | | int small, |
38 | | int verbosity); |
39 | | |
40 | | int |
41 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
42 | 4.30k | { |
43 | 4.30k | int r, blockSize100k, workFactor, small; |
44 | 4.30k | unsigned int nZ, nOut; |
45 | | |
46 | | /* Copying @julian-seward1's comment from |
47 | | * https://github.com/google/oss-fuzz/pull/1887#discussion_r226852388 |
48 | | * |
49 | | * They just reflect the fact that the worst case output size is 101% |
50 | | * of the input size + 600 bytes (I assume -- this is now nearly 20 |
51 | | * years old). Since the buffer is in mallocville, presumably asan |
52 | | * will complain if it gets overrun. I doubt that will happen though. |
53 | | */ |
54 | 4.30k | nZ = size + 600 + (size / 100); |
55 | 4.30k | char *zbuf = malloc(nZ); |
56 | | |
57 | 4.30k | blockSize100k = (size % 11) + 1; |
58 | 4.30k | if (blockSize100k > 9) { |
59 | 767 | blockSize100k = 9; |
60 | 767 | } |
61 | 4.30k | workFactor = size % 251; |
62 | | |
63 | | // Choose highest compression (blockSize100k=9) |
64 | 4.30k | r = BZ2_bzBuffToBuffCompress(zbuf, &nZ, (char *)data, size, |
65 | 4.30k | blockSize100k, /*verbosity=*/0, workFactor); |
66 | 4.30k | if (r != BZ_OK) { |
67 | | #ifdef __DEBUG__ |
68 | | fprintf(stdout, "Compression error: %d\n", r); |
69 | | #endif |
70 | 0 | free(zbuf); |
71 | 0 | return 0; |
72 | 0 | } |
73 | | |
74 | 4.30k | nOut = size*2; |
75 | 4.30k | char *outbuf = malloc(nOut); |
76 | 4.30k | small = size % 2; |
77 | 4.30k | r = BZ2_bzBuffToBuffDecompress(outbuf, &nOut, zbuf, nZ, small, |
78 | 4.30k | /*verbosity=*/0); |
79 | 4.30k | if (r != BZ_OK) { |
80 | | #ifdef __DEBUG__ |
81 | | fprintf(stdout, "Decompression error: %d\n", r); |
82 | | #endif |
83 | 0 | free(zbuf); |
84 | 0 | free(outbuf); |
85 | 0 | return 0; |
86 | 0 | } |
87 | | |
88 | 4.30k | assert(nOut == size); |
89 | 4.30k | assert(memcmp(data, outbuf, size) == 0); |
90 | 4.30k | free(zbuf); |
91 | 4.30k | free(outbuf); |
92 | 4.30k | return 0; |
93 | 4.30k | } |