1 /*
2 * Copyright (c) 2022-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 "codecsendcommand_fuzzer.h"
17 #include "codeccommon_fuzzer.h"
18
19 #include <securec.h>
20
21 namespace {
22 struct AllParameters {
23 enum OMX_COMMANDTYPE cmd;
24 uint32_t param;
25 int8_t *cmdData;
26 uint32_t cmdDataLen;
27 };
28 }
29
30 namespace OHOS {
31 namespace Codec {
CodecSendCommand(const uint8_t * data,size_t size)32 bool CodecSendCommand(const uint8_t *data, size_t size)
33 {
34 struct AllParameters params;
35 if (data == nullptr) {
36 return false;
37 }
38
39 uint8_t *rawData = const_cast<uint8_t *>(data);
40 params.param = Convert2Uint32(rawData);
41 if (size > sizeof(OMX_COMMANDTYPE) + sizeof(uint32_t) + sizeof(int8_t *)) {
42 rawData = rawData + sizeof(uint32_t);
43 size = size - sizeof(uint32_t);
44 params.cmdData = reinterpret_cast<int8_t *>(rawData);
45 params.cmdDataLen = size;
46 rawData = rawData + sizeof(int8_t *);
47 params.cmd = static_cast<OMX_COMMANDTYPE>(*rawData);
48 } else {
49 params.cmdData = reinterpret_cast<int8_t *>(rawData);
50 params.cmdDataLen = size;
51 params.cmd = static_cast<OMX_COMMANDTYPE>(*rawData);
52 }
53
54 bool result = Preconditions();
55 if (!result) {
56 HDF_LOGE("%{public}s: Preconditions failed\n", __func__);
57 return false;
58 }
59
60 int32_t ret = g_component->SendCommand(g_component, params.cmd, params.param, params.cmdData,
61 params.cmdDataLen);
62 if (ret != HDF_SUCCESS) {
63 HDF_LOGE("%{public}s: SendCommand failed, ret is [%{public}x]\n", __func__, ret);
64 }
65
66 result = Destroy();
67 if (!result) {
68 HDF_LOGE("%{public}s: Destroy failed\n", __func__);
69 return false;
70 }
71
72 return true;
73 }
74 } // namespace codec
75 } // namespace OHOS
76
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
78 {
79 OHOS::Codec::CodecSendCommand(data, size);
80 return 0;
81 }