icc_codec_fuzzer.cc (2459B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #include "lib/jxl/enc_icc_codec.h" 7 8 namespace jxl { 9 Status PredictICC(const uint8_t* icc, size_t size, PaddedBytes* result); 10 Status UnpredictICC(const uint8_t* enc, size_t size, PaddedBytes* result); 11 } // namespace jxl 12 13 namespace jpegxl { 14 namespace tools { 15 16 using ::jxl::PaddedBytes; 17 18 #ifdef JXL_ICC_FUZZER_SLOW_TEST 19 using ::jxl::BitReader; 20 using ::jxl::Span; 21 #endif 22 23 int TestOneInput(const uint8_t* data, size_t size) { 24 #if defined(JXL_ICC_FUZZER_ONLY_WRITE) 25 bool read = false; 26 #elif defined(JXL_ICC_FUZZER_ONLY_READ) 27 bool read = true; 28 #else 29 // Decide whether to test the reader or the writer (both use parsing) 30 if (!size) return 0; 31 bool read = data[0] == 0; 32 data++; 33 size--; 34 #endif 35 36 #ifdef JXL_ICC_FUZZER_SLOW_TEST 37 // Including JPEG XL LZ77 and ANS compression. These are already fuzzed 38 // separately, so it is better to disable JXL_ICC_FUZZER_SLOW_TEST to focus on 39 // the ICC parsing. 40 if (read) { 41 // Reading parses the compressed format. 42 BitReader br(Bytes(data, size)); 43 std::vector<uint8_t> result; 44 (void)jxl::test::ReadICC(&br, &result); 45 (void)br.Close(); 46 } else { 47 // Writing parses the original ICC profile. 48 PaddedBytes icc; 49 icc.assign(data, data + size); 50 BitWriter writer; 51 // Writing should support any random bytestream so must succeed, make 52 // fuzzer fail if not. 53 JXL_ASSERT(jxl::WriteICC(icc, &writer, 0, nullptr)); 54 } 55 #else // JXL_ICC_FUZZER_SLOW_TEST 56 if (read) { 57 // Reading (unpredicting) parses the compressed format. 58 PaddedBytes result; 59 (void)jxl::UnpredictICC(data, size, &result); 60 } else { 61 // Writing (predicting) parses the original ICC profile. 62 PaddedBytes result; 63 // Writing should support any random bytestream so must succeed, make 64 // fuzzer fail if not. 65 JXL_ASSERT(jxl::PredictICC(data, size, &result)); 66 PaddedBytes reconstructed; 67 JXL_ASSERT(jxl::UnpredictICC(result.data(), result.size(), &reconstructed)); 68 JXL_ASSERT(reconstructed.size() == size); 69 JXL_ASSERT(memcmp(data, reconstructed.data(), size) == 0); 70 } 71 #endif // JXL_ICC_FUZZER_SLOW_TEST 72 return 0; 73 } 74 75 } // namespace tools 76 } // namespace jpegxl 77 78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 79 return jpegxl::tools::TestOneInput(data, size); 80 }