aboutsummaryrefslogtreecommitdiffhomepage
path: root/projects/lzo/lzo_decompress_target.c
blob: 5b742c1ca90a8dae5f44c0ce10edcc281c4eec4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "lzo1b.h"
#include "lzo1c.h"
#include "lzo1f.h"
#include "lzo1x.h"
#include "lzo1y.h"
#include "lzo1z.h"
#include "lzo2a.h"

typedef int (*decompress_function)( const lzo_bytep, lzo_uint  ,
                                lzo_bytep, lzo_uintp,
                                lzo_voidp  );

#define NUM_DECOMP   7

static decompress_function funcArr[NUM_DECOMP] =
{
        &lzo1b_decompress_safe,
        &lzo1c_decompress_safe,
        &lzo1f_decompress_safe,
        &lzo1x_decompress_safe,
        &lzo1y_decompress_safe,
        &lzo1z_decompress_safe,
        &lzo2a_decompress_safe
};

/* lzo (de)compresses data in blocks. Block size is the
 * size of one such block. This size has a default value of 256KB.
 */
static const size_t bufSize = 256 * 1024L;

extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    int r;
    lzo_uint new_len;
    if (size < 1){
        return 0;
    }
    /* Buffer into which compressed data provided by the fuzzer
     * is going to be decompressed. The buffer size is chosen
     * to be equal to the default block size (256KB) for
     * (de)compression.
     */
    unsigned char __LZO_MMODEL out[bufSize];

    static bool isInit = false;
    if (!isInit)
    {
        if (lzo_init() != LZO_E_OK)
        {
#ifdef __DEBUG__
            printf("internal error - lzo_init() failed !!!\n");
#endif
            return 0;
        }
        isInit = true;
    }

    // Decompress.
    int idx = size % NUM_DECOMP;
    new_len = bufSize;
    // Work memory not necessary for decompression
    r = (*funcArr[idx])(data, size, out, &new_len, /*wrkmem=*/NULL);
#ifdef __DEBUG__
    if (r != LZO_E_OK)
    {
        printf("error thrown by lzo1x_decompress_safe: %d\n", r);
    }
    printf("decompressed %lu bytes back into %lu bytes\n",
            (unsigned long) size, (unsigned long) new_len);
#endif
    return 0;
}