fuzz_target_qcms.rs (3333B)
1 #![no_main] 2 use libfuzzer_sys::fuzz_target; 3 extern crate qcms; 4 extern crate libc; 5 /* This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 8 9 use qcms::c_bindings::{qcms_profile, icSigRgbData, icSigCmykData, icSigGrayData, qcms_profile_is_bogus}; 10 use qcms::c_bindings::{qcms_profile_get_color_space, qcms_profile_get_rendering_intent, qcms_profile_from_memory, qcms_profile_release, qcms_profile_sRGB, qcms_transform_create}; 11 use qcms::c_bindings::{qcms_profile_precache_output_transform, qcms_transform_data, qcms_transform_release, qcms_enable_iccv4}; 12 13 use qcms::DataType::*; 14 15 unsafe fn transform(src_profile: *const qcms_profile, dst_profile: *mut qcms_profile, size: usize) 16 { 17 // qcms supports GRAY and RGB profiles as input, and RGB as output. 18 19 let src_color_space = qcms_profile_get_color_space(&*src_profile); 20 let mut src_type = if (size & 1) != 0 { RGBA8 } else { RGB8 }; 21 if src_color_space == icSigGrayData { 22 src_type = if (size & 1) != 0 { GrayA8 } else { Gray8 }; 23 } else if src_color_space == icSigCmykData { 24 src_type = CMYK; 25 } else if src_color_space != icSigRgbData { 26 return; 27 } 28 29 let dst_color_space = qcms_profile_get_color_space(&*dst_profile); 30 if dst_color_space != icSigRgbData { 31 return; 32 } 33 let dst_type = if (size & 2) != 0 { RGBA8 } else { RGB8 }; 34 35 let intent = qcms_profile_get_rendering_intent(&*src_profile); 36 // Firefox calls this on the display profile to increase performance. 37 // Skip with low probability to increase coverage. 38 if (size % 15) != 0 { 39 qcms_profile_precache_output_transform(&mut *dst_profile); 40 } 41 42 let transform = 43 qcms_transform_create(&*src_profile, src_type, &*dst_profile, dst_type, intent); 44 if transform == std::ptr::null_mut() { 45 return; 46 } 47 48 const SRC_SIZE: usize = 36; 49 let src:[u8; SRC_SIZE] = [ 50 0x7F, 0x7F, 0x7F, 0x00, 0x00, 0x7F, 0x7F, 0xFF, 0x7F, 0x10, 0x20, 0x30, 51 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xB0, 0xBF, 0xEF, 0x6F, 52 0x3F, 0xC0, 0x9F, 0xE0, 0x90, 0xCF, 0x40, 0xAF, 0x0F, 0x01, 0x60, 0xF0, 53 ]; 54 let mut dst: [u8; 36 * 4] = [0; 144]; // 4x in case of GRAY to RGBA 55 56 qcms_transform_data(&*transform, src.as_ptr() as *const libc::c_void, dst.as_mut_ptr() as *mut libc::c_void, (SRC_SIZE / src_type.bytes_per_pixel()) as usize); 57 qcms_transform_release(transform); 58 } 59 60 unsafe fn do_fuzz(data: &[u8]) 61 { 62 let size = data.len(); 63 qcms_enable_iccv4(); 64 65 let profile = qcms_profile_from_memory(data.as_ptr() as *const libc::c_void, size); 66 if profile == std::ptr::null_mut() { 67 return; 68 } 69 70 let srgb_profile = qcms_profile_sRGB(); 71 if srgb_profile == std::ptr::null_mut() { 72 qcms_profile_release(profile); 73 return; 74 } 75 76 transform(profile, srgb_profile, size); 77 78 // Firefox only checks the display (destination) profile. 79 if !qcms_profile_is_bogus(&mut *profile) { 80 81 transform(srgb_profile, profile, size); 82 let identity = qcms::Profile::new_XYZD50(); 83 transform(&*identity, profile, size); 84 } 85 qcms_profile_release(profile); 86 qcms_profile_release(srgb_profile); 87 88 return; 89 } 90 91 92 93 fuzz_target!(|data: &[u8]| { 94 unsafe { do_fuzz(data) } 95 });