File: sndfile_fuzz_header.h

package info (click to toggle)
libsndfile 1.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,108 kB
  • sloc: ansic: 55,350; cpp: 1,108; python: 791; makefile: 545; sh: 539; cs: 122
file content (119 lines) | stat: -rw-r--r-- 2,890 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef SNDFILE_FUZZ_HEADER_H
#define SNDFILE_FUZZ_HEADER_H

typedef struct
{
  sf_count_t offset ;
  sf_count_t length ;
  const unsigned char *data ;
} VIO_DATA ;

static sf_count_t vfget_filelen (void *user_data)
{  VIO_DATA *vf = (VIO_DATA *)user_data ;
   return vf->length ;
}

static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data)
{
  VIO_DATA *vf = (VIO_DATA *)user_data ;
  sf_count_t new_offset ;

  switch (whence)
  {   case SEEK_SET :
        new_offset = offset ;
        break ;

    case SEEK_CUR :
        new_offset = vf->offset + offset ;
        break ;

    case SEEK_END :
        new_offset = vf->length + offset ;
        break ;

    default :
        break ;
  }

  /* Ensure you can't seek outside the data */
  if (new_offset > vf->length)
  {  /* Trying to seek past the end of the data */
     printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");"
            "  whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n",
            new_offset, vf->length, whence, vf->offset, offset) ;
     new_offset = vf->length ;
  }
  else if (new_offset < 0)
  {  /* Trying to seek before the start of the data */
     printf("vf underseek: new_offset(%" PRId64 ") < 0;  whence(%d), vf->offset"
            "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n",
            new_offset, whence, vf->offset, vf->length, offset) ;
     new_offset = 0 ;
  }
  vf->offset = new_offset ;

  return vf->offset ;
}

static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data)
{  VIO_DATA *vf = (VIO_DATA *)user_data ;

   if (vf->offset + count > vf->length)
     count = vf->length - vf->offset ;

   memcpy(ptr, vf->data + vf->offset, count) ;
   vf->offset += count ;

   return count ;
}

static sf_count_t vfwrite (const void *ptr, sf_count_t count, void *user_data)
{
  (void)ptr ;
  (void)count ;
  (void)user_data ;

  // Cannot write to this virtual file.
  return 0;
}

static sf_count_t vftell (void *user_data)
{ VIO_DATA *vf = (VIO_DATA *)user_data ;

  return vf->offset ;
}

int sf_init_file(const uint8_t *data, 
                size_t size, 
                SNDFILE **sndfile, 
                VIO_DATA *vio_data, 
                SF_VIRTUAL_IO *vio, SF_INFO *sndfile_info)
{  float* read_buffer = NULL ;

   // Initialize the virtual IO structure.
   vio->get_filelen = vfget_filelen ;
   vio->seek = vfseek ;
   vio->read = vfread ;
   vio->write = vfwrite ;
   vio->tell = vftell ;

   // Initialize the VIO user data.
   vio_data->data = data ;
   vio_data->length = size ;
   vio_data->offset = 0 ;

   memset(sndfile_info, 0, sizeof(SF_INFO)) ;

   // Try and open the virtual file.
   *sndfile = sf_open_virtual(vio, SFM_READ, sndfile_info, vio_data) ;

   if (sndfile_info->channels == 0)
		 return -1 ;

   if (sndfile_info->channels > 1024 * 1024)
		 return -1 ;

   return 0;
}

#endif