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