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