From b3a8bf7df902bf07621809f479a25993f63ecdd1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 21:55:28 +0000 Subject: [PATCH] fix(codecs): use probe_size, Limits::none, and u64 max_alloc --- crates/cad/src/scene.rs | 3 +-- crates/doc/src/cmyk.rs | 4 ++-- crates/doc/src/links.rs | 8 +++----- crates/doc/src/placed_document.rs | 2 +- crates/pdf/src/export/images.rs | 4 ++-- crates/pdf/src/images.rs | 6 +++--- 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/crates/cad/src/scene.rs b/crates/cad/src/scene.rs index b8da4046ef..28d05e04f9 100644 --- a/crates/cad/src/scene.rs +++ b/crates/cad/src/scene.rs @@ -1406,8 +1406,7 @@ impl<'a> Scene<'a> { use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image as CodecImage, SampleType}; let blob = self.doc.image_blob(im).filter(|b| !b.bytes.is_empty()).ok_or_else(|| "an image has no pixels and was left out".to_string())?; // The pixels it converts to (RGBA, 4 bytes each) are read from its header before decoding. - let info = craft_image_codecs::probe(&blob.bytes).map_err(|_| UNDECODED.to_string())?; - let (w, h) = (info.width, info.height); + let (w, h) = craft_image_codecs::probe_size(&blob.bytes).map_err(|_| UNDECODED.to_string())?; if u64::from(w) * u64::from(h) > MAX_IMAGE_PIXELS { return Err("an image is too large for DXF and was left out".to_string()); } diff --git a/crates/doc/src/cmyk.rs b/crates/doc/src/cmyk.rs index 696c7063e5..228377542a 100644 --- a/crates/doc/src/cmyk.rs +++ b/crates/doc/src/cmyk.rs @@ -276,7 +276,7 @@ enum SampleSlice<'a> { fn decode_cmyk_tiff_samples(bytes: &[u8], info: &craft_tiff::image::ImageInfo) -> Option { let _ = is_cmyk_tiff_info(info)?; let _ = pixels(info.width, info.height)?; - let opts = craft_image_codecs::DecodeOptions { limits: CodecLimits::unlimited(), keep_orientation: true }; + let opts = craft_image_codecs::DecodeOptions { limits: CodecLimits::none(), keep_orientation: true }; let img = craft_image_codecs::decode_as_with(Format::Tiff, bytes, &opts).ok()?; if img.layout != ChannelLayout::Cmyk || img.width != info.width || img.height != info.height { return None; @@ -616,7 +616,7 @@ pub fn extra_samples_tiff_rgba(bytes: &[u8], rgb: impl Fn([f32; 4]) -> [f32; 3]) let n = usize::try_from(pixels(w, h)?).ok()?; // ExtraSamples 1: associated alpha (premultiplied colour), 2: unassociated alpha, 0: not alpha. let extra = ifd.u64s(craft_tiff::tags::EXTRA_SAMPLES).and_then(|v| v.first().copied()).and_then(|v| u16::try_from(v).ok()); - let opts = craft_image_codecs::DecodeOptions { limits: CodecLimits::unlimited(), keep_orientation: true }; + let opts = craft_image_codecs::DecodeOptions { limits: CodecLimits::none(), keep_orientation: true }; let img = craft_image_codecs::decode_as_with(Format::Tiff, bytes, &opts).ok()?; let (samples, colour, alpha) = match img.layout { ChannelLayout::CmykA if is_cmyka => (5, 4, true), diff --git a/crates/doc/src/links.rs b/crates/doc/src/links.rs index 2d9c31a910..abc90c2fb8 100644 --- a/crates/doc/src/links.rs +++ b/crates/doc/src/links.rs @@ -156,8 +156,7 @@ impl ImageBlob { /// decoded none (an image past the allowance). pub fn checked_with_pixels(mut self, proxy: bool) -> Option<(Self, Option)> { let max = crate::MAX_RASTER_ALLOC; - let info = craft_image_codecs::probe(&self.bytes).ok()?; - let (w, h) = (info.width, info.height); + let (w, h) = craft_image_codecs::probe_size(&self.bytes).ok()?; if u64::from(w) * u64::from(h) > max / 4 { return Some((self, None)); } @@ -214,7 +213,7 @@ pub fn missing_placeholder() -> Option> { fn decode_dynamic(bytes: &[u8], max_alloc: u64) -> Result { use craft_image_codecs::{ChannelLayout, CodecError, DecodeOptions, Limits, SampleType}; - let opts = DecodeOptions { limits: Limits { max_alloc: Some(max_alloc), ..Limits::default() }, ..DecodeOptions::default() }; + let opts = DecodeOptions { limits: Limits { max_alloc, ..Limits::default() }, ..DecodeOptions::default() }; let img = craft_image_codecs::decode_with(bytes, &opts).map_err(|e| matches!(e, CodecError::LimitExceeded(_)))?; let (w, h) = img.dimensions(); match (img.layout(), img.sample_type()) { @@ -234,8 +233,7 @@ fn decode_dynamic(bytes: &[u8], max_alloc: u64) -> Result Option> { - let info = craft_image_codecs::probe(bytes).ok()?; - let (w, h) = (info.width, info.height); + let (w, h) = craft_image_codecs::probe_size(bytes).ok()?; if w.max(h) <= PROXY_SIZE { return None; } diff --git a/crates/doc/src/placed_document.rs b/crates/doc/src/placed_document.rs index d762576120..d32b6500a1 100644 --- a/crates/doc/src/placed_document.rs +++ b/crates/doc/src/placed_document.rs @@ -347,7 +347,7 @@ pub fn full_bytes(p: &PlacedDocument, blob: &ImageBlob) -> Option>> /// The size in pixels of image bytes `bytes` (a preview), read from its header. pub fn image_size(bytes: &[u8]) -> Option<(u32, u32)> { - craft_image_codecs::probe(bytes).ok().map(|i| (i.width, i.height)) + craft_image_codecs::probe_size(bytes).ok() } // ---------- reading files ---------- diff --git a/crates/pdf/src/export/images.rs b/crates/pdf/src/export/images.rs index ad0582f9a4..9ab8ce0677 100644 --- a/crates/pdf/src/export/images.rs +++ b/crates/pdf/src/export/images.rs @@ -318,8 +318,8 @@ pub(super) fn png_8_bit(bytes: &[u8]) -> Option> { pub(super) fn embed(blob: &vectorcraft_doc::ImageBlob, interpolate: bool, sixteen_bit: bool) -> Option { // The header's size first: the writer's decoders allocate what it declares, and the encoded // bytes of an image that is far larger than any the app reads may be a few kilobytes. - let info = craft_image_codecs::probe(&blob.bytes).ok(); - if info.as_ref().is_some_and(|i| u64::from(i.width) * u64::from(i.height) > crate::images::MAX_EMBED_PIXELS) { + let dims = craft_image_codecs::probe_size(&blob.bytes).ok(); + if dims.is_some_and(|(w, h)| u64::from(w) * u64::from(h) > crate::images::MAX_EMBED_PIXELS) { return None; } let bytes = blob.bytes.as_ref().clone(); diff --git a/crates/pdf/src/images.rs b/crates/pdf/src/images.rs index 87db970405..7c4a92ec57 100644 --- a/crates/pdf/src/images.rs +++ b/crates/pdf/src/images.rs @@ -417,7 +417,7 @@ pub(crate) fn recode_cmyk( let jpeg = jpeg_components(&blob.bytes).is_some(); let icc = icc.filter(|_| convert.is_none()); if jpeg && convert.is_none() && alpha.is_none() { - let dims = craft_image_codecs::probe(&blob.bytes).ok().map(|i| (i.width, i.height)); + let dims = craft_image_codecs::probe_size(&blob.bytes).ok(); if let Some((w, h)) = dims.filter(|d| kept(&rule, *d, size, true)) { // The JPEG data as it is, tagged with `icc` rather than with its own segments; one // larger than [`MAX_EMBED_PIXELS`] is not written. @@ -511,8 +511,8 @@ pub(crate) fn mask_alpha(blob: &ImageBlob, width: u32, height: u32, clip: bool) } // The size from the header first: a mask of another size, or too large to hold as RGBA, is // not decoded at all. - let info = craft_image_codecs::probe(&blob.bytes).ok()?; - if (info.width, info.height) != (width, height) || u64::from(width) * u64::from(height) * 4 > MAX_DECODE { + let (w, h) = craft_image_codecs::probe_size(&blob.bytes).ok()?; + if (w, h) != (width, height) || u64::from(width) * u64::from(height) * 4 > MAX_DECODE { return None; } let img = decoded(&blob.bytes)?.into_rgba8();