• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "codeccommon_fuzzer.h"
17 
18 #define HDF_LOG_TAG codec_fuzz
19 
20 namespace OHOS {
21 namespace Codec {
22 static const int32_t DATA_BUFFERID = 10;
23 static const int32_t DATA_SIZE = 20;
24 static const int32_t DATA_VERSION_NVERSION = 30;
25 static const int32_t DATA_BUFFERTYPE = 40;
26 static const int32_t DATA_BUFFERLEN = 50;
27 static const int32_t DATA_ALLOCLEN = 60;
28 static const int32_t DATA_FILLEDLEN = 70;
29 static const int32_t DATA_OFFSET = 80;
30 static const int32_t DATA_FENCEFD = 90;
31 static const int32_t DATA_TYPE = 100;
32 static const int32_t DATA_PTS = 200;
33 static const int32_t DATA_FLAG = 300;
34 static const int32_t testingAppData = 33;
35 
36 CodecComponentManager *g_manager = nullptr;
37 CodecComponentType *g_component = nullptr;
38 CodecCallbackType *g_callback = nullptr;
39 uint32_t g_componentId = 0;
40 static int32_t g_appData = testingAppData;
41 
FillDataOmxCodecBuffer(struct OmxCodecBuffer * dataFuzz)42 void FillDataOmxCodecBuffer(struct OmxCodecBuffer *dataFuzz)
43 {
44     dataFuzz->bufferId = DATA_BUFFERID;
45     dataFuzz->size = DATA_SIZE;
46     dataFuzz->version.nVersion = DATA_VERSION_NVERSION;
47     dataFuzz->bufferType = (enum CodecBufferType)DATA_BUFFERTYPE;
48     dataFuzz->buffer = reinterpret_cast<uint8_t *>(OsalMemAlloc(DATA_BUFFERLEN));
49     if (dataFuzz->buffer == nullptr) {
50         HDF_LOGE("%{public}s: dataFuzz->buffer is nullptr", __func__);
51         return;
52     }
53     dataFuzz->bufferLen = DATA_BUFFERLEN;
54     dataFuzz->allocLen = DATA_ALLOCLEN;
55     dataFuzz->filledLen = DATA_FILLEDLEN;
56     dataFuzz->offset = DATA_OFFSET;
57     dataFuzz->fenceFd = DATA_FENCEFD;
58     dataFuzz->type = (enum ShareMemTypes)DATA_TYPE;
59     dataFuzz->pts = DATA_PTS;
60     dataFuzz->flag = DATA_FLAG;
61 }
62 
Preconditions()63 bool Preconditions()
64 {
65     g_manager = GetCodecComponentManager();
66     if (g_manager == nullptr) {
67         HDF_LOGE("%{public}s: GetCodecComponentManager failed", __func__);
68         return false;
69     }
70 
71     g_callback = CodecCallbackTypeGet(nullptr);
72     if (g_callback == nullptr) {
73         HDF_LOGE("%{public}s: CodecCallbackTypeGet failed", __func__);
74         CodecComponentManagerRelease();
75         return false;
76     }
77 
78     int32_t componentCount = g_manager->GetComponentNum();
79     if (componentCount <= 0) {
80         HDF_LOGE("%{public}s: GetComponentNum failed", __func__);
81         CodecCallbackTypeRelease(g_callback);
82         CodecComponentManagerRelease();
83         return false;
84     }
85 
86     CodecCompCapability *capList = new CodecCompCapability[componentCount];
87     if (capList == nullptr) {
88         HDF_LOGE("%{public}s: new CodecCompCapability failed", __func__);
89         CodecCallbackTypeRelease(g_callback);
90         CodecComponentManagerRelease();
91         return false;
92     }
93 
94     int32_t ret = g_manager->GetComponentCapabilityList(capList, componentCount);
95     if (ret != HDF_SUCCESS) {
96         HDF_LOGE("%{public}s: GetComponentCapabilityList failed", __func__);
97         delete[] capList;
98         CodecCallbackTypeRelease(g_callback);
99         CodecComponentManagerRelease();
100         return false;
101     }
102 
103     ret = g_manager->CreateComponent(&g_component, &g_componentId, capList[0].compName, g_appData, g_callback);
104     if (ret != HDF_SUCCESS) {
105         HDF_LOGE("%{public}s: CreateComponent failed", __func__);
106         delete[] capList;
107         CodecCallbackTypeRelease(g_callback);
108         CodecComponentManagerRelease();
109         return false;
110     }
111     delete[] capList;
112 
113     return true;
114 }
115 
Destroy()116 bool Destroy()
117 {
118     int32_t ret = g_manager->DestroyComponent(g_componentId);
119     if (ret != HDF_SUCCESS) {
120         HDF_LOGE("%{public}s: DestroyComponent failed", __func__);
121         return false;
122     }
123     CodecCallbackTypeRelease(g_callback);
124     CodecComponentManagerRelease();
125     return true;
126 }
127 } // namespace Codec
128 } // namespace OHOS