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