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 "codecallocatebuffer_fuzzer.h"
17 #include "codeccommon_fuzzer.h"
18 
19 namespace OHOS {
20 namespace Codec {
21 
22     static const uint32_t OMX_MAX_PORT_INDEX = 2;
23 
CodecAllocateBuffer(const uint8_t * data,size_t size)24     bool CodecAllocateBuffer(const uint8_t *data, size_t size)
25     {
26         if (data == nullptr) {
27             return false;
28         }
29 
30         bool result = Preconditions();
31         if (!result) {
32             HDF_LOGE("%{public}s: Preconditions failed\n", __func__);
33             return false;
34         }
35 
36         struct OmxCodecBuffer inbuffer, outBuffer;
37         FillDataOmxCodecBuffer(&inbuffer);
38         int32_t ret = g_component->SendCommand(HDI::Codec::V3_0::CODEC_COMMAND_STATE_SET,
39                                                HDI::Codec::V3_0::CODEC_STATE_IDLE, {});
40         if (ret != HDF_SUCCESS) {
41             HDF_LOGE("%{public}s: Set LOADED failed, ret is [%{public}x]\n", __func__, ret);
42         }
43         ret = g_component->AllocateBuffer(static_cast<uint32_t>((*(const_cast<uint8_t *>(data)) % OMX_MAX_PORT_INDEX)),
44             inbuffer, outBuffer);
45         if (ret != HDF_SUCCESS) {
46             HDF_LOGE("%{public}s: AllocateBuffer failed, ret is [%{public}x]\n", __func__, ret);
47         }
48 
49         result = Destroy();
50         if (!result) {
51             HDF_LOGE("%{public}s: Destroy failed\n", __func__);
52             return false;
53         }
54 
55         return true;
56     }
57 } // namespace codec
58 } // namespace OHOS
59 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
61 {
62     OHOS::Codec::CodecAllocateBuffer(data, size);
63     return 0;
64 }
65