fuzz_data_producer.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) Facebook, Inc.
  3. * All rights reserved.
  4. *
  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.
  9. */
  10. /**
  11. * Helper APIs for generating random data from input data stream.
  12. The producer reads bytes from the end of the input and appends them together
  13. to generate a random number in the requested range. If it runs out of input
  14. data, it will keep returning the same value (min) over and over again.
  15. */
  16. #ifndef FUZZ_DATA_PRODUCER_H
  17. #define FUZZ_DATA_PRODUCER_H
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "fuzz_helpers.h"
  23. /* Struct used for maintaining the state of the data */
  24. typedef struct FUZZ_dataProducer_s FUZZ_dataProducer_t;
  25. /* Returns a data producer state struct. Use for producer initialization. */
  26. FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size);
  27. /* Frees the data producer */
  28. void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer);
  29. /* Returns value between [min, max] */
  30. uint32_t FUZZ_dataProducer_uint32Range(FUZZ_dataProducer_t *producer, uint32_t min,
  31. uint32_t max);
  32. /* Returns a uint32 value */
  33. uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer);
  34. /* Returns a signed value between [min, max] */
  35. int32_t FUZZ_dataProducer_int32Range(FUZZ_dataProducer_t *producer,
  36. int32_t min, int32_t max);
  37. /* Returns the size of the remaining bytes of data in the producer */
  38. size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer);
  39. /* Rolls back the data producer state to have remainingBytes remaining */
  40. void FUZZ_dataProducer_rollBack(FUZZ_dataProducer_t *producer, size_t remainingBytes);
  41. /* Returns true if the data producer is out of bytes */
  42. int FUZZ_dataProducer_empty(FUZZ_dataProducer_t *producer);
  43. /* Restricts the producer to only the last newSize bytes of data.
  44. If newSize > current data size, nothing happens. Returns the number of bytes
  45. the producer won't use anymore, after contracting. */
  46. size_t FUZZ_dataProducer_contract(FUZZ_dataProducer_t *producer, size_t newSize);
  47. /* Restricts the producer to use only the last X bytes of data, where X is
  48. a random number in the interval [0, data_size]. Returns the size of the
  49. remaining data the producer won't use anymore (the prefix). */
  50. size_t FUZZ_dataProducer_reserveDataPrefix(FUZZ_dataProducer_t *producer);
  51. #endif // FUZZ_DATA_PRODUCER_H