1/*
2 * Copyright (c) 2023 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
16#include <cstddef>
17#include <cstdint>
18#include "native_avcodec_base.h"
19#include "native_avcodec_videodecoder.h"
20#include "native_averrors.h"
21#include "videodec_sample.h"
22
23#define FUZZ_PROJECT_NAME "swdecoderresource_fuzzer"
24
25using namespace std;
26using namespace OHOS;
27using namespace OHOS::Media;
28
29static VDecFuzzSample *vDecSample = nullptr;
30constexpr uint32_t DEFAULT_WIDTH = 1920;
31constexpr uint32_t DEFAULT_HEIGHT = 1080;
32constexpr double DEFAULT_FRAME_RATE = 30.0;
33
34namespace OHOS {
35bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
36{
37    if (!vDecSample) {
38        vDecSample = new VDecFuzzSample();
39        vDecSample->defaultWidth = DEFAULT_WIDTH;
40        vDecSample->defaultHeight = DEFAULT_HEIGHT;
41        vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
42        vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC");
43        vDecSample->ConfigureVideoDecoder();
44        vDecSample->SetVideoDecoderCallback();
45        vDecSample->Start();
46    }
47    OH_AVErrCode ret = vDecSample->InputFuncFUZZ(data, size);
48    if (ret != AV_ERR_OK) {
49        vDecSample->Flush();
50        vDecSample->Stop();
51        vDecSample->Reset();
52        vDecSample->Release();
53        delete vDecSample;
54        vDecSample = nullptr;
55        return false;
56    }
57    return true;
58}
59} // namespace OHOS
60
61/* Fuzzer entry point */
62extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
63{
64    /* Run your code on data */
65    OHOS::DoSomethingInterestingWithMyAPI(data, size);
66    return 0;
67}
68