1 /* Copyright (c) 2017 Google Inc. */
2 /*
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met:
6
7 - Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 - Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
18 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "opus.h"
35 #include "opus_types.h"
36
37 #define MAX_FRAME_SAMP 5760
38 #define MAX_PACKET 1500
39
40 /* 4 bytes: packet length, 4 bytes: encoder final range */
41 #define SETUP_BYTE_COUNT 8
42
43 #define MAX_DECODES 12
44
45 typedef struct {
46 int fs;
47 int channels;
48 } TocInfo;
49
ParseToc(const uint8_t * toc,TocInfo * const info)50 static void ParseToc(const uint8_t *toc, TocInfo *const info) {
51 const int samp_freqs[5] = {8000, 12000, 16000, 24000, 48000};
52 const int bandwidth = opus_packet_get_bandwidth(toc);
53
54 info->fs = samp_freqs[bandwidth - OPUS_BANDWIDTH_NARROWBAND];
55 info->channels = opus_packet_get_nb_channels(toc);
56 }
57
58 /* Treats the input data as concatenated packets encoded by opus_demo,
59 * structured as
60 * bytes 0..3: packet length
61 * bytes 4..7: encoder final range
62 * bytes 8+ : Opus packet, including ToC
63 */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
65 OpusDecoder *dec;
66 opus_int16 *pcm;
67 uint8_t *temp_data;
68 TocInfo toc;
69 int i = 0;
70 int err = OPUS_OK;
71 int num_decodes = 0;
72
73 /* Not enough data to setup the decoder (+1 for the ToC) */
74 if (size < SETUP_BYTE_COUNT + 1) {
75 return 0;
76 }
77
78 /* Create decoder based on info from the first ToC available */
79 ParseToc(&data[SETUP_BYTE_COUNT], &toc);
80
81 dec = opus_decoder_create(toc.fs, toc.channels, &err);
82 if (err != OPUS_OK || dec == NULL) {
83 return 0;
84 }
85
86 pcm = (opus_int16*) malloc(sizeof(*pcm) * MAX_FRAME_SAMP * toc.channels);
87
88 while (i + SETUP_BYTE_COUNT < size && num_decodes++ < MAX_DECODES) {
89 int len, fec;
90
91 len = (opus_uint32) data[i ] << 24 |
92 (opus_uint32) data[i + 1] << 16 |
93 (opus_uint32) data[i + 2] << 8 |
94 (opus_uint32) data[i + 3];
95 if (len > MAX_PACKET || len < 0 || i + SETUP_BYTE_COUNT + len > size) {
96 break;
97 }
98
99 /* Bytes 4..7 represent encoder final range, but are unused here.
100 * Instead, byte 4 is repurposed to determine if FEC is used. */
101 fec = data[i + 4] & 1;
102
103 if (len == 0) {
104 /* Lost packet */
105 int frame_size;
106 opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&frame_size));
107 (void) opus_decode(dec, NULL, len, pcm, frame_size, fec);
108 } else {
109 temp_data = (uint8_t*) malloc(len);
110 memcpy(temp_data, &data[i + SETUP_BYTE_COUNT], len);
111
112 (void) opus_decode(dec, temp_data, len, pcm, MAX_FRAME_SAMP, fec);
113
114 free(temp_data);
115 }
116
117 i += SETUP_BYTE_COUNT + len;
118 }
119
120 opus_decoder_destroy(dec);
121 free(pcm);
122
123 return 0;
124 }
125