sndfile_fuzz_header.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef SNDFILE_FUZZ_HEADER_H
  2. #define SNDFILE_FUZZ_HEADER_H
  3. typedef struct
  4. {
  5. sf_count_t offset ;
  6. sf_count_t length ;
  7. const unsigned char *data ;
  8. } VIO_DATA ;
  9. static sf_count_t vfget_filelen (void *user_data)
  10. { VIO_DATA *vf = (VIO_DATA *)user_data ;
  11. return vf->length ;
  12. }
  13. static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data)
  14. {
  15. VIO_DATA *vf = (VIO_DATA *)user_data ;
  16. sf_count_t new_offset ;
  17. switch (whence)
  18. { case SEEK_SET :
  19. new_offset = offset ;
  20. break ;
  21. case SEEK_CUR :
  22. new_offset = vf->offset + offset ;
  23. break ;
  24. case SEEK_END :
  25. new_offset = vf->length + offset ;
  26. break ;
  27. default :
  28. break ;
  29. }
  30. /* Ensure you can't seek outside the data */
  31. if (new_offset > vf->length)
  32. { /* Trying to seek past the end of the data */
  33. printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
  34. " whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
  35. new_offset, vf->length, whence, vf->offset, offset) ;
  36. new_offset = vf->length ;
  37. }
  38. else if (new_offset < 0)
  39. { /* Trying to seek before the start of the data */
  40. printf("vf underseek: new_offset(%" PRId64 ") < 0; whence(%d), vf->offset"
  41. "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
  42. new_offset, whence, vf->offset, vf->length, offset) ;
  43. new_offset = 0 ;
  44. }
  45. vf->offset = new_offset ;
  46. return vf->offset ;
  47. }
  48. static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data)
  49. { VIO_DATA *vf = (VIO_DATA *)user_data ;
  50. if (vf->offset + count > vf->length)
  51. count = vf->length - vf->offset ;
  52. memcpy(ptr, vf->data + vf->offset, count) ;
  53. vf->offset += count ;
  54. return count ;
  55. }
  56. static sf_count_t vfwrite (const void *ptr, sf_count_t count, void *user_data)
  57. {
  58. (void)ptr ;
  59. (void)count ;
  60. (void)user_data ;
  61. // Cannot write to this virtual file.
  62. return 0;
  63. }
  64. static sf_count_t vftell (void *user_data)
  65. { VIO_DATA *vf = (VIO_DATA *)user_data ;
  66. return vf->offset ;
  67. }
  68. int sf_init_file(const uint8_t *data,
  69. size_t size,
  70. SNDFILE **sndfile,
  71. VIO_DATA *vio_data,
  72. SF_VIRTUAL_IO *vio, SF_INFO *sndfile_info)
  73. { float* read_buffer = NULL ;
  74. // Initialize the virtual IO structure.
  75. vio->get_filelen = vfget_filelen ;
  76. vio->seek = vfseek ;
  77. vio->read = vfread ;
  78. vio->write = vfwrite ;
  79. vio->tell = vftell ;
  80. // Initialize the VIO user data.
  81. vio_data->data = data ;
  82. vio_data->length = size ;
  83. vio_data->offset = 0 ;
  84. memset(sndfile_info, 0, sizeof(SF_INFO)) ;
  85. // Try and open the virtual file.
  86. *sndfile = sf_open_virtual(vio, SFM_READ, sndfile_info, vio_data) ;
  87. if (sndfile_info->channels == 0)
  88. return -1 ;
  89. if (sndfile_info->channels > 1024 * 1024)
  90. return -1 ;
  91. return 0;
  92. }
  93. #endif