• 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 "imagedojpegdecode_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 "v1_0/icodec_image.h"
23 using namespace OHOS::HDI::Codec::Image::V1_0;
24 using namespace OHOS;
25 using namespace std;
26 namespace OHOS {
27 namespace Codec {
28 namespace Image {
Convert2Uint32(const uint8_t * ptr)29 static uint32_t Convert2Uint32(const uint8_t *ptr)
30 {
31     if (ptr == nullptr) {
32         return 0;
33     }
34     return (ptr[0] << 24) | (ptr[1] << 16) |  // 24:bit offset, 16: bit offset, 1:byte offset
35            (ptr[2] << 8) | (ptr[3]);          // 8:bit offset, 2: byte offset, 3:byte offset
36 }
37 
FillCodecJpegDecInfo(CodecJpegDecInfo & decInfo,uint8_t * data,size_t size)38 static bool FillCodecJpegDecInfo(CodecJpegDecInfo &decInfo, uint8_t *data, size_t size)
39 {
40     uint8_t *dataEnd = data + size - 1;
41     if (dataEnd < data + sizeof(decInfo.imageWidth)) {
42         return false;
43     }
44     decInfo.imageWidth = Convert2Uint32(data);
45     data += sizeof(decInfo.imageWidth);
46 
47     if (dataEnd < data + sizeof(decInfo.imageHeight)) {
48         return false;
49     }
50     decInfo.imageHeight = Convert2Uint32(data);
51     data += sizeof(decInfo.imageHeight);
52 
53     if (dataEnd < data + sizeof(decInfo.dataPrecision)) {
54         return false;
55     }
56     decInfo.dataPrecision = Convert2Uint32(data);
57     data += sizeof(decInfo.dataPrecision);
58 
59     if (dataEnd < data + sizeof(decInfo.numComponents)) {
60         return false;
61     }
62     decInfo.numComponents = Convert2Uint32(data);
63     data += sizeof(decInfo.numComponents);
64 
65     if (dataEnd < data + sizeof(decInfo.restartInterval)) {
66         return false;
67     }
68     decInfo.restartInterval = Convert2Uint32(data);
69     data += sizeof(decInfo.restartInterval);
70 
71     if (dataEnd < data + sizeof(decInfo.sampleSize)) {
72         return false;
73     }
74     decInfo.sampleSize = Convert2Uint32(data);
75     data += sizeof(decInfo.sampleSize);
76 
77     if (dataEnd < data + sizeof(decInfo.compressPos)) {
78         return false;
79     }
80     decInfo.compressPos = Convert2Uint32(data);
81     data += sizeof(decInfo.compressPos);
82 
83     if (dataEnd < data + sizeof(decInfo.arithCode)) {
84         return false;
85     }
86     decInfo.compressPos = *data++;
87     if (dataEnd < data + sizeof(decInfo.progressiveMode)) {
88         return false;
89     }
90     decInfo.progressiveMode = *data++;
91     return true;
92 }
93 
DoJpegDecode(const uint8_t * data,size_t size)94 bool DoJpegDecode(const uint8_t *data, size_t size)
95 {
96     if (data == nullptr) {
97         return false;
98     }
99 
100     sptr<ICodecImage> image = ICodecImage::Get(false);
101     if (image == nullptr) {
102         HDF_LOGE("%{public}s: get ICodecImage failed\n", __func__);
103         return false;
104     }
105     CodecImageRole role = CodecImageRole(*data);
106     ImageAutoIniter autoIniter(image, role);
107 
108     CodecImageBuffer inBuffer;
109     FillDataImageBuffer(inBuffer);
110     CodecImageBuffer outBuffer;
111     FillDataImageBuffer(outBuffer);
112     uint8_t *rawData = const_cast<uint8_t *>(data);
113     CodecJpegDecInfo decInfo;
114     if (!FillCodecJpegDecInfo(decInfo, rawData, size)) {
115         HDF_LOGE("%{public}s: FillCodecJpegDecInfo failed\n", __func__);
116         return false;
117     }
118 
119     auto err = image->DoJpegDecode(inBuffer, outBuffer, decInfo);
120     if (err != HDF_SUCCESS) {
121         HDF_LOGE("%{public}s DoJpegDecode return %{public}d", __func__, err);
122     }
123     return true;
124 }
125 }  // namespace Image
126 }  // namespace Codec
127 }  // namespace OHOS
128 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)129 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
130 {
131     OHOS::Codec::Image::DoJpegDecode(data, size);
132     return 0;
133 }
134