1 /*
2 * Copyright (c) 2024 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 "imagedoheifencode_fuzzer.h"
17 #include <hdf_log.h>
18 #include <image_auto_initer.h>
19 #include <securec.h>
20 #include <vector>
21 #include "image_common.h"
22 #include "encode_heif_helper.h"
23 #include "v2_0/icodec_image.h"
24 using namespace OHOS::HDI::Codec::Image::V2_0;
25 using namespace OHOS;
26 using namespace std;
27 namespace OHOS {
28 namespace Codec {
29 namespace Image {
30
31
DoHeifEncode(const uint8_t * data,size_t size)32 bool DoHeifEncode(const uint8_t *data, size_t size)
33 {
34 if (data == nullptr || size < sizeof(unsigned int)) {
35 return false;
36 }
37
38 sptr<ICodecImage> image = ICodecImage::Get(false);
39 if (image == nullptr) {
40 HDF_LOGE("%{public}s: get ICodecImage failed\n", __func__);
41 return false;
42 }
43 CodecImageRole role = CodecImageRole(*data);
44 ImageAutoIniter autoIniter(image, role);
45
46 uint8_t *rawData = const_cast<uint8_t *>(data);
47 uint8_t decision = (*rawData) % 2;
48 rawData += sizeof(decision);
49
50 OHOS::VDI::HEIF::HeifEncodeHelper heifHelper;
51 heifHelper.Reset();
52
53 if (decision) {
54 if (!heifHelper.AssembleParamForTmap(rawData, size)) {
55 HDF_LOGE("%{public}s: AssembleParamForTmap failed\n", __func__);
56 return false;
57 }
58 } else {
59 if (!heifHelper.AssembleParamForPrimaryImg(rawData, size)) {
60 HDF_LOGE("%{public}s: AssembleParamForPrimaryImg failed\n", __func__);
61 return false;
62 }
63 }
64
65 SharedBuffer output;
66 if (!heifHelper.AllocOutputBuffer(output)) {
67 HDF_LOGE("%{public}s: AllocOutputBuffer failed\n", __func__);
68 return false;
69 }
70 uint32_t filledLen = 0;
71
72 auto err = image->DoHeifEncode(heifHelper.inputImgs_, heifHelper.inputMetas_, heifHelper.refs_, output, filledLen);
73 if (err != HDF_SUCCESS) {
74 HDF_LOGE("%{public}s: DOHeifEncode return %{public}d", __func__, err);
75 }
76
77 return true;
78 }
79 } // namespace Image
80 } // namespace Codec
81 } // namespace OHOS
82
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
84 {
85 OHOS::Codec::Image::DoHeifEncode(data, size);
86 return 0;
87 }
88