1 #include <stddef.h>
2 
3 #include "oi_codec_sbc.h"
4 
5 #define CODEC_DATA_WORDS(numChannels, numBuffers)                              \
6   (((sizeof(int32_t) * SBC_MAX_BLOCKS * (numChannels) * SBC_MAX_BANDS) +       \
7     (sizeof(SBC_BUFFER_T) * SBC_MAX_CHANNELS * SBC_MAX_BANDS * (numBuffers)) + \
8     (sizeof(uint32_t) - 1)) /                                                  \
9    sizeof(uint32_t))
10 
11 #define SBC_CODEC_FAST_FILTER_BUFFERS 27
12 
13 #define SBC_MAX_CHANNELS 2
14 #define SBC_MAX_BANDS 8
15 #define SBC_MAX_BLOCKS 16
16 /* Minimum size of the bit allocation pool used to encode the stream */
17 #define SBC_MIN_BITPOOL 2
18 /* Maximum size of the bit allocation pool used to encode the stream */
19 #define SBC_MAX_BITPOOL 250
20 #define SBC_MAX_ONE_CHANNEL_BPS 320000
21 #define SBC_MAX_TWO_CHANNEL_BPS 512000
22 
23 #define SBC_WBS_BITRATE 62000
24 #define SBC_WBS_BITPOOL 27
25 #define SBC_WBS_NROF_BLOCKS 16
26 #define SBC_WBS_FRAME_LEN 62
27 #define SBC_WBS_SAMPLES_PER_FRAME 128
28 
29 #define SBC_HEADER_LEN 4
30 #define SBC_MAX_SAMPLES_PER_FRAME (SBC_MAX_BANDS * SBC_MAX_BLOCKS)
31 
32 static OI_CODEC_SBC_DECODER_CONTEXT btif_a2dp_sink_context;
33 static uint32_t btif_a2dp_sink_context_data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
34 
35 static int16_t btif_a2dp_sink_pcm_data[15 * SBC_MAX_SAMPLES_PER_FRAME * SBC_MAX_CHANNELS];
36 
LLVMFuzzerInitialize(int argc,char ** argv)37 int LLVMFuzzerInitialize(int argc, char** argv) {
38   (void)argc;
39   (void)argv;
40   OI_CODEC_SBC_DecoderReset(&btif_a2dp_sink_context, btif_a2dp_sink_context_data,
41                             sizeof(btif_a2dp_sink_context_data), 2, 2, 0);
42 
43   return 0;
44 }
45 
LLVMFuzzerTestOneInput(const uint8_t * buf,size_t len)46 int LLVMFuzzerTestOneInput(const uint8_t* buf, size_t len) {
47   uint32_t pcmBytes, availPcmBytes;
48   int16_t* pcmDataPointer =
49           btif_a2dp_sink_pcm_data; /* Will be overwritten on next packet receipt */
50   availPcmBytes = sizeof(btif_a2dp_sink_pcm_data);
51 
52   pcmBytes = availPcmBytes;
53   OI_CODEC_SBC_DecodeFrame(&btif_a2dp_sink_context, (const OI_BYTE**)&buf, (uint32_t*)&len,
54                            (int16_t*)pcmDataPointer, (uint32_t*)&pcmBytes);
55 
56   return 0;
57 }
58