fuzz.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * fuzz.c
  3. * ------
  4. * Purpose: Tiny libopenmpt user to be used by fuzzing tools
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include <memory.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <libopenmpt/libopenmpt.h>
  18. #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
  19. #define BUFFERSIZE 450 // shouldn't match OpenMPT's internal mix buffer size (512)
  20. #define SAMPLERATE 22050
  21. static int16_t buffer[BUFFERSIZE];
  22. int main( int argc, char * argv[] ) {
  23. static FILE * file = NULL;
  24. static openmpt_module * mod = NULL;
  25. static size_t count = 0;
  26. static int i = 0;
  27. (void)argc;
  28. #ifdef __AFL_HAVE_MANUAL_CONTROL
  29. __AFL_INIT();
  30. #endif
  31. file = fopen( argv[1], "rb" );
  32. mod = openmpt_module_create( openmpt_stream_get_file_callbacks(), file, NULL, NULL, NULL );
  33. fclose( file );
  34. if ( mod == NULL ) return 1;
  35. openmpt_module_ctl_set( mod, "render.resampler.emulate_amiga", (openmpt_module_get_num_orders( mod ) & 1) ? "0" : "1" );
  36. /* render about a second of the module for fuzzing the actual mix routines */
  37. for(; i < 50; i++) {
  38. count = openmpt_module_read_mono( mod, SAMPLERATE, BUFFERSIZE, buffer );
  39. if ( count == 0 ) {
  40. break;
  41. }
  42. }
  43. openmpt_module_set_position_seconds( mod, 1.0 );
  44. openmpt_module_read_mono( mod, SAMPLERATE, BUFFERSIZE, buffer );
  45. openmpt_module_set_position_order_row( mod, 3, 16 );
  46. openmpt_module_read_mono( mod, SAMPLERATE, BUFFERSIZE, buffer );
  47. /* fuzz string-related stuff */
  48. openmpt_free_string ( openmpt_module_get_metadata( mod, "date" ) );
  49. openmpt_free_string ( openmpt_module_get_metadata( mod, "message" ) );
  50. openmpt_module_destroy( mod );
  51. return 0;
  52. }