1/*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <atomic>
16#include <iostream>
17#include <fstream>
18#include <queue>
19#include <string>
20#include <thread>
21#include "audio_muxer_demo.h"
22#define FUZZ_PROJECT_NAME "audiomuxer_fuzzer"
23
24using namespace std;
25using namespace OHOS::MediaAVCodec;
26using namespace OHOS;
27using namespace OHOS::MediaAVCodec::AudioBufferDemo;
28
29namespace OHOS {
30
31bool AudioMuxerAACFuzzTest(const uint8_t *data, size_t size)
32{
33    if (size < sizeof(int64_t)) {
34        return false;
35    }
36    // FUZZ aac
37    AVMuxerDemo* aMuxerBufferDemo = new AVMuxerDemo();
38    aMuxerBufferDemo->InitFile("aac");
39    auto res = aMuxerBufferDemo->RunCase(data, size);
40    delete aMuxerBufferDemo;
41    return res;
42}
43
44bool AudioMuxerMPEG3FuzzTest(const uint8_t *data, size_t size)
45{
46    if (size < sizeof(int64_t)) {
47        return false;
48    }
49    // FUZZ mpeg3
50    AVMuxerDemo* aMuxerBufferDemo = new AVMuxerDemo();
51    aMuxerBufferDemo->InitFile("mpeg3");
52    auto res = aMuxerBufferDemo->RunCase(data, size);
53    delete aMuxerBufferDemo;
54    return res;
55}
56
57bool AudioMuxerAMRNBFuzzTest(const uint8_t *data, size_t size)
58{
59    if (size < sizeof(int64_t)) {
60        return false;
61    }
62    // FUZZ amrnb
63    AVMuxerDemo* aMuxerBufferDemo = new AVMuxerDemo();
64    aMuxerBufferDemo->InitFile("amrnb");
65    auto res = aMuxerBufferDemo->RunCase(data, size);
66    delete aMuxerBufferDemo;
67    return res;
68}
69
70bool AudioMuxerAMRWBFuzzTest(const uint8_t *data, size_t size)
71{
72    if (size < sizeof(int64_t)) {
73        return false;
74    }
75    // FUZZ amrwb
76    AVMuxerDemo* aMuxerBufferDemo = new AVMuxerDemo();
77    aMuxerBufferDemo->InitFile("amrwb");
78    auto res = aMuxerBufferDemo->RunCase(data, size);
79    delete aMuxerBufferDemo;
80    return res;
81}
82
83bool AudioMuxerJPGFuzzTest(const uint8_t *data, size_t size)
84{
85    if (size < sizeof(int64_t)) {
86        return false;
87    }
88    // FUZZ jpg
89    AVMuxerDemo* aMuxerBufferDemo = new AVMuxerDemo();
90    aMuxerBufferDemo->InitFile("jpg");
91    auto res = aMuxerBufferDemo->RunCase(data, size);
92    delete aMuxerBufferDemo;
93    return res;
94}
95
96}
97
98/* Fuzzer entry point */
99extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
100{
101    /* Run your code on data */
102    OHOS::AudioMuxerAACFuzzTest(data, size);
103    OHOS::AudioMuxerMPEG3FuzzTest(data, size);
104    OHOS::AudioMuxerAMRNBFuzzTest(data, size);
105    OHOS::AudioMuxerAMRWBFuzzTest(data, size);
106    OHOS::AudioMuxerJPGFuzzTest(data, size);
107    return 0;
108}