diff --git a/crates/cdr/PROVENANCE.md b/crates/cdr/PROVENANCE.md index c7cc745be9..62706eb596 100644 --- a/crates/cdr/PROVENANCE.md +++ b/crates/cdr/PROVENANCE.md @@ -114,7 +114,7 @@ file is committed. | R50 | Bit 5 (0x20) of the outline's line type scales the outline's width and dashes with the shape's matrix. The engine multiplies them by the square root of the matrix's area factor (VectorCraft's own value for a matrix that scales x and y differently) | S: UC `outl_scale_with_cdr7.cdr` to `outl_scale_with_cdr12.cdr`, of whose outlines only one, 3.969 mm wide, has line type 0x22 and the others 0x02 or 0x01; TDF 76771 (version 15), whose outline of line type 0x22 is 10 mm wide on a shape that the matrix stretches and skews, and whose picture draws that outline several times wider and skewed | sample (the bit; the single width for a matrix that scales x and y differently: VectorCraft's own) | | R51 | A `trfd` offsets table holds one offset more than the entries; each entry runs from its offset to the next, and the last offset is the record's end | KF L1079, L1113-L1121; S: every `trfd` of versions 4 to 25 in the sample files (25,425 records) has the extra offset, equal to the record's length | public-spec | | R52 | Byte 0 of argument 14001 is the fill's winding flag. VectorCraft reads a byte other than 0 as the nonzero fill rule, and 0 or an object without the argument as even-odd | KF L575-L583, L1047; S: the argument is 4 bytes in the sample files that carry it, with byte 0 = 1 on LT `complexshape_blackfill_nooutline` of `testset/12` and `testset/15` to `testset/25` and on objects of TDF 127576 (13) and 60026 (14), and 0 on 27 objects of TP `example.cdr` (17) | public-spec (the flag: KF; which rule each value means, and the rule without the argument: VectorCraft's own reading, and a wrong reading fills or leaves holes where a curve's subpaths overlap) | -| R53 | A `.cmx` file is a RIFF form of type `CMX1`, little-endian (a `RIFX` form is big-endian and is refused); its chunks are laid out as R4. A chunk can run short of its contents: when the bytes after it don't read as a chunk id (an ASCII letter, then three ASCII letters, digits or spaces), the next id is the first at an even distance of 2 to 64 bytes that names a chunk the reader uses or whose length keeps it inside its list. When that chunk is one the reader passes over or a `page`, the reader may resume instead at a `page` chunk that starts 8 to 64 bytes after it, inside its body, is not empty, ends inside the list and, when the chunk found ends inside the list, reaches its end. It does so only when that page reads with objects and without damage in its instructions, and when walking on from the chunk found and from the page, each without this recovery and entering lists as the reader does, reaches the same position inside the same lists, or leaves the list (at its end, or at stray bytes with no chunk id after them), within 16 steps, with every chunk the reader uses that the first walk reads before then also read by the second (pages that read without objects aside). Of the chunks that only the second walk reads, the header or a table is used only when no other chunk of its id is read, and a page is kept only when it reads with objects and without damage in its instructions. Letters spelling `page` whose length runs past the list are checked the same way. In one read, the search for pages to resume at spends at most four times the length of the RIFF form (cut at the file's length), or 1,048,576 when that is more, counting 64 for each step of a walk and the length of each page body judged; past that, the reader resumes at the first chunk id found. Pages kept this way are read after the other pages, so they spend only what the limits leave, and keep their place in the file; each is judged once more before it is read. One that a limit stops, or leaves without objects, is left out with the warning for limits; one the limits cut short is read as far as they allow, as other pages are. When the chunks that only these recoveries read make the read stop at a limit, the file is read again without the recoveries, with the warning for limits. Resuming at an inner page, at a chunk the reader passes over that is not followed by another chunk id or by the list's end, or after letters whose length runs past the list is reported as damage (this reader's own rule) | CN "Container"; S: all 14 CX files are `RIFF` forms of type `CMX1`; in `balloon.cmx`, `bars.cmx`, `bluecros.cmx`, `bluedrop.cmx`, `gold_bar.cmx`, `goldblue.cmx`, `squares.cmx` and `thatch.cmx` 16 bytes follow `ccmm` and 4 follow `rlst` before the next id | sample | +| R53 | A `.cmx` file is a RIFF form of type `CMX1`, little-endian (a `RIFX` form is big-endian and is refused); its chunks are laid out as R4. A chunk can run short of its contents: when the bytes after it don't read as a chunk id (an ASCII letter, then three ASCII letters, digits or spaces), the next id is the first at an even distance of 2 to 64 bytes that names a chunk the reader uses or whose length keeps it inside its list. When that chunk is one the reader passes over or a `page`, the reader may resume instead at a `page` chunk that starts 8 to 64 bytes after it, inside its body, is not empty, ends inside the list and, when the chunk found ends inside the list, reaches its end. It does so only when that page reads with objects and without damage in its instructions, and when walking on from the chunk found and from the page, each without this recovery and entering lists as the reader does, reaches the same position inside the same lists, or leaves the list (at its end, or at stray bytes with no chunk id after them), within 16 steps, with every chunk the reader uses that the first walk reads before then also read by the second, with its body cut at the same place (pages that read without objects aside). Of the chunks that only the second walk reads, the header or a table is used only when no other chunk of its id is read, and a page is kept only when it reads with objects and without damage in its instructions. Letters spelling `page` whose length runs past the list are checked the same way. In one read, the search for pages to resume at spends at most four times the length of the RIFF form (cut at the file's length), or 1,048,576 when that is more, counting 64 for each step of a walk and the length of each page body judged; past that, the reader resumes at the first chunk id found. Pages kept this way are read after the other pages, so they spend only what the limits leave, and keep their place in the file; each is judged once more before it is read. One that a limit stops, or leaves without objects, is left out with the warning for limits; one the limits cut short is read as far as they allow, as other pages are. When the chunks that only these recoveries read make the read stop at a limit, the file is read again without the recoveries, with the warning for limits. Resuming at an inner page, at a chunk the reader passes over that is not followed by another chunk id or by the list's end, or after letters whose length runs past the list is reported as damage (this reader's own rule) | CN "Container"; S: all 14 CX files are `RIFF` forms of type `CMX1`; in `balloon.cmx`, `bars.cmx`, `bluecros.cmx`, `bluedrop.cmx`, `gold_bar.cmx`, `goldblue.cmx`, `squares.cmx` and `thatch.cmx` 16 bytes follow `ccmm` and 4 follow `rlst` before the next id | sample | | R54 | `cont`: byte 52 is the coordinate size as an ASCII digit, `4` for 32-bit coordinates and `2` for 16-bit ones (refused); bytes 64 to 72 hold an f64, the size of a coordinate unit in meters. A value that isn't above 0 and at most 1 reads as 0.1 µm (VectorCraft's own fallback) | CN "Container"; S: the nine CX files with `4` all hold 1e-7; `abydos.cmx`, `CM33.CMX`, `LIZARDS.CMX`, `output.000.cmx` and `test.cmx` hold `2` | sample | | R55 | A table (`rclr`, `rpen`, `rott`, `rdot`, `rotl`) is a u16 entry count, then one field list per entry; an instruction's body is field lists too. A field is a u8 tag, a u16 length counting its 3 header bytes, and the data; 0xff ends the list. Objects name table entries from 1 | CN "Tables"; S: every one of these tables in the nine CX files with 32-bit coordinates reads its count of entries with no bytes left | sample | | R56 | A color entry: a property list in field 10 (a u32 length, then parts laid out as R11's, the color in part 1 as R14), or in older files the model and palette bytes in field 1 and the values in field 2. Models of the older fields: 2 cyan, magenta, yellow and black in percent; 3 the same in 0 to 255; 4 cyan, magenta and yellow in 0 to 255; 5 red, green and blue in 0 to 255; 9 gray in 0 to 255, 255 white. Other models read as black with a warning | CN "Tables" (`rclr`); S: every color of the nine CX files is model 2 in the older fields | sample (model 2), public-notes (field 10), unconfirmed (models 3 and 5 of the older fields, read as the program of CN reads them, and models 4 and 9, read as R14's; a wrong reading gives a wrong color) | diff --git a/crates/cdr/src/cmx.rs b/crates/cdr/src/cmx.rs index 2b167e3b2c..ef085c8a96 100644 --- a/crates/cdr/src/cmx.rs +++ b/crates/cdr/src/cmx.rs @@ -314,7 +314,7 @@ fn inner_page(b: &[u8], p: usize, end: usize, depth: usize, budget: &Budget, lef let first = walk(b, p, end, lists, left)?; found.find_map(|k| { let alt = walk(b, k, end, lists, left)?; - rejoins(b, end, &first, &alt, budget, left).map(|extra| (k, extra)) + rejoins(b, &first, &alt, budget, left).map(|extra| (k, extra)) }) } @@ -324,8 +324,9 @@ struct Walk { /// Where the walk starts, and its place after each step: the position, and for each `LIST` /// chunk it is inside, where that list's body ends and where the reader goes on after it. at: Vec<(usize, Vec<(usize, usize)>)>, - /// Where each chunk the walk read starts; a `LIST` chunk it enters counts by the chunks inside. - read: Vec, + /// Where each chunk the walk read starts, and where its body ends, cut at the end of the list + /// that holds it; a `LIST` chunk it enters counts by the chunks inside. + read: Vec<(usize, usize)>, /// The walk left the list it started in: at its end, or at stray bytes with no chunk id after /// them, where [`chunks`] stops reading that list too. ended: bool, @@ -349,7 +350,7 @@ fn walk(b: &[u8], from: usize, end: usize, lists: usize, left: &mut usize) -> Op pos = chunk + 12; } Some((next, chunk)) => { - w.read.extend(chunk); + w.read.extend(chunk.map(|c| (c, chunk_stop(b, c).map_or(list_end, |stop| stop.min(list_end))))); pos = next; } None => match inside.pop() { @@ -390,24 +391,26 @@ fn step(b: &[u8], pos: usize, end: usize) -> Option<(usize, Option)> { /// chunk id `first` does not, when the two walks meet at a place both pass (a position inside the /// same lists) or both end, and `alt` reads at least what `first` reads until then (R53). The page /// `alt` starts at reads with objects and without damage in its instructions. Each chunk the reader -/// uses that `first` reads before that point, `alt` reads too, unless it is a page that reads -/// without objects. The pages judged spend `left`; when it runs out, the walks do not meet. -fn rejoins(b: &[u8], end: usize, first: &Walk, alt: &Walk, budget: &Budget, left: &mut usize) -> Option> { +/// uses that `first` reads before that point, `alt` reads too, with the same body, unless it is a +/// page that reads without objects. The pages judged spend `left`; when it runs out, the walks do +/// not meet. +fn rejoins(b: &[u8], first: &Walk, alt: &Walk, budget: &Budget, left: &mut usize) -> Option> { let meet = first.at.iter().find(|q| alt.at.contains(q)).map(|(q, _)| *q).or((first.ended && alt.ended).then_some(usize::MAX))?; let uses = |q: usize| q < meet && id_at(b, q).is_some_and(|id| READ_IDS.contains(&id)); - let used = |w: &Walk| -> Vec { w.read.iter().copied().filter(|&q| uses(q)).collect() }; + let used = |w: &Walk| -> Vec<(usize, usize)> { w.read.iter().copied().filter(|&(q, _)| uses(q)).collect() }; let (kept, taken) = (used(first), used(alt)); - let start = *alt.read.first()?; - if page_reads(b, start, end, budget, left)? != (true, false) { + let (start, stop) = *alt.read.first()?; + if page_reads(b, start, stop, budget, left)? != (true, false) { return None; } - for &q in kept.iter().filter(|q| !taken.contains(q)) { - // A page that reads without objects is the only chunk the walk from `alt` may leave out. - if id_at(b, q) != Some(*b"page") || page_reads(b, q, end, budget, left)?.0 { + for &(q, stop) in kept.iter().filter(|q| !taken.contains(q)) { + // A page that reads without objects is the only chunk the walk from `alt` may leave out, + // or read with a body cut shorter by a list. + if id_at(b, q) != Some(*b"page") || page_reads(b, q, stop, budget, left)?.0 { return None; } } - Some(taken.into_iter().filter(|q| !kept.contains(q)).collect()) + Some(taken.into_iter().map(|(q, _)| q).filter(|q| !kept.iter().any(|&(k, _)| k == *q)).collect()) } /// Whether the page chunk at `at` reads with objects, and whether its instructions read with diff --git a/crates/cdr/tests/it/cmx.rs b/crates/cdr/tests/it/cmx.rs index 62583eb8f0..ec7ab7fe9f 100644 --- a/crates/cdr/tests/it/cmx.rs +++ b/crates/cdr/tests/it/cmx.rs @@ -644,6 +644,38 @@ fn after_stray_bytes_a_page_is_read_when_the_chunk_found_lands_inside_a_list() { assert_eq!(names, [name, "R"]); } +#[test] +fn after_stray_bytes_a_page_is_not_read_when_a_list_cuts_short_the_page_both_walks_reach() { + // As above, except that the list ends after the header of the page inside it, so walking on + // from the inner page reads that page without its body. Walking on from the letters reads the + // whole page, so the reader resumes at the letters and the page keeps its square. + let (name, q) = ["Q", "Qa"] + .iter() + .map(|n| { + let carried = long_instr(-1, &[b"page".as_slice(), &12u32.to_le_bytes()].concat()); + (*n, [begin_page(None), begin_layer(n, None), square(&attrs(Some(1), None, false), 0, 0, 1000), end_layer(), carried].concat()) + }) + .find(|(_, body)| body.len() % 2 == 0) + .unwrap(); + let r = page(&[begin_page(None), begin_layer("R", None), square(&attrs(Some(1), None, false), 0, 0, 2000), end_layer()]); + let mut b = vec![1u8; 2]; + b.extend(b"abcd"); + b.extend((q.len() as u32).to_le_bytes()); + b.extend(chunk(b"page", &q)); + b.extend(b"LIST"); + b.extend(12u32.to_le_bytes()); + b.extend(b"pgls"); + b.extend(r); + let mut children = vec![cont(b'4', 1e-7)]; + children.extend(tables()); + children.push(b); + let d = read_checked(&riff(&children), Limits::default()).unwrap(); + let names: Vec<&str> = d.pages.iter().filter_map(|p| p.layers.first()).map(|l| l.name.as_str()).collect(); + assert!(!names.contains(&name), "{names:?}"); + assert_eq!(names, ["R"]); + assert_eq!(d.pages.last().unwrap().layers[0].objects.len(), 1); +} + /// A page that fills with color 1, then, inside a list, letters spelling `page` whose length runs /// past the list and that hold another page and a color table of one red entry, then `after`. fn page_and_recovered_red(after: &[Vec]) -> Vec {