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
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)34 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
35 {
36 int r;
37 lzo_uint out_len;
38 lzo_uint new_len;
39 /* We want to compress the data block at 'in' with length 'IN_LEN' to
40 * the block at 'out'. Because the input block may be incompressible,
41 * we must provide a little more output space in case that compression
42 * is not possible.
43 */
44 unsigned char __LZO_MMODEL in[size];
45 unsigned char __LZO_MMODEL out[size + size/16 + 64 + 3];
46
47 static bool isInit = false;
48 if (!isInit)
49 {
50 if (lzo_init() != LZO_E_OK)
51 {
52 #ifdef __DEBUG__
53 printf("internal error - lzo_init() failed !!!\n");
54 #endif
55 return 0;
56 }
57 isInit = true;
58 }
59
60 /* Compress with LZO1X-1. */
61 r = lzo1x_1_compress(data, size, out, &out_len, wrkmem);
62 assert(r == LZO_E_OK);
63 #ifdef __DEBUG__
64 printf("compressed %lu bytes into %lu bytes\n",
65 (unsigned long) size, (unsigned long) out_len);
66 #endif
67
68 /* check for an incompressible block */
69 if (out_len >= size)
70 {
71 #ifdef __DEBUG__
72 printf("This block contains incompressible data.\n");
73 #endif
74 return 0;
75 }
76
77 // Decompress
78 new_len = size;
79 r = lzo1x_decompress(out, out_len, in, &new_len,/*wrkmem=*/NULL);
80 assert(r == LZO_E_OK && new_len == size);
81 #ifdef __DEBUG__
82 printf("decompressed %lu bytes back into %lu bytes\n",
83 (unsigned long) out_len, (unsigned long) size);
84 #endif
85 return 0;
86 }
87