diff --git a/Cargo.lock b/Cargo.lock index 34835d8dafe..1a92cea0b92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2169,6 +2169,7 @@ dependencies = [ "image-webp", "moxcms", "num-traits", + "png", ] [[package]] diff --git a/moli-image/Cargo.toml b/moli-image/Cargo.toml index 3ba3b93a766..2b70fb9783d 100644 --- a/moli-image/Cargo.toml +++ b/moli-image/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" [dependencies] color = "0.3.3" -image = { version = "0.25.10", default-features = false, features = ["bmp", "gif", "webp"] } +image = { version = "0.25.10", default-features = false, features = ["bmp", "gif", "ico", "webp"] } jpeg-decoder = "0.3" jpeg-encoder = "0.6" moxcms = { version = "0.8.1", features = ["extended_range"] } diff --git a/moli-image/src/ico.rs b/moli-image/src/ico.rs new file mode 100644 index 00000000000..f574c4a9331 --- /dev/null +++ b/moli-image/src/ico.rs @@ -0,0 +1,116 @@ +//! ICO directory inspection for the bounded, color-managed PNG codec path. +//! BMP icons use the image decoder's palette and AND-mask handling. + +use crate::{PngDecodeError, RasterColorPrecision, png::PngOrientation}; + +#[derive(Debug, thiserror::Error)] +pub enum IcoDecodeError { + #[error("invalid ICO directory")] + InvalidDirectory, + #[error("ICO image entry is outside the encoded buffer")] + InvalidEntry, + #[error("ICO directory dimensions do not match its PNG image")] + DimensionMismatch, + #[error(transparent)] + Png(#[from] PngDecodeError), +} + +pub(super) struct PngIcon<'a> { + bytes: &'a [u8], + width: u32, + height: u32, +} + +impl PngIcon<'_> { + pub(super) fn metadata(&self) -> Result<(u32, u32, RasterColorPrecision), IcoDecodeError> { + // Exif belongs to a standalone PNG. ICO directory frames retain their + // encoded orientation, including when the selected frame is a PNG. + let metadata = + crate::png::png_metadata_with_orientation(self.bytes, PngOrientation::Ignore)?; + // A zero directory byte denotes 256, including larger embedded PNGs. + if metadata.0.min(256) != self.width || metadata.1.min(256) != self.height { + return Err(IcoDecodeError::DimensionMismatch); + } + Ok(metadata) + } + + pub(super) fn decode( + &self, + convert_profile: bool, + ) -> Result { + self.metadata()?; + Ok(crate::png::decode_png_colors_with_orientation( + self.bytes, + convert_profile, + PngOrientation::Ignore, + )?) + } +} + +fn u16_le(bytes: &[u8]) -> u16 { + u16::from_le_bytes(bytes[..2].try_into().expect("ICO directory field")) +} + +fn u32_le(bytes: &[u8]) -> u32 { + u32::from_le_bytes(bytes[..4].try_into().expect("ICO directory field")) +} + +fn dimension(value: u8) -> u32 { + if value == 0 { 256 } else { u32::from(value) } +} + +fn entry_score(entry: &[u8]) -> (u16, u32) { + // Match the underlying ICO decoder's frame choice, including its last-entry + // tie break, so metadata and pixel decoding always refer to the same frame. + ( + u16_le(&entry[6..]), + dimension(entry[0]) * dimension(entry[1]), + ) +} + +pub(super) fn png_icon(bytes: &[u8]) -> Result>, IcoDecodeError> { + let header = bytes.get(..6).ok_or(IcoDecodeError::InvalidDirectory)?; + if header[..4] != [0, 0, 1, 0] { + return Err(IcoDecodeError::InvalidDirectory); + } + let count = usize::from(u16_le(&header[4..])); + if count == 0 { + return Err(IcoDecodeError::InvalidDirectory); + } + let directory_end = 6 + count * 16; + let directory = bytes + .get(6..directory_end) + .ok_or(IcoDecodeError::InvalidDirectory)?; + let mut selected: Option<&[u8]> = None; + for entry in directory.as_chunks::<16>().0 { + if u16_le(&entry[4..]) > 256 || u16_le(&entry[6..]) > 256 { + return Err(IcoDecodeError::InvalidDirectory); + } + if selected.is_none_or(|previous| entry_score(entry) >= entry_score(previous)) { + selected = Some(entry); + } + } + let selected = selected.expect("non-empty ICO directory"); + let offset = + usize::try_from(u32_le(&selected[12..])).map_err(|_| IcoDecodeError::InvalidEntry)?; + let length = + usize::try_from(u32_le(&selected[8..])).map_err(|_| IcoDecodeError::InvalidEntry)?; + let end = offset + .checked_add(length) + .ok_or(IcoDecodeError::InvalidEntry)?; + if offset < directory_end { + return Err(IcoDecodeError::InvalidEntry); + } + let payload = bytes.get(offset..end).ok_or(IcoDecodeError::InvalidEntry)?; + if !payload.starts_with(b"\x89PNG\r\n\x1a\n") { + return Ok(None); + } + Ok(Some(PngIcon { + bytes: payload, + width: dimension(selected[0]), + height: dimension(selected[1]), + })) +} + +#[cfg(test)] +mod tests; diff --git a/moli-image/src/ico/tests.rs b/moli-image/src/ico/tests.rs new file mode 100644 index 00000000000..b0cf5d6471b --- /dev/null +++ b/moli-image/src/ico/tests.rs @@ -0,0 +1,177 @@ +use super::*; +use crate::{ + RasterDecodeError, RasterImageFormat, decode_raster_image, probe_raster_image, + raster_image_dimensions, supports_image_mime_essence, +}; + +fn icon(entries: &[(u8, u8, u16, &[u8])]) -> Vec { + let mut bytes = vec![0, 0, 1, 0]; + bytes.extend_from_slice(&u16::try_from(entries.len()).unwrap().to_le_bytes()); + let mut offset = 6 + entries.len() * 16; + for &(width, height, depth, payload) in entries { + bytes.extend_from_slice(&[width, height, 0, 0]); + bytes.extend_from_slice(&1_u16.to_le_bytes()); + bytes.extend_from_slice(&depth.to_le_bytes()); + bytes.extend_from_slice(&u32::try_from(payload.len()).unwrap().to_le_bytes()); + bytes.extend_from_slice(&u32::try_from(offset).unwrap().to_le_bytes()); + offset += payload.len(); + } + for &(_, _, _, payload) in entries { + bytes.extend_from_slice(payload); + } + bytes +} + +fn png() -> Vec { + crate::encode_png_rgba8(2, 1, &[255, 0, 0, 255, 0, 0, 255, 128]) + .unwrap() + .bytes +} + +#[test] +fn png_icons_share_bounded_metadata_and_transparent_pixels() { + let png = png(); + let bytes = icon(&[(2, 1, 32, &png)]); + let metadata = probe_raster_image(&bytes).unwrap(); + assert_eq!(metadata.format, RasterImageFormat::Ico); + assert_eq!(metadata.decoded_byte_len(), 8); + assert_eq!(raster_image_dimensions(&bytes).unwrap(), (2, 1)); + assert_eq!( + decode_raster_image(&bytes).unwrap().image, + crate::decode_png(&png).unwrap() + ); + for essence in ["image/x-icon", "image/vnd.microsoft.icon"] { + assert!(supports_image_mime_essence(essence), "{essence}"); + } +} + +#[test] +fn png_icons_retain_full_precision_samples() { + let mut png = Vec::new(); + { + let mut encoder = png::Encoder::new(&mut png, 1, 1); + encoder.set_color(png::ColorType::Rgba); + encoder.set_depth(png::BitDepth::Sixteen); + let mut writer = encoder.write_header().unwrap(); + writer + .write_image_data(&[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xff, 0xff]) + .unwrap(); + writer.finish().unwrap(); + } + let plain = decode_raster_image(&png).unwrap(); + let wrapped = decode_raster_image(&icon(&[(1, 1, 32, &png)])).unwrap(); + assert_eq!( + wrapped.metadata.color_precision, + RasterColorPrecision::Float32 + ); + assert_eq!(wrapped.image, plain.image); + assert!(wrapped.color_managed.is_some()); + assert_eq!(wrapped.color_managed, plain.color_managed); +} + +#[test] +fn png_icon_frames_ignore_embedded_exif_orientation() { + let exif = b"II*\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0"; + let mut info = png::Info::with_size(2, 1); + info.color_type = png::ColorType::Rgba; + info.exif_metadata = Some(std::borrow::Cow::Borrowed(exif)); + let mut png = Vec::new(); + { + let mut writer = png::Encoder::with_info(&mut png, info) + .unwrap() + .write_header() + .unwrap(); + writer + .write_image_data(&[255, 0, 0, 255, 0, 0, 255, 255]) + .unwrap(); + writer.finish().unwrap(); + } + let bytes = icon(&[(2, 1, 32, &png)]); + assert_eq!(raster_image_dimensions(&bytes).unwrap(), (2, 1)); + let decoded = decode_raster_image(&bytes).unwrap(); + assert_eq!((decoded.image.width, decoded.image.height), (2, 1)); + assert_eq!(decoded.image.rgba, [255, 0, 0, 255, 0, 0, 255, 255]); +} + +#[test] +fn bmp_icons_apply_bottom_up_rows_and_the_and_mask() { + let mut dib = Vec::new(); + dib.extend_from_slice(&40_u32.to_le_bytes()); + dib.extend_from_slice(&2_i32.to_le_bytes()); + dib.extend_from_slice(&4_i32.to_le_bytes()); // XOR pixels and AND mask share the height. + dib.extend_from_slice(&1_u16.to_le_bytes()); + dib.extend_from_slice(&24_u16.to_le_bytes()); + dib.extend_from_slice(&0_u32.to_le_bytes()); + dib.extend_from_slice(&24_u32.to_le_bytes()); + dib.extend_from_slice(&[0; 16]); + dib.extend_from_slice(&[ + 255, 0, 0, 255, 255, 255, 0, 0, // blue, white, padding (bottom row) + 0, 0, 255, 0, 255, 0, 0, 0, // red, green, padding (top row) + 0x80, 0, 0, 0, // bottom-left pixel is transparent + 0, 0, 0, 0, + ]); + let bytes = icon(&[(2, 2, 24, &dib)]); + assert_eq!(raster_image_dimensions(&bytes).unwrap(), (2, 2)); + assert_eq!( + decode_raster_image(&bytes).unwrap().image.rgba, + [ + 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 0, 255, 255, 255, 255, + ] + ); +} + +#[test] +fn icon_metadata_and_decode_select_the_same_directory_entry() { + let large = png(); + let small = crate::encode_png_rgba8(1, 1, &[0, 255, 0, 255]) + .unwrap() + .bytes; + for bytes in [ + icon(&[(1, 1, 32, &small), (2, 1, 32, &large)]), + icon(&[(2, 1, 32, &large), (1, 1, 32, &small)]), + ] { + assert_eq!(raster_image_dimensions(&bytes).unwrap(), (2, 1)); + assert_eq!( + decode_raster_image(&bytes).unwrap().image, + crate::decode_png(&large).unwrap() + ); + } +} + +#[test] +fn png_icon_metadata_does_not_inflate_the_pixel_payload() { + let mut png = png(); + let idat = png.windows(4).position(|chunk| chunk == b"IDAT").unwrap(); + png[idat + 4] ^= 0xff; + let bytes = icon(&[(2, 1, 32, &png)]); + assert_eq!(raster_image_dimensions(&bytes).unwrap(), (2, 1)); + assert!(decode_raster_image(&bytes).is_err()); +} + +#[test] +fn icon_probe_rejects_invalid_directories_offsets_and_dimensions() { + assert!(matches!( + probe_raster_image(b"\0\0\x01\0\0\0"), + Err(RasterDecodeError::Ico(IcoDecodeError::InvalidDirectory)) + )); + let png = png(); + let bytes = icon(&[(2, 1, 32, &png)]); + for end in [5, 6, 21, bytes.len() - 1] { + assert!( + probe_raster_image(&bytes[..end]).is_err(), + "truncated at {end}" + ); + } + for offset in [0_u32, u32::MAX] { + let mut invalid = bytes.clone(); + invalid[18..22].copy_from_slice(&offset.to_le_bytes()); + assert!(matches!( + probe_raster_image(&invalid), + Err(RasterDecodeError::Ico(IcoDecodeError::InvalidEntry)) + )); + } + assert!(matches!( + probe_raster_image(&icon(&[(1, 1, 32, &png)])), + Err(RasterDecodeError::Ico(IcoDecodeError::DimensionMismatch)) + )); +} diff --git a/moli-image/src/lib.rs b/moli-image/src/lib.rs index 621638741a5..e3e8e61728b 100644 --- a/moli-image/src/lib.rs +++ b/moli-image/src/lib.rs @@ -9,6 +9,7 @@ mod budget; mod color; #[cfg(test)] mod color_tests; +mod ico; mod jpeg; mod orientation; #[cfg(test)] @@ -22,6 +23,7 @@ pub use budget::{MAX_DECODED_RGBA_BYTES, MAX_ENCODED_IMAGE_BYTES}; pub use color::{ ColorDecodeError, ColorManagedRasterImage, RasterColorPrecision, UnconvertedRasterPixels, }; +pub use ico::IcoDecodeError; pub use jpeg::{EncodedJpeg, JpegDecodeError, JpegEncodeError, decode_jpeg, encode_jpeg}; pub use png::{ EncodedPng, PngDecodeError, PngEncodeError, PngEncodeOptions, decode_png, encode_png, diff --git a/moli-image/src/png.rs b/moli-image/src/png.rs index 765677cc3cf..197c07180e3 100644 --- a/moli-image/src/png.rs +++ b/moli-image/src/png.rs @@ -114,9 +114,32 @@ pub fn decode_png(bytes: &[u8]) -> Result { decode_png_colors(bytes, true).map(|(image, _)| image) } +#[derive(Clone, Copy)] +pub(crate) enum PngOrientation { + FromExif, + Ignore, +} + +impl PngOrientation { + fn resolve(self, exif: Option<&[u8]>) -> image::metadata::Orientation { + match self { + Self::FromExif => crate::orientation::from_exif(exif), + Self::Ignore => image::metadata::Orientation::NoTransforms, + } + } +} + pub(crate) fn decode_png_colors( bytes: &[u8], convert_profile: bool, +) -> Result { + decode_png_colors_with_orientation(bytes, convert_profile, PngOrientation::FromExif) +} + +pub(crate) fn decode_png_colors_with_orientation( + bytes: &[u8], + convert_profile: bool, + orientation: PngOrientation, ) -> Result { use crate::color::{ColorSamples, RawColorImage}; let mut decoder = png::Decoder::new_with_limits( @@ -128,7 +151,7 @@ pub(crate) fn decode_png_colors( decoder.set_transformations(png::Transformations::EXPAND); let mut reader = decoder.read_info()?; let (width, height) = (reader.info().width, reader.info().height); - let orientation = crate::orientation::from_exif(reader.info().exif_metadata.as_deref()); + let orientation = orientation.resolve(reader.info().exif_metadata.as_deref()); checked_rgba_len(width, height)?; let cicp = cicp_profile(reader.info()); let expand_gray = cicp.is_some(); @@ -196,6 +219,13 @@ pub(crate) fn decode_png_colors( pub(crate) fn png_metadata( bytes: &[u8], +) -> Result<(u32, u32, crate::RasterColorPrecision), PngDecodeError> { + png_metadata_with_orientation(bytes, PngOrientation::FromExif) +} + +pub(crate) fn png_metadata_with_orientation( + bytes: &[u8], + orientation: PngOrientation, ) -> Result<(u32, u32, crate::RasterColorPrecision), PngDecodeError> { let decoder = png::Decoder::new_with_limits( Cursor::new(bytes), @@ -208,7 +238,7 @@ pub(crate) fn png_metadata( let (width, height) = crate::orientation::dimensions( info.width, info.height, - crate::orientation::from_exif(info.exif_metadata.as_deref()), + orientation.resolve(info.exif_metadata.as_deref()), ); checked_rgba_len(info.width, info.height)?; let high_precision = info.bit_depth == png::BitDepth::Sixteen diff --git a/moli-image/src/raster.rs b/moli-image/src/raster.rs index 9d380d57bff..f5f07878d67 100644 --- a/moli-image/src/raster.rs +++ b/moli-image/src/raster.rs @@ -21,6 +21,7 @@ pub enum RasterImageFormat { Gif, WebP, Bmp, + Ico, } impl RasterImageFormat { @@ -31,6 +32,7 @@ impl RasterImageFormat { ImageFormat::Gif => Some(Self::Gif), ImageFormat::WebP => Some(Self::WebP), ImageFormat::Bmp => Some(Self::Bmp), + ImageFormat::Ico => Some(Self::Ico), _ => None, } } @@ -97,6 +99,8 @@ pub enum RasterDecodeError { Png(#[from] PngDecodeError), #[error("JPEG image could not be decoded: {0}")] Jpeg(#[from] JpegDecodeError), + #[error("ICO image could not be decoded: {0}")] + Ico(#[from] crate::IcoDecodeError), #[error("{format:?} image could not be decoded: {source}")] Image { format: RasterImageFormat, @@ -123,10 +127,22 @@ pub fn probe_raster_image(bytes: &[u8]) -> Result png_metadata(bytes)?, - RasterImageFormat::Jpeg => jpeg_metadata(bytes)?, - RasterImageFormat::Gif | RasterImageFormat::WebP | RasterImageFormat::Bmp => { + let png_icon = if admitted == RasterImageFormat::Ico { + crate::ico::png_icon(bytes)? + } else { + None + }; + let (width, height, color_precision) = match (admitted, png_icon) { + (RasterImageFormat::Png, _) => png_metadata(bytes)?, + (RasterImageFormat::Jpeg, _) => jpeg_metadata(bytes)?, + (RasterImageFormat::Ico, Some(icon)) => icon.metadata()?, + ( + RasterImageFormat::Gif + | RasterImageFormat::WebP + | RasterImageFormat::Bmp + | RasterImageFormat::Ico, + _, + ) => { let mut reader = ImageReader::with_format(BufReader::new(Cursor::new(bytes)), format); reader.limits(decode_limits()); let mut decoder = reader.into_decoder().map_err(RasterDecodeError::Metadata)?; @@ -206,14 +222,27 @@ fn decode_raster( ) -> Result { check_encoded_budget(bytes)?; let convert_profile = matches!(conversion, ColorProfileConversion::Apply); - let (image, color_managed) = match metadata.format { - RasterImageFormat::Png => decode_png_colors(bytes, convert_profile)?, - RasterImageFormat::Jpeg => decode_jpeg_colors(bytes, convert_profile)?, - format @ (RasterImageFormat::Gif | RasterImageFormat::WebP | RasterImageFormat::Bmp) => { + let png_icon = if metadata.format == RasterImageFormat::Ico { + crate::ico::png_icon(bytes)? + } else { + None + }; + let (image, color_managed) = match (metadata.format, png_icon) { + (RasterImageFormat::Png, _) => decode_png_colors(bytes, convert_profile)?, + (RasterImageFormat::Jpeg, _) => decode_jpeg_colors(bytes, convert_profile)?, + (RasterImageFormat::Ico, Some(icon)) => icon.decode(convert_profile)?, + ( + format @ (RasterImageFormat::Gif + | RasterImageFormat::WebP + | RasterImageFormat::Bmp + | RasterImageFormat::Ico), + _, + ) => { let image_format = match format { RasterImageFormat::Gif => ImageFormat::Gif, RasterImageFormat::WebP => ImageFormat::WebP, RasterImageFormat::Bmp => ImageFormat::Bmp, + RasterImageFormat::Ico => ImageFormat::Ico, RasterImageFormat::Png | RasterImageFormat::Jpeg => unreachable!(), }; let mut reader = @@ -328,11 +357,10 @@ mod tests { probe_raster_image(b"not an image"), Err(RasterDecodeError::UnknownFormat) )); - // Minimal ICO signature is enough for format identification; the - // browser resource contract intentionally admits only its named set. + // Recognizing a format does not enable codecs outside the admitted set. assert!(matches!( - probe_raster_image(b"\0\0\x01\0\0\0\0\0\0\0\0\0\0\0\0\0"), - Err(RasterDecodeError::UnsupportedFormat(ImageFormat::Ico)) + probe_raster_image(b"II\x2a\0\0\0\0\0"), + Err(RasterDecodeError::UnsupportedFormat(ImageFormat::Tiff)) )); }