1 1.1 christos /* 2 1.1 christos * Copyright (c) Meta Platforms, Inc. and affiliates. 3 1.1 christos * All rights reserved. 4 1.1 christos * 5 1.1 christos * This source code is licensed under both the BSD-style license (found in the 6 1.1 christos * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7 1.1 christos * in the COPYING file in the root directory of this source tree). 8 1.1 christos * You may select, at your option, one of the above-listed licenses. 9 1.1 christos */ 10 1.1 christos 11 1.1 christos /** 12 1.1 christos * Helper APIs for generating random data from input data stream. 13 1.1 christos The producer reads bytes from the end of the input and appends them together 14 1.1 christos to generate a random number in the requested range. If it runs out of input 15 1.1 christos data, it will keep returning the same value (min) over and over again. 16 1.1 christos 17 1.1 christos */ 18 1.1 christos 19 1.1 christos #ifndef FUZZ_DATA_PRODUCER_H 20 1.1 christos #define FUZZ_DATA_PRODUCER_H 21 1.1 christos 22 1.1 christos #include <stddef.h> 23 1.1 christos #include <stdint.h> 24 1.1 christos #include <stdio.h> 25 1.1 christos #include <stdlib.h> 26 1.1 christos 27 1.1 christos 28 1.1 christos /* Struct used for maintaining the state of the data */ 29 1.1 christos typedef struct FUZZ_dataProducer_s FUZZ_dataProducer_t; 30 1.1 christos 31 1.1 christos /* Returns a data producer state struct. Use for producer initialization. */ 32 1.1 christos FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size); 33 1.1 christos 34 1.1 christos /* Frees the data producer */ 35 1.1 christos void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer); 36 1.1 christos 37 1.1 christos /* Returns value between [min, max] */ 38 1.1 christos uint32_t FUZZ_dataProducer_uint32Range(FUZZ_dataProducer_t *producer, uint32_t min, 39 1.1 christos uint32_t max); 40 1.1 christos 41 1.1 christos /* Returns a uint32 value */ 42 1.1 christos uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer); 43 1.1 christos 44 1.1 christos /* Returns a signed value between [min, max] */ 45 1.1 christos int32_t FUZZ_dataProducer_int32Range(FUZZ_dataProducer_t *producer, 46 1.1 christos int32_t min, int32_t max); 47 1.1 christos 48 1.1 christos /* Returns the size of the remaining bytes of data in the producer */ 49 1.1 christos size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer); 50 1.1 christos 51 1.1 christos /* Rolls back the data producer state to have remainingBytes remaining */ 52 1.1 christos void FUZZ_dataProducer_rollBack(FUZZ_dataProducer_t *producer, size_t remainingBytes); 53 1.1 christos 54 1.1 christos /* Returns true if the data producer is out of bytes */ 55 1.1 christos int FUZZ_dataProducer_empty(FUZZ_dataProducer_t *producer); 56 1.1 christos 57 1.1 christos /* Restricts the producer to only the last newSize bytes of data. 58 1.1 christos If newSize > current data size, nothing happens. Returns the number of bytes 59 1.1 christos the producer won't use anymore, after contracting. */ 60 1.1 christos size_t FUZZ_dataProducer_contract(FUZZ_dataProducer_t *producer, size_t newSize); 61 1.1 christos 62 1.1 christos /* Restricts the producer to use only the last X bytes of data, where X is 63 1.1 christos a random number in the interval [0, data_size]. Returns the size of the 64 1.1 christos remaining data the producer won't use anymore (the prefix). */ 65 1.1 christos size_t FUZZ_dataProducer_reserveDataPrefix(FUZZ_dataProducer_t *producer); 66 1.1 christos #endif // FUZZ_DATA_PRODUCER_H 67