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
49 OHOS::VDI::HEIF::HeifEncodeHelper heifHelper;
50 heifHelper.Reset();
51
52 if (decision) {
53 if (!heifHelper.AssembleParamForTmap(rawData, size)) {
54 HDF_LOGE("%{public}s: AssembleParamForTmap failed\n", __func__);
55 return false;
56 }
57 } else {
58 if (!heifHelper.AssembleParamForPrimaryImg(rawData, size)) {
59 HDF_LOGE("%{public}s: AssembleParamForPrimaryImg failed\n", __func__);
60 return false;
61 }
62 }
63
64 SharedBuffer output;
65 if (!heifHelper.AllocOutputBuffer(output)) {
66 HDF_LOGE("%{public}s: AllocOutputBuffer failed\n", __func__);
67 return false;
68 }
69 uint32_t filledLen = 0;
70
71 auto err = image->DoHeifEncode(heifHelper.inputImgs_, heifHelper.inputMetas_, heifHelper.refs_, output, filledLen);
72 if (err != HDF_SUCCESS) {
73 HDF_LOGE("%{public}s: DOHeifEncode return %{public}d", __func__, err);
74 }
75
76 return true;
77 }
78 } // namespace Image
79 } // namespace Codec
80 } // namespace OHOS
81
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
83 {
84 OHOS::Codec::Image::DoHeifEncode(data, size);
85 return 0;
86 }
87