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 "v1_0/icodec_image.h"
21 using namespace OHOS::HDI::Codec::Image::V1_0;
22 using namespace OHOS;
23 using namespace std;
24 namespace OHOS {
25 namespace Codec {
26 namespace Image {
AllocateInBuffer(const uint8_t * data,size_t size)27 bool AllocateInBuffer(const uint8_t *data, size_t size)
28 {
29 if (data == nullptr || size < sizeof(unsigned int)) {
30 HDF_LOGE("%{public}s: data %p, size %zu\n", __func__, data, size);
31 return false;
32 }
33
34 sptr<ICodecImage> image = ICodecImage::Get(false);
35 if (image == nullptr) {
36 HDF_LOGE("%{public}s: get ICodecImage failed\n", __func__);
37 return false;
38 }
39 CodecImageRole role = CodecImageRole(*data);
40 ImageAutoIniter autoIniter(image, role);
41
42 CodecImageBuffer inBuffer;
43 auto srcData = const_cast<uint8_t *>(data);
44 unsigned int bufferSize = *reinterpret_cast<unsigned int *>(srcData);
45 auto err = image->AllocateInBuffer(inBuffer, bufferSize, role);
46 if (err != HDF_SUCCESS) {
47 HDF_LOGE("%{public}s AllocateInBuffer return %{public}d", __func__, err);
48 return false;
49 }
50 err = image->FreeInBuffer(inBuffer);
51 if (err != HDF_SUCCESS) {
52 HDF_LOGE("%{public}s FreeInBuffer return %{public}d", __func__, err);
53 }
54 return true;
55 }
56 } // namespace Image
57 } // namespace Codec
58 } // namespace OHOS
59
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
61 {
62 OHOS::Codec::Image::AllocateInBuffer(data, size);
63 return 0;
64 }
65