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 #include <fuzzer/FuzzedDataProvider.h>
18 #include <iostream>
19 #include <memory>
20
21 #include "ultrahdr/jpegr.h"
22
23 using namespace ultrahdr;
24
25 // Transfer functions for image data, sync with ultrahdr.h
26 const int kOfMin = ULTRAHDR_OUTPUT_UNSPECIFIED + 1;
27 const int kOfMax = ULTRAHDR_OUTPUT_MAX;
28
29 class UltraHdrDecFuzzer {
30 public:
UltraHdrDecFuzzer(const uint8_t * data,size_t size)31 UltraHdrDecFuzzer(const uint8_t* data, size_t size) : mFdp(data, size){};
32 void process();
33
34 private:
35 FuzzedDataProvider mFdp;
36 };
37
process()38 void UltraHdrDecFuzzer::process() {
39 // hdr_of
40 auto of = static_cast<ultrahdr_output_format>(mFdp.ConsumeIntegralInRange<int>(kOfMin, kOfMax));
41 auto buffer = mFdp.ConsumeRemainingBytes<uint8_t>();
42 jpegr_compressed_struct jpegImgR{buffer.data(), (int)buffer.size(), (int)buffer.size(),
43 ULTRAHDR_COLORGAMUT_UNSPECIFIED};
44
45 jpegr_info_struct info{};
46 JpegR jpegHdr;
47 (void)jpegHdr.getJPEGRInfo(&jpegImgR, &info);
48 //#define DUMP_PARAM
49 #ifdef DUMP_PARAM
50 std::cout << "input buffer size " << jpegImgR.length << std::endl;
51 std::cout << "image dimensions " << info.width << " x " << info.width << std::endl;
52 #endif
53 if (info.width > kMaxWidth || info.height > kMaxHeight) return;
54 size_t outSize = info.width * info.height * ((of == ULTRAHDR_OUTPUT_HDR_LINEAR) ? 8 : 4);
55 jpegr_uncompressed_struct decodedJpegR;
56 auto decodedRaw = std::make_unique<uint8_t[]>(outSize);
57 decodedJpegR.data = decodedRaw.get();
58 ultrahdr_metadata_struct metadata;
59 (void)jpegHdr.decodeJPEGR(&jpegImgR, &decodedJpegR,
60 mFdp.ConsumeFloatingPointInRange<float>(1.0, FLT_MAX), nullptr, of,
61 nullptr, &metadata);
62 }
63
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
65 UltraHdrDecFuzzer fuzzHandle(data, size);
66 fuzzHandle.process();
67 return 0;
68 }
69