/src/libsndfile/ossfuzz/sndfile_fuzz_header.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef SNDFILE_FUZZ_HEADER_H |
2 | | #define SNDFILE_FUZZ_HEADER_H |
3 | | |
4 | | #include <errno.h> |
5 | | |
6 | | typedef struct |
7 | | { |
8 | | sf_count_t offset ; |
9 | | sf_count_t length ; |
10 | | const unsigned char *data ; |
11 | | } VIO_DATA ; |
12 | | |
13 | | static sf_count_t vfget_filelen (void *user_data) |
14 | 22.1k | { VIO_DATA *vf = (VIO_DATA *)user_data ; |
15 | 22.1k | return vf->length ; |
16 | 22.1k | } |
17 | | |
18 | | static sf_count_t vfseek (sf_count_t offset, int whence, void *user_data) |
19 | 411k | { |
20 | 411k | VIO_DATA *vf = (VIO_DATA *)user_data ; |
21 | 411k | sf_count_t new_offset ; |
22 | | |
23 | 411k | switch (whence) |
24 | 411k | { case SEEK_SET : |
25 | 40.6k | new_offset = offset ; |
26 | 40.6k | break ; |
27 | | |
28 | 370k | case SEEK_CUR : |
29 | 370k | new_offset = vf->offset + offset ; |
30 | 370k | break ; |
31 | | |
32 | 0 | case SEEK_END : |
33 | 0 | new_offset = vf->length + offset ; |
34 | 0 | break ; |
35 | | |
36 | 0 | default : |
37 | | // SEEK_DATA and SEEK_HOLE are not supported by this function. |
38 | 0 | errno = EINVAL ; |
39 | 0 | return -1 ; |
40 | 0 | break ; |
41 | 411k | } |
42 | | |
43 | | /* Ensure you can't seek outside the data */ |
44 | 411k | if (new_offset > vf->length) |
45 | 3.24k | { /* Trying to seek past the end of the data */ |
46 | 3.24k | printf("vf overseek: new_offset(%" PRId64 ") > vf->length(%" PRId64 ");" |
47 | 3.24k | " whence(%d), vf->offset(%" PRId64 "), offset(%" PRId64 ")\n", |
48 | 3.24k | new_offset, vf->length, whence, vf->offset, offset) ; |
49 | 3.24k | new_offset = vf->length ; |
50 | 3.24k | } |
51 | 408k | else if (new_offset < 0) |
52 | 254k | { /* Trying to seek before the start of the data */ |
53 | 254k | printf("vf underseek: new_offset(%" PRId64 ") < 0; whence(%d), vf->offset" |
54 | 254k | "(%" PRId64 "), vf->length(%" PRId64 "), offset(%" PRId64 ")\n", |
55 | 254k | new_offset, whence, vf->offset, vf->length, offset) ; |
56 | 254k | new_offset = 0 ; |
57 | 254k | } |
58 | 411k | vf->offset = new_offset ; |
59 | | |
60 | 411k | return vf->offset ; |
61 | 411k | } |
62 | | |
63 | | static sf_count_t vfread (void *ptr, sf_count_t count, void *user_data) |
64 | 16.8M | { VIO_DATA *vf = (VIO_DATA *)user_data ; |
65 | | |
66 | 16.8M | if (vf->offset + count > vf->length) |
67 | 10.0M | count = vf->length - vf->offset ; |
68 | | |
69 | 16.8M | memcpy(ptr, vf->data + vf->offset, count) ; |
70 | 16.8M | vf->offset += count ; |
71 | | |
72 | 16.8M | return count ; |
73 | 16.8M | } |
74 | | |
75 | | static sf_count_t vfwrite (const void *ptr, sf_count_t count, void *user_data) |
76 | 0 | { |
77 | 0 | (void)ptr ; |
78 | 0 | (void)count ; |
79 | 0 | (void)user_data ; |
80 | | |
81 | | // Cannot write to this virtual file. |
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | | static sf_count_t vftell (void *user_data) |
86 | 2.43M | { VIO_DATA *vf = (VIO_DATA *)user_data ; |
87 | | |
88 | 2.43M | return vf->offset ; |
89 | 2.43M | } |
90 | | |
91 | | int sf_init_file(const uint8_t *data, |
92 | | size_t size, |
93 | | SNDFILE **sndfile, |
94 | | VIO_DATA *vio_data, |
95 | | SF_VIRTUAL_IO *vio, SF_INFO *sndfile_info) |
96 | 20.4k | { |
97 | | // Initialize the virtual IO structure. |
98 | 20.4k | vio->get_filelen = vfget_filelen ; |
99 | 20.4k | vio->seek = vfseek ; |
100 | 20.4k | vio->read = vfread ; |
101 | 20.4k | vio->write = vfwrite ; |
102 | 20.4k | vio->tell = vftell ; |
103 | | |
104 | | // Initialize the VIO user data. |
105 | 20.4k | vio_data->data = data ; |
106 | 20.4k | vio_data->length = size ; |
107 | 20.4k | vio_data->offset = 0 ; |
108 | | |
109 | 20.4k | memset(sndfile_info, 0, sizeof(SF_INFO)) ; |
110 | | |
111 | | // Try and open the virtual file. |
112 | 20.4k | *sndfile = sf_open_virtual(vio, SFM_READ, sndfile_info, vio_data) ; |
113 | | |
114 | 20.4k | if (sndfile_info->channels == 0) |
115 | 17.1k | return -1 ; |
116 | | |
117 | 3.27k | if (sndfile_info->channels > 1024 * 1024) |
118 | 0 | return -1 ; |
119 | | |
120 | 3.27k | return 0; |
121 | 3.27k | } |
122 | | |
123 | | #endif |