1 #include "fuzz_data_producer.h"
2
3 struct FUZZ_dataProducer_s{
4 const uint8_t *data;
5 size_t size;
6 };
7
FUZZ_dataProducer_create(const uint8_t * data,size_t size)8 FUZZ_dataProducer_t* FUZZ_dataProducer_create(const uint8_t* data, size_t size) {
9 FUZZ_dataProducer_t* const producer = malloc(sizeof(FUZZ_dataProducer_t));
10
11 FUZZ_ASSERT(producer != NULL);
12
13 producer->data = data;
14 producer->size = size;
15 return producer;
16 }
17
FUZZ_dataProducer_free(FUZZ_dataProducer_t * producer)18 void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); }
19
FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t * producer)20 uint32_t FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t *producer) {
21 const uint8_t* data = producer->data;
22 const size_t size = producer->size;
23 if (size == 0) {
24 return 0;
25 } else if (size < 4) {
26 producer->size -= 1;
27 return (uint32_t)data[size - 1];
28 } else {
29 producer->size -= 4;
30 return *(data + size - 4);
31 }
32 }
33
FUZZ_getRange_from_uint32(uint32_t seed,uint32_t min,uint32_t max)34 uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max)
35 {
36 uint32_t range = max - min;
37 if (range == 0xffffffff) {
38 return seed;
39 }
40 return min + seed % (range + 1);
41 }
42
FUZZ_dataProducer_range32(FUZZ_dataProducer_t * producer,uint32_t min,uint32_t max)43 uint32_t FUZZ_dataProducer_range32(FUZZ_dataProducer_t* producer,
44 uint32_t min, uint32_t max)
45 {
46 size_t const seed = FUZZ_dataProducer_retrieve32(producer);
47 return FUZZ_getRange_from_uint32(seed, min, max);
48 }
49
FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t * producer)50 LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer)
51 {
52 LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO;
53 info.blockSizeID = FUZZ_dataProducer_range32(producer, LZ4F_max64KB - 1, LZ4F_max4MB);
54 if (info.blockSizeID < LZ4F_max64KB) {
55 info.blockSizeID = LZ4F_default;
56 }
57 info.blockMode = FUZZ_dataProducer_range32(producer, LZ4F_blockLinked, LZ4F_blockIndependent);
58 info.contentChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noContentChecksum,
59 LZ4F_contentChecksumEnabled);
60 info.blockChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noBlockChecksum,
61 LZ4F_blockChecksumEnabled);
62 return info;
63 }
64
FUZZ_dataProducer_preferences(FUZZ_dataProducer_t * producer)65 LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer)
66 {
67 LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES;
68 prefs.frameInfo = FUZZ_dataProducer_frameInfo(producer);
69 prefs.compressionLevel = FUZZ_dataProducer_range32(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3;
70 prefs.autoFlush = FUZZ_dataProducer_range32(producer, 0, 1);
71 prefs.favorDecSpeed = FUZZ_dataProducer_range32(producer, 0, 1);
72 return prefs;
73 }
74
FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t * producer)75 size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer){
76 return producer->size;
77 }
78