1 /*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // System include files
18 #include <fuzzer/FuzzedDataProvider.h>
19 #include <iostream>
20 #include <vector>
21
22 // User include files
23 #include "ultrahdr/jpegr.h"
24
25 using namespace android::ultrahdr;
26
27 // Transfer functions for image data, sync with ultrahdr.h
28 const int kOfMin = ULTRAHDR_OUTPUT_UNSPECIFIED + 1;
29 const int kOfMax = ULTRAHDR_OUTPUT_MAX;
30
31 class UltraHdrDecFuzzer {
32 public:
UltraHdrDecFuzzer(const uint8_t * data,size_t size)33 UltraHdrDecFuzzer(const uint8_t* data, size_t size) : mFdp(data, size){};
34 void process();
35
36 private:
37 FuzzedDataProvider mFdp;
38 };
39
process()40 void UltraHdrDecFuzzer::process() {
41 // hdr_of
42 auto of = static_cast<ultrahdr_output_format>(mFdp.ConsumeIntegralInRange<int>(kOfMin, kOfMax));
43 auto buffer = mFdp.ConsumeRemainingBytes<uint8_t>();
44 jpegr_compressed_struct jpegImgR{buffer.data(), (int)buffer.size(), (int)buffer.size(),
45 ULTRAHDR_COLORGAMUT_UNSPECIFIED};
46
47 std::vector<uint8_t> iccData(0);
48 std::vector<uint8_t> exifData(0);
49 jpegr_info_struct info{0, 0, &iccData, &exifData};
50 JpegR jpegHdr;
51 (void)jpegHdr.getJPEGRInfo(&jpegImgR, &info);
52 //#define DUMP_PARAM
53 #ifdef DUMP_PARAM
54 std::cout << "input buffer size " << jpegImgR.length << std::endl;
55 std::cout << "image dimensions " << info.width << " x " << info.width << std::endl;
56 #endif
57 size_t outSize = info.width * info.height * ((of == ULTRAHDR_OUTPUT_SDR) ? 4 : 8);
58 jpegr_uncompressed_struct decodedJpegR;
59 auto decodedRaw = std::make_unique<uint8_t[]>(outSize);
60 decodedJpegR.data = decodedRaw.get();
61 ultrahdr_metadata_struct metadata;
62 jpegr_uncompressed_struct decodedGainMap{};
63 (void)jpegHdr.decodeJPEGR(&jpegImgR, &decodedJpegR,
64 mFdp.ConsumeFloatingPointInRange<float>(1.0, FLT_MAX), nullptr, of,
65 &decodedGainMap, &metadata);
66 if (decodedGainMap.data) free(decodedGainMap.data);
67 }
68
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
70 UltraHdrDecFuzzer fuzzHandle(data, size);
71 fuzzHandle.process();
72 return 0;
73 }
74