1 #include <stddef.h>
2 #include <stdio.h>
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(
34     2, SBC_CODEC_FAST_FILTER_BUFFERS)];
35 
36 static int16_t
37     btif_a2dp_sink_pcm_data[15 * SBC_MAX_SAMPLES_PER_FRAME * SBC_MAX_CHANNELS];
38 
LLVMFuzzerInitialize(int argc,char ** argv)39 int LLVMFuzzerInitialize(int argc, char** argv) {
40   (void)argc;
41   (void)argv;
42   OI_CODEC_SBC_DecoderReset(&btif_a2dp_sink_context,
43                             btif_a2dp_sink_context_data,
44                             sizeof(btif_a2dp_sink_context_data), 2, 2, 0);
45 
46   return 0;
47 }
48 
LLVMFuzzerTestOneInput(const uint8_t * buf,size_t len)49 int LLVMFuzzerTestOneInput(const uint8_t* buf, size_t len) {
50   uint32_t pcmBytes, availPcmBytes;
51   int16_t* pcmDataPointer =
52       btif_a2dp_sink_pcm_data; /* Will be overwritten on next packet receipt */
53   availPcmBytes = sizeof(btif_a2dp_sink_pcm_data);
54 
55   pcmBytes = availPcmBytes;
56   OI_CODEC_SBC_DecodeFrame(&btif_a2dp_sink_context, (const OI_BYTE**)&buf,
57                            (uint32_t*)&len, (int16_t*)pcmDataPointer,
58                            (uint32_t*)&pcmBytes);
59 
60   return 0;
61 }
62