/src/bzip2_compress_target.c
Line  | Count  | Source  | 
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  | 3.54k  | { | 
43  | 3.54k  |     int r, blockSize100k, workFactor, small;  | 
44  | 3.54k  |     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  | 3.54k  |     nZ = size + 600 + (size / 100);  | 
55  | 3.54k  |     char *zbuf = malloc(nZ);  | 
56  |  |  | 
57  | 3.54k  |     blockSize100k = (size % 11) + 1;  | 
58  | 3.54k  |     if (blockSize100k > 9) { | 
59  | 587  |         blockSize100k = 9;  | 
60  | 587  |     }  | 
61  | 3.54k  |     workFactor = size % 251;  | 
62  |  |  | 
63  |  |     // Choose highest compression (blockSize100k=9)  | 
64  | 3.54k  |     r = BZ2_bzBuffToBuffCompress(zbuf, &nZ, (char *)data, size,  | 
65  | 3.54k  |             blockSize100k, /*verbosity=*/0, workFactor);  | 
66  | 3.54k  |     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  | 3.54k  |     nOut = size*2;  | 
75  | 3.54k  |     char *outbuf = malloc(nOut);  | 
76  | 3.54k  |     small = size % 2;  | 
77  | 3.54k  |     r = BZ2_bzBuffToBuffDecompress(outbuf, &nOut, zbuf, nZ, small,  | 
78  | 3.54k  |             /*verbosity=*/0);  | 
79  | 3.54k  |     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  | 3.54k  |     assert(nOut == size);  | 
89  | 3.54k  |     assert(memcmp(data, outbuf, size) == 0);  | 
90  | 3.54k  |     free(zbuf);  | 
91  | 3.54k  |     free(outbuf);  | 
92  | 3.54k  |     return 0;  | 
93  | 3.54k  | }  |