1 /******************************************************************************
2 *
3 * Copyright 2022 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <lc3_cpp.h>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 using namespace lc3;
23
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)24 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
25 {
26 const int dt_list[] = { 2500, 5000, 7500, 10000 };
27 const int sr_list[] = { 8000, 16000, 24000, 32000, 48000, 96000 };
28
29 FuzzedDataProvider fdp(data, size);
30
31 int dt_us = fdp.PickValueInArray(dt_list);
32 int sr_hz = fdp.PickValueInArray(sr_list);
33 int nchannels =fdp.PickValueInArray({1, 2});
34 bool hrmode = fdp.ConsumeBool();
35
36 int sr_pcm_hz = fdp.PickValueInArray(sr_list);
37 if (sr_pcm_hz < sr_hz)
38 sr_pcm_hz = 0;
39
40 Decoder dec(dt_us, sr_hz, sr_pcm_hz, nchannels, hrmode);
41
42 int frame_size = fdp.ConsumeIntegralInRange(
43 LC3_MIN_FRAME_BYTES, LC3_MAX_FRAME_BYTES);
44
45 PcmFormat fmt = fdp.PickValueInArray(
46 { PcmFormat::kS16, PcmFormat::kS24,
47 PcmFormat::kS24In3Le, PcmFormat::kF32 });
48
49 int frame_samples = dec.GetFrameSamples();
50 if (frame_samples < 0)
51 return -1;
52
53 int sample_bytes =
54 fmt == PcmFormat::kS16 ? sizeof(int16_t) :
55 fmt == PcmFormat::kS24 ? sizeof(int32_t) :
56 fmt == PcmFormat::kS24In3Le ? sizeof(uint8_t) * 3 :
57 fmt == PcmFormat::kF32 ? sizeof(float) : 0;
58
59 if (fdp.remaining_bytes() < frame_size * nchannels)
60 return -1;
61
62 dec.Decode(
63 fdp.ConsumeBytes<uint8_t>(nchannels * frame_size).data(), frame_size,
64 fmt, std::vector<uint8_t>(nchannels * frame_samples * sample_bytes).data());
65
66 return 0;
67 }
68