/src/lzo_decompress_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 <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 | 3.49k | #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 | | |
54 | | extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
55 | 3.49k | { |
56 | 3.49k | int r; |
57 | 3.49k | lzo_uint new_len; |
58 | 3.49k | if (size < 1){ |
59 | 0 | return 0; |
60 | 0 | } |
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 | 3.49k | unsigned char __LZO_MMODEL out[bufSize]; |
67 | | |
68 | 3.49k | static bool isInit = false; |
69 | 3.49k | if (!isInit) |
70 | 1 | { |
71 | 1 | if (lzo_init() != LZO_E_OK) |
72 | 0 | { |
73 | | #ifdef __DEBUG__ |
74 | | printf("internal error - lzo_init() failed !!!\n"); |
75 | | #endif |
76 | 0 | return 0; |
77 | 0 | } |
78 | 1 | isInit = true; |
79 | 1 | } |
80 | | |
81 | | // Decompress. |
82 | 3.49k | int idx = size % NUM_DECOMP; |
83 | 3.49k | new_len = bufSize; |
84 | | // Work memory not necessary for decompression |
85 | 3.49k | 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 | 3.49k | return 0; |
95 | 3.49k | } |