mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 16:59:56 +01:00
Merge pull request #3184 from ruchirguitar/feat/pict-reader
craft-metafile: distinguish opcode budget exhaustion from premature EOF in PICT reader
This commit is contained in:
4 files changed
+126
-8
No files matched your search
@@ -48,7 +48,7 @@ use bytes::Bytes;
|
||||
use canvas::Raw;
|
||||
pub use eof::ends_early;
|
||||
use geom::{Op, P};
|
||||
pub use pict::{is_pict, parse as parse_pict};
|
||||
pub use pict::{TruncationReason, is_pict, parse as parse_pict};
|
||||
pub use place::{MAX_BITMAP_SIDE, PlacedClip, PlacedItem, bez_path, crop_window};
|
||||
pub use size::size;
|
||||
pub use text::{Font, HAlign, SYMBOL_CHARSET, Text, VAlign};
|
||||
@@ -86,6 +86,8 @@ pub struct Picture {
|
||||
/// the picture holds what came before. The limits that cut one path, string, region or dash
|
||||
/// pattern short do not set it.
|
||||
pub truncated: bool,
|
||||
/// Granular reason why decoding halted early, distinguishing premature EOF from safety budget exhaustion.
|
||||
pub truncation_reason: Option<TruncationReason>,
|
||||
/// Images the file asks to draw that the reader did not read: bitmaps past a size, item, byte or
|
||||
/// decode limit or in a form the reader cannot decode, EMF+ metafile images that do not parse or
|
||||
/// are nested deeper than 3 levels, and pattern or texture brushes whose bitmap was not read, a
|
||||
|
||||
@@ -16,10 +16,17 @@ use crate::geom::Op;
|
||||
use crate::text::{Font, HAlign, Text, VAlign};
|
||||
use crate::{Cap, Error, Frame, Join, LineStyle, MAX_BYTES, MAX_ITEMS, MAX_PIXELS, Picture, clip, dib, f};
|
||||
|
||||
/// Why a picture's decode stopped early.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TruncationReason {
|
||||
PrematureEof,
|
||||
OpcodeLimitExceeded,
|
||||
}
|
||||
|
||||
/// The application data before the picture in a PICT file.
|
||||
const FILE_HEADER: usize = 512;
|
||||
/// Most opcodes one picture plays (a small file can't keep the reader busy for minutes).
|
||||
const MAX_OPS: usize = 1 << 22;
|
||||
pub const MAX_OPS: usize = 1 << 22;
|
||||
/// Most rectangles one region breaks into.
|
||||
const MAX_RECTS: usize = 1 << 16;
|
||||
/// Steps the regions of one picture may take to read (each inversion point and band costs some).
|
||||
@@ -43,15 +50,22 @@ const FLAT: u32 = 0x200;
|
||||
struct Be<'a> {
|
||||
data: &'a [u8],
|
||||
pos: usize,
|
||||
eof: bool,
|
||||
}
|
||||
|
||||
impl<'a> Be<'a> {
|
||||
fn new(data: &'a [u8], pos: usize) -> Self {
|
||||
Self { data, pos }
|
||||
Self { data, pos, eof: false }
|
||||
}
|
||||
fn bytes(&mut self, n: usize) -> Option<&'a [u8]> {
|
||||
let end = self.pos.checked_add(n)?;
|
||||
let b = self.data.get(self.pos..end)?;
|
||||
let b = match self.data.get(self.pos..end) {
|
||||
Some(b) => b,
|
||||
None => {
|
||||
self.eof = true;
|
||||
return None;
|
||||
}
|
||||
};
|
||||
self.pos = end;
|
||||
Some(b)
|
||||
}
|
||||
@@ -65,6 +79,7 @@ impl<'a> Be<'a> {
|
||||
self.pos = end;
|
||||
Some(())
|
||||
} else {
|
||||
self.eof = true;
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -624,6 +639,7 @@ struct Pict {
|
||||
pending_pen: Option<([u8; 3], f64)>,
|
||||
skipped_records: u32,
|
||||
truncated: bool,
|
||||
truncation_reason: Option<TruncationReason>,
|
||||
dropped_images: u32,
|
||||
ignored_clips: u32,
|
||||
unplayed_raster_ops: u32,
|
||||
@@ -661,6 +677,7 @@ impl Pict {
|
||||
pending_pen: None,
|
||||
skipped_records: 0,
|
||||
truncated: false,
|
||||
truncation_reason: None,
|
||||
dropped_images: 0,
|
||||
ignored_clips: 0,
|
||||
unplayed_raster_ops: 0,
|
||||
@@ -1448,24 +1465,41 @@ pub(crate) fn parse_nested(bytes: &[u8]) -> Result<Picture, Error> {
|
||||
let h = find(bytes).ok_or(Error::NotPict)?;
|
||||
let mut q = Pict::new(h.frame);
|
||||
let mut r = Be::new(bytes, h.ops);
|
||||
let mut stopped_early = true;
|
||||
let mut reached_end = false;
|
||||
let mut halted_for_record_error = false;
|
||||
let mut reason = None;
|
||||
for _ in 0..MAX_OPS {
|
||||
if h.v2 && r.pos % 2 == 1 && r.skip(1).is_none() {
|
||||
reason = Some(TruncationReason::PrematureEof);
|
||||
break;
|
||||
}
|
||||
let Some(op) = (if h.v2 { r.u16() } else { r.u8().map(u16::from) }) else {
|
||||
reason = Some(TruncationReason::PrematureEof);
|
||||
break;
|
||||
};
|
||||
if op == 0x00FF {
|
||||
stopped_early = false;
|
||||
reached_end = true;
|
||||
break;
|
||||
}
|
||||
if q.op(op, &mut r).is_none() {
|
||||
if r.eof {
|
||||
reason = Some(TruncationReason::PrematureEof);
|
||||
} else {
|
||||
halted_for_record_error = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if r.eof {
|
||||
reason = Some(TruncationReason::PrematureEof);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if stopped_early {
|
||||
if !reached_end && !halted_for_record_error && reason.is_none() {
|
||||
reason = Some(TruncationReason::OpcodeLimitExceeded);
|
||||
}
|
||||
if !reached_end {
|
||||
q.truncated = true;
|
||||
q.truncation_reason = reason;
|
||||
}
|
||||
q.flush();
|
||||
let pic =
|
||||
@@ -1473,6 +1507,7 @@ pub(crate) fn parse_nested(bytes: &[u8]) -> Result<Picture, Error> {
|
||||
Ok(Picture {
|
||||
skipped_records: q.skipped_records.saturating_add(pic.skipped_records),
|
||||
truncated: q.truncated || pic.truncated,
|
||||
truncation_reason: q.truncation_reason.or(pic.truncation_reason),
|
||||
dropped_images: q.dropped_images.saturating_add(pic.dropped_images),
|
||||
ignored_clips: q.ignored_clips.saturating_add(pic.ignored_clips),
|
||||
unplayed_raster_ops: q.unplayed_raster_ops.saturating_add(pic.unplayed_raster_ops),
|
||||
|
||||
@@ -194,3 +194,79 @@ fn masking_a_bitmap_to_a_clip_stops_at_the_work_cap() {
|
||||
assert!(side(fits));
|
||||
assert!(!side(fits + 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pict_truncated_halfway_through_rectangle_reports_premature_eof() {
|
||||
let mut pict = vec![0xA5; 512];
|
||||
pict.extend([0, 0]);
|
||||
pict.extend([0, 0, 0, 0, 0, 10, 0, 10]);
|
||||
pict.extend([0x00, 0x11, 0x02, 0xFF, 0x0C, 0x00]);
|
||||
pict.extend([-1i16, 0].iter().flat_map(|x| x.to_be_bytes()));
|
||||
pict.extend([0; 20]);
|
||||
// Truncated halfway through a rectangle opcode (paintRect 0x0031):
|
||||
// Rectangle opcode expects 8 bytes [top, left, bottom, right], but only 4 bytes are provided.
|
||||
pict.extend([0x00, 0x31]);
|
||||
pict.extend([0, 0, 0, 5]);
|
||||
|
||||
let p = crate::parse(&pict).unwrap();
|
||||
assert!(p.truncated);
|
||||
assert_eq!(p.truncation_reason, Some(crate::TruncationReason::PrematureEof));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pict_records_exceeding_max_opcode_budget_reports_opcode_limit_exceeded() {
|
||||
let mut pict = vec![0xA5; 512];
|
||||
pict.extend([0, 0]);
|
||||
pict.extend([0, 0, 0, 0, 0, 10, 0, 10]);
|
||||
pict.extend([0x00, 0x11, 0x02, 0xFF, 0x0C, 0x00]);
|
||||
pict.extend([-1i16, 0].iter().flat_map(|x| x.to_be_bytes()));
|
||||
pict.extend([0; 20]);
|
||||
// Valid records exceeding MAX_OPS budget (e.g. NOP opcodes 0x0000):
|
||||
pict.resize(pict.len() + 2 * (crate::pict::MAX_OPS + 1), 0);
|
||||
pict.extend([0x00, 0xFF]);
|
||||
|
||||
let p = crate::parse(&pict).unwrap();
|
||||
assert!(p.truncated);
|
||||
assert_eq!(p.truncation_reason, Some(crate::TruncationReason::OpcodeLimitExceeded));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pict_invalid_zero_height_bitmap_does_not_report_premature_eof() {
|
||||
let mut pict = vec![0xA5; 512];
|
||||
pict.extend([0, 0]);
|
||||
pict.extend([0, 0, 0, 0, 0, 10, 0, 10]);
|
||||
pict.extend([0x00, 0x11, 0x02, 0xFF, 0x0C, 0x00]);
|
||||
pict.extend([-1i16, 0].iter().flat_map(|x| x.to_be_bytes()));
|
||||
pict.extend([0; 20]);
|
||||
// Complete BitsRect (0x0090) with zero-height bounds, followed by EndPic (0x00FF):
|
||||
pict.extend([0x00, 0x90]);
|
||||
pict.extend([0x00, 0x02]); // row_bytes
|
||||
pict.extend([0, 0, 0, 0, 0, 0, 0, 10]); // bounds (top=0, left=0, bottom=0, right=10; height=0)
|
||||
pict.extend([0, 0, 0, 0, 0, 0, 0, 10]); // src
|
||||
pict.extend([0, 0, 0, 0, 0, 0, 0, 10]); // dst
|
||||
pict.extend([0x00, 0x00]); // mode
|
||||
pict.extend([0x00, 0xFF]); // EndPic
|
||||
|
||||
let p = crate::parse(&pict).unwrap();
|
||||
assert!(p.truncated);
|
||||
assert_ne!(p.truncation_reason, Some(crate::TruncationReason::PrematureEof));
|
||||
assert_eq!(p.truncation_reason, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pict_truncated_region_payload_stops_with_premature_eof() {
|
||||
let mut pict = vec![0xA5; 512];
|
||||
pict.extend([0, 0]);
|
||||
pict.extend([0, 0, 0, 0, 0, 10, 0, 10]);
|
||||
pict.extend([0x00, 0x11, 0x02, 0xFF, 0x0C, 0x00]);
|
||||
pict.extend([-1i16, 0].iter().flat_map(|x| x.to_be_bytes()));
|
||||
pict.extend([0; 20]);
|
||||
// Opcode 0x0080 (frameRgn) claiming 16 bytes for region, followed by EOF before body finishes:
|
||||
pict.extend([0x00, 0x80]);
|
||||
pict.extend([0x00, 0x10]);
|
||||
pict.extend([0x00, 0xFF]);
|
||||
|
||||
let p = crate::parse(&pict).unwrap();
|
||||
assert!(p.truncated);
|
||||
assert_eq!(p.truncation_reason, Some(crate::TruncationReason::PrematureEof));
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
use kurbo::{Rect, Shape};
|
||||
|
||||
use craft_metafile::{Item, bez_path, parse_pict};
|
||||
use craft_metafile::{Item, TruncationReason, bez_path, parse_pict};
|
||||
|
||||
/// Big-endian words.
|
||||
fn be(v: &[i16]) -> Vec<u8> {
|
||||
@@ -355,13 +355,17 @@ fn pictures_cut_short_keep_what_came_before() {
|
||||
let p = parse_pict(cut).unwrap();
|
||||
assert_eq!(p.items.len(), 1);
|
||||
assert!(p.truncated);
|
||||
assert_eq!(p.truncation_reason, Some(TruncationReason::PrematureEof));
|
||||
|
||||
let p = parse_pict(&v2([0, 0, 50, 50], &[(0x31, rect(0, 0, 10, 10)), (0xA1, be(&[100, 5000]))])).unwrap();
|
||||
assert_eq!(p.items.len(), 1);
|
||||
assert!(p.truncated);
|
||||
assert_eq!(p.truncation_reason, Some(TruncationReason::PrematureEof));
|
||||
|
||||
let p = parse_pict(&v2([0, 0, 50, 50], &[(0xA0, be(&[100]))])).unwrap();
|
||||
assert!(p.items.is_empty());
|
||||
assert!(!p.truncated);
|
||||
assert_eq!(p.truncation_reason, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -389,6 +393,7 @@ fn a_picture_that_never_ends_stops_at_the_opcode_limit() {
|
||||
let p = parse_pict(&b).unwrap();
|
||||
assert_eq!(p.items.len(), 1);
|
||||
assert!(p.truncated);
|
||||
assert_eq!(p.truncation_reason, Some(TruncationReason::OpcodeLimitExceeded));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user