1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef A2DP_CODEC_HELPERFUNCTIONS_H_
18 #define A2DP_CODEC_HELPERFUNCTIONS_H_
19 
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #include <vector>
23 
24 #include "stack/include/bt_hdr.h"
25 
26 static const std::vector<const btav_a2dp_codec_index_t> CODEC_INDEX_ENUM_VALS =
27     {BTAV_A2DP_CODEC_INDEX_SOURCE_MIN,
28      BTAV_A2DP_CODEC_INDEX_SOURCE_SBC,
29      BTAV_A2DP_CODEC_INDEX_SOURCE_AAC,
30      BTAV_A2DP_CODEC_INDEX_SOURCE_APTX,
31      BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD,
32      BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC,
33      BTAV_A2DP_CODEC_INDEX_SOURCE_MAX,
34      BTAV_A2DP_CODEC_INDEX_SINK_MIN,
35      BTAV_A2DP_CODEC_INDEX_SINK_SBC,
36      BTAV_A2DP_CODEC_INDEX_SINK_AAC,
37      BTAV_A2DP_CODEC_INDEX_SINK_LDAC,
38      BTAV_A2DP_CODEC_INDEX_SINK_MAX,
39      BTAV_A2DP_CODEC_INDEX_MIN,
40      BTAV_A2DP_CODEC_INDEX_MAX};
41 
42 static const std::vector<const btav_a2dp_codec_priority_t>
43     CODEC_PRIORITY_ENUM_VALS = {BTAV_A2DP_CODEC_PRIORITY_DISABLED,
44                                 BTAV_A2DP_CODEC_PRIORITY_DEFAULT,
45                                 BTAV_A2DP_CODEC_PRIORITY_HIGHEST};
46 
47 static const std::vector<const btav_a2dp_codec_sample_rate_t>
48     CODEC_SAMPLERATE_ENUM_VALS = {
49         BTAV_A2DP_CODEC_SAMPLE_RATE_NONE,   BTAV_A2DP_CODEC_SAMPLE_RATE_44100,
50         BTAV_A2DP_CODEC_SAMPLE_RATE_48000,  BTAV_A2DP_CODEC_SAMPLE_RATE_88200,
51         BTAV_A2DP_CODEC_SAMPLE_RATE_96000,  BTAV_A2DP_CODEC_SAMPLE_RATE_176400,
52         BTAV_A2DP_CODEC_SAMPLE_RATE_192000, BTAV_A2DP_CODEC_SAMPLE_RATE_16000,
53         BTAV_A2DP_CODEC_SAMPLE_RATE_24000};
54 
55 static const std::vector<const btav_a2dp_codec_bits_per_sample_t>
56     CODEC_BPS_ENUM_VALS = {BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE,
57                            BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16,
58                            BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24,
59                            BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32};
60 
61 static const std::vector<const btav_a2dp_codec_channel_mode_t>
62     CODEC_CHANNELMODE_ENUM_VALS = {BTAV_A2DP_CODEC_CHANNEL_MODE_NONE,
63                                    BTAV_A2DP_CODEC_CHANNEL_MODE_MONO,
64                                    BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO};
65 
66 // Construct a btav_a2dp_codec_index_t object
getArbitraryBtavCodecIndex(FuzzedDataProvider * fdp)67 btav_a2dp_codec_index_t getArbitraryBtavCodecIndex(FuzzedDataProvider* fdp) {
68   return CODEC_INDEX_ENUM_VALS.at(
69       fdp->ConsumeIntegralInRange<size_t>(0, CODEC_INDEX_ENUM_VALS.size() - 1));
70 }
71 
72 // Construct a btav_a2dp_codec_priority_t object
getArbitraryBtavCodecPriority(FuzzedDataProvider * fdp)73 btav_a2dp_codec_priority_t getArbitraryBtavCodecPriority(
74     FuzzedDataProvider* fdp) {
75   return CODEC_PRIORITY_ENUM_VALS.at(fdp->ConsumeIntegralInRange<size_t>(
76       0, CODEC_PRIORITY_ENUM_VALS.size() - 1));
77 }
78 // Construct a btav_a2dp_codec_sample_rate_t object
getArbitraryBtavCodecSampleRate(FuzzedDataProvider * fdp)79 btav_a2dp_codec_sample_rate_t getArbitraryBtavCodecSampleRate(
80     FuzzedDataProvider* fdp) {
81   return CODEC_SAMPLERATE_ENUM_VALS.at(fdp->ConsumeIntegralInRange<size_t>(
82       0, CODEC_SAMPLERATE_ENUM_VALS.size() - 1));
83 }
84 // Construct a btav_a2dp_codec_bits_per_sample_t object
getArbitraryBtavCodecBitsPerSample(FuzzedDataProvider * fdp)85 btav_a2dp_codec_bits_per_sample_t getArbitraryBtavCodecBitsPerSample(
86     FuzzedDataProvider* fdp) {
87   return CODEC_BPS_ENUM_VALS.at(
88       fdp->ConsumeIntegralInRange<size_t>(0, CODEC_BPS_ENUM_VALS.size() - 1));
89 }
90 // Construct a btav_a2dp_codec_channel_mode_t object
getArbitraryBtavCodecChannelMode(FuzzedDataProvider * fdp)91 btav_a2dp_codec_channel_mode_t getArbitraryBtavCodecChannelMode(
92     FuzzedDataProvider* fdp) {
93   return CODEC_CHANNELMODE_ENUM_VALS.at(fdp->ConsumeIntegralInRange<size_t>(
94       0, CODEC_CHANNELMODE_ENUM_VALS.size() - 1));
95 }
96 // Construct a btav_a2dp_codec_config_t object
getArbitraryBtavCodecConfig(FuzzedDataProvider * fdp)97 btav_a2dp_codec_config_t getArbitraryBtavCodecConfig(FuzzedDataProvider* fdp) {
98   btav_a2dp_codec_config_t config;
99 
100   config.codec_type = getArbitraryBtavCodecIndex(fdp);
101   config.codec_priority = getArbitraryBtavCodecPriority(fdp);
102   config.sample_rate = getArbitraryBtavCodecSampleRate(fdp);
103   config.bits_per_sample = getArbitraryBtavCodecBitsPerSample(fdp);
104   config.channel_mode = getArbitraryBtavCodecChannelMode(fdp);
105   config.codec_specific_1 = fdp->ConsumeIntegral<int64_t>();
106   config.codec_specific_2 = fdp->ConsumeIntegral<int64_t>();
107   config.codec_specific_3 = fdp->ConsumeIntegral<int64_t>();
108   config.codec_specific_4 = fdp->ConsumeIntegral<int64_t>();
109 
110   return config;
111 }
112 
getArbitraryA2dpEncoderInitPeerParams(FuzzedDataProvider * fdp)113 tA2DP_ENCODER_INIT_PEER_PARAMS getArbitraryA2dpEncoderInitPeerParams(
114     FuzzedDataProvider* fdp) {
115   tA2DP_ENCODER_INIT_PEER_PARAMS params;
116 
117   params.is_peer_edr = fdp->ConsumeBool();
118   params.peer_supports_3mbps = fdp->ConsumeBool();
119   params.peer_mtu = fdp->ConsumeIntegral<uint16_t>();
120 
121   return params;
122 }
123 
124 #define MAX_BTHDR_SIZE 1024
getArbitraryBtHdr(FuzzedDataProvider * fdp)125 std::shared_ptr<BT_HDR> getArbitraryBtHdr(FuzzedDataProvider* fdp) {
126   // Build a data buffer
127   size_t buf_size = fdp->ConsumeIntegralInRange<size_t>(0, MAX_BTHDR_SIZE);
128   std::vector<uint8_t> bytes = fdp->ConsumeBytes<uint8_t>(buf_size);
129 
130   if (bytes.empty()) {
131     return nullptr;
132   }
133 
134   uint16_t hdr_size = bytes.size() + sizeof(BT_HDR);
135   std::shared_ptr<BT_HDR> bt_hdr(reinterpret_cast<BT_HDR*>(calloc(1, hdr_size)),
136                                  free);
137 
138   bt_hdr->event = fdp->ConsumeIntegral<uint16_t>();
139   bt_hdr->len = bytes.size();
140   bt_hdr->offset =
141       fdp->ConsumeIntegralInRange<uint16_t>(0, hdr_size - sizeof(BT_HDR));
142   bt_hdr->layer_specific = fdp->ConsumeIntegral<uint16_t>();
143   std::copy(bytes.begin(), bytes.end(), bt_hdr->data);
144 
145   return bt_hdr;
146 }
147 
148 #include "bta/av/bta_av_int.h"
generateArbitrarytA2dpOffload(FuzzedDataProvider * fdp)149 tBT_A2DP_OFFLOAD generateArbitrarytA2dpOffload(FuzzedDataProvider* fdp) {
150   tBT_A2DP_OFFLOAD retval;
151 
152   retval.codec_type = fdp->ConsumeIntegral<uint32_t>();
153   retval.max_latency = fdp->ConsumeIntegral<uint16_t>();
154   std::vector<uint8_t> scms_t_enable = fdp->ConsumeBytes<uint8_t>(2);
155   memcpy(&retval.scms_t_enable[0], scms_t_enable.data(), scms_t_enable.size());
156   retval.sample_rate = fdp->ConsumeIntegral<uint32_t>();
157   retval.bits_per_sample = fdp->ConsumeIntegral<uint8_t>();
158   retval.ch_mode = fdp->ConsumeIntegral<uint8_t>();
159   retval.encoded_audio_bitrate = fdp->ConsumeIntegral<uint32_t>();
160   retval.acl_hdl = fdp->ConsumeIntegral<uint16_t>();
161   retval.l2c_rcid = fdp->ConsumeIntegral<uint16_t>();
162   retval.mtu = fdp->ConsumeIntegral<uint16_t>();
163 
164   std::vector<uint8_t> codec_info_bytes = fdp->ConsumeBytes<uint8_t>(32);
165   memcpy(&retval.codec_info[0], codec_info_bytes.data(),
166          codec_info_bytes.size());
167 
168   return retval;
169 }
170 
171 #endif  // A2DP_CODEC_HELPERFUNCTIONS_H_
172