2 * Copyright (c) 2016-present, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
10 #define ZSTD_STATIC_LINKING_ONLY
12 #include "zstd_helpers.h"
13 #include "fuzz_helpers.h"
16 static void set(ZSTD_CCtx *cctx, ZSTD_cParameter param, unsigned value)
18 FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, param, value));
21 static void setRand(ZSTD_CCtx *cctx, ZSTD_cParameter param, unsigned min,
22 unsigned max, uint32_t *state) {
23 unsigned const value = FUZZ_rand32(state, min, max);
24 set(cctx, param, value);
27 ZSTD_compressionParameters FUZZ_randomCParams(size_t srcSize, uint32_t *state)
29 /* Select compression parameters */
30 ZSTD_compressionParameters cParams;
31 cParams.windowLog = FUZZ_rand32(state, ZSTD_WINDOWLOG_MIN, 15);
32 cParams.hashLog = FUZZ_rand32(state, ZSTD_HASHLOG_MIN, 15);
33 cParams.chainLog = FUZZ_rand32(state, ZSTD_CHAINLOG_MIN, 16);
34 cParams.searchLog = FUZZ_rand32(state, ZSTD_SEARCHLOG_MIN, 9);
35 cParams.searchLength = FUZZ_rand32(state, ZSTD_SEARCHLENGTH_MIN,
36 ZSTD_SEARCHLENGTH_MAX);
37 cParams.targetLength = FUZZ_rand32(state, ZSTD_TARGETLENGTH_MIN,
39 cParams.strategy = FUZZ_rand32(state, ZSTD_fast, ZSTD_btultra);
40 return ZSTD_adjustCParams(cParams, srcSize, 0);
43 ZSTD_frameParameters FUZZ_randomFParams(uint32_t *state)
45 /* Select frame parameters */
46 ZSTD_frameParameters fParams;
47 fParams.contentSizeFlag = FUZZ_rand32(state, 0, 1);
48 fParams.checksumFlag = FUZZ_rand32(state, 0, 1);
49 fParams.noDictIDFlag = FUZZ_rand32(state, 0, 1);
53 ZSTD_parameters FUZZ_randomParams(size_t srcSize, uint32_t *state)
55 ZSTD_parameters params;
56 params.cParams = FUZZ_randomCParams(srcSize, state);
57 params.fParams = FUZZ_randomFParams(state);
61 void FUZZ_setRandomParameters(ZSTD_CCtx *cctx, size_t srcSize, uint32_t *state)
63 ZSTD_compressionParameters cParams = FUZZ_randomCParams(srcSize, state);
64 set(cctx, ZSTD_p_windowLog, cParams.windowLog);
65 set(cctx, ZSTD_p_hashLog, cParams.hashLog);
66 set(cctx, ZSTD_p_chainLog, cParams.chainLog);
67 set(cctx, ZSTD_p_searchLog, cParams.searchLog);
68 set(cctx, ZSTD_p_minMatch, cParams.searchLength);
69 set(cctx, ZSTD_p_targetLength, cParams.targetLength);
70 set(cctx, ZSTD_p_compressionStrategy, cParams.strategy);
71 /* Select frame parameters */
72 setRand(cctx, ZSTD_p_contentSizeFlag, 0, 1, state);
73 setRand(cctx, ZSTD_p_checksumFlag, 0, 1, state);
74 setRand(cctx, ZSTD_p_dictIDFlag, 0, 1, state);
75 /* Select long distance matchig parameters */
76 setRand(cctx, ZSTD_p_enableLongDistanceMatching, 0, 1, state);
77 setRand(cctx, ZSTD_p_ldmHashLog, ZSTD_HASHLOG_MIN, 16, state);
78 setRand(cctx, ZSTD_p_ldmMinMatch, ZSTD_LDM_MINMATCH_MIN,
79 ZSTD_LDM_MINMATCH_MAX, state);
80 setRand(cctx, ZSTD_p_ldmBucketSizeLog, 0, ZSTD_LDM_BUCKETSIZELOG_MAX,
82 setRand(cctx, ZSTD_p_ldmHashEveryLog, 0,
83 ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN, state);