/src/lzo_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 <stdio.h> |
20 | | #include <stdlib.h> |
21 | | #include <stdint.h> |
22 | | #include <stdbool.h> |
23 | | #include <assert.h> |
24 | | #include "lzo1x.h" |
25 | | |
26 | | /* Work-memory needed for compression. Allocate memory in units |
27 | | * of 'lzo_align_t' (instead of 'char') to make sure it is properly aligned. |
28 | | */ |
29 | | #define HEAP_ALLOC(var,size) \ |
30 | | lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ] |
31 | | |
32 | | static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS); |
33 | | |
34 | | extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
35 | 1.07k | { |
36 | 1.07k | int r; |
37 | 1.07k | lzo_uint out_len; |
38 | 1.07k | lzo_uint new_len; |
39 | | |
40 | | /* Since we allocate in/out on the stack, |
41 | | * we can't handle too large size. |
42 | | */ |
43 | 1.07k | if (size > (1 << 20)) |
44 | 0 | size = 1 << 20; |
45 | | |
46 | | /* We want to compress the data block at 'in' with length 'IN_LEN' to |
47 | | * the block at 'out'. Because the input block may be incompressible, |
48 | | * we must provide a little more output space in case that compression |
49 | | * is not possible. |
50 | | */ |
51 | 1.07k | unsigned char __LZO_MMODEL in[size > 0 ? size : 1]; |
52 | 1.07k | unsigned char __LZO_MMODEL out[size + size/16 + 64 + 3]; |
53 | | |
54 | 1.07k | static bool isInit = false; |
55 | 1.07k | if (!isInit) |
56 | 1 | { |
57 | 1 | if (lzo_init() != LZO_E_OK) |
58 | 0 | { |
59 | | #ifdef __DEBUG__ |
60 | | printf("internal error - lzo_init() failed !!!\n"); |
61 | | #endif |
62 | 0 | return 0; |
63 | 0 | } |
64 | 1 | isInit = true; |
65 | 1 | } |
66 | | |
67 | | /* Compress with LZO1X-1. */ |
68 | 1.07k | r = lzo1x_1_compress(data, size, out, &out_len, wrkmem); |
69 | 1.07k | assert(r == LZO_E_OK); |
70 | | #ifdef __DEBUG__ |
71 | | printf("compressed %lu bytes into %lu bytes\n", |
72 | | (unsigned long) size, (unsigned long) out_len); |
73 | | #endif |
74 | | |
75 | | /* check for an incompressible block */ |
76 | 1.07k | if (out_len >= size) |
77 | 395 | { |
78 | | #ifdef __DEBUG__ |
79 | | printf("This block contains incompressible data.\n"); |
80 | | #endif |
81 | 395 | return 0; |
82 | 395 | } |
83 | | |
84 | | // Decompress |
85 | 680 | new_len = size; |
86 | 680 | r = lzo1x_decompress(out, out_len, in, &new_len,/*wrkmem=*/NULL); |
87 | 680 | assert(r == LZO_E_OK && new_len == size); |
88 | | #ifdef __DEBUG__ |
89 | | printf("decompressed %lu bytes back into %lu bytes\n", |
90 | | (unsigned long) out_len, (unsigned long) size); |
91 | | #endif |
92 | 680 | return 0; |
93 | 680 | } |