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