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 #include <vector>
22 
23 // =============================================================================
24 static const std::vector<const btav_a2dp_codec_index_t> CODEC_INDEX_ENUM_VALS =
25     {BTAV_A2DP_CODEC_INDEX_SOURCE_MIN,
26      BTAV_A2DP_CODEC_INDEX_SOURCE_SBC,
27      BTAV_A2DP_CODEC_INDEX_SOURCE_AAC,
28      BTAV_A2DP_CODEC_INDEX_SOURCE_APTX,
29      BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD,
30      BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC,
31      BTAV_A2DP_CODEC_INDEX_SOURCE_MAX,
32      BTAV_A2DP_CODEC_INDEX_SINK_MIN,
33      BTAV_A2DP_CODEC_INDEX_SINK_SBC,
34      BTAV_A2DP_CODEC_INDEX_SINK_AAC,
35      BTAV_A2DP_CODEC_INDEX_SINK_LDAC,
36      BTAV_A2DP_CODEC_INDEX_SINK_MAX,
37      BTAV_A2DP_CODEC_INDEX_MIN,
38      BTAV_A2DP_CODEC_INDEX_MAX};
39 
40 static const std::vector<const btav_a2dp_codec_priority_t>
41     CODEC_PRIORITY_ENUM_VALS = {BTAV_A2DP_CODEC_PRIORITY_DISABLED,
42                                 BTAV_A2DP_CODEC_PRIORITY_DEFAULT,
43                                 BTAV_A2DP_CODEC_PRIORITY_HIGHEST};
44 
45 static const std::vector<const btav_a2dp_codec_sample_rate_t>
46     CODEC_SAMPLERATE_ENUM_VALS = {
47         BTAV_A2DP_CODEC_SAMPLE_RATE_NONE,   BTAV_A2DP_CODEC_SAMPLE_RATE_44100,
48         BTAV_A2DP_CODEC_SAMPLE_RATE_48000,  BTAV_A2DP_CODEC_SAMPLE_RATE_88200,
49         BTAV_A2DP_CODEC_SAMPLE_RATE_96000,  BTAV_A2DP_CODEC_SAMPLE_RATE_176400,
50         BTAV_A2DP_CODEC_SAMPLE_RATE_192000, BTAV_A2DP_CODEC_SAMPLE_RATE_16000,
51         BTAV_A2DP_CODEC_SAMPLE_RATE_24000};
52 
53 static const std::vector<const btav_a2dp_codec_bits_per_sample_t>
54     CODEC_BPS_ENUM_VALS = {BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE,
55                            BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16,
56                            BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24,
57                            BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32};
58 
59 static const std::vector<const btav_a2dp_codec_channel_mode_t>
60     CODEC_CHANNELMODE_ENUM_VALS = {BTAV_A2DP_CODEC_CHANNEL_MODE_NONE,
61                                    BTAV_A2DP_CODEC_CHANNEL_MODE_MONO,
62                                    BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO};
63 
64 // Construct a btav_a2dp_codec_index_t object
getArbitraryBtavCodecIndex(FuzzedDataProvider * fdp)65 btav_a2dp_codec_index_t getArbitraryBtavCodecIndex(FuzzedDataProvider* fdp) {
66   return CODEC_INDEX_ENUM_VALS.at(
67       fdp->ConsumeIntegralInRange<size_t>(0, CODEC_INDEX_ENUM_VALS.size() - 1));
68 }
69 
70 // Construct a btav_a2dp_codec_priority_t object
getArbitraryBtavCodecPriority(FuzzedDataProvider * fdp)71 btav_a2dp_codec_priority_t getArbitraryBtavCodecPriority(
72     FuzzedDataProvider* fdp) {
73   return CODEC_PRIORITY_ENUM_VALS.at(fdp->ConsumeIntegralInRange<size_t>(
74       0, CODEC_PRIORITY_ENUM_VALS.size() - 1));
75 }
76 // Construct a btav_a2dp_codec_sample_rate_t object
getArbitraryBtavCodecSampleRate(FuzzedDataProvider * fdp)77 btav_a2dp_codec_sample_rate_t getArbitraryBtavCodecSampleRate(
78     FuzzedDataProvider* fdp) {
79   return CODEC_SAMPLERATE_ENUM_VALS.at(fdp->ConsumeIntegralInRange<size_t>(
80       0, CODEC_SAMPLERATE_ENUM_VALS.size() - 1));
81 }
82 // Construct a btav_a2dp_codec_bits_per_sample_t object
getArbitraryBtavCodecBitsPerSample(FuzzedDataProvider * fdp)83 btav_a2dp_codec_bits_per_sample_t getArbitraryBtavCodecBitsPerSample(
84     FuzzedDataProvider* fdp) {
85   return CODEC_BPS_ENUM_VALS.at(
86       fdp->ConsumeIntegralInRange<size_t>(0, CODEC_BPS_ENUM_VALS.size() - 1));
87 }
88 // Construct a btav_a2dp_codec_channel_mode_t object
getArbitraryBtavCodecChannelMode(FuzzedDataProvider * fdp)89 btav_a2dp_codec_channel_mode_t getArbitraryBtavCodecChannelMode(
90     FuzzedDataProvider* fdp) {
91   return CODEC_CHANNELMODE_ENUM_VALS.at(fdp->ConsumeIntegralInRange<size_t>(
92       0, CODEC_CHANNELMODE_ENUM_VALS.size() - 1));
93 }
94 // Construct a btav_a2dp_codec_config_t object
getArbitraryBtavCodecConfig(FuzzedDataProvider * fdp)95 btav_a2dp_codec_config_t getArbitraryBtavCodecConfig(FuzzedDataProvider* fdp) {
96   btav_a2dp_codec_config_t config;
97 
98   config.codec_type = getArbitraryBtavCodecIndex(fdp);
99   config.codec_priority = getArbitraryBtavCodecPriority(fdp);
100   config.sample_rate = getArbitraryBtavCodecSampleRate(fdp);
101   config.bits_per_sample = getArbitraryBtavCodecBitsPerSample(fdp);
102   config.channel_mode = getArbitraryBtavCodecChannelMode(fdp);
103   config.codec_specific_1 = fdp->ConsumeIntegral<int64_t>();
104   config.codec_specific_2 = fdp->ConsumeIntegral<int64_t>();
105   config.codec_specific_3 = fdp->ConsumeIntegral<int64_t>();
106   config.codec_specific_4 = fdp->ConsumeIntegral<int64_t>();
107 
108   return config;
109 }
110 // =============================================================================
getArbitraryA2dpEncoderInitPeerParams(FuzzedDataProvider * fdp)111 tA2DP_ENCODER_INIT_PEER_PARAMS getArbitraryA2dpEncoderInitPeerParams(
112     FuzzedDataProvider* fdp) {
113   tA2DP_ENCODER_INIT_PEER_PARAMS params;
114 
115   params.is_peer_edr = fdp->ConsumeBool();
116   params.peer_supports_3mbps = fdp->ConsumeBool();
117   params.peer_mtu = fdp->ConsumeIntegral<uint16_t>();
118 
119   return params;
120 }
121 // =============================================================================
122 #include "bt_types.h"
123 #define MAX_BTHDR_SIZE 1024
getArbitraryBtHdr(FuzzedDataProvider * fdp)124 std::shared_ptr<BT_HDR> getArbitraryBtHdr(FuzzedDataProvider* fdp) {
125   // Build a data buffer
126   size_t buf_size = fdp->ConsumeIntegralInRange<size_t>(0, MAX_BTHDR_SIZE);
127   std::vector<uint8_t> bytes = fdp->ConsumeBytes<uint8_t>(buf_size);
128 
129   if (bytes.empty()) {
130     return nullptr;
131   }
132 
133   uint16_t hdr_size = bytes.size() + sizeof(BT_HDR);
134   std::shared_ptr<BT_HDR> bt_hdr(
135       reinterpret_cast<BT_HDR*>(calloc(hdr_size, sizeof(uint8_t))), free);
136 
137   bt_hdr->event = fdp->ConsumeIntegral<uint16_t>();
138   bt_hdr->len = bytes.size();
139   bt_hdr->offset =
140       fdp->ConsumeIntegralInRange<uint16_t>(0, hdr_size - sizeof(BT_HDR));
141   bt_hdr->layer_specific = fdp->ConsumeIntegral<uint16_t>();
142   std::copy(bytes.begin(), bytes.end(), bt_hdr->data);
143 
144   return bt_hdr;
145 }
146 // =============================================================================
147 #include "bta/av/bta_av_int.h"
generateArbitrarytA2dpOffload(FuzzedDataProvider * fdp)148 tBT_A2DP_OFFLOAD generateArbitrarytA2dpOffload(FuzzedDataProvider* fdp) {
149   tBT_A2DP_OFFLOAD retval;
150 
151   retval.codec_type = fdp->ConsumeIntegral<uint32_t>();
152   retval.max_latency = fdp->ConsumeIntegral<uint16_t>();
153   std::vector<uint8_t> scms_t_enable = fdp->ConsumeBytes<uint8_t>(2);
154   memcpy(&retval.scms_t_enable[0], scms_t_enable.data(), scms_t_enable.size());
155   retval.sample_rate = fdp->ConsumeIntegral<uint32_t>();
156   retval.bits_per_sample = fdp->ConsumeIntegral<uint8_t>();
157   retval.ch_mode = fdp->ConsumeIntegral<uint8_t>();
158   retval.encoded_audio_bitrate = fdp->ConsumeIntegral<uint32_t>();
159   retval.acl_hdl = fdp->ConsumeIntegral<uint16_t>();
160   retval.l2c_rcid = fdp->ConsumeIntegral<uint16_t>();
161   retval.mtu = fdp->ConsumeIntegral<uint16_t>();
162 
163   std::vector<uint8_t> codec_info_bytes = fdp->ConsumeBytes<uint8_t>(32);
164   memcpy(&retval.codec_info[0], codec_info_bytes.data(),
165          codec_info_bytes.size());
166 
167   return retval;
168 }
169 // =============================================================================
170 
171 #endif  // A2DP_CODEC_HELPERFUNCTIONS_H_
172