From 314682ef6f7a97f5bd65c000a7ebaad394ee6cac Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 21:20:45 -0800 Subject: [PATCH] EPS import: tests for LZW data, skipped image data, save limits and cvs text Adds tests for interpreter paths that no test reached: - LZWDecode: the PDF 32000 example (a code that is the entry it adds), 12 KB of data whose table grows through 9- to 12-bit codes and is cleared, and a code past the end of the table as an ioerror. - An image too large to hold whose data is LZW or run-length: the data is measured and skipped, and the art after it is read. - clipsave and gsave keep up to 1,000 saved states; the next one is a limit error. - A sampled function may hold 4096 x 4096 samples and no more (limitcheck on Size). - currenthsbcolor when green or blue is the largest channel, and gray. - cvs text of reals, booleans, names, operators and other objects, and cvrs radix text with uppercase digits, negative numbers as 32-bit two's complement, and a radix outside 2 to 36 refused. --- crates/eps/src/import/tests.rs | 33 ++++++++++++++++ crates/eps/src/import/tests_generators.rs | 16 ++++++++ crates/eps/src/import/tests_images.rs | 48 ++++++++++++++++++++++- 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/crates/eps/src/import/tests.rs b/crates/eps/src/import/tests.rs index f920a1c364..63793ff690 100644 --- a/crates/eps/src/import/tests.rs +++ b/crates/eps/src/import/tests.rs @@ -1061,6 +1061,20 @@ fn clipsave_and_cliprestore_bring_the_clip_back() { assert!(matches!(o[0].kind, NodeKind::Path { .. }), "{:?}", o[0].kind); } +/// `clipsave` and `gsave` each keep up to 1,000 saved states. One more is a limit error: the +/// square drawn before it stays, and the one after it is left out. +#[test] +fn clipsave_and_gsave_stop_at_a_thousand_saved_states() { + for (op, why) in [("clipsave", "too many saved clips"), ("gsave", "too many saved graphics states")] { + let r = read(&format!("0 0 10 10 rectfill 1000 {{ {op} }} repeat 20 20 10 10 rectfill")); + assert_eq!(objects(&r.document).len(), 2, "{op}: {:?}", r.warnings); + assert!(!r.warnings.iter().any(|w| w.contains(why)), "{op}: {:?}", r.warnings); + let r = read(&format!("0 0 10 10 rectfill 1001 {{ {op} }} repeat 20 20 10 10 rectfill")); + assert_eq!(objects(&r.document).len(), 1, "{op}: {:?}", r.warnings); + assert!(r.warnings.first().is_some_and(|w| w.contains(why)), "{op}: {:?}", r.warnings); + } +} + #[test] fn languagelevel_is_an_integer_and_runs() { // #234. @@ -1099,6 +1113,25 @@ fn intervals_share_their_arrays_and_strings() { ); } +/// `cvs` writes the text of each kind of object (PLRM 3rd ed., `cvs`): a real keeps a decimal +/// point, booleans, names and operators their names, other objects `--nostringval--`. `cvrs` +/// writes a number in a radix from 2 to 36 with uppercase digits, a negative one as its 32-bit +/// two's complement, and refuses any other radix. +#[test] +fn cvs_and_cvrs_write_the_text_of_each_kind() { + check("/s 16 string def 3.0 s cvs (3.0) eq"); + check("/s 16 string def 2.5 s cvs (2.5) eq"); + check("/s 16 string def true s cvs (true) eq"); + check("/s 16 string def /abc s cvs (abc) eq"); + check("/s 16 string def /add load s cvs (add) eq"); + check("/s 16 string def [1] s cvs (--nostringval--) eq"); + check("/s 16 string def 255 16 s cvrs (FF) eq"); + check("/s 16 string def 10 2 s cvrs (1010) eq"); + check("/s 16 string def 0 2 s cvrs (0) eq"); + check("/s 16 string def -1 16 s cvrs (FFFFFFFF) eq"); + check("/s 16 string def { 10 1 s cvrs } stopped { 10 37 s cvrs } stopped and"); +} + #[test] fn resourceforall_enumerates_matching_names() { // #234. diff --git a/crates/eps/src/import/tests_generators.rs b/crates/eps/src/import/tests_generators.rs index 63859b3e52..17df25cd9c 100644 --- a/crates/eps/src/import/tests_generators.rs +++ b/crates/eps/src/import/tests_generators.rs @@ -861,6 +861,11 @@ fn probing_operators_answer() { "/n 0 def 0 0 moveto 10 0 lineto { pop pop /n n 1 add def } { pop pop /n n 2 add def } { 6 { pop } repeat } {} pathforall n 3 eq", ); super::tests::check("1 0 0 setrgbcolor currenthsbcolor 1 eq exch 1 eq and exch 0 eq and"); + // Green largest: hue (2 + (b - r) / d) / 6 = 5/12; blue largest: (4 + (r - g) / d) / 6 = 9/12; + // each with saturation 0.8 and brightness 1. Gray has no hue and no saturation. + super::tests::check("0.2 1 0.6 setrgbcolor currenthsbcolor 1 eq exch 0.8 sub abs 1e-6 lt and exch 12 mul 5 sub abs 1e-5 lt and"); + super::tests::check("0.6 0.2 1 setrgbcolor currenthsbcolor 1 eq exch 0.8 sub abs 1e-6 lt and exch 12 mul 9 sub abs 1e-5 lt and"); + super::tests::check("0.5 setgray currenthsbcolor 0.5 sub abs 1e-6 lt exch 0 eq and exch 0 eq and"); super::tests::check("currentcacheparams counttomark 2 eq exch pop exch pop exch pop"); super::tests::check("cachestatus 7 { pop } repeat true"); super::tests::check("currentcolorrendering /ColorRenderingType known"); @@ -997,6 +1002,17 @@ fn hostile_patterns_glyphs_and_shadings_end() { /Function << /FunctionType 0 /Domain [0 1] /Range [0 1] /Size [99999999] /BitsPerSample 8 /DataSource () >> >> shfill", ); assert!(r.warnings[0].contains("rangecheck in `shfill`: a sampled function"), "{:?}", r.warnings); + // A sampled function of two inputs may hold 2^24 samples (4096 × 4096), and no more. + let sized = |size: &str| { + read(&format!( + "0 0 1 1 rectfill << /ShadingType 1 /ColorSpace /DeviceGray \ + /Function << /FunctionType 0 /Domain [0 1 0 1] /Range [0 1] /Size [{size}] /BitsPerSample 8 /DataSource () >> >> shfill" + )) + }; + let r = sized("4096 4096"); + assert!(!r.warnings.iter().any(|w| w.contains("limitcheck")), "{:?}", r.warnings); + let r = sized("4096 4097"); + assert!(r.warnings[0].contains("limitcheck in `shfill`: Size"), "{:?}", r.warnings); // Procedures nesting through operators that run them end at the nesting limit. for body in ["/f { { f } exec } def f", "/f { true { f } if } def f", "/f { 1 { f } repeat } def f", "/f { { f } stopped } def f"] { assert!(import(&super::tests::eps(body)).is_err(), "{body}"); diff --git a/crates/eps/src/import/tests_images.rs b/crates/eps/src/import/tests_images.rs index 87a7560bfd..2324de4eb4 100644 --- a/crates/eps/src/import/tests_images.rs +++ b/crates/eps/src/import/tests_images.rs @@ -148,6 +148,35 @@ fn filters_read_the_file_they_were_made_over() { ); } +/// LZW data (`LZWDecode`, `EarlyChange` 1, PDF 32000 §7.4.4.2) decodes: the specification's +/// example, whose third code is the entry that code adds; data long enough that the table grows +/// past 511, 1023 and 2047 entries (9-, 10-, 11- and 12-bit codes) and is cleared and grown again; +/// and a code past the end of the table is an `ioerror`, the art after it left out. +#[test] +fn lzw_data_decodes_at_every_code_width() { + super::tests::check("<800B6050220C0C8501> /LZWDecode filter 20 string readstring pop (-----A---B) eq"); + let mut x = 0x2545_F491_u32; + let data: Vec = (0..12_000) + .map(|_| { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + (x >> 24) as u8 + }) + .collect(); + let lzw = craft_stream_codecs::lzw::encode(&data, true); + super::tests::check(&format!( + "/f <{}> /LZWDecode filter def f {} string readstring exch <{}> eq and f read {{ pop false }} {{ true }} ifelse and", + hex(&lzw), + data.len(), + hex(&data) + )); + // A clear-table code, then code 300 while the table ends at 257. + let r = read("0 0 10 10 rectfill <804B00> /LZWDecode filter 4 string readstring 20 20 10 10 rectfill"); + assert_eq!(super::tests::objects(&r.document).len(), 1, "{:?}", r.warnings); + assert!(r.warnings.iter().any(|w| w.contains("ioerror") && w.contains("LZWDecode")), "{:?}", r.warnings); +} + /// CMYK samples keep their ink amounts in a CMYK TIFF: in the operand and dictionary forms, from /// hex and binary data, from one source or one a component, at 4, 8 and 16 bits, read through /// the Decode array. Gray and RGB images stay PNGs. @@ -656,7 +685,8 @@ fn an_image_of_more_data_than_the_reader_holds_keeps_only_the_rows_it_reads() { /// An image too large to read even at a lower resolution (its samples are more data than the /// reader holds) is left out, and its data is skipped: when a procedure reads it, the procedure /// is called until the data ends; when its first filter decompresses to more than the reader -/// holds (and can't be read a row at a time), the compressed data is measured without keeping it. +/// holds (and can't be read a row at a time), the compressed data (Flate, LZW or run-length) is +/// measured without keeping it. #[test] fn the_data_of_an_image_too_large_to_hold_is_skipped() { let after = "0 0 1 setrgbcolor 10 10 80 80 rectfill"; @@ -666,6 +696,20 @@ fn the_data_of_an_image_too_large_to_hold_is_skipped() { b"10000 10000 8 [1 0 0 1 0 0] currentfile << /Predictor 15 /Colors 3 /Columns 10000 >> /FlateDecode filter false 3 colorimage\n".to_vec(); flate.extend(&zeros); flate.extend(format!("\n{after}").as_bytes()); + // The same through LZW, which counts the codes up to its end-of-data code. + let mut lzw = + b"10000 10000 8 [1 0 0 1 0 0] currentfile << /Predictor 15 /Colors 3 /Columns 10000 >> /LZWDecode filter false 3 colorimage\n".to_vec(); + lzw.extend(craft_stream_codecs::lzw::encode(&vec![0u8; 300 << 20], true)); + lzw.extend(format!("\n{after}").as_bytes()); + // Run-length data isn't decoded a row at a time either: a literal run of three bytes, runs of + // 128 bytes, and the end-of-data byte (128) after which the art follows. The literal ends in + // 200 and every run repeats 200, so a count that lands on a wrong byte reads each 200 as a + // run's length and stays off by one. + let mut run_length = b"10000 10000 8 [1 0 0 1 0 0] currentfile /RunLengthDecode filter false 3 colorimage\n".to_vec(); + run_length.extend([2, 200, 200, 200]); + run_length.extend([129u8, 200].repeat((300 << 20) / 128)); + run_length.push(128); + run_length.extend(format!("\n{after}").as_bytes()); for program in [ format!( "/F currentfile 0 (%%EndData) /SubFileDecode filter def \ @@ -673,6 +717,8 @@ fn the_data_of_an_image_too_large_to_hold_is_skipped() { ) .into_bytes(), flate, + lzw, + run_length, ] { let text = String::from_utf8_lossy(program.get(..120).unwrap_or(&program)).into_owned(); let mut file = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 100\n%%EndComments\n".to_vec();