/src/c-blosc/tests/fuzz/fuzz_compress.c
Line | Count | Source |
1 | | #include <stdint.h> |
2 | | #include <stdlib.h> |
3 | | |
4 | | #include "blosc.h" |
5 | | |
6 | | #ifdef __cplusplus |
7 | | extern "C" { |
8 | | #endif |
9 | | |
10 | 11.8k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
11 | 11.8k | const char *const compressors[] = { "blosclz", "lz4", "lz4hc", "snappy", "zlib", "zstd" }; |
12 | 11.8k | int level = 9, filter = BLOSC_BITSHUFFLE, cindex = 0, i = 0; |
13 | 11.8k | size_t nbytes, cbytes, blocksize; |
14 | 11.8k | void *output, *input; |
15 | | |
16 | 11.8k | blosc_set_nthreads(1); |
17 | | |
18 | 11.8k | if (size > 0) |
19 | 11.8k | level = data[0] % (9 + 1); |
20 | 11.8k | if (size > 1) |
21 | 11.8k | filter = data[1] % (BLOSC_BITSHUFFLE + 1); |
22 | 11.8k | if (size > 2) |
23 | 11.8k | cindex = data[2]; |
24 | | |
25 | | /* Find next available compressor */ |
26 | 12.6k | while (blosc_set_compressor(compressors[cindex % 6]) == -1 && i < 6) { |
27 | 858 | cindex++, i++; |
28 | 858 | } |
29 | 11.8k | if (i == 6) { |
30 | | /* No compressors available */ |
31 | 0 | return 0; |
32 | 0 | } |
33 | | |
34 | 11.8k | if (size > 3 && data[3] % 7 == 0) |
35 | 4.23k | blosc_set_blocksize(4096); |
36 | | |
37 | 11.8k | if (size > 4) |
38 | 11.8k | blosc_set_splitmode(data[4] % BLOSC_FORWARD_COMPAT_SPLIT + 1); |
39 | | |
40 | 11.8k | output = malloc(size + 1); |
41 | 11.8k | if (output == NULL) |
42 | 0 | return 0; |
43 | | |
44 | 11.8k | if (blosc_compress(level, filter, 1, size, data, output, size) == 0) { |
45 | | /* Cannot compress src buffer into dest */ |
46 | 1.16k | free(output); |
47 | 1.16k | return 0; |
48 | 1.16k | } |
49 | | |
50 | 10.6k | blosc_cbuffer_sizes(output, &nbytes, &cbytes, &blocksize); |
51 | | |
52 | 10.6k | input = malloc(cbytes); |
53 | 10.6k | if (input != NULL) { |
54 | 10.6k | blosc_decompress(output, input, cbytes); |
55 | 10.6k | free(input); |
56 | 10.6k | } |
57 | | |
58 | 10.6k | free(output); |
59 | | |
60 | 10.6k | return 0; |
61 | 11.8k | } |
62 | | |
63 | | #ifdef __cplusplus |
64 | | } |
65 | | #endif |