• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_jpeg.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<ICodecImageJpeg> image = ICodecImageJpeg::Get(false);
35     if (image == nullptr) {
36         HDF_LOGE("%{public}s: get ICodecImageJpeg failed\n", __func__);
37         return false;
38     }
39     ImageAutoIniter autoIniter(image);
40 
41     CodecImageBuffer inBuffer;
42     auto srcData = const_cast<uint8_t *>(data);
43     unsigned int bufferSize = *reinterpret_cast<unsigned int *>(srcData);
44     auto err = image->AllocateInBuffer(inBuffer, bufferSize);
45     if (err != HDF_SUCCESS) {
46         HDF_LOGE("%{public}s AllocateInBuffer return %{public}d", __func__, err);
47         return false;
48     }
49     err = image->FreeInBuffer(inBuffer);
50     if (err != HDF_SUCCESS) {
51         HDF_LOGE("%{public}s FreeInBuffer return %{public}d", __func__, err);
52     }
53     return true;
54 }
55 }  // namespace Image
56 }  // namespace Codec
57 }  // namespace OHOS
58 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
60 {
61     OHOS::Codec::Image::AllocateInBuffer(data, size);
62     return 0;
63 }
64