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