2019-08-16 23:43:28 +00:00
|
|
|
#include "fuzz_data_producer.h"
|
|
|
|
|
2019-08-17 00:14:47 +00:00
|
|
|
struct FUZZ_dataProducer_s{
|
|
|
|
const uint8_t *data;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2019-08-16 23:43:28 +00:00
|
|
|
FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) {
|
|
|
|
FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t));
|
2019-08-17 00:14:47 +00:00
|
|
|
|
|
|
|
FUZZ_ASSERT(producer != NULL);
|
|
|
|
|
2019-08-16 23:43:28 +00:00
|
|
|
producer->data = data;
|
|
|
|
producer->size = size;
|
|
|
|
return producer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); }
|
|
|
|
|
2019-09-13 23:04:48 +00:00
|
|
|
uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min,
|
2019-08-16 23:43:28 +00:00
|
|
|
uint32_t max) {
|
|
|
|
FUZZ_ASSERT(min <= max);
|
|
|
|
|
|
|
|
uint32_t range = max - min;
|
|
|
|
uint32_t rolling = range;
|
|
|
|
uint32_t result = 0;
|
|
|
|
|
|
|
|
while (rolling > 0 && producer->size > 0) {
|
|
|
|
uint8_t next = *(producer->data + producer->size - 1);
|
|
|
|
producer->size -= 1;
|
|
|
|
result = (result << 8) | next;
|
|
|
|
rolling >>= 8;
|
|
|
|
}
|
|
|
|
|
2019-09-13 21:07:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
2019-08-16 23:43:28 +00:00
|
|
|
|
2019-09-13 23:04:48 +00:00
|
|
|
uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max)
|
2019-09-13 21:07:52 +00:00
|
|
|
{
|
|
|
|
uint32_t range = max - min;
|
|
|
|
if (range == 0xffffffff) {
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
return min + seed % (range + 1);
|
|
|
|
}
|
|
|
|
|
2019-09-13 23:04:48 +00:00
|
|
|
uint32_t FUZZ_dataProducer_range32(FUZZ_dataProducer_t* producer,
|
2019-09-13 21:07:52 +00:00
|
|
|
uint32_t min, uint32_t max)
|
|
|
|
{
|
2019-09-13 23:04:48 +00:00
|
|
|
size_t const seed = FUZZ_dataProducer_uint32(producer, min, max);
|
|
|
|
return FUZZ_getRange_from_uint32(seed, min, max);
|
2019-08-16 23:43:28 +00:00
|
|
|
}
|
2019-08-17 00:14:47 +00:00
|
|
|
|
2019-08-30 17:27:42 +00:00
|
|
|
LZ4F_frameInfo_t FUZZ_dataProducer_frameInfo(FUZZ_dataProducer_t* producer)
|
|
|
|
{
|
|
|
|
LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO;
|
2019-09-13 23:04:48 +00:00
|
|
|
info.blockSizeID = FUZZ_dataProducer_range32(producer, LZ4F_max64KB - 1, LZ4F_max4MB);
|
2019-08-30 17:27:42 +00:00
|
|
|
if (info.blockSizeID < LZ4F_max64KB) {
|
|
|
|
info.blockSizeID = LZ4F_default;
|
|
|
|
}
|
2019-09-13 23:04:48 +00:00
|
|
|
info.blockMode = FUZZ_dataProducer_range32(producer, LZ4F_blockLinked, LZ4F_blockIndependent);
|
|
|
|
info.contentChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noContentChecksum,
|
2019-08-30 17:27:42 +00:00
|
|
|
LZ4F_contentChecksumEnabled);
|
2019-09-13 23:04:48 +00:00
|
|
|
info.blockChecksumFlag = FUZZ_dataProducer_range32(producer, LZ4F_noBlockChecksum,
|
2019-08-30 17:27:42 +00:00
|
|
|
LZ4F_blockChecksumEnabled);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
LZ4F_preferences_t FUZZ_dataProducer_preferences(FUZZ_dataProducer_t* producer)
|
|
|
|
{
|
|
|
|
LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES;
|
|
|
|
prefs.frameInfo = FUZZ_dataProducer_frameInfo(producer);
|
2019-09-13 23:04:48 +00:00
|
|
|
prefs.compressionLevel = FUZZ_dataProducer_range32(producer, 0, LZ4HC_CLEVEL_MAX + 3) - 3;
|
|
|
|
prefs.autoFlush = FUZZ_dataProducer_range32(producer, 0, 1);
|
|
|
|
prefs.favorDecSpeed = FUZZ_dataProducer_range32(producer, 0, 1);
|
2019-08-30 17:27:42 +00:00
|
|
|
return prefs;
|
|
|
|
}
|
|
|
|
|
2019-08-17 00:14:47 +00:00
|
|
|
size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer){
|
|
|
|
return producer->size;
|
|
|
|
}
|