2019-08-16 17:50:46 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-08-16 21:19:06 +00:00
|
|
|
typedef struct {
|
|
|
|
const uint8_t *data;
|
|
|
|
size_t size;
|
|
|
|
} FUZZ_dataProducer_t;
|
|
|
|
|
|
|
|
FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) {
|
|
|
|
FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t));
|
|
|
|
producer->data = data;
|
|
|
|
producer->size = size;
|
|
|
|
return producer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); }
|
|
|
|
|
|
|
|
uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer, uint32_t min,
|
|
|
|
uint32_t max) {
|
2019-08-16 17:50:46 +00:00
|
|
|
if (min > max) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t range = max - min;
|
|
|
|
uint32_t rolling = range;
|
|
|
|
uint32_t result = 0;
|
|
|
|
|
2019-08-16 21:19:06 +00:00
|
|
|
while (rolling > 0 && producer->size > 0) {
|
|
|
|
uint8_t next = *(producer->data + producer->size - 1);
|
|
|
|
producer->size -= 1;
|
2019-08-16 17:50:46 +00:00
|
|
|
result = (result << 8) | next;
|
2019-08-16 21:19:06 +00:00
|
|
|
rolling >>= 8;
|
2019-08-16 17:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (range == 0xffffffff) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return min + result % (range + 1);
|
|
|
|
}
|