2019-09-09 15:43:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fuzz_data_producer.h"
|
|
|
|
|
|
|
|
struct FUZZ_dataProducer_s{
|
|
|
|
const uint8_t *data;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size) {
|
|
|
|
FUZZ_dataProducer_t *producer = malloc(sizeof(FUZZ_dataProducer_t));
|
|
|
|
|
|
|
|
FUZZ_ASSERT(producer != NULL);
|
|
|
|
|
|
|
|
producer->data = data;
|
|
|
|
producer->size = size;
|
|
|
|
return producer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer) { free(producer); }
|
|
|
|
|
|
|
|
uint32_t FUZZ_dataProducer_uint32Range(FUZZ_dataProducer_t *producer, uint32_t min,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range == 0xffffffff) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return min + result % (range + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer) {
|
2019-09-10 23:14:43 +00:00
|
|
|
return FUZZ_dataProducer_uint32Range(producer, 0, 0xffffffff);
|
2019-09-09 15:43:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer){
|
|
|
|
return producer->size;
|
|
|
|
}
|
2019-09-10 23:14:43 +00:00
|
|
|
|
|
|
|
size_t FUZZ_dataProducer_contract(FUZZ_dataProducer_t *producer, size_t newSize)
|
|
|
|
{
|
|
|
|
newSize = newSize > producer->size ? producer->size : newSize;
|
|
|
|
|
|
|
|
size_t remaining = producer->size - newSize;
|
|
|
|
producer->data = producer->data + remaining;
|
|
|
|
producer->size = newSize;
|
|
|
|
return remaining;
|
|
|
|
}
|
2019-09-11 17:09:29 +00:00
|
|
|
|
|
|
|
size_t FUZZ_dataProducer_reserveDataPrefix(FUZZ_dataProducer_t *producer)
|
|
|
|
{
|
|
|
|
size_t producerSliceSize = FUZZ_dataProducer_uint32Range(
|
|
|
|
producer, 0, producer->size);
|
|
|
|
return FUZZ_dataProducer_contract(producer, producerSliceSize);
|
|
|
|
}
|