1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong DID 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 "imageallocateinbuffer_fuzzer.h"
17 #include <hdf_log.h>
18 #include <image_auto_initer.h>
19 #include <vector>
20 #include "v2_0/icodec_image.h"
21 using namespace OHOS::HDI::Codec::Image::V2_0;
22 using namespace OHOS;
23 using namespace std;
24 
dlclose(void * handle)25 extern "C" __attribute__((visibility("default"))) int dlclose(void* handle)
26 {
27     return 0;
28 }
29 
30 namespace OHOS {
31 namespace Codec {
32 namespace Image {
AllocateInBuffer(const uint8_t * data,size_t size)33 bool AllocateInBuffer(const uint8_t *data, size_t size)
34 {
35     if (data == nullptr || size < sizeof(unsigned int)) {
36         HDF_LOGE("%{public}s: data %p, size %zu\n", __func__, data, size);
37         return false;
38     }
39 
40     sptr<ICodecImage> image = ICodecImage::Get(false);
41     if (image == nullptr) {
42         HDF_LOGE("%{public}s: get ICodecImage failed\n", __func__);
43         return false;
44     }
45     CodecImageRole role = CodecImageRole(*data);
46     ImageAutoIniter autoIniter(image, role);
47 
48     CodecImageBuffer inBuffer;
49     auto srcData = const_cast<uint8_t *>(data);
50     unsigned int bufferSize = *reinterpret_cast<unsigned int *>(srcData);
51     auto err = image->AllocateInBuffer(inBuffer, bufferSize, role);
52     if (err != HDF_SUCCESS) {
53         HDF_LOGE("%{public}s AllocateInBuffer return %{public}d", __func__, err);
54         return false;
55     }
56     err = image->FreeInBuffer(inBuffer);
57     if (err != HDF_SUCCESS) {
58         HDF_LOGE("%{public}s FreeInBuffer return %{public}d", __func__, err);
59     }
60     return true;
61 }
62 }  // namespace Image
63 }  // namespace Codec
64 }  // namespace OHOS
65 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
67 {
68     OHOS::Codec::Image::AllocateInBuffer(data, size);
69     return 0;
70 }
71