1 #include "fuzz_helpers.h"
2 #include "lz4_helpers.h"
3 #include "lz4hc.h"
4 
FUZZ_randomFrameInfo(uint32_t * seed)5 LZ4F_frameInfo_t FUZZ_randomFrameInfo(uint32_t* seed)
6 {
7     LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO;
8     info.blockSizeID = FUZZ_rand32(seed, LZ4F_max64KB - 1, LZ4F_max4MB);
9     if (info.blockSizeID < LZ4F_max64KB) {
10         info.blockSizeID = LZ4F_default;
11     }
12     info.blockMode = FUZZ_rand32(seed, LZ4F_blockLinked, LZ4F_blockIndependent);
13     info.contentChecksumFlag = FUZZ_rand32(seed, LZ4F_noContentChecksum,
14                                            LZ4F_contentChecksumEnabled);
15     info.blockChecksumFlag = FUZZ_rand32(seed, LZ4F_noBlockChecksum,
16                                          LZ4F_blockChecksumEnabled);
17     return info;
18 }
19 
FUZZ_randomPreferences(uint32_t * seed)20 LZ4F_preferences_t FUZZ_randomPreferences(uint32_t* seed)
21 {
22     LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES;
23     prefs.frameInfo = FUZZ_randomFrameInfo(seed);
24     prefs.compressionLevel = FUZZ_rand32(seed, 0, LZ4HC_CLEVEL_MAX + 3) - 3;
25     prefs.autoFlush = FUZZ_rand32(seed, 0, 1);
26     prefs.favorDecSpeed = FUZZ_rand32(seed, 0, 1);
27     return prefs;
28 }
29 
FUZZ_decompressFrame(void * dst,const size_t dstCapacity,const void * src,const size_t srcSize)30 size_t FUZZ_decompressFrame(void* dst, const size_t dstCapacity,
31                             const void* src, const size_t srcSize)
32 {
33     LZ4F_decompressOptions_t opts;
34     memset(&opts, 0, sizeof(opts));
35     opts.stableDst = 1;
36     LZ4F_dctx* dctx;
37     LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION);
38     FUZZ_ASSERT(dctx);
39 
40     size_t dstSize = dstCapacity;
41     size_t srcConsumed = srcSize;
42     size_t const rc =
43             LZ4F_decompress(dctx, dst, &dstSize, src, &srcConsumed, &opts);
44     FUZZ_ASSERT(!LZ4F_isError(rc));
45     FUZZ_ASSERT(rc == 0);
46     FUZZ_ASSERT(srcConsumed == srcSize);
47 
48     LZ4F_freeDecompressionContext(dctx);
49 
50     return dstSize;
51 }
52