/src/lzo_decompress_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 <assert.h>  | 
20  |  | #include <stdbool.h>  | 
21  |  | #include <stdint.h>  | 
22  |  | #include <stdio.h>  | 
23  |  | #include <stdlib.h>  | 
24  |  |  | 
25  |  | #include "lzo1b.h"  | 
26  |  | #include "lzo1c.h"  | 
27  |  | #include "lzo1f.h"  | 
28  |  | #include "lzo1x.h"  | 
29  |  | #include "lzo1y.h"  | 
30  |  | #include "lzo1z.h"  | 
31  |  | #include "lzo2a.h"  | 
32  |  | #include "lzoconf.h"  | 
33  |  |  | 
34  |  | typedef int (*decompress_function)(const lzo_bytep, lzo_uint, lzo_bytep,  | 
35  |  |                                    lzo_uintp, lzo_voidp);  | 
36  |  |  | 
37  | 3.32k  | #define NUM_DECOMP 7  | 
38  |  |  | 
39  |  | static decompress_function funcArr[NUM_DECOMP] = { | 
40  |  |     &lzo1b_decompress_safe, &lzo1c_decompress_safe, &lzo1f_decompress_safe,  | 
41  |  |     &lzo1x_decompress_safe, &lzo1y_decompress_safe, &lzo1z_decompress_safe,  | 
42  |  |     &lzo2a_decompress_safe};  | 
43  |  |  | 
44  |  | /* lzo (de)compresses data in blocks. Block size is the  | 
45  |  |  * size of one such block. This size has a default value of 256KB.  | 
46  |  |  */  | 
47  |  | static const size_t kBufSize = 256 * 1024L;  | 
48  |  |  | 
49  | 3.32k  | extern int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 
50  | 3.32k  |   int r;  | 
51  | 3.32k  |   lzo_uint new_len;  | 
52  | 3.32k  |   if (size < 1) { | 
53  | 0  |     return 0;  | 
54  | 0  |   }  | 
55  |  |   /* Buffer into which compressed data provided by the fuzzer  | 
56  |  |    * is going to be decompressed. The buffer size is chosen  | 
57  |  |    * to be equal to the default block size (256KB) for  | 
58  |  |    * (de)compression.  | 
59  |  |    */  | 
60  | 3.32k  |   unsigned char* __LZO_MMODEL buf = malloc(kBufSize);  | 
61  | 3.32k  |   if (!buf) { | 
62  |  |     // OOM here is out of scope.  | 
63  | 0  |     return 0;  | 
64  | 0  |   }  | 
65  |  |  | 
66  | 3.32k  |   static bool isInit = false;  | 
67  | 3.32k  |   if (!isInit) { | 
68  | 1  |     if (lzo_init() != LZO_E_OK) { | 
69  |  | #ifdef __DEBUG__  | 
70  |  |       printf("internal error - lzo_init() failed !!!\n"); | 
71  |  | #endif  | 
72  | 0  |       free(buf);  | 
73  | 0  |       return 0;  | 
74  | 0  |     }  | 
75  | 1  |     isInit = true;  | 
76  | 1  |   }  | 
77  |  |  | 
78  |  |   // Decompress.  | 
79  | 3.32k  |   int idx = size % NUM_DECOMP;  | 
80  | 3.32k  |   new_len = kBufSize;  | 
81  |  |   // Work memory not necessary for decompression  | 
82  | 3.32k  |   r = (*funcArr[idx])(data, size, buf, &new_len, /*wrkmem=*/NULL);  | 
83  |  | #ifdef __DEBUG__  | 
84  |  |   if (r != LZO_E_OK) { | 
85  |  |     printf("error thrown by lzo1x_decompress_safe: %d\n", r); | 
86  |  |   }  | 
87  |  |   printf("decompressed %lu bytes back into %lu bytes\n", (unsigned long)size, | 
88  |  |          (unsigned long)new_len);  | 
89  |  | #endif  | 
90  | 3.32k  |   free(buf);  | 
91  | 3.32k  |   return 0;  | 
92  | 3.32k  | }  |