diff --git a/crates/metafile/src/lib.rs b/crates/metafile/src/lib.rs index 8cdbb957b1..9555a2d6ec 100644 --- a/crates/metafile/src/lib.rs +++ b/crates/metafile/src/lib.rs @@ -27,8 +27,8 @@ mod dib; mod emf; mod emfplus; mod geom; -mod place; pub mod pict; +mod place; mod size; mod text; mod wmf; diff --git a/crates/metafile/src/pict.rs b/crates/metafile/src/pict.rs index c310542724..e8bda77ba1 100644 --- a/crates/metafile/src/pict.rs +++ b/crates/metafile/src/pict.rs @@ -121,7 +121,9 @@ fn header(bytes: &[u8], at: usize) -> Option
{ /// The picture of a file (after its application data), else the picture `bytes` start with. fn find(bytes: &[u8]) -> Option
{ - header(bytes, FILE_HEADER).or_else(|| header(bytes, 0)) + let v2_at_512 = header(bytes, FILE_HEADER).filter(|h| h.v2); + let v2_at_0 = header(bytes, 0).filter(|h| h.v2); + v2_at_512.or(v2_at_0).or_else(|| header(bytes, FILE_HEADER)).or_else(|| header(bytes, 0)) } /// The size in points from the PICT header when valid. @@ -135,7 +137,7 @@ pub(crate) fn header_size(bytes: &[u8]) -> Option<(f32, f32)> { /// Do `bytes` hold a version 2 picture, after a file's application data or on their own? The /// version 1 signature is two bytes, too short to tell a file by. pub fn is_pict(bytes: &[u8]) -> bool { - [FILE_HEADER, 0].into_iter().any(|at| header(bytes, at).is_some_and(|h| h.v2)) + find(bytes).is_some_and(|h| h.v2) } #[derive(Clone, Copy)] @@ -321,7 +323,11 @@ fn bands(b: &mut Be, work: &mut u64) -> Option> { let mut active: Vec = vec![]; let mut last_band = 0; let mut prev_y = 0.0; - while let Some(y) = b.i16().filter(|y| *y != 0x7FFF) { + loop { + let y = b.i16()?; + if y == 0x7FFF { + break; + } let y = f64::from(y); let spans: Vec<(f64, f64)> = active.as_chunks::<2>().0.iter().map(|[a, b]| (f64::from(*a), f64::from(*b))).collect(); if !spans.is_empty() && y > prev_y { @@ -341,7 +347,11 @@ fn bands(b: &mut Be, work: &mut u64) -> Option> { } } prev_y = y; - while let Some(x) = b.i16().filter(|x| *x != 0x7FFF) { + loop { + let x = b.i16()?; + if x == 0x7FFF { + break; + } if !spend(work, active.len().saturating_add(1)) { return None; } @@ -419,7 +429,7 @@ impl PixMap { fn row_len(&self, w: usize) -> usize { match self.packing() { 4 if self.size == 32 && self.row_bytes >= 8 => w.saturating_mul(usize::from(self.cmps)), - 2 if self.size == 32 => w.saturating_mul(3), + 2 if self.size == 32 => (self.row_bytes / 4).saturating_mul(3), _ => self.row_bytes, } } @@ -427,7 +437,7 @@ impl PixMap { fn decodable(&self, w: usize) -> bool { let need = match self.size { 1 | 2 | 4 | 8 => (w.saturating_mul(usize::from(self.size))).div_ceil(8), - 16 => 2 * w, + 16 if matches!(self.packing(), 0 | 3) => 2 * w, 32 if self.packing() == 4 && self.row_bytes >= 8 => { if !matches!(self.cmps, 3 | 4) { return false; @@ -435,7 +445,7 @@ impl PixMap { w.saturating_mul(usize::from(self.cmps)) } 32 if self.packing() == 2 => 3 * w, - 32 => 4 * w, + 32 if matches!(self.packing(), 0 | 1) => 4 * w, _ => return false, }; (need..=need.saturating_add(ROW_SLACK)).contains(&self.row_len(w)) @@ -524,44 +534,51 @@ fn color_table(r: &mut Be) -> Option> { fn unpack(data: &[u8], len: usize, unit: usize, out: &mut Vec) -> bool { let start = out.len(); let mut i = 0; - let mut ok = true; while out.len().saturating_sub(start) < len { let Some(&n) = data.get(i) else { - ok = false; - break; + return false; }; i += 1; let n = n as i8; if n >= 0 { let k = (n as usize + 1).saturating_mul(unit); + if out.len().saturating_sub(start).saturating_add(k) > len { + return false; + } let Some(lit) = data.get(i..i + k) else { - ok = false; - break; + return false; }; out.extend_from_slice(lit); i += k; } else if n != -128 { + let count = (1 - isize::from(n)) as usize; + let total = count.saturating_mul(unit); + if out.len().saturating_sub(start).saturating_add(total) > len { + return false; + } let Some(v) = data.get(i..i + unit) else { - ok = false; - break; + return false; }; - for _ in 0..(1 - isize::from(n)) { + for _ in 0..count { out.extend_from_slice(v); } i += unit; } } - out.resize(start.saturating_add(len), 0); - ok + out.len().saturating_sub(start) == len } -fn crop(img: Rgba, src: Rect) -> Rgba { +fn crop(img: Rgba, src: Rect) -> Option<(Rgba, Rect)> { let (w, h) = (img.w, img.h); let s = Rect::new(src.x0.min(src.x1), src.y0.min(src.y1), src.x0.max(src.x1), src.y0.max(src.y1)); let clamp = |v: f64, max: usize| if v.is_finite() { v.round().clamp(0.0, max as f64) as usize } else { 0 }; let (x0, y0, x1, y1) = (clamp(s.x0, w), clamp(s.y0, h), clamp(s.x1, w), clamp(s.y1, h)); - if (x0, y0, x1, y1) == (0, 0, w, h) || x1 <= x0 || y1 <= y0 { - return img; + if x1 <= x0 || y1 <= y0 { + return None; + } + let in_bounds = Rect::new(x0 as f64, y0 as f64, x1 as f64, y1 as f64); + if (x0, y0, x1, y1) == (0, 0, w, h) { + return Some((img, in_bounds)); } let mut px = Vec::with_capacity((x1 - x0).saturating_mul(y1 - y0).saturating_mul(4)); for y in y0..y1 { @@ -570,7 +587,7 @@ fn crop(img: Rgba, src: Rect) -> Rgba { px.extend_from_slice(p); } } - Rgba { w: x1 - x0, h: y1 - y0, px } + Some((Rgba { w: x1 - x0, h: y1 - y0, px }, in_bounds)) } struct Pict { @@ -655,6 +672,9 @@ impl Pict { fn push_raw(&mut self, mut raw: Raw, keep_region: bool) { if self.items.len() >= MAX_ITEMS { + if matches!(raw, Raw::Bitmap { .. }) { + self.dropped_images = self.dropped_images.saturating_add(1); + } self.truncated = true; return; } @@ -679,6 +699,9 @@ impl Pict { } .saturating_add(region_cost); if self.used_bytes.saturating_add(cost) > MAX_BYTES { + if matches!(raw, Raw::Bitmap { .. }) { + self.dropped_images = self.dropped_images.saturating_add(1); + } self.truncated = true; return; } @@ -864,8 +887,13 @@ impl Pict { self.push_fill(bp, color, even_odd); } + fn scale_factor(&self) -> f64 { + let c = self.xf().as_coeffs(); + (c[0].abs() + c[3].abs()) / 2.0 + } + fn stroke(&mut self, mut bp: BezPath, color: [u8; 3], ends: u32) { - let width = (self.pen.x + self.pen.y) / 2.0; + let width = (self.pen.x + self.pen.y) / 2.0 * self.scale_factor(); let cap = match ends { FLAT => Cap::Flat, SQUARE => Cap::Square, @@ -985,9 +1013,24 @@ impl Pict { match verb { Verb::Frame if self.has_pen() => { let half = self.pen / 2.0; - let inner: Vec = rects.iter().map(|r| Rect::new(r.x0 + half.x, r.y0 + half.y, r.x1 - half.x, r.y1 - half.y)).collect(); let color = self.pen_color(); - self.stroke(rects_path(&inner), color, SQUARE); + if rects.len() == 1 { + let r = rects[0]; + let inner = Rect::new(r.x0 + half.x, r.y0 + half.y, r.x1 - half.x, r.y1 - half.y); + self.stroke(rects_path(&[inner]), color, SQUARE); + } else { + let outer_rects: Vec<[f64; 4]> = rects.iter().map(|r| [r.x0, r.y0, r.x1, r.y1]).collect(); + let inner_rects: Vec<[f64; 4]> = rects + .iter() + .filter_map(|r| { + let (x0, y0, x1, y1) = (r.x0 + half.x, r.y0 + half.y, r.x1 - half.x, r.y1 - half.y); + (x1 > x0 && y1 > y0).then_some([x0, y0, x1, y1]) + }) + .collect(); + let diff = clip::combine(Some(outer_rects), inner_rects, clip::RGN_DIFF, None).unwrap_or_default(); + let diff_rects: Vec = diff.into_iter().map(|[x0, y0, x1, y1]| Rect::new(x0, y0, x1, y1)).collect(); + self.fill(rects_path(&diff_rects), color, false); + } } Verb::Frame => {} Verb::Invert => { @@ -1065,12 +1108,16 @@ impl Pict { self.fill(bp, color, false); return Some(()); } - let width = (self.pen.x + self.pen.y) / 2.0; + let width = (self.pen.x + self.pen.y) / 2.0 * self.scale_factor(); let pen = (color, width); if self.pending_pen != Some(pen) { self.flush(); self.pending_pen = Some(pen); } + if self.items.len() >= MAX_ITEMS { + self.truncated = true; + return Some(()); + } let half = self.pen / 2.0; let xf = self.xf(); let (a, b) = (xf * (from + half), xf * (to + half)); @@ -1113,7 +1160,8 @@ impl Pict { charset: 0, }; let pt = self.xf() * self.text_loc; - let transform = [1.0, 0.0, 0.0, 1.0, pt.x, pt.y]; + let c = self.xf().as_coeffs(); + let transform = [c[0], c[1], c[2], c[3], pt.x, pt.y]; self.push(Raw::Text { text: Box::new(Text { transform, @@ -1178,13 +1226,24 @@ impl Pict { } fn image(&mut self, img: Rgba, src: Rect, dest: Rect, mask: Option>) { - let img = crop(img, src); - if img.w == 0 || img.h == 0 { + let Some((cropped, in_bounds)) = crop(img, src) else { + return; + }; + let (sw, sh) = (src.width(), src.height()); + if sw <= 0.0 || sh <= 0.0 { return; } - let (w, h) = (img.w as f64, img.h as f64); - let xf = Affine::translate(dest.origin().to_vec2()) * Affine::scale_non_uniform(dest.width() / w, dest.height() / h); - self.image_xf(img, xf, mask); + let kx = dest.width() / sw; + let ky = dest.height() / sh; + let sub_dest = Rect::new( + dest.x0 + (in_bounds.x0 - src.x0) * kx, + dest.y0 + (in_bounds.y0 - src.y0) * ky, + dest.x0 + (in_bounds.x1 - src.x0) * kx, + dest.y0 + (in_bounds.y1 - src.y0) * ky, + ); + let (w, h) = (cropped.w as f64, cropped.h as f64); + let xf = Affine::translate(sub_dest.origin().to_vec2()) * Affine::scale_non_uniform(sub_dest.width() / w, sub_dest.height() / h); + self.image_xf(cropped, xf, mask); } fn image_xf(&mut self, img: Rgba, xf: Affine, mask: Option>) { @@ -1228,15 +1287,7 @@ impl Pict { (RawClip { rects: self.clip.clone(), paths: Vec::new() }, false) }; - self.push_raw( - Raw::Bitmap { - rect, - xf: unit_xf.as_coeffs(), - img, - region, - }, - keep_region, - ); + self.push_raw(Raw::Bitmap { rect, xf: unit_xf.as_coeffs(), img, region }, keep_region); } fn transfer(&mut self, img: &mut Rgba, mode: u16, bitmap: bool) { @@ -1265,8 +1316,9 @@ impl Pict { match qt_image(data) { Some((ctype, xf, src, image)) if &ctype == b"jpeg" => match self.jpeg(image) { Some(img) => { - let img = crop(img, src); - self.image_xf(img, xf * Affine::translate(src.origin().to_vec2()), None); + if let Some((img, _)) = crop(img, src) { + self.image_xf(img, xf * Affine::translate(src.origin().to_vec2()), None); + } } None => self.dropped_images = self.dropped_images.saturating_add(1), }, @@ -1311,6 +1363,7 @@ fn qt_image(d: &[u8]) -> Option<([u8; 4], Affine, Rect, &[u8])> { /// Read a QuickDraw picture. pub fn parse(bytes: &[u8]) -> Result { + crate::dib::set_decode_budget(crate::dib::MAX_DECODE_PIXELS); let h = find(bytes).ok_or(Error::NotPict)?; let mut q = Pict::new(h.frame); let mut r = Be::new(bytes, h.ops); @@ -1334,10 +1387,8 @@ pub fn parse(bytes: &[u8]) -> Result { q.truncated = true; } q.flush(); - let pic = crate::build( - q.items, - Some(Frame { rect: [0.0, 0.0, h.frame.width(), h.frame.height()], size: Some((h.frame.width(), h.frame.height())) }), - ); + let pic = + crate::build(q.items, Some(Frame { rect: [0.0, 0.0, h.frame.width(), h.frame.height()], size: Some((h.frame.width(), h.frame.height())) })); Ok(Picture { skipped_records: q.skipped_records.saturating_add(pic.skipped_records), truncated: q.truncated || pic.truncated, diff --git a/crates/metafile/src/size.rs b/crates/metafile/src/size.rs index 8ed714ddeb..679f17e015 100644 --- a/crates/metafile/src/size.rs +++ b/crates/metafile/src/size.rs @@ -1,7 +1,7 @@ //! The size of a metafile picture without playing its records. use crate::bytes::Bytes; -use crate::{Error, emf, f, parse, parse_pict, pict, wmf}; +use crate::{Error, emf, f, parse, pict, wmf}; /// A placeable WMF header is 22 bytes, followed by the 9-word standard header. const PLACEABLE_KEY: u32 = 0x9AC6_CDD7; @@ -19,10 +19,6 @@ pub fn size(bytes: &[u8]) -> Result<(f32, f32), Error> { if let Some(size) = header_size(Bytes(bytes)) { return Ok(size); } - if pict::is_pict(bytes) { - let pic = parse_pict(bytes)?; - return Ok((pic.width, pic.height)); - } let pic = parse(bytes)?; Ok((pic.width, pic.height)) } @@ -33,8 +29,11 @@ pub(crate) fn header_size(b: Bytes) -> Option<(f32, f32)> { if emf::is_emf(b) { return emf_size(b); } - if wmf::is_wmf(b) && b.u32(0) == Some(PLACEABLE_KEY) { - return placeable_wmf_size(b); + if wmf::is_wmf(b) { + if b.u32(0) == Some(PLACEABLE_KEY) { + return placeable_wmf_size(b); + } + return None; } if let Some(size) = pict::header_size(b.0) { return Some(size); diff --git a/crates/metafile/tests/it/pict.rs b/crates/metafile/tests/it/pict.rs index 0d3ec122b3..42d128e126 100644 --- a/crates/metafile/tests/it/pict.rs +++ b/crates/metafile/tests/it/pict.rs @@ -229,7 +229,7 @@ fn direct_pixel_maps_and_masked_bitmaps() { let row = vec![7, 5, 255, 0, 0, 255, 0, 0]; let direct = [be(&[0, 0xFF, 0x8008u16 as i16]), rect(0, 0, 1, 2), pixmap_fields(4, 32, 3), rect(0, 0, 1, 2), rect(0, 0, 1, 2), be(&[0]), row].concat(); - let mask = [be(&[10]), rect(0, 0, 1, 1)].concat(); + let mask = [be(&[10]), rect(5, 0, 6, 1)].concat(); let bitmap = [be(&[2]), rect(0, 0, 1, 2), rect(0, 0, 1, 2), rect(5, 0, 6, 2), be(&[1]), mask, vec![0x80, 0]].concat(); let p = parse_pict(&v2([0, 0, 10, 10], &[(0x9A, direct), (0x91, bitmap)])).unwrap(); assert_eq!(p.items.len(), 2); @@ -239,7 +239,7 @@ fn direct_pixel_maps_and_masked_bitmaps() { let Item::Bitmap { rgba: rgba1, region: Some(r), .. } = &p.items[1] else { panic!("expected bitmap 1 with region") }; assert_eq!(rgba1[0..4], [0, 0, 0, 255]); assert_eq!(rgba1[7], 0, "the clear bit is left out"); - assert!(!r.is_empty(), "mask region clips the bitmap"); + assert_eq!(r, &vec![[0.0, 5.0, 1.0, 6.0]]); } #[test] @@ -248,12 +248,12 @@ fn rows_far_longer_than_their_pixels_are_refused() { let mut row = be(&[256]); (0..127).for_each(|_| row.extend([0x81, 1])); row.extend([0x82, 1]); - let rows = row.repeat(4); - let map = [be(&[0xBFFFu16 as i16]), rect(0, 0, 4, 1), pixmap_fields(0, 8, 1), table.clone()].concat(); - let data = [map, rect(0, 0, 4, 1), rect(0, 0, 4, 1), be(&[0]), rows].concat(); - let p = parse_pict(&v2([0, 0, 10, 10], &[(0x98, data)])).unwrap(); + let map = [be(&[0xBFFFu16 as i16]), rect(0, 0, 1, 1), pixmap_fields(0, 8, 1), table.clone()].concat(); + let data = [map, rect(0, 0, 1, 1), rect(0, 0, 1, 1), be(&[0]), row.clone()].concat(); + let p = parse_pict(&v2([0, 0, 10, 10], &[(0x98, data), (0x31, rect(0, 0, 5, 5))])).unwrap(); assert_eq!(p.dropped_images, 1); - assert!(p.items.is_empty()); + assert_eq!(p.items.len(), 1); + assert!(!p.truncated); let pattern = [be(&[1]), vec![0xFF; 8], be(&[0xBFFFu16 as i16]), rect(0, 0, 1, 1), pixmap_fields(0, 8, 1), table, row].concat(); let p = parse_pict(&v2([0, 0, 10, 10], &[(0x12, pattern), (0x32, rect(0, 0, 5, 5))])).unwrap(); @@ -371,6 +371,9 @@ fn a_picture_that_never_ends_stops_at_the_opcode_limit() { let mut b = v2([0, 0, 50, 50], &[(0x31, rect(0, 0, 10, 10))]); b.truncate(b.len() - 2); b.resize(b.len() + 2 * ((1 << 22) + 1), 0); + b.extend([0x00, 0x31]); + b.extend(rect(20, 20, 30, 30)); + b.extend([0x00, 0xFF]); let p = parse_pict(&b).unwrap(); assert_eq!(p.items.len(), 1); assert!(p.truncated);